Add semantic label finders (#29342)

* Add semantic label finders
This commit is contained in:
Dan Field
2019-03-18 20:32:03 -07:00
committed by GitHub
parent 79e3bf4ad3
commit 5e27ebbe8d
6 changed files with 206 additions and 0 deletions

View File

@@ -272,6 +272,51 @@ class CommonFinders {
Finder ancestor({ Finder of, Finder matching, bool matchRoot = false }) {
return _AncestorFinder(of, matching, matchRoot: matchRoot);
}
/// Finds [Semantics] widgets matching the given `label`, either by
/// [RegExp.hasMatch] or string equality.
///
/// The framework may combine semantics labels in certain scenarios, such as
/// when multiple [Text] widgets are in a [MaterialButton] widget. In such a
/// case, it may be preferable to match by regular expression. Consumers of
/// this API __must not__ introduce unsuitable content into the semantics tree
/// for the purposes of testing; in particular, you should prefer matching by
/// regular expression rather than by string if the framework has combined
/// your semantics, and not try to force the framework to break up the
/// semantics nodes. Breaking up the nodes would have an undesirable effect on
/// screen readers and other accessibility services.
///
/// ## Sample code
///
/// ```dart
/// expect(find.BySemanticsLabel('Back'), findsOneWidget);
/// ```
///
/// If the `skipOffstage` argument is true (the default), then this skips
/// nodes that are [Offstage] or that are from inactive [Route]s.
Finder bySemanticsLabel(Pattern label, { bool skipOffstage = true }) {
if (WidgetsBinding.instance.pipelineOwner.semanticsOwner == null)
throw StateError('Semantics are not enabled. '
'Make sure to call tester.enableSemantics() before using '
'this finder, and call dispose on its return value after.');
return byElementPredicate(
(Element element) {
// Multiple elements can have the same renderObject - we want the "owner"
// of the renderObject, i.e. the RenderObjectElement.
if (element is! RenderObjectElement) {
return false;
}
final String semanticsLabel = element.renderObject?.debugSemantics?.label;
if (semanticsLabel == null) {
return false;
}
return label is RegExp
? label.hasMatch(semanticsLabel)
: label == semanticsLabel;
},
skipOffstage: skipOffstage,
);
}
}
/// Searches a widget tree and returns nodes that match a particular

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: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;
}
}