Skip to content

Commit 6de42a7

Browse files
authored
Fix how tests count open SemanticsHandles (#121571)
Fix how tests count open SemanticsHandles
1 parent f97a515 commit 6de42a7

27 files changed

Lines changed: 219 additions & 85 deletions

packages/flutter/lib/src/semantics/binding.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ mixin SemanticsBinding on BindingBase {
6767
_semanticsEnabled.removeListener(listener);
6868
}
6969

70+
/// The number of clients registered to listen for semantics.
71+
///
72+
/// The number is increased whenever [ensureSemantics] is called and decreased
73+
/// when [SemanticsHandle.dispose] is called.
74+
int get debugOutstandingSemanticsHandles => _outstandingHandles;
7075
int _outstandingHandles = 0;
7176

7277
/// Creates a new [SemanticsHandle] and requests the collection of semantics

packages/flutter/test/cupertino/dialog_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,7 @@ void main() {
12471247
label: 'Custom label',
12481248
flags: <SemanticsFlag>[SemanticsFlag.namesRoute],
12491249
)));
1250+
semantics.dispose();
12501251
});
12511252

12521253
testWidgets('CupertinoDialogRoute is state restorable', (WidgetTester tester) async {

packages/flutter/test/cupertino/route_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,7 @@ void main() {
14841484
label: 'Dismiss',
14851485
)));
14861486
debugDefaultTargetPlatformOverride = null;
1487+
semantics.dispose();
14871488
});
14881489

14891490
testWidgets('showCupertinoModalPopup allows for semantics dismiss when set', (WidgetTester tester) async {
@@ -1519,6 +1520,7 @@ void main() {
15191520
label: 'Dismiss',
15201521
));
15211522
debugDefaultTargetPlatformOverride = null;
1523+
semantics.dispose();
15221524
});
15231525

15241526
testWidgets('showCupertinoModalPopup passes RouteSettings to PopupRoute', (WidgetTester tester) async {

packages/flutter/test/material/calendar_date_picker_test.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,6 @@ void main() {
657657
group('Semantics', () {
658658
testWidgets('day mode', (WidgetTester tester) async {
659659
final SemanticsHandle semantics = tester.ensureSemantics();
660-
addTearDown(semantics.dispose);
661660

662661
await tester.pumpWidget(calendarDatePicker());
663662

@@ -837,11 +836,11 @@ void main() {
837836
hasTapAction: true,
838837
isFocusable: true,
839838
));
839+
semantics.dispose();
840840
});
841841

842842
testWidgets('calendar year mode', (WidgetTester tester) async {
843843
final SemanticsHandle semantics = tester.ensureSemantics();
844-
addTearDown(semantics.dispose);
845844

846845
await tester.pumpWidget(calendarDatePicker(
847846
initialCalendarMode: DatePickerMode.year,
@@ -863,8 +862,8 @@ void main() {
863862
isButton: true,
864863
));
865864
}
865+
semantics.dispose();
866866
});
867-
868867
});
869868
});
870869

packages/flutter/test/material/date_picker_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,6 @@ void main() {
814814
group('Semantics', () {
815815
testWidgets('calendar mode', (WidgetTester tester) async {
816816
final SemanticsHandle semantics = tester.ensureSemantics();
817-
addTearDown(semantics.dispose);
818817

819818
await prepareDatePicker(tester, (Future<DateTime?> date) async {
820819
// Header
@@ -858,11 +857,11 @@ void main() {
858857
isFocusable: true,
859858
));
860859
});
860+
semantics.dispose();
861861
});
862862

863863
testWidgets('input mode', (WidgetTester tester) async {
864864
final SemanticsHandle semantics = tester.ensureSemantics();
865-
addTearDown(semantics.dispose);
866865

867866
initialEntryMode = DatePickerEntryMode.input;
868867
await prepareDatePicker(tester, (Future<DateTime?> date) async {
@@ -901,6 +900,7 @@ void main() {
901900
isFocusable: true,
902901
));
903902
});
903+
semantics.dispose();
904904
});
905905
});
906906

packages/flutter/test/material/date_range_picker_test.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,21 +1086,21 @@ void main() {
10861086
});
10871087
});
10881088

1089-
group('Semantics', () {
1090-
testWidgets('calendar mode', (WidgetTester tester) async {
1091-
final SemanticsHandle semantics = tester.ensureSemantics();
1092-
currentDate = DateTime(2016, DateTime.january, 30);
1093-
addTearDown(semantics.dispose);
1094-
await preparePicker(tester, (Future<DateTimeRange?> range) async {
1095-
expect(
1096-
tester.getSemantics(find.text('30')),
1097-
matchesSemantics(
1098-
label: '30, Saturday, January 30, 2016, Today',
1099-
hasTapAction: true,
1100-
isFocusable: true,
1101-
),
1102-
);
1103-
});
1089+
group('Semantics', () {
1090+
testWidgets('calendar mode', (WidgetTester tester) async {
1091+
final SemanticsHandle semantics = tester.ensureSemantics();
1092+
currentDate = DateTime(2016, DateTime.january, 30);
1093+
await preparePicker(tester, (Future<DateTimeRange?> range) async {
1094+
expect(
1095+
tester.getSemantics(find.text('30')),
1096+
matchesSemantics(
1097+
label: '30, Saturday, January 30, 2016, Today',
1098+
hasTapAction: true,
1099+
isFocusable: true,
1100+
),
1101+
);
1102+
});
1103+
semantics.dispose();
11041104
});
11051105
});
11061106
}

packages/flutter/test/material/dialog_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,6 +2505,7 @@ void main() {
25052505
label: 'Custom label',
25062506
flags: <SemanticsFlag>[SemanticsFlag.namesRoute],
25072507
)));
2508+
semantics.dispose();
25082509
});
25092510

25102511
testWidgets('DialogRoute is state restorable', (WidgetTester tester) async {

packages/flutter/test/material/input_date_picker_form_field_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ void main() {
262262

263263
testWidgets('Semantics', (WidgetTester tester) async {
264264
final SemanticsHandle semantics = tester.ensureSemantics();
265-
addTearDown(semantics.dispose);
266265

267266
// Fill the clipboard so that the Paste option is available in the text
268267
// selection menu.
@@ -287,6 +286,7 @@ void main() {
287286
hasMoveCursorBackwardByCharacterAction: true,
288287
hasMoveCursorBackwardByWordAction: true,
289288
));
289+
semantics.dispose();
290290
});
291291

292292
testWidgets('InputDecorationTheme is honored', (WidgetTester tester) async {

packages/flutter/test/material/text_field_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4514,6 +4514,7 @@ void main() {
45144514
),
45154515
],
45164516
), ignoreTransform: true, ignoreRect: true));
4517+
semantics.dispose();
45174518
});
45184519

45194520
testWidgets('TextField with specified suffixStyle', (WidgetTester tester) async {

packages/flutter/test/semantics/semantics_owner_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ void main() {
1313
// Regression test for https://github.com/flutter/flutter/issues/100358.
1414

1515
final SemanticsTester semantics = SemanticsTester(tester);
16-
addTearDown(semantics.dispose);
1716

1817
await tester.pumpWidget(
1918
Directionality(
@@ -44,5 +43,6 @@ void main() {
4443
type: SemanticsAction.showOnScreen,
4544
nodeId: nodeId,
4645
));
46+
semantics.dispose();
4747
});
4848
}

0 commit comments

Comments
 (0)