long_press_test.dart 3.17 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';

9 10
import 'gesture_tester.dart';

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

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

void main() {
22 23
  setUp(ensureGesturer);

24
  test('Should recognize long press', () {
25
    LongPressGestureRecognizer longPress = new LongPressGestureRecognizer();
26 27 28 29 30 31

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

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

    longPress.dispose();
  });

  test('Up cancels long press', () {
48
    LongPressGestureRecognizer longPress = new LongPressGestureRecognizer();
49 50 51 52 53 54

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

55
    new FakeAsync().run((FakeAsync async) {
56
      longPress.addPointer(down);
57
      Gesturer.instance.gestureArena.close(5);
58
      expect(longPressRecognized, isFalse);
59
      Gesturer.instance.pointerRouter.route(down);
60
      expect(longPressRecognized, isFalse);
Ian Hickson's avatar
Ian Hickson committed
61
      async.elapse(const Duration(milliseconds: 300));
62
      expect(longPressRecognized, isFalse);
63
      Gesturer.instance.pointerRouter.route(up);
64
      expect(longPressRecognized, isFalse);
Ian Hickson's avatar
Ian Hickson committed
65
      async.elapse(const Duration(seconds: 1));
66 67 68 69 70 71
      expect(longPressRecognized, isFalse);
    });

    longPress.dispose();
  });

Hixie's avatar
Hixie committed
72
  test('Should recognize both tap down and long press', () {
73 74
    LongPressGestureRecognizer longPress = new LongPressGestureRecognizer();
    TapGestureRecognizer tap = new TapGestureRecognizer();
75

Hixie's avatar
Hixie committed
76 77 78
    bool tapDownRecognized = false;
    tap.onTapDown = (_) {
      tapDownRecognized = true;
79 80 81 82 83 84 85
    };

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

86
    new FakeAsync().run((FakeAsync async) {
Hixie's avatar
Hixie committed
87
      tap.addPointer(down);
88
      longPress.addPointer(down);
89
      Gesturer.instance.gestureArena.close(5);
Hixie's avatar
Hixie committed
90
      expect(tapDownRecognized, isFalse);
91
      expect(longPressRecognized, isFalse);
92
      Gesturer.instance.pointerRouter.route(down);
Hixie's avatar
Hixie committed
93
      expect(tapDownRecognized, isFalse);
94
      expect(longPressRecognized, isFalse);
Ian Hickson's avatar
Ian Hickson committed
95
      async.elapse(const Duration(milliseconds: 300));
Hixie's avatar
Hixie committed
96
      expect(tapDownRecognized, isTrue);
97
      expect(longPressRecognized, isFalse);
Ian Hickson's avatar
Ian Hickson committed
98
      async.elapse(const Duration(milliseconds: 700));
Hixie's avatar
Hixie committed
99
      expect(tapDownRecognized, isTrue);
100 101 102
      expect(longPressRecognized, isTrue);
    });

Hixie's avatar
Hixie committed
103
    tap.dispose();
104 105 106
    longPress.dispose();
  });
}