gesture_detector_test.dart 5.99 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 9 10
import 'package:test/test.dart';

void main() {
  test('Uncontested scrolls start immediately', () {
11 12 13 14 15 16 17 18
    testWidgets((WidgetTester tester) {
      TestPointer pointer = new TestPointer(7);

      bool didStartDrag = false;
      double updatedDragDelta;
      bool didEndDrag = false;

      Widget widget = new GestureDetector(
19
        onVerticalDragStart: (_) {
20 21 22 23 24 25 26 27
          didStartDrag = true;
        },
        onVerticalDragUpdate: (double scrollDelta) {
          updatedDragDelta = scrollDelta;
        },
        onVerticalDragEnd: (Offset velocity) {
          didEndDrag = true;
        },
Adam Barth's avatar
Adam Barth committed
28 29 30 31 32
        child: new Container(
          decoration: const BoxDecoration(
            backgroundColor: const Color(0xFF00FF00)
          )
        )
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      );

      tester.pumpWidget(widget);
      expect(didStartDrag, isFalse);
      expect(updatedDragDelta, isNull);
      expect(didEndDrag, isFalse);

      Point firstLocation = new Point(10.0, 10.0);
      tester.dispatchEvent(pointer.down(firstLocation), firstLocation);
      expect(didStartDrag, isTrue);
      didStartDrag = false;
      expect(updatedDragDelta, isNull);
      expect(didEndDrag, isFalse);

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

      tester.dispatchEvent(pointer.up(), firstLocation);
      expect(didStartDrag, isFalse);
      expect(updatedDragDelta, isNull);
      expect(didEndDrag, isTrue);
      didEndDrag = false;

      tester.pumpWidget(new Container());
    });
62
  });
63 64

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

68 69
      int gestureCount = 0;
      double dragDistance = 0.0;
70

71 72
      Point downLocation = new Point(10.0, 10.0);
      Point upLocation = new Point(10.0, 20.0);
73

74 75 76 77 78
      Widget widget = new GestureDetector(
        onVerticalDragUpdate: (double delta) { dragDistance += delta; },
        onVerticalDragEnd: (Offset velocity) { gestureCount += 1; },
        onHorizontalDragUpdate: (_) { fail("gesture should not match"); },
        onHorizontalDragEnd: (Offset velocity) { fail("gesture should not match"); },
Adam Barth's avatar
Adam Barth committed
79 80 81 82 83
        child: new Container(
          decoration: const BoxDecoration(
            backgroundColor: const Color(0xFF00FF00)
          )
        )
84 85
      );
      tester.pumpWidget(widget);
86

87 88 89
      tester.dispatchEvent(pointer.down(downLocation), downLocation);
      tester.dispatchEvent(pointer.move(upLocation), downLocation);
      tester.dispatchEvent(pointer.up(), downLocation);
90

91 92 93
      tester.dispatchEvent(pointer.down(downLocation), downLocation);
      tester.dispatchEvent(pointer.move(upLocation), downLocation);
      tester.dispatchEvent(pointer.up(), downLocation);
94

95 96
      expect(gestureCount, 2);
      expect(dragDistance, 20.0);
Adam Barth's avatar
Adam Barth committed
97

98 99
      tester.pumpWidget(new Container());
    });
100
  });
101 102 103 104 105 106 107 108 109

  test('Pan doesn\'t crash', () {
    testWidgets((WidgetTester tester) {
      bool didStartPan = false;
      Offset panDelta;
      bool didEndPan = false;

      tester.pumpWidget(
        new GestureDetector(
110
          onPanStart: (_) {
111 112 113 114 115 116 117 118
            didStartPan = true;
          },
          onPanUpdate: (Offset delta) {
            panDelta = delta;
          },
          onPanEnd: (_) {
            didEndPan = true;
          },
Adam Barth's avatar
Adam Barth committed
119 120 121 122 123
          child: new Container(
            decoration: const BoxDecoration(
              backgroundColor: const Color(0xFF00FF00)
            )
          )
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        )
      );

      expect(didStartPan, isFalse);
      expect(panDelta, isNull);
      expect(didEndPan, isFalse);

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

      expect(didStartPan, isTrue);
      expect(panDelta.dx, 20.0);
      expect(panDelta.dy, 30.0);
      expect(didEndPan, isTrue);
    });
  });
139 140 141 142 143 144 145 146

  test('Translucent', () {
    testWidgets((WidgetTester tester) {
      bool didReceivePointerDown;
      bool didTap;

      void pumpWidgetTree(HitTestBehavior behavior) {
        tester.pumpWidget(
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
          new Stack(
            children: <Widget>[
              new Listener(
                onPointerDown: (_) {
                  didReceivePointerDown = true;
                },
                child: new Container(
                  width: 100.0,
                  height: 100.0,
                  decoration: const BoxDecoration(
                    backgroundColor: const Color(0xFF00FF00)
                  )
                )
              ),
              new Container(
162 163
                width: 100.0,
                height: 100.0,
164 165 166 167 168
                child: new GestureDetector(
                  onTap: () {
                    didTap = true;
                  },
                  behavior: behavior
169 170
                )
              )
171 172
            ]
          )
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        );
      }

      didReceivePointerDown = false;
      didTap = false;
      pumpWidgetTree(null);
      tester.tapAt(new Point(10.0, 10.0));
      expect(didReceivePointerDown, isTrue);
      expect(didTap, isTrue);

      didReceivePointerDown = false;
      didTap = false;
      pumpWidgetTree(HitTestBehavior.deferToChild);
      tester.tapAt(new Point(10.0, 10.0));
      expect(didReceivePointerDown, isTrue);
      expect(didTap, isFalse);

      didReceivePointerDown = false;
      didTap = false;
      pumpWidgetTree(HitTestBehavior.opaque);
      tester.tapAt(new Point(10.0, 10.0));
      expect(didReceivePointerDown, isFalse);
      expect(didTap, isTrue);

      didReceivePointerDown = false;
      didTap = false;
      pumpWidgetTree(HitTestBehavior.translucent);
      tester.tapAt(new Point(10.0, 10.0));
      expect(didReceivePointerDown, isTrue);
      expect(didTap, isTrue);

    });
  });
206
}