androidx.test.espresso.ViewAction Java Examples

The following examples show how to use androidx.test.espresso.ViewAction. 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: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 8 votes vote down vote up
public static ViewAction setEndIconOnLongClickListener(
    final OnLongClickListener onLongClickListener) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TextInputLayout.class);
    }

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

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setEndIconOnLongClickListener(onLongClickListener);
    }
  };
}
 
Example #2
Source File: BottomSheetDialogTest.java    From material-components-android with Apache License 2.0 7 votes vote down vote up
private static ViewAction setTallPeekHeight() {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return ViewMatchers.isDisplayed();
    }

    @Override
    public String getDescription() {
      return "set tall peek height";
    }

    @Override
    public void perform(UiController uiController, View view) {
      BottomSheetBehavior<?> behavior = BottomSheetBehavior.from(view);
      behavior.setPeekHeight(view.getHeight() + 100);
    }
  };
}
 
Example #3
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 #4
Source File: TestUtilsActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * Clears and inflates the menu.
 *
 * @param menuResId The menu resource XML to be used.
 */
public static ViewAction reinflateMenu(final @MenuRes int menuResId) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(NavigationView.class);
    }

    @Override
    public String getDescription() {
      return "clear and inflate menu " + menuResId;
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();
      final NavigationView nv = (NavigationView) view;
      nv.getMenu().clear();
      nv.inflateMenu(menuResId);
      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #5
Source File: TabLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Setup and show badge number for the specified tab of the <code>TabLayout</code>. */
public static ViewAction showBadgeOnTab(final int tabIndex, final int badgeNumber) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayingAtLeast(90);
    }

    @Override
    public String getDescription() {
      return "Setup tab badge number";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TabLayout tabLayout = (TabLayout) view;
      tabLayout.getTabAt(tabIndex).getOrCreateBadge().setNumber(badgeNumber);
    }
  };
}
 
Example #6
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, final boolean autoRefresh) {
  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, autoRefresh);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #7
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 #8
Source File: TabLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets the specified tab mode in the <code>TabLayout</code>. */
public static ViewAction setTabMode(final int tabMode) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayingAtLeast(90);
    }

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

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

      TabLayout tabLayout = (TabLayout) view;
      tabLayout.setTabMode(tabMode);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #9
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 #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 the right by one page. */
public static ViewAction scrollRight() {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayingAtLeast(90);
    }

    @Override
    public String getDescription() {
      return "ViewPager scroll one page to the right";
    }

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

      ViewPager viewPager = (ViewPager) view;
      int current = viewPager.getCurrentItem();
      viewPager.setCurrentItem(current + 1, false);

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

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

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

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

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setTypeface(typeface);
    }
  };
}
 
Example #14
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 #15
Source File: TestUtilsActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setPressed(final boolean pressed) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

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

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

      view.setPressed(pressed);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #16
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 #17
Source File: TestUtilsActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * Dummy Espresso action that waits for at least the given amount of milliseconds. This action can
 * be performed on the root view to wait for an ongoing animation to be completed.
 */
public static ViewAction waitFor(final long ms) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isRoot();
    }

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

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

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

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();
      view.setSelected(selected);
      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #19
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 #20
Source File: TestUtilsActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets title on the {@link CollapsingToolbarLayout}. */
public static ViewAction setTitle(final CharSequence title) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(CollapsingToolbarLayout.class);
    }

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

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

      CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) view;
      collapsingToolbarLayout.setTitle(title);

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

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

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

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

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setCounterEnabled(enabled);
    }
  };
}
 
Example #24
Source File: NavigationViewActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets checked item on the navigation view. */
public static ViewAction setCheckedItem(final @IdRes int id) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isDisplayed();
    }

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

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

      NavigationView navigationView = (NavigationView) view;
      navigationView.setCheckedItem(id);

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example #25
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets suffix. */
public static ViewAction setSuffixText(final CharSequence suffixText) {
  return new ViewAction() {

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

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

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setSuffixText(suffixText);
    }
  };
}
 
Example #26
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 #27
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 #28
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 #29
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public static ViewAction setStartIconOnLongClickListener(
    final OnLongClickListener onLongClickListener) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TextInputLayout.class);
    }

    @Override
    public String getDescription() {
      return "Set a long click listener for the start icon";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout layout = (TextInputLayout) view;
      layout.setStartIconOnLongClickListener(onLongClickListener);
    }
  };
}
 
Example #30
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();
    }
  };
}