Unittest to check Input box initial value (#5976)
Adds unit test to verify the initial value of Input widgets.
diff --git a/AUTHORS b/AUTHORS
index 223d9a9..5d0cac1 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -5,3 +5,4 @@
Google Inc.
Jim Simon <jim.j.simon@gmail.com>
+Lex Berezhny <lex@damoti.com>
diff --git a/packages/flutter/test/widget/form_test.dart b/packages/flutter/test/widget/form_test.dart
index aab0b1a..24fb8f4 100644
--- a/packages/flutter/test/widget/form_test.dart
+++ b/packages/flutter/test/widget/form_test.dart
@@ -10,6 +10,7 @@
MockKeyboard() : super.unbound();
mojom.KeyboardClient client;
+ mojom.EditingState currentState;
@override
void setClient(mojom.KeyboardClientStub client, mojom.KeyboardConfiguration configuraiton) {
@@ -23,7 +24,10 @@
void hide() {}
@override
- void setEditingState(mojom.EditingState state) {}
+ void setEditingState(mojom.EditingState state) {
+ currentState = state;
+ }
+
}
void main() {
@@ -43,7 +47,6 @@
}
testWidgets('Setter callback is called', (WidgetTester tester) async {
- GlobalKey inputKey = new GlobalKey();
String fieldValue;
Widget builder() {
@@ -51,9 +54,8 @@
child: new Material(
child: new Form(
child: new Input(
- key: inputKey,
formField: new FormField<String>(
- setter: (String val) { fieldValue = val; }
+ setter: (String value) { fieldValue = value; }
)
)
)
@@ -63,12 +65,12 @@
await tester.pumpWidget(builder());
- Future<Null> checkText(String testValue) {
- enterText(testValue);
+ expect(fieldValue, isNull);
- // Check that the FormField's setter was called.
+ Future<Null> checkText(String testValue) async {
+ enterText(testValue);
+ // pump'ing is unnecessary because callback happens regardless of frames
expect(fieldValue, equals(testValue));
- return tester.pumpWidget(builder());
}
await checkText('Test');
@@ -98,11 +100,9 @@
Future<Null> checkErrorText(String testValue) async {
enterText(testValue);
- await tester.pumpWidget(builder());
-
+ await tester.pump();
// Check for a new Text widget with our error text.
expect(find.text(errorText(testValue)), findsOneWidget);
- return null;
}
await checkErrorText('Test');
@@ -128,7 +128,7 @@
new Input(
key: inputKey,
formField: new FormField<String>(
- setter: (String val) { fieldValue = val; }
+ setter: (String value) { fieldValue = value; }
)
),
new Input(
@@ -150,7 +150,7 @@
Future<Null> checkErrorText(String testValue) async {
enterText(testValue);
- await tester.pumpWidget(builder());
+ await tester.pump();
expect(fieldValue, equals(testValue));
@@ -162,4 +162,42 @@
await checkErrorText('Test');
await checkErrorText('');
});
+
+ testWidgets('Provide initial value to input', (WidgetTester tester) async {
+ String initialValue = 'hello';
+ String currentValue;
+
+ Widget builder() {
+ return new Center(
+ child: new Material(
+ child: new Form(
+ child: new Input(
+ value: new InputValue(text: initialValue),
+ formField: new FormField<String>(
+ setter: (String value) { currentValue = value; }
+ )
+ )
+ )
+ )
+ );
+ }
+
+ await tester.pumpWidget(builder());
+
+ // initial value should be loaded into keyboard editing state
+ expect(mockKeyboard.currentState, isNotNull);
+ expect(mockKeyboard.currentState.text, equals(initialValue));
+
+ // initial value should also be visible in the raw input line
+ RawInputLineState editableText = tester.state(find.byType(RawInputLine));
+ expect(editableText.config.value.text, equals(initialValue));
+
+ // sanity check, make sure we can still edit the text and everything updates
+ expect(currentValue, isNull);
+ enterText('world');
+ expect(currentValue, equals('world'));
+ await tester.pump();
+ expect(editableText.config.value.text, equals('world'));
+
+ });
}