Skip to content

[material_ui] Migrate off of flutter_test's find.byTooltip and on to the local findByTooltip - #12492

Merged
auto-submit[bot] merged 4 commits into
flutter:mainfrom
justinmc:material-find-by-tooltip
Sep 2, 2026
Merged

[material_ui] Migrate off of flutter_test's find.byTooltip and on to the local findByTooltip#12492
auto-submit[bot] merged 4 commits into
flutter:mainfrom
justinmc:material-find-by-tooltip

Conversation

@justinmc

@justinmc justinmc commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

The find.byTooltip finder in flutter/flutter's flutter_test package is unreliable for material_ui due to using flutter/flutter's Tooltip class. This PR migrates all usages to the existing findByTooltip in material_ui, which does not have this problem.

See flutter/flutter#191063 (comment)

Fixes flutter/flutter#191063

@github-actions github-actions Bot added triage-framework Should be looked at in framework triage p: material_ui labels Aug 17, 2026
@justinmc justinmc added the CICD Run CI/CD label Aug 18, 2026
@justinmc
justinmc requested a review from Piinks August 18, 2026 16:43
@justinmc
justinmc marked this pull request as ready for review August 18, 2026 16:43

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request replaces usages of find.byTooltip with a custom findByTooltip helper function across multiple test files in packages/material_ui. Feedback on the changes suggests addressing the duplication of the findByTooltip helper function in the example test files by extracting it into a shared location. Additionally, the feedback recommends replacing unsafe null assertions (!) on widget.richMessage with optional chaining (?.) and a fallback value to prevent potential runtime exceptions.

Comment on lines +82 to +120
/// 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.
///
// TODO(justinmc): Port flutter_test to material_ui, then delete this method and
// use that one. See https://github.com/flutter/flutter/issues/186966
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);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The findByTooltip helper function is duplicated across multiple example test files (e.g., bottom_app_bar.2_test.dart and tooltip.3_test.dart). To improve maintainability and adhere to DRY (Don't Repeat Yourself) principles, consider extracting this helper into a shared finders.dart file under packages/material_ui/example/test/ and importing it in the respective test files.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

Comment on lines +107 to +108
final String tooltipMessage =
widget.message ?? widget.richMessage!.toPlainText();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using the null assertion operator ! on widget.richMessage can lead to a runtime exception if both widget.message and widget.richMessage are null. Use optional chaining ?. and a fallback empty string ?? '' to ensure defensive programming and null safety.

Suggested change
final String tooltipMessage =
widget.message ?? widget.richMessage!.toPlainText();
final String tooltipMessage =
widget.message ?? widget.richMessage?.toPlainText() ?? '';

Copy link
Copy Markdown
Contributor Author

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.

// 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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using the null assertion operator ! on widget.richMessage can lead to a runtime exception if both widget.message and widget.richMessage are null. Use optional chaining ?. and a fallback empty string ?? '' to ensure defensive programming and null safety.

Suggested change
final String tooltipMessage = widget.message ?? widget.richMessage!.toPlainText();
final String tooltipMessage = widget.message ?? widget.richMessage?.toPlainText() ?? '';

///
/// This was copied from flutter_test, which uses flutter/material.dart.
///
// TODO(justinmc): Port flutter_test to material_ui, then delete this method and

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want flutter_test to depend on material_ui, I thought we were going to remove any material/cupertino references from flutter_test?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I thought we had already done this.. See https://github.com/flutter/packages/blob/main/packages/material_ui/test/finders.dart

I don't think flutter_test can depend on material_ui. We'd end up in another circular circus.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I'll remove these TODOs. The linked issue explains the situation correctlly.

@github-actions github-actions Bot added triage-design Should be looked at in design triage and removed triage-framework Should be looked at in framework triage labels Aug 20, 2026
@justinmc
justinmc requested a review from Piinks August 20, 2026 22:16

@Piinks Piinks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Piinks Piinks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need a pending changelog. :)

@Piinks
Piinks force-pushed the material-find-by-tooltip branch from 116dcb6 to 530e5bc Compare September 2, 2026 21:23
@Piinks

Piinks commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

This will need a pending changelog. :)

Oh wait! It won't!
This just changes test files.

@Piinks Piinks added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 2, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Sep 2, 2026
@auto-submit

auto-submit Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/packages/12492, because - The status or check suite Dashboard Checks has failed. Please fix the issues identified (or deflake) before re-applying this label.

@justinmc justinmc added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 2, 2026
@auto-submit
auto-submit Bot merged commit 1efac12 into flutter:main Sep 2, 2026
11 of 13 checks passed
github-merge-queue Bot pushed a commit to flutter/flutter that referenced this pull request Sep 3, 2026
flutter/packages@18fe786...f9b3954

2026-09-03 ashutoshagarwal2014@gmail.com [image_picker] Fix android-16
not picking up file (flutter/packages#11320)
2026-09-02 jmccandless@google.com [material_ui] Migrate off of
flutter_test's find.byTooltip and on to the local findByTooltip
(flutter/packages#12492)
2026-09-02 32538273+ValentinVignal@users.noreply.github.com
[material_ui] Remove no shuffle from text field tests
(flutter/packages#12547)
2026-09-02 21270878+elliette@users.noreply.github.com Add Material style
variant enum (#12221) (flutter/packages#12717)
2026-09-02 brian.egan@verygood.ventures [go_router_builder] Report
duplicate route paths at build time (flutter/packages#12399)
2026-09-02 victor.orozco@cloudsufi.com [google_sign_in] PR 1/4
google_sign_in_ios SPM packaging (flutter/packages#12654)
2026-09-02 55357489+voledyaev@users.noreply.github.com
[video_player_android] Report display size for anamorphic video
(flutter/packages#12360)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD p: material_ui triage-design Should be looked at in design triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

flutter_test finders still references flutter/material Tooltip widget

2 participants