-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[material_ui] Migrate off of flutter_test's find.byTooltip and on to the local findByTooltip #12492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,11 +70,49 @@ void main() { | |
| await tester.pumpWidget(const example.BottomAppBarDemo()); | ||
|
|
||
| // Trigger the SnackBar. | ||
| await tester.tap(find.byTooltip('Open popup menu')); | ||
| await tester.tap(findByTooltip('Open popup menu')); | ||
| await tester.pump(); | ||
|
|
||
| expect(find.text('Yay! A SnackBar!'), findsOneWidget); | ||
|
|
||
| expect(find.text('Undo'), findsOneWidget); | ||
| }); | ||
| } | ||
|
|
||
| /// Finds [RawTooltip] or [Tooltip] widgets with the given `message`. | ||
| /// | ||
| /// ## Sample code | ||
| /// | ||
| /// ```dart | ||
| /// expect(findByTooltip('Back'), findsOneWidget); | ||
| /// expect(findByTooltip(RegExp('Back.*')), findsNWidgets(2)); | ||
| /// ``` | ||
| /// | ||
| /// If the `skipOffstage` argument is true (the default), then this skips | ||
| /// nodes that are [Offstage] or that are from inactive [Route]s. | ||
| /// | ||
| /// This was copied from flutter_test, which uses flutter/material.dart. | ||
| /// | ||
| Finder findByTooltip(Pattern message, {bool skipOffstage = true}) { | ||
| return find.byWidgetPredicate((Widget widget) { | ||
| // Compare RawTooltip's semantics tooltip with the given message. | ||
| // However, Tooltip's message needs to be checked directly if: | ||
| // 1. Tooltip.excludeFromSemantics is true, since in this case Tooltip | ||
| // provides no semantics tooltip to the underlying RawTooltip. | ||
| // 2. Tooltip.message and Tooltip.richMessage are empty, since in this | ||
| // case no RawTooltip is created. | ||
| if (widget is Tooltip) { | ||
| final String tooltipMessage = | ||
| widget.message ?? widget.richMessage!.toPlainText(); | ||
| if ((widget.excludeFromSemantics ?? false) || tooltipMessage.isEmpty) { | ||
| return message is RegExp | ||
| ? message.hasMatch(tooltipMessage) | ||
| : tooltipMessage == message; | ||
| } | ||
| } | ||
| return widget is RawTooltip && | ||
| (message is RegExp | ||
| ? message.hasMatch(widget.semanticsTooltip ?? '') | ||
| : widget.semanticsTooltip == message); | ||
| }, skipOffstage: skipOffstage); | ||
| } | ||
|
Comment on lines
+82
to
+118
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm ok either way here. I could make a utils file in examples/test, though it would be the first one. This is already going to be duplicated with material_ui/test/finders.dart, and we will clean them up after flutter_test migrates to material_ui. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the null assertion operator
!onwidget.richMessagecan lead to a runtime exception if bothwidget.messageandwidget.richMessageare null. Use optional chaining?.and a fallback empty string?? ''to ensure defensive programming and null safety.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an assertion that enforces at least one of these is set.