android.support.test.espresso.ViewAction Java Examples

The following examples show how to use android.support.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 Project: u2020-mvp   Author: LiveTyping   File: ViewActions.java    License: 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 #2
Source Project: android-PictureInPicture   Author: googlearchive   File: MediaSessionPlaybackActivityTest.java    License: 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 #3
Source Project: android-PictureInPicture   Author: googlearchive   File: MainActivityTest.java    License: 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 #4
Source Project: polling-station-app   Author: digital-voting-pass   File: TestMainActivity.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 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()));
}
 
Example #5
Source Project: Villains-and-Heroes   Author: andremion   File: CustomActions.java    License: Apache License 2.0 6 votes vote down vote up
public static ViewAction typeQuery(final String value) {
    return new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return allOf(isDisplayed(), isAssignableFrom(SearchView.class));
        }

        @Override
        public void perform(UiController uiController, View view) {
            ((SearchView) view).setQuery(value, false);
        }

        @Override
        public String getDescription() {
            return String.format("type query(%s)", value);
        }
    };
}
 
Example #6
Source Project: px-android   Author: mercadopago   File: ViewText.java    License: MIT License 6 votes vote down vote up
public static String getTextFromMatcher(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(final UiController uiController, final View view) {
            final TextView tv = (TextView) view;
            stringHolder[0] = tv.getText().toString();
        }
    });
    return stringHolder[0];
}
 
Example #7
Source Project: TokenAutoComplete   Author: splitwise   File: InputConnectionTest.java    License: Apache License 2.0 6 votes vote down vote up
public static ViewAction forceCommitText(final String text) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isAssignableFrom(ContactsCompletionView.class);
        }

        @Override
        public String getDescription() {
            return null;
        }

        @Override
        public void perform(UiController uiController, View view) {
            ContactsCompletionView completionView = (ContactsCompletionView)view;
            InputConnection connection = completionView.testAccessibleInputConnection;
            connection.commitText(text, 1);
        }
    };
}
 
Example #8
Source Project: mobile-android-samples   Author: CartoDB   File: MapActivityTest.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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);
        }
    };
}
 
Example #9
Source Project: Anecdote   Author: HugoGresse   File: AnecdoteRecyclerViewAction.java    License: Apache License 2.0 6 votes vote down vote up
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);
            if (v != null) {
                v.performClick();
            }
        }
    };
}
 
Example #10
Source Project: Anecdote   Author: HugoGresse   File: AnecdoteRecyclerViewAction.java    License: Apache License 2.0 6 votes vote down vote up
public static ViewAction clickAnecdoteTextViewWithId(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);
            if (v != null) {
                v.performClick();
            }
        }
    };
}
 
Example #11
Source Project: ChipsLayoutManager   Author: BelooS   File: ColumnTest.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #12
Source Project: ChipsLayoutManager   Author: BelooS   File: ColumnTest.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #13
Source Project: ChipsLayoutManager   Author: BelooS   File: RowTest.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #14
Source Project: espresso-macchiato   Author: nenick   File: AppBarLayoutAction.java    License: MIT License 6 votes vote down vote up
public static ViewAction collapse() {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return ViewMatchers.isAssignableFrom(AppBarLayout.class);
        }

        @Override
        public String getDescription() {
            return "Collapse AppBarLayout inside of a CollapsingToolbarLayout.";
        }

        @Override
        public void perform(UiController uiController, View view) {
            AppBarLayout appBarLayout = (AppBarLayout) view;
            appBarLayout.setExpanded(false, false);
        }
    };
}
 
Example #15
Source Project: espresso-macchiato   Author: nenick   File: AppBarLayoutAction.java    License: MIT License 6 votes vote down vote up
public static ViewAction expand() {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return ViewMatchers.isAssignableFrom(AppBarLayout.class);
        }

        @Override
        public String getDescription() {
            return "Expand AppBarLayout inside of a CollapsingToolbarLayout.";
        }

        @Override
        public void perform(UiController uiController, View view) {
            AppBarLayout appBarLayout = (AppBarLayout) view;
            appBarLayout.setExpanded(true, false);
        }
    };
}
 
Example #16
Source Project: espresso-macchiato   Author: nenick   File: EspWait.java    License: MIT License 6 votes vote down vote up
public static void forIdle() {
    onView(isRoot()).perform(new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isRoot();
        }

        @Override
        public String getDescription() {
            return null;
        }

        @Override
        public void perform(UiController uiController, View view) {
            uiController.loopMainThreadForAtLeast(1000);
            uiController.loopMainThreadUntilIdle();
        }
    });
}
 
Example #17
Source Project: SmoothClicker   Author: pylapp   File: ItSelectMultiPointsActivity.java    License: MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example #18
Source Project: SmoothClicker   Author: pylapp   File: ItClickerActivity.java    License: MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example #19
Source Project: android-material-stepper   Author: stepstone-tech   File: StepperNavigationActions.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Clicks the Next button.
 */
public static ViewAction clickNext() {
    return new AbstractStepperNavigationAction() {

        @Override
        public String getDescription() {
            return "Click on the Next button";
        }

        @Override
        protected void performAction(StepperLayout stepperLayout) {
            View nextButton = stepperLayout.findViewById(com.stepstone.stepper.R.id.ms_stepNextButton);
            nextButton.performClick();
        }

    };
}
 
Example #20
Source Project: android-material-stepper   Author: stepstone-tech   File: StepperNavigationActions.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Clicks the Back button.
 */
public static ViewAction clickBack() {
    return new AbstractStepperNavigationAction() {

        @Override
        public String getDescription() {
            return "Click on the Back button";
        }

        @Override
        protected void performAction(StepperLayout stepperLayout) {
            View backButton = stepperLayout.findViewById(com.stepstone.stepper.R.id.ms_stepPrevButton);
            backButton.performClick();
        }

    };
}
 
Example #21
Source Project: android-material-stepper   Author: stepstone-tech   File: StepperNavigationActions.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Clicks the Complete button.
 */
public static ViewAction clickComplete() {
    return new AbstractStepperNavigationAction() {

        @Override
        public String getDescription() {
            return "Click on the Complete button";
        }

        @Override
        protected void performAction(StepperLayout stepperLayout) {
            View completeButton = stepperLayout.findViewById(com.stepstone.stepper.R.id.ms_stepCompleteButton);
            completeButton.performClick();
        }

    };
}
 
Example #22
Source Project: android-agera   Author: googlecodelabs   File: Step4CalculatorActivityTest.java    License: Apache License 2.0 6 votes vote down vote up
public static ViewAction setProgress(int progress) {
    return new ViewAction() {
        @Override
        public void perform(UiController uiController, View view) {
            SeekBar seekBar = (SeekBar) view;
            seekBar.setProgress(progress);
        }
        @Override
        public String getDescription() {
            return "Set a progress on a SeekBar";
        }
        @Override
        public Matcher<View> getConstraints() {
            return ViewMatchers.isAssignableFrom(SeekBar.class);
        }
    };
}
 
Example #23
Source Project: android-agera   Author: googlecodelabs   File: Step4CalculatorActivityFinalTest.java    License: Apache License 2.0 6 votes vote down vote up
public static ViewAction setProgress(int progress) {
    return new ViewAction() {
        @Override
        public void perform(UiController uiController, View view) {
            SeekBar seekBar = (SeekBar) view;
            seekBar.setProgress(progress);
        }
        @Override
        public String getDescription() {
            return "Set a progress on a SeekBar";
        }
        @Override
        public Matcher<View> getConstraints() {
            return ViewMatchers.isAssignableFrom(SeekBar.class);
        }
    };
}
 
Example #24
Source Project: cameraview   Author: google   File: CameraViewActions.java    License: Apache License 2.0 6 votes vote down vote up
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);
        }
    };
}
 
Example #25
Source Project: friendly-plans   Author: autyzm-pg   File: StepListFragmentTest.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static ViewAction clickChildViewWithId(final int id) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return null;
        }

        @Override
        public String getDescription() {
            return null;
        }

        @Override
        public void perform(UiController uiController, View view) {
            View v = view.findViewById(id);
            v.performClick();
        }
    };
}
 
Example #26
Source Project: Equate   Author: EvanRespaut   File: EspressoTestUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Clicks on the tab for the provided Unit Type name. Note that the Unit Type
 * doesn't need to be visible.
 */
public static void selectUnitTypeDirect(final String unitTypeName) {
	onView(allOf(withText(unitTypeName))).perform(
			  new ViewAction() {
				  @Override
				  public Matcher<View> getConstraints() {
					  return isEnabled(); // no constraints, they are checked above
				  }

				  @Override
				  public String getDescription() {
					  return "click unit type" + unitTypeName;
				  }

				  @Override
				  public void perform(UiController uiController, View view) {
					  view.performClick();
				  }
			  }
	);
}
 
Example #27
Source Project: u2020-mvp   Author: LiveTyping   File: ViewActions.java    License: 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 #28
Source Project: PrettyBundle   Author: talenguyen   File: ExtViewActions.java    License: 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 #29
Source Project: TokenAutoComplete   Author: splitwise   File: InputConnectionTest.java    License: Apache License 2.0 6 votes vote down vote up
public static ViewAction forceComposingText(final String text) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isAssignableFrom(ContactsCompletionView.class);
        }

        @Override
        public String getDescription() {
            return null;
        }

        @Override
        public void perform(UiController uiController, View view) {
            ContactsCompletionView completionView = (ContactsCompletionView)view;
            InputConnection connection = completionView.testAccessibleInputConnection;
            connection.setComposingText(text, -1);
        }
    };
}
 
Example #30
Source Project: BuildmLearn-Toolkit-Android   Author: BuildmLearn   File: ComprehensionTest.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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];
}