Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

@Skip(
'This file is skipped due to a cross-import that needs to be fixed. Tracked in https://github.com/flutter/flutter/issues/177028.',
)
import 'package:cupertino_ui/cupertino_ui.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
Expand All @@ -13,8 +10,6 @@ import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

import '../widgets/semantics_tester.dart';

const TextStyle testStyle = TextStyle(fontSize: 10.0, letterSpacing: 0.0);

void main() {
Expand Down Expand Up @@ -348,7 +343,8 @@ void main() {
});

testWidgets('Cupertino button is semantically a button', (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

To ensure that the SemanticsHandle is always disposed of, even if an assertion/expectation fails during the test, it is recommended to use addTearDown(handle.dispose) immediately after creating it.

Suggested change
final SemanticsHandle handle = tester.ensureSemantics();
final SemanticsHandle handle = tester.ensureSemantics();
addTearDown(handle.dispose);

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.

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.


await tester.pumpWidget(
boilerplate(
child: Center(
Expand All @@ -358,24 +354,17 @@ void main() {
);

expect(
semantics,
hasSemantics(
TestSemantics.root(
children: <TestSemantics>[
TestSemantics.rootChild(
actions: SemanticsAction.tap.index | SemanticsAction.focus.index,
label: 'ABC',
flags: <SemanticsFlag>[SemanticsFlag.isButton, SemanticsFlag.isFocusable],
),
],
),
ignoreId: true,
ignoreRect: true,
ignoreTransform: true,
tester.getSemantics(find.text('ABC')),
isSemantics(
label: 'ABC',
isButton: true,
isFocusable: true,
hasTapAction: true,
hasFocusAction: true,
),
);

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

Since addTearDown(handle.dispose) is now used to clean up the SemanticsHandle, this manual call to handle.dispose() can be removed.

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.

});

testWidgets('Can specify colors', (WidgetTester tester) async {
Expand Down
Loading