mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Allow find.byTooltip
to use a RegEx (#149348)
## Description This adds the ability for `find.byTooltip` to use a `RegEx` to match the tooltip. Also, adds some tests for `byTooltip`, since there weren't any. ## Tests - added tests
This commit is contained in:
parent
c77c0cf0bc
commit
ea8ae8c81b
@ -365,13 +365,20 @@ class CommonFinders {
|
|||||||
///
|
///
|
||||||
/// ```dart
|
/// ```dart
|
||||||
/// expect(find.byTooltip('Back'), findsOneWidget);
|
/// expect(find.byTooltip('Back'), findsOneWidget);
|
||||||
|
/// expect(find.byTooltip(RegExp('Back.*')), findsNWidgets(2));
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// If the `skipOffstage` argument is true (the default), then this skips
|
/// If the `skipOffstage` argument is true (the default), then this skips
|
||||||
/// nodes that are [Offstage] or that are from inactive [Route]s.
|
/// nodes that are [Offstage] or that are from inactive [Route]s.
|
||||||
Finder byTooltip(String message, { bool skipOffstage = true }) {
|
Finder byTooltip(Pattern message, {bool skipOffstage = true}) {
|
||||||
return byWidgetPredicate(
|
return byWidgetPredicate(
|
||||||
(Widget widget) => widget is Tooltip && widget.message == message,
|
(Widget widget) {
|
||||||
|
return widget is Tooltip &&
|
||||||
|
(message is RegExp
|
||||||
|
? ((widget.message != null && message.hasMatch(widget.message!)) ||
|
||||||
|
(widget.richMessage != null && message.hasMatch(widget.richMessage!.toPlainText())))
|
||||||
|
: ((widget.message ?? widget.richMessage?.toPlainText()) == message));
|
||||||
|
},
|
||||||
skipOffstage: skipOffstage,
|
skipOffstage: skipOffstage,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -301,6 +301,98 @@ void main() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
group('byTooltip', () {
|
||||||
|
testWidgets('finds widgets by tooltip', (WidgetTester tester) async {
|
||||||
|
await tester.pumpWidget(_boilerplate(
|
||||||
|
const Tooltip(
|
||||||
|
message: 'Tooltip Message',
|
||||||
|
child: Text('+'),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
expect(find.byTooltip('Tooltip Message'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('finds widgets with tooltip by RegExp', (WidgetTester tester) async {
|
||||||
|
await tester.pumpWidget(_boilerplate(
|
||||||
|
const Tooltip(
|
||||||
|
message: 'Tooltip Message',
|
||||||
|
child: Text('+'),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
expect(find.byTooltip('Tooltip'), findsNothing);
|
||||||
|
expect(find.byTooltip(RegExp(r'^Tooltip')), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('finds widgets by rich text tooltip', (WidgetTester tester) async {
|
||||||
|
await tester.pumpWidget(_boilerplate(
|
||||||
|
const Tooltip(
|
||||||
|
richMessage: TextSpan(
|
||||||
|
children: <InlineSpan>[
|
||||||
|
TextSpan(text: 'Tooltip '),
|
||||||
|
TextSpan(text: 'Message'),
|
||||||
|
]),
|
||||||
|
child: Text('+'),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
expect(find.byTooltip('Tooltip Message'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('finds widgets with rich text tooltip by RegExp', (WidgetTester tester) async {
|
||||||
|
await tester.pumpWidget(_boilerplate(
|
||||||
|
const Tooltip(
|
||||||
|
richMessage: TextSpan(
|
||||||
|
children: <InlineSpan>[
|
||||||
|
TextSpan(text: 'Tooltip '),
|
||||||
|
TextSpan(text: 'Message'),
|
||||||
|
]),
|
||||||
|
child: Text('+'),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
expect(find.byTooltip('Tooltip M'), findsNothing);
|
||||||
|
expect(find.byTooltip(RegExp(r'^Tooltip M')), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('finds empty string with tooltip', (WidgetTester tester) async {
|
||||||
|
await tester.pumpWidget(_boilerplate(
|
||||||
|
const Tooltip(
|
||||||
|
message: '',
|
||||||
|
child: Text('+'),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
expect(find.byTooltip(''), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.pumpWidget(_boilerplate(
|
||||||
|
const Tooltip(
|
||||||
|
richMessage: TextSpan(
|
||||||
|
children: <InlineSpan>[
|
||||||
|
TextSpan(text: ''),
|
||||||
|
]),
|
||||||
|
child: Text('+'),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
expect(find.byTooltip(''), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.pumpWidget(_boilerplate(
|
||||||
|
const Tooltip(
|
||||||
|
message: '',
|
||||||
|
child: Text('+'),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
expect(find.byTooltip(RegExp(r'^$')), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.pumpWidget(_boilerplate(
|
||||||
|
const Tooltip(
|
||||||
|
richMessage: TextSpan(
|
||||||
|
children: <InlineSpan>[
|
||||||
|
TextSpan(text: ''),
|
||||||
|
]),
|
||||||
|
child: Text('+'),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
expect(find.byTooltip(RegExp(r'^$')), findsOneWidget);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
group('hitTestable', () {
|
group('hitTestable', () {
|
||||||
testWidgets('excludes non-hit-testable widgets',
|
testWidgets('excludes non-hit-testable widgets',
|
||||||
(WidgetTester tester) async {
|
(WidgetTester tester) async {
|
||||||
@ -1356,10 +1448,17 @@ void main() {
|
|||||||
Widget _boilerplate(Widget child) {
|
Widget _boilerplate(Widget child) {
|
||||||
return Directionality(
|
return Directionality(
|
||||||
textDirection: TextDirection.ltr,
|
textDirection: TextDirection.ltr,
|
||||||
child: child,
|
child: Navigator(
|
||||||
|
onGenerateRoute: (RouteSettings settings) {
|
||||||
|
return MaterialPageRoute<void>(
|
||||||
|
builder: (BuildContext context) => child,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class SimpleCustomSemanticsWidget extends LeafRenderObjectWidget {
|
class SimpleCustomSemanticsWidget extends LeafRenderObjectWidget {
|
||||||
const SimpleCustomSemanticsWidget(this.label, {super.key});
|
const SimpleCustomSemanticsWidget(this.label, {super.key});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user