scale_test.dart 5.65 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
import 'package:test/test.dart';

9 10
import 'gesture_tester.dart';

11
void main() {
12 13
  setUp(ensureGesturer);

14
  test('Should recognize scale gestures', () {
15 16 17 18
    GestureArena gestureArena = Gesturer.instance.gestureArena;
    PointerRouter pointerRouter = Gesturer.instance.pointerRouter;
    ScaleGestureRecognizer scale = new ScaleGestureRecognizer();
    TapGestureRecognizer tap = new TapGestureRecognizer();
19 20

    bool didStartScale = false;
Ian Hickson's avatar
Ian Hickson committed
21 22
    Point updatedFocalPoint;
    scale.onStart = (Point focalPoint) {
23 24 25 26 27
      didStartScale = true;
      updatedFocalPoint = focalPoint;
    };

    double updatedScale;
Ian Hickson's avatar
Ian Hickson committed
28
    scale.onUpdate = (double scale, Point focalPoint) {
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
      updatedScale = scale;
      updatedFocalPoint = focalPoint;
    };

    bool didEndScale = false;
    scale.onEnd = () {
      didEndScale = true;
    };

    bool didTap = false;
    tap.onTap = () {
      didTap = true;
    };

    TestPointer pointer1 = new TestPointer(1);

Ian Hickson's avatar
Ian Hickson committed
45
    PointerDownEvent down = pointer1.down(const Point(10.0, 10.0));
46 47 48
    scale.addPointer(down);
    tap.addPointer(down);

Ian Hickson's avatar
Ian Hickson committed
49
    gestureArena.close(1);
50 51 52 53 54 55 56
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // One-finger panning
57
    pointerRouter.route(down);
58 59 60 61 62 63
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

64
    pointerRouter.route(pointer1.move(const Point(20.0, 30.0)));
65 66
    expect(didStartScale, isTrue);
    didStartScale = false;
Ian Hickson's avatar
Ian Hickson committed
67
    expect(updatedFocalPoint, const Point(20.0, 30.0));
68 69 70 71 72 73 74 75
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    updatedScale = null;
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // Two-finger scaling
    TestPointer pointer2 = new TestPointer(2);
Ian Hickson's avatar
Ian Hickson committed
76
    PointerDownEvent down2 = pointer2.down(const Point(10.0, 20.0));
77 78
    scale.addPointer(down2);
    tap.addPointer(down2);
Ian Hickson's avatar
Ian Hickson committed
79
    gestureArena.close(2);
80
    pointerRouter.route(down2);
81 82 83 84 85 86 87 88

    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(didStartScale, isFalse);

    // Zoom in
89
    pointerRouter.route(pointer2.move(const Point(0.0, 10.0)));
90 91
    expect(didStartScale, isTrue);
    didStartScale = false;
Ian Hickson's avatar
Ian Hickson committed
92
    expect(updatedFocalPoint, const Point(10.0, 20.0));
93 94 95 96 97 98 99
    updatedFocalPoint = null;
    expect(updatedScale, 2.0);
    updatedScale = null;
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // Zoom out
100
    pointerRouter.route(pointer2.move(const Point(15.0, 25.0)));
Ian Hickson's avatar
Ian Hickson committed
101
    expect(updatedFocalPoint, const Point(17.5, 27.5));
102 103 104 105 106 107 108
    updatedFocalPoint = null;
    expect(updatedScale, 0.5);
    updatedScale = null;
    expect(didTap, isFalse);

    // Three-finger scaling
    TestPointer pointer3 = new TestPointer(3);
Ian Hickson's avatar
Ian Hickson committed
109
    PointerDownEvent down3 = pointer3.down(const Point(25.0, 35.0));
110 111
    scale.addPointer(down3);
    tap.addPointer(down3);
Ian Hickson's avatar
Ian Hickson committed
112
    gestureArena.close(3);
113
    pointerRouter.route(down3);
114 115 116 117 118 119 120 121

    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(didStartScale, isFalse);

    // Zoom in
122
    pointerRouter.route(pointer3.move(const Point(55.0, 65.0)));
123 124
    expect(didStartScale, isTrue);
    didStartScale = false;
Ian Hickson's avatar
Ian Hickson committed
125
    expect(updatedFocalPoint, const Point(30.0, 40.0));
126 127 128 129 130 131 132
    updatedFocalPoint = null;
    expect(updatedScale, 5.0);
    updatedScale = null;
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // Return to original positions but with different fingers
133 134 135
    pointerRouter.route(pointer1.move(const Point(25.0, 35.0)));
    pointerRouter.route(pointer2.move(const Point(20.0, 30.0)));
    pointerRouter.route(pointer3.move(const Point(15.0, 25.0)));
136
    expect(didStartScale, isFalse);
Ian Hickson's avatar
Ian Hickson committed
137
    expect(updatedFocalPoint, const Point(20.0, 30.0));
138 139 140 141 142 143
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    updatedScale = null;
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

144
    pointerRouter.route(pointer1.up());
145 146 147 148 149 150 151 152
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(didTap, isFalse);

    // Continue scaling with two fingers
153
    pointerRouter.route(pointer3.move(const Point(10.0, 20.0)));
154 155
    expect(didStartScale, isTrue);
    didStartScale = false;
Ian Hickson's avatar
Ian Hickson committed
156
    expect(updatedFocalPoint, const Point(15.0, 25.0));
157 158 159 160
    updatedFocalPoint = null;
    expect(updatedScale, 2.0);
    updatedScale = null;

161
    pointerRouter.route(pointer2.up());
162 163 164 165 166 167 168 169
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(didTap, isFalse);

    // Continue panning with one finger
170
    pointerRouter.route(pointer3.move(const Point(0.0, 0.0)));
171 172
    expect(didStartScale, isTrue);
    didStartScale = false;
Ian Hickson's avatar
Ian Hickson committed
173
    expect(updatedFocalPoint, const Point(0.0, 0.0));
174 175 176 177 178
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    updatedScale = null;

    // We are done
179
    pointerRouter.route(pointer3.up());
180 181 182 183 184 185 186 187 188 189 190
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(didTap, isFalse);

    scale.dispose();
    tap.dispose();
  });
}