Index: content/public/test/android/javatests/src/org/chromium/content/browser/test/util/TouchCommon.java |
diff --git a/content/public/test/android/javatests/src/org/chromium/content/browser/test/util/TouchCommon.java b/content/public/test/android/javatests/src/org/chromium/content/browser/test/util/TouchCommon.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..757fd21980f857862e21a21a4475c611537417d6 |
--- /dev/null |
+++ b/content/public/test/android/javatests/src/org/chromium/content/browser/test/util/TouchCommon.java |
@@ -0,0 +1,193 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.content.browser.test.util; |
+ |
+import android.app.Activity; |
+import android.os.SystemClock; |
+import android.view.MotionEvent; |
+import android.view.View; |
+import android.view.ViewConfiguration; |
+ |
+import org.chromium.content.browser.test.util.UiUtils; |
+ |
+/** |
+ * Touch-related functionality reused across test cases. |
+ */ |
+public class TouchCommon { |
+ private Activity mActivity; |
+ |
+ public TouchCommon(Activity activity) { |
+ mActivity = activity; |
+ } |
+ |
+ /** |
+ * Starts (synchronously) a drag motion. Normally followed by dragTo() and dragEnd(). |
+ * |
+ * @param x |
+ * @param y |
+ * @param downTime (in ms) |
+ * @see TouchUtils |
+ */ |
+ public void dragStart(float x, float y, long downTime) { |
+ MotionEvent event = MotionEvent.obtain(downTime, downTime, |
+ MotionEvent.ACTION_DOWN, x, y, 0); |
+ dispatchTouchEvent(event); |
+ } |
+ |
+ /** |
+ * Drags / moves (synchronously) to the specified coordinates. Normally preceeded by |
+ * dragStart() and followed by dragEnd() |
+ * |
+ * @param fromX |
+ * @param toX |
+ * @param fromY |
+ * @param toY |
+ * @param stepCount |
+ * @param downTime (in ms) |
+ * @see TouchUtils |
+ */ |
+ 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); |
+ } |
+ } |
+ |
+ /** |
+ * Finishes (synchronously) a drag / move at the specified coordinate. |
+ * Normally preceeded by dragStart() and dragTo(). |
+ * |
+ * @param x |
+ * @param y |
+ * @param downTime (in ms) |
+ * @see TouchUtils |
+ */ |
+ 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); |
+ } |
+ |
+ /** |
+ * Sends (synchronously) a single click to an absolute screen coordinates. |
+ * |
+ * @param x screen absolute |
+ * @param y screen absolute |
+ * @see TouchUtils |
+ */ |
+ 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); |
+ } |
+ |
+ /** |
+ * Sends (synchronously) a single click to the View at the specified coordinates. |
+ * |
+ * @param v The view to be clicked. |
+ * @param x Relative x location to v |
+ * @param y Relative y location to v |
+ */ |
+ 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); |
+ } |
+ |
+ /** |
+ * Sends (synchronously) a long press to an absolute screen coordinates. |
+ * |
+ * @param x screen absolute |
+ * @param y screen absolute |
+ * @see TouchUtils |
+ */ |
+ 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(mActivity).getLongPressTimeout(); |
+ |
+ // Long press is flaky with just longPressTimeout. Doubling the time to be safe. |
+ SystemClock.sleep(longPressTimeout * 2); |
+ |
+ eventTime = SystemClock.uptimeMillis(); |
+ event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, |
+ x, y, 0); |
+ dispatchTouchEvent(event); |
+ } |
+ |
+ /** |
+ * Sends (synchronously) a long press to the View at the specified coordinates. |
+ * |
+ * @param v The view to be clicked. |
+ * @param x Relative x location to v |
+ * @param y Relative y location to v |
+ */ |
+ 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); |
+ } |
+ |
+ /** |
+ * Send a MotionEvent to the root view of the activity. |
+ * @param event |
+ */ |
+ private void dispatchTouchEvent(final MotionEvent event) { |
+ final View view = mActivity.findViewById(android.R.id.content).getRootView(); |
+ try { |
+ UiUtils.runOnUiThread(mActivity, new Runnable() { |
+ @Override |
+ public void run() { |
+ view.dispatchTouchEvent(event); |
+ } |
+ }); |
+ } catch(Throwable e) { |
+ throw new RuntimeException("Dispatching touch event failed", e); |
+ } |
+ } |
+ |
+ /** |
+ * Returns the absolute location in screen coordinates from location relative |
+ * to view. |
+ * @param v The view the coordinates are relative to. |
+ * @param x Relative x location. |
+ * @param y Relative y location. |
+ * @return absolute x and y location in an array. |
+ */ |
+ 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; |
+ } |
+} |