Java Code Examples for android.support.test.espresso.UiController#loopMainThreadUntilIdle()

The following examples show how to use android.support.test.espresso.UiController#loopMainThreadUntilIdle() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestUtils.java    From Awesome-WanAndroid with Apache License 2.0 6 votes vote down vote up
public void perform(UiController uiController, View view) {
    RecyclerView recyclerView = (RecyclerView) view;
    (new ScrollToPositionViewAction(this.position)).perform(uiController, view);
    uiController.loopMainThreadUntilIdle();

    View targetView = recyclerView.getChildAt(this.position).findViewById(this.viewId);

    if (targetView == null) {
        throw (new PerformException.Builder()).withActionDescription(this.toString())
                .withViewDescription(

                        HumanReadables.describe(view))
                .withCause(new IllegalStateException(
                        "No view with id "
                                + this.viewId
                                + " found at position: "
                                + this.position))
                .build();
    } else {
        this.viewAction.perform(uiController, targetView);
    }
}
 
Example 2
Source File: MoreViewActions.java    From yandex-money-sdk-android with MIT License 6 votes vote down vote up
@Override
public final void perform(UiController uiController, View view) {
    uiController.loopMainThreadUntilIdle();

    long finishTime = System.currentTimeMillis() + duration;
    while (System.currentTimeMillis() < finishTime) {
        if (isConditionMet(view)) {
            return;
        }
        uiController.loopMainThreadForAtLeast(50L);
    }

    throw new PerformException.Builder()
            .withActionDescription(this.getDescription())
            .withViewDescription(HumanReadables.describe(view))
            .withCause(new TimeoutException())
            .build();
}
 
Example 3
Source File: ViewActions.java    From u2020-mvp with Apache License 2.0 6 votes vote down vote up
/**
 * Perform action of waiting until UI thread is free.
 * <p/>
 * E.g.:
 * onView(isRoot()).perform(waitUntilIdle());
 *
 * @return
 */
public static ViewAction waitUntilIdle() {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return anything();
        }

        @Override
        public String getDescription() {
            return "wait until UI thread is free";
        }

        @Override
        public void perform(final UiController uiController, final View view) {
            uiController.loopMainThreadUntilIdle();
        }
    };
}
 
Example 4
Source File: ViewActions.java    From u2020-mvp with Apache License 2.0 6 votes vote down vote up
/**
 * Perform action of waiting for a specific time. Useful when you need
 * to wait for animations to end and Espresso fails at waiting.
 * <p/>
 * E.g.:
 * onView(isRoot()).perform(waitAtLeast(Sampling.SECONDS_15));
 *
 * @param millis
 * @return
 */
public static ViewAction waitAtLeast(final long millis) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return anything();
        }

        @Override
        public String getDescription() {
            return "wait for at least " + millis + " millis.";
        }

        @Override
        public void perform(final UiController uiController, final View view) {
            uiController.loopMainThreadUntilIdle();
            uiController.loopMainThreadForAtLeast(millis);
        }
    };
}
 
Example 5
Source File: ExtViewActions.java    From PrettyBundle with Apache License 2.0 6 votes vote down vote up
/**
 * Perform action of waiting until UI thread is free.
 * <p/>
 * E.g.:
 * onView(isRoot()).perform(waitUntilIdle());
 *
 * @return
 */
public static ViewAction waitUntilIdle() {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return anything();
        }

        @Override
        public String getDescription() {
            return "wait until UI thread is free";
        }

        @Override
        public void perform(final UiController uiController, final View view) {
            uiController.loopMainThreadUntilIdle();
        }
    };
}
 
Example 6
Source File: ExtViewActions.java    From PrettyBundle with Apache License 2.0 6 votes vote down vote up
/**
 * Perform action of waiting for a specific time. Useful when you need
 * to wait for animations to end and Espresso fails at waiting.
 * <p/>
 * E.g.:
 * onView(isRoot()).perform(waitAtLeast(Sampling.SECONDS_15));
 *
 * @param millis
 * @return
 */
public static ViewAction waitAtLeast(final long millis) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return anything();
        }

        @Override
        public String getDescription() {
            return "wait for at least " + millis + " millis.";
        }

        @Override
        public void perform(final UiController uiController, final View view) {
            uiController.loopMainThreadUntilIdle();
            uiController.loopMainThreadForAtLeast(millis);
        }
    };
}
 
Example 7
Source File: TestUtils.java    From espresso-samples with Apache License 2.0 6 votes vote down vote up
public void perform(UiController uiController, View view) {
    RecyclerView recyclerView = (RecyclerView) view;
    (new ScrollToPositionViewAction(this.position)).perform(uiController, view);
    uiController.loopMainThreadUntilIdle();

    View targetView = recyclerView.getChildAt(this.position).findViewById(this.viewId);

    if (targetView == null) {
        throw (new PerformException.Builder()).withActionDescription(this.toString())
                                              .withViewDescription(

                                                  HumanReadables.describe(view))
                                              .withCause(new IllegalStateException(
                                                  "No view with id "
                                                      + this.viewId
                                                      + " found at position: "
                                                      + this.position))
                                              .build();
    } else {
        this.viewAction.perform(uiController, targetView);
    }
}
 
Example 8
Source File: TestUtils.java    From Scoops with Apache License 2.0 6 votes vote down vote up
public void perform(UiController uiController, View view) {
    RecyclerView recyclerView = (RecyclerView) view;
    (new ScrollToPositionViewAction(this.position)).perform(uiController, view);
    uiController.loopMainThreadUntilIdle();

    View targetView = recyclerView.getChildAt(this.position).findViewById(this.viewId);

    if (targetView == null) {
        throw (new PerformException.Builder()).withActionDescription(this.toString())
                .withViewDescription(

                        HumanReadables.describe(view))
                .withCause(new IllegalStateException(
                        "No view with id "
                                + this.viewId
                                + " found at position: "
                                + this.position))
                .build();
    } else {
        this.viewAction.perform(uiController, targetView);
    }
}
 
Example 9
Source File: MainActivityTest.java    From android-PictureInPicture with Apache License 2.0 6 votes vote down vote up
private static ViewAction showControls() {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isAssignableFrom(MovieView.class);
        }

        @Override
        public String getDescription() {
            return "Show controls of MovieView";
        }

        @Override
        public void perform(UiController uiController, View view) {
            uiController.loopMainThreadUntilIdle();
            ((MovieView) view).showControls();
            uiController.loopMainThreadUntilIdle();
        }
    };
}
 
Example 10
Source File: MediaSessionPlaybackActivityTest.java    From android-PictureInPicture with Apache License 2.0 6 votes vote down vote up
private static ViewAction showControls() {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isAssignableFrom(MovieView.class);
        }

        @Override
        public String getDescription() {
            return "Show controls of MovieView";
        }

        @Override
        public void perform(UiController uiController, View view) {
            uiController.loopMainThreadUntilIdle();
            ((MovieView) view).showControls();
            uiController.loopMainThreadUntilIdle();
        }
    };
}
 
Example 11
Source File: EspressoTools.java    From android-step-by-step with Apache License 2.0 5 votes vote down vote up
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)) {
                    // found view with required ID
                    if (viewMatcher.matches(child)) {
                        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();
        }
    };
}
 
Example 12
Source File: StepperNavigationActions.java    From android-material-stepper with Apache License 2.0 5 votes vote down vote up
@Override
public final void perform(UiController uiController, View view) {
    final StepperLayout stepperLayout = (StepperLayout) view;
    final ViewPager viewPager = (ViewPager) stepperLayout.findViewById(com.stepstone.stepper.R.id.ms_stepPager);
    // Add a custom tracker listener
    final CustomViewPagerListener customListener = new CustomViewPagerListener();
    viewPager.addOnPageChangeListener(customListener);

    // Note that we're running the following block in a try-finally construct. This
    // is needed since some of the actions are going to throw (expected) exceptions. If that
    // happens, we still need to clean up after ourselves to leave the system (Espresso) in a good
    // state.
    try {
        // Register our listener as idling resource so that Espresso waits until the
        // wrapped action results in the view pager getting to the STATE_IDLE state
        Espresso.registerIdlingResources(customListener);

        uiController.loopMainThreadUntilIdle();

        performAction(stepperLayout);

        uiController.loopMainThreadUntilIdle();

        customListener.mNeedsIdle = true;
        uiController.loopMainThreadUntilIdle();
        customListener.mNeedsIdle = false;
    } finally {
        // Unregister our idling resource
        Espresso.unregisterIdlingResources(customListener);
        // And remove our tracker listener from ViewPager
        viewPager.removeOnPageChangeListener(customListener);
    }
}
 
Example 13
Source File: CustomRecyclerViewActions.java    From android-espresso-revealed with Apache License 2.0 5 votes vote down vote up
@Override
public void perform(UiController uiController, View view) {
    RecyclerView recyclerView = (RecyclerView) view;
    int itemCount = recyclerView.getAdapter().getItemCount();
    try {
        recyclerView.scrollToPosition(itemCount - 1);
        uiController.loopMainThreadUntilIdle();
    } catch (RuntimeException e) {
        throw new PerformException.Builder().withActionDescription(this.getDescription())
                .withViewDescription(HumanReadables.describe(view)).withCause(e).build();
    }
}
 
Example 14
Source File: OrientationChangeAction.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
@Override
public void perform(UiController uiController, View view) {
  uiController.loopMainThreadUntilIdle();
  Activity activity = getActivity(view.getContext());
  if (activity == null && view instanceof ViewGroup) {
    ViewGroup viewGroup = (ViewGroup) view;
    int childCount = viewGroup.getChildCount();
    for (int i = 0; i < childCount && activity == null; ++i) {
      activity = getActivity(viewGroup.getChildAt(i).getContext());
    }
  }
  activity.setRequestedOrientation(orientation);
}
 
Example 15
Source File: OrientationChangeAction.java    From PhilHackerNews with MIT License 4 votes vote down vote up
@Override
public void perform(UiController uiController, View view) {
    uiController.loopMainThreadUntilIdle();
    final Activity activity = (Activity) view.getContext();
    activity.setRequestedOrientation(orientation);
}
 
Example 16
Source File: ExtViewActions.java    From PrettyBundle with Apache License 2.0 4 votes vote down vote up
/**
 * Perform action of waiting for a specific view id.
 * <p/>
 * E.g.:
 * onView(isRoot()).perform(waitId(R.id.dialogEditor, Sampling.SECONDS_15));
 *
 * @param viewId
 * @param millis
 * @return
 */
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)) {
                    // found view with required ID
                    if (viewMatcher.matches(child)) {
                        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();
        }
    };
}
 
Example 17
Source File: NestedScroll.java    From px-android with MIT License 4 votes vote down vote up
public static ViewAction nestedScrollTo() {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return allOf(isDescendantOfA(isAssignableFrom(NestedScrollView.class)), withEffectiveVisibility(
                ViewMatchers.Visibility.VISIBLE));
        }

        @Override
        public String getDescription() {
            return "Scrolling to view inside NestedScrollView";
        }

        @Override
        public void perform(final UiController uiController, final View view) {
            try {
                final NestedScrollView nestedScrollView = (NestedScrollView)
                    findFirstParentLayoutOfClass(view, NestedScrollView.class);
                if (nestedScrollView != null) {
                    final CoordinatorLayout coordinatorLayout =
                        (CoordinatorLayout) findFirstParentLayoutOfClass(view, CoordinatorLayout.class);
                    if (coordinatorLayout != null) {
                        final CollapsingToolbarLayout collapsingToolbarLayout =
                            findCollapsingToolbarLayoutChildIn(coordinatorLayout);
                        if (collapsingToolbarLayout != null) {
                            final int toolbarHeight = collapsingToolbarLayout.getHeight();
                            nestedScrollView.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
                            nestedScrollView.dispatchNestedPreScroll(0, toolbarHeight, null, null);
                        }
                    }
                    nestedScrollView.scrollTo(0, view.getTop());
                } else {
                    throw new Exception("Unable to find NestedScrollView parent.");
                }
            } catch (final Exception e) {
                throw new PerformException.Builder()
                    .withActionDescription(this.getDescription())
                    .withViewDescription(HumanReadables.describe(view))
                    .withCause(e)
                    .build();
            }
            uiController.loopMainThreadUntilIdle();
        }
    };
}
 
Example 18
Source File: ViewActions.java    From u2020-mvp with Apache License 2.0 4 votes vote down vote up
/**
 * Perform action of waiting for a specific view id.
 * <p/>
 * E.g.:
 * onView(isRoot()).perform(waitId(R.id.dialogEditor, Sampling.SECONDS_15));
 *
 * @param viewId
 * @param millis
 * @return
 */
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)) {
                    // found view with required ID
                    if (viewMatcher.matches(child)) {
                        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();
        }
    };
}