Skip to content

Remove semantics_tester import from card_test.dart#184808

Closed
Sanaullah49 wants to merge 8 commits into
flutter:masterfrom
Sanaullah49:chore/182636-card-semantics
Closed

Remove semantics_tester import from card_test.dart#184808
Sanaullah49 wants to merge 8 commits into
flutter:masterfrom
Sanaullah49:chore/182636-card-semantics

Conversation

@Sanaullah49

Copy link
Copy Markdown
Contributor

Part of #182636

Summary

Remove the semantics_tester.dart cross-import from card_test.dart.

What changed

  • Replaced the deprecated SemanticsTester/TestSemantics assertions in the Card semantics tests with direct semantics lookups and matchesSemantics(...)
  • Kept the existing coverage for separate child semantics and merged semantic container behavior
  • Removed the ../widgets/semantics_tester.dart import

Testing

  • ../../bin/flutter analyze test/material/card_test.dart
  • ../../bin/flutter test test/material/card_test.dart

@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Apr 9, 2026

@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 refactors card_test.dart to use SemanticsHandle and matchesSemantics for semantics testing. The feedback recommends using addTearDown for SemanticsHandle disposal to ensure test isolation and removing the manual disposal calls.


testWidgets('Card can take semantic text from multiple children', (WidgetTester tester) async {
final semantics = SemanticsTester(tester);
final SemanticsHandle handle = tester.ensureSemantics();

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

In Flutter tests, it is a best practice to use addTearDown to ensure that resources like SemanticsHandle are properly disposed of even if the test fails. This prevents semantics from remaining enabled and potentially causing flakiness or failures in subsequent tests. Since the handle is not used for anything other than disposal in this test, you can simplify the implementation.

Suggested change
final SemanticsHandle handle = tester.ensureSemantics();
addTearDown(tester.ensureSemantics().dispose);
References
  1. The Flutter repository follows the 'Writing Effective Tests' guide, which advocates for using addTearDown for reliable resource cleanup to ensure test isolation. (link)

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.

Yes, rather than your try / finally approach, use addTearDown

);

semantics.dispose();
handle.dispose();

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

This manual disposal is redundant if addTearDown is used at the beginning of the test.

testWidgets('Card merges children when it is a semanticContainer', (WidgetTester tester) async {
final semantics = SemanticsTester(tester);
debugResetSemanticsIdCounter();
final SemanticsHandle handle = tester.ensureSemantics();

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

As with the previous test, using addTearDown for the SemanticsHandle is the preferred pattern for robust cleanup in the Flutter repository.

Suggested change
final SemanticsHandle handle = tester.ensureSemantics();
addTearDown(tester.ensureSemantics().dispose);
References
  1. The Flutter repository follows the 'Writing Effective Tests' guide, which advocates for using addTearDown for reliable resource cleanup to ensure test isolation. (link)

);

semantics.dispose();
handle.dispose();

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

This manual disposal is redundant if addTearDown is used.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@Sanaullah49

Copy link
Copy Markdown
Contributor Author

Updated the semantics-handle cleanup to dispose in finally, which preserves cleanup if the test body fails while still satisfying Flutter test binding verification that handles are closed before test teardown. Verified locally: ./bin/flutter test packages/flutter/test/material/card_test.dart.

@Sanaullah49

Copy link
Copy Markdown
Contributor Author

The latest push now also has the Material/Cupertino code-freeze guard as the remaining failing check because this PR touches packages/flutter/test/material/**. I do not have permission to add labels on flutter/flutter; could a maintainer please add the override code freeze label if this semantics-test-only cleanup should land during the freeze?

@justinmc justinmc added the override code freeze Override an active code freeze. label Apr 29, 2026
@justinmc justinmc moved this from Todo to In Progress in Test cross-imports Review Queue Apr 29, 2026
@victorsanni victorsanni added the CICD Run CI/CD label Apr 30, 2026
@victorsanni victorsanni self-requested a review April 30, 2026 23:37
@Sanaullah49

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback @justinmc and @gemini-code-assist! Updated both tests to use addTearDown(tester.ensureSemantics().dispose) instead of the try/finally pattern.

@github-actions github-actions Bot removed the CICD Run CI/CD label May 5, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Thanks for the update, @Sanaullah49. Using addTearDown(tester.ensureSemantics().dispose) is indeed the cleaner and more idiomatic approach for ensuring semantics are disposed of correctly in tests. This change looks good.

Replace manual SemanticsHandle + try/finally + dispose with the
addTearDown pattern as requested by @justinmc and @gemini-code-assist.
@Sanaullah49 Sanaullah49 force-pushed the chore/182636-card-semantics branch from a9e5c58 to 7025132 Compare May 5, 2026 05:51
@victorsanni victorsanni added the CICD Run CI/CD label May 5, 2026
victorsanni
victorsanni previously approved these changes May 5, 2026
Comment on lines -172 to -173
ignoreTransform: true,
ignoreRect: true,

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.

Are we losing information by not having these?

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.

Good question — here's what each approach checks vs. what changed:

The old approach used SemanticsTester + hasSemantics with a full TestSemantics tree, which verified the entire semantics tree as a unit including node IDs (1, 2, 3) and exact parent-child structure. The new approach uses tester.getSemantics + matchesSemantics to verify each node's properties individually.

What we keep: all the meaningful semantic properties — label, textDirection, hasTapAction, hasFocusAction, hasEnabledState, isButton, isEnabled, isFocusable.

What we intentionally drop:

  • Node IDs (id: 1, 2, 3 and debugResetSemanticsIdCounter()): IDs are internal counter-based implementation details — not meaningful semantic properties and fragile enough to require a reset helper.
  • Strict full-tree assertion: the old tree check would fail if an unexpected extra sibling appeared; the new individual checks don't catch that. This is the main tradeoff. For this test the goal is to verify the semantic content of each card child, not the exact tree shape, so individual node checks are appropriate and align with the direction most other tests in the repo are moving.

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 response is a wall of text that does nothing to address the point I raised above.

I asked a specific question - if we may be losing information by not specifying ignoreTransform and ignoreRect, and I get a generic, and probably unfiltered AI-generated response containing no mention of ignoreTransform or ignoreRect, but comparing both approaches in general.

Please respect maintainers time by answering questions directly instead of pasting unoriginal responses.

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.

Sorry, the previous reply missed your point. Direct answer:

No, we're not losing information. ignoreTransform: true, ignoreRect: true on the old hasSemantics(...) call told the matcher to skip comparing transform/rect against the TestSemantics literal — and the literal didn't set either field anyway, so the old test wasn't asserting on transform or rect.

matchesSemantics(...) also doesn't check transform/rect by default — _MatchesSemanticsData.matches in matchers.dart only compares rect when a rect: argument is passed, and there's no transform: parameter at all. So the new assertions cover the same surface as before (label, textDirection, actions, flags) with no transform/rect change.

If you'd prefer the new tests do assert rect/size (which the old ones never did), happy to add that — just let me know.

@github-actions github-actions Bot removed the CICD Run CI/CD label May 6, 2026
@dkwingsmt dkwingsmt requested a review from victorsanni May 6, 2026 18:25

@victorsanni victorsanni 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.

@dkwingsmt dkwingsmt added the CICD Run CI/CD label May 13, 2026
testWidgets has semanticsEnabled: true by default, which creates and
disposes a SemanticsHandle automatically. The addTearDown registered
an extra handle that runs AFTER the framework's
_verifySemanticsHandlesWereDisposed check, causing it to fail with
'A SemanticsHandle was active at the end of the test.'

Removing the addTearDown line - tester.getSemantics works against the
auto-created handle.
@github-actions github-actions Bot removed the CICD Run CI/CD label May 15, 2026
@dkwingsmt dkwingsmt requested review from justinmc and victorsanni May 20, 2026 18:21
@dkwingsmt dkwingsmt added the CICD Run CI/CD label May 20, 2026
@Piinks Piinks added Decoupling: Port to flutter/packages This PR is ready to be ported to flutter/packages. We will provide instructions to do so. Decoupling: Pre-release This PR is needed for pre-release of material_ui and cupertino_ui and removed override code freeze Override an active code freeze. labels Jun 24, 2026
@Piinks

Piinks commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

This PR is ready to be ported over to flutter/packages in the new cupertino_ui package!
Instructions for porting this PR over can be found in #188444 along with an example.

This PR is also necessary for pre-release of the cupertino_ui package. If you are unable to work on this right now, no worries! We’ll go ahead and port it over to get it landed. Your contribution is greatly appreciated. 💙

@QuncCccccc

Copy link
Copy Markdown
Contributor

Thanks a lot for your contribution! But seems your another PR #185736 has covered the changes in this PR. So I'll close this one.

@QuncCccccc QuncCccccc closed this Jun 25, 2026
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Test cross-imports Review Queue Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD Decoupling: Port to flutter/packages This PR is ready to be ported to flutter/packages. We will provide instructions to do so. Decoupling: Pre-release This PR is needed for pre-release of material_ui and cupertino_ui f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Projects

Development

Successfully merging this pull request may close these issues.

8 participants