This source file includes following definitions.
- sendAction
- dragStart
- dragTo
- dragEnd
- dragCompleteView
package org.chromium.android_webview.test.util;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import java.util.concurrent.CountDownLatch;
public class AwTestTouchUtils {
private static void sendAction(View view, int action, long downTime, float x, float y) {
long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, 0);
view.onTouchEvent(event);
}
private static long dragStart(View view, float x, float y) {
long downTime = SystemClock.uptimeMillis();
sendAction(view, MotionEvent.ACTION_DOWN, downTime, x, y);
return downTime;
}
private static void dragTo(View view, float fromX, float toX, float fromY,
float toY, int stepCount, long downTime) {
float x = fromX;
float y = fromY;
float yStep = (toY - fromY) / stepCount;
float xStep = (toX - fromX) / stepCount;
for (int i = 0; i < stepCount; ++i) {
y += yStep;
x += xStep;
sendAction(view, MotionEvent.ACTION_MOVE, downTime, x, y);
}
}
private static void dragEnd(View view, float x, float y, long downTime) {
sendAction(view, MotionEvent.ACTION_UP, downTime, x, y);
}
public static void dragCompleteView(final View view, final int fromX, final int toX,
final int fromY, final int toY, final int stepCount,
final CountDownLatch completionLatch) {
view.post(new Runnable() {
@Override
public void run() {
long downTime = dragStart(view, fromX, fromY);
dragTo(view, fromX, toX, fromY, toY, stepCount, downTime);
dragEnd(view, toX, toY, downTime);
if (completionLatch != null) {
completionLatch.countDown();
}
}
});
}
}