testing - 如何使用 Espresso 执行多点触控滑动?

标签 testing android-espresso gui-testing ui-testing

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

最佳答案

正如@Daniel建议的那样,我创建了MotionEvents的自定义版本,因为当使用多个手指时,您必须注入(inject)ACTION_POINTER_DOWN/UP而不是ACTION_DOWN/UP。 我创建了 LinearSwipe 的双指版本,如下所示:

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;
}

现在一切正常了!谢谢@丹尼尔

关于testing - 如何使用 Espresso 执行多点触控滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29033873/

相关文章:

php - 问题模拟 Laravel 可引导模型特征

ios - 在 iOS8 设备上运行测试时如何禁用 UITesting?

testing - 集成和单元测试 Nifi 过程组

android - JUnit4 - AssertionFailedError : No tests found

Android Espresso onData 奇怪的行为

java - 是否有任何可能有助于理解框架的示例 GUI 和 FEST 代码?

在不窃取桌面焦点的情况下是否可以进行 Java 回归测试(涉及 AWT)?

automated-tests - 如何对devexpress控件进行自动化的UI测试?

ruby-on-rails - 使用 I18N 数据库后端测试 Rails 应用程序

java - Android 测试构建错误 : Multiple dex files define Landroid/support/test/BuildConfig