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