Robotium 相当于 Espresso 中的 drag()?

Robotium equivalent of drag() in Espresso?

我正在使用 Espresso 测试我的应用程序,我需要在 RecyclerView 中测试拖放行为。不幸的是,我在 Espresso 中看不到任何 drag() 操作,我是不是漏掉了什么?

我没有找到任何 Espresso 拖动选项。只有 swipeUp()swipeDown()swipeLwft()swipeRight(),在那种情况下可能没有用。

此外,在 Google 组中存在一个没有答案的问题:https://groups.google.com/forum/#!msg/android-test-kit-discuss/X3TDJBUT4Ho/OJAOkTOoxGcJ

虽然也有Espresso MotionEventsclass和sendMovement()的方法,但是我没试过

使用下面的代码:

 public static void drag(Instrumentation inst, float fromX, float toX, float fromY,
                            float toY, int stepCount) {
        long downTime = SystemClock.uptimeMillis();
        long eventTime = SystemClock.uptimeMillis();

        float y = fromY;
        float x = fromX;

        float yStep = (toY - fromY) / stepCount;
        float xStep = (toX - fromX) / stepCount;

        MotionEvent event = MotionEvent.obtain(downTime, eventTime,
                MotionEvent.ACTION_DOWN, x, y, 0);
        inst.sendPointerSync(event);
        for (int i = 0; i < stepCount; ++i) {
            y += yStep;
            x += xStep;
            eventTime = SystemClock.uptimeMillis();
            event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0);
            inst.sendPointerSync(event);
        }

        eventTime = SystemClock.uptimeMillis();
        event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);
        inst.sendPointerSync(event);
        inst.waitForIdleSync();
    }

另请参阅 TouchUtils(已从 API 24 弃用):https://developer.android.com/reference/android/test/TouchUtils.html

希望对您有所帮助