draggable_test.dart 2.21 KB
Newer Older
1
import 'package:flutter/widgets.dart';
Hixie's avatar
Hixie committed
2 3 4
import 'package:test/test.dart';

import '../engine/mock_events.dart';
Adam Barth's avatar
Adam Barth committed
5
import 'widget_tester.dart';
Hixie's avatar
Hixie committed
6 7 8

void main() {
  test('Drag and drop - control test', () {
9 10
    testWidgets((WidgetTester tester) {
      TestPointer pointer = new TestPointer(7);
Hixie's avatar
Hixie committed
11

12
      List accepted = [];
Hixie's avatar
Hixie committed
13

14
      tester.pumpWidget(new Navigator(
15 16
        routes: <String, RouteBuilder>{
          '/': (RouteArguments args) { return new Column(<Widget>[
17
              new Draggable(
18
                navigator: args.navigator,
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
                data: 1,
                child: new Text('Source'),
                feedback: new Text('Dragging')
              ),
              new DragTarget(
                builder: (context, data, rejects) {
                  return new Container(
                    height: 100.0,
                    child: new Text('Target')
                  );
                },
                onAccept: (data) {
                  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 45 46
      Point firstLocation = tester.getCenter(tester.findText('Source'));
      tester.dispatchEvent(pointer.down(firstLocation), firstLocation);
      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 54 55
      Point secondLocation = tester.getCenter(tester.findText('Target'));
      tester.dispatchEvent(pointer.move(secondLocation), firstLocation);
      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

62 63
      tester.dispatchEvent(pointer.up(), firstLocation);
      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 71
  });
}