@@ -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:flutter/rendering.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
@@ -29,6 +30,53 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
group('semantics', () {
|
||||
testWidgets('Throws StateError if semantics are not enabled', (WidgetTester tester) async {
|
||||
expect(() => find.bySemanticsLabel('Add'), throwsStateError);
|
||||
});
|
||||
|
||||
testWidgets('finds Semantically labeled widgets', (WidgetTester tester) async {
|
||||
final SemanticsHandle semanticsHandle = tester.ensureSemantics();
|
||||
await tester.pumpWidget(_boilerplate(
|
||||
Semantics(
|
||||
label: 'Add',
|
||||
button: true,
|
||||
child: const FlatButton(
|
||||
child: Text('+'),
|
||||
onPressed: null,
|
||||
),
|
||||
),
|
||||
));
|
||||
expect(find.bySemanticsLabel('Add'), findsOneWidget);
|
||||
semanticsHandle.dispose();
|
||||
});
|
||||
|
||||
testWidgets('finds Semantically labeled widgets by RegExp', (WidgetTester tester) async {
|
||||
final SemanticsHandle semanticsHandle = tester.ensureSemantics();
|
||||
await tester.pumpWidget(_boilerplate(
|
||||
Semantics(
|
||||
container: true,
|
||||
child: Row(children: const <Widget>[
|
||||
Text('Hello'),
|
||||
Text('World'),
|
||||
]),
|
||||
),
|
||||
));
|
||||
expect(find.bySemanticsLabel('Hello'), findsNothing);
|
||||
expect(find.bySemanticsLabel(RegExp(r'^Hello')), findsOneWidget);
|
||||
semanticsHandle.dispose();
|
||||
});
|
||||
|
||||
testWidgets('finds Semantically labeled widgets without explicit Semantics', (WidgetTester tester) async {
|
||||
final SemanticsHandle semanticsHandle = tester.ensureSemantics();
|
||||
await tester.pumpWidget(_boilerplate(
|
||||
const SimpleCustomSemanticsWidget('Foo')
|
||||
));
|
||||
expect(find.bySemanticsLabel('Foo'), findsOneWidget);
|
||||
semanticsHandle.dispose();
|
||||
});
|
||||
});
|
||||
|
||||
group('hitTestable', () {
|
||||
testWidgets('excludes non-hit-testable widgets', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
@@ -92,3 +140,28 @@ Widget _boilerplate(Widget child) {
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
|
||||
class SimpleCustomSemanticsWidget extends LeafRenderObjectWidget {
|
||||
const SimpleCustomSemanticsWidget(this.label);
|
||||
|
||||
final String label;
|
||||
|
||||
@override
|
||||
RenderObject createRenderObject(BuildContext context) => SimpleCustomSemanticsRenderObject(label);
|
||||
}
|
||||
|
||||
class SimpleCustomSemanticsRenderObject extends RenderBox {
|
||||
SimpleCustomSemanticsRenderObject(this.label);
|
||||
|
||||
final String label;
|
||||
|
||||
@override
|
||||
bool get sizedByParent => true;
|
||||
|
||||
@override
|
||||
void describeSemanticsConfiguration(SemanticsConfiguration config) {
|
||||
super.describeSemanticsConfiguration(config);
|
||||
config..label = label..textDirection = TextDirection.ltr;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user