问题
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.
回答1:
You can use GeneralSwipeAction to perform drag & drop.
public static ViewAction swipeUp() {
return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.BOTTOM_CENTER,
GeneralLocation.TOP_CENTER, Press.FINGER);
}
You can customize the location to meet your requirement as well.
回答2:
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();
}
来源:https://stackoverflow.com/questions/35297442/drag-drop-espresso