androidx.test.espresso.UiController Java Examples

The following examples show how to use androidx.test.espresso.UiController. 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: PullDownToRefreshTest.java    From focus-android with Mozilla Public License 2.0 6 votes vote down vote up
public static ViewAction withCustomConstraints(final ViewAction action, final Matcher<View> constraints) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return constraints;
        }

        @Override
        public String getDescription() {
            return action.getDescription();
        }

        @Override
        public void perform(UiController uiController, View view) {
            action.perform(uiController, view);
        }
    };
}
 
Example #2
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setHelperTextEnabled(final boolean enabled) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TextInputLayout.class);
    }

    @Override
    public String getDescription() {
      return "Enables/disables the helper";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setHelperTextEnabled(enabled);
    }
  };
}
 
Example #3
Source File: FloatingActionButtonActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setBackgroundTintList(@ColorInt final ColorStateList tint) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(FloatingActionButton.class);
    }

    @Override
    public String getDescription() {
      return "Sets FloatingActionButton background tint";
    }

    @Override
    public void perform(UiController uiController, View view) {
      final FloatingActionButton fab = (FloatingActionButton) view;
      fab.setBackgroundTintList(tint);
    }
  };
}
 
Example #4
Source File: NavigationViewActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * Inflates a view from the specified layout ID and adds it as a header to the navigation view.
 */
public static ViewAction inflateHeaderView(final @LayoutRes int res) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

    @Override
    public String getDescription() {
      return "Inflate and add header view";
    }

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

      NavigationView navigationView = (NavigationView) view;
      navigationView.inflateHeaderView(res);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #5
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the text field's focused stroke width.
 *
 * @param strokeWidthFocused the value to use for the text field box's stroke when focused
 * @return the action of setting the box's focused stroke width on a {@link TextInputLayout}
 */
public static ViewAction setBoxStrokeWidthFocused(final int strokeWidthFocused) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TextInputLayout.class);
    }

    @Override
    public String getDescription() {
      return "Sets the box's stroke width when focused.";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setBoxStrokeWidthFocused(strokeWidthFocused);
    }
  };
}
 
Example #6
Source File: NavigationViewActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets item text appearance on the content of the navigation view. */
public static ViewAction setItemTextAppearance(final @StyleRes int resId) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

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

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

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

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #7
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setStartIconContentDescription(final CharSequence contentDesc) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TextInputLayout.class);
    }

    @Override
    public String getDescription() {
      return "Set a content description for the start icon";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setStartIconContentDescription(contentDesc);
    }
  };
}
 
Example #8
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setErrorContentDescription(final CharSequence errorContentDesc) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TextInputLayout.class);
    }

    @Override
    public String getDescription() {
      return "Sets the error message's content description";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setErrorContentDescription(errorContentDesc);
    }
  };
}
 
Example #9
Source File: TestUtilsActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets layout direction on the view. */
public static ViewAction setLayoutDirection(final int layoutDirection) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

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

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

      ViewCompat.setLayoutDirection(view, layoutDirection);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #10
Source File: CustomChecks.java    From fullscreen-video-view with Apache License 2.0 6 votes vote down vote up
static ViewAction clickNoConstraints() {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isEnabled(); // No constraints, isEnabled and isClickable are checked
        }

        @Override
        public String getDescription() {
            return "Click a view with no constraints.";
        }

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

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

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

      view.setClickable(clickable);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #12
Source File: TabLayoutWithViewPagerTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
private static <Q> ViewAction removeItemAtIndexFromPager(final int index) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(ViewPager.class);
    }

    @Override
    public String getDescription() {
      return "Remove item at specified index and notify on content change";
    }

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

      final ViewPager viewPager = (ViewPager) view;
      @SuppressWarnings("unchecked") // no way to avoid this cast
      final BasePagerAdapter<Q> viewPagerAdapter = (BasePagerAdapter<Q>) viewPager.getAdapter();
      viewPagerAdapter.remove(index);
      viewPagerAdapter.notifyDataSetChanged();

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #13
Source File: BottomNavigationViewActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Add a navigation item to the bottom navigation view. */
public static ViewAction addMenuItem(final String title) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

    @Override
    public String getDescription() {
      return "Add item with title" + title;
    }

    @Override
    public void perform(UiController uiController, View view) {
      BottomNavigationView navigationView = (BottomNavigationView) view;
      navigationView.getMenu().add(title);
    }
  };
}
 
Example #14
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setPlaceholderText(final CharSequence placeholder) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TextInputLayout.class);
    }

    @Override
    public String getDescription() {
      return "Sets the placeholder";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setPlaceholderText(placeholder);
    }
  };
}
 
Example #15
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 #16
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setCounterMaxLength(final int maxLength) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TextInputLayout.class);
    }

    @Override
    public String getDescription() {
      return "Sets the counter max length";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setCounterMaxLength(maxLength);
    }
  };
}
 
Example #17
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets prefix. */
public static ViewAction setPrefixText(final CharSequence prefixText) {
  return new ViewAction() {

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

    @Override
    public String getDescription() {
      return "Sets prefix text.";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setPrefixText(prefixText);
    }
  };
}
 
Example #18
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setStartIcon(final Drawable startIconDrawable) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TextInputLayout.class);
    }

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

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setStartIconDrawable(startIconDrawable);
    }
  };
}
 
Example #19
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setHelperTextTextAppearance(final int resId) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TextInputLayout.class);
    }

    @Override
    public String getDescription() {
      return "Sets the helper text appearance";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setHelperTextTextAppearance(resId);
    }
  };
}
 
Example #20
Source File: TestUtilsActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setTabMode(final int tabMode) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TabLayout.class);
    }

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

    @Override
    public void perform(UiController uiController, View view) {
      ((TabLayout) view).setTabMode(tabMode);
    }
  };
}
 
Example #21
Source File: RepeatActionUntilViewState.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Override
public void perform(UiController uiController, View view) {
  int noOfAttempts = 1;
  for (; !mDesiredStateMatcher.matches(view) && noOfAttempts <= mMaxAttempts; noOfAttempts++) {
    mAction.perform(uiController, view);
    uiController.loopMainThreadUntilIdle();
  }
  if (noOfAttempts > mMaxAttempts) {
    throw new PerformException.Builder()
        .withActionDescription(this.getDescription())
        .withViewDescription(HumanReadables.describe(view))
        .withCause(
            new RuntimeException(
                String.format(
                    Locale.ROOT, "Failed to achieve view state after %d attempts", mMaxAttempts)))
        .build();
  }
}
 
Example #22
Source File: BottomNavigationViewActions.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(
    @IdRes final 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) {
      BottomNavigationView navigationView = (BottomNavigationView) view;
      navigationView.getMenu().findItem(menuItemId).setIcon(iconDrawable);
    }
  };
}
 
Example #23
Source File: TestUtilsActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets system ui visibility and waits for the change to complete. */
public static ViewAction setSystemUiVisibility(final int sysUiVisibility) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

    @Override
    public String getDescription() {
      return "Sets system ui visibility";
    }

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

      view.setSystemUiVisibility(sysUiVisibility);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #24
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Long clicks end or start icon. */
public static ViewAction longClickIcon(final boolean isEndIcon) {
  return new ViewAction() {

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

    @Override
    public String getDescription() {
      return "Long clicks the end or start icon";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout item = (TextInputLayout) view;
      // Reach in and find the icon view since we don't have a public API to get a reference to it
      CheckableImageButton iconView =
          item.findViewById(isEndIcon ? R.id.text_input_end_icon : R.id.text_input_start_icon);
      iconView.performLongClick();
    }
  };
}
 
Example #25
Source File: TestUtilsActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets text content on {@link TextView} */
public static ViewAction setText(final @Nullable CharSequence text) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TextView.class);
    }

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

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

      TextView textView = (TextView) view;
      textView.setText(text);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #26
Source File: NestedScrollTo.java    From Kore with Apache License 2.0 6 votes vote down vote up
@Override
public void perform(UiController uiController, View view) {
    if (isDisplayingAtLeast(90).matches(view)) {
        LogUtils.LOGI(TAG, "View is already displayed. Returning.");
        return;
    }
    Rect rect = new Rect();
    view.getDrawingRect(rect);
    if (!view.requestRectangleOnScreen(rect, true /* immediate */)) {
        LogUtils.LOGW(TAG, "Scrolling to view was requested, but none of the parents scrolled.");
    }
    uiController.loopMainThreadUntilIdle();
    if (!isDisplayingAtLeast(90).matches(view)) {
        throw new PerformException.Builder()
                .withActionDescription(this.getDescription())
                .withViewDescription(HumanReadables.describe(view))
                .withCause(new RuntimeException(
                        "Scrolling to view was attempted, but the view is not displayed"))
                .build();
    }
}
 
Example #27
Source File: ViewPagerActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets the specified adapter on <code>ViewPager</code>. */
public static ViewAction setAdapter(final @Nullable PagerAdapter adapter) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(ViewPager.class);
    }

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

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

      ViewPager viewPager = (ViewPager) view;
      viewPager.setAdapter(adapter);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #28
Source File: NavigationViewActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * Removes the specified menu item from the navigation view.
 *
 * @param menuItemId The ID of the menu item to be removed.
 */
public static ViewAction removeMenuItem(final @IdRes int menuItemId) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(NavigationView.class);
    }

    @Override
    public String getDescription() {
      return "Remove menu item " + menuItemId;
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();
      NavigationView navigationView = (NavigationView) view;
      navigationView.getMenu().removeItem(menuItemId);
      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #29
Source File: FloatingActionButtonActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setCustomSize(final int size) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(FloatingActionButton.class);
    }

    @Override
    public String getDescription() {
      return "sets FloatingActionButton custom size";
    }

    @Override
    public void perform(UiController uiController, View view) {
      FloatingActionButton fab = (FloatingActionButton) view;
      fab.setCustomSize(size);
    }
  };
}
 
Example #30
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setErrorTextAppearance(final int resId) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TextInputLayout.class);
    }

    @Override
    public String getDescription() {
      return "Sets the error text appearance";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setErrorTextAppearance(resId);
    }
  };
}