monodrag_test.dart 6.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2014 The Flutter Authors. All rights reserved.
// 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';
import 'package:flutter_test/flutter_test.dart';

import 'gesture_tester.dart';

void main() {
11
  TestWidgetsFlutterBinding.ensureInitialized();
12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  test('acceptGesture tolerates a null lastPendingEventTimestamp', () {
    // Regression test for https://github.com/flutter/flutter/issues/112403
    // and b/249091367
    final DragGestureRecognizer recognizer = VerticalDragGestureRecognizer();
    const PointerDownEvent event = PointerDownEvent(timeStamp: Duration(days: 10));

    expect(recognizer.debugLastPendingEventTimestamp, null);

    recognizer.addAllowedPointer(event);
    expect(recognizer.debugLastPendingEventTimestamp, event.timeStamp);

    // Normal case: acceptGesture called and we have a last timestamp set.
    recognizer.acceptGesture(event.pointer);
    expect(recognizer.debugLastPendingEventTimestamp, null);

    // Reject the gesture to reset state and allow accepting it again.
    recognizer.rejectGesture(event.pointer);
    expect(recognizer.debugLastPendingEventTimestamp, null);

    // Not entirely clear how this can happen, but the bugs mentioned above show
33
    // we can end up in this state empirically.
34 35 36 37
    recognizer.acceptGesture(event.pointer);
    expect(recognizer.debugLastPendingEventTimestamp, null);
  });

38 39 40 41 42
  testGesture('do not crash on up event for a pending pointer after winning arena for another pointer', (GestureTester tester) {
    // Regression test for https://github.com/flutter/flutter/issues/75061.

    final VerticalDragGestureRecognizer v = VerticalDragGestureRecognizer()
      ..onStart = (_) { };
43
    addTearDown(v.dispose);
44 45
    final HorizontalDragGestureRecognizer h = HorizontalDragGestureRecognizer()
      ..onStart = (_) { };
46
    addTearDown(h.dispose);
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

    const PointerDownEvent down90 = PointerDownEvent(
      pointer: 90,
      position: Offset(10.0, 10.0),
    );

    const PointerUpEvent up90 = PointerUpEvent(
      pointer: 90,
      position: Offset(10.0, 10.0),
    );

    const PointerDownEvent down91 = PointerDownEvent(
      pointer: 91,
      position: Offset(20.0, 20.0),
    );

    const PointerUpEvent up91 = PointerUpEvent(
      pointer: 91,
      position: Offset(20.0, 20.0),
    );

    v.addPointer(down90);
69
    GestureBinding.instance.gestureArena.close(90);
70 71
    h.addPointer(down91);
    v.addPointer(down91);
72
    GestureBinding.instance.gestureArena.close(91);
73 74
    tester.async.flushMicrotasks();

75 76
    GestureBinding.instance.handleEvent(up90, HitTestEntry(MockHitTestTarget()));
    GestureBinding.instance.handleEvent(up91, HitTestEntry(MockHitTestTarget()));
77
  });
78

79 80 81 82 83 84 85 86 87 88 89 90 91 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
  testGesture('DragGestureRecognizer should not dispatch drag callbacks when it wins the arena if onlyAcceptDragOnThreshold is true and the threshold has not been met', (GestureTester tester) {
    final VerticalDragGestureRecognizer verticalDrag = VerticalDragGestureRecognizer();
    final List<String> dragCallbacks = <String>[];
    verticalDrag
      ..onlyAcceptDragOnThreshold = true
      ..onStart = (DragStartDetails details) {
        dragCallbacks.add('onStart');
      }
      ..onUpdate = (DragUpdateDetails details) {
        dragCallbacks.add('onUpdate');
      }
      ..onEnd = (DragEndDetails details) {
        dragCallbacks.add('onEnd');
      };

    const PointerDownEvent down1 = PointerDownEvent(
      pointer: 6,
      position: Offset(10.0, 10.0),
    );

    const PointerUpEvent up1 = PointerUpEvent(
      pointer: 6,
      position: Offset(10.0, 10.0),
    );

    verticalDrag.addPointer(down1);
    tester.closeArena(down1.pointer);
    tester.route(down1);
    tester.route(up1);
    expect(dragCallbacks.isEmpty, true);
    verticalDrag.dispose();
    dragCallbacks.clear();
  });

  testGesture('DragGestureRecognizer should dispatch drag callbacks when it wins the arena if onlyAcceptDragOnThreshold is false and the threshold has not been met', (GestureTester tester) {
    final VerticalDragGestureRecognizer verticalDrag = VerticalDragGestureRecognizer();
    final List<String> dragCallbacks = <String>[];
    verticalDrag
      ..onlyAcceptDragOnThreshold = false
      ..onStart = (DragStartDetails details) {
        dragCallbacks.add('onStart');
      }
      ..onUpdate = (DragUpdateDetails details) {
        dragCallbacks.add('onUpdate');
      }
      ..onEnd = (DragEndDetails details) {
        dragCallbacks.add('onEnd');
      };

    const PointerDownEvent down1 = PointerDownEvent(
      pointer: 6,
      position: Offset(10.0, 10.0),
    );

    const PointerUpEvent up1 = PointerUpEvent(
      pointer: 6,
      position: Offset(10.0, 10.0),
    );

    verticalDrag.addPointer(down1);
    tester.closeArena(down1.pointer);
    tester.route(down1);
    tester.route(up1);
    expect(dragCallbacks.isEmpty, false);
    expect(dragCallbacks, <String>['onStart', 'onEnd']);
    verticalDrag.dispose();
    dragCallbacks.clear();
  });

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  group('Recognizers on different button filters:', () {
    final List<String> recognized = <String>[];
    late HorizontalDragGestureRecognizer primaryRecognizer;
    late HorizontalDragGestureRecognizer secondaryRecognizer;
    setUp(() {
      primaryRecognizer = HorizontalDragGestureRecognizer(
          allowedButtonsFilter: (int buttons) => kPrimaryButton == buttons)
        ..onStart = (DragStartDetails details) {
          recognized.add('onStartPrimary');
        };
      secondaryRecognizer = HorizontalDragGestureRecognizer(
          allowedButtonsFilter: (int buttons) => kSecondaryButton == buttons)
        ..onStart = (DragStartDetails details) {
          recognized.add('onStartSecondary');
        };
    });

    tearDown(() {
      recognized.clear();
      primaryRecognizer.dispose();
      secondaryRecognizer.dispose();
    });

    testGesture('Primary button works', (GestureTester tester) {
      const PointerDownEvent down1 = PointerDownEvent(
        pointer: 6,
        position: Offset(10.0, 10.0),
      );

      primaryRecognizer.addPointer(down1);
      secondaryRecognizer.addPointer(down1);
      tester.closeArena(down1.pointer);
      tester.route(down1);
      expect(recognized, <String>['onStartPrimary']);
    });

    testGesture('Secondary button works', (GestureTester tester) {
      const PointerDownEvent down1 = PointerDownEvent(
        pointer: 6,
        position: Offset(10.0, 10.0),
        buttons: kSecondaryMouseButton,
      );

      primaryRecognizer.addPointer(down1);
      secondaryRecognizer.addPointer(down1);
      tester.closeArena(down1.pointer);
      tester.route(down1);
      expect(recognized, <String>['onStartSecondary']);
    });
  });
198 199 200 201 202 203
}

class MockHitTestTarget implements HitTestTarget {
  @override
  void handleEvent(PointerEvent event, HitTestEntry entry) { }
}