Move key event and semantics related method from WidgetTester to WidgetController (#62362)
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flutter/semantics.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
@@ -22,6 +23,140 @@ 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: 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);
|
||||
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 {
|
||||
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',
|
||||
|
||||
Reference in New Issue
Block a user