| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.test; |
| 18 |
|
| 19 |
import static android.view.WindowInsets.Type.displayCutout; |
| 20 |
import static android.view.WindowInsets.Type.navigationBars; |
| 21 |
|
| 22 |
import android.app.Activity; |
| 23 |
import android.app.Instrumentation; |
| 24 |
import android.graphics.Insets; |
| 25 |
import android.graphics.Rect; |
| 26 |
import android.os.SystemClock; |
| 27 |
import android.util.Size; |
| 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; |
| 33 |
import android.view.WindowInsets; |
| 34 |
import android.view.WindowManager; |
| 35 |
import android.view.WindowMetrics; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
@Deprecated |
| 48 |
public class TouchUtils { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
@Deprecated |
| 59 |
public static void dragQuarterScreenDown(ActivityInstrumentationTestCase test) { |
| 60 |
dragQuarterScreenDown(test, test.getActivity()); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static void dragQuarterScreenDown(InstrumentationTestCase test, Activity activity) { |
| 69 |
WindowManager wm = activity.getWindowManager(); |
| 70 |
final Size size = getSizeExcludingNavigationBarAndCutout(wm.getCurrentWindowMetrics()); |
| 71 |
|
| 72 |
final float x = size.getWidth() / 2.0f; |
| 73 |
final float fromY = size.getHeight() * 0.5f; |
| 74 |
final float toY = size.getHeight() * 0.75f; |
| 75 |
|
| 76 |
drag(test, x, x, fromY, toY, 4); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
@Deprecated |
| 88 |
public static void dragQuarterScreenUp(ActivityInstrumentationTestCase test) { |
| 89 |
dragQuarterScreenUp(test, test.getActivity()); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public static void dragQuarterScreenUp(InstrumentationTestCase test, Activity activity) { |
| 98 |
WindowManager wm = activity.getWindowManager(); |
| 99 |
final Size size = getSizeExcludingNavigationBarAndCutout(wm.getCurrentWindowMetrics()); |
| 100 |
|
| 101 |
final float x = size.getWidth() / 2.0f; |
| 102 |
final float fromY = size.getHeight() * 0.5f; |
| 103 |
final float toY = size.getHeight() * 0.25f; |
| 104 |
|
| 105 |
drag(test, x, x, fromY, toY, 4); |
| 106 |
} |
| 107 |
|
| 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 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
@Deprecated |
| 131 |
public static void scrollToBottom(ActivityInstrumentationTestCase test, ViewGroup v) { |
| 132 |
scrollToBottom(test, test.getActivity(), v); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public static void scrollToBottom(InstrumentationTestCase test, Activity activity, |
| 144 |
ViewGroup v) { |
| 145 |
ViewStateSnapshot prev; |
| 146 |
ViewStateSnapshot next = new ViewStateSnapshot(v); |
| 147 |
do { |
| 148 |
prev = next; |
| 149 |
TouchUtils.dragQuarterScreenUp(test, activity); |
| 150 |
next = new ViewStateSnapshot(v); |
| 151 |
} while (!prev.equals(next)); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
@Deprecated |
| 166 |
public static void scrollToTop(ActivityInstrumentationTestCase test, ViewGroup v) { |
| 167 |
scrollToTop(test, test.getActivity(), v); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public static void scrollToTop(InstrumentationTestCase test, Activity activity, ViewGroup v) { |
| 179 |
ViewStateSnapshot prev; |
| 180 |
ViewStateSnapshot next = new ViewStateSnapshot(v); |
| 181 |
do { |
| 182 |
prev = next; |
| 183 |
TouchUtils.dragQuarterScreenDown(test, activity); |
| 184 |
next = new ViewStateSnapshot(v); |
| 185 |
} while (!prev.equals(next)); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
@Deprecated |
| 199 |
public static void dragViewToBottom(ActivityInstrumentationTestCase test, View v) { |
| 200 |
dragViewToBottom(test, test.getActivity(), v, 4); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public static void dragViewToBottom(InstrumentationTestCase test, Activity activity, View v) { |
| 211 |
dragViewToBottom(test, activity, v, 4); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
@Deprecated |
| 226 |
public static void dragViewToBottom(ActivityInstrumentationTestCase test, View v, |
| 227 |
int stepCount) { |
| 228 |
dragViewToBottom(test, test.getActivity(), v, stepCount); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public static void dragViewToBottom(InstrumentationTestCase test, Activity activity, View v, |
| 240 |
int stepCount) { |
| 241 |
WindowManager wm = activity.getWindowManager(); |
| 242 |
final int screenHeight = getSizeExcludingNavigationBarAndCutout( |
| 243 |
wm.getCurrentWindowMetrics()).getHeight(); |
| 244 |
|
| 245 |
int[] xy = new int[2]; |
| 246 |
v.getLocationOnScreen(xy); |
| 247 |
|
| 248 |
final int viewWidth = v.getWidth(); |
| 249 |
final int viewHeight = v.getHeight(); |
| 250 |
|
| 251 |
final float x = xy[0] + (viewWidth / 2.0f); |
| 252 |
float fromY = xy[1] + (viewHeight / 2.0f); |
| 253 |
float toY = screenHeight - 1; |
| 254 |
|
| 255 |
drag(test, x, x, fromY, toY, stepCount); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public static void tapView(InstrumentationTestCase test, View v) { |
| 265 |
int[] xy = new int[2]; |
| 266 |
v.getLocationOnScreen(xy); |
| 267 |
|
| 268 |
final int viewWidth = v.getWidth(); |
| 269 |
final int viewHeight = v.getHeight(); |
| 270 |
|
| 271 |
final float x = xy[0] + (viewWidth / 2.0f); |
| 272 |
float y = xy[1] + (viewHeight / 2.0f); |
| 273 |
|
| 274 |
Instrumentation inst = test.getInstrumentation(); |
| 275 |
|
| 276 |
long downTime = SystemClock.uptimeMillis(); |
| 277 |
long eventTime = SystemClock.uptimeMillis(); |
| 278 |
|
| 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 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 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 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public static void clickView(InstrumentationTestCase test, View v) { |
| 340 |
int[] xy = new int[2]; |
| 341 |
v.getLocationOnScreen(xy); |
| 342 |
|
| 343 |
final int viewWidth = v.getWidth(); |
| 344 |
final int viewHeight = v.getHeight(); |
| 345 |
|
| 346 |
final float x = xy[0] + (viewWidth / 2.0f); |
| 347 |
float y = xy[1] + (viewHeight / 2.0f); |
| 348 |
|
| 349 |
Instrumentation inst = test.getInstrumentation(); |
| 350 |
|
| 351 |
long downTime = SystemClock.uptimeMillis(); |
| 352 |
long eventTime = SystemClock.uptimeMillis(); |
| 353 |
|
| 354 |
MotionEvent event = MotionEvent.obtain(downTime, eventTime, |
| 355 |
MotionEvent.ACTION_DOWN, x, y, 0); |
| 356 |
inst.sendPointerSync(event); |
| 357 |
inst.waitForIdleSync(); |
| 358 |
|
| 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(); |
| 371 |
|
| 372 |
try { |
| 373 |
Thread.sleep(1000); |
| 374 |
} catch (InterruptedException e) { |
| 375 |
e.printStackTrace(); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
@Deprecated |
| 390 |
public static void longClickView(ActivityInstrumentationTestCase test, View v) { |
| 391 |
longClickView((InstrumentationTestCase) test, v); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public static void longClickView(InstrumentationTestCase test, View v) { |
| 401 |
int[] xy = new int[2]; |
| 402 |
v.getLocationOnScreen(xy); |
| 403 |
|
| 404 |
final int viewWidth = v.getWidth(); |
| 405 |
final int viewHeight = v.getHeight(); |
| 406 |
|
| 407 |
final float x = xy[0] + (viewWidth / 2.0f); |
| 408 |
float y = xy[1] + (viewHeight / 2.0f); |
| 409 |
|
| 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(); |
| 426 |
|
| 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 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
@Deprecated |
| 450 |
public static void dragViewToTop(ActivityInstrumentationTestCase test, View v) { |
| 451 |
dragViewToTop((InstrumentationTestCase) test, v, 4); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
@Deprecated |
| 466 |
public static void dragViewToTop(ActivityInstrumentationTestCase test, View v, int stepCount) { |
| 467 |
dragViewToTop((InstrumentationTestCase) test, v, stepCount); |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
public static void dragViewToTop(InstrumentationTestCase test, View v) { |
| 477 |
dragViewToTop(test, v, 4); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
public static void dragViewToTop(InstrumentationTestCase test, View v, int stepCount) { |
| 488 |
int[] xy = new int[2]; |
| 489 |
v.getLocationOnScreen(xy); |
| 490 |
|
| 491 |
final int viewWidth = v.getWidth(); |
| 492 |
final int viewHeight = v.getHeight(); |
| 493 |
|
| 494 |
final float x = xy[0] + (viewWidth / 2.0f); |
| 495 |
float fromY = xy[1] + (viewHeight / 2.0f); |
| 496 |
float toY = 0; |
| 497 |
|
| 498 |
drag(test, x, x, fromY, toY, stepCount); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 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(); |
| 515 |
|
| 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 |
|
| 527 |
} |
| 528 |
|
| 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 |
|
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 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 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
@Deprecated |
| 582 |
public static int dragViewBy(InstrumentationTestCase test, View v, int gravity, int deltaX, |
| 583 |
int deltaY) { |
| 584 |
int[] xy = new int[2]; |
| 585 |
|
| 586 |
getStartLocation(v, gravity, xy); |
| 587 |
|
| 588 |
final int fromX = xy[0]; |
| 589 |
final int fromY = xy[1]; |
| 590 |
|
| 591 |
int distance = (int) Math.hypot(deltaX, deltaY); |
| 592 |
|
| 593 |
drag(test, fromX, fromX + deltaX, fromY, fromY + deltaY, distance); |
| 594 |
|
| 595 |
return distance; |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 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 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 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); |
| 637 |
|
| 638 |
final int fromX = xy[0]; |
| 639 |
final int fromY = xy[1]; |
| 640 |
|
| 641 |
int deltaX = fromX - toX; |
| 642 |
int deltaY = fromY - toY; |
| 643 |
|
| 644 |
int distance = (int)Math.hypot(deltaX, deltaY); |
| 645 |
drag(test, fromX, toX, fromY, toY, distance); |
| 646 |
|
| 647 |
return distance; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 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 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 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); |
| 686 |
|
| 687 |
final int fromX = xy[0]; |
| 688 |
final int fromY = xy[1]; |
| 689 |
|
| 690 |
int deltaX = fromX - toX; |
| 691 |
|
| 692 |
drag(test, fromX, toX, fromY, fromY, deltaX); |
| 693 |
|
| 694 |
return deltaX; |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 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 |
} |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 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); |
| 733 |
|
| 734 |
final int fromX = xy[0]; |
| 735 |
final int fromY = xy[1]; |
| 736 |
|
| 737 |
int deltaY = fromY - toY; |
| 738 |
|
| 739 |
drag(test, fromX, fromX, fromY, toY, deltaY); |
| 740 |
|
| 741 |
return deltaY; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 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 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 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; |
| 784 |
|
| 785 |
float yStep = (toY - fromY) / stepCount; |
| 786 |
float xStep = (toX - fromX) / stepCount; |
| 787 |
|
| 788 |
MotionEvent event = MotionEvent.obtain(downTime, eventTime, |
| 789 |
MotionEvent.ACTION_DOWN, x, y, 0); |
| 790 |
inst.sendPointerSync(event); |
| 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); |
| 797 |
} |
| 798 |
|
| 799 |
eventTime = SystemClock.uptimeMillis(); |
| 800 |
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0); |
| 801 |
inst.sendPointerSync(event); |
| 802 |
inst.waitForIdleSync(); |
| 803 |
} |
| 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 |
} |
| 851 |
} |