androidx.test.espresso.matcher.ViewMatchers Java Examples
The following examples show how to use
androidx.test.espresso.matcher.ViewMatchers.
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: BottomSheetDialogTest.java From material-components-android with Apache License 2.0 | 7 votes |
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 #2
Source File: TextInputLayoutActions.java From material-components-android with Apache License 2.0 | 6 votes |
/** Sets the transformation method. */ public static ViewAction setTransformationMethod( final TransformationMethod transformationMethod) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return ViewMatchers.isAssignableFrom(TextInputLayout.class); } @Override public String getDescription() { return "Sets the transformation method"; } @Override public void perform(UiController uiController, View view) { TextInputLayout item = (TextInputLayout) view; item.getEditText().setTransformationMethod(transformationMethod); } }; }
Example #3
Source File: BottomSheetBehaviorTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test @MediumTest public void testSwipeUpToExpand() { Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) .perform( DesignViewActions.withCustomConstraints( new GeneralSwipeAction( Swipe.FAST, GeneralLocation.VISIBLE_CENTER, view -> new float[] {view.getWidth() / 2, 0}, Press.FINGER), ViewMatchers.isDisplayingAtLeast(5))); registerIdlingResourceCallback(); try { Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) .check(ViewAssertions.matches(ViewMatchers.isDisplayed())); assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_EXPANDED)); } finally { unregisterIdlingResourceCallback(); } }
Example #4
Source File: BottomSheetBehaviorTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test @MediumTest public void testSwipeDownToHide() { Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) .perform( DesignViewActions.withCustomConstraints( ViewActions.swipeDown(), ViewMatchers.isDisplayingAtLeast(5))); registerIdlingResourceCallback(); try { Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) .check(ViewAssertions.matches(not(ViewMatchers.isDisplayed()))); assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_HIDDEN)); } finally { unregisterIdlingResourceCallback(); } }
Example #5
Source File: BottomSheetBehaviorTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test @MediumTest public void testNoSwipeUpToExpand() { getBehavior().setDraggable(false); Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) .perform( DesignViewActions.withCustomConstraints( new GeneralSwipeAction( Swipe.FAST, GeneralLocation.VISIBLE_CENTER, view -> new float[] {view.getWidth() / 2, 0}, Press.FINGER), ViewMatchers.isDisplayingAtLeast(5))); Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) .check(ViewAssertions.matches(ViewMatchers.isDisplayed())); assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_COLLAPSED)); }
Example #6
Source File: BottomSheetDialogTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test public void testBasicDialogSetup() throws Throwable { showDialog(); // Confirms that the dialog is shown assertThat(dialog.isShowing(), is(true)); FrameLayout bottomSheet = dialog.findViewById(R.id.design_bottom_sheet); assertThat(bottomSheet, is(notNullValue())); BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet); assertThat(behavior.isHideable(), is(true)); assertThat(behavior, is(notNullValue())); // Modal bottom sheets have auto peek height by default. assertThat(behavior.getPeekHeight(), is(BottomSheetBehavior.PEEK_HEIGHT_AUTO)); // Click outside the bottom sheet onView(ViewMatchers.withId(R.id.touch_outside)).perform(click()); // Confirm that the dialog is no longer shown assertThat(dialog.isShowing(), is(false)); }
Example #7
Source File: BottomSheetDialogTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test public void testClickContent() throws Throwable { final View.OnClickListener mockListener = mock(View.OnClickListener.class); showDialog(); // Confirms that the dialog is shown assertThat(dialog.isShowing(), is(true)); FrameLayout bottomSheet = dialog.findViewById(R.id.design_bottom_sheet); // Set up an OnClickListener to the content of the bottom sheet assertNotNull(bottomSheet); View child = bottomSheet.getChildAt(0); child.setOnClickListener(mockListener); // Click on the bottom sheet; since the whole sheet is occupied with its only child, this // clicks the child onView(ViewMatchers.withParent(ViewMatchers.withId(R.id.design_bottom_sheet))) .perform(click()); verify(mockListener, times(1)).onClick(any(View.class)); }
Example #8
Source File: BottomSheetDialogTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test public void testShortDialog() throws Throwable { showDialog(); // This ensures that the views are laid out before assertions below onView(ViewMatchers.withId(R.id.design_bottom_sheet)) .perform(setTallPeekHeight()) .check(ViewAssertions.matches(ViewMatchers.isDisplayed())); FrameLayout bottomSheet = dialog.findViewById(R.id.design_bottom_sheet); CoordinatorLayout coordinator = (CoordinatorLayout) bottomSheet.getParent(); BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet); assertThat(bottomSheet, is(notNullValue())); assertThat(coordinator, is(notNullValue())); assertThat(behavior, is(notNullValue())); // This bottom sheet is shorter than the peek height assertThat(bottomSheet.getHeight(), is(lessThan(behavior.getPeekHeight()))); // Confirm that the bottom sheet is bottom-aligned assertThat(bottomSheet.getTop(), is(coordinator.getHeight() - bottomSheet.getHeight())); }
Example #9
Source File: BottomSheetDialogTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test public void testNonCancelableDialog() throws Throwable { showDialog(); dialog.setCancelable(false); // Click outside the bottom sheet onView(ViewMatchers.withId(R.id.touch_outside)).perform(click()); FrameLayout bottomSheet = dialog.findViewById(R.id.design_bottom_sheet); BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet); assertThat(behavior.isHideable(), is(false)); assertThat(dialog.isShowing(), is(true)); activityTestRule.runOnUiThread( () -> { dialog.cancel(); assertThat(dialog.isShowing(), is(false)); }); }
Example #10
Source File: BottomSheetDialogTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test public void testHideBottomSheet() throws Throwable { final AtomicBoolean canceled = new AtomicBoolean(false); showDialog(); dialog.setOnCancelListener(dialogInterface -> canceled.set(true)); onView(ViewMatchers.withId(R.id.design_bottom_sheet)) .perform(setState(BottomSheetBehavior.STATE_HIDDEN)); // The dialog should be canceled long start = System.currentTimeMillis(); while (!canceled.get()) { SystemClock.sleep(31); if (System.currentTimeMillis() - start > 3000) { fail("Timed out while waiting for the dialog to be canceled."); } } }
Example #11
Source File: BottomSheetDialogTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test @MediumTest public void testHideThenShow() throws Throwable { // Hide the bottom sheet and wait for the dialog to be canceled. final DialogInterface.OnCancelListener onCancelListener = mock(DialogInterface.OnCancelListener.class); showDialog(); dialog.setOnCancelListener(onCancelListener); onView(ViewMatchers.withId(R.id.design_bottom_sheet)) .perform(setState(BottomSheetBehavior.STATE_HIDDEN)); verify(onCancelListener, timeout(3000)).onCancel(any(DialogInterface.class)); // Reshow the same dialog instance and wait for the bottom sheet to be collapsed. final BottomSheetBehavior.BottomSheetCallback callback = mock(BottomSheetBehavior.BottomSheetCallback.class); BottomSheetBehavior.from(dialog.findViewById(R.id.design_bottom_sheet)) .addBottomSheetCallback(callback); // Show the same dialog again. activityTestRule.runOnUiThread(() -> dialog.show()); verify(callback, timeout(3000)) .onStateChanged(any(View.class), eq(BottomSheetBehavior.STATE_SETTLING)); verify(callback, timeout(3000)) .onStateChanged(any(View.class), eq(BottomSheetBehavior.STATE_COLLAPSED)); }
Example #12
Source File: DesignViewActions.java From material-components-android with Apache License 2.0 | 6 votes |
public static ViewAction setVisibility(final int visibility) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return ViewMatchers.isEnabled(); } @Override public String getDescription() { return "Set view visibility"; } @Override public void perform(UiController uiController, View view) { uiController.loopMainThreadUntilIdle(); view.setVisibility(visibility); uiController.loopMainThreadUntilIdle(); } }; }
Example #13
Source File: TabLayoutActions.java From material-components-android with Apache License 2.0 | 6 votes |
/** * 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 #14
Source File: TextInputLayoutActions.java From material-components-android with Apache License 2.0 | 6 votes |
/** Clicks end or start icon. */ public static ViewAction clickIcon(final boolean isEndIcon) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return ViewMatchers.isAssignableFrom(TextInputLayout.class); } @Override public String getDescription() { return "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.performClick(); } }; }
Example #15
Source File: TextInputLayoutActions.java From material-components-android with Apache License 2.0 | 6 votes |
/** 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 #16
Source File: TextInputLayoutActions.java From material-components-android with Apache License 2.0 | 6 votes |
/** Skips any animations on the layout. */ public static ViewAction skipAnimations() { return new ViewAction() { @Override public Matcher<View> getConstraints() { return ViewMatchers.isAssignableFrom(TextInputLayout.class); } @Override public String getDescription() { return "Skips any animations."; } @Override public void perform(UiController uiController, View view) { view.jumpDrawablesToCurrentState(); } }; }
Example #17
Source File: TextInputLayoutActions.java From material-components-android with Apache License 2.0 | 6 votes |
/** 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 |
/** 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 #19
Source File: MainActivityHolderTest.java From toktok-android with GNU General Public License v3.0 | 6 votes |
@Test public void rejectVideoCall() { // Go to the Friends tab. onView(withText("Friends")).perform(click()); // Click on one of the friends. onView(withText("Bart Simpson")).perform(click()); // Click "call". onView(allOf(withText("Call"), withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE))) .perform(click()); // Swipe the decline button to the left. onView(withId(R.id.call_decline)) .perform(swipeLeft()); }
Example #20
Source File: BottomSheetBehaviorTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test @MediumTest public void testNoDragging() { getBehavior().setDraggable(false); Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) // Drag (and not release) .perform( new DragAction( GeneralLocation.VISIBLE_CENTER, GeneralLocation.TOP_CENTER, Press.FINGER)) // Check that the bottom sheet is NOT in STATE_DRAGGING .check( (view, e) -> { assertThat(view, is(ViewMatchers.isDisplayed())); BottomSheetBehavior<?> behavior = BottomSheetBehavior.from(view); assertThat(behavior.getState(), not(is(BottomSheetBehavior.STATE_DRAGGING))); }); }
Example #21
Source File: BottomSheetBehaviorTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test @MediumTest public void testHalfExpandedToExpanded() throws Throwable { getBehavior().setFitToContents(false); checkSetState(BottomSheetBehavior.STATE_HALF_EXPANDED, ViewMatchers.isDisplayed()); Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) .perform( DesignViewActions.withCustomConstraints( new GeneralSwipeAction( Swipe.FAST, GeneralLocation.VISIBLE_CENTER, view -> new float[] {view.getWidth() / 2, 0}, Press.FINGER), ViewMatchers.isDisplayingAtLeast(5))); registerIdlingResourceCallback(); try { Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) .check(ViewAssertions.matches(ViewMatchers.isDisplayed())); assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_EXPANDED)); } finally { unregisterIdlingResourceCallback(); } }
Example #22
Source File: BottomSheetBehaviorTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test @MediumTest public void testCollapsedToExpanded() throws Throwable { getBehavior().setFitToContents(false); checkSetState(BottomSheetBehavior.STATE_COLLAPSED, ViewMatchers.isDisplayed()); Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) .perform( DesignViewActions.withCustomConstraints( new GeneralSwipeAction( Swipe.FAST, GeneralLocation.VISIBLE_CENTER, view -> new float[] {view.getWidth() / 2, 0}, Press.FINGER), ViewMatchers.isDisplayingAtLeast(5))); registerIdlingResourceCallback(); try { Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) .check(ViewAssertions.matches(ViewMatchers.isDisplayed())); assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_EXPANDED)); } finally { unregisterIdlingResourceCallback(); } }
Example #23
Source File: BottomSheetBehaviorTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test @MediumTest public void testLayoutWhileDragging() { Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) // Drag (and not release) .perform( new DragAction( GeneralLocation.VISIBLE_CENTER, GeneralLocation.TOP_CENTER, Press.FINGER)) // Check that the bottom sheet is in STATE_DRAGGING .check( (view, e) -> { assertThat(view, is(ViewMatchers.isDisplayed())); BottomSheetBehavior<?> behavior = BottomSheetBehavior.from(view); assertThat(behavior.getState(), is(BottomSheetBehavior.STATE_DRAGGING)); }) // Add a new view .perform(new AddViewAction(R.layout.frame_layout)) // Check that the newly added view is properly laid out .check( (view, e) -> { ViewGroup parent = (ViewGroup) view; assertThat(parent.getChildCount(), is(1)); View child = parent.getChildAt(0); assertThat(ViewCompat.isLaidOut(child), is(true)); }); }
Example #24
Source File: BottomSheetBehaviorTest.java From material-components-android with Apache License 2.0 | 6 votes |
@Test @MediumTest public void testInvisible() throws Throwable { // Make the bottomsheet invisible activityTestRule.runOnUiThread( () -> { getBottomSheet().setVisibility(View.INVISIBLE); assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_COLLAPSED)); }); // Swipe up as if to expand it Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) .perform( DesignViewActions.withCustomConstraints( new GeneralSwipeAction( Swipe.FAST, GeneralLocation.VISIBLE_CENTER, view -> new float[] {view.getWidth() / 2, 0}, Press.FINGER), not(ViewMatchers.isDisplayed()))); // Check that the bottom sheet stays the same collapsed state activityTestRule.runOnUiThread( () -> assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_COLLAPSED))); }
Example #25
Source File: RecyclerViewSampleTest.java From testing-samples with Apache License 2.0 | 5 votes |
@Test public void scrollToItemBelowFold_checkItsText() { // First scroll to the position that needs to be matched and click on it. onView(ViewMatchers.withId(R.id.recyclerView)) .perform(RecyclerViewActions.actionOnItemAtPosition(ITEM_BELOW_THE_FOLD, click())); // Match the text in an item below the fold and check that it's displayed. String itemElementText = getApplicationContext().getResources().getString( R.string.item_element_text) + String.valueOf(ITEM_BELOW_THE_FOLD); onView(withText(itemElementText)).check(matches(isDisplayed())); }
Example #26
Source File: LongListActivityTest.java From testing-samples with Apache License 2.0 | 5 votes |
/** * Make sure that clicking on the toggle button doesn't trigger a click on the row. */ @Test public void toggle_ClickDoesntPropagate() { // Click on one of the rows. onRow(TEXT_ITEM_30).onChildView(withId(R.id.rowContentTextView)).perform(click()); // Click on the toggle button, in a different row. onRow(TEXT_ITEM_60).onChildView(withId(R.id.rowToggleButton)).perform(click()); // Check that the activity didn't detect the click on the first column. onView(ViewMatchers.withId(R.id.selection_row_value)) .check(matches(withText(TEXT_ITEM_30_SELECTED))); }
Example #27
Source File: BottomSheetBehaviorTest.java From material-components-android with Apache License 2.0 | 5 votes |
@Test @MediumTest public void testDragOutside() { // Swipe up outside of the bottom sheet Espresso.onView(ViewMatchers.withId(R.id.coordinator)) .perform( DesignViewActions.withCustomConstraints( new GeneralSwipeAction( Swipe.FAST, // Just above the bottom sheet view -> new float[] { view.getWidth() / 2, view.getHeight() - getBehavior().getPeekHeight() - 9 }, // Top of the CoordinatorLayout view -> new float[] {view.getWidth() / 2, 1}, Press.FINGER), ViewMatchers.isDisplayed())); registerIdlingResourceCallback(); try { Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) .check(ViewAssertions.matches(ViewMatchers.isDisplayed())); // The bottom sheet should remain collapsed assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_COLLAPSED)); } finally { unregisterIdlingResourceCallback(); } }
Example #28
Source File: RecyclerViewSampleTest.java From testing-samples with Apache License 2.0 | 5 votes |
@Test public void itemInMiddleOfList_hasSpecialText() { // First, scroll to the view holder using the isInTheMiddle matcher. onView(ViewMatchers.withId(R.id.recyclerView)) .perform(RecyclerViewActions.scrollToHolder(isInTheMiddle())); // Check that the item has the special text. String middleElementText = getApplicationContext().getResources().getString(R.string.middle); onView(withText(middleElementText)).check(matches(isDisplayed())); }
Example #29
Source File: BottomSheetBehaviorTest.java From material-components-android with Apache License 2.0 | 5 votes |
@Test @MediumTest public void testAutoPeekHeightHide() throws Throwable { activityTestRule.runOnUiThread( () -> { getBehavior().setHideable(true); getBehavior().setPeekHeight(0); getBehavior().setPeekHeight(BottomSheetBehavior.PEEK_HEIGHT_AUTO); }); checkSetState(BottomSheetBehavior.STATE_HIDDEN, not(ViewMatchers.isDisplayed())); }
Example #30
Source File: MainActivityTest.java From Walk-In-Clinic-Android-App with MIT License | 5 votes |
@Test public void changeText_sameActivity() { onView(withId(R.id.SignInButton)).perform(click()); onView(ViewMatchers.withId(R.id.passwordField2)) .perform(typeText("EspressoUser123"), closeSoftKeyboard()); onView(ViewMatchers.withId(R.id.usernameField2)) .perform(typeText("EspressoUser123"), closeSoftKeyboard()); onView(withId(R.id.signInButton)).perform(click()); while (true) { try { onView(withId(R.id.profileButton)).perform(click()); break; } catch (Exception e) { onView(withId(R.id.signInButton)).perform(click()); continue; } } activityRuleProfile = new ActivityTestRule<>(Profile.class); onView(withId(R.id.addressField2)).perform(replaceText(""), closeSoftKeyboard()); onView(withId(R.id.phoneNumField)).perform(replaceText(""), closeSoftKeyboard()); onView(withId(R.id.nameField)).perform(replaceText(""), closeSoftKeyboard()); onView(withId(R.id.saveButton)).perform(click()); onView(withId(R.id.addressField2)).perform(typeText("100 Bank Street"), closeSoftKeyboard()); onView(withId(R.id.saveButton)).perform(click()); onView(withId(R.id.phoneNumField)).perform(typeText("(905)-806-2222"), closeSoftKeyboard()); onView(withId(R.id.saveButton)).perform(click()); onView(withId(R.id.nameField)).perform(typeText("Test"), closeSoftKeyboard()); onView(withId(R.id.saveButton)).perform(click()); }