Unverified Commit 14150788 authored by Christian Mürtz's avatar Christian Mürtz Committed by GitHub

Find text containing in tests (#65072)

parent a17b3309
......@@ -36,6 +36,20 @@ class CommonFinders {
/// nodes that are [Offstage] or that are from inactive [Route]s.
Finder text(String text, { bool skipOffstage = true }) => _TextFinder(text, skipOffstage: skipOffstage);
/// Finds [Text] and [EditableText] widgets which contain the given
/// `pattern` argument.
///
/// ## Sample code
///
/// ```dart
/// expect(find.textContain('Back'), findsOneWidget);
/// expect(find.textContain(RegExp(r'(\w+)')), findsOneWidget);
/// ```
///
/// If the `skipOffstage` argument is true (the default), then this skips
/// nodes that are [Offstage] or that are from inactive [Route]s.
Finder textContaining(Pattern pattern, { bool skipOffstage = true }) => _TextContainingFinder(pattern, skipOffstage: skipOffstage);
/// Looks for widgets that contain a [Text] descendant with `text`
/// in it.
///
......@@ -556,6 +570,29 @@ class _TextFinder extends MatchFinder {
}
}
class _TextContainingFinder extends MatchFinder {
_TextContainingFinder(this.pattern, {bool skipOffstage = true})
: super(skipOffstage: skipOffstage);
final Pattern pattern;
@override
String get description => 'text containing $pattern';
@override
bool matches(Element candidate) {
final Widget widget = candidate.widget;
if (widget is Text) {
if (widget.data != null)
return widget.data.contains(pattern);
return widget.textSpan.toPlainText().contains(pattern);
} else if (widget is EditableText) {
return widget.controller.text.contains(pattern);
}
return false;
}
}
class _KeyFinder extends MatchFinder {
_KeyFinder(this.key, { bool skipOffstage = true }) : super(skipOffstage: skipOffstage);
......
......@@ -30,6 +30,46 @@ void main() {
});
});
group('textContaining', () {
testWidgets('finds Text widgets', (WidgetTester tester) async {
await tester.pumpWidget(_boilerplate(
const Text('this is a test'),
));
expect(find.textContaining(RegExp(r'test')), findsOneWidget);
expect(find.textContaining('test'), findsOneWidget);
expect(find.textContaining('a'), findsOneWidget);
expect(find.textContaining('s'), findsOneWidget);
});
testWidgets('finds Text.rich widgets', (WidgetTester tester) async {
await tester.pumpWidget(_boilerplate(
const Text.rich(
TextSpan(text: 'this', children: <TextSpan>[
TextSpan(text: 'is'),
TextSpan(text: 'a'),
TextSpan(text: 'test'),
],
),
)));
expect(find.textContaining(RegExp(r'isatest')), findsOneWidget);
expect(find.textContaining('isatest'), findsOneWidget);
});
testWidgets('finds EditableText widgets', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: _boilerplate(TextField(
controller: TextEditingController()..text = 'this is test',
)),
),
));
expect(find.textContaining(RegExp(r'test')), findsOneWidget);
expect(find.textContaining('test'), findsOneWidget);
});
});
group('semantics', () {
testWidgets('Throws StateError if semantics are not enabled', (WidgetTester tester) async {
expect(() => find.bySemanticsLabel('Add'), throwsStateError);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment