Adds semantics role checks (#162290)
Adds test and error detection system for semantics roles. ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- 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:
@@ -209,12 +209,9 @@ class Tab extends StatelessWidget implements PreferredSizeWidget {
|
||||
);
|
||||
}
|
||||
|
||||
return Semantics(
|
||||
role: SemanticsRole.tab,
|
||||
child: SizedBox(
|
||||
height: height ?? calculatedHeight,
|
||||
child: Center(widthFactor: 1.0, child: label),
|
||||
),
|
||||
return SizedBox(
|
||||
height: height ?? calculatedHeight,
|
||||
child: Center(widthFactor: 1.0, child: label),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1909,6 +1906,7 @@ class _TabBarState extends State<TabBar> {
|
||||
children: <Widget>[
|
||||
wrappedTabs[index],
|
||||
Semantics(
|
||||
role: SemanticsRole.tab,
|
||||
selected: index == _currentIndex,
|
||||
label:
|
||||
kIsWeb ? null : localizations.tabLabel(tabIndex: index + 1, tabCount: tabCount),
|
||||
@@ -1924,6 +1922,8 @@ class _TabBarState extends State<TabBar> {
|
||||
|
||||
Widget tabBar = Semantics(
|
||||
role: SemanticsRole.tabBar,
|
||||
container: true,
|
||||
explicitChildNodes: true,
|
||||
child: CustomPaint(
|
||||
painter: _indicatorPainter,
|
||||
child: _TabStyle(
|
||||
|
||||
@@ -101,6 +101,62 @@ final int _kUnblockedUserActions =
|
||||
SemanticsAction.didGainAccessibilityFocus.index |
|
||||
SemanticsAction.didLoseAccessibilityFocus.index;
|
||||
|
||||
/// Function signature for checks in [DebugSemanticsRoleChecks.kChecks].
|
||||
///
|
||||
/// The check is run against any `node` that is sent to the platform.
|
||||
///
|
||||
/// To access the flags and properties, one should call the
|
||||
/// [SemanticsNode.getSemanticsData].
|
||||
@visibleForTesting
|
||||
typedef DebugSemanticsRoleCheck = FlutterError? Function(SemanticsNode node);
|
||||
|
||||
/// A static class to conduct semantics role checks.
|
||||
///
|
||||
/// When adding a new [SemanticsRole], one must also add a corresponding check
|
||||
/// to [kChecks].
|
||||
@visibleForTesting
|
||||
sealed class DebugSemanticsRoleChecks {
|
||||
/// A map to map each [SemanticsRole] to its check.
|
||||
static const Map<SemanticsRole, DebugSemanticsRoleCheck> kChecks =
|
||||
<SemanticsRole, DebugSemanticsRoleCheck>{
|
||||
SemanticsRole.none: _noCheckRequired,
|
||||
SemanticsRole.tab: _semanticsTab,
|
||||
SemanticsRole.tabBar: _semanticsTabBar,
|
||||
SemanticsRole.tabPanel: _noCheckRequired,
|
||||
};
|
||||
|
||||
static FlutterError? _checkSemanticsData(SemanticsNode node) => kChecks[node.role]!(node);
|
||||
|
||||
static FlutterError? _noCheckRequired(SemanticsNode node) => null;
|
||||
|
||||
static FlutterError? _semanticsTab(SemanticsNode node) {
|
||||
final SemanticsData data = node.getSemanticsData();
|
||||
if (!data.hasFlag(SemanticsFlag.hasSelectedState)) {
|
||||
return FlutterError('A tab needs selected states');
|
||||
}
|
||||
|
||||
if (!node.areUserActionsBlocked && !data.hasAction(SemanticsAction.tap)) {
|
||||
return FlutterError('A tab must have a tap action');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static FlutterError? _semanticsTabBar(SemanticsNode node) {
|
||||
if (node.childrenCount < 1) {
|
||||
return FlutterError('a TabBar cannot be empty');
|
||||
}
|
||||
FlutterError? error;
|
||||
node.visitChildren((SemanticsNode child) {
|
||||
if (child.getSemanticsData().role != SemanticsRole.tab) {
|
||||
error = FlutterError('Children of TabBar must have the tab role');
|
||||
}
|
||||
return error == null;
|
||||
});
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
/// A tag for a [SemanticsNode].
|
||||
///
|
||||
/// Tags can be interpreted by the parent of a [SemanticsNode]
|
||||
@@ -3003,6 +3059,13 @@ class SemanticsNode with DiagnosticableTreeMixin {
|
||||
void _addToUpdate(SemanticsUpdateBuilder builder, Set<int> customSemanticsActionIdsUpdate) {
|
||||
assert(_dirty);
|
||||
final SemanticsData data = getSemanticsData();
|
||||
assert(() {
|
||||
final FlutterError? error = DebugSemanticsRoleChecks._checkSemanticsData(this);
|
||||
if (error != null) {
|
||||
throw error;
|
||||
}
|
||||
return true;
|
||||
}());
|
||||
final Int32List childrenInTraversalOrder;
|
||||
final Int32List childrenInHitTestOrder;
|
||||
if (!hasChildren || mergeAllDescendantsIntoThisNode) {
|
||||
|
||||
@@ -2162,7 +2162,7 @@ void main() {
|
||||
|
||||
// Regression test for https://github.com/flutter/flutter/issues/158158.
|
||||
testWidgets('SliverAppBar should update TabBar before TabBar build', (WidgetTester tester) async {
|
||||
final List<Tab> tabs = <Tab>[];
|
||||
final List<Tab> tabs = <Tab>[const Tab(text: 'initial tab')];
|
||||
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
@@ -2179,7 +2179,7 @@ void main() {
|
||||
child: const Text('Add Tab'),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
tabs.add(Tab(text: 'Tab ${tabs.length + 1}'));
|
||||
tabs.add(Tab(text: 'Tab ${tabs.length}'));
|
||||
});
|
||||
},
|
||||
),
|
||||
@@ -2195,7 +2195,8 @@ void main() {
|
||||
),
|
||||
);
|
||||
|
||||
// Initializes with zero tabs.
|
||||
// Initializes with only initial tabs.
|
||||
expect(find.text('initial tab'), findsOneWidget);
|
||||
expect(find.text('Tab 1'), findsNothing);
|
||||
expect(find.text('Tab 2'), findsNothing);
|
||||
|
||||
|
||||
@@ -6002,39 +6002,44 @@ void main() {
|
||||
label: 'Tab 1 of 2',
|
||||
id: 1,
|
||||
rect: TestSemantics.fullScreen,
|
||||
role: SemanticsRole.tabBar,
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
label: 'TAB1${kIsWeb ? '' : '\nTab 1 of 2'}',
|
||||
flags: <SemanticsFlag>[
|
||||
SemanticsFlag.isFocusable,
|
||||
SemanticsFlag.isSelected,
|
||||
SemanticsFlag.hasSelectedState,
|
||||
],
|
||||
id: 2,
|
||||
rect: TestSemantics.fullScreen,
|
||||
actions: 1 | SemanticsAction.focus.index,
|
||||
role: SemanticsRole.tab,
|
||||
role: SemanticsRole.tabBar,
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
label: 'TAB1${kIsWeb ? '' : '\nTab 1 of 2'}',
|
||||
flags: <SemanticsFlag>[
|
||||
SemanticsFlag.isFocusable,
|
||||
SemanticsFlag.isSelected,
|
||||
SemanticsFlag.hasSelectedState,
|
||||
],
|
||||
id: 3,
|
||||
rect: TestSemantics.fullScreen,
|
||||
actions: 1 | SemanticsAction.focus.index,
|
||||
role: SemanticsRole.tab,
|
||||
),
|
||||
TestSemantics(
|
||||
label: 'TAB2${kIsWeb ? '' : '\nTab 2 of 2'}',
|
||||
flags: <SemanticsFlag>[SemanticsFlag.isFocusable, SemanticsFlag.hasSelectedState],
|
||||
id: 4,
|
||||
rect: TestSemantics.fullScreen,
|
||||
actions: <SemanticsAction>[SemanticsAction.tap, SemanticsAction.focus],
|
||||
role: SemanticsRole.tab,
|
||||
),
|
||||
],
|
||||
),
|
||||
TestSemantics(
|
||||
label: 'TAB2${kIsWeb ? '' : '\nTab 2 of 2'}',
|
||||
flags: <SemanticsFlag>[SemanticsFlag.isFocusable, SemanticsFlag.hasSelectedState],
|
||||
id: 3,
|
||||
rect: TestSemantics.fullScreen,
|
||||
actions: <SemanticsAction>[SemanticsAction.tap, SemanticsAction.focus],
|
||||
role: SemanticsRole.tab,
|
||||
),
|
||||
TestSemantics(
|
||||
id: 4,
|
||||
id: 5,
|
||||
rect: TestSemantics.fullScreen,
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
id: 6,
|
||||
id: 7,
|
||||
rect: TestSemantics.fullScreen,
|
||||
actions: <SemanticsAction>[SemanticsAction.scrollLeft],
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
id: 5,
|
||||
id: 6,
|
||||
rect: TestSemantics.fullScreen,
|
||||
label: 'PAGE1',
|
||||
role: SemanticsRole.tabPanel,
|
||||
|
||||
112
packages/flutter/test/widgets/semantics_role_checks_test.dart
Normal file
112
packages/flutter/test/widgets/semantics_role_checks_test.dart
Normal file
@@ -0,0 +1,112 @@
|
||||
// Copyright 2014 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 'dart:ui';
|
||||
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
test('All semantics roles has checks', () {
|
||||
expect(SemanticsRole.values.toSet(), DebugSemanticsRoleChecks.kChecks.keys.toSet());
|
||||
});
|
||||
|
||||
group('tab', () {
|
||||
testWidgets('failure case, empty', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Semantics(role: SemanticsRole.tab, child: const Text('a tab')),
|
||||
),
|
||||
);
|
||||
final Object? exception = tester.takeException();
|
||||
expect(exception, isFlutterError);
|
||||
final FlutterError error = exception! as FlutterError;
|
||||
expect(error.message, 'A tab needs selected states');
|
||||
});
|
||||
|
||||
testWidgets('failure case, no tap', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Semantics(role: SemanticsRole.tab, selected: false, child: const Text('a tab')),
|
||||
),
|
||||
);
|
||||
final Object? exception = tester.takeException();
|
||||
expect(exception, isFlutterError);
|
||||
final FlutterError error = exception! as FlutterError;
|
||||
expect(error.message, 'A tab must have a tap action');
|
||||
});
|
||||
|
||||
testWidgets('success case', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Semantics(
|
||||
role: SemanticsRole.tab,
|
||||
selected: false,
|
||||
onTap: () {},
|
||||
child: const Text('a tab'),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('tabBar', () {
|
||||
testWidgets('failure case, empty child', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Semantics(
|
||||
role: SemanticsRole.tabBar,
|
||||
child: const ExcludeSemantics(child: Text('something')),
|
||||
),
|
||||
),
|
||||
);
|
||||
final Object? exception = tester.takeException();
|
||||
expect(exception, isFlutterError);
|
||||
final FlutterError error = exception! as FlutterError;
|
||||
expect(error.message, 'a TabBar cannot be empty');
|
||||
});
|
||||
|
||||
testWidgets('failure case, non tab child', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Semantics(
|
||||
role: SemanticsRole.tabBar,
|
||||
explicitChildNodes: true,
|
||||
child: Semantics(child: const Text('some child')),
|
||||
),
|
||||
),
|
||||
);
|
||||
final Object? exception = tester.takeException();
|
||||
expect(exception, isFlutterError);
|
||||
final FlutterError error = exception! as FlutterError;
|
||||
expect(error.message, 'Children of TabBar must have the tab role');
|
||||
});
|
||||
|
||||
testWidgets('Success case', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Semantics(
|
||||
role: SemanticsRole.tabBar,
|
||||
explicitChildNodes: true,
|
||||
child: Semantics(
|
||||
role: SemanticsRole.tab,
|
||||
selected: false,
|
||||
onTap: () {},
|
||||
child: const Text('some child'),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user