'Android Espresso waiting for view, R.id value keeps changing

in many parts of our app we have some videos. and in my tests i am waiting for the video to be finished with this:

onView(isRoot()).perform(waitId(R.id.activity_generic_video_video_control_next, 80000));

waitId() is a function I snagged off of a post here on StackOverflow, and it worked very well for a long time. Now for some reason it's not working and I am not sure when this broke or why.

/** Perform action of waiting for a specific view id. */
    public static ViewAction waitId(final int viewId, final long millis) {
        return new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return isRoot();
            }

            @Override
            public String getDescription() {
                return "wait for a specific view with id <" + viewId + "> during " + millis + " millis.";
            }

            @Override
            public void perform(final UiController uiController, final View view) {
                uiController.loopMainThreadUntilIdle();
                final long startTime = System.currentTimeMillis();
                final long endTime = startTime + millis;
                final Matcher<View> viewMatcher = withId(viewId);

                do {
                    for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                        if (child.getId() != -1) {
                            Log.d(TAG, "child id " + child.getId() + " vs " + viewId + " vs " + R.id.activity_generic_video_video_control);
                        }

                        // found view with required ID and it's visible
                        if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) {
                            return;
                        }
                    }

                    uiController.loopMainThreadForAtLeast(50);
                }
                while (System.currentTimeMillis() < endTime);

                // timeout happens
                throw new PerformException.Builder()
                        .withActionDescription(this.getDescription())
                        .withViewDescription(HumanReadables.describe(view))
                        .withCause(new TimeoutException())
                        .build();
            }
        };
    }

As you can see, the code loops for a period of time until the view in question is visible. The interesting part is I am getting this output from my log statement in the for loop:

child id 2131363297 vs 2131362267 vs 2131362265

The first # is the ID of the view given why traversing the display tree. The second # is the ID that is passed into the function. The third # is the ID of the same view while referencing the R.id directly.

I can say that at least the 2nd and 3rd numbers should be the same, but they aren't. What's going on? Why is the id value changing?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source