scale_test.dart 44.1 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 6
import 'dart:math' as math;

7
import 'package:flutter/gestures.dart';
8
import 'package:flutter_test/flutter_test.dart';
9

10 11
import 'gesture_tester.dart';

12
void main() {
13
  TestWidgetsFlutterBinding.ensureInitialized();
14

15
  testGesture('Should recognize scale gestures', (GestureTester tester) {
16 17
    final ScaleGestureRecognizer scale = ScaleGestureRecognizer();
    final TapGestureRecognizer tap = TapGestureRecognizer();
18 19

    bool didStartScale = false;
20
    Offset? updatedFocalPoint;
21
    scale.onStart = (ScaleStartDetails details) {
22
      didStartScale = true;
23
      updatedFocalPoint = details.focalPoint;
24 25
    };

26 27 28
    double? updatedScale;
    double? updatedHorizontalScale;
    double? updatedVerticalScale;
29
    Offset? updatedDelta;
30 31
    scale.onUpdate = (ScaleUpdateDetails details) {
      updatedScale = details.scale;
32 33
      updatedHorizontalScale = details.horizontalScale;
      updatedVerticalScale = details.verticalScale;
34
      updatedFocalPoint = details.focalPoint;
35
      updatedDelta = details.focalPointDelta;
36 37 38
    };

    bool didEndScale = false;
39
    scale.onEnd = (ScaleEndDetails details) {
40 41 42 43 44 45 46 47
      didEndScale = true;
    };

    bool didTap = false;
    tap.onTap = () {
      didTap = true;
    };

48
    final TestPointer pointer1 = TestPointer();
49

50
    final PointerDownEvent down = pointer1.down(Offset.zero);
51 52 53
    scale.addPointer(down);
    tap.addPointer(down);

54
    tester.closeArena(1);
55 56 57
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
58
    expect(updatedDelta, isNull);
59 60 61 62
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // One-finger panning
63
    tester.route(down);
64 65 66
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
67
    expect(updatedDelta, isNull);
68 69 70
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

71
    tester.route(pointer1.move(const Offset(20.0, 30.0)));
72 73
    expect(didStartScale, isTrue);
    didStartScale = false;
74
    expect(updatedFocalPoint, const Offset(20.0, 30.0));
75 76 77
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    updatedScale = null;
78 79
    expect(updatedDelta, const Offset(20.0, 30.0));
    updatedDelta = null;
80 81
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);
82
    expect(scale.pointerCount, 1);
83 84

    // Two-finger scaling
85
    final TestPointer pointer2 = TestPointer(2);
86
    final PointerDownEvent down2 = pointer2.down(const Offset(10.0, 20.0));
87 88
    scale.addPointer(down2);
    tap.addPointer(down2);
89 90
    tester.closeArena(2);
    tester.route(down2);
91
    expect(scale.pointerCount, 2);
92 93 94 95 96

    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
97
    expect(updatedDelta, isNull);
98 99 100
    expect(didStartScale, isFalse);

    // Zoom in
101
    tester.route(pointer2.move(const Offset(0.0, 10.0)));
102 103
    expect(didStartScale, isTrue);
    didStartScale = false;
104
    expect(updatedFocalPoint, const Offset(10.0, 20.0));
105 106
    updatedFocalPoint = null;
    expect(updatedScale, 2.0);
107 108
    expect(updatedHorizontalScale, 2.0);
    expect(updatedVerticalScale, 2.0);
109
    expect(updatedDelta, const Offset(-5.0, -5.0));
110
    updatedScale = null;
111 112
    updatedHorizontalScale = null;
    updatedVerticalScale = null;
113
    updatedDelta = null;
114 115 116 117
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // Zoom out
118 119
    tester.route(pointer2.move(const Offset(15.0, 25.0)));
    expect(updatedFocalPoint, const Offset(17.5, 27.5));
120
    expect(updatedScale, 0.5);
121 122
    expect(updatedHorizontalScale, 0.5);
    expect(updatedVerticalScale, 0.5);
123
    expect(updatedDelta, const Offset(7.5, 7.5));
124 125
    expect(didTap, isFalse);

126 127 128 129 130 131 132 133 134
    // Horizontal scaling
    tester.route(pointer2.move(const Offset(0.0, 20.0)));
    expect(updatedHorizontalScale, 2.0);
    expect(updatedVerticalScale, 1.0);

    // Vertical scaling
    tester.route(pointer2.move(const Offset(10.0, 10.0)));
    expect(updatedHorizontalScale, 1.0);
    expect(updatedVerticalScale, 2.0);
135
    expect(updatedDelta, const Offset(5.0, -5.0));
136 137 138
    tester.route(pointer2.move(const Offset(15.0, 25.0)));
    updatedFocalPoint = null;
    updatedScale = null;
139
    updatedDelta = null;
140

141
    // Three-finger scaling
142
    final TestPointer pointer3 = TestPointer(3);
143
    final PointerDownEvent down3 = pointer3.down(const Offset(25.0, 35.0));
144 145
    scale.addPointer(down3);
    tap.addPointer(down3);
146 147
    tester.closeArena(3);
    tester.route(down3);
148 149 150 151 152

    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
153
    expect(updatedDelta, isNull);
154 155 156
    expect(didStartScale, isFalse);

    // Zoom in
157
    tester.route(pointer3.move(const Offset(55.0, 65.0)));
158 159
    expect(didStartScale, isTrue);
    didStartScale = false;
160
    expect(updatedFocalPoint, const Offset(30.0, 40.0));
161 162 163
    updatedFocalPoint = null;
    expect(updatedScale, 5.0);
    updatedScale = null;
164 165
    expect(updatedDelta, const Offset(10.0, 10.0));
    updatedDelta = null;
166 167 168 169
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // Return to original positions but with different fingers
170 171 172
    tester.route(pointer1.move(const Offset(25.0, 35.0)));
    tester.route(pointer2.move(const Offset(20.0, 30.0)));
    tester.route(pointer3.move(const Offset(15.0, 25.0)));
173
    expect(didStartScale, isFalse);
174
    expect(updatedFocalPoint, const Offset(20.0, 30.0));
175 176 177
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    updatedScale = null;
178 179
    expect(updatedDelta!.dx, closeTo(-13.3, 0.1));
    expect(updatedDelta!.dy, closeTo(-13.3, 0.1));
180
    updatedDelta = null;
181 182 183
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

184
    tester.route(pointer1.up());
185 186 187
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
188
    expect(updatedDelta, isNull);
189 190 191 192 193
    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(didTap, isFalse);

    // Continue scaling with two fingers
194
    tester.route(pointer3.move(const Offset(10.0, 20.0)));
195 196
    expect(didStartScale, isTrue);
    didStartScale = false;
197
    expect(updatedFocalPoint, const Offset(15.0, 25.0));
198 199 200
    updatedFocalPoint = null;
    expect(updatedScale, 2.0);
    updatedScale = null;
201 202
    expect(updatedDelta, const Offset(-2.5, -2.5));
    updatedDelta = null;
203

204 205 206 207 208 209
    // Continue rotating with two fingers
    tester.route(pointer3.move(const Offset(30.0, 40.0)));
    expect(updatedFocalPoint, const Offset(25.0, 35.0));
    updatedFocalPoint = null;
    expect(updatedScale, 2.0);
    updatedScale = null;
210 211
    expect(updatedDelta, const Offset(10.0, 10.0));
    updatedDelta = null;
212 213 214 215 216
    tester.route(pointer3.move(const Offset(10.0, 20.0)));
    expect(updatedFocalPoint, const Offset(15.0, 25.0));
    updatedFocalPoint = null;
    expect(updatedScale, 2.0);
    updatedScale = null;
217
    expect(updatedDelta, const Offset(-10.0, -10.0));
218
    updatedDelta = null;
219

220
    tester.route(pointer2.up());
221 222 223
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
224
    expect(updatedDelta, isNull);
225 226 227 228 229
    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(didTap, isFalse);

    // Continue panning with one finger
230
    tester.route(pointer3.move(Offset.zero));
231 232
    expect(didStartScale, isTrue);
    didStartScale = false;
233
    expect(updatedFocalPoint, Offset.zero);
234 235 236
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    updatedScale = null;
237 238
    expect(updatedDelta, const Offset(-10.0, -20.0));
    updatedDelta = null;
239 240

    // We are done
241
    tester.route(pointer3.up());
242 243 244
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
245
    expect(updatedDelta, isNull);
246 247 248 249 250 251 252
    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(didTap, isFalse);

    scale.dispose();
    tap.dispose();
  });
253

254
  testGesture('Rejects scale gestures from unallowed device kinds', (GestureTester tester) {
255 256 257
    final ScaleGestureRecognizer scale = ScaleGestureRecognizer(
      supportedDevices: <PointerDeviceKind>{ PointerDeviceKind.touch },
    );
258 259 260 261 262 263

    bool didStartScale = false;
    scale.onStart = (ScaleStartDetails details) {
      didStartScale = true;
    };

264
    double? updatedScale;
265 266 267 268 269 270
    scale.onUpdate = (ScaleUpdateDetails details) {
      updatedScale = details.scale;
    };

    final TestPointer mousePointer = TestPointer(1, PointerDeviceKind.mouse);

271
    final PointerDownEvent down = mousePointer.down(Offset.zero);
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    scale.addPointer(down);
    tester.closeArena(1);

    // One-finger panning
    tester.route(down);
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);

    // Using a mouse, the scale gesture shouldn't even start.
    tester.route(mousePointer.move(const Offset(20.0, 30.0)));
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);

    scale.dispose();
  });

  testGesture('Scale gestures starting from allowed device kinds cannot be ended from unallowed devices', (GestureTester tester) {
289 290 291
    final ScaleGestureRecognizer scale = ScaleGestureRecognizer(
      supportedDevices: <PointerDeviceKind>{ PointerDeviceKind.touch },
    );
292 293

    bool didStartScale = false;
294
    Offset? updatedFocalPoint;
295 296 297 298 299
    scale.onStart = (ScaleStartDetails details) {
      didStartScale = true;
      updatedFocalPoint = details.focalPoint;
    };

300
    double? updatedScale;
301 302 303 304 305 306 307 308 309 310
    scale.onUpdate = (ScaleUpdateDetails details) {
      updatedScale = details.scale;
      updatedFocalPoint = details.focalPoint;
    };

    bool didEndScale = false;
    scale.onEnd = (ScaleEndDetails details) {
      didEndScale = true;
    };

311
    final TestPointer touchPointer = TestPointer();
312

313
    final PointerDownEvent down = touchPointer.down(Offset.zero);
314 315 316 317 318 319 320 321
    scale.addPointer(down);
    tester.closeArena(1);

    // One-finger panning
    tester.route(down);
    expect(didStartScale, isTrue);
    didStartScale = false;
    expect(updatedScale, isNull);
322
    expect(updatedFocalPoint, Offset.zero);
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
    expect(didEndScale, isFalse);

    // The gesture can start using one touch finger.
    tester.route(touchPointer.move(const Offset(20.0, 30.0)));
    expect(updatedFocalPoint, const Offset(20.0, 30.0));
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    updatedScale = null;
    expect(didEndScale, isFalse);

    // Two-finger scaling
    final TestPointer mousePointer = TestPointer(2, PointerDeviceKind.mouse);
    final PointerDownEvent down2 = mousePointer.down(const Offset(10.0, 20.0));
    scale.addPointer(down2);
    tester.closeArena(2);
    tester.route(down2);

    // Mouse-generated events are ignored.
    expect(didEndScale, isFalse);
    expect(updatedScale, isNull);
    expect(didStartScale, isFalse);

    // Zoom in using a mouse doesn't work either.
    tester.route(mousePointer.move(const Offset(0.0, 10.0)));
    expect(updatedScale, isNull);
    expect(didEndScale, isFalse);

    scale.dispose();
  });

353
  testGesture('Scale gesture competes with drag', (GestureTester tester) {
354 355
    final ScaleGestureRecognizer scale = ScaleGestureRecognizer();
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer();
356 357 358 359 360 361 362 363 364 365

    final List<String> log = <String>[];

    scale.onStart = (ScaleStartDetails details) { log.add('scale-start'); };
    scale.onUpdate = (ScaleUpdateDetails details) { log.add('scale-update'); };
    scale.onEnd = (ScaleEndDetails details) { log.add('scale-end'); };

    drag.onStart = (DragStartDetails details) { log.add('drag-start'); };
    drag.onEnd = (DragEndDetails details) { log.add('drag-end'); };

366
    final TestPointer pointer1 = TestPointer();
367

368
    final PointerDownEvent down = pointer1.down(const Offset(10.0, 10.0));
369 370 371 372 373 374 375 376 377 378
    scale.addPointer(down);
    drag.addPointer(down);

    tester.closeArena(1);
    expect(log, isEmpty);

    // Vertical moves are scales.
    tester.route(down);
    expect(log, isEmpty);

379
    // Scale will win if focal point delta exceeds 18.0*2.
380

381
    tester.route(pointer1.move(const Offset(10.0, 50.0))); // Delta of 40.0 exceeds 18.0*2.
382 383 384
    expect(log, equals(<String>['scale-start', 'scale-update']));
    log.clear();

385
    final TestPointer pointer2 = TestPointer(2);
386
    final PointerDownEvent down2 = pointer2.down(const Offset(10.0, 20.0));
387 388 389 390 391 392 393 394 395 396 397
    scale.addPointer(down2);
    drag.addPointer(down2);

    tester.closeArena(2);
    expect(log, isEmpty);

    // Second pointer joins scale even though it moves horizontally.
    tester.route(down2);
    expect(log, <String>['scale-end']);
    log.clear();

398
    tester.route(pointer2.move(const Offset(30.0, 20.0)));
399 400 401 402 403 404 405 406 407 408 409
    expect(log, equals(<String>['scale-start', 'scale-update']));
    log.clear();

    tester.route(pointer1.up());
    expect(log, equals(<String>['scale-end']));
    log.clear();

    tester.route(pointer2.up());
    expect(log, isEmpty);
    log.clear();

410 411 412 413
    // Horizontal moves are either drags or scales, depending on which wins first.
    // TODO(ianh): https://github.com/flutter/flutter/issues/11384
    // In this case, we move fast, so that the scale wins. If we moved slowly,
    // the horizontal drag would win, since it was added first.
414
    final TestPointer pointer3 = TestPointer(3);
415
    final PointerDownEvent down3 = pointer3.down(const Offset(30.0, 30.0));
416 417 418 419 420 421 422
    scale.addPointer(down3);
    drag.addPointer(down3);
    tester.closeArena(3);
    tester.route(down3);

    expect(log, isEmpty);

423
    tester.route(pointer3.move(const Offset(100.0, 30.0)));
424 425 426 427 428 429 430 431 432 433
    expect(log, equals(<String>['scale-start', 'scale-update']));
    log.clear();

    tester.route(pointer3.up());
    expect(log, equals(<String>['scale-end']));
    log.clear();

    scale.dispose();
    drag.dispose();
  });
434 435 436 437 438 439

  testGesture('Should recognize rotation gestures', (GestureTester tester) {
    final ScaleGestureRecognizer scale = ScaleGestureRecognizer();
    final TapGestureRecognizer tap = TapGestureRecognizer();

    bool didStartScale = false;
440
    Offset? updatedFocalPoint;
441 442 443 444 445
    scale.onStart = (ScaleStartDetails details) {
      didStartScale = true;
      updatedFocalPoint = details.focalPoint;
    };

446
    double? updatedRotation;
447
    Offset? updatedDelta;
448 449 450
    scale.onUpdate = (ScaleUpdateDetails details) {
      updatedRotation = details.rotation;
      updatedFocalPoint = details.focalPoint;
451
      updatedDelta = details.focalPointDelta;
452 453 454 455 456 457 458 459 460 461 462 463
    };

    bool didEndScale = false;
    scale.onEnd = (ScaleEndDetails details) {
      didEndScale = true;
    };

    bool didTap = false;
    tap.onTap = () {
      didTap = true;
    };

464
    final TestPointer pointer1 = TestPointer();
465

466
    final PointerDownEvent down = pointer1.down(Offset.zero);
467 468 469 470 471 472 473
    scale.addPointer(down);
    tap.addPointer(down);

    tester.closeArena(1);
    expect(didStartScale, isFalse);
    expect(updatedRotation, isNull);
    expect(updatedFocalPoint, isNull);
474
    expect(updatedDelta, isNull);
475 476 477 478 479 480 481 482 483 484
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    tester.route(down);
    tester.route(pointer1.move(const Offset(20.0, 30.0)));
    expect(didStartScale, isTrue);
    didStartScale = false;

    expect(updatedFocalPoint, const Offset(20.0, 30.0));
    updatedFocalPoint = null;
485 486
    expect(updatedDelta, const Offset(20.0, 30.0));
    updatedDelta = null;
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
    expect(updatedRotation, 0.0);
    updatedRotation = null;
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // Two-finger scaling
    final TestPointer pointer2 = TestPointer(2);
    final PointerDownEvent down2 = pointer2.down(const Offset(30.0, 40.0));
    scale.addPointer(down2);
    tap.addPointer(down2);
    tester.closeArena(2);
    tester.route(down2);

    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(updatedFocalPoint, isNull);
503
    expect(updatedDelta, isNull);
504 505 506 507 508 509 510 511 512
    expect(updatedRotation, isNull);
    expect(didStartScale, isFalse);

    // Zoom in
    tester.route(pointer2.move(const Offset(40.0, 50.0)));
    expect(didStartScale, isTrue);
    didStartScale = false;
    expect(updatedFocalPoint, const Offset(30.0, 40.0));
    updatedFocalPoint = null;
513 514
    expect(updatedDelta, const Offset(5.0, 5.0));
    updatedDelta = null;
515 516 517 518 519 520 521 522 523
    expect(updatedRotation, 0.0);
    updatedRotation = null;
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // Rotation
    tester.route(pointer2.move(const Offset(0.0, 10.0)));
    expect(updatedFocalPoint, const Offset(10.0, 20.0));
    updatedFocalPoint = null;
524
    expect(updatedDelta, const Offset(-20.0, -20.0));
525
    updatedDelta = null;
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
    expect(updatedRotation, math.pi);
    updatedRotation = null;
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // Three-finger scaling
    final TestPointer pointer3 = TestPointer(3);
    final PointerDownEvent down3 = pointer3.down(const Offset(25.0, 35.0));
    scale.addPointer(down3);
    tap.addPointer(down3);
    tester.closeArena(3);
    tester.route(down3);

    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(updatedFocalPoint, isNull);
542
    expect(updatedDelta, isNull);
543 544 545 546 547 548 549 550 551
    expect(updatedRotation, isNull);
    expect(didStartScale, isFalse);

    // Zoom in
    tester.route(pointer3.move(const Offset(55.0, 65.0)));
    expect(didStartScale, isTrue);
    didStartScale = false;
    expect(updatedFocalPoint, const Offset(25.0, 35.0));
    updatedFocalPoint = null;
552 553
    expect(updatedDelta, const Offset(10.0, 10.0));
    updatedDelta = null;
554 555 556 557 558 559 560 561 562 563 564 565
    expect(updatedRotation, 0.0);
    updatedRotation = null;
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // Return to original positions but with different fingers
    tester.route(pointer1.move(const Offset(25.0, 35.0)));
    tester.route(pointer2.move(const Offset(20.0, 30.0)));
    tester.route(pointer3.move(const Offset(15.0, 25.0)));
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, const Offset(20.0, 30.0));
    updatedFocalPoint = null;
566 567
    expect(updatedDelta!.dx, closeTo(-13.3, 0.1));
    expect(updatedDelta!.dy, closeTo(-13.3, 0.1));
568
    updatedDelta = null;
569 570 571 572 573 574 575 576
    expect(updatedRotation, 0.0);
    updatedRotation = null;
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    tester.route(pointer1.up());
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
577
    expect(updatedDelta, isNull);
578 579 580 581 582 583 584 585 586 587 588
    expect(updatedRotation, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(didTap, isFalse);

    // Continue scaling with two fingers
    tester.route(pointer3.move(const Offset(10.0, 20.0)));
    expect(didStartScale, isTrue);
    didStartScale = false;
    expect(updatedFocalPoint, const Offset(15.0, 25.0));
    updatedFocalPoint = null;
589 590
    expect(updatedDelta, const Offset(-2.5, -2.5));
    updatedDelta = null;
591 592 593 594 595 596 597
    expect(updatedRotation, 0.0);
    updatedRotation = null;

    // Continue rotating with two fingers
    tester.route(pointer3.move(const Offset(30.0, 40.0)));
    expect(updatedFocalPoint, const Offset(25.0, 35.0));
    updatedFocalPoint = null;
598
    expect(updatedDelta, const Offset(10.0, 10.0));
599
    updatedDelta = null;
600 601 602 603 604
    expect(updatedRotation, - math.pi);
    updatedRotation = null;
    tester.route(pointer3.move(const Offset(10.0, 20.0)));
    expect(updatedFocalPoint, const Offset(15.0, 25.0));
    updatedFocalPoint = null;
605
    expect(updatedDelta, const Offset(-10.0, -10.0));
606
    updatedDelta = null;
607 608 609 610 611 612
    expect(updatedRotation, 0.0);
    updatedRotation = null;

    tester.route(pointer2.up());
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
613
    expect(updatedDelta, isNull);
614 615 616 617 618 619 620 621 622
    expect(updatedRotation, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(didTap, isFalse);

    // We are done
    tester.route(pointer3.up());
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
623
    expect(updatedDelta, isNull);
624 625 626 627 628 629 630 631
    expect(updatedRotation, isNull);
    expect(didEndScale, isFalse);
    didEndScale = false;
    expect(didTap, isFalse);

    scale.dispose();
    tap.dispose();
  });
632

633 634 635 636 637 638 639 640 641
  // Regressing test for https://github.com/flutter/flutter/issues/78941
  testGesture('First rotation test', (GestureTester tester) {
    final ScaleGestureRecognizer scale = ScaleGestureRecognizer();

    double? updatedRotation;
    scale.onUpdate = (ScaleUpdateDetails details) {
      updatedRotation = details.rotation;
    };

642
    final TestPointer pointer1 = TestPointer();
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
    final PointerDownEvent down = pointer1.down(Offset.zero);
    scale.addPointer(down);
    tester.closeArena(1);
    tester.route(down);

    final TestPointer pointer2 = TestPointer(2);
    final PointerDownEvent down2 = pointer2.down(const Offset(10.0, 10.0));
    scale.addPointer(down2);
    tester.closeArena(2);
    tester.route(down2);

    expect(updatedRotation, isNull);

    // Rotation 45°.
    tester.route(pointer2.move(const Offset(0.0, 10.0)));
    expect(updatedRotation, math.pi / 4.0);
  });

661 662 663 664 665 666 667 668 669 670 671 672
  testGesture('Scale gestures pointer count test', (GestureTester tester) {
    final ScaleGestureRecognizer scale = ScaleGestureRecognizer();

    int pointerCountOfStart = 0;
    scale.onStart = (ScaleStartDetails details) => pointerCountOfStart = details.pointerCount;

    int pointerCountOfUpdate = 0;
    scale.onUpdate = (ScaleUpdateDetails details) => pointerCountOfUpdate = details.pointerCount;

    int pointerCountOfEnd = 0;
    scale.onEnd = (ScaleEndDetails details) => pointerCountOfEnd = details.pointerCount;

673
    final TestPointer pointer1 = TestPointer();
674
    final PointerDownEvent down = pointer1.down(Offset.zero);
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
    scale.addPointer(down);
    tester.closeArena(1);

    // One-finger panning
    tester.route(down);
    // One pointer in contact with the screen now.
    expect(pointerCountOfStart, 1);
    tester.route(pointer1.move(const Offset(20.0, 30.0)));
    expect(pointerCountOfUpdate, 1);

    // Two-finger scaling
    final TestPointer pointer2 = TestPointer(2);
    final PointerDownEvent down2 = pointer2.down(const Offset(10.0, 20.0));
    scale.addPointer(down2);
    tester.closeArena(2);
    tester.route(down2);
    // Two pointers in contact with the screen now.
    expect(pointerCountOfEnd, 2); // Additional pointer down will trigger an end event.

    tester.route(pointer2.move(const Offset(0.0, 10.0)));
    expect(pointerCountOfStart, 2); // The new pointer move will trigger a start event.
    expect(pointerCountOfUpdate, 2);

    tester.route(pointer1.up());
    // One pointer in contact with the screen now.
    expect(pointerCountOfEnd, 1);

    tester.route(pointer2.move(const Offset(0.0, 10.0)));
    expect(pointerCountOfStart, 1);
    expect(pointerCountOfUpdate, 1);

    tester.route(pointer2.up());
    // No pointer in contact with the screen now.
    expect(pointerCountOfEnd, 0);

    scale.dispose();
  });
712

713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
  testGesture('Should recognize scale gestures from pointer pan/zoom events', (GestureTester tester) {
    final ScaleGestureRecognizer scale = ScaleGestureRecognizer();
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer();

    bool didStartScale = false;
    Offset? updatedFocalPoint;
    scale.onStart = (ScaleStartDetails details) {
      didStartScale = true;
      updatedFocalPoint = details.focalPoint;
    };

    double? updatedScale;
    double? updatedHorizontalScale;
    double? updatedVerticalScale;
    Offset? updatedDelta;
    scale.onUpdate = (ScaleUpdateDetails details) {
      updatedScale = details.scale;
      updatedHorizontalScale = details.horizontalScale;
      updatedVerticalScale = details.verticalScale;
      updatedFocalPoint = details.focalPoint;
      updatedDelta = details.focalPointDelta;
    };

    bool didEndScale = false;
    scale.onEnd = (ScaleEndDetails details) {
      didEndScale = true;
    };

741
    final TestPointer pointer1 = TestPointer(2, PointerDeviceKind.trackpad);
742 743 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 807 808 809 810 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

    final PointerPanZoomStartEvent start = pointer1.panZoomStart(Offset.zero);
    scale.addPointerPanZoom(start);
    drag.addPointerPanZoom(start);

    tester.closeArena(2);
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(updatedDelta, isNull);
    expect(didEndScale, isFalse);

    // Panning.
    tester.route(start);
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(updatedDelta, isNull);
    expect(didEndScale, isFalse);

    tester.route(pointer1.panZoomUpdate(Offset.zero, pan: const Offset(20.0, 30.0)));
    expect(didStartScale, isTrue);
    didStartScale = false;
    expect(updatedFocalPoint, const Offset(20.0, 30.0));
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    updatedScale = null;
    expect(updatedDelta, const Offset(20.0, 30.0));
    updatedDelta = null;
    expect(didEndScale, isFalse);

    // Zoom in.
    tester.route(pointer1.panZoomUpdate(Offset.zero, pan: const Offset(20.0, 30.0), scale: 2.0));
    expect(updatedFocalPoint, const Offset(20.0, 30.0));
    updatedFocalPoint = null;
    expect(updatedScale, 2.0);
    expect(updatedHorizontalScale, 2.0);
    expect(updatedVerticalScale, 2.0);
    expect(updatedDelta, Offset.zero);
    updatedScale = null;
    updatedHorizontalScale = null;
    updatedVerticalScale = null;
    updatedDelta = null;
    expect(didEndScale, isFalse);

    // Zoom out.
    tester.route(pointer1.panZoomUpdate(Offset.zero, pan: const Offset(20.0, 30.0)));
    expect(updatedFocalPoint, const Offset(20.0, 30.0));
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    expect(updatedHorizontalScale, 1.0);
    expect(updatedVerticalScale, 1.0);
    expect(updatedDelta, Offset.zero);
    updatedScale = null;
    updatedHorizontalScale = null;
    updatedVerticalScale = null;
    updatedDelta = null;
    expect(didEndScale, isFalse);

    // We are done.
    tester.route(pointer1.panZoomEnd());
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
    expect(updatedDelta, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;

    scale.dispose();
  });

  testGesture('Pointer pan/zooms should work alongside touches', (GestureTester tester) {
    final ScaleGestureRecognizer scale = ScaleGestureRecognizer();
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer();

    bool didStartScale = false;
    Offset? updatedFocalPoint;
    scale.onStart = (ScaleStartDetails details) {
      didStartScale = true;
      updatedFocalPoint = details.focalPoint;
    };

    double? updatedScale;
    double? updatedHorizontalScale;
    double? updatedVerticalScale;
    Offset? updatedDelta;
    double? updatedRotation;
    scale.onUpdate = (ScaleUpdateDetails details) {
      updatedScale = details.scale;
      updatedHorizontalScale = details.horizontalScale;
      updatedVerticalScale = details.verticalScale;
      updatedFocalPoint = details.focalPoint;
      updatedDelta = details.focalPointDelta;
      updatedRotation = details.rotation;
    };

    bool didEndScale = false;
    scale.onEnd = (ScaleEndDetails details) {
      didEndScale = true;
    };

    final TestPointer touchPointer1 = TestPointer(2);
    final TestPointer touchPointer2 = TestPointer(3);
845
    final TestPointer panZoomPointer = TestPointer(4, PointerDeviceKind.trackpad);
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 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993

    final PointerPanZoomStartEvent panZoomStart = panZoomPointer.panZoomStart(Offset.zero);
    scale.addPointerPanZoom(panZoomStart);
    drag.addPointerPanZoom(panZoomStart);

    tester.closeArena(4);
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(updatedDelta, isNull);
    expect(didEndScale, isFalse);

    // Panning starting with trackpad.
    tester.route(panZoomStart);
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(updatedDelta, isNull);
    expect(didEndScale, isFalse);

    tester.route(panZoomPointer.panZoomUpdate(Offset.zero, pan: const Offset(40, 40)));
    expect(didStartScale, isTrue);
    didStartScale = false;
    expect(updatedFocalPoint, const Offset(40.0, 40.0));
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    updatedScale = null;
    expect(updatedDelta, const Offset(40.0, 40.0));
    updatedDelta = null;
    expect(didEndScale, isFalse);

    // Add a touch pointer.
    final PointerDownEvent touchStart1 = touchPointer1.down(const Offset(40, 40));
    scale.addPointer(touchStart1);
    drag.addPointer(touchStart1);
    tester.closeArena(2);
    tester.route(touchStart1);
    expect(didEndScale, isTrue);
    didEndScale = false;

    tester.route(touchPointer1.move(const Offset(10, 10)));
    expect(didStartScale, isTrue);
    didStartScale = false;
    expect(updatedFocalPoint, const Offset(25, 25));
    updatedFocalPoint = null;
    // 1 down pointer + pointer pan/zoom should not scale, only pan.
    expect(updatedScale, 1.0);
    updatedScale = null;
    expect(updatedDelta, const Offset(-15, -15));
    updatedDelta = null;
    expect(didEndScale, isFalse);

    // Add a second touch pointer.
    final PointerDownEvent touchStart2 = touchPointer2.down(const Offset(10, 40));
    scale.addPointer(touchStart2);
    drag.addPointer(touchStart2);
    tester.closeArena(3);
    tester.route(touchStart2);
    expect(didEndScale, isTrue);
    didEndScale = false;

    // Move the second pointer to cause pan, zoom, and rotation.
    tester.route(touchPointer2.move(const Offset(40, 40)));
    expect(didStartScale, isTrue);
    didStartScale = false;
    expect(updatedFocalPoint, const Offset(30, 30));
    updatedFocalPoint = null;
    expect(updatedScale, math.sqrt(2));
    updatedScale = null;
    expect(updatedHorizontalScale, 1.0);
    updatedHorizontalScale = null;
    expect(updatedVerticalScale, 1.0);
    updatedVerticalScale = null;
    expect(updatedDelta, const Offset(10, 0));
    updatedDelta = null;
    expect(updatedRotation, -math.pi / 4);
    updatedRotation = null;
    expect(didEndScale, isFalse);

    // Change the scale and angle of the pan/zoom to test combining.
    // Scale should be multiplied together.
    // Rotation angle should be added together.
    tester.route(panZoomPointer.panZoomUpdate(Offset.zero, pan: const Offset(40, 40), scale: math.sqrt(2), rotation: math.pi / 3));
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, const Offset(30, 30));
    updatedFocalPoint = null;
    expect(updatedScale, closeTo(2, 0.0001));
    updatedScale = null;
    expect(updatedHorizontalScale, math.sqrt(2));
    updatedHorizontalScale = null;
    expect(updatedVerticalScale, math.sqrt(2));
    updatedVerticalScale = null;
    expect(updatedDelta, Offset.zero);
    updatedDelta = null;
    expect(updatedRotation, closeTo(math.pi / 12, 0.0001));
    updatedRotation = null;
    expect(didEndScale, isFalse);

    // Move the pan/zoom origin to test combining.
    tester.route(panZoomPointer.panZoomUpdate(const Offset(15, 15), pan: const Offset(55, 55), scale: math.sqrt(2), rotation: math.pi / 3));
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, const Offset(40, 40));
    updatedFocalPoint = null;
    expect(updatedScale, closeTo(2, 0.0001));
    updatedScale = null;
    expect(updatedDelta, const Offset(10, 10));
    updatedDelta = null;
    expect(updatedRotation, closeTo(math.pi / 12, 0.0001));
    updatedRotation = null;
    expect(didEndScale, isFalse);

    // We are done.
    tester.route(panZoomPointer.panZoomEnd());
    expect(updatedFocalPoint, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(updatedScale, isNull);
    expect(updatedDelta, isNull);
    expect(didStartScale, isFalse);
    tester.route(touchPointer1.up());
    expect(updatedFocalPoint, isNull);
    expect(didEndScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedDelta, isNull);
    expect(didStartScale, isFalse);
    tester.route(touchPointer2.up());
    expect(didEndScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
    expect(updatedDelta, isNull);
    expect(didStartScale, isFalse);

    scale.dispose();
  });

  testGesture('Scale gesture competes with drag for trackpad gesture', (GestureTester tester) {
    final ScaleGestureRecognizer scale = ScaleGestureRecognizer();
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer();

    final List<String> log = <String>[];

    scale.onStart = (ScaleStartDetails details) { log.add('scale-start'); };
    scale.onUpdate = (ScaleUpdateDetails details) { log.add('scale-update'); };
    scale.onEnd = (ScaleEndDetails details) { log.add('scale-end'); };

    drag.onStart = (DragStartDetails details) { log.add('drag-start'); };
    drag.onEnd = (DragEndDetails details) { log.add('drag-end'); };

994
    final TestPointer pointer1 = TestPointer(2, PointerDeviceKind.trackpad);
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

    final PointerPanZoomStartEvent down = pointer1.panZoomStart(const Offset(10.0, 10.0));
    scale.addPointerPanZoom(down);
    drag.addPointerPanZoom(down);

    tester.closeArena(2);
    expect(log, isEmpty);

    // Vertical moves are scales.
    tester.route(down);
    expect(log, isEmpty);

    // Scale will win if focal point delta exceeds 18.0*2.

    tester.route(pointer1.panZoomUpdate(const Offset(10.0, 10.0), pan: const Offset(10.0, 40.0))); // delta of 40.0 exceeds 18.0*2.
    expect(log, equals(<String>['scale-start', 'scale-update']));
    log.clear();

1013
    final TestPointer pointer2 = TestPointer(3, PointerDeviceKind.trackpad);
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
    final PointerPanZoomStartEvent down2 = pointer2.panZoomStart(const Offset(10.0, 20.0));
    scale.addPointerPanZoom(down2);
    drag.addPointerPanZoom(down2);

    tester.closeArena(3);
    expect(log, isEmpty);

    // Second pointer joins scale even though it moves horizontally.
    tester.route(down2);
    expect(log, <String>['scale-end']);
    log.clear();

    tester.route(pointer2.panZoomUpdate(const Offset(10.0, 20.0), pan: const Offset(20.0, 0.0)));
    expect(log, equals(<String>['scale-start', 'scale-update']));
    log.clear();

    tester.route(pointer1.panZoomEnd());
    expect(log, equals(<String>['scale-end']));
    log.clear();

    tester.route(pointer2.panZoomEnd());
    expect(log, isEmpty);
    log.clear();

    // Horizontal moves are either drags or scales, depending on which wins first.
    // TODO(ianh): https://github.com/flutter/flutter/issues/11384
    // In this case, we move fast, so that the scale wins. If we moved slowly,
    // the horizontal drag would win, since it was added first.
1042
    final TestPointer pointer3 = TestPointer(4, PointerDeviceKind.trackpad);
1043 1044 1045 1046 1047 1048 1049 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 1089 1090 1091 1092
    final PointerPanZoomStartEvent down3 = pointer3.panZoomStart(const Offset(30.0, 30.0));
    scale.addPointerPanZoom(down3);
    drag.addPointerPanZoom(down3);
    tester.closeArena(4);
    tester.route(down3);

    expect(log, isEmpty);

    tester.route(pointer3.panZoomUpdate(const Offset(30.0, 30.0), pan: const Offset(70.0, 0.0)));
    expect(log, equals(<String>['scale-start', 'scale-update']));
    log.clear();

    tester.route(pointer3.panZoomEnd());
    expect(log, equals(<String>['scale-end']));
    log.clear();

    scale.dispose();
    drag.dispose();
  });

  testGesture('Scale gesture from pan/zoom events properly handles DragStartBehavior.start', (GestureTester tester) {
    final ScaleGestureRecognizer scale = ScaleGestureRecognizer(dragStartBehavior: DragStartBehavior.start);
    final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer();

    bool didStartScale = false;
    Offset? updatedFocalPoint;
    scale.onStart = (ScaleStartDetails details) {
      didStartScale = true;
      updatedFocalPoint = details.focalPoint;
    };

    double? updatedScale;
    double? updatedHorizontalScale;
    double? updatedVerticalScale;
    double? updatedRotation;
    Offset? updatedDelta;
    scale.onUpdate = (ScaleUpdateDetails details) {
      updatedScale = details.scale;
      updatedHorizontalScale = details.horizontalScale;
      updatedVerticalScale = details.verticalScale;
      updatedFocalPoint = details.focalPoint;
      updatedRotation = details.rotation;
      updatedDelta = details.focalPointDelta;
    };

    bool didEndScale = false;
    scale.onEnd = (ScaleEndDetails details) {
      didEndScale = true;
    };

1093
    final TestPointer pointer1 = TestPointer(2, PointerDeviceKind.trackpad);
1094 1095 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 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

    final PointerPanZoomStartEvent start = pointer1.panZoomStart(Offset.zero);
    scale.addPointerPanZoom(start);
    drag.addPointerPanZoom(start);

    tester.closeArena(2);
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(updatedDelta, isNull);
    expect(didEndScale, isFalse);

    tester.route(start);
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(updatedDelta, isNull);
    expect(didEndScale, isFalse);

    // Zoom enough to win the gesture.
    tester.route(pointer1.panZoomUpdate(Offset.zero, scale: 1.1, rotation: 1));
    expect(didStartScale, isTrue);
    didStartScale = false;
    expect(updatedFocalPoint, Offset.zero);
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    updatedScale = null;
    expect(updatedDelta, Offset.zero);
    updatedDelta = null;
    expect(didEndScale, isFalse);

    // Zoom in - should be relative to 1.1.
    tester.route(pointer1.panZoomUpdate(Offset.zero, scale: 1.21, rotation: 1.5));
    expect(updatedFocalPoint, Offset.zero);
    updatedFocalPoint = null;
    expect(updatedScale, closeTo(1.1, 0.0001));
    expect(updatedHorizontalScale, closeTo(1.1, 0.0001));
    expect(updatedVerticalScale, closeTo(1.1, 0.0001));
    expect(updatedRotation, 0.5);
    expect(updatedDelta, Offset.zero);
    updatedScale = null;
    updatedHorizontalScale = null;
    updatedVerticalScale = null;
    updatedRotation = null;
    updatedDelta = null;
    expect(didEndScale, isFalse);

    // Zoom out - should be relative to 1.1.
    tester.route(pointer1.panZoomUpdate(Offset.zero, scale: 0.99, rotation: 1.0));
    expect(updatedFocalPoint, Offset.zero);
    updatedFocalPoint = null;
    expect(updatedScale, closeTo(0.9, 0.0001));
    expect(updatedHorizontalScale, closeTo(0.9, 0.0001));
    expect(updatedVerticalScale, closeTo(0.9, 0.0001));
    expect(updatedRotation, 0.0);
    expect(updatedDelta, Offset.zero);
    updatedScale = null;
    updatedHorizontalScale = null;
    updatedVerticalScale = null;
    updatedDelta = null;
    expect(didEndScale, isFalse);

    // We are done.
    tester.route(pointer1.panZoomEnd());
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
    expect(updatedDelta, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;

    scale.dispose();
  });

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 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 1249 1250 1251 1252 1253 1254 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 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 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
  testGesture('scale trackpadScrollCausesScale', (GestureTester tester) {
    final ScaleGestureRecognizer scale = ScaleGestureRecognizer(
      dragStartBehavior: DragStartBehavior.start,
      trackpadScrollCausesScale: true
    );

    bool didStartScale = false;
    Offset? updatedFocalPoint;
    scale.onStart = (ScaleStartDetails details) {
      didStartScale = true;
      updatedFocalPoint = details.focalPoint;
    };

    double? updatedScale;
    Offset? updatedDelta;
    scale.onUpdate = (ScaleUpdateDetails details) {
      updatedScale = details.scale;
      updatedFocalPoint = details.focalPoint;
      updatedDelta = details.focalPointDelta;
    };

    bool didEndScale = false;
    scale.onEnd = (ScaleEndDetails details) {
      didEndScale = true;
    };

    final TestPointer pointer1 = TestPointer(2, PointerDeviceKind.trackpad);

    final PointerPanZoomStartEvent start = pointer1.panZoomStart(Offset.zero);
    scale.addPointerPanZoom(start);

    tester.closeArena(2);
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(updatedDelta, isNull);
    expect(didEndScale, isFalse);

    tester.route(start);
    expect(didStartScale, isTrue);
    didStartScale = false;
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, Offset.zero);
    updatedFocalPoint = null;
    expect(updatedDelta, isNull);
    expect(didEndScale, isFalse);

    // Zoom in by scrolling up.
    tester.route(pointer1.panZoomUpdate(Offset.zero, pan: const Offset(0, -200)));
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, Offset.zero);
    updatedFocalPoint = null;
    expect(updatedScale, math.e);
    updatedScale = null;
    expect(updatedDelta, Offset.zero);
    updatedDelta = null;
    expect(didEndScale, isFalse);

    // A horizontal scroll should do nothing.
    tester.route(pointer1.panZoomUpdate(Offset.zero, pan: const Offset(200, -200)));
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, Offset.zero);
    updatedFocalPoint = null;
    expect(updatedScale, math.e);
    updatedScale = null;
    expect(updatedDelta, Offset.zero);
    updatedDelta = null;
    expect(didEndScale, isFalse);

    // End.
    tester.route(pointer1.panZoomEnd());
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
    expect(updatedDelta, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;

    // Try with a different trackpadScrollToScaleFactor
    scale.trackpadScrollToScaleFactor = const Offset(1/125, 0);

    final PointerPanZoomStartEvent start2 = pointer1.panZoomStart(Offset.zero);
    scale.addPointerPanZoom(start2);

    tester.closeArena(2);
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(updatedDelta, isNull);
    expect(didEndScale, isFalse);

    tester.route(start2);
    expect(didStartScale, isTrue);
    didStartScale = false;
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, Offset.zero);
    updatedFocalPoint = null;
    expect(updatedDelta, isNull);
    expect(didEndScale, isFalse);

    // Zoom in by scrolling left.
    tester.route(pointer1.panZoomUpdate(Offset.zero, pan: const Offset(125, 0)));
    expect(didStartScale, isFalse);
    didStartScale = false;
    expect(updatedFocalPoint, Offset.zero);
    updatedFocalPoint = null;
    expect(updatedScale, math.e);
    updatedScale = null;
    expect(updatedDelta, Offset.zero);
    updatedDelta = null;
    expect(didEndScale, isFalse);

    // A vertical scroll should do nothing.
    tester.route(pointer1.panZoomUpdate(Offset.zero, pan: const Offset(125, 125)));
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, Offset.zero);
    updatedFocalPoint = null;
    expect(updatedScale, math.e);
    updatedScale = null;
    expect(updatedDelta, Offset.zero);
    updatedDelta = null;
    expect(didEndScale, isFalse);

    // End.
    tester.route(pointer1.panZoomEnd());
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
    expect(updatedDelta, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;

    scale.dispose();
  });

  testGesture('scale ending velocity', (GestureTester tester) {
    final ScaleGestureRecognizer scale = ScaleGestureRecognizer(
      dragStartBehavior: DragStartBehavior.start,
      trackpadScrollCausesScale: true
    );

    bool didStartScale = false;
    Offset? updatedFocalPoint;
    scale.onStart = (ScaleStartDetails details) {
      didStartScale = true;
      updatedFocalPoint = details.focalPoint;
    };

    bool didEndScale = false;
    double? scaleEndVelocity;
    scale.onEnd = (ScaleEndDetails details) {
      didEndScale = true;
      scaleEndVelocity = details.scaleVelocity;
    };

    final TestPointer pointer1 = TestPointer(2, PointerDeviceKind.trackpad);

    final PointerPanZoomStartEvent start = pointer1.panZoomStart(Offset.zero);
    scale.addPointerPanZoom(start);

    tester.closeArena(2);
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(didEndScale, isFalse);

    tester.route(start);
    expect(didStartScale, isTrue);
    didStartScale = false;
    expect(updatedFocalPoint, Offset.zero);
    updatedFocalPoint = null;
    expect(didEndScale, isFalse);

    // Zoom in by scrolling up.
    for (int i = 0; i < 100; i++) {
      tester.route(pointer1.panZoomUpdate(
        Offset.zero,
        pan: Offset(0, i * -10),
        timeStamp: Duration(milliseconds: i * 25)
      ));
    }

    // End.
    tester.route(pointer1.panZoomEnd(timeStamp: const Duration(milliseconds: 2500)));
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(scaleEndVelocity, moreOrLessEquals(281.41454098027765));

    scale.dispose();
  });
1359
}