diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 98083bf0ba..7b39bb743c 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -42640,6 +42640,7 @@ ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics.dart + ../../../flu ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/accessibility.dart + ../../../flutter/LICENSE ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/alert.dart + ../../../flutter/LICENSE ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/checkable.dart + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/disable.dart + ../../../flutter/LICENSE ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/expandable.dart + ../../../flutter/LICENSE ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/focusable.dart + ../../../flutter/LICENSE ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/header.dart + ../../../flutter/LICENSE @@ -42650,6 +42651,7 @@ ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/label_and_value.dar ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/link.dart + ../../../flutter/LICENSE ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/list.dart + ../../../flutter/LICENSE ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/live_region.dart + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/menus.dart + ../../../flutter/LICENSE ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/platform_view.dart + ../../../flutter/LICENSE ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/requirable.dart + ../../../flutter/LICENSE ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/route.dart + ../../../flutter/LICENSE @@ -45615,6 +45617,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/accessibility.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/alert.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/checkable.dart +FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/disable.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/expandable.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/focusable.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/header.dart @@ -45625,6 +45628,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/label_and_value.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/link.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/list.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/live_region.dart +FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/menus.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/platform_view.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/requirable.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/route.dart diff --git a/engine/src/flutter/lib/ui/semantics.dart b/engine/src/flutter/lib/ui/semantics.dart index 472ec31126..ba274549de 100644 --- a/engine/src/flutter/lib/ui/semantics.dart +++ b/engine/src/flutter/lib/ui/semantics.dart @@ -412,22 +412,45 @@ enum SemanticsRole { /// A input field with a dropdown list box attached. /// - /// For example, a [DropDownMenu] + /// For example, a [DropdownMenu] comboBox, - /// Contains a list of [menu]s. + /// A presentation of [menu] that usually remains visible and is usually + /// presented horizontally. /// /// For example, a [MenuBar]. menuBar, - /// A button that opens a dropdown that contains multiple [menuItem]s. + /// A permanently visible list of controls or a widget that can be made to + /// open and close. /// - /// For example, a [MenuAnchor] or [DropDownButton]. + /// For example, a [MenuAnchor] or [DropdownButton]. menu, - /// A item in a dropdown created by [menu] or [comboBox]. + /// An item in a dropdown created by [menu] or [menuBar]. + /// + /// See also: + /// + /// * [menuItemCheckbox], a menu item with a checkbox. The [menuItemCheckbox] + /// can also be used within [menu] and [menuBar]. + /// * [menuItemRadio], a menu item with a radio button. This role is used by + /// [menu] or [menuBar] as well. menuItem, + /// An item with a checkbox in a dropdown created by [menu] or [menuBar]. + /// + /// See also: + /// + /// * [menuItem] and [menuItemRadio] for menu related roles. + menuItemCheckbox, + + /// An item with a radio button in a dropdown created by [menu] or [menuBar]. + /// + /// See also: + /// + /// * [menuItem] and [menuItemCheckbox] for menu related roles. + menuItemRadio, + /// A container to display multiple [listItem]s in vertical or horizontal /// layout. /// diff --git a/engine/src/flutter/lib/ui/semantics/semantics_node.h b/engine/src/flutter/lib/ui/semantics/semantics_node.h index f4e02a65cd..29d759f9fd 100644 --- a/engine/src/flutter/lib/ui/semantics/semantics_node.h +++ b/engine/src/flutter/lib/ui/semantics/semantics_node.h @@ -73,12 +73,12 @@ enum class SemanticsRole : int32_t { kTab = 1, kTabBar = 2, kTabPanel = 3, - kTable = 4, - kCell = 5, - kRow = 6, - kColumnHeader = 7, - kDialog = 8, - kAlertDialog = 9, + kDialog = 4, + kAlertDialog = 5, + kTable = 6, + kCell = 7, + kRow = 8, + kColumnHeader = 9, kSearchBox = 10, kDragHandle = 11, kSpinButton = 12, @@ -86,16 +86,18 @@ enum class SemanticsRole : int32_t { kMenuBar = 14, kMenu = 15, kMenuItem = 16, - kList = 17, - kListItem = 18, - kForm = 19, - kTooltip = 20, - kLoadingSpinner = 21, - kProgressBar = 22, - kHotKey = 23, - kRadioGroup = 24, - kStatus = 25, - kAlert = 26, + kMenuItemCheckbox = 17, + kMenuItemRadio = 18, + kList = 19, + kListItem = 20, + kForm = 21, + kTooltip = 22, + kLoadingSpinner = 23, + kProgressBar = 24, + kHotKey = 25, + kRadioGroup = 26, + kStatus = 27, + kAlert = 28, }; /// C/C++ representation of `SemanticsFlags` defined in diff --git a/engine/src/flutter/lib/web_ui/lib/semantics.dart b/engine/src/flutter/lib/web_ui/lib/semantics.dart index d64fcc0061..f5b4ffce41 100644 --- a/engine/src/flutter/lib/web_ui/lib/semantics.dart +++ b/engine/src/flutter/lib/web_ui/lib/semantics.dart @@ -283,6 +283,8 @@ enum SemanticsRole { menuBar, menu, menuItem, + menuItemCheckbox, + menuItemRadio, list, listItem, form, diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine.dart b/engine/src/flutter/lib/web_ui/lib/src/engine.dart index 3355a475e1..e6a07b6c2c 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine.dart @@ -106,6 +106,7 @@ export 'engine/scene_view.dart'; export 'engine/semantics/accessibility.dart'; export 'engine/semantics/alert.dart'; export 'engine/semantics/checkable.dart'; +export 'engine/semantics/disable.dart'; export 'engine/semantics/expandable.dart'; export 'engine/semantics/focusable.dart'; export 'engine/semantics/header.dart'; @@ -116,6 +117,7 @@ export 'engine/semantics/label_and_value.dart'; export 'engine/semantics/link.dart'; export 'engine/semantics/list.dart'; export 'engine/semantics/live_region.dart'; +export 'engine/semantics/menus.dart'; export 'engine/semantics/platform_view.dart'; export 'engine/semantics/requirable.dart'; export 'engine/semantics/route.dart'; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics.dart index 7f7f593b41..48b945386a 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics.dart @@ -5,6 +5,7 @@ export 'semantics/accessibility.dart'; export 'semantics/alert.dart'; export 'semantics/checkable.dart'; +export 'semantics/disable.dart'; export 'semantics/expandable.dart'; export 'semantics/focusable.dart'; export 'semantics/header.dart'; @@ -15,6 +16,7 @@ export 'semantics/label_and_value.dart'; export 'semantics/link.dart'; export 'semantics/list.dart'; export 'semantics/live_region.dart'; +export 'semantics/menus.dart'; export 'semantics/platform_view.dart'; export 'semantics/requirable.dart'; export 'semantics/scrollable.dart'; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/checkable.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/checkable.dart index 2df46ec131..1f1b503e6d 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/checkable.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/checkable.dart @@ -158,3 +158,35 @@ class Selectable extends SemanticBehavior { } } } + +/// Adds checkability behavior to a semantic node. +/// +/// A checkable node would have the `aria-checked` set to "true" if the node +/// is currently checked (i.e. [SemanticsObject.isChecked] is true), set to +/// "mixed" if the node is in a mixed state (i.e. [SemanticsObject.isMixed]) and +/// set to "false" if it's not checked or mixed +/// (i.e. [SemanticsObject.isChecked] and [SemanticsObject.isMixed] are +/// false). If the node is not checkable (i.e. [SemanticsObject.isCheckable] +/// is false), then `aria-checked` is unset. +/// +/// This behavior is typically used for a checkbox or a radio button. +class Checkable extends SemanticBehavior { + Checkable(super.semanticsObject, super.owner); + + @override + void update() { + if (semanticsObject.isFlagsDirty) { + if (semanticsObject.isCheckable) { + if (semanticsObject.isChecked) { + owner.setAttribute('aria-checked', 'true'); + } else if (semanticsObject.isMixed) { + owner.setAttribute('aria-checked', 'mixed'); + } else { + owner.setAttribute('aria-checked', 'false'); + } + } else { + owner.removeAttribute('aria-checked'); + } + } + } +} diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/disable.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/disable.dart new file mode 100644 index 0000000000..68c217664c --- /dev/null +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/disable.dart @@ -0,0 +1,26 @@ +// Copyright 2013 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 'semantics.dart'; + +/// Adds disable behavior to a semantic node. +/// +/// A disabled node would have the `aria-disabled` set to "true" if the node +/// is currently disabled (i.e. [SemanticsObject.isEnabled] is false). If the +/// node is enabled (i.e. [SemanticsObject.isEnabled] +/// is true), then `aria-disabled` is unset. +class CanDisable extends SemanticBehavior { + CanDisable(super.semanticsObject, super.owner); + + @override + void update() { + if (semanticsObject.isFlagsDirty) { + if (semanticsObject.enabledState() == EnabledState.disabled) { + owner.setAttribute('aria-disabled', 'true'); + } else { + owner.removeAttribute('aria-disabled'); + } + } + } +} diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/menus.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/menus.dart new file mode 100644 index 0000000000..025f83418b --- /dev/null +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/menus.dart @@ -0,0 +1,204 @@ +// Copyright 2013 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:ui/ui.dart' as ui; + +import 'label_and_value.dart'; +import 'semantics.dart'; + +/// Indicates a menu element. +/// +/// Uses aria menu role to convey this semantic information to the element. +/// +/// Screen-readers takes advantage of "aria-label" to describe the visual. +class SemanticMenu extends SemanticRole { + SemanticMenu(SemanticsObject semanticsObject) + : super.withBasics( + EngineSemanticsRole.menu, + semanticsObject, + preferredLabelRepresentation: LabelRepresentation.ariaLabel, + ) { + setAriaRole('menu'); + } + + @override + void update() { + super.update(); + // Menu items in DropdownButton, PopupMenuButton and MenuAnchor are not the + // immediate children of the menu, so we need to set `aria-owns` on menu. + // When the menu is open, the tree is still the old one without the menu + // item information, so `addOneTimePostUpdateCallback` is called here to get + // the latest tree info. + semanticsObject.owner.addOneTimePostUpdateCallback(_updateMenuItemId); + } + + bool _isMenuItem(SemanticsObject semanticsObject) { + return semanticsObject.role == ui.SemanticsRole.menuItem || + semanticsObject.role == ui.SemanticsRole.menuItemCheckbox || + semanticsObject.role == ui.SemanticsRole.menuItemRadio; + } + + // Starting from the current semantics node, this method traverses the + // semantics tree and collects the menu items by checking whether the role of + // the node is [menuItem], then set `aria-owns` attribute to them. + void _updateMenuItemId() { + final Map tree = semanticsObject.owner.semanticsTree; + final List ids = []; + final int root = semanticsObject.id; + final List queue = []; + if (tree[root]?.childrenInTraversalOrder != null) { + queue.addAll(tree[root]!.childrenInTraversalOrder!); + } + while (queue.isNotEmpty) { + final int child = queue.removeAt(0); + if (tree[child] != null && _isMenuItem(tree[child]!)) { + ids.add(child); + } else { + if (tree[child]?.childrenInTraversalOrder != null) { + queue.addAll(tree[child]!.childrenInTraversalOrder!); + } + } + } + + final String attributeValue = ids.map((id) => 'flt-semantic-node-$id').join(' '); + setAttribute('aria-owns', attributeValue); + } + + @override + bool focusAsRouteDefault() => focusable?.focusAsRouteDefault() ?? false; +} + +/// Indicates a menu bar element. +/// +/// Uses aria menubar role to convey this semantic information to the element. +/// +/// Screen-readers takes advantage of "aria-label" to describe the visual. +class SemanticMenuBar extends SemanticRole { + SemanticMenuBar(SemanticsObject semanticsObject) + : super.withBasics( + EngineSemanticsRole.menuBar, + semanticsObject, + preferredLabelRepresentation: LabelRepresentation.ariaLabel, + ) { + setAriaRole('menubar'); + } + + @override + void update() { + super.update(); + // Menu items in Menu bar are not the immediate children of the menu, so we + // need to set `aria-owns` on menu bar. When the menu is open, the tree is + // still the old one without the menu item information, so + // `addOneTimePostUpdateCallback` is called to get the latest tree info. + semanticsObject.owner.addOneTimePostUpdateCallback(_updateMenuItemId); + } + + bool _isMenuItem(SemanticsObject semanticsObject) { + return semanticsObject.role == ui.SemanticsRole.menuItem || + semanticsObject.role == ui.SemanticsRole.menuItemCheckbox || + semanticsObject.role == ui.SemanticsRole.menuItemRadio; + } + + // Starting from the current semantics node, this method traverses the + // semantics tree and collects the menu items by checking whether the role of + // the node is [menuItem], then set `aria-owns` attribute to them. + void _updateMenuItemId() { + final Map tree = semanticsObject.owner.semanticsTree; + final List ids = []; + final int root = semanticsObject.id; + final List queue = []; + if (tree[root]?.childrenInTraversalOrder != null) { + queue.addAll(tree[root]!.childrenInTraversalOrder!); + } + while (queue.isNotEmpty) { + final int child = queue.removeAt(0); + if (tree[child] != null && _isMenuItem(tree[child]!)) { + ids.add(child); + } else { + if (tree[child]?.childrenInTraversalOrder != null) { + queue.addAll(tree[child]!.childrenInTraversalOrder!); + } + } + } + + final String attributeValue = ids.map((id) => 'flt-semantic-node-$id').join(' '); + setAttribute('aria-owns', attributeValue); + } + + @override + bool focusAsRouteDefault() => focusable?.focusAsRouteDefault() ?? false; +} + +/// Indicates a menu item element. +/// +/// Uses aria menuitem role to convey this semantic information to the element. +/// +/// Screen-readers takes advantage of "aria-label" to describe the visual. +class SemanticMenuItem extends SemanticRole { + SemanticMenuItem(SemanticsObject semanticsObject) + : super.withBasics( + EngineSemanticsRole.menuItem, + semanticsObject, + preferredLabelRepresentation: LabelRepresentation.ariaLabel, + ) { + setAriaRole('menuitem'); + addDisabledBehavior(); + addTappable(); + } + + @override + void update() { + super.update(); + if (semanticsObject.hasExpandedState) { + setAttribute('aria-haspopup', 'menu'); + } else { + removeAttribute('aria-haspopup'); + } + } + + @override + bool focusAsRouteDefault() => focusable?.focusAsRouteDefault() ?? false; +} + +/// Indicates a menu item element with a checkbox. +/// +/// Uses aria menuitemcheckbox role to convey this semantic information to the element. +/// +/// Screen-readers takes advantage of "aria-label" to describe the visual. +class SemanticMenuItemCheckbox extends SemanticRole { + SemanticMenuItemCheckbox(SemanticsObject semanticsObject) + : super.withBasics( + EngineSemanticsRole.menuItemCheckbox, + semanticsObject, + preferredLabelRepresentation: LabelRepresentation.ariaLabel, + ) { + setAriaRole('menuitemcheckbox'); + addCheckedBehavior(); + addDisabledBehavior(); + } + + @override + bool focusAsRouteDefault() => focusable?.focusAsRouteDefault() ?? false; +} + +/// Indicates a menu item element with a radio button. +/// +/// Uses aria menuitemradio role to convey this semantic information to the element. +/// +/// Screen-readers takes advantage of "aria-label" to describe the visual. +class SemanticMenuItemRadio extends SemanticRole { + SemanticMenuItemRadio(SemanticsObject semanticsObject) + : super.withBasics( + EngineSemanticsRole.menuItemRadio, + semanticsObject, + preferredLabelRepresentation: LabelRepresentation.ariaLabel, + ) { + setAriaRole('menuitemradio'); + addCheckedBehavior(); + addDisabledBehavior(); + } + + @override + bool focusAsRouteDefault() => focusable?.focusAsRouteDefault() ?? false; +} diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart index 4e23e4baf6..355b02d9ec 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart @@ -21,6 +21,7 @@ import '../window.dart'; import 'accessibility.dart'; import 'alert.dart'; import 'checkable.dart'; +import 'disable.dart'; import 'expandable.dart'; import 'focusable.dart'; import 'header.dart'; @@ -31,6 +32,7 @@ import 'label_and_value.dart'; import 'link.dart'; import 'list.dart'; import 'live_region.dart'; +import 'menus.dart'; import 'platform_view.dart'; import 'requirable.dart'; import 'route.dart'; @@ -464,6 +466,21 @@ enum EngineSemanticsRole { /// /// Provides a label or a value. generic, + + /// A visible list of items or a widget that can be made to open and close. + menu, + + /// A horizontally displayed [menu] that remains visible. + menuBar, + + /// An option in a set of choices contained by a [menu] or [menuBar]. + menuItem, + + /// An option with a checkbox in a set of choices contained by a [menu] or [menuBar]. + menuItemCheckbox, + + /// An option with a radio button in a set of choices contained by a [menu] or [menuBar]. + menuItemRadio, } /// Responsible for setting the `role` ARIA attribute, for attaching @@ -638,6 +655,14 @@ abstract class SemanticRole { ); } + void addCheckedBehavior() { + addSemanticBehavior(Checkable(semanticsObject, this)); + } + + void addDisabledBehavior() { + addSemanticBehavior(CanDisable(semanticsObject, this)); + } + /// Adds generic functionality for handling taps and clicks. void addTappable() { addSemanticBehavior(Tappable(semanticsObject, this)); @@ -1422,6 +1447,9 @@ class SemanticsObject { /// This field is only meaningful if [hasEnabledState] is true. bool get isEnabled => hasFlag(ui.SemanticsFlag.isEnabled); + /// Whether this object can be in one of "expanded" or "collapsed" state. + bool get hasExpandedState => hasFlag(ui.SemanticsFlag.hasExpandedState); + /// Whether this object represents a vertically scrollable area. bool get isVerticalScrollContainer => hasAction(ui.SemanticsAction.scrollDown) || hasAction(ui.SemanticsAction.scrollUp); @@ -1868,6 +1896,16 @@ class SemanticsObject { return EngineSemanticsRole.columnHeader; case ui.SemanticsRole.radioGroup: return EngineSemanticsRole.radioGroup; + case ui.SemanticsRole.menu: + return EngineSemanticsRole.menu; + case ui.SemanticsRole.menuBar: + return EngineSemanticsRole.menuBar; + case ui.SemanticsRole.menuItem: + return EngineSemanticsRole.menuItem; + case ui.SemanticsRole.menuItemCheckbox: + return EngineSemanticsRole.menuItemCheckbox; + case ui.SemanticsRole.menuItemRadio: + return EngineSemanticsRole.menuItemRadio; case ui.SemanticsRole.alert: return EngineSemanticsRole.alert; case ui.SemanticsRole.status: @@ -1882,9 +1920,6 @@ class SemanticsObject { case ui.SemanticsRole.dragHandle: case ui.SemanticsRole.spinButton: case ui.SemanticsRole.comboBox: - case ui.SemanticsRole.menuBar: - case ui.SemanticsRole.menu: - case ui.SemanticsRole.menuItem: case ui.SemanticsRole.form: case ui.SemanticsRole.tooltip: case ui.SemanticsRole.loadingSpinner: @@ -1948,6 +1983,11 @@ class SemanticsObject { EngineSemanticsRole.cell => SemanticCell(this), EngineSemanticsRole.row => SemanticRow(this), EngineSemanticsRole.columnHeader => SemanticColumnHeader(this), + EngineSemanticsRole.menu => SemanticMenu(this), + EngineSemanticsRole.menuBar => SemanticMenuBar(this), + EngineSemanticsRole.menuItem => SemanticMenuItem(this), + EngineSemanticsRole.menuItemCheckbox => SemanticMenuItemCheckbox(this), + EngineSemanticsRole.menuItemRadio => SemanticMenuItemRadio(this), EngineSemanticsRole.alert => SemanticAlert(this), EngineSemanticsRole.status => SemanticStatus(this), EngineSemanticsRole.generic => GenericRole(this), @@ -2031,6 +2071,14 @@ class SemanticsObject { bool get isCheckable => hasFlag(ui.SemanticsFlag.hasCheckedState) || hasFlag(ui.SemanticsFlag.hasToggledState); + /// If true, this node represents something that can be in a "checked" or + /// state, such as checkboxes, radios, and switches. + bool get isChecked => hasFlag(ui.SemanticsFlag.isChecked); + + /// If true, this node represents something that can be in a "mixed" or + /// state, such as checkboxes. + bool get isMixed => hasFlag(ui.SemanticsFlag.isCheckStateMixed); + /// If true, this node represents something that can be annotated as /// "selected", such as a tab, or an item in a list. /// @@ -2653,6 +2701,8 @@ class EngineSemanticsOwner { SemanticsUpdatePhase get phase => _phase; SemanticsUpdatePhase _phase = SemanticsUpdatePhase.idle; + /// The current semantics tree. + Map get semanticsTree => _semanticsTree; final Map _semanticsTree = {}; final Map identifiersToIds = {}; diff --git a/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart index ce9d9db01a..53d4806bb8 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart @@ -138,6 +138,9 @@ void runSemanticsTests() { group('controlsNodes', () { _testControlsNodes(); }); + group('menu', () { + _testMenus(); + }); group('requirable', () { _testRequirable(); }); @@ -4094,6 +4097,310 @@ void _testTables() { semantics().semanticsEnabled = false; } +void _testMenus() { + test('nodes with menu role', () { + semantics() + ..debugOverrideTimestampFunction(() => _testTime) + ..semanticsEnabled = true; + + SemanticsObject pumpSemantics() { + final SemanticsTester tester = SemanticsTester(owner()); + tester.updateNode( + id: 0, + role: ui.SemanticsRole.menu, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + ); + tester.apply(); + return tester.getSemanticsObject(0); + } + + final SemanticsObject object = pumpSemantics(); + expect(object.semanticRole?.kind, EngineSemanticsRole.menu); + expect(object.element.getAttribute('role'), 'menu'); + }); + + test('menu can have non-immidiate menu item nodes', () { + semantics() + ..debugOverrideTimestampFunction(() => _testTime) + ..semanticsEnabled = true; + + final SemanticsTester tester = SemanticsTester(owner()); + tester.updateNode( + id: 0, + role: ui.SemanticsRole.menu, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + children: [ + tester.updateNode( + id: 1, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + children: [ + tester.updateNode(id: 2, role: ui.SemanticsRole.menuItem), + tester.updateNode(id: 3, role: ui.SemanticsRole.menuItem), + tester.updateNode(id: 4, role: ui.SemanticsRole.menuItem), + ], + ), + ], + ); + tester.apply(); + + final SemanticsObject object = tester.getSemanticsObject(0); + expect( + object.element.getAttribute('aria-owns'), + 'flt-semantic-node-2 flt-semantic-node-3 flt-semantic-node-4', + ); + }); + + test('nested menus have correct menu item nodes', () { + semantics() + ..debugOverrideTimestampFunction(() => _testTime) + ..semanticsEnabled = true; + + final SemanticsTester tester = SemanticsTester(owner()); + tester.updateNode( + id: 0, + role: ui.SemanticsRole.menu, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + children: [ + tester.updateNode( + id: 1, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + children: [ + tester.updateNode(id: 2, role: ui.SemanticsRole.menuItem), + tester.updateNode(id: 3, role: ui.SemanticsRole.menuItem), + tester.updateNode( + id: 4, + role: ui.SemanticsRole.menuItem, + isExpandable: true, + isExpanded: true, + children: [ + tester.updateNode( + id: 5, + role: ui.SemanticsRole.menu, + children: [ + tester.updateNode( + id: 6, + children: [ + tester.updateNode(id: 7, role: ui.SemanticsRole.menuItem), + tester.updateNode(id: 8, role: ui.SemanticsRole.menuItem), + ], + ), + ], + ), + ], + ), + ], + ), + ], + ); + tester.apply(); + + final SemanticsObject object0 = tester.getSemanticsObject(0); + expect( + object0.element.getAttribute('aria-owns'), + 'flt-semantic-node-2 flt-semantic-node-3 flt-semantic-node-4', + ); + final SemanticsObject object1 = tester.getSemanticsObject(5); + expect(object1.element.getAttribute('aria-owns'), 'flt-semantic-node-7 flt-semantic-node-8'); + }); + + test('menu bar can have non-immidiate menu item nodes', () { + semantics() + ..debugOverrideTimestampFunction(() => _testTime) + ..semanticsEnabled = true; + + final SemanticsTester tester = SemanticsTester(owner()); + tester.updateNode( + id: 0, + role: ui.SemanticsRole.menuBar, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + children: [ + tester.updateNode( + id: 1, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + children: [ + tester.updateNode(id: 2, role: ui.SemanticsRole.menuItem), + tester.updateNode(id: 3, role: ui.SemanticsRole.menuItem), + tester.updateNode(id: 4, role: ui.SemanticsRole.menuItem), + ], + ), + ], + ); + tester.apply(); + + final SemanticsObject object = tester.getSemanticsObject(0); + expect( + object.element.getAttribute('aria-owns'), + 'flt-semantic-node-2 flt-semantic-node-3 flt-semantic-node-4', + ); + }); + + test('menu bar and its submenu have correct menu item nodes', () { + semantics() + ..debugOverrideTimestampFunction(() => _testTime) + ..semanticsEnabled = true; + + final SemanticsTester tester = SemanticsTester(owner()); + tester.updateNode( + id: 0, + role: ui.SemanticsRole.menuBar, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + children: [ + tester.updateNode( + id: 1, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + children: [ + tester.updateNode(id: 2, role: ui.SemanticsRole.menuItem), + tester.updateNode(id: 3, role: ui.SemanticsRole.menuItem), + tester.updateNode( + id: 4, + role: ui.SemanticsRole.menuItem, + isExpandable: true, + isExpanded: true, + children: [ + tester.updateNode( + id: 5, + role: ui.SemanticsRole.menu, + children: [ + tester.updateNode( + id: 6, + children: [ + tester.updateNode(id: 7, role: ui.SemanticsRole.menuItem), + tester.updateNode(id: 8, role: ui.SemanticsRole.menuItem), + ], + ), + ], + ), + ], + ), + ], + ), + ], + ); + tester.apply(); + + final SemanticsObject object0 = tester.getSemanticsObject(0); + expect( + object0.element.getAttribute('aria-owns'), + 'flt-semantic-node-2 flt-semantic-node-3 flt-semantic-node-4', + ); + final SemanticsObject object1 = tester.getSemanticsObject(5); + expect(object1.element.getAttribute('aria-owns'), 'flt-semantic-node-7 flt-semantic-node-8'); + }); + + test('nodes with menuitem role', () { + semantics() + ..debugOverrideTimestampFunction(() => _testTime) + ..semanticsEnabled = true; + + SemanticsObject pumpSemantics() { + final SemanticsTester tester = SemanticsTester(owner()); + tester.updateNode( + id: 0, + role: ui.SemanticsRole.menuItem, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + ); + tester.apply(); + return tester.getSemanticsObject(0); + } + + final SemanticsObject object = pumpSemantics(); + expect(object.semanticRole?.kind, EngineSemanticsRole.menuItem); + expect(object.element.getAttribute('role'), 'menuitem'); + }); + + test('nodes with menuitem role and hasPopup attribute', () { + semantics() + ..debugOverrideTimestampFunction(() => _testTime) + ..semanticsEnabled = true; + + SemanticsObject pumpSemantics(bool isExpandable) { + final SemanticsTester tester = SemanticsTester(owner()); + tester.updateNode( + id: 0, + role: ui.SemanticsRole.menuItem, + isExpandable: isExpandable, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + ); + tester.apply(); + return tester.getSemanticsObject(0); + } + + final SemanticsObject object0 = pumpSemantics(true); + expect(object0.semanticRole?.kind, EngineSemanticsRole.menuItem); + expect(object0.element.getAttribute('role'), 'menuitem'); + expect(object0.element.getAttribute('aria-haspopup'), 'menu'); + + final SemanticsObject object1 = pumpSemantics(false); + expect(object1.element.getAttribute('role'), 'menuitem'); + expect(object1.element.getAttribute('aria-haspopup'), isNull); + }); + + test('nodes with menubar role', () { + semantics() + ..debugOverrideTimestampFunction(() => _testTime) + ..semanticsEnabled = true; + + SemanticsObject pumpSemantics() { + final SemanticsTester tester = SemanticsTester(owner()); + tester.updateNode( + id: 0, + role: ui.SemanticsRole.menuBar, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + ); + tester.apply(); + return tester.getSemanticsObject(0); + } + + final SemanticsObject object = pumpSemantics(); + expect(object.semanticRole?.kind, EngineSemanticsRole.menuBar); + expect(object.element.getAttribute('role'), 'menubar'); + }); + + test('nodes with menuitemcheckbox role', () { + semantics() + ..debugOverrideTimestampFunction(() => _testTime) + ..semanticsEnabled = true; + + SemanticsObject pumpSemantics() { + final SemanticsTester tester = SemanticsTester(owner()); + tester.updateNode( + id: 0, + role: ui.SemanticsRole.menuItemCheckbox, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + ); + tester.apply(); + return tester.getSemanticsObject(0); + } + + final SemanticsObject object = pumpSemantics(); + expect(object.semanticRole?.kind, EngineSemanticsRole.menuItemCheckbox); + expect(object.element.getAttribute('role'), 'menuitemcheckbox'); + }); + + test('nodes with menuitemRadio role', () { + semantics() + ..debugOverrideTimestampFunction(() => _testTime) + ..semanticsEnabled = true; + + SemanticsObject pumpSemantics() { + final SemanticsTester tester = SemanticsTester(owner()); + tester.updateNode( + id: 0, + role: ui.SemanticsRole.menuItemRadio, + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + ); + tester.apply(); + return tester.getSemanticsObject(0); + } + + final SemanticsObject object = pumpSemantics(); + expect(object.semanticRole?.kind, EngineSemanticsRole.menuItemRadio); + expect(object.element.getAttribute('role'), 'menuitemradio'); + }); + + semantics().semanticsEnabled = false; +} + void _testLists() { test('nodes with list role', () { semantics() diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 975b7e7876..f9387e686e 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -11,6 +11,7 @@ library; import 'dart:math' as math; +import 'dart:ui'; import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; @@ -230,7 +231,7 @@ class _DropdownMenuItemButtonState extends State<_DropdownMenuItemButton> if (kIsWeb && dropdownMenuItem.enabled) { child = Shortcuts(shortcuts: _webShortcuts, child: child); } - return child; + return Semantics(role: SemanticsRole.menuItem, child: child); } } @@ -332,6 +333,7 @@ class _DropdownMenuState extends State<_DropdownMenu> { getSelectedItemOffset: () => route.getItemOffset(route.selectedIndex), ), child: Semantics( + role: SemanticsRole.menu, scopesRoute: true, namesRoute: true, explicitChildNodes: true, @@ -1303,6 +1305,7 @@ class _DropdownButtonState extends State> with WidgetsBindi late Map> _actionMap; bool _isHovering = false; bool _hasPrimaryFocus = false; + bool _isMenuExpanded = false; // Only used if needed to create _internalNode. FocusNode _createFocusNode() { @@ -1346,6 +1349,7 @@ class _DropdownButtonState extends State> with WidgetsBindi void _removeDropdownRoute() { _dropdownRoute?._dismiss(); + _isMenuExpanded = false; _dropdownRoute = null; _lastOrientation = null; } @@ -1443,6 +1447,7 @@ class _DropdownButtonState extends State> with WidgetsBindi }); widget.onTap?.call(); + _isMenuExpanded = true; } // When isDense is true, reduce the height of this button from _kMenuItemHeight to @@ -1671,6 +1676,7 @@ class _DropdownButtonState extends State> with WidgetsBindi hintIndex != null || (_selectedIndex != null && widget.selectedItemBuilder == null); return Semantics( button: !childHasButtonSemantic, + expanded: _isMenuExpanded, child: Actions(actions: _actionMap, child: result), ); } diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index 4c5fab690f..873cb1f510 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -6,6 +6,8 @@ /// @docImport 'text_button.dart'; library; +import 'dart:ui'; + import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/scheduler.dart'; @@ -399,6 +401,7 @@ class PopupMenuItemState> extends State { return MergeSemantics( child: Semantics( + role: SemanticsRole.menuItem, enabled: widget.enabled, button: true, child: InkWell( @@ -686,6 +689,7 @@ class _PopupMenuState extends State<_PopupMenu> { child: IntrinsicWidth( stepWidth: _kMenuWidthStep, child: Semantics( + role: SemanticsRole.menu, scopesRoute: true, namesRoute: true, explicitChildNodes: true, @@ -1515,6 +1519,7 @@ class PopupMenuButton extends StatefulWidget { /// See [showButtonMenu] for a way to programmatically open the popup menu /// of your button state. class PopupMenuButtonState extends State> { + bool _isMenuExpanded = false; RelativeRect _positionBuilder(BuildContext _, BoxConstraints constraints) { final PopupMenuThemeData popupMenuTheme = PopupMenuTheme.of(context); final RenderBox button = context.findRenderObject()! as RenderBox; @@ -1562,6 +1567,7 @@ class PopupMenuButtonState extends State> { // Only show the menu if there is something to show if (items.isNotEmpty) { widget.onOpened?.call(); + _isMenuExpanded = true; showMenu( context: context, elevation: widget.elevation ?? popupMenuTheme.elevation, @@ -1588,6 +1594,7 @@ class PopupMenuButtonState extends State> { return null; } widget.onSelected?.call(newValue); + _isMenuExpanded = false; }); } } @@ -1634,20 +1641,22 @@ class PopupMenuButtonState extends State> { child: child, ); } - return child; + return Semantics(expanded: _isMenuExpanded, child: child); } - return IconButton( - key: StandardComponentType.moreButton.key, - icon: widget.icon ?? Icon(Icons.adaptive.more), - padding: widget.padding, - splashRadius: widget.splashRadius, - iconSize: widget.iconSize ?? popupMenuTheme.iconSize ?? iconTheme.size, - color: widget.iconColor ?? popupMenuTheme.iconColor ?? iconTheme.color, - tooltip: widget.tooltip ?? MaterialLocalizations.of(context).showMenuTooltip, - onPressed: widget.enabled ? showButtonMenu : null, - enableFeedback: enableFeedback, - style: widget.style, + return Semantics( + child: IconButton( + key: StandardComponentType.moreButton.key, + icon: Semantics(expanded: _isMenuExpanded, child: widget.icon ?? Icon(Icons.adaptive.more)), + padding: widget.padding, + splashRadius: widget.splashRadius, + iconSize: widget.iconSize ?? popupMenuTheme.iconSize ?? iconTheme.size, + color: widget.iconColor ?? popupMenuTheme.iconColor ?? iconTheme.color, + tooltip: widget.tooltip ?? MaterialLocalizations.of(context).showMenuTooltip, + onPressed: widget.enabled ? showButtonMenu : null, + enableFeedback: enableFeedback, + style: widget.style, + ), ); } } diff --git a/packages/flutter/lib/src/semantics/semantics.dart b/packages/flutter/lib/src/semantics/semantics.dart index ff670d4170..cb4d71450c 100644 --- a/packages/flutter/lib/src/semantics/semantics.dart +++ b/packages/flutter/lib/src/semantics/semantics.dart @@ -115,6 +115,11 @@ sealed class _DebugSemanticsRoleChecks { SemanticsRole.row => _semanticsRow, SemanticsRole.columnHeader => _semanticsColumnHeader, SemanticsRole.radioGroup => _semanticsRadioGroup, + SemanticsRole.menu => _semanticsMenu, + SemanticsRole.menuBar => _semanticsMenuBar, + SemanticsRole.menuItem => _semanticsMenuItem, + SemanticsRole.menuItemCheckbox => _semanticsMenuItemCheckbox, + SemanticsRole.menuItemRadio => _semanticsMenuItemRadio, SemanticsRole.alert => _noLiveRegion, SemanticsRole.status => _noLiveRegion, SemanticsRole.list => _noCheckRequired, @@ -125,9 +130,6 @@ sealed class _DebugSemanticsRoleChecks { SemanticsRole.dragHandle => _unimplemented, SemanticsRole.spinButton => _unimplemented, SemanticsRole.comboBox => _unimplemented, - SemanticsRole.menuBar => _unimplemented, - SemanticsRole.menu => _unimplemented, - SemanticsRole.menuItem => _unimplemented, SemanticsRole.form => _unimplemented, SemanticsRole.tooltip => _unimplemented, SemanticsRole.loadingSpinner => _unimplemented, @@ -240,6 +242,68 @@ sealed class _DebugSemanticsRoleChecks { return error; } + static FlutterError? _semanticsMenu(SemanticsNode node) { + if (node.childrenCount < 1) { + return FlutterError('a menu cannot be empty'); + } + + return null; + } + + static FlutterError? _semanticsMenuBar(SemanticsNode node) { + if (node.childrenCount < 1) { + return FlutterError('a menu bar cannot be empty'); + } + + return null; + } + + static FlutterError? _semanticsMenuItem(SemanticsNode node) { + SemanticsNode? currentNode = node; + while (currentNode?.parent != null) { + if (currentNode?.parent?.role == SemanticsRole.menu || + currentNode?.parent?.role == SemanticsRole.menuBar) { + return null; + } + currentNode = currentNode?.parent; + } + return FlutterError('A menu item must be a child of a menu or a menu bar'); + } + + static FlutterError? _semanticsMenuItemCheckbox(SemanticsNode node) { + final SemanticsData data = node.getSemanticsData(); + if (!data.hasFlag(SemanticsFlag.hasCheckedState)) { + return FlutterError('a menu item checkbox must be checkable'); + } + + SemanticsNode? currentNode = node; + while (currentNode?.parent != null) { + if (currentNode?.parent?.role == SemanticsRole.menu || + currentNode?.parent?.role == SemanticsRole.menuBar) { + return null; + } + currentNode = currentNode?.parent; + } + return FlutterError('A menu item checkbox must be a child of a menu or a menu bar'); + } + + static FlutterError? _semanticsMenuItemRadio(SemanticsNode node) { + final SemanticsData data = node.getSemanticsData(); + if (!data.hasFlag(SemanticsFlag.hasCheckedState)) { + return FlutterError('a menu item radio must be checkable'); + } + + SemanticsNode? currentNode = node; + while (currentNode?.parent != null) { + if (currentNode?.parent?.role == SemanticsRole.menu || + currentNode?.parent?.role == SemanticsRole.menuBar) { + return null; + } + currentNode = currentNode?.parent; + } + return FlutterError('A menu item radio must be a child of a menu or a menu bar'); + } + static FlutterError? _noLiveRegion(SemanticsNode node) { final SemanticsData data = node.getSemanticsData(); if (data.hasFlag(SemanticsFlag.isLiveRegion)) { diff --git a/packages/flutter/test/material/dropdown_test.dart b/packages/flutter/test/material/dropdown_test.dart index b77e7fef4d..a2e14adb8d 100644 --- a/packages/flutter/test/material/dropdown_test.dart +++ b/packages/flutter/test/material/dropdown_test.dart @@ -14,10 +14,10 @@ library; import 'dart:math' as math; +import 'dart:ui'; import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; -import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; @@ -1347,6 +1347,7 @@ void main() { tester.getSemantics(find.text('test')), matchesSemantics( isButton: true, + hasExpandedState: true, label: 'test', hasTapAction: true, hasFocusAction: true, @@ -1363,6 +1364,7 @@ void main() { tester.getSemantics(find.text('three')), matchesSemantics( isButton: true, + hasExpandedState: true, label: 'three', hasTapAction: true, hasFocusAction: true, @@ -1390,6 +1392,7 @@ void main() { textDirection: TextDirection.ltr, children: [ TestSemantics( + role: SemanticsRole.menu, flags: [SemanticsFlag.scopesRoute, SemanticsFlag.namesRoute], label: 'Popup menu', children: [ @@ -1399,6 +1402,7 @@ void main() { flags: [SemanticsFlag.hasImplicitScrolling], children: [ TestSemantics( + role: SemanticsRole.menuItem, label: 'one', textDirection: TextDirection.ltr, flags: [ @@ -1413,6 +1417,7 @@ void main() { ], ), TestSemantics( + role: SemanticsRole.menuItem, label: 'two', textDirection: TextDirection.ltr, flags: [ @@ -1426,6 +1431,7 @@ void main() { ], ), TestSemantics( + role: SemanticsRole.menuItem, label: 'three', textDirection: TextDirection.ltr, flags: [ @@ -1439,6 +1445,7 @@ void main() { ], ), TestSemantics( + role: SemanticsRole.menuItem, label: 'four', textDirection: TextDirection.ltr, flags: [ diff --git a/packages/flutter/test/material/popup_menu_test.dart b/packages/flutter/test/material/popup_menu_test.dart index e4793ca4b2..180888d4db 100644 --- a/packages/flutter/test/material/popup_menu_test.dart +++ b/packages/flutter/test/material/popup_menu_test.dart @@ -2,10 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:ui' show DisplayFeature, DisplayFeatureState, DisplayFeatureType, SemanticsFlag; +import 'dart:ui'; import 'package:flutter/foundation.dart'; -import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -1280,6 +1279,7 @@ void main() { TestSemantics( children: [ TestSemantics( + role: SemanticsRole.menu, flags: [SemanticsFlag.scopesRoute, SemanticsFlag.namesRoute], label: 'Popup menu', textDirection: TextDirection.ltr, @@ -1288,6 +1288,7 @@ void main() { flags: [SemanticsFlag.hasImplicitScrolling], children: [ TestSemantics( + role: SemanticsRole.menuItem, flags: [ SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, @@ -1302,6 +1303,7 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( + role: SemanticsRole.menuItem, flags: [ SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, @@ -1316,6 +1318,7 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( + role: SemanticsRole.menuItem, flags: [ SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, @@ -1330,6 +1333,7 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( + role: SemanticsRole.menuItem, flags: [ SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, @@ -1344,6 +1348,7 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( + role: SemanticsRole.menuItem, flags: [ SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, @@ -1419,6 +1424,7 @@ void main() { TestSemantics( children: [ TestSemantics( + role: SemanticsRole.menu, flags: [SemanticsFlag.scopesRoute, SemanticsFlag.namesRoute], label: 'Popup menu', textDirection: TextDirection.ltr, @@ -1427,6 +1433,7 @@ void main() { flags: [SemanticsFlag.hasImplicitScrolling], children: [ TestSemantics( + role: SemanticsRole.menuItem, flags: [ SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, @@ -1499,6 +1506,7 @@ void main() { TestSemantics( children: [ TestSemantics( + role: SemanticsRole.menu, flags: [SemanticsFlag.scopesRoute, SemanticsFlag.namesRoute], label: 'Popup menu', textDirection: TextDirection.ltr, @@ -1507,6 +1515,7 @@ void main() { flags: [SemanticsFlag.hasImplicitScrolling], children: [ TestSemantics( + role: SemanticsRole.menuItem, flags: [ SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, @@ -1521,6 +1530,7 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( + role: SemanticsRole.menuItem, flags: [ SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, @@ -1530,6 +1540,7 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( + role: SemanticsRole.menuItem, flags: [ SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, @@ -1544,6 +1555,7 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( + role: SemanticsRole.menuItem, flags: [ SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, @@ -1558,6 +1570,7 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( + role: SemanticsRole.menuItem, flags: [ SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, @@ -2433,11 +2446,16 @@ void main() { child: Material( child: MouseRegion( cursor: SystemMouseCursors.forbidden, - child: PopupMenuItem( - key: key, - mouseCursor: SystemMouseCursors.text, - value: 1, - child: Container(), + // The [SemanticsRole.menu] is added here to make sure + // [PopupMenuItem]'s parent role is menu. + child: Semantics( + role: SemanticsRole.menu, + child: PopupMenuItem( + key: key, + mouseCursor: SystemMouseCursors.text, + value: 1, + child: Container(), + ), ), ), ), @@ -2468,7 +2486,12 @@ void main() { child: Material( child: MouseRegion( cursor: SystemMouseCursors.forbidden, - child: PopupMenuItem(key: key, value: 1, child: Container()), + // The [SemanticsRole.menu] is added here to make sure + // [PopupMenuItem]'s parent role is menu. + child: Semantics( + role: SemanticsRole.menu, + child: PopupMenuItem(key: key, value: 1, child: Container()), + ), ), ), ), @@ -2490,7 +2513,12 @@ void main() { child: Material( child: MouseRegion( cursor: SystemMouseCursors.forbidden, - child: PopupMenuItem(key: key, value: 1, enabled: false, child: Container()), + // The [SemanticsRole.menu] is added here to make sure + // [PopupMenuItem]'s parent role is menu. + child: Semantics( + role: SemanticsRole.menu, + child: PopupMenuItem(key: key, value: 1, enabled: false, child: Container()), + ), ), ), ), @@ -2517,11 +2545,16 @@ void main() { child: Material( child: MouseRegion( cursor: SystemMouseCursors.forbidden, - child: CheckedPopupMenuItem( - key: key, - mouseCursor: SystemMouseCursors.text, - value: 1, - child: Container(), + // The [SemanticsRole.menu] is added here to make sure + // [CheckedPopupMenuItem]'s parent role is menu. + child: Semantics( + role: SemanticsRole.menu, + child: CheckedPopupMenuItem( + key: key, + mouseCursor: SystemMouseCursors.text, + value: 1, + child: Container(), + ), ), ), ), @@ -2553,7 +2586,12 @@ void main() { child: Material( child: MouseRegion( cursor: SystemMouseCursors.forbidden, - child: CheckedPopupMenuItem(key: key, value: 1, child: Container()), + // The [SemanticsRole.menu] is added here to make sure + // [CheckedPopupMenuItem]'s parent role is menu. + child: Semantics( + role: SemanticsRole.menu, + child: CheckedPopupMenuItem(key: key, value: 1, child: Container()), + ), ), ), ), @@ -2575,11 +2613,16 @@ void main() { child: Material( child: MouseRegion( cursor: SystemMouseCursors.forbidden, - child: CheckedPopupMenuItem( - key: key, - value: 1, - enabled: false, - child: Container(), + // The [SemanticsRole.menu] is added here to make sure + // [CheckedPopupMenuItem]'s parent role is menu. + child: Semantics( + role: SemanticsRole.menu, + child: CheckedPopupMenuItem( + key: key, + value: 1, + enabled: false, + child: Container(), + ), ), ), ), diff --git a/packages/flutter/test/widgets/semantics_role_checks_test.dart b/packages/flutter/test/widgets/semantics_role_checks_test.dart index fefeb4f9eb..aea1c8567d 100644 --- a/packages/flutter/test/widgets/semantics_role_checks_test.dart +++ b/packages/flutter/test/widgets/semantics_role_checks_test.dart @@ -300,6 +300,280 @@ void main() { }); }); + group('menu', () { + testWidgets('failure case, empty child', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menu, + child: const ExcludeSemantics(child: Text('something')), + ), + ), + ); + final Object? exception = tester.takeException(); + expect(exception, isFlutterError); + final FlutterError error = exception! as FlutterError; + expect(error.message, 'a menu cannot be empty'); + }); + + testWidgets('Success case', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menu, + explicitChildNodes: true, + child: Semantics( + role: SemanticsRole.menuItem, + selected: false, + onTap: () {}, + child: const Text('some child'), + ), + ), + ), + ); + expect(tester.takeException(), isNull); + }); + }); + + group('menuBar', () { + testWidgets('failure case, empty child', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menuBar, + child: const ExcludeSemantics(child: Text('something')), + ), + ), + ); + final Object? exception = tester.takeException(); + expect(exception, isFlutterError); + final FlutterError error = exception! as FlutterError; + expect(error.message, 'a menu bar cannot be empty'); + }); + + testWidgets('Success case', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menuBar, + explicitChildNodes: true, + child: Semantics( + role: SemanticsRole.menuItem, + selected: false, + onTap: () {}, + child: const Text('some child'), + ), + ), + ), + ); + expect(tester.takeException(), isNull); + }); + }); + + group('menuItem', () { + testWidgets('failure case, no menu or menuBar as ancester', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menuItem, + child: const ExcludeSemantics(child: Text('something')), + ), + ), + ); + final Object? exception = tester.takeException(); + expect(exception, isFlutterError); + final FlutterError error = exception! as FlutterError; + expect(error.message, 'A menu item must be a child of a menu or a menu bar'); + }); + + testWidgets('Success case with menu as an ancester', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menu, + explicitChildNodes: true, + child: Semantics( + role: SemanticsRole.menuItem, + onTap: () {}, + child: const Text('some child'), + ), + ), + ), + ); + expect(tester.takeException(), isNull); + }); + + testWidgets('Success case with menuBar as an ancester', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menuBar, + explicitChildNodes: true, + child: Semantics( + role: SemanticsRole.menuItem, + onTap: () {}, + child: const Text('some child'), + ), + ), + ), + ); + expect(tester.takeException(), isNull); + }); + }); + + group('menuItemCheckbox', () { + testWidgets('failure case, no checked flag', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menuItemCheckbox, + child: const ExcludeSemantics(child: Text('something')), + ), + ), + ); + final Object? exception = tester.takeException(); + expect(exception, isFlutterError); + final FlutterError error = exception! as FlutterError; + expect(error.message, 'a menu item checkbox must be checkable'); + }); + + testWidgets('failure case, no menu or menuBar as its ancestor', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menuItemCheckbox, + checked: false, + child: const ExcludeSemantics(child: Text('something')), + ), + ), + ); + final Object? exception = tester.takeException(); + expect(exception, isFlutterError); + final FlutterError error = exception! as FlutterError; + expect(error.message, 'A menu item checkbox must be a child of a menu or a menu bar'); + }); + + testWidgets('Success case with menu as an ancester', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menu, + explicitChildNodes: true, + child: Semantics( + role: SemanticsRole.menuItemCheckbox, + checked: true, + onTap: () {}, + child: const Text('some child'), + ), + ), + ), + ); + expect(tester.takeException(), isNull); + }); + + testWidgets('Success case with menuBar as an ancester', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menuBar, + explicitChildNodes: true, + child: Semantics( + role: SemanticsRole.menuItemCheckbox, + checked: false, + onTap: () {}, + child: const Text('some child'), + ), + ), + ), + ); + expect(tester.takeException(), isNull); + }); + }); + + group('menuItemRadio', () { + testWidgets('failure case, no checked flag', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menuItemRadio, + child: const ExcludeSemantics(child: Text('something')), + ), + ), + ); + final Object? exception = tester.takeException(); + expect(exception, isFlutterError); + final FlutterError error = exception! as FlutterError; + expect(error.message, 'a menu item radio must be checkable'); + }); + + testWidgets('failure case, no menu or menuBar as its ancestor', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menuItemRadio, + checked: false, + child: const ExcludeSemantics(child: Text('something')), + ), + ), + ); + final Object? exception = tester.takeException(); + expect(exception, isFlutterError); + final FlutterError error = exception! as FlutterError; + expect(error.message, 'A menu item radio must be a child of a menu or a menu bar'); + }); + + testWidgets('Success case with menu as an ancester', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menu, + explicitChildNodes: true, + child: Semantics( + role: SemanticsRole.menuItemRadio, + checked: true, + onTap: () {}, + child: const Text('some child'), + ), + ), + ), + ); + expect(tester.takeException(), isNull); + }); + + testWidgets('Success case with menuBar as an ancester', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Semantics( + role: SemanticsRole.menuBar, + explicitChildNodes: true, + child: Semantics( + role: SemanticsRole.menuItemRadio, + checked: false, + onTap: () {}, + child: const Text('some child'), + ), + ), + ), + ); + expect(tester.takeException(), isNull); + }); + }); + group('alert and status', () { testWidgets('failure case, alert and live region', (WidgetTester tester) async { await tester.pumpWidget(