Skip to content

Commit

Permalink
Cover more test/widgets tests with leak tracking flutter#3 (#134576)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksokolovskyi authored Sep 14, 2023
1 parent ba233b8 commit ff10c52
Show file tree
Hide file tree
Showing 21 changed files with 333 additions and 273 deletions.
47 changes: 26 additions & 21 deletions packages/flutter/test/widgets/form_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

void main() {
testWidgets('onSaved callback is called', (WidgetTester tester) async {
testWidgetsWithLeakTracking('onSaved callback is called', (WidgetTester tester) async {
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
String? fieldValue;

Expand Down Expand Up @@ -48,7 +49,7 @@ void main() {
await checkText('');
});

testWidgets('onChanged callback is called', (WidgetTester tester) async {
testWidgetsWithLeakTracking('onChanged callback is called', (WidgetTester tester) async {
String? fieldValue;

Widget builder() {
Expand Down Expand Up @@ -85,7 +86,7 @@ void main() {
await checkText('');
});

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

Expand Down Expand Up @@ -139,7 +140,7 @@ void main() {
await checkErrorText('');
});

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

});

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

testWidgets(
testWidgetsWithLeakTracking(
'isValid returns false when the field is invalid and does not change error display',
(WidgetTester tester) async {
final GlobalKey<FormFieldState<String>> fieldKey1 = GlobalKey<FormFieldState<String>>();
Expand Down Expand Up @@ -272,7 +273,7 @@ void main() {
},
);

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

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

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

testWidgets('Controller defines initial value', (WidgetTester tester) async {
testWidgetsWithLeakTracking('Controller defines initial value', (WidgetTester tester) async {
final TextEditingController controller = TextEditingController(text: 'hello');
addTearDown(controller.dispose);
const String initialValue = 'hello';
final GlobalKey<FormFieldState<String>> inputKey = GlobalKey<FormFieldState<String>>();

Expand Down Expand Up @@ -413,10 +415,11 @@ void main() {
expect(controller.text, equals('world'));
});

testWidgets('TextFormField resets to its initial value', (WidgetTester tester) async {
testWidgetsWithLeakTracking('TextFormField resets to its initial value', (WidgetTester tester) async {
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
final GlobalKey<FormFieldState<String>> inputKey = GlobalKey<FormFieldState<String>>();
final TextEditingController controller = TextEditingController(text: 'Plover');
addTearDown(controller.dispose);

Widget builder() {
return MaterialApp(
Expand Down Expand Up @@ -459,9 +462,11 @@ void main() {
expect(controller.text, equals('Plover'));
});

testWidgets('TextEditingController updates to/from form field value', (WidgetTester tester) async {
testWidgetsWithLeakTracking('TextEditingController updates to/from form field value', (WidgetTester tester) async {
final TextEditingController controller1 = TextEditingController(text: 'Foo');
addTearDown(controller1.dispose);
final TextEditingController controller2 = TextEditingController(text: 'Bar');
addTearDown(controller2.dispose);
final GlobalKey<FormFieldState<String>> inputKey = GlobalKey<FormFieldState<String>>();

TextEditingController? currentController;
Expand Down Expand Up @@ -566,7 +571,7 @@ void main() {
expect(controller2.text, equals('Xyzzy'));
});

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

Expand Down Expand Up @@ -620,7 +625,7 @@ void main() {
expect(formKey.currentState!.validate(), isTrue);
});

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

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

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

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

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

Expand Down Expand Up @@ -743,7 +748,7 @@ void main() {
expect(find.text(errorText(initialValue)!), findsNWidgets(2));
});

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

Widget builder() {
Expand Down Expand Up @@ -773,7 +778,7 @@ void main() {
expect(find.text(errorText('')!), findsOneWidget);
});

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

Expand Down Expand Up @@ -818,7 +823,7 @@ void main() {
});

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

Expand Down Expand Up @@ -855,7 +860,7 @@ void main() {
expect(formKey.currentState!.validate(), isFalse);
});

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

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

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

final Widget widget = MaterialApp(
Expand Down Expand Up @@ -908,7 +913,7 @@ void main() {
expect(fieldKey.currentState!.hasInteractedByUser, isTrue);
});

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

final Widget widget = MaterialApp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

void main() {
testWidgets('FractionallySizedBox', (WidgetTester tester) async {
testWidgetsWithLeakTracking('FractionallySizedBox', (WidgetTester tester) async {
final GlobalKey inner = GlobalKey();
await tester.pumpWidget(OverflowBox(
minWidth: 0.0,
Expand All @@ -29,7 +30,7 @@ void main() {
expect(box.localToGlobal(Offset.zero), equals(const Offset(25.0, 37.5)));
});

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

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

testWidgets('OverflowBox alignment with FractionallySizedBox', (WidgetTester tester) async {
testWidgetsWithLeakTracking('OverflowBox alignment with FractionallySizedBox', (WidgetTester tester) async {
final GlobalKey inner = GlobalKey();
await tester.pumpWidget(Directionality(
textDirection: TextDirection.rtl,
Expand Down
Loading

0 comments on commit ff10c52

Please sign in to comment.