From 396cd247708f91342070bf292d2cc34315a06c2e Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Wed, 28 Oct 2020 17:32:12 -0700 Subject: [PATCH] Allow adding/removing onTap/onDismiss to Semantics (#69230) --- .../flutter/lib/src/rendering/proxy_box.dart | 4 +- .../flutter/test/widgets/semantics_test.dart | 387 ++++++++++++++++++ 2 files changed, 389 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 68f1227291..4c3eb264da 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -4087,7 +4087,7 @@ class RenderSemanticsAnnotations extends RenderProxyBox { return; final bool hadValue = _onTap != null; _onTap = handler; - if ((handler != null) == hadValue) + if ((handler != null) != hadValue) markNeedsSemanticsUpdate(); } @@ -4105,7 +4105,7 @@ class RenderSemanticsAnnotations extends RenderProxyBox { return; final bool hadValue = _onDismiss != null; _onDismiss = handler; - if ((handler != null) == hadValue) + if ((handler != null) != hadValue) markNeedsSemanticsUpdate(); } diff --git a/packages/flutter/test/widgets/semantics_test.dart b/packages/flutter/test/widgets/semantics_test.dart index d899ec980e..314f74fe5b 100644 --- a/packages/flutter/test/widgets/semantics_test.dart +++ b/packages/flutter/test/widgets/semantics_test.dart @@ -1096,6 +1096,393 @@ void main() { ); semantics.dispose(); }); + + testWidgets('Can change handlers', (WidgetTester tester) async { + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onTap: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasTapAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onDismiss: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasDismissAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onLongPress: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasLongPressAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onScrollLeft: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasScrollLeftAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onScrollRight: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasScrollRightAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onScrollUp: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasScrollUpAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onScrollDown: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasScrollDownAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onIncrease: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasIncreaseAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onDecrease: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasDecreaseAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onCopy: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasCopyAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onCut: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasCutAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onPaste: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasPasteAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onSetSelection: (TextSelection _) {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasSetSelectionAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onDidGainAccessibilityFocus: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasDidGainAccessibilityFocusAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + onDidLoseAccessibilityFocus: () {}, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + hasDidLoseAccessibilityFocusAction: true, + textDirection: TextDirection.ltr, + )); + + await tester.pumpWidget(Semantics( + container: true, + label: 'foo', + textDirection: TextDirection.ltr, + )); + + expect(tester.getSemantics(find.bySemanticsLabel('foo')), matchesSemantics( + label: 'foo', + textDirection: TextDirection.ltr, + )); + }); } class CustomSortKey extends OrdinalSortKey {