Add aria menu roles to menu-related widgets (#164741)

Related to https://github.com/flutter/flutter/issues/157177

This PR is to add the following changes:
* Implement `SemanticsRole.menu`, `SemanticsRole.menuItem`,
`SemanticsRole.menuBar`, `SemanticsRole.menuItemCheckbox`, and
`SemanticsRole.menuItemRadio`.
* Add `SemanticsRole.menu` role in ~`MenuAnchor`~, `DropdownButton`, and
`PopupMenuButton`
~* Add `SemanticsRole.menuBar` role to `MenuBar`~
* Add `SemanticsRole.menuItem` to ~`MenuItemButton`~, `DropdownMenuItem`
and `PopupMenuItem`
~* Add `SemanticsRole.menuItemCheckbox` to `CheckboxMenuButton`~
~* Add `SemanticsRole.menuItemRadio` to `RadioMenuButton`~

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [x] 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].
- [x] 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].

<!-- Links -->
[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
This commit is contained in:
Qun Cheng
2025-03-19 13:33:15 -07:00
committed by GitHub
parent bf3826d2ce
commit 32b34ff99e
17 changed files with 1118 additions and 61 deletions

View File

@@ -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

View File

@@ -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.
///

View File

@@ -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

View File

@@ -283,6 +283,8 @@ enum SemanticsRole {
menuBar,
menu,
menuItem,
menuItemCheckbox,
menuItemRadio,
list,
listItem,
form,

View File

@@ -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';

View File

@@ -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';

View File

@@ -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');
}
}
}
}

View File

@@ -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');
}
}
}
}

View File

@@ -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<int, SemanticsObject> tree = semanticsObject.owner.semanticsTree;
final List<int> ids = [];
final int root = semanticsObject.id;
final List<int> 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<int, SemanticsObject> tree = semanticsObject.owner.semanticsTree;
final List<int> ids = [];
final int root = semanticsObject.id;
final List<int> 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;
}

View File

@@ -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<int, SemanticsObject> get semanticsTree => _semanticsTree;
final Map<int, SemanticsObject> _semanticsTree = <int, SemanticsObject>{};
final Map<String, int> identifiersToIds = <String, int>{};

View File

@@ -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: <SemanticsNodeUpdate>[
tester.updateNode(
id: 1,
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
children: <SemanticsNodeUpdate>[
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: <SemanticsNodeUpdate>[
tester.updateNode(
id: 1,
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
children: <SemanticsNodeUpdate>[
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: <SemanticsNodeUpdate>[
tester.updateNode(
id: 5,
role: ui.SemanticsRole.menu,
children: <SemanticsNodeUpdate>[
tester.updateNode(
id: 6,
children: <SemanticsNodeUpdate>[
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: <SemanticsNodeUpdate>[
tester.updateNode(
id: 1,
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
children: <SemanticsNodeUpdate>[
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: <SemanticsNodeUpdate>[
tester.updateNode(
id: 1,
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
children: <SemanticsNodeUpdate>[
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: <SemanticsNodeUpdate>[
tester.updateNode(
id: 5,
role: ui.SemanticsRole.menu,
children: <SemanticsNodeUpdate>[
tester.updateNode(
id: 6,
children: <SemanticsNodeUpdate>[
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()