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

The following examples show how to use androidx.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: TabLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Selects the specified tab in the <code>TabLayout</code>. */
public static ViewAction selectTab(final int tabIndex) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayingAtLeast(90);
    }

    @Override
    public String getDescription() {
      return "Selects tab";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();

      TabLayout tabLayout = (TabLayout) view;
      tabLayout.getTabAt(tabIndex).select();

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 2
Source File: DrawerActions.java    From android-test with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an action which closes the {@link DrawerLayout} with the gravity. This method blocks
 * until the drawer is fully closed. No operation if the drawer is already closed.
 */
// TODO alias to closeDrawer before 3.0 and deprecate this method.
public static ViewAction close(final int gravity) {
  return new DrawerAction() {
    @Override
    public String getDescription() {
      return "close drawer with gravity " + gravity;
    }

    @Override
    protected Matcher<View> checkAction() {
      return isOpen(gravity);
    }

    @Override
    protected void performAction(UiController uiController, DrawerLayout view) {
      view.closeDrawer(gravity);
      uiController.loopMainThreadUntilIdle();
      // If still open wait some more...
      if (view.isDrawerVisible(gravity)) {
        uiController.loopMainThreadForAtLeast(300);
      }
    }
  };
}
 
Example 3
Source File: NavigationViewActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Removes a previously added header view from the navigation view. */
public static ViewAction removeHeaderView(final @Nullable View headerView) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

    @Override
    public String getDescription() {
      return "Remove header view";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();

      NavigationView navigationView = (NavigationView) view;
      navigationView.removeHeaderView(headerView);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 4
Source File: NavigationViewActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets icon for the menu item of the navigation view. */
public static ViewAction setIconForMenuItem(
    final @IdRes int menuItemId, final Drawable iconDrawable) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

    @Override
    public String getDescription() {
      return "Set menu item icon";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();

      NavigationView navigationView = (NavigationView) view;
      navigationView.getMenu().findItem(menuItemId).setIcon(iconDrawable);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 5
Source File: NavigationViewActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Add the specified view as a header to the navigation view. */
public static ViewAction addHeaderView(
    final @NonNull LayoutInflater inflater, final @LayoutRes int res) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

    @Override
    public String getDescription() {
      return "Add header view";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();

      NavigationView navigationView = (NavigationView) view;
      navigationView.addHeaderView(inflater.inflate(res, null, false));

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 6
Source File: NavigationViewActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets item icon tint list on the content of the navigation view. */
public static ViewAction setItemIconTintList(final @Nullable ColorStateList tint) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

    @Override
    public String getDescription() {
      return "Set item icon tint list";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();

      NavigationView navigationView = (NavigationView) view;
      navigationView.setItemIconTintList(tint);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 7
Source File: NavigationViewActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets item background on the content of the navigation view. */
public static ViewAction setItemBackgroundResource(final @DrawableRes int resId) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

    @Override
    public String getDescription() {
      return "Set item background";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();

      NavigationView navigationView = (NavigationView) view;
      navigationView.setItemBackgroundResource(resId);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 8
Source File: TestUtilsActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * Dummy Espresso action that waits until the UI thread is idle. This action can be performed on
 * the root view to wait for an ongoing animation to be completed.
 */
public static ViewAction waitUntilIdle() {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isRoot();
    }

    @Override
    public String getDescription() {
      return "wait for idle";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 9
Source File: TestUtilsActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setEnabled(final boolean enabled) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

    @Override
    public String getDescription() {
      return "set enabled";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();

      view.setEnabled(enabled);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 10
Source File: ViewPagerActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Moves <code>ViewPager</code> to specific page. */
public static ViewAction scrollToPage(final int page) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayingAtLeast(90);
    }

    @Override
    public String getDescription() {
      return "ViewPager move to a specific page";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();

      ViewPager viewPager = (ViewPager) view;
      viewPager.setCurrentItem(page, false);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 11
Source File: MediaSessionPlaybackActivityTest.java    From media-samples 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 12
Source File: TabLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * Calls <code>setScrollPosition(position, positionOffset, true)</code> on the <code>TabLayout
 * </code>
 */
public static ViewAction setScrollPosition(final int position, final float positionOffset) {
  return new ViewAction() {

    @Override
    public Matcher<View> getConstraints() {
      return ViewMatchers.isAssignableFrom(TabLayout.class);
    }

    @Override
    public String getDescription() {
      return "setScrollPosition(" + position + ", " + positionOffset + ", true)";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TabLayout tabs = (TabLayout) view;
      tabs.setScrollPosition(position, positionOffset, true);
      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 13
Source File: TabLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Wires <code>TabLayout</code> to <code>ViewPager</code> content. */
public static ViewAction setupWithViewPager(final @Nullable ViewPager viewPager) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayingAtLeast(90);
    }

    @Override
    public String getDescription() {
      return "Setup with ViewPager content";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();

      TabLayout tabLayout = (TabLayout) view;
      tabLayout.setupWithViewPager(viewPager);
      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 14
Source File: TestUtilsActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Adds tabs to {@link TabLayout} */
public static ViewAction addTabs(final String... tabs) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TabLayout.class);
    }

    @Override
    public String getDescription() {
      return "TabLayout add tabs";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();

      TabLayout tabLayout = (TabLayout) view;
      for (int i = 0; i < tabs.length; i++) {
        tabLayout.addTab(tabLayout.newTab().setText(tabs[i]));
      }

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 15
Source File: BaseDynamicCoordinatorLayoutTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
protected ViewAction setLayoutDirection(final int layoutDir) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return any(View.class);
    }

    @Override
    public String getDescription() {
      return "Sets layout direction";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();

      ViewCompat.setLayoutDirection(view, layoutDir);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 16
Source File: KeyEventActionBase.java    From android-test with Apache License 2.0 5 votes vote down vote up
static void waitForPendingForegroundActivities(UiController controller, boolean conditional) {
  ActivityLifecycleMonitor activityLifecycleMonitor =
      ActivityLifecycleMonitorRegistry.getInstance();
  boolean pendingForegroundActivities = false;
  for (int attempts = 0; attempts < CLEAR_TRANSITIONING_ACTIVITIES_ATTEMPTS; attempts++) {
    controller.loopMainThreadUntilIdle();
    pendingForegroundActivities = hasTransitioningActivities(activityLifecycleMonitor);
    if (pendingForegroundActivities) {
      controller.loopMainThreadForAtLeast(CLEAR_TRANSITIONING_ACTIVITIES_MILLIS_DELAY);
    } else {
      break;
    }
  }

  // Pressing back can kill the app: log a warning.
  if (!hasForegroundActivities(activityLifecycleMonitor)) {
    if (conditional) {
      throw new NoActivityResumedException("Pressed back and killed the app");
    }
    Log.w(TAG, "Pressed back and hopped to a different process or potentially killed the app");
  }

  if (pendingForegroundActivities) {
    Log.w(
        TAG,
        "Back was pressed and left the application in an inconsistent state even after "
            + (CLEAR_TRANSITIONING_ACTIVITIES_MILLIS_DELAY
                * CLEAR_TRANSITIONING_ACTIVITIES_ATTEMPTS)
            + "ms.");
  }
}
 
Example 17
Source File: ViewPagerActions.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
public final void perform(UiController uiController, View view) {
  final ViewPager viewPager = (ViewPager) view;
  // 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();

    performScroll((ViewPager) view);

    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 18
Source File: CloseKeyboardAction.java    From android-test with Apache License 2.0 5 votes vote down vote up
private void tryToCloseKeyboard(View view, UiController uiController) throws TimeoutException {
  InputMethodManager imm =
      (InputMethodManager)
          getRootActivity(uiController).getSystemService(Context.INPUT_METHOD_SERVICE);

  CloseKeyboardIdlingResult idlingResult =
      new CloseKeyboardIdlingResult(new Handler(Looper.getMainLooper()));

  IdlingRegistry.getInstance().register(idlingResult);

  try {

    if (!imm.hideSoftInputFromWindow(view.getWindowToken(), 0, idlingResult)) {
      Log.w(TAG, "Attempting to close soft keyboard, while it is not shown.");
      return;
    }
    // set 2 second timeout
    idlingResult.scheduleTimeout(2000);
    uiController.loopMainThreadUntilIdle();
    if (idlingResult.timedOut) {
      throw new TimeoutException("Wait on operation result timed out.");
    }
  } finally {
    IdlingRegistry.getInstance().unregister(idlingResult);
  }

  if (idlingResult.result != InputMethodManager.RESULT_UNCHANGED_HIDDEN
      && idlingResult.result != InputMethodManager.RESULT_HIDDEN) {
    String error =
        "Attempt to close the soft keyboard did not result in soft keyboard to be hidden."
            + " resultCode = "
            + idlingResult.result;
    Log.e(TAG, error);
    throw new PerformException.Builder()
        .withActionDescription(this.getDescription())
        .withViewDescription(HumanReadables.describe(view))
        .withCause(new RuntimeException(error))
        .build();
  }
}
 
Example 19
Source File: OrientationChangeAction.java    From LocaleChanger with Apache License 2.0 4 votes vote down vote up
@Override
public void perform(UiController uiController, View view) {
    uiController.loopMainThreadUntilIdle();
    final Activity activity = EspressoUtil.getCurrentActivity();
    activity.setRequestedOrientation(orientation);
}
 
Example 20
Source File: Utils.java    From BottomNavigation with Apache License 2.0 4 votes vote down vote up
/**
 * 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)) {
                    // 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();
        }
    };
}