From ad20d368ffa09559754e4b2b5c12951341ca3b2d Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Thu, 5 Dec 2019 19:33:01 -0800 Subject: [PATCH] Fix null event crash in TapGestureRecognizer (#45943) --- .../flutter/lib/src/gestures/recognizer.dart | 8 +++- packages/flutter/test/gestures/tap_test.dart | 45 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/gestures/recognizer.dart b/packages/flutter/lib/src/gestures/recognizer.dart index 3e63374a7e..3b988c1e04 100644 --- a/packages/flutter/lib/src/gestures/recognizer.dart +++ b/packages/flutter/lib/src/gestures/recognizer.dart @@ -398,6 +398,9 @@ abstract class PrimaryPointerGestureRecognizer extends OneSequenceGestureRecogni /// If non-null, the recognizer will call [didExceedDeadline] after this /// amount of time has elapsed since starting to track the primary pointer. + /// + /// The [didExceedDeadline] will not be called if the primary pointer is + /// accepted, rejected, or all pointers are up or canceled before [deadline]. final Duration deadline; /// The maximum distance in logical pixels the gesture is allowed to drift @@ -495,7 +498,10 @@ abstract class PrimaryPointerGestureRecognizer extends OneSequenceGestureRecogni @override void acceptGesture(int pointer) { - _gestureAccepted = true; + if (pointer == primaryPointer) { + _stopTimer(); + _gestureAccepted = true; + } } @override diff --git a/packages/flutter/test/gestures/tap_test.dart b/packages/flutter/test/gestures/tap_test.dart index b3071ffee2..55c2f8062a 100644 --- a/packages/flutter/test/gestures/tap_test.dart +++ b/packages/flutter/test/gestures/tap_test.dart @@ -582,6 +582,51 @@ void main() { drag.dispose(); }); + testGesture('non-primary pointers does not trigger timeout', (GestureTester tester) { + // Regression test for https://github.com/flutter/flutter/issues/43310 + // Pointer1 down, pointer2 down, then pointer 1 up, all within the timeout. + // In this way, `BaseTapGestureRecognizer.didExceedDeadline` can be triggered + // after its `_reset`. + final TapGestureRecognizer tap = TapGestureRecognizer(); + + final List recognized = []; + tap.onTapDown = (_) { + recognized.add('down'); + }; + tap.onTapUp = (_) { + recognized.add('up'); + }; + tap.onTap = () { + recognized.add('tap'); + }; + tap.onTapCancel = () { + recognized.add('cancel'); + }; + + tap.addPointer(down1); + tester.closeArena(down1.pointer); + + tap.addPointer(down2); + tester.closeArena(down2.pointer); + + expect(recognized, isEmpty); + + tester.route(up1); + GestureBinding.instance.gestureArena.sweep(down1.pointer); + expect(recognized, ['down', 'up', 'tap']); + recognized.clear(); + + // If regression happens, the following step will throw error + tester.async.elapse(const Duration(milliseconds: 200)); + expect(recognized, isEmpty); + + tester.route(up2); + GestureBinding.instance.gestureArena.sweep(down2.pointer); + expect(recognized, isEmpty); + + tap.dispose(); + }); + group('Enforce consistent-button restriction:', () { // Change buttons during down-up sequence 1 const PointerMoveEvent move1lr = PointerMoveEvent(