androidx.test.espresso.action.Swipe Java Examples

The following examples show how to use androidx.test.espresso.action.Swipe. 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: TestUtilities.java    From google-authenticator-android with Apache License 2.0 6 votes vote down vote up
public static int dragViewToX(View v, int gravity, int toX) {

    if (gravity != Gravity.CENTER) {
      throw new IllegalArgumentException("Can only handle Gravity.CENTER.");
    }
    Point point = getCenterOfViewOnScreen(v);

    final int fromX = point.x;
    final int fromY = point.y;

    int deltaX = Math.abs(fromX - toX);

    onView(equalTo(v))
        .perform(
            new GeneralSwipeAction(
                Swipe.SLOW,
                (view) -> {
                  return new float[] {fromX, fromY};
                },
                (view) -> {
                  return new float[] {toX, fromY};
                },
                Press.FINGER));

    return deltaX;
  }
 
Example #2
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 #3
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 #4
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 #5
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 #6
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@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 #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: SwipeUtils.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
public static GeneralSwipeAction swipeDown(
    final int swipeX, final int swipeStartY, final int swipeAmountY) {
  return new GeneralSwipeAction(
      Swipe.SLOW,
      view -> new float[] {swipeX, swipeStartY},
      view -> new float[] {swipeX, swipeStartY + swipeAmountY},
      Press.FINGER);
}
 
Example #9
Source File: SwipeUtils.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
public static GeneralSwipeAction swipeUp(
    final int swipeX, final int swipeStartY, final int swipeAmountY) {
  return new GeneralSwipeAction(
      Swipe.SLOW,
      view -> new float[] {swipeX, swipeStartY},
      view -> new float[] {swipeX, swipeStartY - swipeAmountY},
      Press.FINGER);
}
 
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 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 #11
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 #12
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 #13
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 #14
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 #15
Source File: AuthenticatorActivityTest.java    From google-authenticator-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testReorderAccounts() {
  accountDb.add(
      "first", "7777777777777777", OtpType.HOTP, null, null, AccountDb.GOOGLE_ISSUER_NAME);
  accountDb.add("second", "6666666666666666", OtpType.TOTP, null, null, null);
  accountDb.add("third", "5555555555555555", OtpType.TOTP, null, null, "Yahoo");

  activityTestRule.launchActivity(null);

  EmptySpaceClickableDragSortListView userList =
      activityTestRule.getActivity().findViewById(R.id.user_list);
  View firstView = userList.getChildAt(0);
  View thirdView = userList.getChildAt(2);
  View firstDragHandle = firstView.findViewById(R.id.user_row_drag_handle);
  View thirdDragHandle = thirdView.findViewById(R.id.user_row_drag_handle);
  int dragHandleWidth = firstDragHandle.getWidth();
  int dragHandleHeight = firstDragHandle.getHeight();
  int[] firstDragHandleLocation = new int[2];
  int[] thirdDragHandleLocation = new int[2];
  firstDragHandle.getLocationOnScreen(firstDragHandleLocation);
  thirdDragHandle.getLocationOnScreen(thirdDragHandleLocation);
  float fromX = firstDragHandleLocation[0] + (dragHandleWidth / 2.0f);
  float fromY = firstDragHandleLocation[1] + (dragHandleHeight / 2.0f);
  float toX = thirdDragHandleLocation[0] + (dragHandleWidth / 2.0f);
  float toY = thirdDragHandleLocation[1] + (dragHandleHeight / 2.0f);

  onView(equalTo(firstView))
      .perform(longClick())
      .perform(
          new GeneralSwipeAction(
              Swipe.SLOW,
              (view) -> {
                return new float[] {fromX, fromY};
              },
              (view) -> {
                return new float[] {toX, toY};
              },
              Press.FINGER));

  ListAdapter listAdapter = userList.getAdapter();
  PinInfo firstPinInfo = (PinInfo) listAdapter.getItem(0);
  PinInfo secondPinInfo = (PinInfo) listAdapter.getItem(1);
  PinInfo thirdPinInfo = (PinInfo) listAdapter.getItem(2);
  assertThat(firstPinInfo.getIndex().getName()).isEqualTo("second");
  assertThat(secondPinInfo.getIndex().getName()).isEqualTo("third");
  assertThat(thirdPinInfo.getIndex().getName()).isEqualTo("first");
}
 
Example #16
Source File: Utils.java    From BottomNavigation with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an action that performs a swipe right-to-left across the vertical center of the
 * view. The swipe doesn't start at the very edge of the view, but is a bit offset.<br>
 * <br>
 * View constraints:
 * <ul>
 * <li>must be displayed on screen
 * <ul>
 */
public static ViewAction swipeLeftSlow() {
    return actionWithAssertions(new GeneralSwipeAction(Swipe.SLOW,
            translate(GeneralLocation.CENTER_RIGHT, -EDGE_FUZZ_FACTOR, 0),
            GeneralLocation.CENTER_LEFT, Press.FINGER));
}
 
Example #17
Source File: Utils.java    From BottomNavigation with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an action that performs a swipe left-to-right across the vertical center of the
 * view. The swipe doesn't start at the very edge of the view, but is a bit offset.<br>
 * <br>
 * View constraints:
 * <ul>
 * <li>must be displayed on screen
 * <ul>
 */
public static ViewAction swipeRightSlow() {
    return actionWithAssertions(new GeneralSwipeAction(Swipe.SLOW,
            translate(GeneralLocation.CENTER_LEFT, EDGE_FUZZ_FACTOR, 0),
            GeneralLocation.CENTER_RIGHT, Press.FINGER));
}
 
Example #18
Source File: Utils.java    From BottomNavigation with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an action that performs a swipe top-to-bottom across the horizontal center of the view.
 * The swipe doesn't start at the very edge of the view, but has a bit of offset.<br>
 * <br>
 * View constraints:
 * <ul>
 * <li>must be displayed on screen
 * <ul>
 */
public static ViewAction swipeDownSlow() {
    return actionWithAssertions(new GeneralSwipeAction(Swipe.SLOW,
            translate(GeneralLocation.TOP_CENTER, 0, EDGE_FUZZ_FACTOR),
            GeneralLocation.BOTTOM_CENTER, Press.FINGER));
}
 
Example #19
Source File: Utils.java    From BottomNavigation with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an action that performs a swipe bottom-to-top across the horizontal center of the view.
 * The swipe doesn't start at the very edge of the view, but has a bit of offset.<br>
 * <br>
 * View constraints:
 * <ul>
 * <li>must be displayed on screen
 * <ul>
 */
public static ViewAction swipeUpSlow() {
    return actionWithAssertions(new GeneralSwipeAction(Swipe.SLOW,
            translate(GeneralLocation.BOTTOM_CENTER, 0, -EDGE_FUZZ_FACTOR),
            GeneralLocation.TOP_CENTER, Press.FINGER));
}
 
Example #20
Source File: SwipeOpenItemTouchHelperTest.java    From SwipeOpenItemTouchHelper with Apache License 2.0 2 votes vote down vote up
/**
 * Uses a slow swipe to simulate a scroll
 *
 * @return the view action
 */
private ViewAction scroll() {
  return new GeneralSwipeAction(Swipe.SLOW, GeneralLocation.BOTTOM_CENTER,
      GeneralLocation.TOP_CENTER, Press.FINGER);
}