Remove semantics_tester import from card_test.dart#184808
Conversation
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
| final SemanticsHandle handle = tester.ensureSemantics(); | |
| addTearDown(tester.ensureSemantics().dispose); |
References
- The Flutter repository follows the 'Writing Effective Tests' guide, which advocates for using addTearDown for reliable resource cleanup to ensure test isolation. (link)
There was a problem hiding this comment.
Yes, rather than your try / finally approach, use addTearDown
| ); | ||
|
|
||
| semantics.dispose(); | ||
| handle.dispose(); |
| testWidgets('Card merges children when it is a semanticContainer', (WidgetTester tester) async { | ||
| final semantics = SemanticsTester(tester); | ||
| debugResetSemanticsIdCounter(); | ||
| final SemanticsHandle handle = tester.ensureSemantics(); |
There was a problem hiding this comment.
As with the previous test, using addTearDown for the SemanticsHandle is the preferred pattern for robust cleanup in the Flutter repository.
| final SemanticsHandle handle = tester.ensureSemantics(); | |
| addTearDown(tester.ensureSemantics().dispose); |
References
- 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(); |
|
Updated the semantics-handle cleanup to dispose in |
|
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 |
|
Thanks for the feedback @justinmc and @gemini-code-assist! Updated both tests to use |
|
Thanks for the update, @Sanaullah49. Using |
Replace manual SemanticsHandle + try/finally + dispose with the addTearDown pattern as requested by @justinmc and @gemini-code-assist.
a9e5c58 to
7025132
Compare
| ignoreTransform: true, | ||
| ignoreRect: true, |
There was a problem hiding this comment.
Are we losing information by not having these?
There was a problem hiding this comment.
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, 3anddebugResetSemanticsIdCounter()): 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.
|
This PR is ready to be ported over to flutter/packages in the new cupertino_ui package! 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. 💙 |
|
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. |
Part of #182636
Summary
Remove the
semantics_tester.dartcross-import fromcard_test.dart.What changed
SemanticsTester/TestSemanticsassertions in the Card semantics tests with direct semantics lookups andmatchesSemantics(...)../widgets/semantics_tester.dartimportTesting
../../bin/flutter analyze test/material/card_test.dart../../bin/flutter test test/material/card_test.dart