drag_test.dart 5.84 KB
Newer Older
Ian Hickson's avatar
Ian Hickson 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/gestures.dart';
7

8 9
import 'gesture_tester.dart';

10
void main() {
11
  setUp(ensureGestureBinding);
12

13
  testGesture('Should recognize pan', (GestureTester tester) {
14 15
    final PanGestureRecognizer pan = new PanGestureRecognizer();
    final TapGestureRecognizer tap = new TapGestureRecognizer();
16

17
    bool didStartPan = false;
18
    pan.onStart = (_) {
19
      didStartPan = true;
20 21
    };

Ian Hickson's avatar
Ian Hickson committed
22
    Offset updatedScrollDelta;
23 24
    pan.onUpdate = (DragUpdateDetails details) {
      updatedScrollDelta = details.delta;
25 26
    };

27
    bool didEndPan = false;
28
    pan.onEnd = (DragEndDetails details) {
29
      didEndPan = true;
30 31
    };

32 33 34 35
    bool didTap = false;
    tap.onTap = () {
      didTap = true;
    };
36

37 38
    final TestPointer pointer = new TestPointer(5);
    final PointerDownEvent down = pointer.down(const Point(10.0, 10.0));
39 40
    pan.addPointer(down);
    tap.addPointer(down);
41
    tester.closeArena(5);
42 43 44 45 46
    expect(didStartPan, isFalse);
    expect(updatedScrollDelta, isNull);
    expect(didEndPan, isFalse);
    expect(didTap, isFalse);

47
    tester.route(down);
48 49 50 51 52
    expect(didStartPan, isFalse);
    expect(updatedScrollDelta, isNull);
    expect(didEndPan, isFalse);
    expect(didTap, isFalse);

53
    tester.route(pointer.move(const Point(20.0, 20.0)));
54 55
    expect(didStartPan, isTrue);
    didStartPan = false;
Ian Hickson's avatar
Ian Hickson committed
56
    expect(updatedScrollDelta, const Offset(10.0, 10.0));
57 58 59 60
    updatedScrollDelta = null;
    expect(didEndPan, isFalse);
    expect(didTap, isFalse);

61
    tester.route(pointer.move(const Point(20.0, 25.0)));
62
    expect(didStartPan, isFalse);
Ian Hickson's avatar
Ian Hickson committed
63
    expect(updatedScrollDelta, const Offset(0.0, 5.0));
64 65 66 67
    updatedScrollDelta = null;
    expect(didEndPan, isFalse);
    expect(didTap, isFalse);

68
    tester.route(pointer.up());
69 70 71 72 73 74 75 76
    expect(didStartPan, isFalse);
    expect(updatedScrollDelta, isNull);
    expect(didEndPan, isTrue);
    didEndPan = false;
    expect(didTap, isFalse);

    pan.dispose();
    tap.dispose();
77
  });
78 79

  testGesture('Should recognize drag', (GestureTester tester) {
80
    final HorizontalDragGestureRecognizer drag = new HorizontalDragGestureRecognizer();
81 82 83 84 85 86 87

    bool didStartDrag = false;
    drag.onStart = (_) {
      didStartDrag = true;
    };

    double updatedDelta;
88 89
    drag.onUpdate = (DragUpdateDetails details) {
      updatedDelta = details.primaryDelta;
90 91 92
    };

    bool didEndDrag = false;
93
    drag.onEnd = (DragEndDetails details) {
94 95 96
      didEndDrag = true;
    };

97 98
    final TestPointer pointer = new TestPointer(5);
    final PointerDownEvent down = pointer.down(const Point(10.0, 10.0));
99 100 101 102 103 104 105 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
    drag.addPointer(down);
    tester.closeArena(5);
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(down);
    expect(didStartDrag, isTrue);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isFalse);

    tester.route(pointer.move(const Point(20.0, 25.0)));
    expect(didStartDrag, isTrue);
    didStartDrag = false;
    expect(updatedDelta, 10.0);
    updatedDelta = null;
    expect(didEndDrag, isFalse);

    tester.route(pointer.move(const Point(20.0, 25.0)));
    expect(didStartDrag, isFalse);
    expect(updatedDelta, 0.0);
    updatedDelta = null;
    expect(didEndDrag, isFalse);

    tester.route(pointer.up());
    expect(didStartDrag, isFalse);
    expect(updatedDelta, isNull);
    expect(didEndDrag, isTrue);
    didEndDrag = false;

    drag.dispose();
  });
131 132

  testGesture('Clamp max velocity', (GestureTester tester) {
133
    final HorizontalDragGestureRecognizer drag = new HorizontalDragGestureRecognizer();
134 135

    Velocity velocity;
136
    double primaryVelocity;
137 138
    drag.onEnd = (DragEndDetails details) {
      velocity = details.velocity;
139
      primaryVelocity = details.primaryVelocity;
140 141
    };

142 143
    final TestPointer pointer = new TestPointer(5);
    final PointerDownEvent down = pointer.down(const Point(10.0, 25.0), timeStamp: const Duration(milliseconds: 10));
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    drag.addPointer(down);
    tester.closeArena(5);
    tester.route(down);
    tester.route(pointer.move(const Point(20.0, 25.0), timeStamp: const Duration(milliseconds: 10)));
    tester.route(pointer.move(const Point(30.0, 25.0), timeStamp: const Duration(milliseconds: 11)));
    tester.route(pointer.move(const Point(40.0, 25.0), timeStamp: const Duration(milliseconds: 12)));
    tester.route(pointer.move(const Point(50.0, 25.0), timeStamp: const Duration(milliseconds: 13)));
    tester.route(pointer.move(const Point(60.0, 25.0), timeStamp: const Duration(milliseconds: 14)));
    tester.route(pointer.move(const Point(70.0, 25.0), timeStamp: const Duration(milliseconds: 15)));
    tester.route(pointer.move(const Point(80.0, 25.0), timeStamp: const Duration(milliseconds: 16)));
    tester.route(pointer.move(const Point(90.0, 25.0), timeStamp: const Duration(milliseconds: 17)));
    tester.route(pointer.move(const Point(100.0, 25.0), timeStamp: const Duration(milliseconds: 18)));
    tester.route(pointer.move(const Point(110.0, 25.0), timeStamp: const Duration(milliseconds: 19)));
    tester.route(pointer.move(const Point(120.0, 25.0), timeStamp: const Duration(milliseconds: 20)));
    tester.route(pointer.up(timeStamp: const Duration(milliseconds: 20)));
    expect(velocity.pixelsPerSecond.dx, inInclusiveRange(0.99 * kMaxFlingVelocity, kMaxFlingVelocity));
160 161
    expect(velocity.pixelsPerSecond.dy, moreOrLessEquals(0.0));
    expect(primaryVelocity, velocity.pixelsPerSecond.dx);
162 163 164

    drag.dispose();
  });
165 166 167 168 169 170 171

  testGesture('Drag details', (GestureTester tester) {
    expect(new DragDownDetails(), hasOneLineDescription);
    expect(new DragStartDetails(), hasOneLineDescription);
    expect(new DragUpdateDetails(globalPosition: Point.origin), hasOneLineDescription);
    expect(new DragEndDetails(), hasOneLineDescription);
  });
172
}