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(); } }; }
public static ViewAction clickChildViewWithId(final int id) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return null; } @Override public String getDescription() { return "Click on a child view with specified id."; } @Override public void perform(UiController uiController, View view) { View v = view.findViewById(id); v.performClick(); } }; }
public static ViewAction closeSoftKeyboard() { return new ViewAction() { /** * The real {@link CloseKeyboardAction} instance. */ private final ViewAction mCloseSoftKeyboard = new CloseKeyboardAction(); @Override public Matcher<View> getConstraints() { return mCloseSoftKeyboard.getConstraints(); } @Override public String getDescription() { return mCloseSoftKeyboard.getDescription(); } @Override public void perform(final UiController uiController, final View view) { mCloseSoftKeyboard.perform(uiController, view); uiController.loopMainThreadForAtLeast(KEYBOARD_DISMISSAL_DELAY_MILLIS); } }; }
public static ViewAction loopMainThreadFor(final long millis) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isEnabled(); } @Override public String getDescription() { return "Rturns an action that loops the main thread for at least " + millis +"ms."; } @Override public void perform(final UiController uiController, final View view) { uiController.loopMainThreadForAtLeast(millis); } }; }
private ViewAction fillEntropy() { return new ViewAction() { @Override public Matcher<View> getConstraints() { return allOf(isDisplayed(), isAssignableFrom(EntropyView.class)); } @Override public String getDescription() { return "Dismisses the 'Gathering entropy...' dialog"; } @Override public void perform(final UiController uiController, final View view) { ((EntropyView) view).notifyListeners(); } }; }
/** * Returns an action that clicks a descendant of the view matched with the given resource id. * * @param id resource id of the view to click. * @return an action that clicks a descendant of the view matched with the given resource id. */ public static ViewAction clickDescendantViewWithId(@IdRes final int id) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return hasDescendant(withId(id)); } @Override public String getDescription() { return "Click on a descendant view with id: " + id; } @Override public void perform(UiController uiController, View view) { GeneralClickAction action = new GeneralClickAction(Tap.SINGLE, GeneralLocation.VISIBLE_CENTER, Press.FINGER); View target = view.findViewById(id); // getConstraints() guarantees that the target never be null. action.perform(uiController, target); } }; }
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(); } }; }
/** * From: http://stackoverflow.com/questions/33505953/espresso-how-to-test-swiperefreshlayout/33516360#33516360 * <p> * ViewPager will throw an error if not 90% of it is displayed. This reduces the restriction. * * @param action Action to be done. * @param constraints Restrictions on the view. * @return Performs an action on the view. */ 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); } }; }
static ViewAction setOrientation(final int orientation) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "Change orientation"; } @Override public void perform(UiController uiController, View view) { uiController.loopMainThreadUntilIdle(); changeOrientation(view, orientation); } }; }
static ViewAction setAspectRatio(@NonNull final AspectRatio ratio) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isAssignableFrom(CameraView.class); } @Override public String getDescription() { return "Set aspect ratio to " + ratio; } @Override public void perform(UiController controller, View view) { ((CameraView) view).setAspectRatio(ratio); } }; }
public static ViewAction swipeOnCoord(final float fromX, final float fromY, final float toX, final float toY) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "Swipe from" + fromX + ", " + fromY + " to " + toX + ", " + toY; } @Override public void perform(UiController uiController, final View view) { drag(fromX, fromY, toX, toY); } }; }
public static ViewAction waitFor(final long millis) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "Wait for " + millis + " milliseconds."; } @Override public void perform(UiController uiController, final View view) { uiController.loopMainThreadForAtLeast(millis); } }; }
/** * Test if the manual input activity opens. */ @Test public void testGoToManual() { onView(withId(R.id.manual_input_button)).check(matches(allOf( isEnabled(), isClickable()))).perform( new ViewAction() { @Override public Matcher<View> getConstraints() { return isEnabled(); // no constraints, they are checked above } @Override public String getDescription() { return "click manual input button"; } @Override public void perform(UiController uiController, View view) { view.performClick(); } } ); intended(hasComponent(ManualInputActivity.class.getName())); }
/** * Set value for {@link NumberPicker} */ public static ViewAction setNumber(final int n) { return new ViewAction() { @Override public void perform(UiController uiController, View view) { ((NumberPicker) view).setValue(n); } @Override public String getDescription() { return "Set NumberPicker value"; } @Override public Matcher<View> getConstraints() { return ViewMatchers.isAssignableFrom(NumberPicker.class); } }; }
@Test public synchronized void smoothScrollToPosition_ScrollItemIsNotVisible_FirstVisiblePositionsEqualsScrollingTarget() throws Exception { //arrange InstrumentalUtil.waitForIdle(); //act ViewAction scrollAction = smoothScrollToPosition(18); //noinspection SynchronizationOnLocalVariableOrMethodParameter synchronized (scrollAction) { recyclerView.perform(scrollAction); //wait for completion of SmoothScrollAction scrollAction.wait(); } //assert int actual = layoutManager.findFirstCompletelyVisibleItemPosition(); assertEquals(18, actual); }
@Test public synchronized void smoothScrollToPosition_ScrollItemIsVisible_ScrollItemDockedToStartBorder() throws Exception { //arrange InstrumentalUtil.waitForIdle(); //act ViewAction scrollAction = smoothScrollToPosition(3); //noinspection SynchronizationOnLocalVariableOrMethodParameter synchronized (scrollAction) { recyclerView.perform(scrollAction); //wait for completion of SmoothScrollAction scrollAction.wait(); } //assert int actual = layoutManager.findFirstCompletelyVisibleItemPosition(); assertEquals(3, actual); }
@Test public synchronized void smoothScrollToPosition_LMInInitialState_FirstVisiblePositionsEqualsScrollingTarget() throws Exception { //arrange InstrumentalUtil.waitForIdle(); //act ViewAction scrollAction = smoothScrollToPosition(8); //noinspection SynchronizationOnLocalVariableOrMethodParameter synchronized (scrollAction) { recyclerView.perform(scrollAction); //wait for completion of SmoothScrollAction scrollAction.wait(); } //assert int actual = layoutManager.findFirstCompletelyVisibleItemPosition(); assertEquals(8, actual); }
/** * Returns an action that clicks a descendant of the view matched with the given resource id. */ public static ViewAction clickDescendantViewWithId(@IdRes final int id) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return hasDescendant(withId(id)); } @Override public String getDescription() { return "Click on a descendant view with id: " + id; } @Override public void perform(UiController uiController, View view) { GeneralClickAction action = new GeneralClickAction(Tap.SINGLE, GeneralLocation.VISIBLE_CENTER, Press.FINGER); View target = view.findViewById(id); // getConstraints() guarantees that the target never be null. action.perform(uiController, target); } }; }
public void fooAllOf() { Espresso.onView(AllOf.allOf(ViewMatchers.withId(R.id.some_id), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Espresso.onView(AllOf.allOf(ViewMatchers.withText(R.string.some_text), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Espresso.onView(AllOf.allOf(ViewMatchers.withText("some text"), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Espresso.onView(AllOf.allOf(RistrettoViewMatchers.with("some text"), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Ristretto.withView(AllOf.allOf(ViewMatchers.withId(R.id.some_id), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Ristretto.withView(AllOf.allOf(ViewMatchers.withText(R.string.some_text), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Ristretto.withView(AllOf.allOf(ViewMatchers.withText("some text"), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Ristretto.withView(AllOf.allOf(RistrettoViewMatchers.with("some text"), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Espresso.onView(AllOf.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withId(R.id.some_id))).perform(ViewAction.click()); Espresso.onView(AllOf.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withText(R.string.some_text))).perform(ViewAction.click()); Espresso.onView(AllOf.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withText("some text"))).perform(ViewAction.click()); Espresso.onView(AllOf.allOf(ViewMatchers.isDisplayed(), RistrettoViewMatchers.with("some text"))).perform(ViewAction.click()); Ristretto.withView(AllOf.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withId(R.id.some_id))).perform(ViewAction.click()); Ristretto.withView(AllOf.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withText(R.string.some_text))).perform(ViewAction.click()); Ristretto.withView(AllOf.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withText("some text"))).perform(ViewAction.click()); Ristretto.withView(AllOf.allOf(ViewMatchers.isDisplayed(), RistrettoViewMatchers.with("some text"))).perform(ViewAction.click()); }
public void fooMatchers() { Espresso.onView(Matchers.allOf(ViewMatchers.withId(R.id.some_id), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Espresso.onView(Matchers.allOf(ViewMatchers.withText(R.string.some_text), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Espresso.onView(Matchers.allOf(ViewMatchers.withText("some text"), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Espresso.onView(Matchers.allOf(RistrettoViewMatchers.with("some text"), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Ristretto.withView(Matchers.allOf(ViewMatchers.withId(R.id.some_id), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Ristretto.withView(Matchers.allOf(ViewMatchers.withText(R.string.some_text), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Ristretto.withView(Matchers.allOf(ViewMatchers.withText("some text"), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Ristretto.withView(Matchers.allOf(RistrettoViewMatchers.with("some text"), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Espresso.onView(Matchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withId(R.id.some_id))).perform(ViewAction.click()); Espresso.onView(Matchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withText(R.string.some_text))).perform(ViewAction.click()); Espresso.onView(Matchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withText("some text"))).perform(ViewAction.click()); Espresso.onView(Matchers.allOf(ViewMatchers.isDisplayed(), RistrettoViewMatchers.with("some text"))).perform(ViewAction.click()); Ristretto.withView(Matchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withId(R.id.some_id))).perform(ViewAction.click()); Ristretto.withView(Matchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withText(R.string.some_text))).perform(ViewAction.click()); Ristretto.withView(Matchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withText("some text"))).perform(ViewAction.click()); Ristretto.withView(Matchers.allOf(ViewMatchers.isDisplayed(), RistrettoViewMatchers.with("some text"))).perform(ViewAction.click()); }
public void fooCoreMatchers() { Espresso.onView(CoreMatchers.allOf(ViewMatchers.withId(R.id.some_id), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Espresso.onView(CoreMatchers.allOf(ViewMatchers.withText(R.string.some_text), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Espresso.onView(CoreMatchers.allOf(ViewMatchers.withText("some text"), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Espresso.onView(CoreMatchers.allOf(RistrettoViewMatchers.with("some text"), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Ristretto.withView(CoreMatchers.allOf(ViewMatchers.withId(R.id.some_id), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Ristretto.withView(CoreMatchers.allOf(ViewMatchers.withText(R.string.some_text), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Ristretto.withView(CoreMatchers.allOf(ViewMatchers.withText("some text"), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Ristretto.withView(CoreMatchers.allOf(RistrettoViewMatchers.with("some text"), ViewMatchers.isDisplayed())).perform(ViewAction.click()); Espresso.onView(CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withId(R.id.some_id))).perform(ViewAction.click()); Espresso.onView(CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withText(R.string.some_text))).perform(ViewAction.click()); Espresso.onView(CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withText("some text"))).perform(ViewAction.click()); Espresso.onView(CoreMatchers.allOf(ViewMatchers.isDisplayed(), RistrettoViewMatchers.with("some text"))).perform(ViewAction.click()); Ristretto.withView(CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withId(R.id.some_id))).perform(ViewAction.click()); Ristretto.withView(CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withText(R.string.some_text))).perform(ViewAction.click()); Ristretto.withView(CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withText("some text"))).perform(ViewAction.click()); Ristretto.withView(CoreMatchers.allOf(ViewMatchers.isDisplayed(), RistrettoViewMatchers.with("some text"))).perform(ViewAction.click()); }
public static ViewAction scrollToStart() { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isAssignableFrom(RecyclerBodyView.class); } @Override public String getDescription() { return "scroll to start"; } @Override public void perform(final UiController uiController, final View view) { ((RecyclerBodyView) view).showItem(0); } }; }
public static ViewAction scrollToEnd() { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isAssignableFrom(RecyclerBodyView.class); } @Override public String getDescription() { return "scroll to end"; } @Override public void perform(final UiController uiController, final View view) { final RecyclerBodyView castView = (RecyclerBodyView) view; castView.showItem(castView.getItems().size() - 1); } }; }
/** * Try to getText from TextView */ private String getText(final Matcher<View> matcher) { final String[] stringHolder = { null }; onView(matcher).perform(new ViewAction() { @Override public Matcher<View> getConstraints() { return isAssignableFrom(TextView.class); } @Override public String getDescription() { return "getting text from a TextView"; } @Override public void perform(UiController uiController, View view) { TextView tv = (TextView) view; stringHolder[0] = tv.getText().toString(); } }); return stringHolder[0]; }
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); } }; }
/** * Action that does nothing * <p/> * Useful for waiting for idle sync. In some cases waiting for IdlingResource to be idle is * crucial for some sync code execution, for example assertions. * <p/> * Usage: * //register desired IdlingResource * onView(withText("Yes")).perform(DoppioActions.noAction()); //espresso will stay here until idle * * @return new ViewAction that does nothing */ public static ViewAction noAction() { return new ViewAction() { @Override public Matcher<View> getConstraints() { return Matchers.any(View.class); } @Override public String getDescription() { return "Performs no action, just to satisfy idling resources"; } @Override public void perform(UiController uiController, View view) { } }; }
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); } }; }
public static ViewAction checkAttributionView(String library, String author, String description, String license) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return null; } @Override public String getDescription() { return null; } @Override public void perform(UiController uiController, View view) { TextView txtLibrary = ButterKnife.findById(view, R.id.txt_attribution_library); TextView txtAuthor = ButterKnife.findById(view, R.id.txt_attribution_author); TextView txtDescription = ButterKnife.findById(view, R.id.txt_attribution_description); TextView txtLicense = ButterKnife.findById(view, R.id.txt_attribution_license); assertThat(txtLibrary).containsText(library); assertThat(txtAuthor).containsText(author); assertThat(txtDescription).containsText(description); assertThat(txtLicense).containsText(license); } }; }
/** 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(); } }; }
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); } }; }
public static ViewAction scrollToPosition(final int position) { return new ViewAction() { @SuppressWarnings("unchecked") @Override public Matcher<View> getConstraints() { return isAssignableFrom(RecyclerView.class); } @Override public void perform(UiController uiController, View view) { ((RecyclerView) view).scrollToPosition(position); } @Override public String getDescription() { return "scroll to"; } }; }
public static ViewAction setTime(final int hours, final int minutes) { return new ViewAction() { @Override public void perform(UiController uiController, View view) { final RadialPickerLayout timePicker = (RadialPickerLayout) view; timePicker.setTime(new Timepoint(hours, minutes, 0)); } @Override public String getDescription() { return "set time"; } @Override public Matcher<View> getConstraints() { return allOf(isAssignableFrom(RadialPickerLayout.class), isDisplayed()); } }; }
public static ViewAction setErrorEnabled(final boolean enabled) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isAssignableFrom(TextInputLayout.class); } @Override public String getDescription() { return "Enables/disables the error"; } @Override public void perform(UiController uiController, View view) { TextInputLayout layout = (TextInputLayout) view; layout.setErrorEnabled(enabled); } }; }
public static ViewAction setBoxBackgroundColor(@ColorInt final int backgroundColor) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isAssignableFrom(TextInputLayout.class); } @Override public String getDescription() { return "Sets the box's background color"; } @Override public void perform(UiController uiController, View view) { TextInputLayout layout = (TextInputLayout) view; layout.setBoxBackgroundColor(backgroundColor); } }; }
private static ViewAction actionOpenDrawer() { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isAssignableFrom(DrawerLayout.class); } @Override public String getDescription() { return "open drawer"; } @Override public void perform(UiController uiController, View view) { ((DrawerLayout) view).openDrawer(GravityCompat.START); } }; }
/** * Custom ViewAction to click on dedicated coordinates * @param x - * @param y - * @return ViewAction - */ private ViewAction clickXY( final int x, final int y ){ return new GeneralClickAction( Tap.SINGLE, new CoordinatesProvider() { @Override public float[] calculateCoordinates( View view ){ final int[] screenPos = new int[2]; view.getLocationOnScreen(screenPos); final float screenX = screenPos[0] + x; final float screenY = screenPos[1] + y; return new float[]{screenX, screenY}; } }, Press.FINGER); }