gesture_detector_test.dart 2.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
import 'package:sky/widgets.dart';
import 'package:test/test.dart';

import '../engine/mock_events.dart';
import 'widget_tester.dart';

void main() {
  test('Uncontested scrolls start immediately', () {
    WidgetTester tester = new WidgetTester();
    TestPointer pointer = new TestPointer(7);

12 13 14
    bool didStartDrag = false;
    double updatedDragDelta;
    bool didEndDrag = false;
15 16 17

    Widget builder() {
      return new GestureDetector(
18 19
        onVerticalDragStart: () {
          didStartDrag = true;
20
        },
21 22
        onVerticalDragUpdate: (double scrollDelta) {
          updatedDragDelta = scrollDelta;
23
        },
24
        onVerticalDragEnd: (Offset velocity) {
25
          didEndDrag = true;
26 27 28 29 30 31
        },
        child: new Container()
      );
    }

    tester.pumpFrame(builder);
32 33 34
    expect(didStartDrag, isFalse);
    expect(updatedDragDelta, isNull);
    expect(didEndDrag, isFalse);
35 36 37

    Point firstLocation = new Point(10.0, 10.0);
    tester.dispatchEvent(pointer.down(firstLocation), firstLocation);
38 39 40 41
    expect(didStartDrag, isTrue);
    didStartDrag = false;
    expect(updatedDragDelta, isNull);
    expect(didEndDrag, isFalse);
42 43

    Point secondLocation = new Point(10.0, 9.0);
44
    tester.dispatchEvent(pointer.move(secondLocation), firstLocation);
45
    expect(didStartDrag, isFalse);
46
    expect(updatedDragDelta, -1.0);
47 48
    updatedDragDelta = null;
    expect(didEndDrag, isFalse);
49

50
    tester.dispatchEvent(pointer.up(), firstLocation);
51 52 53 54
    expect(didStartDrag, isFalse);
    expect(updatedDragDelta, isNull);
    expect(didEndDrag, isTrue);
    didEndDrag = false;
55 56 57

    tester.pumpFrame(() => new Container());
  });
58 59 60 61 62 63 64 65 66 67 68 69 70

  test('Match two scroll gestures in succession', () {
    WidgetTester tester = new WidgetTester();
    TestPointer pointer = new TestPointer(7);

    int gestureCount = 0;
    double dragDistance = 0.0;

    Point downLocation = new Point(10.0, 10.0);
    Point upLocation = new Point(10.0, 20.0);

    Widget builder() {
      return new GestureDetector(
71
        onVerticalDragUpdate: (double delta) { dragDistance += delta; },
72
        onVerticalDragEnd: (Offset velocity) { gestureCount += 1; },
73
        onHorizontalDragUpdate: (_) { fail("gesture should not match"); },
74
        onHorizontalDragEnd: (Offset velocity) { fail("gesture should not match"); },
75 76 77 78 79 80 81 82 83 84 85 86 87 88
        child: new Container()
      );
    }
    tester.pumpFrame(builder);

    tester.dispatchEvent(pointer.down(downLocation), downLocation);
    tester.dispatchEvent(pointer.move(upLocation), downLocation);
    tester.dispatchEvent(pointer.up(), downLocation);

    tester.dispatchEvent(pointer.down(downLocation), downLocation);
    tester.dispatchEvent(pointer.move(upLocation), downLocation);
    tester.dispatchEvent(pointer.up(), downLocation);

    expect(gestureCount, 2);
89
    expect(dragDistance, 20.0);
90
  });
91
}