draggable_test.dart 18.4 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) {
Hixie's avatar
Hixie committed
12
      List<dynamic> accepted = <dynamic>[];
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 66 67 68 69
      expect(accepted, equals([1]));
      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
  });
Hixie's avatar
Hixie committed
556
}