long_press_test.dart 3.49 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.

5
import 'package:quiver/testing/async.dart';
6
import 'package:flutter/gestures.dart';
7 8
import 'package:test/test.dart';

Ian Hickson's avatar
Ian Hickson committed
9
const PointerDownEvent down = const PointerDownEvent(
10
  pointer: 5,
Ian Hickson's avatar
Ian Hickson committed
11
  position: const Point(10.0, 10.0)
12 13
);

Ian Hickson's avatar
Ian Hickson committed
14
const PointerUpEvent up = const PointerUpEvent(
15
  pointer: 5,
Ian Hickson's avatar
Ian Hickson committed
16
  position: const Point(11.0, 9.0)
17 18 19 20 21
);

void main() {
  test('Should recognize long press', () {
    PointerRouter router = new PointerRouter();
Ian Hickson's avatar
Ian Hickson committed
22 23 24 25 26
    GestureArena gestureArena = new GestureArena();
    LongPressGestureRecognizer longPress = new LongPressGestureRecognizer(
      router: router,
      gestureArena: gestureArena
    );
27 28 29 30 31 32

    bool longPressRecognized = false;
    longPress.onLongPress = () {
      longPressRecognized = true;
    };

33
    new FakeAsync().run((FakeAsync async) {
34
      longPress.addPointer(down);
Ian Hickson's avatar
Ian Hickson committed
35
      gestureArena.close(5);
36
      expect(longPressRecognized, isFalse);
37
      router.route(down);
38
      expect(longPressRecognized, isFalse);
Ian Hickson's avatar
Ian Hickson committed
39
      async.elapse(const Duration(milliseconds: 300));
40
      expect(longPressRecognized, isFalse);
Ian Hickson's avatar
Ian Hickson committed
41
      async.elapse(const Duration(milliseconds: 700));
42 43 44 45 46 47 48 49
      expect(longPressRecognized, isTrue);
    });

    longPress.dispose();
  });

  test('Up cancels long press', () {
    PointerRouter router = new PointerRouter();
Ian Hickson's avatar
Ian Hickson committed
50 51 52 53 54
    GestureArena gestureArena = new GestureArena();
    LongPressGestureRecognizer longPress = new LongPressGestureRecognizer(
      router: router,
      gestureArena: gestureArena
    );
55 56 57 58 59 60

    bool longPressRecognized = false;
    longPress.onLongPress = () {
      longPressRecognized = true;
    };

61
    new FakeAsync().run((FakeAsync async) {
62
      longPress.addPointer(down);
Ian Hickson's avatar
Ian Hickson committed
63
      gestureArena.close(5);
64
      expect(longPressRecognized, isFalse);
65
      router.route(down);
66
      expect(longPressRecognized, isFalse);
Ian Hickson's avatar
Ian Hickson committed
67
      async.elapse(const Duration(milliseconds: 300));
68
      expect(longPressRecognized, isFalse);
69
      router.route(up);
70
      expect(longPressRecognized, isFalse);
Ian Hickson's avatar
Ian Hickson committed
71
      async.elapse(const Duration(seconds: 1));
72 73 74 75 76 77
      expect(longPressRecognized, isFalse);
    });

    longPress.dispose();
  });

Hixie's avatar
Hixie committed
78
  test('Should recognize both tap down and long press', () {
79
    PointerRouter router = new PointerRouter();
Ian Hickson's avatar
Ian Hickson committed
80 81 82 83 84 85 86 87 88
    GestureArena gestureArena = new GestureArena();
    LongPressGestureRecognizer longPress = new LongPressGestureRecognizer(
      router: router,
      gestureArena: gestureArena
    );
    TapGestureRecognizer tap = new TapGestureRecognizer(
      router: router,
      gestureArena: gestureArena
    );
89

Hixie's avatar
Hixie committed
90 91 92
    bool tapDownRecognized = false;
    tap.onTapDown = (_) {
      tapDownRecognized = true;
93 94 95 96 97 98 99
    };

    bool longPressRecognized = false;
    longPress.onLongPress = () {
      longPressRecognized = true;
    };

100
    new FakeAsync().run((FakeAsync async) {
Hixie's avatar
Hixie committed
101
      tap.addPointer(down);
102
      longPress.addPointer(down);
Ian Hickson's avatar
Ian Hickson committed
103
      gestureArena.close(5);
Hixie's avatar
Hixie committed
104
      expect(tapDownRecognized, isFalse);
105
      expect(longPressRecognized, isFalse);
106
      router.route(down);
Hixie's avatar
Hixie committed
107
      expect(tapDownRecognized, isFalse);
108
      expect(longPressRecognized, isFalse);
Ian Hickson's avatar
Ian Hickson committed
109
      async.elapse(const Duration(milliseconds: 300));
Hixie's avatar
Hixie committed
110
      expect(tapDownRecognized, isTrue);
111
      expect(longPressRecognized, isFalse);
Ian Hickson's avatar
Ian Hickson committed
112
      async.elapse(const Duration(milliseconds: 700));
Hixie's avatar
Hixie committed
113
      expect(tapDownRecognized, isTrue);
114 115 116
      expect(longPressRecognized, isTrue);
    });

Hixie's avatar
Hixie committed
117
    tap.dispose();
118 119 120
    longPress.dispose();
  });
}