This source file includes following definitions.
- dragStart
- dragTo
- dragEnd
- singleClick
- singleClickView
- singleClickView
- singleClickViewRelative
- longPress
- longPressView
- dispatchTouchEvent
- dispatchTouchEvent
- getAbsoluteLocationFromRelative
package org.chromium.content.browser.test.util;
import android.os.SystemClock;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
public class TouchCommon {
private ActivityInstrumentationTestCase2 mActivityTestCase;
public TouchCommon(ActivityInstrumentationTestCase2 activityTestCase) {
mActivityTestCase = activityTestCase;
}
public void dragStart(float x, float y, long downTime) {
MotionEvent event = MotionEvent.obtain(downTime, downTime,
MotionEvent.ACTION_DOWN, x, y, 0);
dispatchTouchEvent(event);
}
public void dragTo(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;
long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_MOVE, x, y, 0);
dispatchTouchEvent(event);
}
}
public void dragEnd(float x, float y, long downTime) {
long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_UP, x, y, 0);
dispatchTouchEvent(event);
}
public void singleClick(float x, float y) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_DOWN, x, y, 0);
dispatchTouchEvent(event);
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP,
x, y, 0);
dispatchTouchEvent(event);
}
public void singleClickView(View v, int x, int y) {
int location[] = getAbsoluteLocationFromRelative(v, x, y);
int absoluteX = location[0];
int absoluteY = location[1];
singleClick(absoluteX, absoluteY);
}
public void singleClickView(View v) {
singleClickView(v, v.getWidth() / 2, v.getHeight() / 2);
}
public void singleClickViewRelative(View view, int x, int y) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_DOWN, x, y, 0);
dispatchTouchEvent(view, event);
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP,
x, y, 0);
dispatchTouchEvent(view, event);
}
public void longPress(float x, float y) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_DOWN, x, y, 0);
dispatchTouchEvent(event);
int longPressTimeout = ViewConfiguration.get(
mActivityTestCase.getActivity()).getLongPressTimeout();
SystemClock.sleep(longPressTimeout * 2);
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP,
x, y, 0);
dispatchTouchEvent(event);
}
public void longPressView(View v, int x, int y) {
int location[] = getAbsoluteLocationFromRelative(v, x, y);
int absoluteX = location[0];
int absoluteY = location[1];
longPress(absoluteX, absoluteY);
}
private void dispatchTouchEvent(final MotionEvent event) {
View view =
mActivityTestCase.getActivity().findViewById(android.R.id.content).getRootView();
dispatchTouchEvent(view, event);
}
private void dispatchTouchEvent(final View view, final MotionEvent event) {
try {
mActivityTestCase.runTestOnUiThread(new Runnable() {
@Override
public void run() {
view.dispatchTouchEvent(event);
}
});
} catch (Throwable e) {
throw new RuntimeException("Dispatching touch event failed", e);
}
}
private 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;
}
}