[cupertino_ui] Migrate sliding_segmented_control_test.dart to SemanticsHandle#11979
Conversation
There was a problem hiding this comment.
Code Review
This pull request removes the skip annotation and refactors the semantics test in sliding_segmented_control_test.dart to use tester.ensureSemantics() and isSemantics assertions instead of SemanticsTester. The review feedback recommends using addTearDown to ensure the SemanticsHandle is properly disposed of even if assertions fail, and removing the manual dispose call at the end of the test.
|
|
||
| testWidgets('Segmented control semantics', (WidgetTester tester) async { | ||
| final semantics = SemanticsTester(tester); | ||
| final SemanticsHandle handle = tester.ensureSemantics(); |
There was a problem hiding this comment.
Using addTearDown(handle.dispose) right after creating the SemanticsHandle is safer than calling handle.dispose() at the end of the test. If any of the assertions (expect) fail, the test will throw an exception and terminate early, which would skip the manual handle.dispose() call at the end and leak the semantics handle to subsequent tests. Using addTearDown ensures cleanup runs even on test failure.
final SemanticsHandle handle = tester.ensureSemantics();
addTearDown(handle.dispose);There was a problem hiding this comment.
This is how the test was previously structured, no addTearDown was previously used. Also explanation from gemini below as to why the test fails if we do use addTearDown(handle.dispose).
From gemini:
Flutter's TestWidgetsFlutterBinding runs its end-of-test leak verifications (specifically _verifySemanticsHandlesWereDisposed) inside the test body execution flow, immediately after your test callback completes, but before returning control to the Dart test runner:
1. [Test Runner] Starts test
2. [Flutter Binding] Runs test body (your async callback)
3. Your test code executes...
4. Your test code completes.
5. [Flutter Binding] Runs _endOfTestVerifications() <-- CRITICAL POINT
6. Checking active SemanticsHandles...
ERROR: "A SemanticsHandle was active at the end of the test."
7. [Test Runner] Runs addTearDown callbacks <-- TOO LATE!
8. handle.dispose() is called here.
Because the verification (Step 5) happens before the test runner runs the teardown callbacks (Step 7), the binding sees that the SemanticsHandle is still active and throws a FlutterError, failing the test.
| ); | ||
|
|
||
| semantics.dispose(); | ||
| handle.dispose(); |
There was a problem hiding this comment.
sliding_segmented_control_test.dart to SemanticsHandlesliding_segmented_control_test.dart to SemanticsHandle
Part of flutter/flutter#182636 and flutter/flutter#188395
This PR:
widgets/semantics_tester.dart. ReplacedSemanticsTesterwithSemanticsHandle.@Skipannotation, all tests in this file has passed.semantics_tester.darthas existed incupertino_ui, so we can directly importsemantics_tester.dart;test/folder.Pre-Review Checklist
[shared_preferences]///).