From 3d74fe15629fbf61d86bb412d200cc3da83e6f8f Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Fri, 14 Mar 2025 18:30:35 -0700 Subject: [PATCH] Revert "Prevent explicit roles from merging (#164732)" (#165222) This reverts commit c2bb3accbac0d33b43b1e89982425de2657198a1. the previous pr may break some of the mobile a11y. some newly splitted role can't be focus by mobile a11y focus, will need to fix some of the engine code first ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../flutter/lib/src/semantics/semantics.dart | 19 -- .../flutter/test/material/search_test.dart | 79 +++---- packages/flutter/test/widgets/basic_test.dart | 198 ------------------ 3 files changed, 32 insertions(+), 264 deletions(-) diff --git a/packages/flutter/lib/src/semantics/semantics.dart b/packages/flutter/lib/src/semantics/semantics.dart index 675b14f45f7..6ca090b9135 100644 --- a/packages/flutter/lib/src/semantics/semantics.dart +++ b/packages/flutter/lib/src/semantics/semantics.dart @@ -5575,22 +5575,6 @@ class SemanticsConfiguration { bool _hasFlag(SemanticsFlag flag) => (_flags & flag.index) != 0; - bool get _hasExplicitRole { - if (_role != SemanticsRole.none) { - return true; - } - if (_hasFlag(SemanticsFlag.isTextField) || - _hasFlag(SemanticsFlag.isHeader) || - _hasFlag(SemanticsFlag.isSlider) || - _hasFlag(SemanticsFlag.isLink) || - _hasFlag(SemanticsFlag.scopesRoute) || - _hasFlag(SemanticsFlag.isImage) || - _hasFlag(SemanticsFlag.isKeyboardKey)) { - return true; - } - return false; - } - // CONFIGURATION COMBINATION LOGIC /// Whether this configuration is compatible with the provided `other` @@ -5620,9 +5604,6 @@ class SemanticsConfiguration { if (_attributedValue.string.isNotEmpty && other._attributedValue.string.isNotEmpty) { return false; } - if (_hasExplicitRole && other._hasExplicitRole) { - return false; - } return true; } diff --git a/packages/flutter/test/material/search_test.dart b/packages/flutter/test/material/search_test.dart index e8267670c37..aad47130128 100644 --- a/packages/flutter/test/material/search_test.dart +++ b/packages/flutter/test/material/search_test.dart @@ -689,36 +689,27 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( + id: 9, flags: [ + SemanticsFlag.isTextField, + SemanticsFlag.hasEnabledState, + SemanticsFlag.isEnabled, + SemanticsFlag.isFocused, SemanticsFlag.isHeader, if (!isCupertino) SemanticsFlag.namesRoute, ], - children: [ - TestSemantics( - id: 9, - flags: [ - SemanticsFlag.isTextField, - SemanticsFlag.hasEnabledState, - SemanticsFlag.isEnabled, - SemanticsFlag.isFocused, - ], - actions: [ - if (isDesktop) SemanticsAction.didGainAccessibilityFocus, - if (isDesktop) SemanticsAction.didLoseAccessibilityFocus, - SemanticsAction.tap, - SemanticsAction.focus, - SemanticsAction.setSelection, - SemanticsAction.setText, - SemanticsAction.paste, - ], - label: 'Search', - textDirection: TextDirection.ltr, - textSelection: const TextSelection( - baseOffset: 0, - extentOffset: 0, - ), - ), + actions: [ + if (isDesktop) SemanticsAction.didGainAccessibilityFocus, + if (isDesktop) SemanticsAction.didLoseAccessibilityFocus, + SemanticsAction.tap, + SemanticsAction.focus, + SemanticsAction.setSelection, + SemanticsAction.setText, + SemanticsAction.paste, ], + label: 'Search', + textDirection: TextDirection.ltr, + textSelection: const TextSelection(baseOffset: 0, extentOffset: 0), ), TestSemantics( id: 10, @@ -862,33 +853,27 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( + id: 11, flags: [ + SemanticsFlag.isTextField, + SemanticsFlag.hasEnabledState, + SemanticsFlag.isEnabled, + SemanticsFlag.isFocused, SemanticsFlag.isHeader, if (!isCupertino) SemanticsFlag.namesRoute, ], - children: [ - TestSemantics( - id: 11, - flags: [ - SemanticsFlag.isTextField, - SemanticsFlag.hasEnabledState, - SemanticsFlag.isEnabled, - SemanticsFlag.isFocused, - ], - actions: [ - if (isDesktop) SemanticsAction.didGainAccessibilityFocus, - if (isDesktop) SemanticsAction.didLoseAccessibilityFocus, - SemanticsAction.tap, - SemanticsAction.focus, - SemanticsAction.setSelection, - SemanticsAction.setText, - SemanticsAction.paste, - ], - label: 'Search', - textDirection: TextDirection.ltr, - textSelection: const TextSelection(baseOffset: 0, extentOffset: 0), - ), + actions: [ + if (isDesktop) SemanticsAction.didGainAccessibilityFocus, + if (isDesktop) SemanticsAction.didLoseAccessibilityFocus, + SemanticsAction.tap, + SemanticsAction.focus, + SemanticsAction.setSelection, + SemanticsAction.setText, + SemanticsAction.paste, ], + label: 'Search', + textDirection: TextDirection.ltr, + textSelection: const TextSelection(baseOffset: 0, extentOffset: 0), ), TestSemantics(id: 14, label: 'Bottom', textDirection: TextDirection.ltr), ], diff --git a/packages/flutter/test/widgets/basic_test.dart b/packages/flutter/test/widgets/basic_test.dart index 50662649ef3..5310051e57a 100644 --- a/packages/flutter/test/widgets/basic_test.dart +++ b/packages/flutter/test/widgets/basic_test.dart @@ -380,204 +380,6 @@ void main() { expect(attributedHint.attributes[0].range, const TextRange(start: 1, end: 2)); }); - testWidgets('Semantics does not merge role', (WidgetTester tester) async { - final UniqueKey key1 = UniqueKey(); - final UniqueKey key2 = UniqueKey(); - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Semantics( - key: key1, - role: SemanticsRole.dialog, - child: Semantics( - key: key2, - role: SemanticsRole.alertDialog, - child: const Placeholder(), - ), - ), - ), - ), - ); - final SemanticsNode node1 = tester.getSemantics(find.byKey(key1)); - final SemanticsNode node2 = tester.getSemantics(find.byKey(key2)); - expect(node1 != node2, isTrue); - expect(node1.role, SemanticsRole.dialog); - expect(node2.role, SemanticsRole.alertDialog); - }); - - testWidgets('Semantics does not merge role - text field', (WidgetTester tester) async { - final UniqueKey key1 = UniqueKey(); - final UniqueKey key2 = UniqueKey(); - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Semantics( - key: key1, - role: SemanticsRole.dialog, - child: Semantics(key: key2, textField: true, child: const Placeholder()), - ), - ), - ), - ); - final SemanticsNode node1 = tester.getSemantics(find.byKey(key1)); - final SemanticsNode node2 = tester.getSemantics(find.byKey(key2)); - expect(node1 != node2, isTrue); - expect(node1.role, SemanticsRole.dialog); - expect(node2.hasFlag(SemanticsFlag.isTextField), isTrue); - }); - - testWidgets('Semantics does not merge role - link', (WidgetTester tester) async { - final UniqueKey key1 = UniqueKey(); - final UniqueKey key2 = UniqueKey(); - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Semantics( - key: key1, - role: SemanticsRole.dialog, - child: Semantics(key: key2, link: true, child: const Placeholder()), - ), - ), - ), - ); - final SemanticsNode node1 = tester.getSemantics(find.byKey(key1)); - final SemanticsNode node2 = tester.getSemantics(find.byKey(key2)); - expect(node1 != node2, isTrue); - expect(node1.role, SemanticsRole.dialog); - expect(node2.hasFlag(SemanticsFlag.isLink), isTrue); - }); - - testWidgets('Semantics does not merge role - scopes route', (WidgetTester tester) async { - final UniqueKey key1 = UniqueKey(); - final UniqueKey key2 = UniqueKey(); - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Semantics( - key: key1, - role: SemanticsRole.dialog, - child: Semantics( - key: key2, - scopesRoute: true, - explicitChildNodes: true, - child: const Placeholder(), - ), - ), - ), - ), - ); - final SemanticsNode node1 = tester.getSemantics(find.byKey(key1)); - final SemanticsNode node2 = tester.getSemantics(find.byKey(key2)); - expect(node1 != node2, isTrue); - expect(node1.role, SemanticsRole.dialog); - expect(node2.hasFlag(SemanticsFlag.scopesRoute), isTrue); - }); - - testWidgets('Semantics does not merge role - header', (WidgetTester tester) async { - final UniqueKey key1 = UniqueKey(); - final UniqueKey key2 = UniqueKey(); - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Semantics( - key: key1, - role: SemanticsRole.dialog, - child: Semantics(key: key2, header: true, child: const Placeholder()), - ), - ), - ), - ); - final SemanticsNode node1 = tester.getSemantics(find.byKey(key1)); - final SemanticsNode node2 = tester.getSemantics(find.byKey(key2)); - expect(node1 != node2, isTrue); - expect(node1.role, SemanticsRole.dialog); - expect(node2.hasFlag(SemanticsFlag.isHeader), isTrue); - }); - - testWidgets('Semantics does not merge role - image', (WidgetTester tester) async { - final UniqueKey key1 = UniqueKey(); - final UniqueKey key2 = UniqueKey(); - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Semantics( - key: key1, - role: SemanticsRole.dialog, - child: Semantics(key: key2, image: true, child: const Placeholder()), - ), - ), - ), - ); - final SemanticsNode node1 = tester.getSemantics(find.byKey(key1)); - final SemanticsNode node2 = tester.getSemantics(find.byKey(key2)); - expect(node1 != node2, isTrue); - expect(node1.role, SemanticsRole.dialog); - expect(node2.hasFlag(SemanticsFlag.isImage), isTrue); - }); - - testWidgets('Semantics does not merge role - slider', (WidgetTester tester) async { - final UniqueKey key1 = UniqueKey(); - final UniqueKey key2 = UniqueKey(); - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Semantics( - key: key1, - role: SemanticsRole.dialog, - child: Semantics(key: key2, slider: true, child: const Placeholder()), - ), - ), - ), - ); - final SemanticsNode node1 = tester.getSemantics(find.byKey(key1)); - final SemanticsNode node2 = tester.getSemantics(find.byKey(key2)); - expect(node1 != node2, isTrue); - expect(node1.role, SemanticsRole.dialog); - expect(node2.hasFlag(SemanticsFlag.isSlider), isTrue); - }); - - testWidgets('Semantics does not merge role - keyboard key', (WidgetTester tester) async { - final UniqueKey key1 = UniqueKey(); - final UniqueKey key2 = UniqueKey(); - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Semantics( - key: key1, - role: SemanticsRole.dialog, - child: Semantics(key: key2, keyboardKey: true, child: const Placeholder()), - ), - ), - ), - ); - final SemanticsNode node1 = tester.getSemantics(find.byKey(key1)); - final SemanticsNode node2 = tester.getSemantics(find.byKey(key2)); - expect(node1 != node2, isTrue); - expect(node1.role, SemanticsRole.dialog); - expect(node2.hasFlag(SemanticsFlag.isKeyboardKey), isTrue); - }); - - testWidgets('Semantics does not merge role - scopes route', (WidgetTester tester) async { - final UniqueKey key1 = UniqueKey(); - final UniqueKey key2 = UniqueKey(); - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Semantics( - key: key1, - role: SemanticsRole.dialog, - child: Semantics(key: key2, slider: true, child: const Placeholder()), - ), - ), - ), - ); - final SemanticsNode node1 = tester.getSemantics(find.byKey(key1)); - final SemanticsNode node2 = tester.getSemantics(find.byKey(key2)); - expect(node1 != node2, isTrue); - expect(node1.role, SemanticsRole.dialog); - expect(node2.hasFlag(SemanticsFlag.isSlider), isTrue); - }); - testWidgets('Semantics can set controls visibility of nodes', (WidgetTester tester) async { final UniqueKey key = UniqueKey(); await tester.pumpWidget(