Skip to content

Commit ff10c52

Browse files
authored
Cover more test/widgets tests with leak tracking #3 (flutter#134576)
1 parent ba233b8 commit ff10c52

21 files changed

+333
-273
lines changed

packages/flutter/test/widgets/form_test.dart

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import 'package:flutter/material.dart';
66
import 'package:flutter/rendering.dart';
77
import 'package:flutter/services.dart';
88
import 'package:flutter_test/flutter_test.dart';
9+
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
910

1011
void main() {
11-
testWidgets('onSaved callback is called', (WidgetTester tester) async {
12+
testWidgetsWithLeakTracking('onSaved callback is called', (WidgetTester tester) async {
1213
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
1314
String? fieldValue;
1415

@@ -48,7 +49,7 @@ void main() {
4849
await checkText('');
4950
});
5051

51-
testWidgets('onChanged callback is called', (WidgetTester tester) async {
52+
testWidgetsWithLeakTracking('onChanged callback is called', (WidgetTester tester) async {
5253
String? fieldValue;
5354

5455
Widget builder() {
@@ -85,7 +86,7 @@ void main() {
8586
await checkText('');
8687
});
8788

88-
testWidgets('Validator sets the error text only when validate is called', (WidgetTester tester) async {
89+
testWidgetsWithLeakTracking('Validator sets the error text only when validate is called', (WidgetTester tester) async {
8990
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
9091
String? errorText(String? value) => '${value ?? ''}/error';
9192

@@ -139,7 +140,7 @@ void main() {
139140
await checkErrorText('');
140141
});
141142

142-
testWidgets('Should announce error text when validate returns error', (WidgetTester tester) async {
143+
testWidgetsWithLeakTracking('Should announce error text when validate returns error', (WidgetTester tester) async {
143144
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
144145
await tester.pumpWidget(
145146
MaterialApp(
@@ -178,7 +179,7 @@ void main() {
178179

179180
});
180181

181-
testWidgets('isValid returns true when a field is valid', (WidgetTester tester) async {
182+
testWidgetsWithLeakTracking('isValid returns true when a field is valid', (WidgetTester tester) async {
182183
final GlobalKey<FormFieldState<String>> fieldKey1 = GlobalKey<FormFieldState<String>>();
183184
final GlobalKey<FormFieldState<String>> fieldKey2 = GlobalKey<FormFieldState<String>>();
184185
const String validString = 'Valid string';
@@ -223,7 +224,7 @@ void main() {
223224
expect(fieldKey2.currentState!.isValid, isTrue);
224225
});
225226

226-
testWidgets(
227+
testWidgetsWithLeakTracking(
227228
'isValid returns false when the field is invalid and does not change error display',
228229
(WidgetTester tester) async {
229230
final GlobalKey<FormFieldState<String>> fieldKey1 = GlobalKey<FormFieldState<String>>();
@@ -272,7 +273,7 @@ void main() {
272273
},
273274
);
274275

275-
testWidgets('Multiple TextFormFields communicate', (WidgetTester tester) async {
276+
testWidgetsWithLeakTracking('Multiple TextFormFields communicate', (WidgetTester tester) async {
276277
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
277278
final GlobalKey<FormFieldState<String>> fieldKey = GlobalKey<FormFieldState<String>>();
278279
// Input 2's validator depends on a input 1's value.
@@ -322,7 +323,7 @@ void main() {
322323
await checkErrorText('');
323324
});
324325

325-
testWidgets('Provide initial value to input when no controller is specified', (WidgetTester tester) async {
326+
testWidgetsWithLeakTracking('Provide initial value to input when no controller is specified', (WidgetTester tester) async {
326327
const String initialValue = 'hello';
327328
final GlobalKey<FormFieldState<String>> inputKey = GlobalKey<FormFieldState<String>>();
328329

@@ -366,8 +367,9 @@ void main() {
366367
expect(editableText.widget.controller.text, equals('world'));
367368
});
368369

369-
testWidgets('Controller defines initial value', (WidgetTester tester) async {
370+
testWidgetsWithLeakTracking('Controller defines initial value', (WidgetTester tester) async {
370371
final TextEditingController controller = TextEditingController(text: 'hello');
372+
addTearDown(controller.dispose);
371373
const String initialValue = 'hello';
372374
final GlobalKey<FormFieldState<String>> inputKey = GlobalKey<FormFieldState<String>>();
373375

@@ -413,10 +415,11 @@ void main() {
413415
expect(controller.text, equals('world'));
414416
});
415417

416-
testWidgets('TextFormField resets to its initial value', (WidgetTester tester) async {
418+
testWidgetsWithLeakTracking('TextFormField resets to its initial value', (WidgetTester tester) async {
417419
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
418420
final GlobalKey<FormFieldState<String>> inputKey = GlobalKey<FormFieldState<String>>();
419421
final TextEditingController controller = TextEditingController(text: 'Plover');
422+
addTearDown(controller.dispose);
420423

421424
Widget builder() {
422425
return MaterialApp(
@@ -459,9 +462,11 @@ void main() {
459462
expect(controller.text, equals('Plover'));
460463
});
461464

462-
testWidgets('TextEditingController updates to/from form field value', (WidgetTester tester) async {
465+
testWidgetsWithLeakTracking('TextEditingController updates to/from form field value', (WidgetTester tester) async {
463466
final TextEditingController controller1 = TextEditingController(text: 'Foo');
467+
addTearDown(controller1.dispose);
464468
final TextEditingController controller2 = TextEditingController(text: 'Bar');
469+
addTearDown(controller2.dispose);
465470
final GlobalKey<FormFieldState<String>> inputKey = GlobalKey<FormFieldState<String>>();
466471

467472
TextEditingController? currentController;
@@ -566,7 +571,7 @@ void main() {
566571
expect(controller2.text, equals('Xyzzy'));
567572
});
568573

569-
testWidgets('No crash when a TextFormField is removed from the tree', (WidgetTester tester) async {
574+
testWidgetsWithLeakTracking('No crash when a TextFormField is removed from the tree', (WidgetTester tester) async {
570575
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
571576
String? fieldValue;
572577

@@ -620,7 +625,7 @@ void main() {
620625
expect(formKey.currentState!.validate(), isTrue);
621626
});
622627

623-
testWidgets('Does not auto-validate before value changes when autovalidateMode is set to onUserInteraction', (WidgetTester tester) async {
628+
testWidgetsWithLeakTracking('Does not auto-validate before value changes when autovalidateMode is set to onUserInteraction', (WidgetTester tester) async {
624629
late FormFieldState<String> formFieldState;
625630

626631
String? errorText(String? value) => '$value/error';
@@ -656,7 +661,7 @@ void main() {
656661
expect(find.text(errorText('foo')!), findsNothing);
657662
});
658663

659-
testWidgets('auto-validate before value changes if autovalidateMode was set to always', (WidgetTester tester) async {
664+
testWidgetsWithLeakTracking('auto-validate before value changes if autovalidateMode was set to always', (WidgetTester tester) async {
660665
late FormFieldState<String> formFieldState;
661666

662667
String? errorText(String? value) => '$value/error';
@@ -689,7 +694,7 @@ void main() {
689694
expect(formFieldState.hasError, isTrue);
690695
});
691696

692-
testWidgets('Form auto-validates form fields only after one of them changes if autovalidateMode is onUserInteraction', (WidgetTester tester) async {
697+
testWidgetsWithLeakTracking('Form auto-validates form fields only after one of them changes if autovalidateMode is onUserInteraction', (WidgetTester tester) async {
693698
const String initialValue = 'foo';
694699
String? errorText(String? value) => 'error/$value';
695700

@@ -743,7 +748,7 @@ void main() {
743748
expect(find.text(errorText(initialValue)!), findsNWidgets(2));
744749
});
745750

746-
testWidgets('Form auto-validates form fields even before any have changed if autovalidateMode is set to always', (WidgetTester tester) async {
751+
testWidgetsWithLeakTracking('Form auto-validates form fields even before any have changed if autovalidateMode is set to always', (WidgetTester tester) async {
747752
String? errorText(String? value) => 'error/$value';
748753

749754
Widget builder() {
@@ -773,7 +778,7 @@ void main() {
773778
expect(find.text(errorText('')!), findsOneWidget);
774779
});
775780

776-
testWidgets('Form.reset() resets form fields, and auto validation will only happen on the next user interaction if autovalidateMode is onUserInteraction', (WidgetTester tester) async {
781+
testWidgetsWithLeakTracking('Form.reset() resets form fields, and auto validation will only happen on the next user interaction if autovalidateMode is onUserInteraction', (WidgetTester tester) async {
777782
final GlobalKey<FormState> formState = GlobalKey<FormState>();
778783
String? errorText(String? value) => '$value/error';
779784

@@ -818,7 +823,7 @@ void main() {
818823
});
819824

820825
// Regression test for https://github.com/flutter/flutter/issues/63753.
821-
testWidgets('Validate form should return correct validation if the value is composing', (WidgetTester tester) async {
826+
testWidgetsWithLeakTracking('Validate form should return correct validation if the value is composing', (WidgetTester tester) async {
822827
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
823828
String? fieldValue;
824829

@@ -855,7 +860,7 @@ void main() {
855860
expect(formKey.currentState!.validate(), isFalse);
856861
});
857862

858-
testWidgets('hasInteractedByUser returns false when the input has not changed', (WidgetTester tester) async {
863+
testWidgetsWithLeakTracking('hasInteractedByUser returns false when the input has not changed', (WidgetTester tester) async {
859864
final GlobalKey<FormFieldState<String>> fieldKey = GlobalKey<FormFieldState<String>>();
860865

861866
final Widget widget = MaterialApp(
@@ -879,7 +884,7 @@ void main() {
879884
expect(fieldKey.currentState!.hasInteractedByUser, isFalse);
880885
});
881886

882-
testWidgets('hasInteractedByUser returns true after the input has changed', (WidgetTester tester) async {
887+
testWidgetsWithLeakTracking('hasInteractedByUser returns true after the input has changed', (WidgetTester tester) async {
883888
final GlobalKey<FormFieldState<String>> fieldKey = GlobalKey<FormFieldState<String>>();
884889

885890
final Widget widget = MaterialApp(
@@ -908,7 +913,7 @@ void main() {
908913
expect(fieldKey.currentState!.hasInteractedByUser, isTrue);
909914
});
910915

911-
testWidgets('hasInteractedByUser returns false after the field is reset', (WidgetTester tester) async {
916+
testWidgetsWithLeakTracking('hasInteractedByUser returns false after the field is reset', (WidgetTester tester) async {
912917
final GlobalKey<FormFieldState<String>> fieldKey = GlobalKey<FormFieldState<String>>();
913918

914919
final Widget widget = MaterialApp(

packages/flutter/test/widgets/fractionally_sized_box_test.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
import 'package:flutter/widgets.dart';
66
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
78

89
void main() {
9-
testWidgets('FractionallySizedBox', (WidgetTester tester) async {
10+
testWidgetsWithLeakTracking('FractionallySizedBox', (WidgetTester tester) async {
1011
final GlobalKey inner = GlobalKey();
1112
await tester.pumpWidget(OverflowBox(
1213
minWidth: 0.0,
@@ -29,7 +30,7 @@ void main() {
2930
expect(box.localToGlobal(Offset.zero), equals(const Offset(25.0, 37.5)));
3031
});
3132

32-
testWidgets('FractionallySizedBox alignment', (WidgetTester tester) async {
33+
testWidgetsWithLeakTracking('FractionallySizedBox alignment', (WidgetTester tester) async {
3334
final GlobalKey inner = GlobalKey();
3435
await tester.pumpWidget(Directionality(
3536
textDirection: TextDirection.rtl,
@@ -45,7 +46,7 @@ void main() {
4546
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(800.0 - 400.0 / 2.0, 0.0 + 300.0 / 2.0)));
4647
});
4748

48-
testWidgets('FractionallySizedBox alignment (direction-sensitive)', (WidgetTester tester) async {
49+
testWidgetsWithLeakTracking('FractionallySizedBox alignment (direction-sensitive)', (WidgetTester tester) async {
4950
final GlobalKey inner = GlobalKey();
5051
await tester.pumpWidget(Directionality(
5152
textDirection: TextDirection.rtl,
@@ -61,7 +62,7 @@ void main() {
6162
expect(box.localToGlobal(box.size.center(Offset.zero)), equals(const Offset(0.0 + 400.0 / 2.0, 0.0 + 300.0 / 2.0)));
6263
});
6364

64-
testWidgets('OverflowBox alignment with FractionallySizedBox', (WidgetTester tester) async {
65+
testWidgetsWithLeakTracking('OverflowBox alignment with FractionallySizedBox', (WidgetTester tester) async {
6566
final GlobalKey inner = GlobalKey();
6667
await tester.pumpWidget(Directionality(
6768
textDirection: TextDirection.rtl,

0 commit comments

Comments
 (0)