[semantics] Use a switch over a map to enumerate checks (#162424)
- eliminates a const map and a typedef - leverage switch + enum language check to ensure coverage - eliminates a runtime null check - Can make the check class private
This commit is contained in:
parent
2c145cea51
commit
61865ec959
@ -101,31 +101,14 @@ 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);
|
||||
sealed class _DebugSemanticsRoleChecks {
|
||||
static FlutterError? _checkSemanticsData(SemanticsNode node) => switch (node.role) {
|
||||
SemanticsRole.none => _noCheckRequired,
|
||||
SemanticsRole.tab => _semanticsTab,
|
||||
SemanticsRole.tabBar => _semanticsTabBar,
|
||||
SemanticsRole.tabPanel => _noCheckRequired,
|
||||
}(node);
|
||||
|
||||
static FlutterError? _noCheckRequired(SemanticsNode node) => null;
|
||||
|
||||
@ -3060,7 +3043,7 @@ class SemanticsNode with DiagnosticableTreeMixin {
|
||||
assert(_dirty);
|
||||
final SemanticsData data = getSemanticsData();
|
||||
assert(() {
|
||||
final FlutterError? error = DebugSemanticsRoleChecks._checkSemanticsData(this);
|
||||
final FlutterError? error = _DebugSemanticsRoleChecks._checkSemanticsData(this);
|
||||
if (error != null) {
|
||||
throw error;
|
||||
}
|
||||
|
@ -4,15 +4,10 @@
|
||||
|
||||
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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user