mirror of
https://github.com/flutter/flutter
synced 2024-10-13 11:42:54 +00:00
Make it easier to ensure semantics in widgetTests (#29387)
* Make it easier to ensure semantics
This commit is contained in:
parent
a1bee54fda
commit
8e14c227eb
|
@ -58,6 +58,11 @@ typedef WidgetTesterCallback = Future<void> Function(WidgetTester widgetTester);
|
||||||
/// is defined by the timeout property,
|
/// is defined by the timeout property,
|
||||||
/// which defaults to [TestWidgetsFlutterBinding.defaultTestTimeout].
|
/// which defaults to [TestWidgetsFlutterBinding.defaultTestTimeout].
|
||||||
///
|
///
|
||||||
|
/// If the `enableSemantics` parameter is set to `true`,
|
||||||
|
/// [WidgetTester.ensureSemantics] will have been called before the tester is
|
||||||
|
/// passed to the `callback`, and that handle will automatically be disposed
|
||||||
|
/// after the callback is finished.
|
||||||
|
///
|
||||||
/// This function uses the [test] function in the test package to
|
/// This function uses the [test] function in the test package to
|
||||||
/// register the given callback as a test. The callback, when run,
|
/// register the given callback as a test. The callback, when run,
|
||||||
/// will be given a new instance of [WidgetTester]. The [find] object
|
/// will be given a new instance of [WidgetTester]. The [find] object
|
||||||
|
@ -84,6 +89,7 @@ void testWidgets(
|
||||||
WidgetTesterCallback callback, {
|
WidgetTesterCallback callback, {
|
||||||
bool skip = false,
|
bool skip = false,
|
||||||
test_package.Timeout timeout,
|
test_package.Timeout timeout,
|
||||||
|
bool semanticsEnabled = false,
|
||||||
}) {
|
}) {
|
||||||
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
|
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
final WidgetTester tester = WidgetTester._(binding);
|
final WidgetTester tester = WidgetTester._(binding);
|
||||||
|
@ -91,10 +97,17 @@ void testWidgets(
|
||||||
test(
|
test(
|
||||||
description,
|
description,
|
||||||
() {
|
() {
|
||||||
|
SemanticsHandle semanticsHandle;
|
||||||
|
if (semanticsEnabled == true) {
|
||||||
|
semanticsHandle = tester.ensureSemantics();
|
||||||
|
}
|
||||||
tester._recordNumberOfSemanticsHandles();
|
tester._recordNumberOfSemanticsHandles();
|
||||||
test_package.addTearDown(binding.postTest);
|
test_package.addTearDown(binding.postTest);
|
||||||
return binding.runTest(
|
return binding.runTest(
|
||||||
() => callback(tester),
|
() async {
|
||||||
|
await callback(tester);
|
||||||
|
semanticsHandle?.dispose();
|
||||||
|
},
|
||||||
tester._endOfTestVerifications,
|
tester._endOfTestVerifications,
|
||||||
description: description ?? '',
|
description: description ?? '',
|
||||||
);
|
);
|
||||||
|
|
|
@ -589,6 +589,27 @@ void main() {
|
||||||
semanticsHandle.dispose();
|
semanticsHandle.dispose();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('Can enable semantics for tests via semanticsEnabled', (WidgetTester tester) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
home: Scaffold(
|
||||||
|
body: Container(
|
||||||
|
child: OutlineButton(
|
||||||
|
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);
|
||||||
|
}, semanticsEnabled: true);
|
||||||
|
|
||||||
testWidgets('Returns merged SemanticsData', (WidgetTester tester) async {
|
testWidgets('Returns merged SemanticsData', (WidgetTester tester) async {
|
||||||
final SemanticsHandle semanticsHandle = tester.ensureSemantics();
|
final SemanticsHandle semanticsHandle = tester.ensureSemantics();
|
||||||
const Key key = Key('test');
|
const Key key = Key('test');
|
||||||
|
|
Loading…
Reference in a new issue