This source file includes following definitions.
- getAbsoluteLocationFromRelative
- sendAction
- singleClick
- singleClickView
- singleClickView
- sleepForDoubleTapTimeout
- longClickView
- longClickView
- dragStart
- dragTo
- dragEnd
- dragCompleteView
- performClickOnMainSync
- clickById
package org.chromium.content.browser.test.util;
import android.app.Activity;
import android.app.Instrumentation;
import android.os.SystemClock;
import android.test.InstrumentationTestCase;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
public class TestTouchUtils extends android.test.TouchUtils {
public static int[] getAbsoluteLocationFromRelative(View v, int x, int y) {
int location[] = new int[2];
v.getLocationOnScreen(location);
location[0] += x;
location[1] += y;
return location;
}
private static void sendAction(Instrumentation instrumentation, int action, long downTime,
float x, float y) {
long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, 0);
instrumentation.sendPointerSync(event);
instrumentation.waitForIdleSync();
}
public static void singleClick(Instrumentation instrumentation, float x, float y) {
long downTime = SystemClock.uptimeMillis();
sendAction(instrumentation, MotionEvent.ACTION_DOWN, downTime, x, y);
sendAction(instrumentation, MotionEvent.ACTION_UP, downTime, x, y);
}
public static void singleClickView(Instrumentation instrumentation, View v, int x, int y) {
int location[] = getAbsoluteLocationFromRelative(v, x, y);
int absoluteX = location[0];
int absoluteY = location[1];
singleClick(instrumentation, absoluteX, absoluteY);
}
public static void singleClickView(Instrumentation instrumentation, View v) {
int x = v.getWidth() / 2;
int y = v.getHeight() / 2;
singleClickView(instrumentation, v, x, y);
}
public static void sleepForDoubleTapTimeout(Instrumentation instrumentation) {
SystemClock.sleep((long) (ViewConfiguration.getDoubleTapTimeout() * 1.5));
}
public static void longClickView(Instrumentation instrumentation, View v, int x, int y) {
int location[] = getAbsoluteLocationFromRelative(v, x, y);
int absoluteX = location[0];
int absoluteY = location[1];
long downTime = SystemClock.uptimeMillis();
sendAction(instrumentation, MotionEvent.ACTION_DOWN, downTime, absoluteX, absoluteY);
SystemClock.sleep((long) (ViewConfiguration.getLongPressTimeout() * 1.5));
sendAction(instrumentation, MotionEvent.ACTION_UP, downTime, absoluteX, absoluteY);
}
public static void longClickView(Instrumentation instrumentation, View v) {
int x = v.getWidth() / 2;
int y = v.getHeight() / 2;
longClickView(instrumentation, v, x, y);
}
public static long dragStart(Instrumentation instrumentation, float x, float y) {
long downTime = SystemClock.uptimeMillis();
sendAction(instrumentation, MotionEvent.ACTION_DOWN, downTime, x, y);
return downTime;
}
public static void dragTo(Instrumentation instrumentation, 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(instrumentation, MotionEvent.ACTION_MOVE, downTime, x, y);
}
}
public static void dragEnd(Instrumentation instrumentation, float x, float y, long downTime) {
sendAction(instrumentation, MotionEvent.ACTION_UP, downTime, x, y);
}
public static void dragCompleteView(Instrumentation instrumentation, View view,
int fromX, int toX, int fromY, int toY, int stepCount) {
int fromLocation[] = getAbsoluteLocationFromRelative(view, fromX, fromY);
int toLocation[] = getAbsoluteLocationFromRelative(view, toX, toY);
long downTime = dragStart(instrumentation, fromLocation[0], fromLocation[1]);
dragTo(instrumentation, fromLocation[0], toLocation[0], fromLocation[1], toLocation[1],
stepCount, downTime);
dragEnd(instrumentation, toLocation[0], toLocation[1], downTime);
}
public static void performClickOnMainSync(Instrumentation instrumentation, final View v) {
instrumentation.runOnMainSync(new Runnable() {
@Override
public void run() {
v.performClick();
}
});
}
public static boolean clickById(InstrumentationTestCase test, Activity activity, int id) {
View v = activity.findViewById(id);
if (v == null) return false;
clickView(test, v);
return true;
}
}