scale_test.dart 5.31 KB
Newer Older
1
import 'dart:ui' as ui;
2

3
import 'package:flutter/gestures.dart';
4 5 6 7 8 9 10 11 12 13 14
import 'package:test/test.dart';

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

void main() {
  test('Should recognize scale gestures', () {
    PointerRouter router = new PointerRouter();
    ScaleGestureRecognizer scale = new ScaleGestureRecognizer(router: router);
    TapGestureRecognizer tap = new TapGestureRecognizer(router: router);

    bool didStartScale = false;
15 16
    ui.Point updatedFocalPoint;
    scale.onStart = (ui.Point focalPoint) {
17 18 19 20 21
      didStartScale = true;
      updatedFocalPoint = focalPoint;
    };

    double updatedScale;
22
    scale.onUpdate = (double scale, ui.Point focalPoint) {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
      updatedScale = scale;
      updatedFocalPoint = focalPoint;
    };

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

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

    TestPointer pointer1 = new TestPointer(1);

Kris Giesing's avatar
Kris Giesing committed
39
    PointerInputEvent down = pointer1.down(new Point(10.0, 10.0));
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    scale.addPointer(down);
    tap.addPointer(down);

    GestureArena.instance.close(1);
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // One-finger panning
    router.route(down);
    expect(didStartScale, isFalse);
    expect(updatedScale, isNull);
    expect(updatedFocalPoint, isNull);
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

Kris Giesing's avatar
Kris Giesing committed
58
    router.route(pointer1.move(new Point(20.0, 30.0)));
59 60
    expect(didStartScale, isTrue);
    didStartScale = false;
61
    expect(updatedFocalPoint, new ui.Point(20.0, 30.0));
62 63 64 65 66 67 68 69
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    updatedScale = null;
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // Two-finger scaling
    TestPointer pointer2 = new TestPointer(2);
Kris Giesing's avatar
Kris Giesing committed
70
    PointerInputEvent down2 = pointer2.down(new Point(10.0, 20.0));
71 72 73 74 75 76 77 78 79 80 81 82
    scale.addPointer(down2);
    tap.addPointer(down2);
    GestureArena.instance.close(2);
    router.route(down2);

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

    // Zoom in
Kris Giesing's avatar
Kris Giesing committed
83
    router.route(pointer2.move(new Point(0.0, 10.0)));
84 85
    expect(didStartScale, isTrue);
    didStartScale = false;
86
    expect(updatedFocalPoint, new ui.Point(10.0, 20.0));
87 88 89 90 91 92 93
    updatedFocalPoint = null;
    expect(updatedScale, 2.0);
    updatedScale = null;
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // Zoom out
Kris Giesing's avatar
Kris Giesing committed
94
    router.route(pointer2.move(new Point(15.0, 25.0)));
95
    expect(updatedFocalPoint, new ui.Point(17.5, 27.5));
96 97 98 99 100 101 102
    updatedFocalPoint = null;
    expect(updatedScale, 0.5);
    updatedScale = null;
    expect(didTap, isFalse);

    // Three-finger scaling
    TestPointer pointer3 = new TestPointer(3);
Kris Giesing's avatar
Kris Giesing committed
103
    PointerInputEvent down3 = pointer3.down(new Point(25.0, 35.0));
104 105 106 107 108 109 110 111 112 113 114 115
    scale.addPointer(down3);
    tap.addPointer(down3);
    GestureArena.instance.close(3);
    router.route(down3);

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

    // Zoom in
Kris Giesing's avatar
Kris Giesing committed
116
    router.route(pointer3.move(new Point(55.0, 65.0)));
117 118
    expect(didStartScale, isTrue);
    didStartScale = false;
119
    expect(updatedFocalPoint, new ui.Point(30.0, 40.0));
120 121 122 123 124 125 126
    updatedFocalPoint = null;
    expect(updatedScale, 5.0);
    updatedScale = null;
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    // Return to original positions but with different fingers
Kris Giesing's avatar
Kris Giesing committed
127 128 129
    router.route(pointer1.move(new Point(25.0, 35.0)));
    router.route(pointer2.move(new Point(20.0, 30.0)));
    router.route(pointer3.move(new Point(15.0, 25.0)));
130
    expect(didStartScale, isFalse);
131
    expect(updatedFocalPoint, new ui.Point(20.0, 30.0));
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    updatedScale = null;
    expect(didEndScale, isFalse);
    expect(didTap, isFalse);

    router.route(pointer1.up());
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(didTap, isFalse);

    // Continue scaling with two fingers
Kris Giesing's avatar
Kris Giesing committed
147
    router.route(pointer3.move(new Point(10.0, 20.0)));
148 149
    expect(didStartScale, isTrue);
    didStartScale = false;
150
    expect(updatedFocalPoint, new ui.Point(15.0, 25.0));
151 152 153 154 155 156 157 158 159 160 161 162 163
    updatedFocalPoint = null;
    expect(updatedScale, 2.0);
    updatedScale = null;

    router.route(pointer2.up());
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(didTap, isFalse);

    // Continue panning with one finger
Kris Giesing's avatar
Kris Giesing committed
164
    router.route(pointer3.move(new Point(0.0, 0.0)));
165 166
    expect(didStartScale, isTrue);
    didStartScale = false;
167
    expect(updatedFocalPoint, new ui.Point(0.0, 0.0));
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    updatedFocalPoint = null;
    expect(updatedScale, 1.0);
    updatedScale = null;

    // We are done
    router.route(pointer3.up());
    expect(didStartScale, isFalse);
    expect(updatedFocalPoint, isNull);
    expect(updatedScale, isNull);
    expect(didEndScale, isTrue);
    didEndScale = false;
    expect(didTap, isFalse);

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