private static CoordinatesProvider getCoordinatesProvider(final int x, final int y){ return 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; float[] coordinates = {screenX, screenY}; return coordinates; } }; }
@Test @MediumTest public void testSwipeUpToExpand() { Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet)) .perform( DesignViewActions.withCustomConstraints( new GeneralSwipeAction( Swipe.FAST, GeneralLocation.VISIBLE_CENTER, new CoordinatesProvider() { @Override public float[] calculateCoordinates(View view) { return 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(); } }
/** * 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); }
private static 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; float[] coordinates = {screenX, screenY}; return coordinates; } }, Press.FINGER); }
public static 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; float[] coordinates = {screenX, screenY}; return coordinates; } }, Press.FINGER); }
/** * Translates the given coordinates by the given distances. The distances are given in term * of the view's size -- 1.0 means to translate by an amount equivalent to the view's length. */ static CoordinatesProvider translate(final CoordinatesProvider coords, final float dx, final float dy) { return new CoordinatesProvider() { @Override public float[] calculateCoordinates(View view) { float xy[] = coords.calculateCoordinates(view); xy[0] += dx * view.getWidth(); xy[1] += dy * view.getHeight(); return xy; } }; }
static ViewAction swipeRightNotReachingThreshold(Context context) { final float x = getWidthScreen(context) * 0.3f; return new GeneralSwipeAction(Swipe.SLOW, GeneralLocation.TOP_LEFT, new CoordinatesProvider() { @Override public float[] calculateCoordinates(View view) { return new float[] {x, 0f}; } }, Press.FINGER); }
static ViewAction swipeRightReachingThreshold(Context context) { final float x = getWidthScreen(context) * 0.8f; return new GeneralSwipeAction(Swipe.SLOW, GeneralLocation.TOP_LEFT, new CoordinatesProvider() { @Override public float[] calculateCoordinates(View view) { return new float[] {x, 0f}; } }, Press.FINGER); }
static ViewAction swipeDownNotReachingThreshold(Context context) { final float y = getHeightScreen(context) * 0.3f; return new GeneralSwipeAction(Swipe.SLOW, GeneralLocation.TOP_LEFT, new CoordinatesProvider() { @Override public float[] calculateCoordinates(View view) { return new float[] {0f, y}; } }, Press.FINGER); }
static ViewAction swipeDownReachingThreshold(Context context) { final float y = getHeightScreen(context) * 0.8f; return new GeneralSwipeAction(Swipe.SLOW, GeneralLocation.TOP_LEFT, new CoordinatesProvider() { @Override public float[] calculateCoordinates(View view) { return new float[] {0f, y}; } }, Press.FINGER); }
@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, new CoordinatesProvider() { @Override public float[] calculateCoordinates(View view) { return 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(); } }
@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, new CoordinatesProvider() { @Override public float[] calculateCoordinates(View view) { return 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(); } }
@Test @MediumTest public void testInvisible() throws Throwable { // Make the bottomsheet invisible activityTestRule.runOnUiThread( new Runnable() { @Override public void run() { 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, new CoordinatesProvider() { @Override public float[] calculateCoordinates(View view) { return new float[] {view.getWidth() / 2, 0}; } }, Press.FINGER), not(ViewMatchers.isDisplayed()))); // Check that the bottom sheet stays the same collapsed state activityTestRule.runOnUiThread( new Runnable() { @Override public void run() { assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_COLLAPSED)); } }); }
public NoConstraintsSwipeAction(Swiper swiper, CoordinatesProvider startCoordinatesProvider, CoordinatesProvider endCoordinatesProvider, PrecisionDescriber precisionDescriber) { this.swiper = swiper; this.startCoordinatesProvider = startCoordinatesProvider; this.endCoordinatesProvider = endCoordinatesProvider; this.precisionDescriber = precisionDescriber; }
ViewAction swipeAwayRight() { return actionWithAssertions(new GeneralSwipeAction(Swipe.FAST, GeneralLocation.CENTER, new CoordinatesProvider() { @Override public float[] calculateCoordinates(View view) { float xy[] = GeneralLocation.CENTER_RIGHT.calculateCoordinates(view); xy[0] += 20f * view.getWidth(); return xy; } }, Press.FINGER)); }
public GeneralZoomAction(Zoomer zoomer, CoordinatesProvider[] startCoordinatesProviders, CoordinatesProvider[] endCoordinatesProviders, PrecisionDescriber precisionDescriber) { this.zoomer = zoomer; this.startCoordinatesProviders = startCoordinatesProviders; this.endCoordinatesProviders = endCoordinatesProviders; this.precisionDescriber = precisionDescriber; }
public DragAction( CoordinatesProvider start, CoordinatesProvider end, PrecisionDescriber precisionDescriber) { this.start = start; this.end = end; this.precisionDescriber = precisionDescriber; }
private CustomClickAction(Tapper tapper, CoordinatesProvider coordinatesProvider, PrecisionDescriber precisionDescriber) { this.coordinatesProvider = coordinatesProvider; this.tapper = tapper; this.precisionDescriber = precisionDescriber; }
private SwipeDirection(CoordinatesProvider start, CoordinatesProvider end) { this.start = start; this.end = end; }
/** * Variadic parameters to simplify the look of this function. * First two assumed to be start locations, * next two assumed to be end location, * and the rest are ignored. */ private ZoomDirection(CoordinatesProvider... providers) { this.start = new CoordinatesProvider[]{ providers[0], providers[1] }; this.end = new CoordinatesProvider[]{ providers[2], providers[3] }; }