Drag & Drop Espresso

前端 未结 2 662
轮回少年
轮回少年 2021-01-04 13:50

Is it possible to perform drag & drop action via Espresso? I need to move one view down (in straight line) in order to accept some conditions in my automation test.

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 14:19

    Thats how I have done it. You have more access to what should happen with your view like that. But accepted answer perform drag&drop too.

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

提交回复
热议问题