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()

View File

@@ -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<T> extends State<_DropdownMenuItemButton<T>>
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<T> extends State<_DropdownMenu<T>> {
getSelectedItemOffset: () => route.getItemOffset(route.selectedIndex),
),
child: Semantics(
role: SemanticsRole.menu,
scopesRoute: true,
namesRoute: true,
explicitChildNodes: true,
@@ -1303,6 +1305,7 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
late Map<Type, Action<Intent>> _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<T> extends State<DropdownButton<T>> with WidgetsBindi
void _removeDropdownRoute() {
_dropdownRoute?._dismiss();
_isMenuExpanded = false;
_dropdownRoute = null;
_lastOrientation = null;
}
@@ -1443,6 +1447,7 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> 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<T> extends State<DropdownButton<T>> with WidgetsBindi
hintIndex != null || (_selectedIndex != null && widget.selectedItemBuilder == null);
return Semantics(
button: !childHasButtonSemantic,
expanded: _isMenuExpanded,
child: Actions(actions: _actionMap, child: result),
);
}

View File

@@ -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<T, W extends PopupMenuItem<T>> extends State<W> {
return MergeSemantics(
child: Semantics(
role: SemanticsRole.menuItem,
enabled: widget.enabled,
button: true,
child: InkWell(
@@ -686,6 +689,7 @@ class _PopupMenuState<T> extends State<_PopupMenu<T>> {
child: IntrinsicWidth(
stepWidth: _kMenuWidthStep,
child: Semantics(
role: SemanticsRole.menu,
scopesRoute: true,
namesRoute: true,
explicitChildNodes: true,
@@ -1515,6 +1519,7 @@ class PopupMenuButton<T> extends StatefulWidget {
/// See [showButtonMenu] for a way to programmatically open the popup menu
/// of your button state.
class PopupMenuButtonState<T> extends State<PopupMenuButton<T>> {
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<T> extends State<PopupMenuButton<T>> {
// Only show the menu if there is something to show
if (items.isNotEmpty) {
widget.onOpened?.call();
_isMenuExpanded = true;
showMenu<T?>(
context: context,
elevation: widget.elevation ?? popupMenuTheme.elevation,
@@ -1588,6 +1594,7 @@ class PopupMenuButtonState<T> extends State<PopupMenuButton<T>> {
return null;
}
widget.onSelected?.call(newValue);
_isMenuExpanded = false;
});
}
}
@@ -1634,20 +1641,22 @@ class PopupMenuButtonState<T> extends State<PopupMenuButton<T>> {
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,
),
);
}
}

View File

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

View File

@@ -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>[
TestSemantics(
role: SemanticsRole.menu,
flags: <SemanticsFlag>[SemanticsFlag.scopesRoute, SemanticsFlag.namesRoute],
label: 'Popup menu',
children: <TestSemantics>[
@@ -1399,6 +1402,7 @@ void main() {
flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling],
children: <TestSemantics>[
TestSemantics(
role: SemanticsRole.menuItem,
label: 'one',
textDirection: TextDirection.ltr,
flags: <SemanticsFlag>[
@@ -1413,6 +1417,7 @@ void main() {
],
),
TestSemantics(
role: SemanticsRole.menuItem,
label: 'two',
textDirection: TextDirection.ltr,
flags: <SemanticsFlag>[
@@ -1426,6 +1431,7 @@ void main() {
],
),
TestSemantics(
role: SemanticsRole.menuItem,
label: 'three',
textDirection: TextDirection.ltr,
flags: <SemanticsFlag>[
@@ -1439,6 +1445,7 @@ void main() {
],
),
TestSemantics(
role: SemanticsRole.menuItem,
label: 'four',
textDirection: TextDirection.ltr,
flags: <SemanticsFlag>[

View File

@@ -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>[
TestSemantics(
role: SemanticsRole.menu,
flags: <SemanticsFlag>[SemanticsFlag.scopesRoute, SemanticsFlag.namesRoute],
label: 'Popup menu',
textDirection: TextDirection.ltr,
@@ -1288,6 +1288,7 @@ void main() {
flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling],
children: <TestSemantics>[
TestSemantics(
role: SemanticsRole.menuItem,
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.hasEnabledState,
@@ -1302,6 +1303,7 @@ void main() {
textDirection: TextDirection.ltr,
),
TestSemantics(
role: SemanticsRole.menuItem,
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.hasEnabledState,
@@ -1316,6 +1318,7 @@ void main() {
textDirection: TextDirection.ltr,
),
TestSemantics(
role: SemanticsRole.menuItem,
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.hasEnabledState,
@@ -1330,6 +1333,7 @@ void main() {
textDirection: TextDirection.ltr,
),
TestSemantics(
role: SemanticsRole.menuItem,
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.hasEnabledState,
@@ -1344,6 +1348,7 @@ void main() {
textDirection: TextDirection.ltr,
),
TestSemantics(
role: SemanticsRole.menuItem,
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.hasEnabledState,
@@ -1419,6 +1424,7 @@ void main() {
TestSemantics(
children: <TestSemantics>[
TestSemantics(
role: SemanticsRole.menu,
flags: <SemanticsFlag>[SemanticsFlag.scopesRoute, SemanticsFlag.namesRoute],
label: 'Popup menu',
textDirection: TextDirection.ltr,
@@ -1427,6 +1433,7 @@ void main() {
flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling],
children: <TestSemantics>[
TestSemantics(
role: SemanticsRole.menuItem,
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.hasEnabledState,
@@ -1499,6 +1506,7 @@ void main() {
TestSemantics(
children: <TestSemantics>[
TestSemantics(
role: SemanticsRole.menu,
flags: <SemanticsFlag>[SemanticsFlag.scopesRoute, SemanticsFlag.namesRoute],
label: 'Popup menu',
textDirection: TextDirection.ltr,
@@ -1507,6 +1515,7 @@ void main() {
flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling],
children: <TestSemantics>[
TestSemantics(
role: SemanticsRole.menuItem,
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.hasEnabledState,
@@ -1521,6 +1530,7 @@ void main() {
textDirection: TextDirection.ltr,
),
TestSemantics(
role: SemanticsRole.menuItem,
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.hasEnabledState,
@@ -1530,6 +1540,7 @@ void main() {
textDirection: TextDirection.ltr,
),
TestSemantics(
role: SemanticsRole.menuItem,
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.hasEnabledState,
@@ -1544,6 +1555,7 @@ void main() {
textDirection: TextDirection.ltr,
),
TestSemantics(
role: SemanticsRole.menuItem,
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.hasEnabledState,
@@ -1558,6 +1570,7 @@ void main() {
textDirection: TextDirection.ltr,
),
TestSemantics(
role: SemanticsRole.menuItem,
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.hasEnabledState,
@@ -2433,11 +2446,16 @@ void main() {
child: Material(
child: MouseRegion(
cursor: SystemMouseCursors.forbidden,
child: PopupMenuItem<int>(
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<int>(
key: key,
mouseCursor: SystemMouseCursors.text,
value: 1,
child: Container(),
),
),
),
),
@@ -2468,7 +2486,12 @@ void main() {
child: Material(
child: MouseRegion(
cursor: SystemMouseCursors.forbidden,
child: PopupMenuItem<int>(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<int>(key: key, value: 1, child: Container()),
),
),
),
),
@@ -2490,7 +2513,12 @@ void main() {
child: Material(
child: MouseRegion(
cursor: SystemMouseCursors.forbidden,
child: PopupMenuItem<int>(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<int>(key: key, value: 1, enabled: false, child: Container()),
),
),
),
),
@@ -2517,11 +2545,16 @@ void main() {
child: Material(
child: MouseRegion(
cursor: SystemMouseCursors.forbidden,
child: CheckedPopupMenuItem<int>(
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<int>(
key: key,
mouseCursor: SystemMouseCursors.text,
value: 1,
child: Container(),
),
),
),
),
@@ -2553,7 +2586,12 @@ void main() {
child: Material(
child: MouseRegion(
cursor: SystemMouseCursors.forbidden,
child: CheckedPopupMenuItem<int>(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<int>(key: key, value: 1, child: Container()),
),
),
),
),
@@ -2575,11 +2613,16 @@ void main() {
child: Material(
child: MouseRegion(
cursor: SystemMouseCursors.forbidden,
child: CheckedPopupMenuItem<int>(
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<int>(
key: key,
value: 1,
enabled: false,
child: Container(),
),
),
),
),

View File

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