The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.test; |
| 18 | |
Charles Chen | 49f329c | 2020-02-13 16:41:32 +0800 | [diff] [blame] | 19 | import static android.view.WindowInsets.Type.displayCutout; |
| 20 | import static android.view.WindowInsets.Type.navigationBars; |
| 21 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.app.Activity; |
| 23 | import android.app.Instrumentation; |
Charles Chen | 49f329c | 2020-02-13 16:41:32 +0800 | [diff] [blame] | 24 | import android.graphics.Insets; |
| 25 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.os.SystemClock; |
Charles Chen | 49f329c | 2020-02-13 16:41:32 +0800 | [diff] [blame] | 27 | import android.util.Size; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.view.Gravity; |
| 29 | import android.view.MotionEvent; |
| 30 | import android.view.View; |
| 31 | import android.view.ViewConfiguration; |
| 32 | import android.view.ViewGroup; |
Charles Chen | 49f329c | 2020-02-13 16:41:32 +0800 | [diff] [blame] | 33 | import android.view.WindowInsets; |
| 34 | import android.view.WindowManager; |
| 35 | import android.view.WindowMetrics; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Reusable methods for generating touch events. These methods can be used with |
| 39 | * InstrumentationTestCase or ActivityInstrumentationTestCase2 to simulate user interaction with |
| 40 | * the application through a touch screen. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 41 | * |
| 42 | * @deprecated Use |
| 43 | * <a href="{@docRoot}training/testing/ui-testing/espresso-testing.html">Espresso UI testing |
| 44 | * framework</a> instead. New tests should be written using the |
| 45 | * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | */ |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 47 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | public class TouchUtils { |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 49 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | /** |
| 51 | * Simulate touching in the center of the screen and dragging one quarter of the way down |
| 52 | * @param test The test case that is being run |
| 53 | * |
| 54 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 55 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 56 | * configuring the Activity under test |
| 57 | */ |
| 58 | @Deprecated |
| 59 | public static void dragQuarterScreenDown(ActivityInstrumentationTestCase test) { |
| 60 | dragQuarterScreenDown(test, test.getActivity()); |
| 61 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 62 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Simulate touching in the center of the screen and dragging one quarter of the way down |
| 65 | * @param test The test case that is being run |
| 66 | * @param activity The activity that is in the foreground of the test case |
| 67 | */ |
| 68 | public static void dragQuarterScreenDown(InstrumentationTestCase test, Activity activity) { |
Charles Chen | 49f329c | 2020-02-13 16:41:32 +0800 | [diff] [blame] | 69 | WindowManager wm = activity.getWindowManager(); |
| 70 | final Size size = getSizeExcludingNavigationBarAndCutout(wm.getCurrentWindowMetrics()); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 71 | |
Charles Chen | 49f329c | 2020-02-13 16:41:32 +0800 | [diff] [blame] | 72 | final float x = size.getWidth() / 2.0f; |
| 73 | final float fromY = size.getHeight() * 0.5f; |
| 74 | final float toY = size.getHeight() * 0.75f; |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 75 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | drag(test, x, x, fromY, toY, 4); |
| 77 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | /** |
| 80 | * Simulate touching in the center of the screen and dragging one quarter of the way up |
| 81 | * @param test The test case that is being run |
| 82 | * |
| 83 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 84 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 85 | * configuring the Activity under test |
| 86 | */ |
| 87 | @Deprecated |
| 88 | public static void dragQuarterScreenUp(ActivityInstrumentationTestCase test) { |
| 89 | dragQuarterScreenUp(test, test.getActivity()); |
| 90 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 91 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Simulate touching in the center of the screen and dragging one quarter of the way up |
| 94 | * @param test The test case that is being run |
| 95 | * @param activity The activity that is in the foreground of the test case |
| 96 | */ |
| 97 | public static void dragQuarterScreenUp(InstrumentationTestCase test, Activity activity) { |
Charles Chen | 49f329c | 2020-02-13 16:41:32 +0800 | [diff] [blame] | 98 | WindowManager wm = activity.getWindowManager(); |
| 99 | final Size size = getSizeExcludingNavigationBarAndCutout(wm.getCurrentWindowMetrics()); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 100 | |
Charles Chen | 49f329c | 2020-02-13 16:41:32 +0800 | [diff] [blame] | 101 | final float x = size.getWidth() / 2.0f; |
| 102 | final float fromY = size.getHeight() * 0.5f; |
| 103 | final float toY = size.getHeight() * 0.25f; |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | drag(test, x, x, fromY, toY, 4); |
| 106 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 107 | |
Charles Chen | 49f329c | 2020-02-13 16:41:32 +0800 | [diff] [blame] | 108 | private static Size getSizeExcludingNavigationBarAndCutout(WindowMetrics windowMetrics) { |
| 109 | WindowInsets windowInsets = windowMetrics.getWindowInsets(); |
| 110 | final Insets insetsWithCutout = windowInsets |
| 111 | .getInsetsIgnoringVisibility(navigationBars() | displayCutout()); |
| 112 | final int insetsWidth = insetsWithCutout.left + insetsWithCutout.right; |
| 113 | final int insetsHeight = insetsWithCutout.top + insetsWithCutout.bottom; |
| 114 | |
| 115 | Rect bounds = windowMetrics.getBounds(); |
| 116 | return new Size(bounds.width() - insetsWidth, bounds.height() - insetsHeight); |
| 117 | } |
| 118 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | /** |
| 120 | * Scroll a ViewGroup to the bottom by repeatedly calling |
| 121 | * {@link #dragQuarterScreenUp(InstrumentationTestCase, Activity)} |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 122 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | * @param test The test case that is being run |
| 124 | * @param v The ViewGroup that should be dragged |
| 125 | * |
| 126 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 127 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 128 | * configuring the Activity under test |
| 129 | */ |
| 130 | @Deprecated |
| 131 | public static void scrollToBottom(ActivityInstrumentationTestCase test, ViewGroup v) { |
| 132 | scrollToBottom(test, test.getActivity(), v); |
| 133 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 134 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | /** |
| 136 | * Scroll a ViewGroup to the bottom by repeatedly calling |
| 137 | * {@link #dragQuarterScreenUp(InstrumentationTestCase, Activity)} |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 138 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | * @param test The test case that is being run |
| 140 | * @param activity The activity that is in the foreground of the test case |
| 141 | * @param v The ViewGroup that should be dragged |
| 142 | */ |
| 143 | public static void scrollToBottom(InstrumentationTestCase test, Activity activity, |
| 144 | ViewGroup v) { |
Yigit Boyar | de8d789 | 2014-09-22 15:22:45 -0700 | [diff] [blame] | 145 | ViewStateSnapshot prev; |
| 146 | ViewStateSnapshot next = new ViewStateSnapshot(v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | do { |
Yigit Boyar | de8d789 | 2014-09-22 15:22:45 -0700 | [diff] [blame] | 148 | prev = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | TouchUtils.dragQuarterScreenUp(test, activity); |
Yigit Boyar | de8d789 | 2014-09-22 15:22:45 -0700 | [diff] [blame] | 150 | next = new ViewStateSnapshot(v); |
| 151 | } while (!prev.equals(next)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Scroll a ViewGroup to the top by repeatedly calling |
| 156 | * {@link #dragQuarterScreenDown(InstrumentationTestCase, Activity)} |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 157 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | * @param test The test case that is being run |
| 159 | * @param v The ViewGroup that should be dragged |
| 160 | * |
| 161 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 162 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 163 | * configuring the Activity under test |
| 164 | */ |
| 165 | @Deprecated |
| 166 | public static void scrollToTop(ActivityInstrumentationTestCase test, ViewGroup v) { |
| 167 | scrollToTop(test, test.getActivity(), v); |
| 168 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | /** |
| 171 | * Scroll a ViewGroup to the top by repeatedly calling |
| 172 | * {@link #dragQuarterScreenDown(InstrumentationTestCase, Activity)} |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 173 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | * @param test The test case that is being run |
| 175 | * @param activity The activity that is in the foreground of the test case |
| 176 | * @param v The ViewGroup that should be dragged |
| 177 | */ |
| 178 | public static void scrollToTop(InstrumentationTestCase test, Activity activity, ViewGroup v) { |
Yigit Boyar | de8d789 | 2014-09-22 15:22:45 -0700 | [diff] [blame] | 179 | ViewStateSnapshot prev; |
| 180 | ViewStateSnapshot next = new ViewStateSnapshot(v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | do { |
Yigit Boyar | de8d789 | 2014-09-22 15:22:45 -0700 | [diff] [blame] | 182 | prev = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | TouchUtils.dragQuarterScreenDown(test, activity); |
Yigit Boyar | de8d789 | 2014-09-22 15:22:45 -0700 | [diff] [blame] | 184 | next = new ViewStateSnapshot(v); |
| 185 | } while (!prev.equals(next)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 187 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | /** |
| 189 | * Simulate touching the center of a view and dragging to the bottom of the screen. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 190 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | * @param test The test case that is being run |
| 192 | * @param v The view that should be dragged |
| 193 | * |
| 194 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 195 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 196 | * configuring the Activity under test |
| 197 | */ |
| 198 | @Deprecated |
| 199 | public static void dragViewToBottom(ActivityInstrumentationTestCase test, View v) { |
| 200 | dragViewToBottom(test, test.getActivity(), v, 4); |
| 201 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 202 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | /** |
| 204 | * Simulate touching the center of a view and dragging to the bottom of the screen. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 205 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | * @param test The test case that is being run |
| 207 | * @param activity The activity that is in the foreground of the test case |
| 208 | * @param v The view that should be dragged |
| 209 | */ |
| 210 | public static void dragViewToBottom(InstrumentationTestCase test, Activity activity, View v) { |
| 211 | dragViewToBottom(test, activity, v, 4); |
| 212 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | /** |
| 215 | * Simulate touching the center of a view and dragging to the bottom of the screen. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 216 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | * @param test The test case that is being run |
| 218 | * @param v The view that should be dragged |
| 219 | * @param stepCount How many move steps to include in the drag |
| 220 | * |
| 221 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 222 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 223 | * configuring the Activity under test |
| 224 | */ |
| 225 | @Deprecated |
| 226 | public static void dragViewToBottom(ActivityInstrumentationTestCase test, View v, |
| 227 | int stepCount) { |
| 228 | dragViewToBottom(test, test.getActivity(), v, stepCount); |
| 229 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | /** |
| 232 | * Simulate touching the center of a view and dragging to the bottom of the screen. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 233 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | * @param test The test case that is being run |
| 235 | * @param activity The activity that is in the foreground of the test case |
| 236 | * @param v The view that should be dragged |
| 237 | * @param stepCount How many move steps to include in the drag |
| 238 | */ |
| 239 | public static void dragViewToBottom(InstrumentationTestCase test, Activity activity, View v, |
| 240 | int stepCount) { |
Charles Chen | 49f329c | 2020-02-13 16:41:32 +0800 | [diff] [blame] | 241 | WindowManager wm = activity.getWindowManager(); |
| 242 | final int screenHeight = getSizeExcludingNavigationBarAndCutout( |
| 243 | wm.getCurrentWindowMetrics()).getHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | |
| 245 | int[] xy = new int[2]; |
| 246 | v.getLocationOnScreen(xy); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 247 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | final int viewWidth = v.getWidth(); |
| 249 | final int viewHeight = v.getHeight(); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 250 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | final float x = xy[0] + (viewWidth / 2.0f); |
| 252 | float fromY = xy[1] + (viewHeight / 2.0f); |
| 253 | float toY = screenHeight - 1; |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | drag(test, x, x, fromY, toY, stepCount); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Simulate touching the center of a view and releasing quickly (before the tap timeout). |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 260 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | * @param test The test case that is being run |
| 262 | * @param v The view that should be clicked |
| 263 | */ |
| 264 | public static void tapView(InstrumentationTestCase test, View v) { |
| 265 | int[] xy = new int[2]; |
| 266 | v.getLocationOnScreen(xy); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 267 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | final int viewWidth = v.getWidth(); |
| 269 | final int viewHeight = v.getHeight(); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 270 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | final float x = xy[0] + (viewWidth / 2.0f); |
| 272 | float y = xy[1] + (viewHeight / 2.0f); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 273 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | Instrumentation inst = test.getInstrumentation(); |
| 275 | |
| 276 | long downTime = SystemClock.uptimeMillis(); |
| 277 | long eventTime = SystemClock.uptimeMillis(); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 278 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | MotionEvent event = MotionEvent.obtain(downTime, eventTime, |
| 280 | MotionEvent.ACTION_DOWN, x, y, 0); |
| 281 | inst.sendPointerSync(event); |
| 282 | inst.waitForIdleSync(); |
| 283 | |
| 284 | eventTime = SystemClock.uptimeMillis(); |
| 285 | final int touchSlop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop(); |
| 286 | event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, |
| 287 | x + (touchSlop / 2.0f), y + (touchSlop / 2.0f), 0); |
| 288 | inst.sendPointerSync(event); |
| 289 | inst.waitForIdleSync(); |
| 290 | |
| 291 | eventTime = SystemClock.uptimeMillis(); |
| 292 | event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0); |
| 293 | inst.sendPointerSync(event); |
| 294 | inst.waitForIdleSync(); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Simulate touching the center of a view and cancelling (so no onClick should |
| 299 | * fire, etc). |
| 300 | * |
| 301 | * @param test The test case that is being run |
| 302 | * @param v The view that should be clicked |
| 303 | */ |
| 304 | public static void touchAndCancelView(InstrumentationTestCase test, View v) { |
| 305 | int[] xy = new int[2]; |
| 306 | v.getLocationOnScreen(xy); |
| 307 | |
| 308 | final int viewWidth = v.getWidth(); |
| 309 | final int viewHeight = v.getHeight(); |
| 310 | |
| 311 | final float x = xy[0] + (viewWidth / 2.0f); |
| 312 | float y = xy[1] + (viewHeight / 2.0f); |
| 313 | |
| 314 | Instrumentation inst = test.getInstrumentation(); |
| 315 | |
| 316 | long downTime = SystemClock.uptimeMillis(); |
| 317 | long eventTime = SystemClock.uptimeMillis(); |
| 318 | |
| 319 | MotionEvent event = MotionEvent.obtain(downTime, eventTime, |
| 320 | MotionEvent.ACTION_DOWN, x, y, 0); |
| 321 | inst.sendPointerSync(event); |
| 322 | inst.waitForIdleSync(); |
| 323 | |
| 324 | eventTime = SystemClock.uptimeMillis(); |
| 325 | final int touchSlop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop(); |
| 326 | event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_CANCEL, |
| 327 | x + (touchSlop / 2.0f), y + (touchSlop / 2.0f), 0); |
| 328 | inst.sendPointerSync(event); |
| 329 | inst.waitForIdleSync(); |
| 330 | |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Simulate touching the center of a view and releasing. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 335 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | * @param test The test case that is being run |
| 337 | * @param v The view that should be clicked |
| 338 | */ |
| 339 | public static void clickView(InstrumentationTestCase test, View v) { |
| 340 | int[] xy = new int[2]; |
| 341 | v.getLocationOnScreen(xy); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 342 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | final int viewWidth = v.getWidth(); |
| 344 | final int viewHeight = v.getHeight(); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 345 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | final float x = xy[0] + (viewWidth / 2.0f); |
| 347 | float y = xy[1] + (viewHeight / 2.0f); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 348 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | Instrumentation inst = test.getInstrumentation(); |
| 350 | |
| 351 | long downTime = SystemClock.uptimeMillis(); |
| 352 | long eventTime = SystemClock.uptimeMillis(); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 353 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | MotionEvent event = MotionEvent.obtain(downTime, eventTime, |
| 355 | MotionEvent.ACTION_DOWN, x, y, 0); |
| 356 | inst.sendPointerSync(event); |
| 357 | inst.waitForIdleSync(); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 358 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | |
| 360 | eventTime = SystemClock.uptimeMillis(); |
| 361 | final int touchSlop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop(); |
| 362 | event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, |
| 363 | x + (touchSlop / 2.0f), y + (touchSlop / 2.0f), 0); |
| 364 | inst.sendPointerSync(event); |
| 365 | inst.waitForIdleSync(); |
| 366 | |
| 367 | eventTime = SystemClock.uptimeMillis(); |
| 368 | event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0); |
| 369 | inst.sendPointerSync(event); |
| 370 | inst.waitForIdleSync(); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 371 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | try { |
| 373 | Thread.sleep(1000); |
| 374 | } catch (InterruptedException e) { |
| 375 | e.printStackTrace(); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Simulate touching the center of a view, holding until it is a long press, and then releasing. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 381 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | * @param test The test case that is being run |
| 383 | * @param v The view that should be clicked |
| 384 | * |
| 385 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 386 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 387 | * configuring the Activity under test |
| 388 | */ |
| 389 | @Deprecated |
| 390 | public static void longClickView(ActivityInstrumentationTestCase test, View v) { |
| 391 | longClickView((InstrumentationTestCase) test, v); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Simulate touching the center of a view, holding until it is a long press, and then releasing. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 396 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | * @param test The test case that is being run |
| 398 | * @param v The view that should be clicked |
| 399 | */ |
| 400 | public static void longClickView(InstrumentationTestCase test, View v) { |
| 401 | int[] xy = new int[2]; |
| 402 | v.getLocationOnScreen(xy); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 403 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | final int viewWidth = v.getWidth(); |
| 405 | final int viewHeight = v.getHeight(); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 406 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | final float x = xy[0] + (viewWidth / 2.0f); |
| 408 | float y = xy[1] + (viewHeight / 2.0f); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 409 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | Instrumentation inst = test.getInstrumentation(); |
| 411 | |
| 412 | long downTime = SystemClock.uptimeMillis(); |
| 413 | long eventTime = SystemClock.uptimeMillis(); |
| 414 | |
| 415 | MotionEvent event = MotionEvent.obtain(downTime, eventTime, |
| 416 | MotionEvent.ACTION_DOWN, x, y, 0); |
| 417 | inst.sendPointerSync(event); |
| 418 | inst.waitForIdleSync(); |
| 419 | |
| 420 | eventTime = SystemClock.uptimeMillis(); |
| 421 | final int touchSlop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop(); |
| 422 | event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, |
| 423 | x + touchSlop / 2, y + touchSlop / 2, 0); |
| 424 | inst.sendPointerSync(event); |
| 425 | inst.waitForIdleSync(); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 426 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | try { |
| 428 | Thread.sleep((long)(ViewConfiguration.getLongPressTimeout() * 1.5f)); |
| 429 | } catch (InterruptedException e) { |
| 430 | e.printStackTrace(); |
| 431 | } |
| 432 | |
| 433 | eventTime = SystemClock.uptimeMillis(); |
| 434 | event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0); |
| 435 | inst.sendPointerSync(event); |
| 436 | inst.waitForIdleSync(); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Simulate touching the center of a view and dragging to the top of the screen. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 441 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | * @param test The test case that is being run |
| 443 | * @param v The view that should be dragged |
| 444 | * |
| 445 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 446 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 447 | * configuring the Activity under test |
| 448 | */ |
| 449 | @Deprecated |
| 450 | public static void dragViewToTop(ActivityInstrumentationTestCase test, View v) { |
| 451 | dragViewToTop((InstrumentationTestCase) test, v, 4); |
| 452 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 453 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | /** |
| 455 | * Simulate touching the center of a view and dragging to the top of the screen. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 456 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | * @param test The test case that is being run |
| 458 | * @param v The view that should be dragged |
| 459 | * @param stepCount How many move steps to include in the drag |
| 460 | * |
| 461 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 462 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 463 | * configuring the Activity under test |
| 464 | */ |
| 465 | @Deprecated |
| 466 | public static void dragViewToTop(ActivityInstrumentationTestCase test, View v, int stepCount) { |
| 467 | dragViewToTop((InstrumentationTestCase) test, v, stepCount); |
| 468 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 469 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | /** |
| 471 | * Simulate touching the center of a view and dragging to the top of the screen. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 472 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | * @param test The test case that is being run |
| 474 | * @param v The view that should be dragged |
| 475 | */ |
| 476 | public static void dragViewToTop(InstrumentationTestCase test, View v) { |
| 477 | dragViewToTop(test, v, 4); |
| 478 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 479 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | /** |
| 481 | * Simulate touching the center of a view and dragging to the top of the screen. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 482 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | * @param test The test case that is being run |
| 484 | * @param v The view that should be dragged |
| 485 | * @param stepCount How many move steps to include in the drag |
| 486 | */ |
| 487 | public static void dragViewToTop(InstrumentationTestCase test, View v, int stepCount) { |
| 488 | int[] xy = new int[2]; |
| 489 | v.getLocationOnScreen(xy); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 490 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | final int viewWidth = v.getWidth(); |
| 492 | final int viewHeight = v.getHeight(); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 493 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | final float x = xy[0] + (viewWidth / 2.0f); |
| 495 | float fromY = xy[1] + (viewHeight / 2.0f); |
| 496 | float toY = 0; |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 497 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | drag(test, x, x, fromY, toY, stepCount); |
| 499 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 500 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | /** |
| 502 | * Get the location of a view. Use the gravity param to specify which part of the view to |
| 503 | * return. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 504 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | * @param v View to find |
| 506 | * @param gravity A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, |
| 507 | * RIGHT) |
| 508 | * @param xy Result |
| 509 | */ |
| 510 | private static void getStartLocation(View v, int gravity, int[] xy) { |
| 511 | v.getLocationOnScreen(xy); |
| 512 | |
| 513 | final int viewWidth = v.getWidth(); |
| 514 | final int viewHeight = v.getHeight(); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 515 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) { |
| 517 | case Gravity.TOP: |
| 518 | break; |
| 519 | case Gravity.CENTER_VERTICAL: |
| 520 | xy[1] += viewHeight / 2; |
| 521 | break; |
| 522 | case Gravity.BOTTOM: |
| 523 | xy[1] += viewHeight - 1; |
| 524 | break; |
| 525 | default: |
| 526 | // Same as top -- do nothing |
| 527 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 528 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) { |
| 530 | case Gravity.LEFT: |
| 531 | break; |
| 532 | case Gravity.CENTER_HORIZONTAL: |
| 533 | xy[0] += viewWidth / 2; |
| 534 | break; |
| 535 | case Gravity.RIGHT: |
| 536 | xy[0] += viewWidth - 1; |
| 537 | break; |
| 538 | default: |
| 539 | // Same as left -- do nothing |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Simulate touching a view and dragging it by the specified amount. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 545 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | * @param test The test case that is being run |
| 547 | * @param v The view that should be dragged |
| 548 | * @param gravity Which part of the view to use for the initial down event. A combination of |
| 549 | * (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT) |
| 550 | * @param deltaX Amount to drag horizontally in pixels |
| 551 | * @param deltaY Amount to drag vertically in pixels |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 552 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | * @return distance in pixels covered by the drag |
| 554 | * |
| 555 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 556 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 557 | * configuring the Activity under test |
| 558 | */ |
| 559 | @Deprecated |
| 560 | public static int dragViewBy(ActivityInstrumentationTestCase test, View v, int gravity, |
| 561 | int deltaX, int deltaY) { |
| 562 | return dragViewBy((InstrumentationTestCase) test, v, gravity, deltaX, deltaY); |
| 563 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 564 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | /** |
| 566 | * Simulate touching a view and dragging it by the specified amount. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 567 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | * @param test The test case that is being run |
| 569 | * @param v The view that should be dragged |
| 570 | * @param gravity Which part of the view to use for the initial down event. A combination of |
| 571 | * (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT) |
| 572 | * @param deltaX Amount to drag horizontally in pixels |
| 573 | * @param deltaY Amount to drag vertically in pixels |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 574 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | * @return distance in pixels covered by the drag |
| 576 | * |
| 577 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 578 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 579 | * configuring the Activity under test |
| 580 | */ |
Jean-Baptiste Queru | 9db3d07 | 2009-11-12 18:45:53 -0800 | [diff] [blame] | 581 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | public static int dragViewBy(InstrumentationTestCase test, View v, int gravity, int deltaX, |
| 583 | int deltaY) { |
| 584 | int[] xy = new int[2]; |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 585 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | getStartLocation(v, gravity, xy); |
| 587 | |
| 588 | final int fromX = xy[0]; |
| 589 | final int fromY = xy[1]; |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 590 | |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 591 | int distance = (int) Math.hypot(deltaX, deltaY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | |
| 593 | drag(test, fromX, fromX + deltaX, fromY, fromY + deltaY, distance); |
| 594 | |
| 595 | return distance; |
| 596 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 597 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | /** |
| 599 | * Simulate touching a view and dragging it to a specified location. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 600 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | * @param test The test case that is being run |
| 602 | * @param v The view that should be dragged |
| 603 | * @param gravity Which part of the view to use for the initial down event. A combination of |
| 604 | * (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT) |
| 605 | * @param toX Final location of the view after dragging |
| 606 | * @param toY Final location of the view after dragging |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 607 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | * @return distance in pixels covered by the drag |
| 609 | * |
| 610 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 611 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 612 | * configuring the Activity under test |
| 613 | */ |
| 614 | @Deprecated |
| 615 | public static int dragViewTo(ActivityInstrumentationTestCase test, View v, int gravity, int toX, |
| 616 | int toY) { |
| 617 | return dragViewTo((InstrumentationTestCase) test, v, gravity, toX, toY); |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Simulate touching a view and dragging it to a specified location. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 622 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | * @param test The test case that is being run |
| 624 | * @param v The view that should be dragged |
| 625 | * @param gravity Which part of the view to use for the initial down event. A combination of |
| 626 | * (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT) |
| 627 | * @param toX Final location of the view after dragging |
| 628 | * @param toY Final location of the view after dragging |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 629 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | * @return distance in pixels covered by the drag |
| 631 | */ |
| 632 | public static int dragViewTo(InstrumentationTestCase test, View v, int gravity, int toX, |
| 633 | int toY) { |
| 634 | int[] xy = new int[2]; |
| 635 | |
| 636 | getStartLocation(v, gravity, xy); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 637 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | final int fromX = xy[0]; |
| 639 | final int fromY = xy[1]; |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 640 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | int deltaX = fromX - toX; |
| 642 | int deltaY = fromY - toY; |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 643 | |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 644 | int distance = (int)Math.hypot(deltaX, deltaY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | drag(test, fromX, toX, fromY, toY, distance); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 646 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | return distance; |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * Simulate touching a view and dragging it to a specified location. Only moves horizontally. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 652 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | * @param test The test case that is being run |
| 654 | * @param v The view that should be dragged |
| 655 | * @param gravity Which part of the view to use for the initial down event. A combination of |
| 656 | * (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT) |
| 657 | * @param toX Final location of the view after dragging |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 658 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | * @return distance in pixels covered by the drag |
| 660 | * |
| 661 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 662 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 663 | * configuring the Activity under test |
| 664 | */ |
| 665 | @Deprecated |
| 666 | public static int dragViewToX(ActivityInstrumentationTestCase test, View v, int gravity, |
| 667 | int toX) { |
| 668 | return dragViewToX((InstrumentationTestCase) test, v, gravity, toX); |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Simulate touching a view and dragging it to a specified location. Only moves horizontally. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 673 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 | * @param test The test case that is being run |
| 675 | * @param v The view that should be dragged |
| 676 | * @param gravity Which part of the view to use for the initial down event. A combination of |
| 677 | * (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT) |
| 678 | * @param toX Final location of the view after dragging |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 679 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | * @return distance in pixels covered by the drag |
| 681 | */ |
| 682 | public static int dragViewToX(InstrumentationTestCase test, View v, int gravity, int toX) { |
| 683 | int[] xy = new int[2]; |
| 684 | |
| 685 | getStartLocation(v, gravity, xy); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 686 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | final int fromX = xy[0]; |
| 688 | final int fromY = xy[1]; |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 689 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | int deltaX = fromX - toX; |
| 691 | |
| 692 | drag(test, fromX, toX, fromY, fromY, deltaX); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 693 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | return deltaX; |
| 695 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 696 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | /** |
| 698 | * Simulate touching a view and dragging it to a specified location. Only moves vertically. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 699 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | * @param test The test case that is being run |
| 701 | * @param v The view that should be dragged |
| 702 | * @param gravity Which part of the view to use for the initial down event. A combination of |
| 703 | * (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT) |
| 704 | * @param toY Final location of the view after dragging |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 705 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | * @return distance in pixels covered by the drag |
| 707 | * |
| 708 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 709 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 710 | * configuring the Activity under test |
| 711 | */ |
| 712 | @Deprecated |
| 713 | public static int dragViewToY(ActivityInstrumentationTestCase test, View v, int gravity, |
| 714 | int toY) { |
| 715 | return dragViewToY((InstrumentationTestCase) test, v, gravity, toY); |
| 716 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 717 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | /** |
| 719 | * Simulate touching a view and dragging it to a specified location. Only moves vertically. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 720 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | * @param test The test case that is being run |
| 722 | * @param v The view that should be dragged |
| 723 | * @param gravity Which part of the view to use for the initial down event. A combination of |
| 724 | * (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT) |
| 725 | * @param toY Final location of the view after dragging |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 726 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | * @return distance in pixels covered by the drag |
| 728 | */ |
| 729 | public static int dragViewToY(InstrumentationTestCase test, View v, int gravity, int toY) { |
| 730 | int[] xy = new int[2]; |
| 731 | |
| 732 | getStartLocation(v, gravity, xy); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 733 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | final int fromX = xy[0]; |
| 735 | final int fromY = xy[1]; |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 736 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | int deltaY = fromY - toY; |
| 738 | |
| 739 | drag(test, fromX, fromX, fromY, toY, deltaY); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 740 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | return deltaY; |
| 742 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 743 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | |
| 745 | /** |
| 746 | * Simulate touching a specific location and dragging to a new location. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 747 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | * @param test The test case that is being run |
| 749 | * @param fromX X coordinate of the initial touch, in screen coordinates |
| 750 | * @param toX Xcoordinate of the drag destination, in screen coordinates |
| 751 | * @param fromY X coordinate of the initial touch, in screen coordinates |
| 752 | * @param toY Y coordinate of the drag destination, in screen coordinates |
| 753 | * @param stepCount How many move steps to include in the drag |
| 754 | * |
| 755 | * @deprecated {@link android.test.ActivityInstrumentationTestCase} is deprecated in favor of |
| 756 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for |
| 757 | * configuring the Activity under test |
| 758 | */ |
| 759 | @Deprecated |
| 760 | public static void drag(ActivityInstrumentationTestCase test, float fromX, float toX, |
| 761 | float fromY, float toY, int stepCount) { |
| 762 | drag((InstrumentationTestCase) test, fromX, toX, fromY, toY, stepCount); |
| 763 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 764 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | /** |
| 766 | * Simulate touching a specific location and dragging to a new location. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 767 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 768 | * @param test The test case that is being run |
| 769 | * @param fromX X coordinate of the initial touch, in screen coordinates |
| 770 | * @param toX Xcoordinate of the drag destination, in screen coordinates |
| 771 | * @param fromY X coordinate of the initial touch, in screen coordinates |
| 772 | * @param toY Y coordinate of the drag destination, in screen coordinates |
| 773 | * @param stepCount How many move steps to include in the drag |
| 774 | */ |
| 775 | public static void drag(InstrumentationTestCase test, float fromX, float toX, float fromY, |
| 776 | float toY, int stepCount) { |
| 777 | Instrumentation inst = test.getInstrumentation(); |
| 778 | |
| 779 | long downTime = SystemClock.uptimeMillis(); |
| 780 | long eventTime = SystemClock.uptimeMillis(); |
| 781 | |
| 782 | float y = fromY; |
| 783 | float x = fromX; |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 784 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | float yStep = (toY - fromY) / stepCount; |
| 786 | float xStep = (toX - fromX) / stepCount; |
| 787 | |
| 788 | MotionEvent event = MotionEvent.obtain(downTime, eventTime, |
Marc Capdevielle | 8621cfa | 2010-02-05 19:28:23 +0100 | [diff] [blame] | 789 | MotionEvent.ACTION_DOWN, x, y, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | inst.sendPointerSync(event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | for (int i = 0; i < stepCount; ++i) { |
| 792 | y += yStep; |
| 793 | x += xStep; |
| 794 | eventTime = SystemClock.uptimeMillis(); |
| 795 | event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0); |
| 796 | inst.sendPointerSync(event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | eventTime = SystemClock.uptimeMillis(); |
Marc Capdevielle | 8621cfa | 2010-02-05 19:28:23 +0100 | [diff] [blame] | 800 | event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | inst.sendPointerSync(event); |
| 802 | inst.waitForIdleSync(); |
| 803 | } |
Yigit Boyar | de8d789 | 2014-09-22 15:22:45 -0700 | [diff] [blame] | 804 | |
| 805 | private static class ViewStateSnapshot { |
| 806 | final View mFirst; |
| 807 | final View mLast; |
| 808 | final int mFirstTop; |
| 809 | final int mLastBottom; |
| 810 | final int mChildCount; |
| 811 | private ViewStateSnapshot(ViewGroup viewGroup) { |
| 812 | mChildCount = viewGroup.getChildCount(); |
| 813 | if (mChildCount == 0) { |
| 814 | mFirst = mLast = null; |
| 815 | mFirstTop = mLastBottom = Integer.MIN_VALUE; |
| 816 | } else { |
| 817 | mFirst = viewGroup.getChildAt(0); |
| 818 | mLast = viewGroup.getChildAt(mChildCount - 1); |
| 819 | mFirstTop = mFirst.getTop(); |
| 820 | mLastBottom = mLast.getBottom(); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | @Override |
| 825 | public boolean equals(Object o) { |
| 826 | if (this == o) { |
| 827 | return true; |
| 828 | } |
| 829 | if (o == null || getClass() != o.getClass()) { |
| 830 | return false; |
| 831 | } |
| 832 | |
| 833 | final ViewStateSnapshot that = (ViewStateSnapshot) o; |
| 834 | return mFirstTop == that.mFirstTop && |
| 835 | mLastBottom == that.mLastBottom && |
| 836 | mFirst == that.mFirst && |
| 837 | mLast == that.mLast && |
| 838 | mChildCount == that.mChildCount; |
| 839 | } |
| 840 | |
| 841 | @Override |
| 842 | public int hashCode() { |
| 843 | int result = mFirst != null ? mFirst.hashCode() : 0; |
| 844 | result = 31 * result + (mLast != null ? mLast.hashCode() : 0); |
| 845 | result = 31 * result + mFirstTop; |
| 846 | result = 31 * result + mLastBottom; |
| 847 | result = 31 * result + mChildCount; |
| 848 | return result; |
| 849 | } |
| 850 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | } |