我如何使用Espresso进行多点触控滑动?

问题描述 投票:1回答:2

如何使用Espresso执行多点触控滑动?例如用两根手指向右滑动。

testing android-espresso gui-testing ui-testing
2个回答
2
投票

按照@Daniel的建议,我创建了MotionEvents的自定义版本,因为使用多个手指时,您必须注入ACTION_POINTER_DOWN / UP而不是ACTION_DOWN / UP。我创建了LinearFipe的twoFinger版本,如下所示:

private static Swiper.Status sendLinearSwipe(UiController uiController,
                                             float[] startCoordinates,
                                             float[] startCoordinatesSecondFinger,
                                             float[] endCoordinates,
                                             float[]endCoordinatesSecondFinger,
                                             float[] precision,
                                             float[] precisionSecond,
                                             int duration) {
    checkNotNull(uiController);
    checkNotNull(startCoordinates);
    checkNotNull(startCoordinatesSecondFinger);
    checkNotNull(endCoordinates);
    checkNotNull(endCoordinatesSecondFinger);
    checkNotNull(precision);
    checkNotNull(precisionSecond);

    float[][] steps = interpolate(startCoordinates, endCoordinates, SWIPE_EVENT_COUNT);
    float[][] stepsSecondFinger = interpolate(startCoordinatesSecondFinger, endCoordinatesSecondFinger, SWIPE_EVENT_COUNT);
    final int delayBetweenMovements = duration / steps.length;
    final int delayBetweenMovementsSecondFinger = duration / stepsSecondFinger.length;

    int maxLength=Math.min(steps.length, stepsSecondFinger.length);

    MotionEvent downEvent;
    downEvent = MotionEvents.sendDown(uiController, steps[0], precision,true).down;
    MotionEvent downEventSecondFinger;
    downEventSecondFinger = MotionEvents.sendDown(uiController,stepsSecondFinger[0], precisionSecond,false).down;

    try {
        for (int i = 1; i < maxLength; i++) {

            if (sendMovement(uiController, steps[i], downEvent)) return Status.FAILURE;
            if (sendMovement(uiController, stepsSecondFinger[i], downEventSecondFinger)) return Status.FAILURE;


            long desiredTime = downEvent.getDownTime() + delayBetweenMovements * i;
            long desiredTimeSecondFinger = downEventSecondFinger.getDownTime() + delayBetweenMovementsSecondFinger * i;

            long timeUntilDesired = desiredTime - SystemClock.uptimeMillis();
            long timeUntilDesiredSecondFinger = desiredTimeSecondFinger - SystemClock.uptimeMillis();
            loopMainThread(uiController, timeUntilDesired);
            loopMainThread(uiController, timeUntilDesiredSecondFinger);

        }

        if (!MotionEvents.sendUp(uiController, downEventSecondFinger, endCoordinatesSecondFinger,false)) {
            Log.e(TAG, "Injection of up event as part of the swipe failed. Sending cancel event.");
            MotionEvents.sendCancel(uiController, downEventSecondFinger);
            return Swiper.Status.FAILURE;
        }
    } finally {
        downEvent.recycle();
        downEventSecondFinger.recycle();
    }
    return Swiper.Status.SUCCESS;
}

private static void loopMainThread(UiController uiController, long timeUntilDesired) {
    if (timeUntilDesired > 10) {
        uiController.loopMainThreadForAtLeast(timeUntilDesired);
    }
}

private static boolean sendMovement(UiController uiController, float[] step, MotionEvent downEvent) {
    if (!MotionEvents.sendMovement(uiController, downEvent, step)) {
        Log.e(TAG, "Injection of move event as part of the swipe failed. Sending cancel event.");
        MotionEvents.sendCancel(uiController, downEvent);
        return true;
    }
    return false;
}

现在,它运行完美!谢谢@丹尼尔


1
投票

Espresso不提供该功能,但是您可以自己完成此操作

  • 注入两个中断事件
  • 注入几对运动事件,每个手指一个]
  • 注入两个向上事件
  • Espresso有一些实用程序可以使它变得更容易。特别地,MotionEvents类具有一些用于创建和注入这些低级事件的辅助方法。

您可能希望参考sendLinearSwipe代码,其中包含用于单次轻扫的大部分逻辑。

如果将其写为sendLinearSwipe,它将完全适合Espresso框架(例如,您可以轻松地让它等待空闲的资源)。

© www.soinside.com 2019 - 2024. All rights reserved.