android.support.test.espresso.action.Tap Java Examples

The following examples show how to use android.support.test.espresso.action.Tap. 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: ItSelectMultiPointsActivity.java    From SmoothClicker with 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 #2
Source File: ItClickerActivity.java    From SmoothClicker with 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 #3
Source File: ApplicationTest.java    From CompactCalendarView with MIT License 6 votes vote down vote up
public ViewAction clickXY(final float x, final float y){
    final DisplayMetrics dm = activityRule.getActivity().getResources().getDisplayMetrics() ;
    final float spX = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, x, dm);
    final float spY = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, y, dm);
    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] + spX;
                    final float screenY = screenPos[1] + spY;
                    float[] coordinates = {screenX, screenY};

                    return coordinates;
                }
            },
            Press.FINGER);
}
 
Example #4
Source File: CustomClickAction.java    From android-espresso-revealed with Apache License 2.0 5 votes vote down vote up
public static ViewAction clickElementWithVisibility(final int viewVisibility) {
    checkNotNull(viewVisibility);
    if ((viewVisibility > 0) && (viewVisibility <= 100)) {
        visibility = viewVisibility;
    }
    return new CustomClickAction(Tap.SINGLE, GeneralLocation.TOP_CENTER, Press.FINGER);
}
 
Example #5
Source File: CustomClickAction.java    From android-espresso-revealed with Apache License 2.0 4 votes vote down vote up
@Override
public void perform(UiController uiController, View view) {
    float[] coordinates = coordinatesProvider.calculateCoordinates(view);
    float[] precision = precisionDescriber.describePrecision();

    Tapper.Status status = Tapper.Status.FAILURE;
    int loopCount = 0;
    // Native event injection is quite a tricky process. A tap is actually 2
    // seperate motion events which need to get injected into the system. Injection
    // makes an RPC call from our app under test to the Android system server, the
    // system server decides which window layer to deliver the event to, the system
    // server makes an RPC to that window layer, that window layer delivers the event
    // to the correct UI element, activity, or window object. Now we need to repeat
    // that 2x. for a simple down and up. Oh and the down event triggers timers to
    // detect whether or not the event is a long vs. short press. The timers are
    // removed the moment the up event is received (NOTE: the possibility of eventTime
    // being in the future is totally ignored by most motion event processors).
    //
    // Phew.
    //
    // The net result of this is sometimes we'll want to do a regular tap, and for
    // whatever reason the up event (last half) of the tap is delivered after long
    // press timeout (depending on system load) and the long press behaviour is
    // displayed (EG: show a context menu). There is no way to avoid or handle this more
    // gracefully. Also the longpress behavour is app/widget specific. So if you have
    // a seperate long press behaviour from your short press, you can pass in a
    // 'RollBack' ViewAction which when executed will undo the effects of long press.

    while (status != Tapper.Status.SUCCESS && loopCount < 3) {
        try {
            status = tapper.sendTap(uiController, coordinates, precision);
        } catch (RuntimeException re) {
            throw new PerformException.Builder()
                    .withActionDescription(this.getDescription())
                    .withViewDescription(HumanReadables.describe(view))
                    .withCause(re)
                    .build();
        }

        // ensures that all work enqueued to process the tap has been run.
        uiController.loopMainThreadForAtLeast(ViewConfiguration.getPressedStateDuration());
        if (status == Tapper.Status.WARNING) {
            if (rollbackAction.isPresent()) {
                rollbackAction.get().perform(uiController, view);
            } else {
                break;
            }
        }
        loopCount++;
    }
    if (status == Tapper.Status.FAILURE) {
        throw new PerformException.Builder()
                .withActionDescription(this.getDescription())
                .withViewDescription(HumanReadables.describe(view))
                .withCause(new RuntimeException(String.format("Couldn't "
                                + "click at: %s,%s precision: %s, %s . Tapper: %s coordinate provider: %s precision " +
                                "describer: %s. Tried %s times. With Rollback? %s", coordinates[0], coordinates[1],
                        precision[0], precision[1], tapper, coordinatesProvider, precisionDescriber, loopCount,
                        rollbackAction.isPresent())))
                .build();
    }

    if (tapper == Tap.SINGLE && view instanceof WebView) {
        // WebViews will not process click events until double tap
        // timeout. Not the best place for this - but good for now.
        uiController.loopMainThreadForAtLeast(ViewConfiguration.getDoubleTapTimeout());
    }
}
 
Example #6
Source File: ViewActions.java    From material-activity-chooser with Apache License 2.0 4 votes vote down vote up
public static ViewAction clickOnTop() {
    return new GeneralClickAction(Tap.SINGLE, GeneralLocation.TOP_CENTER, Press.FINGER);
}
 
Example #7
Source File: CustomViewActions.java    From easy-adapter with Apache License 2.0 4 votes vote down vote up
public static ViewAction clickOnChild(int childViewId) {
    return actionWithAssertions((new ChildClickAction(
            new GeneralClickAction(Tap.SINGLE, GeneralLocation.VISIBLE_CENTER, Press.FINGER),
            childViewId)));
}