multidrag_test.dart 5.46 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/gestures.dart';
6
import 'package:flutter_test/flutter_test.dart';
7 8 9

import 'gesture_tester.dart';

10
class TestDrag extends Drag { }
11 12

void main() {
13
  TestWidgetsFlutterBinding.ensureInitialized();
14

15
  testGesture('MultiDrag: moving before delay rejects', (GestureTester tester) {
16
    final DelayedMultiDragGestureRecognizer drag = DelayedMultiDragGestureRecognizer();
17 18

    bool didStartDrag = false;
19
    drag.onStart = (Offset position) {
20
      didStartDrag = true;
21
      return TestDrag();
22 23
    };

24
    final TestPointer pointer = TestPointer(5);
25
    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
26 27 28 29 30
    drag.addPointer(down);
    tester.closeArena(5);
    expect(didStartDrag, isFalse);
    tester.async.flushMicrotasks();
    expect(didStartDrag, isFalse);
31 32 33 34 35 36 37 38 39 40
    tester.route(pointer.move(const Offset(20.0, 60.0))); // move more than touch slop before delay expires
    expect(didStartDrag, isFalse);
    tester.async.elapse(kLongPressTimeout * 2); // expire delay
    expect(didStartDrag, isFalse);
    tester.route(pointer.move(const Offset(30.0, 120.0))); // move some more after delay expires
    expect(didStartDrag, isFalse);
    drag.dispose();
  });

  testGesture('MultiDrag: delay triggers', (GestureTester tester) {
41
    final DelayedMultiDragGestureRecognizer drag = DelayedMultiDragGestureRecognizer();
42 43 44 45

    bool didStartDrag = false;
    drag.onStart = (Offset position) {
      didStartDrag = true;
46
      return TestDrag();
47 48
    };

49
    final TestPointer pointer = TestPointer(5);
50 51 52
    final PointerDownEvent down = pointer.down(const Offset(10.0, 10.0));
    drag.addPointer(down);
    tester.closeArena(5);
53
    expect(didStartDrag, isFalse);
54
    tester.async.flushMicrotasks();
55
    expect(didStartDrag, isFalse);
56
    tester.route(pointer.move(const Offset(20.0, 20.0))); // move less than touch slop before delay expires
57
    expect(didStartDrag, isFalse);
58 59 60 61
    tester.async.elapse(kLongPressTimeout * 2); // expire delay
    expect(didStartDrag, isTrue);
    tester.route(pointer.move(const Offset(30.0, 70.0))); // move more than touch slop after delay expires
    expect(didStartDrag, isTrue);
62 63
    drag.dispose();
  });
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

  testGesture('MultiDrag: can filter based on device kind', (GestureTester tester) {
    final DelayedMultiDragGestureRecognizer drag =
        DelayedMultiDragGestureRecognizer(kind: PointerDeviceKind.touch);

    bool didStartDrag = false;
    drag.onStart = (Offset position) {
      didStartDrag = true;
      return TestDrag();
    };

    final TestPointer mousePointer = TestPointer(5, PointerDeviceKind.mouse);
    final PointerDownEvent down = mousePointer.down(const Offset(10.0, 10.0));
    drag.addPointer(down);
    tester.closeArena(5);
    expect(didStartDrag, isFalse);
    tester.async.flushMicrotasks();
    expect(didStartDrag, isFalse);
    tester.route(mousePointer.move(const Offset(20.0, 20.0))); // move less than touch slop before delay expires
    expect(didStartDrag, isFalse);
    tester.async.elapse(kLongPressTimeout * 2); // expire delay
    // Still false because it shouldn't recognize mouse events.
    expect(didStartDrag, isFalse);
    tester.route(mousePointer.move(const Offset(30.0, 70.0))); // move more than touch slop after delay expires
    // And still false.
    expect(didStartDrag, isFalse);
    drag.dispose();
  });
92 93 94 95 96 97 98 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

  testWidgets('ImmediateMultiGestureRecognizer asserts when kind and supportedDevices are both set', (WidgetTester tester) async {
    try {
      ImmediateMultiDragGestureRecognizer(
        kind: PointerDeviceKind.touch,
        supportedDevices: <PointerDeviceKind>{ PointerDeviceKind.touch },
      );
    } catch(error) {
      expect(error, isAssertionError);
      expect(error.toString(), contains('kind == null || supportedDevices == null'));
    }
  });

  testWidgets('HorizontalMultiDragGestureRecognizer asserts when kind and supportedDevices are both set', (WidgetTester tester) async {
    expect(
      () {
        HorizontalMultiDragGestureRecognizer(
            kind: PointerDeviceKind.touch,
            supportedDevices: <PointerDeviceKind>{ PointerDeviceKind.touch },
        );
      },
      throwsA(
        isA<AssertionError>().having((AssertionError error) => error.toString(),
        'description', contains('kind == null || supportedDevices == null')),
      ),
    );
  });

  testWidgets('VerticalMultiDragGestureRecognizer asserts when kind and supportedDevices are both set', (WidgetTester tester) async {
    expect(
      () {
        VerticalMultiDragGestureRecognizer(
            kind: PointerDeviceKind.touch,
            supportedDevices: <PointerDeviceKind>{ PointerDeviceKind.touch },
        );
      },
      throwsA(
        isA<AssertionError>().having((AssertionError error) => error.toString(),
        'description', contains('kind == null || supportedDevices == null')),
      ),
    );
  });

  testWidgets('DelayedMultiDragGestureRecognizer asserts when kind and supportedDevices are both set', (WidgetTester tester) async {
    expect(
      () {
        DelayedMultiDragGestureRecognizer(
            kind: PointerDeviceKind.touch,
            supportedDevices: <PointerDeviceKind>{ PointerDeviceKind.touch },
        );
      },
      throwsA(
        isA<AssertionError>().having((AssertionError error) => error.toString(),
        'description', contains('kind == null || supportedDevices == null')),
      ),
    );
  });
149
}