From 9f5eeb1beeb8192fd6463d81d87d59b4c4e0a51f Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Tue, 20 Sep 2016 23:13:30 -0400 Subject: [PATCH] Unittest to check Input box initial value (#5976) Adds unit test to verify the initial value of Input widgets. --- AUTHORS | 1 + packages/flutter/test/widget/form_test.dart | 64 ++++++++++++++++----- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/AUTHORS b/AUTHORS index 223d9a985b..5d0cac1742 100644 --- a/AUTHORS +++ b/AUTHORS @@ -5,3 +5,4 @@ Google Inc. Jim Simon +Lex Berezhny diff --git a/packages/flutter/test/widget/form_test.dart b/packages/flutter/test/widget/form_test.dart index aab0b1a534..24fb8f4817 100644 --- a/packages/flutter/test/widget/form_test.dart +++ b/packages/flutter/test/widget/form_test.dart @@ -10,6 +10,7 @@ class MockKeyboard extends mojom.KeyboardProxy { MockKeyboard() : super.unbound(); mojom.KeyboardClient client; + mojom.EditingState currentState; @override void setClient(mojom.KeyboardClientStub client, mojom.KeyboardConfiguration configuraiton) { @@ -23,7 +24,10 @@ class MockKeyboard extends mojom.KeyboardProxy { void hide() {} @override - void setEditingState(mojom.EditingState state) {} + void setEditingState(mojom.EditingState state) { + currentState = state; + } + } void main() { @@ -43,7 +47,6 @@ void main() { } testWidgets('Setter callback is called', (WidgetTester tester) async { - GlobalKey inputKey = new GlobalKey(); String fieldValue; Widget builder() { @@ -51,9 +54,8 @@ void main() { child: new Material( child: new Form( child: new Input( - key: inputKey, formField: new FormField( - setter: (String val) { fieldValue = val; } + setter: (String value) { fieldValue = value; } ) ) ) @@ -63,12 +65,12 @@ void main() { await tester.pumpWidget(builder()); - Future checkText(String testValue) { - enterText(testValue); + expect(fieldValue, isNull); - // Check that the FormField's setter was called. + Future 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 @@ void main() { Future 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 @@ void main() { new Input( key: inputKey, formField: new FormField( - setter: (String val) { fieldValue = val; } + setter: (String value) { fieldValue = value; } ) ), new Input( @@ -150,7 +150,7 @@ void main() { Future checkErrorText(String testValue) async { enterText(testValue); - await tester.pumpWidget(builder()); + await tester.pump(); expect(fieldValue, equals(testValue)); @@ -162,4 +162,42 @@ void main() { 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( + 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')); + + }); }