From 137ec45da1dc682973615952241cd6af16186738 Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Wed, 27 Feb 2019 14:58:59 -0800 Subject: [PATCH] Call onTapCancel when down pointer gets cancelled (#28546) --- packages/flutter/lib/src/gestures/tap.dart | 6 +- packages/flutter/test/gestures/tap_test.dart | 67 ++++++++++++++++++- .../flutter/test/material/ink_well_test.dart | 2 +- .../test/widgets/gesture_detector_test.dart | 15 ++--- .../test/widgets/text_selection_test.dart | 6 +- 5 files changed, 81 insertions(+), 15 deletions(-) diff --git a/packages/flutter/lib/src/gestures/tap.dart b/packages/flutter/lib/src/gestures/tap.dart index 0f063bf8b0..82123507d8 100644 --- a/packages/flutter/lib/src/gestures/tap.dart +++ b/packages/flutter/lib/src/gestures/tap.dart @@ -174,6 +174,9 @@ class TapGestureRecognizer extends PrimaryPointerGestureRecognizer { _finalPosition = event.position; _checkUp(); } else if (event is PointerCancelEvent) { + if (_sentTapDown && onTapCancel != null) { + invokeCallback('onTapCancel', onTapCancel); + } _reset(); } } @@ -183,6 +186,7 @@ class TapGestureRecognizer extends PrimaryPointerGestureRecognizer { if (_wonArenaForPrimaryPointer && disposition == GestureDisposition.rejected) { // This can happen if the superclass decides the primary pointer // exceeded the touch slop, or if the recognizer is disposed. + assert(_sentTapDown); if (onTapCancel != null) invokeCallback('spontaneous onTapCancel', onTapCancel); _reset(); @@ -211,7 +215,7 @@ class TapGestureRecognizer extends PrimaryPointerGestureRecognizer { if (pointer == primaryPointer) { // Another gesture won the arena. assert(state != GestureRecognizerState.possible); - if (onTapCancel != null) + if (_sentTapDown && onTapCancel != null) invokeCallback('forced onTapCancel', onTapCancel); _reset(); } diff --git a/packages/flutter/test/gestures/tap_test.dart b/packages/flutter/test/gestures/tap_test.dart index f09d46588b..8c0beb2117 100644 --- a/packages/flutter/test/gestures/tap_test.dart +++ b/packages/flutter/test/gestures/tap_test.dart @@ -388,7 +388,6 @@ void main() { 'tapA onTapDown', 'tapA onTapUp', 'tapA onTap', - 'tapB onTapCancel', 'swept 1', 'down 2 to A', 'down 2 to B', @@ -398,10 +397,74 @@ void main() { 'tapA onTapDown', 'tapA onTapUp', 'tapA onTap', - 'tapB onTapCancel', 'swept 2', 'disposed A', 'disposed B', ]); }); + + testGesture('PointerCancelEvent cancels tap', (GestureTester tester) { + const PointerDownEvent down = PointerDownEvent( + pointer: 5, + position: Offset(10.0, 10.0) + ); + const PointerCancelEvent cancel = PointerCancelEvent( + pointer: 5, + position: Offset(10.0, 10.0) + ); + + 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(down); + tester.closeArena(5); + tester.async.elapse(const Duration(milliseconds: 5000)); + expect(recognized, ['down']); + tester.route(cancel); + expect(recognized, ['down', 'cancel']); + + tap.dispose(); + }); + + testGesture('losing tap gesture recognizer does not send onTapCancel', (GestureTester tester) { + final TapGestureRecognizer tap = TapGestureRecognizer(); + final HorizontalDragGestureRecognizer drag = HorizontalDragGestureRecognizer(); + + 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(down3); + drag.addPointer(down3); + tester.closeArena(3); + tester.route(move3); + GestureBinding.instance.gestureArena.sweep(3); + expect(recognized, isEmpty); + + tap.dispose(); + drag.dispose(); + }); } diff --git a/packages/flutter/test/material/ink_well_test.dart b/packages/flutter/test/material/ink_well_test.dart index e89d1baf1f..fb2d0401bc 100644 --- a/packages/flutter/test/material/ink_well_test.dart +++ b/packages/flutter/test/material/ink_well_test.dart @@ -54,7 +54,7 @@ void main() { await tester.tap(find.byType(InkWell), pointer: 2); await tester.tap(find.byType(InkWell), pointer: 3); - expect(log, equals(['tap-cancel', 'double-tap'])); + expect(log, equals(['double-tap'])); log.clear(); await tester.longPress(find.byType(InkWell), pointer: 4); diff --git a/packages/flutter/test/widgets/gesture_detector_test.dart b/packages/flutter/test/widgets/gesture_detector_test.dart index 71b801bd00..773d9d2504 100644 --- a/packages/flutter/test/widgets/gesture_detector_test.dart +++ b/packages/flutter/test/widgets/gesture_detector_test.dart @@ -287,33 +287,32 @@ void main() { ); // Pointer is dragged from the center of the 800x100 gesture detector - // to a point (400,300) below it. This always causes onTapCancel to be - // called; onTap should never be called. + // to a point (400,300) below it. This should never call onTap. Future dragOut(Duration timeout) async { final TestGesture gesture = await tester.startGesture(const Offset(400.0, 50.0)); - // If the timeout is less than kPressTimeout the recognizer will just trigger - // the onTapCancel callback. If the timeout is greater than kLongPressTimeout + // If the timeout is less than kPressTimeout the recognizer will not + // trigger any callbacks. If the timeout is greater than kLongPressTimeout // then onTapDown, onLongPress, and onCancel will be called. await tester.pump(timeout); await gesture.moveTo(const Offset(400.0, 300.0)); await gesture.up(); } - await dragOut(kPressTimeout * 0.5); // generates tapCancel + await dragOut(kPressTimeout * 0.5); // generates nothing expect(tapDown, 0); - expect(tapCancel, 1); + expect(tapCancel, 0); expect(tap, 0); expect(longPress, 0); await dragOut(kPressTimeout); // generates tapDown, tapCancel expect(tapDown, 1); - expect(tapCancel, 2); + expect(tapCancel, 1); expect(tap, 0); expect(longPress, 0); await dragOut(kLongPressTimeout); // generates tapDown, longPress, tapCancel expect(tapDown, 2); - expect(tapCancel, 3); + expect(tapCancel, 2); expect(tap, 0); expect(longPress, 1); }); diff --git a/packages/flutter/test/widgets/text_selection_test.dart b/packages/flutter/test/widgets/text_selection_test.dart index 39b39fa5c9..c4df7c5d81 100644 --- a/packages/flutter/test/widgets/text_selection_test.dart +++ b/packages/flutter/test/widgets/text_selection_test.dart @@ -119,7 +119,7 @@ void main() { expect(singleLongTapStartCount, 0); }); - testWidgets('a very quick swipe is just a canceled tap', (WidgetTester tester) async { + testWidgets('a very quick swipe is ignored', (WidgetTester tester) async { await pumpGestureDetector(tester); final TestGesture gesture = await tester.startGesture(const Offset(200, 200)); await tester.pump(const Duration(milliseconds: 20)); @@ -127,7 +127,7 @@ void main() { await tester.pump(); expect(singleTapUpCount, 0); expect(tapCount, 0); - expect(singleTapCancelCount, 1); + expect(singleTapCancelCount, 0); expect(doubleTapDownCount, 0); expect(singleLongTapStartCount, 0); @@ -135,7 +135,7 @@ void main() { // Nothing else happens on up. expect(singleTapUpCount, 0); expect(tapCount, 0); - expect(singleTapCancelCount, 1); + expect(singleTapCancelCount, 0); expect(doubleTapDownCount, 0); expect(singleLongTapStartCount, 0); });