drag_test.dart 53.3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Ian Hickson's avatar
Ian Hickson committed
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/gestures.dart';
6
import 'package:flutter_test/flutter_test.dart';
7

8 9
import 'gesture_tester.dart';

10
void main() {
11
  setUp(ensureGestureBinding);
12

13
  testGesture('Should recognize pan', (GestureTester tester) {
14
    final PanGestureRecognizer pan = PanGestureRecognizer();
15
    final TapGestureRecognizer tap = TapGestureRecognizer()..onTap = () {};
16 17
    addTearDown(pan.dispose);
    addTearDown(tap.dispose);
18

19
    bool didStartPan = false;
20
    pan.onStart = (_) {
21
      didStartPan = true;
22 23
    };

24
    Offset? updatedScrollDelta;
25 26
    pan.onUpdate = (DragUpdateDetails details) {
      updatedScrollDelta = details.delta;
27 28
    };

29
    bool didEndPan = false;
30
    pan.onEnd = (DragEndDetails details) {
31
      didEndPan = true;
32 33
    };

34 35 36 37
    bool didTap = false;
    tap.onTap = () {
      didTap = true;
    };
38

39
    final TestPointer pointer = TestPointer(5);
40
    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
41 42
    pan.addPointer(down);
    tap.addPointer(down);
43
    tester.closeArena(5);
44 45 46 47 48
    expect(didStartPan, isFalse);
    expect(updatedScrollDelta, isNull);
    expect(didEndPan, isFalse);
    expect(didTap, isFalse);

49
    tester.route(down);
50 51 52 53 54
    expect(didStartPan, isFalse);
    expect(updatedScrollDelta, isNull);
    expect(didEndPan, isFalse);
    expect(didTap, isFalse);

55 56 57 58 59 60
    // touch should give up when it hits kTouchSlop, which was 18.0 when this test was last updated.

    tester.route(pointer.move(const Offset(20.0, 20.0))); // moved 10 horizontally and 10 vertically which is 14 total
    expect(didStartPan, isFalse); // 14 < 18
    tester.route(pointer.move(const Offset(20.0, 30.0))); // moved 10 horizontally and 20 vertically which is 22 total
    expect(didStartPan, isTrue); // 22 > 18
61 62 63 64
    didStartPan = false;
    expect(didEndPan, isFalse);
    expect(didTap, isFalse);

65
    tester.route(pointer.move(const Offset(20.0, 25.0)));
66
    expect(didStartPan, isFalse);
67
    expect(updatedScrollDelta, const Offset(0.0, -5.0));
68 69 70 71
    updatedScrollDelta = null;
    expect(didEndPan, isFalse);
    expect(didTap, isFalse);

72
    tester.route(pointer.up());
73 74 75 76 77
    expect(didStartPan, isFalse);
    expect(updatedScrollDelta, isNull);
    expect(didEndPan, isTrue);
    didEndPan = false;
    expect(didTap, isFalse);
78
  });
79

80
  testGesture('Should report most recent point to onStart by default', (GestureTester tester) {
81 82
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer();
    final VerticalDragGestureRecognizer competingDrag = VerticalDragGestureRecognizer()
83
      ..onStart = (_) {};
84 85
    addTearDown(() => drag.dispose);
    addTearDown(() => competingDrag.dispose);
86

87
    late Offset positionAtOnStart;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    drag.onStart = (DragStartDetails details) {
      positionAtOnStart = details.globalPosition;
    };

    final TestPointer pointer = TestPointer(5);
    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
    drag.addPointer(down);
    competingDrag.addPointer(down);
    tester.closeArena(5);
    tester.route(down);

    tester.route(pointer.move(const Offset(30.0, 0.0)));
    expect(positionAtOnStart, const Offset(30.0, 00.0));
  });

  testGesture('Should report most recent point to onStart with a start configuration', (GestureTester tester) {
104 105
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer();
    final VerticalDragGestureRecognizer competingDrag = VerticalDragGestureRecognizer()
106
      ..onStart = (_) {};
107 108
    addTearDown(() => drag.dispose);
    addTearDown(() => competingDrag.dispose);
109

110
    Offset? positionAtOnStart;
111 112 113
    drag.onStart = (DragStartDetails details) {
      positionAtOnStart = details.globalPosition;
    };
114
    Offset? updateOffset;
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    drag.onUpdate = (DragUpdateDetails details) {
      updateOffset = details.globalPosition;
    };

    final TestPointer pointer = TestPointer(5);
    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
    drag.addPointer(down);
    competingDrag.addPointer(down);
    tester.closeArena(5);
    tester.route(down);

    tester.route(pointer.move(const Offset(30.0, 0.0)));

    expect(positionAtOnStart, const Offset(30.0, 0.0));
    expect(updateOffset, null);
  });

132
  testGesture('Should recognize drag', (GestureTester tester) {
133
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer() ..dragStartBehavior = DragStartBehavior.down;
134
    addTearDown(drag.dispose);
135 136 137 138 139 140

    bool didStartDrag = false;
    drag.onStart = (_) {
      didStartDrag = true;
    };

141
    double? updatedDelta;
142 143
    drag.onUpdate = (DragUpdateDetails details) {
      updatedDelta = details.primaryDelta;
144 145 146
    };

    bool didEndDrag = false;
147
    drag.onEnd = (DragEndDetails details) {
148 149 150
      didEndDrag = true;
    };

151
    final TestPointer pointer = TestPointer(5);
152
    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
153 154 155 156 157 158 159 160 161 162 163
    drag.addPointer(down);
    tester.closeArena(5);
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(down);
    expect(didStartDrag, isTrue);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

164
    tester.route(pointer.move(const Offset(20.0, 25.0)));
165 166 167 168 169 170
    expect(didStartDrag, isTrue);
    didStartDrag = false;
    expect(updatedDelta, 10.0);
    updatedDelta = null;
    expect(didEndDrag, isFalse);

171
    tester.route(pointer.move(const Offset(20.0, 25.0)));
172 173 174 175 176 177 178 179 180 181 182
    expect(didStartDrag, isFalse);
    expect(updatedDelta, 0.0);
    updatedDelta = null;
    expect(didEndDrag, isFalse);

    tester.route(pointer.up());
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isTrue);
    didEndDrag = false;
  });
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  testGesture('Should reject mouse drag when configured to ignore mouse pointers - Horizontal', (GestureTester tester) {
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer(supportedDevices: <PointerDeviceKind>{
      PointerDeviceKind.touch,
    }) ..dragStartBehavior = DragStartBehavior.down;
    addTearDown(drag.dispose);

    bool didStartDrag = false;
    drag.onStart = (_) {
      didStartDrag = true;
    };

    double? updatedDelta;
    drag.onUpdate = (DragUpdateDetails details) {
      updatedDelta = details.primaryDelta;
    };

    bool didEndDrag = false;
    drag.onEnd = (DragEndDetails details) {
      didEndDrag = true;
    };

    final TestPointer pointer = TestPointer(5, PointerDeviceKind.mouse);
    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
    drag.addPointer(down);
    tester.closeArena(5);
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(down);
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(pointer.move(const Offset(20.0, 25.0)));
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(pointer.move(const Offset(20.0, 25.0)));
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(pointer.up());
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);
  });

  testGesture('Should reject mouse drag when configured to ignore mouse pointers - Vertical', (GestureTester tester) {
    final VerticalDragGestureRecognizer drag = VerticalDragGestureRecognizer(supportedDevices: <PointerDeviceKind>{
      PointerDeviceKind.touch,
    })..dragStartBehavior = DragStartBehavior.down;
    addTearDown(drag.dispose);

    bool didStartDrag = false;
    drag.onStart = (_) {
      didStartDrag = true;
    };

    double? updatedDelta;
    drag.onUpdate = (DragUpdateDetails details) {
      updatedDelta = details.primaryDelta;
    };

    bool didEndDrag = false;
    drag.onEnd = (DragEndDetails details) {
      didEndDrag = true;
    };

    final TestPointer pointer = TestPointer(5, PointerDeviceKind.mouse);
    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
    drag.addPointer(down);
    tester.closeArena(5);
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(down);
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(pointer.move(const Offset(25.0, 20.0)));
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(pointer.move(const Offset(25.0, 20.0)));
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(pointer.up());
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);
  });

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  testGesture('DragGestureRecognizer.onStart behavior test', (GestureTester tester) {
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer()
      ..dragStartBehavior = DragStartBehavior.down;
    addTearDown(drag.dispose);

    Duration? startTimestamp;
    Offset? positionAtOnStart;
    drag.onStart = (DragStartDetails details) {
      startTimestamp = details.sourceTimeStamp;
      positionAtOnStart = details.globalPosition;
    };

    Duration? updatedTimestamp;
    Offset? updateDelta;
    drag.onUpdate = (DragUpdateDetails details) {
      updatedTimestamp = details.sourceTimeStamp;
      updateDelta = details.delta;
    };

    // No competing, dragStartBehavior == DragStartBehavior.down
    final TestPointer pointer = TestPointer(5);
    PointerDownEvent down = pointer.down(const Offset(10.0, 10.0), timeStamp: const Duration(milliseconds: 100));
    drag.addPointer(down);
    tester.closeArena(5);
    expect(startTimestamp, isNull);
    expect(positionAtOnStart, isNull);
    expect(updatedTimestamp, isNull);

    tester.route(down);
    // The only horizontal drag gesture win the arena when the pointer down.
    expect(startTimestamp, const Duration(milliseconds: 100));
    expect(positionAtOnStart, const Offset(10.0, 10.0));
    expect(updatedTimestamp, isNull);

    tester.route(pointer.move(const Offset(20.0, 25.0), timeStamp: const Duration(milliseconds: 200)));
    expect(updatedTimestamp, const Duration(milliseconds: 200));
    expect(updateDelta, const Offset(10.0, 0.0));

    tester.route(pointer.move(const Offset(20.0, 25.0), timeStamp: const Duration(milliseconds: 300)));
    expect(updatedTimestamp, const Duration(milliseconds: 300));
    expect(updateDelta, Offset.zero);
    tester.route(pointer.up());

    // No competing, dragStartBehavior == DragStartBehavior.start
    // When there are no other gestures competing with this gesture in the arena,
    // there's no difference in behavior between the two settings.
    drag.dragStartBehavior = DragStartBehavior.start;
    startTimestamp = null;
    positionAtOnStart = null;
    updatedTimestamp = null;
    updateDelta = null;

    down = pointer.down(const Offset(10.0, 10.0), timeStamp: const Duration(milliseconds: 400));
    drag.addPointer(down);
    tester.closeArena(5);
    tester.route(down);

    expect(startTimestamp, const Duration(milliseconds: 400));
    expect(positionAtOnStart, const Offset(10.0, 10.0));
    expect(updatedTimestamp, isNull);

    tester.route(pointer.move(const Offset(20.0, 25.0), timeStamp: const Duration(milliseconds: 500)));
    expect(updatedTimestamp, const Duration(milliseconds: 500));
    tester.route(pointer.up());

    // With competing, dragStartBehavior == DragStartBehavior.start
    startTimestamp = null;
    positionAtOnStart = null;
    updatedTimestamp = null;
    updateDelta = null;

    final VerticalDragGestureRecognizer competingDrag = VerticalDragGestureRecognizer()
      ..onStart = (_) {};
    addTearDown(() => competingDrag.dispose);

    down = pointer.down(const Offset(10.0, 10.0), timeStamp: const Duration(milliseconds: 600));
    drag.addPointer(down);
    competingDrag.addPointer(down);
    tester.closeArena(5);
    tester.route(down);

    // The pointer down event do not trigger anything.
    expect(startTimestamp, isNull);
    expect(positionAtOnStart, isNull);
    expect(updatedTimestamp, isNull);

    tester.route(pointer.move(const Offset(30.0, 10.0), timeStamp: const Duration(milliseconds: 700)));
    expect(startTimestamp, const Duration(milliseconds: 700));
    // Using the position of the pointer at the time this gesture recognizer won the arena.
    expect(positionAtOnStart, const Offset(30.0, 10.0));
    expect(updatedTimestamp, isNull); // Do not trigger an update event.
    tester.route(pointer.up());

    // With competing, dragStartBehavior == DragStartBehavior.down
    drag.dragStartBehavior = DragStartBehavior.down;
    startTimestamp = null;
    positionAtOnStart = null;
    updatedTimestamp = null;
    updateDelta = null;

    down = pointer.down(const Offset(10.0, 10.0), timeStamp: const Duration(milliseconds: 800));
    drag.addPointer(down);
    competingDrag.addPointer(down);
    tester.closeArena(5);
    tester.route(down);

    expect(startTimestamp, isNull);
    expect(positionAtOnStart, isNull);
    expect(updatedTimestamp, isNull);

    tester.route(pointer.move(const Offset(30.0, 10.0), timeStamp: const Duration(milliseconds: 900)));
    expect(startTimestamp, const Duration(milliseconds: 900));
    // Using the position of the first detected down event for the pointer.
    expect(positionAtOnStart, const Offset(10.0, 10.0));
    expect(updatedTimestamp, const Duration(milliseconds: 900)); // Also, trigger an update event.
    expect(updateDelta, const Offset(20.0, 0.0));
    tester.route(pointer.up());
  });

403
  testGesture('Should report original timestamps', (GestureTester tester) {
404
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer() ..dragStartBehavior = DragStartBehavior.down;
405
    addTearDown(drag.dispose);
406

407
    Duration? startTimestamp;
408 409 410 411
    drag.onStart = (DragStartDetails details) {
      startTimestamp = details.sourceTimeStamp;
    };

412
    Duration? updatedTimestamp;
413 414 415 416
    drag.onUpdate = (DragUpdateDetails details) {
      updatedTimestamp = details.sourceTimeStamp;
    };

417
    final TestPointer pointer = TestPointer(5);
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0), timeStamp: const Duration(milliseconds: 100));
    drag.addPointer(down);
    tester.closeArena(5);
    expect(startTimestamp, isNull);

    tester.route(down);
    expect(startTimestamp, const Duration(milliseconds: 100));

    tester.route(pointer.move(const Offset(20.0, 25.0), timeStamp: const Duration(milliseconds: 200)));
    expect(updatedTimestamp, const Duration(milliseconds: 200));

    tester.route(pointer.move(const Offset(20.0, 25.0), timeStamp: const Duration(milliseconds: 300)));
    expect(updatedTimestamp, const Duration(milliseconds: 300));
  });

433
  testGesture('Should report initial down point to onStart with a down configuration', (GestureTester tester) {
434
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer()
435
      ..dragStartBehavior = DragStartBehavior.down;
436
    final VerticalDragGestureRecognizer competingDrag = VerticalDragGestureRecognizer()
437 438
      ..dragStartBehavior = DragStartBehavior.down
      ..onStart = (_) {};
439 440
    addTearDown(() => drag.dispose);
    addTearDown(() => competingDrag.dispose);
441

442
    Offset? positionAtOnStart;
443 444 445
    drag.onStart = (DragStartDetails details) {
      positionAtOnStart = details.globalPosition;
    };
446 447
    Offset? updateOffset;
    Offset? updateDelta;
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    drag.onUpdate = (DragUpdateDetails details) {
      updateOffset = details.globalPosition;
      updateDelta = details.delta;
    };

    final TestPointer pointer = TestPointer(5);
    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
    drag.addPointer(down);
    competingDrag.addPointer(down);
    tester.closeArena(5);
    tester.route(down);

    tester.route(pointer.move(const Offset(30.0, 0.0)));
    expect(positionAtOnStart, const Offset(10.0, 10.0));

    // The drag is horizontal so we're going to ignore the vertical delta position
    // when calculating the new global position.
    expect(updateOffset, const Offset(30.0, 10.0));
    expect(updateDelta, const Offset(20.0, 0.0));
  });

  testGesture('Drag with multiple pointers in down behavior', (GestureTester tester) {
470 471 472 473 474 475
    final HorizontalDragGestureRecognizer drag1 =
      HorizontalDragGestureRecognizer() ..dragStartBehavior = DragStartBehavior.down;
    final VerticalDragGestureRecognizer drag2 =
      VerticalDragGestureRecognizer() ..dragStartBehavior = DragStartBehavior.down;
    addTearDown(() => drag1.dispose);
    addTearDown(() => drag2.dispose);
476 477 478 479 480 481 482 483 484 485 486 487 488

    final List<String> log = <String>[];
    drag1.onDown = (_) { log.add('drag1-down'); };
    drag1.onStart = (_) { log.add('drag1-start'); };
    drag1.onUpdate = (_) { log.add('drag1-update'); };
    drag1.onEnd = (_) { log.add('drag1-end'); };
    drag1.onCancel = () { log.add('drag1-cancel'); };
    drag2.onDown = (_) { log.add('drag2-down'); };
    drag2.onStart = (_) { log.add('drag2-start'); };
    drag2.onUpdate = (_) { log.add('drag2-update'); };
    drag2.onEnd = (_) { log.add('drag2-end'); };
    drag2.onCancel = () { log.add('drag2-cancel'); };

489
    final TestPointer pointer5 = TestPointer(5);
490 491 492 493 494 495 496 497 498 499 500 501
    final PointerDownEvent down5 = pointer5.down(const Offset(10.0, 10.0));
    drag1.addPointer(down5);
    drag2.addPointer(down5);
    tester.closeArena(5);
    tester.route(down5);
    log.add('-a');

    tester.route(pointer5.move(const Offset(100.0, 0.0)));
    log.add('-b');
    tester.route(pointer5.move(const Offset(50.0, 50.0)));
    log.add('-c');

502
    final TestPointer pointer6 = TestPointer(6);
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
    final PointerDownEvent down6 = pointer6.down(const Offset(20.0, 20.0));
    drag1.addPointer(down6);
    drag2.addPointer(down6);
    tester.closeArena(6);
    tester.route(down6);
    log.add('-d');

    tester.route(pointer5.move(const Offset(0.0, 100.0)));
    log.add('-e');
    tester.route(pointer5.move(const Offset(70.0, 70.0)));
    log.add('-f');

    tester.route(pointer5.up());
    tester.route(pointer6.up());

    expect(log, <String>[
      'drag1-down',
      'drag2-down',
      '-a',
      'drag2-cancel',
      'drag1-start',
      'drag1-update',
      '-b',
      'drag1-update',
      '-c',
      'drag2-down',
      'drag2-cancel',
      '-d',
      'drag1-update',
      '-e',
      'drag1-update',
      '-f',
535
      'drag1-end',
536 537 538
    ]);
  });

539
  testGesture('Clamp max velocity', (GestureTester tester) {
540
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer() ..dragStartBehavior = DragStartBehavior.down;
541
    addTearDown(drag.dispose);
542

543 544
    late Velocity velocity;
    double? primaryVelocity;
545 546
    drag.onEnd = (DragEndDetails details) {
      velocity = details.velocity;
547
      primaryVelocity = details.primaryVelocity;
548 549
    };

550
    final TestPointer pointer = TestPointer(5);
551
    final PointerDownEvent down = pointer.down(const Offset(10.0, 25.0), timeStamp: const Duration(milliseconds: 10));
552 553 554
    drag.addPointer(down);
    tester.closeArena(5);
    tester.route(down);
555 556 557 558 559 560 561 562 563 564 565
    tester.route(pointer.move(const Offset(20.0, 25.0), timeStamp: const Duration(milliseconds: 10)));
    tester.route(pointer.move(const Offset(30.0, 25.0), timeStamp: const Duration(milliseconds: 11)));
    tester.route(pointer.move(const Offset(40.0, 25.0), timeStamp: const Duration(milliseconds: 12)));
    tester.route(pointer.move(const Offset(50.0, 25.0), timeStamp: const Duration(milliseconds: 13)));
    tester.route(pointer.move(const Offset(60.0, 25.0), timeStamp: const Duration(milliseconds: 14)));
    tester.route(pointer.move(const Offset(70.0, 25.0), timeStamp: const Duration(milliseconds: 15)));
    tester.route(pointer.move(const Offset(80.0, 25.0), timeStamp: const Duration(milliseconds: 16)));
    tester.route(pointer.move(const Offset(90.0, 25.0), timeStamp: const Duration(milliseconds: 17)));
    tester.route(pointer.move(const Offset(100.0, 25.0), timeStamp: const Duration(milliseconds: 18)));
    tester.route(pointer.move(const Offset(110.0, 25.0), timeStamp: const Duration(milliseconds: 19)));
    tester.route(pointer.move(const Offset(120.0, 25.0), timeStamp: const Duration(milliseconds: 20)));
566 567
    tester.route(pointer.up(timeStamp: const Duration(milliseconds: 20)));
    expect(velocity.pixelsPerSecond.dx, inInclusiveRange(0.99 * kMaxFlingVelocity, kMaxFlingVelocity));
568 569
    expect(velocity.pixelsPerSecond.dy, moreOrLessEquals(0.0));
    expect(primaryVelocity, velocity.pixelsPerSecond.dx);
570
  });
571

572
  testGesture('Synthesized pointer events are ignored for velocity tracking', (GestureTester tester) {
573
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer() ..dragStartBehavior = DragStartBehavior.down;
574
    addTearDown(drag.dispose);
575

576
    late Velocity velocity;
577 578 579 580
    drag.onEnd = (DragEndDetails details) {
      velocity = details.velocity;
    };

581
    final TestPointer pointer = TestPointer(1);
582 583 584 585 586 587 588 589
    final PointerDownEvent down = pointer.down(const Offset(10.0, 25.0), timeStamp: const Duration(milliseconds: 10));
    drag.addPointer(down);
    tester.closeArena(1);
    tester.route(down);
    tester.route(pointer.move(const Offset(20.0, 25.0), timeStamp: const Duration(milliseconds: 20)));
    tester.route(pointer.move(const Offset(30.0, 25.0), timeStamp: const Duration(milliseconds: 30)));
    tester.route(pointer.move(const Offset(40.0, 25.0), timeStamp: const Duration(milliseconds: 40)));
    tester.route(pointer.move(const Offset(50.0, 25.0), timeStamp: const Duration(milliseconds: 50)));
590
    tester.route(const PointerMoveEvent(
591 592 593
      pointer: 1,
      // Simulate a small synthesized wobble which would have slowed down the
      // horizontal velocity from 1 px/ms and introduced a slight vertical velocity.
594 595
      position: Offset(51.0, 26.0),
      timeStamp: Duration(milliseconds: 60),
596 597
      synthesized: true,
    ));
598 599 600 601 602 603 604 605
    tester.route(pointer.up(timeStamp: const Duration(milliseconds: 70)));
    expect(velocity.pixelsPerSecond.dx, moreOrLessEquals(1000.0));
    expect(velocity.pixelsPerSecond.dy, moreOrLessEquals(0.0));
  });

  /// Checks that quick flick gestures with 1 down, 2 move and 1 up pointer
  /// events still have a velocity
  testGesture('Quick flicks have velocity', (GestureTester tester) {
606
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer() ..dragStartBehavior = DragStartBehavior.down;
607
    addTearDown(drag.dispose);
608

609
    late Velocity velocity;
610 611 612 613
    drag.onEnd = (DragEndDetails details) {
      velocity = details.velocity;
    };

614
    final TestPointer pointer = TestPointer(1);
615 616 617 618 619 620 621 622
    final PointerDownEvent down = pointer.down(const Offset(10.0, 25.0), timeStamp: const Duration(milliseconds: 10));
    drag.addPointer(down);
    tester.closeArena(1);
    tester.route(down);
    tester.route(pointer.move(const Offset(20.0, 25.0), timeStamp: const Duration(milliseconds: 20)));
    tester.route(pointer.move(const Offset(30.0, 25.0), timeStamp: const Duration(milliseconds: 30)));
    tester.route(pointer.up(timeStamp: const Duration(milliseconds: 40)));
    // 3 events moving by 10px every 10ms = 1000px/s.
623 624 625 626
    expect(velocity.pixelsPerSecond.dx, moreOrLessEquals(1000.0));
    expect(velocity.pixelsPerSecond.dy, moreOrLessEquals(0.0));
  });

627
  testGesture('Drag details', (GestureTester tester) {
628 629 630 631
    expect(DragDownDetails(), hasOneLineDescription);
    expect(DragStartDetails(), hasOneLineDescription);
    expect(DragUpdateDetails(globalPosition: Offset.zero), hasOneLineDescription);
    expect(DragEndDetails(), hasOneLineDescription);
632
  });
jslavitz's avatar
jslavitz committed
633 634

  testGesture('Should recognize drag', (GestureTester tester) {
635
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer() ..dragStartBehavior = DragStartBehavior.down;
636
    addTearDown(drag.dispose);
jslavitz's avatar
jslavitz committed
637 638 639 640 641 642

    bool didStartDrag = false;
    drag.onStart = (_) {
      didStartDrag = true;
    };

643 644
    Offset? updateDelta;
    double? updatePrimaryDelta;
jslavitz's avatar
jslavitz committed
645
    drag.onUpdate = (DragUpdateDetails details) {
646 647
      updateDelta = details.delta;
      updatePrimaryDelta = details.primaryDelta;
jslavitz's avatar
jslavitz committed
648 649 650 651 652 653 654
    };

    bool didEndDrag = false;
    drag.onEnd = (DragEndDetails details) {
      didEndDrag = true;
    };

655
    final TestPointer pointer = TestPointer(5);
jslavitz's avatar
jslavitz committed
656 657 658
    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
    drag.addPointer(down);
    tester.closeArena(5);
659

jslavitz's avatar
jslavitz committed
660
    expect(didStartDrag, isFalse);
661 662
    expect(updateDelta, isNull);
    expect(updatePrimaryDelta, isNull);
jslavitz's avatar
jslavitz committed
663 664 665 666
    expect(didEndDrag, isFalse);

    tester.route(down);
    expect(didStartDrag, isTrue);
667 668
    expect(updateDelta, isNull);
    expect(updatePrimaryDelta, isNull);
jslavitz's avatar
jslavitz committed
669
    expect(didEndDrag, isFalse);
670
    didStartDrag = false;
jslavitz's avatar
jslavitz committed
671 672

    tester.route(pointer.move(const Offset(20.0, 25.0)));
673 674 675
    expect(didStartDrag, isFalse);
    expect(updateDelta, const Offset(10.0, 0.0));
    expect(updatePrimaryDelta, 10.0);
jslavitz's avatar
jslavitz committed
676
    expect(didEndDrag, isFalse);
677 678
    updateDelta = null;
    updatePrimaryDelta = null;
jslavitz's avatar
jslavitz committed
679 680 681

    tester.route(pointer.move(const Offset(20.0, 25.0)));
    expect(didStartDrag, isFalse);
682
    expect(updateDelta, Offset.zero);
683
    expect(updatePrimaryDelta, 0.0);
jslavitz's avatar
jslavitz committed
684
    expect(didEndDrag, isFalse);
685 686
    updateDelta = null;
    updatePrimaryDelta = null;
jslavitz's avatar
jslavitz committed
687 688 689

    tester.route(pointer.up());
    expect(didStartDrag, isFalse);
690 691
    expect(updateDelta, isNull);
    expect(updatePrimaryDelta, isNull);
jslavitz's avatar
jslavitz committed
692 693 694 695 696
    expect(didEndDrag, isTrue);
    didEndDrag = false;
  });

  testGesture('Should recognize drag', (GestureTester tester) {
697
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer() ..dragStartBehavior = DragStartBehavior.down;
698
    addTearDown(drag.dispose);
jslavitz's avatar
jslavitz committed
699

700
    Offset? latestGlobalPosition;
701 702 703
    drag.onStart = (DragStartDetails details) {
      latestGlobalPosition = details.globalPosition;
    };
704
    Offset? latestDelta;
jslavitz's avatar
jslavitz committed
705
    drag.onUpdate = (DragUpdateDetails details) {
706 707
      latestGlobalPosition = details.globalPosition;
      latestDelta = details.delta;
jslavitz's avatar
jslavitz committed
708 709
    };

710
    final TestPointer pointer = TestPointer(5);
jslavitz's avatar
jslavitz committed
711 712 713
    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
    drag.addPointer(down);
    tester.closeArena(5);
714

jslavitz's avatar
jslavitz committed
715
    tester.route(down);
716 717 718 719 720 721 722 723 724 725 726
    expect(latestGlobalPosition, const Offset(10.0, 10.0));
    expect(latestDelta, isNull);

    tester.route(pointer.move(const Offset(20.0, 25.0)));
    expect(latestGlobalPosition, const Offset(20.0, 25.0));
    expect(latestDelta, const Offset(10.0, 0.0));

    tester.route(pointer.move(const Offset(0.0, 45.0)));
    expect(latestGlobalPosition, const Offset(0.0, 45.0));
    expect(latestDelta, const Offset(-20.0, 0.0));

jslavitz's avatar
jslavitz committed
727 728
    tester.route(pointer.up());
  });
729 730 731 732 733 734 735

  testGesture('Can filter drags based on device kind', (GestureTester tester) {
    final HorizontalDragGestureRecognizer drag =
        HorizontalDragGestureRecognizer(
            kind: PointerDeviceKind.mouse,
        )
        ..dragStartBehavior = DragStartBehavior.down;
736
    addTearDown(drag.dispose);
737 738 739 740 741 742

    bool didStartDrag = false;
    drag.onStart = (_) {
      didStartDrag = true;
    };

743
    double? updatedDelta;
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
    drag.onUpdate = (DragUpdateDetails details) {
      updatedDelta = details.primaryDelta;
    };

    bool didEndDrag = false;
    drag.onEnd = (DragEndDetails details) {
      didEndDrag = true;
    };

    // Using a touch pointer to drag shouldn't be recognized.
    final TestPointer touchPointer = TestPointer(5, PointerDeviceKind.touch);
    final PointerDownEvent touchDown = touchPointer.down(const Offset(10.0, 10.0));
    drag.addPointer(touchDown);
    tester.closeArena(5);
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(touchDown);
    // Still doesn't recognize the drag because it's coming from a touch pointer.
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(touchPointer.move(const Offset(20.0, 25.0)));
    // Still doesn't recognize the drag because it's coming from a touch pointer.
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(touchPointer.up());
    // Still doesn't recognize the drag because it's coming from a touch pointer.
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    // Using a mouse pointer to drag should be recognized.
    final TestPointer mousePointer = TestPointer(5, PointerDeviceKind.mouse);
    final PointerDownEvent mouseDown = mousePointer.down(const Offset(10.0, 10.0));
    drag.addPointer(mouseDown);
    tester.closeArena(5);
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(mouseDown);
    expect(didStartDrag, isTrue);
    didStartDrag = false;
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(mousePointer.move(const Offset(20.0, 25.0)));
    expect(didStartDrag, isFalse);
    expect(updatedDelta, 10.0);
    updatedDelta = null;
    expect(didEndDrag, isFalse);

    tester.route(mousePointer.up());
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isTrue);
    didEndDrag = false;
  });
807 808

  group('Enforce consistent-button restriction:', () {
809 810
    late PanGestureRecognizer pan;
    late TapGestureRecognizer tap;
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
    final List<String> logs = <String>[];

    setUp(() {
      tap = TapGestureRecognizer()
        ..onTap = () {}; // Need a callback to enable competition
      pan = PanGestureRecognizer()
        ..onStart = (DragStartDetails details) {
          logs.add('start');
        }
        ..onDown = (DragDownDetails details) {
          logs.add('down');
        }
        ..onUpdate = (DragUpdateDetails details) {
          logs.add('update');
        }
        ..onCancel = () {
          logs.add('cancel');
        }
        ..onEnd = (DragEndDetails details) {
          logs.add('end');
        };
    });

    tearDown(() {
      pan.dispose();
      tap.dispose();
      logs.clear();
    });

    testGesture('Button change before acceptance should lead to immediate cancel', (GestureTester tester) {
      final TestPointer pointer = TestPointer(5, PointerDeviceKind.mouse, kPrimaryButton);
      final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
      pan.addPointer(down);
      tap.addPointer(down);
      tester.closeArena(5);

      tester.route(down);
      expect(logs, <String>['down']);
      // Move out of slop so make sure button changes takes priority over slops
      tester.route(pointer.move(const Offset(30.0, 30.0), buttons: kSecondaryButton));
      expect(logs, <String>['down', 'cancel']);

      tester.route(pointer.up());
    });

    testGesture('Button change before acceptance should not prevent the next drag', (GestureTester tester) {
      { // First drag (which is canceled)
        final TestPointer pointer = TestPointer(5, PointerDeviceKind.mouse, kPrimaryButton);
        final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
        pan.addPointer(down);
        tap.addPointer(down);
        tester.closeArena(down.pointer);

        tester.route(down);
        tester.route(pointer.move(const Offset(10.0, 10.0), buttons: kSecondaryButton));
        tester.route(pointer.up());
        expect(logs, <String>['down', 'cancel']);
      }
      logs.clear();

      final TestPointer pointer2 = TestPointer(6, PointerDeviceKind.mouse, kPrimaryButton);
      final PointerDownEvent down2 = pointer2.down(const Offset(10.0, 10.0));
      pan.addPointer(down2);
      tap.addPointer(down2);
      tester.closeArena(down2.pointer);
      tester.route(down2);
      expect(logs, <String>['down']);

      tester.route(pointer2.move(const Offset(30.0, 30.0)));
      expect(logs, <String>['down', 'start']);

      tester.route(pointer2.up());
      expect(logs, <String>['down', 'start', 'end']);
    });

    testGesture('Button change after acceptance should lead to immediate end', (GestureTester tester) {
      final TestPointer pointer = TestPointer(5, PointerDeviceKind.mouse, kPrimaryButton);
      final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
      pan.addPointer(down);
      tap.addPointer(down);
      tester.closeArena(down.pointer);

      tester.route(down);
      expect(logs, <String>['down']);
      tester.route(pointer.move(const Offset(30.0, 30.0)));
      expect(logs, <String>['down', 'start']);
      tester.route(pointer.move(const Offset(30.0, 30.0), buttons: kSecondaryButton));
      expect(logs, <String>['down', 'start', 'end']);

      // Make sure no further updates are sent
      tester.route(pointer.move(const Offset(50.0, 50.0)));
      expect(logs, <String>['down', 'start', 'end']);

      tester.route(pointer.up());
    });

    testGesture('Button change after acceptance should not prevent the next drag', (GestureTester tester) {
      { // First drag (which is canceled)
        final TestPointer pointer = TestPointer(5, PointerDeviceKind.mouse, kPrimaryButton);
        final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
        pan.addPointer(down);
        tap.addPointer(down);
        tester.closeArena(down.pointer);

        tester.route(down);

        tester.route(pointer.move(const Offset(30.0, 30.0)));

        tester.route(pointer.move(const Offset(30.0, 31.0), buttons: kSecondaryButton));
        tester.route(pointer.up());
        expect(logs, <String>['down', 'start', 'end']);
      }
      logs.clear();

      final TestPointer pointer2 = TestPointer(6, PointerDeviceKind.mouse, kPrimaryButton);
      final PointerDownEvent down2 = pointer2.down(const Offset(10.0, 10.0));
      pan.addPointer(down2);
      tap.addPointer(down2);
      tester.closeArena(down2.pointer);
      tester.route(down2);
      expect(logs, <String>['down']);

      tester.route(pointer2.move(const Offset(30.0, 30.0)));
      expect(logs, <String>['down', 'start']);

      tester.route(pointer2.up());
      expect(logs, <String>['down', 'start', 'end']);
    });
  });

  group('Recognizers listening on different buttons do not form competition:', () {
    // This test is assisted by tap recognizers. If a tap gesture has
    // no competing recognizers, a pointer down event triggers its onTapDown
    // immediately; if there are competitors, onTapDown is triggered after a
    // timeout.
    // The following tests make sure that drag recognizers do not form
    // competition with a tap gesture recognizer listening on a different button.

    final List<String> recognized = <String>[];
950 951 952
    late TapGestureRecognizer tapPrimary;
    late TapGestureRecognizer tapSecondary;
    late PanGestureRecognizer pan;
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
    setUp(() {
      tapPrimary = TapGestureRecognizer()
        ..onTapDown = (TapDownDetails details) {
          recognized.add('tapPrimary');
        };
      tapSecondary = TapGestureRecognizer()
        ..onSecondaryTapDown = (TapDownDetails details) {
          recognized.add('tapSecondary');
        };
      pan = PanGestureRecognizer()
        ..onStart = (_) {
          recognized.add('drag');
        };
    });

    tearDown(() {
      recognized.clear();
      tapPrimary.dispose();
      tapSecondary.dispose();
      pan.dispose();
    });

Chris Bracken's avatar
Chris Bracken committed
975
    testGesture('A primary pan recognizer does not form competition with a secondary tap recognizer', (GestureTester tester) {
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
      final TestPointer pointer = TestPointer(
        1,
        PointerDeviceKind.touch,
        0,
        kSecondaryButton,
      );
      final PointerDownEvent down = pointer.down(const Offset(10, 10));
      pan.addPointer(down);
      tapSecondary.addPointer(down);
      tester.closeArena(down.pointer);

      tester.route(down);
      expect(recognized, <String>['tapSecondary']);
    });

Chris Bracken's avatar
Chris Bracken committed
991
    testGesture('A primary pan recognizer forms competition with a primary tap recognizer', (GestureTester tester) {
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
      final TestPointer pointer = TestPointer(
        1,
        PointerDeviceKind.touch,
        kPrimaryButton,
      );
      final PointerDownEvent down = pointer.down(const Offset(10, 10));
      pan.addPointer(down);
      tapPrimary.addPointer(down);
      tester.closeArena(down.pointer);

      tester.route(down);
      expect(recognized, <String>[]);

      tester.route(pointer.up());
      expect(recognized, <String>['tapPrimary']);
    });
  });

  testGesture('A secondary drag should not trigger primary', (GestureTester tester) {
    final List<String> recognized = <String>[];
    final TapGestureRecognizer tap = TapGestureRecognizer()
Chris Bracken's avatar
Chris Bracken committed
1013
      ..onTap = () {}; // Need a listener to enable competition.
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
    final PanGestureRecognizer pan = PanGestureRecognizer()
      ..onDown = (DragDownDetails details) {
        recognized.add('primaryDown');
      }
      ..onStart = (DragStartDetails details) {
        recognized.add('primaryStart');
      }
      ..onUpdate = (DragUpdateDetails details) {
        recognized.add('primaryUpdate');
      }
      ..onEnd = (DragEndDetails details) {
        recognized.add('primaryEnd');
      }
      ..onCancel = () {
        recognized.add('primaryCancel');
      };
1030 1031
    addTearDown(pan.dispose);
    addTearDown(tap.dispose);
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

    final TestPointer pointer = TestPointer(
      5,
      PointerDeviceKind.touch,
      0,
      kSecondaryButton,
    );

    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
    pan.addPointer(down);
    tap.addPointer(down);
    tester.closeArena(5);
    tester.route(down);
    tester.route(pointer.move(const Offset(20.0, 30.0)));
    tester.route(pointer.move(const Offset(20.0, 25.0)));
    tester.route(pointer.up());
    expect(recognized, <String>[]);
    recognized.clear();
1050
  });
1051

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
  testGesture('A secondary drag should not trigger primary', (GestureTester tester) {
    final List<String> recognized = <String>[];
    final TapGestureRecognizer tap = TapGestureRecognizer()
      ..onTap = () {}; // Need a listener to enable competition.
    final PanGestureRecognizer pan = PanGestureRecognizer()
      ..onDown = (DragDownDetails details) {
        recognized.add('primaryDown');
      }
      ..onStart = (DragStartDetails details) {
        recognized.add('primaryStart');
      }
      ..onUpdate = (DragUpdateDetails details) {
        recognized.add('primaryUpdate');
      }
      ..onEnd = (DragEndDetails details) {
        recognized.add('primaryEnd');
      }
      ..onCancel = () {
        recognized.add('primaryCancel');
      };

    final TestPointer pointer = TestPointer(
      5,
      PointerDeviceKind.touch,
      0,
      kSecondaryButton,
    );

    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
    pan.addPointer(down);
    tap.addPointer(down);
    tester.closeArena(5);
    tester.route(down);
    tester.route(pointer.move(const Offset(20.0, 30.0)));
    tester.route(pointer.move(const Offset(20.0, 25.0)));
    tester.route(pointer.up());
    expect(recognized, <String>[]);
1089
    recognized.clear();
1090 1091 1092 1093 1094 1095

    addTearDown(pan.dispose);
    addTearDown(tap.dispose);
    recognized.clear();
  });

1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
  testGesture(
    'On multiple pointers, DragGestureRecognizer is canceled '
    'when all pointers are canceled (FIFO)',
    (GestureTester tester) {
      // This test simulates the following scenario:
      // P1 down, P2 down, P1 up, P2 up
      final List<String> logs = <String>[];
      final HorizontalDragGestureRecognizer hori = HorizontalDragGestureRecognizer()
        ..onDown = (DragDownDetails details) { logs.add('downH'); }
        ..onStart = (DragStartDetails details) { logs.add('startH'); }
        ..onUpdate = (DragUpdateDetails details) { logs.add('updateH'); }
        ..onEnd = (DragEndDetails details) { logs.add('endH'); }
        ..onCancel = () { logs.add('cancelH'); };
      // Competitor
      final TapGestureRecognizer vert = TapGestureRecognizer()
        ..onTapDown = (TapDownDetails details) { logs.add('downT'); }
        ..onTapUp = (TapUpDetails details) { logs.add('upT'); }
        ..onTapCancel = () {};
      addTearDown(hori.dispose);
      addTearDown(vert.dispose);

      final TestPointer pointer1 = TestPointer(4, PointerDeviceKind.touch);
      final TestPointer pointer2 = TestPointer(5, PointerDeviceKind.touch);

      final PointerDownEvent down1 = pointer1.down(const Offset(10.0, 10.0));
      final PointerDownEvent down2 = pointer2.down(const Offset(11.0, 10.0));

      hori.addPointer(down1);
      vert.addPointer(down1);
      tester.route(down1);
      tester.closeArena(pointer1.pointer);
      expect(logs, <String>['downH']);
      logs.clear();
1129

1130 1131 1132 1133 1134 1135
      hori.addPointer(down2);
      vert.addPointer(down2);
      tester.route(down2);
      tester.closeArena(pointer2.pointer);
      expect(logs, <String>[]);
      logs.clear();
1136

1137
      tester.route(pointer1.up());
1138
      GestureBinding.instance!.gestureArena.sweep(pointer1.pointer);
1139 1140
      expect(logs, <String>['downT', 'upT']);
      logs.clear();
1141

1142
      tester.route(pointer2.up());
1143
      GestureBinding.instance!.gestureArena.sweep(pointer2.pointer);
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
      expect(logs, <String>['cancelH']);
      logs.clear();
    },
  );

  testGesture(
    'On multiple pointers, DragGestureRecognizer is canceled '
    'when all pointers are canceled (FILO)',
    (GestureTester tester) {
      // This test simulates the following scenario:
      // P1 down, P2 down, P1 up, P2 up
      final List<String> logs = <String>[];
      final HorizontalDragGestureRecognizer hori = HorizontalDragGestureRecognizer()
        ..onDown = (DragDownDetails details) { logs.add('downH'); }
        ..onStart = (DragStartDetails details) { logs.add('startH'); }
        ..onUpdate = (DragUpdateDetails details) { logs.add('updateH'); }
        ..onEnd = (DragEndDetails details) { logs.add('endH'); }
        ..onCancel = () { logs.add('cancelH'); };
      // Competitor
      final TapGestureRecognizer vert = TapGestureRecognizer()
        ..onTapDown = (TapDownDetails details) { logs.add('downT'); }
        ..onTapUp = (TapUpDetails details) { logs.add('upT'); }
        ..onTapCancel = () {};
      addTearDown(hori.dispose);
      addTearDown(vert.dispose);

      final TestPointer pointer1 = TestPointer(4, PointerDeviceKind.touch);
      final TestPointer pointer2 = TestPointer(5, PointerDeviceKind.touch);

      final PointerDownEvent down1 = pointer1.down(const Offset(10.0, 10.0));
      final PointerDownEvent down2 = pointer2.down(const Offset(11.0, 10.0));

      hori.addPointer(down1);
      vert.addPointer(down1);
      tester.route(down1);
      tester.closeArena(pointer1.pointer);
      expect(logs, <String>['downH']);
      logs.clear();
1182

1183 1184 1185 1186 1187 1188
      hori.addPointer(down2);
      vert.addPointer(down2);
      tester.route(down2);
      tester.closeArena(pointer2.pointer);
      expect(logs, <String>[]);
      logs.clear();
1189

1190
      tester.route(pointer2.up());
1191
      GestureBinding.instance!.gestureArena.sweep(pointer2.pointer);
1192 1193 1194 1195 1196
      // Tap is not triggered because pointer2 is not its primary pointer
      expect(logs, <String>[]);
      logs.clear();

      tester.route(pointer1.up());
1197
      GestureBinding.instance!.gestureArena.sweep(pointer1.pointer);
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
      expect(logs, <String>['cancelH', 'downT', 'upT']);
      logs.clear();
    },
  );

  testGesture(
    'On multiple pointers, DragGestureRecognizer is accepted when the '
    'first pointer is accepted',
    (GestureTester tester) {
      // This test simulates the following scenario:
      // P1 down, P2 down, P1 moves away, P2 up
      final List<String> logs = <String>[];
      final HorizontalDragGestureRecognizer hori = HorizontalDragGestureRecognizer()
        ..onDown = (DragDownDetails details) { logs.add('downH'); }
        ..onStart = (DragStartDetails details) { logs.add('startH'); }
        ..onUpdate = (DragUpdateDetails details) { logs.add('updateH'); }
        ..onEnd = (DragEndDetails details) { logs.add('endH'); }
        ..onCancel = () { logs.add('cancelH'); };
      // Competitor
      final TapGestureRecognizer vert = TapGestureRecognizer()
        ..onTapDown = (TapDownDetails details) { logs.add('downT'); }
        ..onTapUp = (TapUpDetails details) { logs.add('upT'); }
        ..onTapCancel = () {};
      addTearDown(hori.dispose);
      addTearDown(vert.dispose);

      final TestPointer pointer1 = TestPointer(4, PointerDeviceKind.touch);
      final TestPointer pointer2 = TestPointer(5, PointerDeviceKind.touch);

      final PointerDownEvent down1 = pointer1.down(const Offset(10.0, 10.0));
      final PointerDownEvent down2 = pointer2.down(const Offset(11.0, 10.0));

      hori.addPointer(down1);
      vert.addPointer(down1);
      tester.route(down1);
      tester.closeArena(pointer1.pointer);
      expect(logs, <String>['downH']);
      logs.clear();

      hori.addPointer(down2);
      vert.addPointer(down2);
      tester.route(down2);
      tester.closeArena(pointer2.pointer);
      expect(logs, <String>[]);
      logs.clear();

      tester.route(pointer1.move(const Offset(100, 100)));
      expect(logs, <String>['startH']);
      logs.clear();

      tester.route(pointer2.up());
1249
      GestureBinding.instance!.gestureArena.sweep(pointer2.pointer);
1250 1251 1252 1253
      expect(logs, <String>[]);
      logs.clear();

      tester.route(pointer1.up());
1254
      GestureBinding.instance!.gestureArena.sweep(pointer1.pointer);
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
      expect(logs, <String>['endH']);
      logs.clear();
    },
  );

  testGesture(
    'On multiple pointers, canceled pointers (due to up) do not '
    'prevent later pointers getting accepted',
    (GestureTester tester) {
      // This test simulates the following scenario:
      // P1 down, P2 down, P1 Up, P2 moves away
      final List<String> logs = <String>[];
      final HorizontalDragGestureRecognizer hori = HorizontalDragGestureRecognizer()
        ..onDown = (DragDownDetails details) { logs.add('downH'); }
        ..onStart = (DragStartDetails details) { logs.add('startH'); }
        ..onUpdate = (DragUpdateDetails details) { logs.add('updateH'); }
        ..onEnd = (DragEndDetails details) { logs.add('endH'); }
        ..onCancel = () { logs.add('cancelH'); };
      // Competitor
      final TapGestureRecognizer vert = TapGestureRecognizer()
        ..onTapDown = (TapDownDetails details) { logs.add('downT'); }
        ..onTapUp = (TapUpDetails details) { logs.add('upT'); }
        ..onTapCancel = () {};
      addTearDown(hori.dispose);
      addTearDown(vert.dispose);

      final TestPointer pointer1 = TestPointer(4, PointerDeviceKind.touch);
      final TestPointer pointer2 = TestPointer(5, PointerDeviceKind.touch);

      final PointerDownEvent down1 = pointer1.down(const Offset(10.0, 10.0));
      final PointerDownEvent down2 = pointer2.down(const Offset(11.0, 10.0));

      hori.addPointer(down1);
      vert.addPointer(down1);
      tester.route(down1);
      tester.closeArena(pointer1.pointer);
      expect(logs, <String>['downH']);
      logs.clear();

      hori.addPointer(down2);
      vert.addPointer(down2);
      tester.route(down2);
      tester.closeArena(pointer2.pointer);
      expect(logs, <String>[]);
      logs.clear();

      tester.route(pointer1.up());
1302
      GestureBinding.instance!.gestureArena.sweep(pointer1.pointer);
1303 1304 1305 1306 1307 1308 1309 1310
      expect(logs, <String>['downT', 'upT']);
      logs.clear();

      tester.route(pointer2.move(const Offset(100, 100)));
      expect(logs, <String>['startH']);
      logs.clear();

      tester.route(pointer2.up());
1311
      GestureBinding.instance!.gestureArena.sweep(pointer2.pointer);
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
      expect(logs, <String>['endH']);
      logs.clear();
    },
  );

  testGesture(
    'On multiple pointers, canceled pointers (due to buttons) do not '
    'prevent later pointers getting accepted',
    (GestureTester tester) {
      // This test simulates the following scenario:
      // P1 down, P2 down, P1 change buttons, P2 moves away
      final List<String> logs = <String>[];
      final HorizontalDragGestureRecognizer hori = HorizontalDragGestureRecognizer()
        ..onDown = (DragDownDetails details) { logs.add('downH'); }
        ..onStart = (DragStartDetails details) { logs.add('startH'); }
        ..onUpdate = (DragUpdateDetails details) { logs.add('updateH'); }
        ..onEnd = (DragEndDetails details) { logs.add('endH'); }
        ..onCancel = () { logs.add('cancelH'); };
      // Competitor
      final TapGestureRecognizer vert = TapGestureRecognizer()
        ..onTapDown = (TapDownDetails details) { logs.add('downT'); }
        ..onTapUp = (TapUpDetails details) { logs.add('upT'); }
        ..onTapCancel = () {};
      addTearDown(hori.dispose);
      addTearDown(vert.dispose);

      final TestPointer pointer1 = TestPointer(1, PointerDeviceKind.touch);
      final TestPointer pointer2 = TestPointer(2, PointerDeviceKind.touch);

      final PointerDownEvent down1 = pointer1.down(const Offset(10.0, 10.0));
      final PointerDownEvent down2 = pointer2.down(const Offset(11.0, 10.0));

      hori.addPointer(down1);
      vert.addPointer(down1);
      tester.route(down1);
      tester.closeArena(pointer1.pointer);

      hori.addPointer(down2);
      vert.addPointer(down2);
      tester.route(down2);
      tester.closeArena(pointer2.pointer);
      expect(logs, <String>['downH']);
      logs.clear();

      // Pointer 1 changes buttons, which cancel tap, leaving drag the only
      // remaining member of arena 1, therefore drag is accepted.
      tester.route(pointer1.move(const Offset(9.9, 9.9), buttons: kSecondaryButton));
      expect(logs, <String>['startH']);
      logs.clear();

      tester.route(pointer2.move(const Offset(100, 100)));
      expect(logs, <String>['updateH']);
      logs.clear();

      tester.route(pointer2.up());
1367
      GestureBinding.instance!.gestureArena.sweep(pointer2.pointer);
1368 1369 1370 1371
      expect(logs, <String>['endH']);
      logs.clear();
    },
  );
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428

  testGesture(
    'On multiple pointers, the last tracking pointer can be rejected by [resolvePointer] when the '
    'other pointer already accepted the VerticalDragGestureRecognizer',
    (GestureTester tester) {
      // Regressing test for https://github.com/flutter/flutter/issues/68373
      final List<String> logs = <String>[];
      final VerticalDragGestureRecognizer drag = VerticalDragGestureRecognizer()
        ..onDown = (DragDownDetails details) { logs.add('downD'); }
        ..onStart = (DragStartDetails details) { logs.add('startD'); }
        ..onUpdate = (DragUpdateDetails details) { logs.add('updateD'); }
        ..onEnd = (DragEndDetails details) { logs.add('endD'); }
        ..onCancel = () { logs.add('cancelD'); };
      // Competitor
      final TapGestureRecognizer tap = TapGestureRecognizer()
        ..onTapDown = (TapDownDetails details) { logs.add('downT'); }
        ..onTapUp = (TapUpDetails details) { logs.add('upT'); }
        ..onTapCancel = () {};
      addTearDown(tap.dispose);
      addTearDown(drag.dispose);

      final TestPointer pointer1 = TestPointer(1, PointerDeviceKind.touch);
      final TestPointer pointer2 = TestPointer(2, PointerDeviceKind.touch);
      final TestPointer pointer3 = TestPointer(3, PointerDeviceKind.touch);
      final TestPointer pointer4 = TestPointer(4, PointerDeviceKind.touch);

      final PointerDownEvent down1 = pointer1.down(const Offset(10.0, 10.0));
      final PointerDownEvent down2 = pointer2.down(const Offset(11.0, 11.0));
      final PointerDownEvent down3 = pointer3.down(const Offset(12.0, 12.0));
      final PointerDownEvent down4 = pointer4.down(const Offset(13.0, 13.0));

      tap.addPointer(down1);
      drag.addPointer(down1);
      tester.closeArena(pointer1.pointer);
      tester.route(down1);
      expect(logs, <String>['downD']);
      logs.clear();

      tap.addPointer(down2);
      drag.addPointer(down2);
      tester.closeArena(pointer2.pointer);
      tester.route(down2);
      expect(logs, <String>[]);

      tap.addPointer(down3);
      drag.addPointer(down3);
      tester.closeArena(pointer3.pointer);
      tester.route(down3);
      expect(logs, <String>[]);

      drag.addPointer(down4);
      tester.closeArena(pointer4.pointer);
      tester.route(down4);
      expect(logs, <String>['startD']);
      logs.clear();

      tester.route(pointer2.up());
1429
      GestureBinding.instance!.gestureArena.sweep(pointer2.pointer);
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
      expect(logs, <String>[]);

      tester.route(pointer4.cancel());
      expect(logs, <String>[]);

      tester.route(pointer3.cancel());
      expect(logs, <String>[]);

      tester.route(pointer1.cancel());
      expect(logs, <String>['endD']);
      logs.clear();
    },
  );
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486

  testGesture('Does not crash when one of the 2 pointers wins by default and is then released', (GestureTester tester) {
    // Regression test for https://github.com/flutter/flutter/issues/82784

    bool didStartDrag = false;
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer()
      ..onStart = (_) { didStartDrag = true; }
      ..onEnd = (DragEndDetails details) {} // Crash triggers at onEnd.
      ..dragStartBehavior = DragStartBehavior.down;
    final TapGestureRecognizer tap = TapGestureRecognizer()..onTap = () {};
    final TapGestureRecognizer tap2 = TapGestureRecognizer()..onTap = () {};

    // The pointer1 is caught by drag and tap.
    final TestPointer pointer1 = TestPointer(5);
    final PointerDownEvent down1 = pointer1.down(const Offset(10.0, 10.0));
    drag.addPointer(down1);
    tap.addPointer(down1);
    tester.closeArena(pointer1.pointer);
    tester.route(down1);

    // The pointer2 is caught by drag and tap2.
    final TestPointer pointer2 = TestPointer(6);
    final PointerDownEvent down2 = pointer2.down(const Offset(10.0, 10.0));
    drag.addPointer(down2);
    tap2.addPointer(down2);
    tester.closeArena(pointer2.pointer);
    tester.route(down2);

    // The tap is disposed, leaving drag the default winner.
    tap.dispose();

    // Wait for microtasks to finish, during which drag claims victory.
    tester.async.flushMicrotasks();
    expect(didStartDrag, true);

    // The pointer1 is released, leaving pointer2 drag's only pointer.
    tester.route(pointer1.up());

    drag.dispose();

    // Passes if no crashes here.

    tap2.dispose();
  });
1487
}