107866: Add support for verifying SemanticsNode ordering in widget tests (#113133)

This commit is contained in:
pdblasi-google
2022-10-26 13:17:15 -07:00
committed by GitHub
parent e334ac1122
commit 671c532019
3 changed files with 524 additions and 155 deletions

View File

@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:collection/collection.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
@@ -20,136 +21,6 @@ class TestDragData {
}
void main() {
group('getSemanticsData', () {
testWidgets('throws when there are no semantics', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: Text('hello'),
),
),
);
expect(() => tester.getSemantics(find.text('hello')), throwsStateError);
}, semanticsEnabled: false);
testWidgets('throws when there are multiple results from the finder', (WidgetTester tester) async {
final SemanticsHandle semanticsHandle = tester.ensureSemantics();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Row(
children: const <Widget>[
Text('hello'),
Text('hello'),
],
),
),
),
);
expect(() => tester.getSemantics(find.text('hello')), throwsStateError);
semanticsHandle.dispose();
});
testWidgets('Returns the correct SemanticsData', (WidgetTester tester) async {
final SemanticsHandle semanticsHandle = tester.ensureSemantics();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: OutlinedButton(
onPressed: () { },
child: const Text('hello'),
),
),
),
);
final SemanticsNode node = tester.getSemantics(find.text('hello'));
final SemanticsData semantics = node.getSemanticsData();
expect(semantics.label, 'hello');
expect(semantics.hasAction(SemanticsAction.tap), true);
expect(semantics.hasFlag(SemanticsFlag.isButton), true);
semanticsHandle.dispose();
});
testWidgets('Can enable semantics for tests via semanticsEnabled', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: OutlinedButton(
onPressed: () { },
child: const Text('hello'),
),
),
),
);
final SemanticsNode node = tester.getSemantics(find.text('hello'));
final SemanticsData semantics = node.getSemanticsData();
expect(semantics.label, 'hello');
expect(semantics.hasAction(SemanticsAction.tap), true);
expect(semantics.hasFlag(SemanticsFlag.isButton), true);
});
testWidgets('Returns merged SemanticsData', (WidgetTester tester) async {
final SemanticsHandle semanticsHandle = tester.ensureSemantics();
const Key key = Key('test');
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Semantics(
label: 'A',
child: Semantics(
label: 'B',
child: Semantics(
key: key,
label: 'C',
child: Container(),
),
),
),
),
),
);
final SemanticsNode node = tester.getSemantics(find.byKey(key));
final SemanticsData semantics = node.getSemanticsData();
expect(semantics.label, 'A\nB\nC');
semanticsHandle.dispose();
});
testWidgets('Does not return partial semantics', (WidgetTester tester) async {
final SemanticsHandle semanticsHandle = tester.ensureSemantics();
final Key key = UniqueKey();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: MergeSemantics(
child: Semantics(
container: true,
label: 'A',
child: Semantics(
container: true,
key: key,
label: 'B',
child: Container(),
),
),
),
),
),
);
final SemanticsNode node = tester.getSemantics(find.byKey(key));
final SemanticsData semantics = node.getSemanticsData();
expect(semantics.label, 'A\nB');
semanticsHandle.dispose();
});
});
testWidgets(
'WidgetTester.drag must break the offset into multiple parallel components if '
'the drag goes outside the touch slop values',
@@ -805,4 +676,308 @@ void main() {
expect(find.text('Item b-45'), findsOneWidget);
});
});
group('SemanticsController', () {
group('find', () {
testWidgets('throws when there are no semantics', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: Text('hello'),
),
),
);
expect(() => tester.semantics.find(find.text('hello')), throwsStateError);
}, semanticsEnabled: false);
testWidgets('throws when there are multiple results from the finder', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Row(
children: const <Widget>[
Text('hello'),
Text('hello'),
],
),
),
),
);
expect(() => tester.semantics.find(find.text('hello')), throwsStateError);
});
testWidgets('Returns the correct SemanticsData', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: OutlinedButton(
onPressed: () { },
child: const Text('hello'),
),
),
),
);
final SemanticsNode node = tester.semantics.find(find.text('hello'));
final SemanticsData semantics = node.getSemanticsData();
expect(semantics.label, 'hello');
expect(semantics.hasAction(SemanticsAction.tap), true);
expect(semantics.hasFlag(SemanticsFlag.isButton), true);
});
testWidgets('Can enable semantics for tests via semanticsEnabled', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: OutlinedButton(
onPressed: () { },
child: const Text('hello'),
),
),
),
);
final SemanticsNode node = tester.semantics.find(find.text('hello'));
final SemanticsData semantics = node.getSemanticsData();
expect(semantics.label, 'hello');
expect(semantics.hasAction(SemanticsAction.tap), true);
expect(semantics.hasFlag(SemanticsFlag.isButton), true);
});
testWidgets('Returns merged SemanticsData', (WidgetTester tester) async {
const Key key = Key('test');
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Semantics(
label: 'A',
child: Semantics(
label: 'B',
child: Semantics(
key: key,
label: 'C',
child: Container(),
),
),
),
),
),
);
final SemanticsNode node = tester.semantics.find(find.byKey(key));
final SemanticsData semantics = node.getSemanticsData();
expect(semantics.label, 'A\nB\nC');
});
testWidgets('Does not return partial semantics', (WidgetTester tester) async {
final Key key = UniqueKey();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: MergeSemantics(
child: Semantics(
container: true,
label: 'A',
child: Semantics(
container: true,
key: key,
label: 'B',
child: Container(),
),
),
),
),
),
);
final SemanticsNode node = tester.semantics.find(find.byKey(key));
final SemanticsData semantics = node.getSemanticsData();
expect(semantics.label, 'A\nB');
});
});
group('simulatedTraversal', () {
final List<Matcher> fullTraversalMatchers = <Matcher>[
containsSemantics(isHeader: true, label: 'Semantics Test'),
containsSemantics(isTextField: true),
containsSemantics(label: 'Off Switch'),
containsSemantics(hasToggledState: true),
containsSemantics(label: 'On Switch'),
containsSemantics(hasToggledState: true, isToggled: true),
containsSemantics(label: "Multiline\nIt's a\nmultiline label!"),
containsSemantics(label: 'Slider'),
containsSemantics(isSlider: true, value: '50%'),
containsSemantics(label: 'Enabled Button'),
containsSemantics(isButton: true, label: 'Tap'),
containsSemantics(label: 'Disabled Button'),
containsSemantics(isButton: true, label: "Don't Tap"),
containsSemantics(label: 'Checked Radio'),
containsSemantics(hasCheckedState: true, isChecked: true),
containsSemantics(label: 'Unchecked Radio'),
containsSemantics(hasCheckedState: true, isChecked: false),
];
testWidgets('produces expected traversal', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: _SemanticsTestWidget()));
expect(
tester.semantics.simulatedAccessibilityTraversal(),
orderedEquals(fullTraversalMatchers));
});
testWidgets('starts traversal at semantics node for `start`', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: _SemanticsTestWidget()));
// We're expecting the traversal to start where the slider is.
final List<Matcher> expectedMatchers = <Matcher>[...fullTraversalMatchers]..removeRange(0, 8);
expect(
tester.semantics.simulatedAccessibilityTraversal(start: find.byType(Slider)),
orderedEquals(expectedMatchers));
});
testWidgets('throws StateError if `start` not found in traversal', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: _SemanticsTestWidget()));
// We look for a SingleChildScrollView since the view itself isn't
// important for accessiblity, so it won't show up in the traversal
expect(
() => tester.semantics.simulatedAccessibilityTraversal(start: find.byType(SingleChildScrollView)),
throwsA(isA<StateError>()),
);
});
testWidgets('ends traversal at semantics node for `end`', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: _SemanticsTestWidget()));
// We're expecting the traversal to end where the slider is, inclusive.
final Iterable<Matcher> expectedMatchers = <Matcher>[...fullTraversalMatchers].getRange(0, 9);
expect(
tester.semantics.simulatedAccessibilityTraversal(end: find.byType(Slider)),
orderedEquals(expectedMatchers));
});
testWidgets('throws StateError if `end` not found in traversal', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: _SemanticsTestWidget()));
// We look for a SingleChildScrollView since the view itself isn't
// important for semantics, so it won't show up in the traversal
expect(
() => tester.semantics.simulatedAccessibilityTraversal(end: find.byType(SingleChildScrollView)),
throwsA(isA<StateError>()),
);
});
testWidgets('returns traversal between `start` and `end` if both are provided', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: _SemanticsTestWidget()));
// We're expecting the traversal to start at the text field and end at the slider.
final Iterable<Matcher> expectedMatchers = <Matcher>[...fullTraversalMatchers].getRange(1, 9);
expect(
tester.semantics.simulatedAccessibilityTraversal(
start: find.byType(TextField),
end: find.byType(Slider),
),
orderedEquals(expectedMatchers));
});
testWidgets('can do fuzzy traversal match with `containsAllInOrder`', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: _SemanticsTestWidget()));
// Grab a sample of the matchers to validate that not every matcher is
// needed to validate a traversal when using `containsAllInOrder`.
final Iterable<Matcher> expectedMatchers = <Matcher>[...fullTraversalMatchers]
..removeAt(0)
..removeLast()
..mapIndexed<Matcher?>((int i, Matcher element) => i.isEven ? element : null)
.whereNotNull();
expect(
tester.semantics.simulatedAccessibilityTraversal(),
containsAllInOrder(expectedMatchers));
});
});
});
}
class _SemanticsTestWidget extends StatelessWidget {
const _SemanticsTestWidget();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Semantics Test')),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
const _SemanticsTestCard(
label: 'TextField',
widget: TextField(),
),
_SemanticsTestCard(
label: 'Off Switch',
widget: Switch(value: false, onChanged: (bool value) {}),
),
_SemanticsTestCard(
label: 'On Switch',
widget: Switch(value: true, onChanged: (bool value) {}),
),
const _SemanticsTestCard(
label: 'Multiline',
widget: Text("It's a\nmultiline label!", maxLines: 2),
),
_SemanticsTestCard(
label: 'Slider',
widget: Slider(value: .5, onChanged: (double value) {}),
),
_SemanticsTestCard(
label: 'Enabled Button',
widget: TextButton(onPressed: () {}, child: const Text('Tap')),
),
const _SemanticsTestCard(
label: 'Disabled Button',
widget: TextButton(onPressed: null, child: Text("Don't Tap")),
),
_SemanticsTestCard(
label: 'Checked Radio',
widget: Radio<String>(
value: 'checked',
groupValue: 'checked',
onChanged: (String? value) {},
),
),
_SemanticsTestCard(
label: 'Unchecked Radio',
widget: Radio<String>(
value: 'unchecked',
groupValue: 'checked',
onChanged: (String? value) {},
),
),
],
),
),
);
}
}
class _SemanticsTestCard extends StatelessWidget {
const _SemanticsTestCard({required this.label, required this.widget});
final String label;
final Widget widget;
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
title: Text(label),
trailing: SizedBox(width: 200, child: widget),
),
);
}
}