draggable_test.dart 29.8 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
Adam Barth's avatar
Adam Barth committed
6
import 'package:flutter/material.dart';
Hixie's avatar
Hixie committed
7 8 9 10
import 'package:test/test.dart';

void main() {
  test('Drag and drop - control test', () {
11
    testWidgets((WidgetTester tester) {
12
      List<int> accepted = <int>[];
Hixie's avatar
Hixie committed
13

Adam Barth's avatar
Adam Barth committed
14
      tester.pumpWidget(new MaterialApp(
15 16
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) { return new Column(
17
            children: <Widget>[
18
              new Draggable<int>(
19 20 21 22
                data: 1,
                child: new Text('Source'),
                feedback: new Text('Dragging')
              ),
23 24
              new DragTarget<int>(
                builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
25 26 27 28 29
                  return new Container(
                    height: 100.0,
                    child: new Text('Target')
                  );
                },
30
                onAccept: (int data) {
31 32 33 34 35 36 37
                  accepted.add(data);
                }
              ),
            ]);
          },
        }
      ));
Hixie's avatar
Hixie committed
38

39 40 41 42
      expect(accepted, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNull);
      expect(tester.findText('Target'), isNotNull);
Hixie's avatar
Hixie committed
43

44
      Point firstLocation = tester.getCenter(tester.findText('Source'));
Adam Barth's avatar
Adam Barth committed
45
      TestGesture gesture = tester.startGesture(firstLocation, pointer: 7);
46
      tester.pump();
Hixie's avatar
Hixie committed
47

48 49 50 51
      expect(accepted, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNotNull);
      expect(tester.findText('Target'), isNotNull);
Hixie's avatar
Hixie committed
52

53
      Point secondLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
54
      gesture.moveTo(secondLocation);
55
      tester.pump();
Hixie's avatar
Hixie committed
56

57 58 59 60
      expect(accepted, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNotNull);
      expect(tester.findText('Target'), isNotNull);
Hixie's avatar
Hixie committed
61

Adam Barth's avatar
Adam Barth committed
62
      gesture.up();
63
      tester.pump();
Hixie's avatar
Hixie committed
64

65
      expect(accepted, equals(<int>[1]));
66 67 68 69
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNull);
      expect(tester.findText('Target'), isNotNull);
    });
Hixie's avatar
Hixie committed
70
  });
Hixie's avatar
Hixie committed
71 72 73 74 75 76 77

  test('Drag and drop - dragging over button', () {
    testWidgets((WidgetTester tester) {
      List<String> events = <String>[];
      Point firstLocation, secondLocation;

      tester.pumpWidget(new MaterialApp(
78 79
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) { return new Column(
Hixie's avatar
Hixie committed
80
            children: <Widget>[
81
              new Draggable<int>(
Hixie's avatar
Hixie committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
                data: 1,
                child: new Text('Source'),
                feedback: new Text('Dragging')
              ),
              new Stack(
                children: <Widget>[
                  new GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: () {
                      events.add('tap');
                    },
                    child: new Container(
                      child: new Text('Button')
                    )
                  ),
97 98
                  new DragTarget<int>(
                    builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
Hixie's avatar
Hixie committed
99 100 101 102 103 104
                      return new IgnorePointer(
                        child: new Container(
                          child: new Text('Target')
                        )
                      );
                    },
105
                    onAccept: (int data) {
Hixie's avatar
Hixie committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
                      events.add('drop');
                    }
                  ),
                ]
              ),
            ]);
          },
        }
      ));

      expect(events, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNull);
      expect(tester.findText('Target'), isNotNull);
      expect(tester.findText('Button'), isNotNull);

      // taps (we check both to make sure the test is consistent)

      expect(events, isEmpty);
      tester.tap(tester.findText('Button'));
      expect(events, equals(<String>['tap']));
      events.clear();

      expect(events, isEmpty);
      tester.tap(tester.findText('Target'));
      expect(events, equals(<String>['tap']));
      events.clear();

      // drag and drop

      firstLocation = tester.getCenter(tester.findText('Source'));
Adam Barth's avatar
Adam Barth committed
137
      TestGesture gesture = tester.startGesture(firstLocation, pointer: 7);
Hixie's avatar
Hixie committed
138 139 140
      tester.pump();

      secondLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
141
      gesture.moveTo(secondLocation);
Hixie's avatar
Hixie committed
142 143 144
      tester.pump();

      expect(events, isEmpty);
Adam Barth's avatar
Adam Barth committed
145
      gesture.up();
Hixie's avatar
Hixie committed
146 147 148 149 150 151 152
      tester.pump();
      expect(events, equals(<String>['drop']));
      events.clear();

      // drag and tap and drop

      firstLocation = tester.getCenter(tester.findText('Source'));
Adam Barth's avatar
Adam Barth committed
153
      gesture = tester.startGesture(firstLocation, pointer: 7);
Hixie's avatar
Hixie committed
154 155 156
      tester.pump();

      secondLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
157
      gesture.moveTo(secondLocation);
Hixie's avatar
Hixie committed
158 159 160 161 162
      tester.pump();

      expect(events, isEmpty);
      tester.tap(tester.findText('Button'));
      tester.tap(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
163
      gesture.up();
Hixie's avatar
Hixie committed
164 165 166
      tester.pump();
      expect(events, equals(<String>['tap', 'tap', 'drop']));
      events.clear();
167
    });
168
  });
169

170
  test('Drag and drop - tapping button', () {
171 172 173 174 175
    testWidgets((WidgetTester tester) {
      List<String> events = <String>[];
      Point firstLocation, secondLocation;

      tester.pumpWidget(new MaterialApp(
176 177
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) { return new Column(
178
            children: <Widget>[
179
              new Draggable<int>(
180 181 182 183 184 185 186 187 188 189 190 191
                data: 1,
                child: new GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    events.add('tap');
                  },
                  child: new Container(
                    child: new Text('Button')
                  )
                ),
                feedback: new Text('Dragging')
              ),
192 193
              new DragTarget<int>(
                builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
194 195
                  return new Text('Target');
                },
196
                onAccept: (int data) {
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
                  events.add('drop');
                }
              ),
            ]);
          },
        }
      ));

      expect(events, isEmpty);
      expect(tester.findText('Button'), isNotNull);
      expect(tester.findText('Target'), isNotNull);

      expect(events, isEmpty);
      tester.tap(tester.findText('Button'));
      expect(events, equals(<String>['tap']));
      events.clear();

      firstLocation = tester.getCenter(tester.findText('Button'));
Adam Barth's avatar
Adam Barth committed
215
      TestGesture gesture = tester.startGesture(firstLocation, pointer: 7);
216 217 218
      tester.pump();

      secondLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
219
      gesture.moveTo(secondLocation);
220 221 222
      tester.pump();

      expect(events, isEmpty);
Adam Barth's avatar
Adam Barth committed
223
      gesture.up();
224 225 226
      tester.pump();
      expect(events, equals(<String>['drop']));
      events.clear();
Hixie's avatar
Hixie committed
227 228

    });
229 230 231 232 233 234 235 236
  });

  test('Drag and drop - long press draggable, short press', () {
    testWidgets((WidgetTester tester) {
      List<String> events = <String>[];
      Point firstLocation, secondLocation;

      tester.pumpWidget(new MaterialApp(
237 238
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) { return new Column(
239
            children: <Widget>[
240
              new LongPressDraggable<int>(
241 242 243 244
                data: 1,
                child: new Text('Source'),
                feedback: new Text('Dragging')
              ),
245 246
              new DragTarget<int>(
                builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
247 248
                  return new Text('Target');
                },
249
                onAccept: (int data) {
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
                  events.add('drop');
                }
              ),
            ]);
          },
        }
      ));

      expect(events, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Target'), isNotNull);

      expect(events, isEmpty);
      tester.tap(tester.findText('Source'));
      expect(events, isEmpty);

      firstLocation = tester.getCenter(tester.findText('Source'));
Adam Barth's avatar
Adam Barth committed
267
      TestGesture gesture = tester.startGesture(firstLocation, pointer: 7);
268 269 270
      tester.pump();

      secondLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
271
      gesture.moveTo(secondLocation);
272 273 274
      tester.pump();

      expect(events, isEmpty);
Adam Barth's avatar
Adam Barth committed
275
      gesture.up();
276 277 278 279 280 281 282 283 284 285
      tester.pump();
      expect(events, isEmpty);

    });
  });

  test('Drag and drop - long press draggable, long press', () {
    testWidgets((WidgetTester tester) {
      List<String> events = <String>[];
      Point firstLocation, secondLocation;
286

287
      tester.pumpWidget(new MaterialApp(
288 289
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) { return new Column(
290
            children: <Widget>[
291
              new Draggable<int>(
292 293 294 295
                data: 1,
                child: new Text('Source'),
                feedback: new Text('Dragging')
              ),
296 297
              new DragTarget<int>(
                builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
298 299
                  return new Text('Target');
                },
300
                onAccept: (int data) {
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
                  events.add('drop');
                }
              ),
            ]);
          },
        }
      ));

      expect(events, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Target'), isNotNull);

      expect(events, isEmpty);
      tester.tap(tester.findText('Source'));
      expect(events, isEmpty);

      firstLocation = tester.getCenter(tester.findText('Source'));
Adam Barth's avatar
Adam Barth committed
318
      TestGesture gesture = tester.startGesture(firstLocation, pointer: 7);
319 320 321 322 323
      tester.pump();

      tester.pump(const Duration(seconds: 20));

      secondLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
324
      gesture.moveTo(secondLocation);
325 326 327
      tester.pump();

      expect(events, isEmpty);
Adam Barth's avatar
Adam Barth committed
328
      gesture.up();
329 330
      tester.pump();
      expect(events, equals(<String>['drop']));
331 332 333 334 335 336 337 338 339
    });
  });

  test('Drag and drop - horizontal and vertical draggables in vertical block', () {
    testWidgets((WidgetTester tester) {
      List<String> events = <String>[];
      Point firstLocation, secondLocation, thirdLocation;

      tester.pumpWidget(new MaterialApp(
340 341
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) {
342 343
            return new Block(
              children: <Widget>[
344 345
                new DragTarget<int>(
                  builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
346 347
                    return new Text('Target');
                  },
348
                  onAccept: (int data) {
349 350 351 352
                    events.add('drop $data');
                  }
                ),
                new Container(height: 400.0),
353
                new HorizontalDraggable<int>(
354 355 356 357
                  data: 1,
                  child: new Text('H'),
                  feedback: new Text('Dragging')
                ),
358
                new VerticalDraggable<int>(
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
                  data: 2,
                  child: new Text('V'),
                  feedback: new Text('Dragging')
                ),
                new Container(height: 500.0),
                new Container(height: 500.0),
                new Container(height: 500.0),
                new Container(height: 500.0),
              ]
            );
          },
        }
      ));

      expect(events, isEmpty);
      expect(tester.findText('Target'), isNotNull);
      expect(tester.findText('H'), isNotNull);
      expect(tester.findText('V'), isNotNull);

      // vertical draggable drags vertically
      expect(events, isEmpty);
      firstLocation = tester.getCenter(tester.findText('V'));
      secondLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
382
      TestGesture gesture = tester.startGesture(firstLocation, pointer: 7);
383
      tester.pump();
Adam Barth's avatar
Adam Barth committed
384
      gesture.moveTo(secondLocation);
385
      tester.pump();
Adam Barth's avatar
Adam Barth committed
386
      gesture.up();
387 388 389 390 391 392 393 394 395 396
      tester.pump();
      expect(events, equals(<String>['drop 2']));
      expect(tester.getCenter(tester.findText('Target')).y, greaterThan(0.0));
      events.clear();

      // horizontal draggable drags horizontally
      expect(events, isEmpty);
      firstLocation = tester.getTopLeft(tester.findText('H'));
      secondLocation = tester.getTopRight(tester.findText('H'));
      thirdLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
397
      gesture = tester.startGesture(firstLocation, pointer: 7);
398
      tester.pump();
Adam Barth's avatar
Adam Barth committed
399
      gesture.moveTo(secondLocation);
400
      tester.pump();
Adam Barth's avatar
Adam Barth committed
401
      gesture.moveTo(thirdLocation);
402
      tester.pump();
Adam Barth's avatar
Adam Barth committed
403
      gesture.up();
404 405 406 407 408 409 410 411 412 413 414
      tester.pump();
      expect(events, equals(<String>['drop 1']));
      expect(tester.getCenter(tester.findText('Target')).y, greaterThan(0.0));
      events.clear();

      // vertical draggable drags horizontally when there's no competition
      // from other gesture detectors
      expect(events, isEmpty);
      firstLocation = tester.getTopLeft(tester.findText('V'));
      secondLocation = tester.getTopRight(tester.findText('V'));
      thirdLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
415
      gesture = tester.startGesture(firstLocation, pointer: 7);
416
      tester.pump();
Adam Barth's avatar
Adam Barth committed
417
      gesture.moveTo(secondLocation);
418
      tester.pump();
Adam Barth's avatar
Adam Barth committed
419
      gesture.moveTo(thirdLocation);
420
      tester.pump();
Adam Barth's avatar
Adam Barth committed
421
      gesture.up();
422 423 424 425 426 427 428 429 430 431
      tester.pump();
      expect(events, equals(<String>['drop 2']));
      expect(tester.getCenter(tester.findText('Target')).y, greaterThan(0.0));
      events.clear();

      // horizontal draggable doesn't drag vertically when there is competition
      // for vertical gestures
      expect(events, isEmpty);
      firstLocation = tester.getCenter(tester.findText('H'));
      secondLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
432
      gesture = tester.startGesture(firstLocation, pointer: 7);
433
      tester.pump();
Adam Barth's avatar
Adam Barth committed
434
      gesture.moveTo(secondLocation);
435
      tester.pump(); // scrolls off screen!
Adam Barth's avatar
Adam Barth committed
436
      gesture.up();
437 438 439 440 441 442 443 444 445 446 447 448 449 450
      tester.pump();
      expect(events, equals(<String>[]));
      expect(tester.getCenter(tester.findText('Target')).y, lessThan(0.0));
      events.clear();

    });
  });

  test('Drag and drop - horizontal and vertical draggables in horizontal block', () {
    testWidgets((WidgetTester tester) {
      List<String> events = <String>[];
      Point firstLocation, secondLocation, thirdLocation;

      tester.pumpWidget(new MaterialApp(
451 452
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) {
453 454 455
            return new Block(
              scrollDirection: Axis.horizontal,
              children: <Widget>[
456 457
                new DragTarget<int>(
                  builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
458 459
                    return new Text('Target');
                  },
460
                  onAccept: (int data) {
461 462 463 464
                    events.add('drop $data');
                  }
                ),
                new Container(width: 400.0),
465
                new HorizontalDraggable<int>(
466 467 468 469
                  data: 1,
                  child: new Text('H'),
                  feedback: new Text('Dragging')
                ),
470
                new VerticalDraggable<int>(
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
                  data: 2,
                  child: new Text('V'),
                  feedback: new Text('Dragging')
                ),
                new Container(width: 500.0),
                new Container(width: 500.0),
                new Container(width: 500.0),
                new Container(width: 500.0),
              ]
            );
          },
        }
      ));

      expect(events, isEmpty);
      expect(tester.findText('Target'), isNotNull);
      expect(tester.findText('H'), isNotNull);
      expect(tester.findText('V'), isNotNull);

      // horizontal draggable drags horizontally
      expect(events, isEmpty);
      firstLocation = tester.getCenter(tester.findText('H'));
      secondLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
494
      TestGesture gesture = tester.startGesture(firstLocation, pointer: 7);
495
      tester.pump();
Adam Barth's avatar
Adam Barth committed
496
      gesture.moveTo(secondLocation);
497
      tester.pump();
Adam Barth's avatar
Adam Barth committed
498
      gesture.up();
499 500 501 502 503 504 505 506 507 508
      tester.pump();
      expect(events, equals(<String>['drop 1']));
      expect(tester.getCenter(tester.findText('Target')).x, greaterThan(0.0));
      events.clear();

      // vertical draggable drags vertically
      expect(events, isEmpty);
      firstLocation = tester.getTopLeft(tester.findText('V'));
      secondLocation = tester.getBottomLeft(tester.findText('V'));
      thirdLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
509
      gesture = tester.startGesture(firstLocation, pointer: 7);
510
      tester.pump();
Adam Barth's avatar
Adam Barth committed
511
      gesture.moveTo(secondLocation);
512
      tester.pump();
Adam Barth's avatar
Adam Barth committed
513
      gesture.moveTo(thirdLocation);
514
      tester.pump();
Adam Barth's avatar
Adam Barth committed
515
      gesture.up();
516 517 518 519 520 521 522 523 524 525 526
      tester.pump();
      expect(events, equals(<String>['drop 2']));
      expect(tester.getCenter(tester.findText('Target')).x, greaterThan(0.0));
      events.clear();

      // horizontal draggable drags vertically when there's no competition
      // from other gesture detectors
      expect(events, isEmpty);
      firstLocation = tester.getTopLeft(tester.findText('H'));
      secondLocation = tester.getBottomLeft(tester.findText('H'));
      thirdLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
527
      gesture = tester.startGesture(firstLocation, pointer: 7);
528
      tester.pump();
Adam Barth's avatar
Adam Barth committed
529
      gesture.moveTo(secondLocation);
530
      tester.pump();
Adam Barth's avatar
Adam Barth committed
531
      gesture.moveTo(thirdLocation);
532
      tester.pump();
Adam Barth's avatar
Adam Barth committed
533
      gesture.up();
534 535 536 537 538 539 540 541 542 543
      tester.pump();
      expect(events, equals(<String>['drop 1']));
      expect(tester.getCenter(tester.findText('Target')).x, greaterThan(0.0));
      events.clear();

      // vertical draggable doesn't drag horizontally when there is competition
      // for horizontal gestures
      expect(events, isEmpty);
      firstLocation = tester.getCenter(tester.findText('V'));
      secondLocation = tester.getCenter(tester.findText('Target'));
Adam Barth's avatar
Adam Barth committed
544
      gesture = tester.startGesture(firstLocation, pointer: 7);
545
      tester.pump();
Adam Barth's avatar
Adam Barth committed
546
      gesture.moveTo(secondLocation);
547
      tester.pump(); // scrolls off screen!
Adam Barth's avatar
Adam Barth committed
548
      gesture.up();
549 550 551 552
      tester.pump();
      expect(events, equals(<String>[]));
      expect(tester.getCenter(tester.findText('Target')).x, lessThan(0.0));
      events.clear();
553 554

    });
Hixie's avatar
Hixie committed
555
  });
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 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 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 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

  test('Drag and drop - onDraggableDropped not called if dropped on accepting target', () {
    testWidgets((WidgetTester tester) {
      List<int> accepted = <int>[];
      bool onDraggableCanceledCalled = false;

      tester.pumpWidget(new MaterialApp(
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) { return new Column(
            children: <Widget>[
              new Draggable<int>(
                data: 1,
                child: new Text('Source'),
                feedback: new Text('Dragging'),
                onDraggableCanceled: (Velocity velocity, Offset offset) {
                  onDraggableCanceledCalled = true;
                }
              ),
              new DragTarget<int>(
                builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
                  return new Container(
                    height: 100.0,
                    child: new Text('Target')
                  );
                },
                onAccept: (int data) {
                  accepted.add(data);
                }
              ),
            ]);
          },
        }
      ));

      expect(accepted, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNull);
      expect(tester.findText('Target'), isNotNull);
      expect(onDraggableCanceledCalled, isFalse);

      Point firstLocation = tester.getCenter(tester.findText('Source'));
      TestGesture gesture = tester.startGesture(firstLocation, pointer: 7);
      tester.pump();

      expect(accepted, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNotNull);
      expect(tester.findText('Target'), isNotNull);
      expect(onDraggableCanceledCalled, isFalse);

      Point secondLocation = tester.getCenter(tester.findText('Target'));
      gesture.moveTo(secondLocation);
      tester.pump();

      expect(accepted, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNotNull);
      expect(tester.findText('Target'), isNotNull);
      expect(onDraggableCanceledCalled, isFalse);

      gesture.up();
      tester.pump();

      expect(accepted, equals(<int>[1]));
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNull);
      expect(tester.findText('Target'), isNotNull);
      expect(onDraggableCanceledCalled, isFalse);
    });
  });

  test('Drag and drop - onDraggableDropped called if dropped on non-accepting target', () {
    testWidgets((WidgetTester tester) {
      List<int> accepted = <int>[];
      bool onDraggableCanceledCalled = false;
      Velocity onDraggableCanceledVelocity;
      Offset onDraggableCanceledOffset;

      tester.pumpWidget(new MaterialApp(
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) { return new Column(
            children: <Widget>[
              new Draggable<int>(
                data: 1,
                child: new Text('Source'),
                feedback: new Text('Dragging'),
                onDraggableCanceled: (Velocity velocity, Offset offset) {
                  onDraggableCanceledCalled = true;
                  onDraggableCanceledVelocity = velocity;
                  onDraggableCanceledOffset = offset;
                }
              ),
              new DragTarget<int>(
                builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
                  return new Container(
                    height: 100.0,
                    child: new Text('Target')
                  );
                },
                onWillAccept: (int data) => false
              ),
            ]);
          },
        }
      ));

      expect(accepted, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNull);
      expect(tester.findText('Target'), isNotNull);
      expect(onDraggableCanceledCalled, isFalse);

      Point firstLocation = tester.getTopLeft(tester.findText('Source'));
      TestGesture gesture = tester.startGesture(firstLocation, pointer: 7);
      tester.pump();

      expect(accepted, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNotNull);
      expect(tester.findText('Target'), isNotNull);
      expect(onDraggableCanceledCalled, isFalse);

      Point secondLocation = tester.getCenter(tester.findText('Target'));
      gesture.moveTo(secondLocation);
      tester.pump();

      expect(accepted, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNotNull);
      expect(tester.findText('Target'), isNotNull);
      expect(onDraggableCanceledCalled, isFalse);

      gesture.up();
      tester.pump();

      expect(accepted, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNull);
      expect(tester.findText('Target'), isNotNull);
      expect(onDraggableCanceledCalled, isTrue);
      expect(onDraggableCanceledVelocity, equals(Velocity.zero));
      expect(onDraggableCanceledOffset, equals(new Offset(secondLocation.x, secondLocation.y)));
    });
  });

  test('Drag and drop - onDraggableDropped called if dropped on non-accepting target with correct velocity', () {
    testWidgets((WidgetTester tester) {
      List<int> accepted = <int>[];
      bool onDraggableCanceledCalled = false;
      Velocity onDraggableCanceledVelocity;
      Offset onDraggableCanceledOffset;

      tester.pumpWidget(new MaterialApp(
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) { return new Column(
            children: <Widget>[
              new Draggable<int>(
                data: 1,
                child: new Text('Source'),
                feedback: new Text('Source'),
                onDraggableCanceled: (Velocity velocity, Offset offset) {
                  onDraggableCanceledCalled = true;
                  onDraggableCanceledVelocity = velocity;
                  onDraggableCanceledOffset = offset;
                }
              ),
              new DragTarget<int>(
                builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
                  return new Container(
                    height: 100.0,
                    child: new Text('Target')
                  );
                },
                onWillAccept: (int data) => false
              ),
            ]);
          },
        }
      ));

      expect(accepted, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNull);
      expect(tester.findText('Target'), isNotNull);
      expect(onDraggableCanceledCalled, isFalse);

      Point flingStart = tester.getTopLeft(tester.findText('Source'));
      tester.flingFrom(flingStart, new Offset(0.0,100.0), 1000.0);
      tester.pump();

      expect(accepted, isEmpty);
      expect(tester.findText('Source'), isNotNull);
      expect(tester.findText('Dragging'), isNull);
      expect(tester.findText('Target'), isNotNull);
      expect(onDraggableCanceledCalled, isTrue);
      expect(onDraggableCanceledVelocity.pixelsPerSecond.dx.abs(), lessThan(0.0000001));
      expect((onDraggableCanceledVelocity.pixelsPerSecond.dy - 1000.0).abs(), lessThan(0.0000001));
      expect(onDraggableCanceledOffset, equals(new Offset(flingStart.x, flingStart.y) + new Offset(0.0, 100.0)));
    });
  });
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 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

  test('Drag and drop - allow pass thru of unaccepted data test', () {
    testWidgets((WidgetTester tester) {
      List<int> acceptedInts = <int>[];
      List<double> acceptedDoubles = <double>[];

      tester.pumpWidget(new MaterialApp(
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) { return new Column(
            children: <Widget>[
              new Draggable<int>(
                data: 1,
                child: new Text('IntSource'),
                feedback: new Text('IntDragging')
              ),
              new Draggable<double>(
                data: 1.0,
                child: new Text('DoubleSource'),
                feedback: new Text('DoubleDragging')
              ),
              new Stack(children:[
                new DragTarget<int>(
                  builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
                    return new IgnorePointer(
                      child: new Container(
                        height: 100.0,
                        child: new Text('Target1')
                      )
                    );
                  },
                  onAccept: (int data) {
                    acceptedInts.add(data);
                  }
                ),
                new DragTarget<double>(
                  builder: (BuildContext context, List<double> data, List<dynamic> rejects) {
                    return new IgnorePointer(
                      child: new Container(
                        height: 100.0,
                        child: new Text('Target2')
                      )
                    );
                  },
                  onAccept: (double data) {
                    acceptedDoubles.add(data);
                  }
                ),
              ])
            ]);
          },
        }
      ));

      expect(acceptedInts, isEmpty);
      expect(acceptedDoubles, isEmpty);
      expect(tester.findText('IntSource'), isNotNull);
      expect(tester.findText('IntDragging'), isNull);
      expect(tester.findText('DoubleSource'), isNotNull);
      expect(tester.findText('DoubleDragging'), isNull);
      expect(tester.findText('Target1'), isNotNull);
      expect(tester.findText('Target2'), isNotNull);

      Point intLocation = tester.getCenter(tester.findText('IntSource'));
      Point doubleLocation = tester.getCenter(tester.findText('DoubleSource'));
      Point targetLocation = tester.getCenter(tester.findText('Target1'));

      // Drag the double draggable.
      TestGesture doubleGesture = tester.startGesture(doubleLocation, pointer: 7);
      tester.pump();

      expect(acceptedInts, isEmpty);
      expect(acceptedDoubles, isEmpty);
      expect(tester.findText('IntDragging'), isNull);
      expect(tester.findText('DoubleDragging'), isNotNull);

      doubleGesture.moveTo(targetLocation);
      tester.pump();

      expect(acceptedInts, isEmpty);
      expect(acceptedDoubles, isEmpty);
      expect(tester.findText('IntDragging'), isNull);
      expect(tester.findText('DoubleDragging'), isNotNull);

      doubleGesture.up();
      tester.pump();

      expect(acceptedInts, isEmpty);
      expect(acceptedDoubles, equals(<double>[1.0]));
      expect(tester.findText('IntDragging'), isNull);
      expect(tester.findText('DoubleDragging'), isNull);

      acceptedDoubles.clear();

      // Drag the int draggable.
      TestGesture intGesture = tester.startGesture(intLocation, pointer: 7);
      tester.pump();

      expect(acceptedInts, isEmpty);
      expect(acceptedDoubles, isEmpty);
      expect(tester.findText('IntDragging'), isNotNull);
      expect(tester.findText('DoubleDragging'), isNull);

      intGesture.moveTo(targetLocation);
      tester.pump();

      expect(acceptedInts, isEmpty);
      expect(acceptedDoubles, isEmpty);
      expect(tester.findText('IntDragging'), isNotNull);
      expect(tester.findText('DoubleDragging'), isNull);

      intGesture.up();
      tester.pump();

      expect(acceptedInts, equals(<int>[1]));
      expect(acceptedDoubles, isEmpty);
      expect(tester.findText('IntDragging'), isNull);
      expect(tester.findText('DoubleDragging'), isNull);
    });
  });
Hixie's avatar
Hixie committed
875
}