gesture_detector_test.dart 6.37 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';
6
import 'package:flutter/widgets.dart';
7 8

void main() {
9
  testWidgets('Uncontested scrolls start immediately', (WidgetTester tester) async {
10 11 12 13 14
    bool didStartDrag = false;
    double updatedDragDelta;
    bool didEndDrag = false;

    Widget widget = new GestureDetector(
15
      onVerticalDragStart: (DragStartDetails details) {
16 17
        didStartDrag = true;
      },
18 19
      onVerticalDragUpdate: (DragUpdateDetails details) {
        updatedDragDelta = details.primaryDelta;
20
      },
21
      onVerticalDragEnd: (DragEndDetails details) {
22 23 24 25 26
        didEndDrag = true;
      },
      child: new Container(
        decoration: const BoxDecoration(
          backgroundColor: const Color(0xFF00FF00)
Adam Barth's avatar
Adam Barth committed
27
        )
28 29 30
      )
    );

31
    await tester.pumpWidget(widget);
32 33 34 35 36
    expect(didStartDrag, isFalse);
    expect(updatedDragDelta, isNull);
    expect(didEndDrag, isFalse);

    Point firstLocation = new Point(10.0, 10.0);
37
    TestGesture gesture = await tester.startGesture(firstLocation, pointer: 7);
38 39 40 41 42 43
    expect(didStartDrag, isTrue);
    didStartDrag = false;
    expect(updatedDragDelta, isNull);
    expect(didEndDrag, isFalse);

    Point secondLocation = new Point(10.0, 9.0);
44
    await gesture.moveTo(secondLocation);
45 46 47 48 49
    expect(didStartDrag, isFalse);
    expect(updatedDragDelta, -1.0);
    updatedDragDelta = null;
    expect(didEndDrag, isFalse);

50
    await gesture.up();
51 52 53 54 55
    expect(didStartDrag, isFalse);
    expect(updatedDragDelta, isNull);
    expect(didEndDrag, isTrue);
    didEndDrag = false;

56
    await tester.pumpWidget(new Container());
57
  });
58

59
  testWidgets('Match two scroll gestures in succession', (WidgetTester tester) async {
60 61 62 63 64 65 66
    int gestureCount = 0;
    double dragDistance = 0.0;

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

    Widget widget = new GestureDetector(
67 68 69 70
      onVerticalDragUpdate: (DragUpdateDetails details) { dragDistance += details.primaryDelta; },
      onVerticalDragEnd: (DragEndDetails details) { gestureCount += 1; },
      onHorizontalDragUpdate: (DragUpdateDetails details) { fail("gesture should not match"); },
      onHorizontalDragEnd: (DragEndDetails details) { fail("gesture should not match"); },
71 72 73
      child: new Container(
        decoration: const BoxDecoration(
          backgroundColor: const Color(0xFF00FF00)
Adam Barth's avatar
Adam Barth committed
74
        )
75 76
      )
    );
77
    await tester.pumpWidget(widget);
78

79 80 81
    TestGesture gesture = await tester.startGesture(downLocation, pointer: 7);
    await gesture.moveTo(upLocation);
    await gesture.up();
82

83 84 85
    gesture = await tester.startGesture(downLocation, pointer: 7);
    await gesture.moveTo(upLocation);
    await gesture.up();
86

87 88
    expect(gestureCount, 2);
    expect(dragDistance, 20.0);
Adam Barth's avatar
Adam Barth committed
89

90
    await tester.pumpWidget(new Container());
91
  });
92

93
  testWidgets('Pan doesn\'t crash', (WidgetTester tester) async {
94 95 96
    bool didStartPan = false;
    Offset panDelta;
    bool didEndPan = false;
97

98
    await tester.pumpWidget(
99
      new GestureDetector(
100
        onPanStart: (DragStartDetails details) {
101 102
          didStartPan = true;
        },
103 104
        onPanUpdate: (DragUpdateDetails details) {
          panDelta = details.delta;
105
        },
106
        onPanEnd: (DragEndDetails details) {
107 108 109 110 111
          didEndPan = true;
        },
        child: new Container(
          decoration: const BoxDecoration(
            backgroundColor: const Color(0xFF00FF00)
Adam Barth's avatar
Adam Barth committed
112
          )
113
        )
114 115
      )
    );
116

117 118 119
    expect(didStartPan, isFalse);
    expect(panDelta, isNull);
    expect(didEndPan, isFalse);
120

121
    await tester.scrollAt(new Point(10.0, 10.0), new Offset(20.0, 30.0));
122

123 124 125 126
    expect(didStartPan, isTrue);
    expect(panDelta.dx, 20.0);
    expect(panDelta.dy, 30.0);
    expect(didEndPan, isTrue);
127
  });
128

129
  testWidgets('Translucent', (WidgetTester tester) async {
130 131 132
    bool didReceivePointerDown;
    bool didTap;

133 134
    Future<Null> pumpWidgetTree(HitTestBehavior behavior) {
      return tester.pumpWidget(
135 136 137 138 139 140 141
        new Stack(
          children: <Widget>[
            new Listener(
              onPointerDown: (_) {
                didReceivePointerDown = true;
              },
              child: new Container(
142 143
                width: 100.0,
                height: 100.0,
144 145
                decoration: const BoxDecoration(
                  backgroundColor: const Color(0xFF00FF00)
146 147
                )
              )
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
            ),
            new Container(
              width: 100.0,
              height: 100.0,
              child: new GestureDetector(
                onTap: () {
                  didTap = true;
                },
                behavior: behavior
              )
            )
          ]
        )
      );
    }

    didReceivePointerDown = false;
    didTap = false;
166 167
    await pumpWidgetTree(null);
    await tester.tapAt(new Point(10.0, 10.0));
168 169 170 171 172
    expect(didReceivePointerDown, isTrue);
    expect(didTap, isTrue);

    didReceivePointerDown = false;
    didTap = false;
173 174
    await pumpWidgetTree(HitTestBehavior.deferToChild);
    await tester.tapAt(new Point(10.0, 10.0));
175 176 177 178 179
    expect(didReceivePointerDown, isTrue);
    expect(didTap, isFalse);

    didReceivePointerDown = false;
    didTap = false;
180 181
    await pumpWidgetTree(HitTestBehavior.opaque);
    await tester.tapAt(new Point(10.0, 10.0));
182 183 184 185 186
    expect(didReceivePointerDown, isFalse);
    expect(didTap, isTrue);

    didReceivePointerDown = false;
    didTap = false;
187 188
    await pumpWidgetTree(HitTestBehavior.translucent);
    await tester.tapAt(new Point(10.0, 10.0));
189 190 191
    expect(didReceivePointerDown, isTrue);
    expect(didTap, isTrue);

192
  });
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

  testWidgets('Empty', (WidgetTester tester) async {
    bool didTap = false;
    await tester.pumpWidget(
      new Center(
        child: new GestureDetector(
          onTap: () {
            didTap = true;
          },
        )
      )
    );
    expect(didTap, isFalse);
    await tester.tapAt(new Point(10.0, 10.0));
    expect(didTap, isTrue);
  });

  testWidgets('Only container', (WidgetTester tester) async {
    bool didTap = false;
    await tester.pumpWidget(
      new Center(
        child: new GestureDetector(
          onTap: () {
            didTap = true;
          },
          child: new Container(),
        )
      )
    );
    expect(didTap, isFalse);
    await tester.tapAt(new Point(10.0, 10.0));
    expect(didTap, isFalse);
  });
226
}