androidx.test.espresso.assertion.ViewAssertions Java Examples

The following examples show how to use androidx.test.espresso.assertion.ViewAssertions. 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: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: BottomSheetDialogTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@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 #3
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@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 vote down vote up
@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 #5
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@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 #7
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Test
@MediumTest
public void testSwipeDownToCollapse() throws Throwable {
  checkSetState(BottomSheetBehavior.STATE_EXPANDED, ViewMatchers.isDisplayed());
  Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
      .perform(
          DesignViewActions.withCustomConstraints(
              new GeneralSwipeAction(
                  Swipe.FAST,
                  // Manually calculate the starting coordinates to make sure that the touch
                  // actually falls onto the view on Gingerbread
                  view -> {
                    int[] location = new int[2];
                    view.getLocationInWindow(location);
                    return new float[] {view.getWidth() / 2, location[1] + 1};
                  },
                  // Manually calculate the ending coordinates to make sure that the bottom
                  // sheet is collapsed, not hidden
                  view -> {
                    BottomSheetBehavior<?> behavior = getBehavior();
                    return new float[] {
                      // x: center of the bottom sheet
                      view.getWidth() / 2,
                      // y: just above the peek height
                      view.getHeight() - behavior.getPeekHeight()
                    };
                  },
                  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_COLLAPSED));
  } finally {
    unregisterIdlingResourceCallback();
  }
}
 
Example #8
Source File: EspressoTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void registerResourceWithNullName() {
  DummyIdlingResource resource = new DummyIdlingResource(null);
  try {
    Espresso.registerIdlingResources(resource);
    // IRs are taken into account only on the next Espresso interaction, thus preform the
    // following dummy check (could be anything)
    onView(isRoot()).check(ViewAssertions.matches(isDisplayed()));
  } catch (RuntimeException expected) {
    // expected
  } finally {
    // cleanup
    Espresso.unregisterIdlingResources(resource);
  }
}
 
Example #9
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void checkSetState(final int state, Matcher<View> matcher) throws Throwable {
  registerIdlingResourceCallback();
  try {
    activityTestRule.runOnUiThread(() -> getBehavior().setState(state));
    Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
        .check(ViewAssertions.matches(matcher));
    assertThat(getBehavior().getState(), is(state));
    assertAccessibilityActions(getBehavior(), getBottomSheet());
  } finally {
    unregisterIdlingResourceCallback();
  }
}
 
Example #10
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Test
@MediumTest
public void testDynamicContent() throws Throwable {
  registerIdlingResourceCallback();
  try {
    activityTestRule.runOnUiThread(
        () -> {
          ViewGroup.LayoutParams params = getBottomSheet().getLayoutParams();
          params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
          getBottomSheet().setLayoutParams(params);
          View view = new View(getBottomSheet().getContext());
          int size = getBehavior().getPeekHeight() * 2;
          getBottomSheet().addView(view, new ViewGroup.LayoutParams(size, size));
          assertThat(getBottomSheet().getChildCount(), is(1));
          // Shrink the content height.
          ViewGroup.LayoutParams lp = view.getLayoutParams();
          lp.height = (int) (size * 0.8);
          view.setLayoutParams(lp);
          // Immediately expand the bottom sheet.
          getBehavior().setState(BottomSheetBehavior.STATE_EXPANDED);
        });
    Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
        .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_EXPANDED));
    // Make sure that the bottom sheet is not floating above the bottom.
    assertThat(getBottomSheet().getBottom(), is(getCoordinatorLayout().getBottom()));
  } finally {
    unregisterIdlingResourceCallback();
  }
}
 
Example #11
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@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 #12
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Test
@MediumTest
public void testNoSwipeDownToCollapse() throws Throwable {
  checkSetState(BottomSheetBehavior.STATE_EXPANDED, ViewMatchers.isDisplayed());
  getBehavior().setDraggable(false);
  Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
      .perform(
          DesignViewActions.withCustomConstraints(
              new GeneralSwipeAction(
                  Swipe.FAST,
                  // Manually calculate the starting coordinates to make sure that the touch
                  // actually falls onto the view on Gingerbread
                  view -> {
                    int[] location = new int[2];
                    view.getLocationInWindow(location);
                    return new float[] {view.getWidth() / 2, location[1] + 1};
                  },
                  // Manually calculate the ending coordinates to make sure that the bottom
                  // sheet is collapsed, not hidden
                  view -> {
                    BottomSheetBehavior<?> behavior = getBehavior();
                    return new float[] {
                      // x: center of the bottom sheet
                      view.getWidth() / 2,
                      // y: just above the peek height
                      view.getHeight() - behavior.getPeekHeight()
                    };
                  },
                  Press.FINGER),
              ViewMatchers.isDisplayingAtLeast(5)));

  Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
      .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
  assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_EXPANDED));
}
 
Example #13
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Test
@MediumTest
public void testNestedScroll() throws Throwable {
  final ViewGroup bottomSheet = getBottomSheet();
  final BottomSheetBehavior<?> behavior = getBehavior();
  final NestedScrollView scroll = new NestedScrollView(activityTestRule.getActivity());
  // Set up nested scrolling area
  activityTestRule.runOnUiThread(
      () -> {
        bottomSheet.addView(
            scroll,
            new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        View view = new View(activityTestRule.getActivity());
        // Make sure that the NestedScrollView is always scrollable
        view.setMinimumHeight(bottomSheet.getHeight() + 1000);
        scroll.addView(view);

        assertThat(behavior.getState(), is(BottomSheetBehavior.STATE_COLLAPSED));
        // The scroll offset is 0 at first
        assertThat(scroll.getScrollY(), is(0));
      });
  // Swipe from the very bottom of the bottom sheet to the top edge of the screen so that the
  // scrolling content is also scrolled
  Espresso.onView(ViewMatchers.withId(R.id.coordinator))
      .perform(
          new GeneralSwipeAction(
              Swipe.SLOW,
              view -> new float[] {view.getWidth() / 2, view.getHeight() - 1},
              view -> new float[] {view.getWidth() / 2, 1},
              Press.FINGER));
  registerIdlingResourceCallback();
  try {
    Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
        .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    activityTestRule.runOnUiThread(
        () -> {
          assertThat(behavior.getState(), is(BottomSheetBehavior.STATE_EXPANDED));
          // This confirms that the nested scrolling area was scrolled continuously after
          // the bottom sheet is expanded.
          assertThat(scroll.getScrollY(), is(not(0)));
        });
  } finally {
    unregisterIdlingResourceCallback();
  }
}
 
Example #14
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private void testSkipCollapsed_smallSwipe(int expectedState, float swipeViewHeightPercentage)
    throws Throwable {
  getBehavior().setSkipCollapsed(true);
  checkSetState(BottomSheetBehavior.STATE_EXPANDED, ViewMatchers.isDisplayed());
  Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
      .perform(
          DesignViewActions.withCustomConstraints(
              new GeneralSwipeAction(
                  Swipe.SLOW,
                  // Manually calculate the starting coordinates to make sure that the touch
                  // actually falls onto the view on Gingerbread
                  view -> {
                    int[] location = new int[2];
                    view.getLocationInWindow(location);
                    return new float[] {view.getWidth() / 2, location[1] + 1};
                  },
                  // Manually calculate the ending coordinates to make sure that the bottom
                  // sheet is collapsed, not hidden
                  view -> {
                    return new float[] {
                      // x: center of the bottom sheet
                      view.getWidth() / 2,
                      // y: some percentage down the view
                      view.getHeight() * swipeViewHeightPercentage,
                    };
                  },
                  Press.FINGER),
              ViewMatchers.isDisplayingAtLeast(5)));
  registerIdlingResourceCallback();
  try {
    if (expectedState == BottomSheetBehavior.STATE_HIDDEN) {
      Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
          .check(ViewAssertions.matches(not(ViewMatchers.isDisplayed())));
    } else {
      Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
          .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    }
    assertThat(getBehavior().getState(), is(expectedState));
  } finally {
    unregisterIdlingResourceCallback();
  }
}
 
Example #15
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Test
@MediumTest
public void testSkipCollapsed() throws Throwable {
  assertAccessibilityActions(getBehavior(), getBottomSheet());
  getBehavior().setSkipCollapsed(true);
  assertAccessibilityActions(getBehavior(), getBottomSheet());
  checkSetState(BottomSheetBehavior.STATE_EXPANDED, ViewMatchers.isDisplayed());
  Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
      .perform(
          DesignViewActions.withCustomConstraints(
              new GeneralSwipeAction(
                  Swipe.FAST,
                  // Manually calculate the starting coordinates to make sure that the touch
                  // actually falls onto the view on Gingerbread
                  view -> {
                    int[] location = new int[2];
                    view.getLocationInWindow(location);
                    return new float[] {view.getWidth() / 2, location[1] + 1};
                  },
                  // Manually calculate the ending coordinates to make sure that the bottom
                  // sheet is collapsed, not hidden
                  view -> {
                    BottomSheetBehavior<?> behavior = getBehavior();
                    return new float[] {
                      // x: center of the bottom sheet
                      view.getWidth() / 2,
                      // y: just above the peek height
                      view.getHeight() - behavior.getPeekHeight()
                    };
                  },
                  Press.FINGER),
              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 #16
Source File: IdlingTest.java    From android-test with Apache License 2.0 4 votes vote down vote up
private void test() {
  rule.launchActivity(null);
  onView(ViewMatchers.withId(R.id.textView))
      .check(ViewAssertions.matches(withText(IdlingUIActivity.FINISHED)));
  rule.finishActivity();
}
 
Example #17
Source File: AuthenticatorActivityTest.java    From google-authenticator-android with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@TargetApi(11)
@FixWhenMinSdkVersion(11) // Will be able to get rid of the contextual menu
private void openContextualActionBarOrMenuAndInvokeItem(
    ListView listView, int position, int menuItemTextId) {
  String menuItemText = activityTestRule.getActivity().getString(menuItemTextId);

  onView(equalTo(listView.getChildAt(position))).perform(longClick());

  // Force a UI synchronization by checking a trivial statement about the Root view
  onView(isRoot()).check(ViewAssertions.matches(isDisplayed()));
  // Now either the old fashioned context menu or CAB is open...
  // Check that the CAB appears to be active
  ActionMode actionMode = activityTestRule.getActivity().actionMode;
  assertThat(actionMode).isNotNull();

  // In either case, find the requested menu item and click it
  try {
    // Could be a text menu or an image menu (with content descriptions matching the text)
    onView(
            allOf(
                isDisplayed(),
                anyOf(withText(menuItemText), withContentDescription(menuItemText))))
        .perform(click());
  } catch (NoMatchingViewException e) {

    // Perhaps the menu item is buried in the overflow menu. Try opening that.
    // We can't use openActionBarOverflowOrOptionsMenu because a Toolbar is used instead of the
    // default ActionBar, so when the CAB show up it will be show on a different ActionBar that
    // overlay the Toolbar and there will be two different "OverflowMenuButton" on the Toolbar and
    // the CAB, which make the Espresso get an ambiguous exception failure.
    onView(
            allOf(
                withClassName(endsWith("OverflowMenuButton")),
                withParent(withParent(withClassName(endsWith("ActionBarContextView"))))))
        .perform(click());

    // Overflow menu items are only shown as text, and do not have the original MenuItem's id
    onView(withText(menuItemText)).perform(click());
  }
}
 
Example #18
Source File: PFCodeViewTest.java    From PFLockScreen-Android with Apache License 2.0 3 votes vote down vote up
@Test
public void pfCodeViewTest() {

    mViewTestRule.launchActivity(null);

    Espresso.onView(withId(R.id.code_view)).check(ViewAssertions.matches(
            PFViewMatchers.withCodeValue("")));

    for (int i = 0; i < CODE_LENGTH * 2; i ++) {
        Espresso.onView(withId(R.id.code_view)).perform(new PFCodeViewActionInput(i));
    }

    Espresso.onView(withId(R.id.code_view)).check(ViewAssertions.matches(
            PFViewMatchers.withCodeValue("01234")));

    Espresso.onView(withId(R.id.code_view)).check(ViewAssertions.matches(
            PFViewMatchers.withCodeLength(CODE_LENGTH)));

    Espresso.onView(withId(R.id.code_view)).perform(new PFCodeViewActionDelete());

    Espresso.onView(withId(R.id.code_view)).check(ViewAssertions.matches(
            PFViewMatchers.withCodeValue("0123")));

    Espresso.onView(withId(R.id.code_view)).check(ViewAssertions.matches(
            PFViewMatchers.withCodeLength(CODE_LENGTH - 1)));

    for (int i = 0; i < CODE_LENGTH * 3; i ++) {
        Espresso.onView(withId(R.id.code_view)).perform(new PFCodeViewActionDelete());
    }

    Espresso.onView(withId(R.id.code_view)).check(ViewAssertions.matches(
            PFViewMatchers.withCodeLength(0)));

    Espresso.onView(withId(R.id.code_view)).check(ViewAssertions.matches(
            PFViewMatchers.withCodeValue("")));


}