Skip to content

Remove unnecessary Material imports from non-Material tests in flutter package#185271

Merged
auto-submit[bot] merged 6 commits into
flutter:masterfrom
momshaddinury:cleanup/test-remove-material-import
May 21, 2026
Merged

Remove unnecessary Material imports from non-Material tests in flutter package#185271
auto-submit[bot] merged 6 commits into
flutter:masterfrom
momshaddinury:cleanup/test-remove-material-import

Conversation

@momshaddinury
Copy link
Copy Markdown
Contributor

@momshaddinury momshaddinury commented Apr 20, 2026

Several tests under packages/flutter/test/ were importing package:flutter/material.dart even though they only exercise APIs from package:flutter/widgets.dart (or lower layers like painting, animation, gestures). Pulling in Material unnecessarily increases cross-package coupling between these tests and the Material library and works against the cross-import hygiene enforced by dev/bots/check_tests_cross_imports.dart.

This PR:

  • Switches the import from package:flutter/material.dart to package:flutter/widgets.dart in tests that do not reference any Material symbols.
  • Where tests referenced Colors.* only, replaces those with the equivalent raw const Color(0x…) values.
  • Where tests wrapped widgets in MaterialApp purely as a Directionality/MediaQuery/localization host, swaps MaterialApp for TestWidgetsApp from widgets_app_tester.dart, and replaces materialTextSelectionControls with emptyTextSelectionControls.
  • Removes the corresponding entry from the check_tests_cross_imports.dart allowlist where applicable.

No production code is modified and no test semantics change — the tests exercise the same widget/painting/gesture/animation behavior, just without dragging in Material.

Files changed

Tests updated:

  • packages/flutter/test/gestures/force_press_test.dart
  • packages/flutter/test/gestures/transformed_scale_test.dart
  • packages/flutter/test/widgets/default_text_editing_shortcuts_test.dart
  • packages/flutter/test/widgets/semantics_owner_test.dart
  • packages/flutter/test/animation/animation_sheet_test.dart
  • packages/flutter/test/painting/star_border_test.dart
  • packages/flutter/test/painting/paint_image_test.dart

Allowlist updated:

  • dev/bots/check_tests_cross_imports.dart

Scope note — two files intentionally left out

Two test files originally staged in this cleanup were dropped after local test runs showed they aren't a clean Material → widgets swap. Details and a request for guidance are in a separate PR comment below:

  • packages/flutter/test/widgets/editable_text_scribe_test.dart — the handle-tap tests rely on CustomPaint widgets painted by materialTextSelectionControls; emptyTextSelectionControls paints nothing, so the handle finders return 0 widgets.
  • packages/flutter/test/widgets/editable_text_test.dart (Live Text test only) — swapping MaterialAppTestWidgetsApp triggers A FocusManager was used after being disposed in the shared tearDown, cascading into five downstream a11y/semantics tests.

Both files remain on the check_tests_cross_imports.dart allowlist and can be picked up in follow-up PRs once we agree on the approach.

Related issues

Context — why one PR

Per reviewer guidance from @navaronbracke on #185238 (comment), similar Material-import cleanups are being consolidated into a single PR that lands closer to the ~300–500-line "sweet spot" rather than as many tiny per-file PRs.

Superseded PRs (please close once this lands)

The following open PRs are subsumed by this one and can be closed if this PR is approved (see this comment):

Not superseded (corresponding files were dropped from this PR — see the scope note above):

Pre-launch Checklist

@github-actions github-actions Bot added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. a: animation Animation APIs a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) f: gestures flutter/packages/flutter/gestures repository. labels Apr 20, 2026
Copy link
Copy Markdown
Contributor

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

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 removes Material library dependencies from several tests by replacing MaterialApp with TestWidgetsApp, updating imports to widgets.dart, and substituting Material-specific constants like colors and selection controls with hardcoded values or generic alternatives. Feedback was provided regarding an inconsistent hex color value used for blue in editable_text_test.dart.

Comment thread packages/flutter/test/widgets/editable_text_test.dart Outdated
@momshaddinury
Copy link
Copy Markdown
Contributor Author

Tagging @justinmc and @navaronbracke for review — this consolidates the Material-import cleanups per @navaronbracke's suggestion on #185238, and is part of the cross-import effort in #177028 / #177412.

victorsanni
victorsanni previously approved these changes Apr 23, 2026
Comment thread packages/flutter/test/animation/animation_sheet_test.dart
style: const TextStyle(fontSize: 10.0),
cursorColor: Colors.blue,
backgroundCursorColor: Colors.grey,
cursorColor: const Color(0xFF2196F3),
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.

Nit: Since this is reused, maybe we can define a global const here? This will make it slightly easier to read.

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.

Alright, makes sense. I'll address this.

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 think that's what other people have done when migrating these tests off of Color.

@victorsanni victorsanni added the CICD Run CI/CD label Apr 23, 2026
@momshaddinury momshaddinury force-pushed the cleanup/test-remove-material-import branch from c435f7d to fc18fa4 Compare April 23, 2026 05:31
@github-actions github-actions Bot removed the CICD Run CI/CD label Apr 23, 2026
Comment thread packages/flutter/test/gestures/force_press_test.dart
@momshaddinury momshaddinury force-pushed the cleanup/test-remove-material-import branch 2 times, most recently from 346f3ce to 2450cb8 Compare April 23, 2026 06:23
@momshaddinury
Copy link
Copy Markdown
Contributor Author

Heads-up @navaronbracke @justinmc @victorsanni — I dropped two files from this PR because neither is a clean Material → widgets swap:

  • editable_text_scribe_test.dart — the handle-tap tests at lines 186/231 use find.descendant(of: CompositedTransformFollower, matching: CustomPaint) to locate rendered selection handles, and emptyTextSelectionControls paints nothing.
  • editable_text_test.dart (Live Text test only) — swapping MaterialAppTestWidgetsApp here triggers FocusManager was used after being disposed in the shared tearDown, cascading into five downstream a11y/semantics tests.

What's the preferred path forward — follow-up PRs with real fixes, stubs/workarounds, or leaving these in the allowlist? I can file tracking issues if that helps.

Copy link
Copy Markdown
Contributor

@victorsanni victorsanni left a comment

Choose a reason for hiding this comment

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

Hi @momshaddinury, can you fix the failing linux analyze CI check?

@momshaddinury momshaddinury force-pushed the cleanup/test-remove-material-import branch from 06671b7 to de04920 Compare April 25, 2026 01:44
@github-actions github-actions Bot removed the CICD Run CI/CD label Apr 25, 2026
Where these tests previously used Colors.X, the cleanup PR that
removed the Material import substituted the literal value of each
constant (e.g. 0xFFF44336 for Colors.red). For tests that don't
assert exact RGB values, pure-color hex codes (0xFFFF0000, etc.) are
clearer about intent.

  - painting/star_border_test.dart:                 green  4CAF50 -> 00FF00
  - widgets/default_text_editing_shortcuts_test.dart: blue   2196F3 -> 0000FF

In default_text_editing_shortcuts_test.dart, also extract the cursor
and background-cursor colors into top-level private consts
(_cursorColor, _backgroundCursorColor) so the values are defined once
and reused across all three EditableText sites.

Also re-run `dart format` on the file, which collapses the two
`testWidgets(...)` calls in the `Linux numpad shortcuts` group from
the older multi-line style to the inline style. These are formatting
changes only, no behavior changes. They surface because the format
check validates files in the PR diff against the current dart_style
version.

Part of flutter#177028
navaronbracke
navaronbracke previously approved these changes May 16, 2026
Copy link
Copy Markdown
Contributor

@navaronbracke navaronbracke left a comment

Choose a reason for hiding this comment

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

LGTM

@momshaddinury I had to review this with whitespace changes hidden, but that is because the formatting thing you explained in #185271 (comment) right?

@momshaddinury
Copy link
Copy Markdown
Contributor Author

LGTM

@momshaddinury I had to review this with whitespace changes hidden, but that is because the formatting thing you explained in #185271 (comment) right?

Yes

@momshaddinury
Copy link
Copy Markdown
Contributor Author

@navaronbracke @justinmc @victorsanni I am afraid there won't be anything to merge after a while. Already down to two files from 6. :3

Copy link
Copy Markdown
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@momshaddinury
Copy link
Copy Markdown
Contributor Author

momshaddinury commented May 17, 2026

@Piinks
@justinmc
@navaronbracke

It's passing all the tests. Good to merge?

@momshaddinury
Copy link
Copy Markdown
Contributor Author

@Piinks let me know if anything else is needed before it gets merged. All checks have passed. Thanks.

@victorsanni
Copy link
Copy Markdown
Contributor

editable_text_scribe_test.dart — the handle-tap tests at lines 186/231 use find.descendant(of: CompositedTransformFollower, matching: CustomPaint) to locate rendered selection handles, and emptyTextSelectionControls paints nothing.

I think here we can use testTextSelectionHandleControls here?

editable_text_test.dart (Live Text test only) — swapping MaterialApp → TestWidgetsApp here triggers FocusManager was used after being disposed in the shared tearDown, cascading into five downstream a11y/semantics tests.

This can be done in this PR as well?

Check this one: #185271 (comment)

Yes, and my reply was that both can be added to this PR.

This was referenced May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: animation Animation APIs CICD Run CI/CD framework flutter/packages/flutter repository. See also f: labels.

Projects

Development

Successfully merging this pull request may close these issues.

6 participants