From a10cd03b05c685e72036f2e1471146165c6bf79c Mon Sep 17 00:00:00 2001 From: YoungSeok Yoon Date: Thu, 14 Jul 2016 13:29:49 -0700 Subject: [PATCH] Flutter driver commands for controlling the Input widget (#4913) * Driver commands for controlling the Input widget This commit introduces two new driver commands for controlling the material Input widget. * setInputText(SerializableFinder finder, String text) * submitInputText(SerializableFinder finder) Since it is not possible to directly modify the Input widget text, these driver commands invokes the handler functions of the Input widget: onChanged and onSubmitted, respectively. The `submitInputText` command returns the submitted String as a result. * Make input command handler methods private Addressing comments from @yjbanov. --- packages/flutter_driver/lib/src/driver.dart | 19 ++++++ .../flutter_driver/lib/src/extension.dart | 19 ++++++ packages/flutter_driver/lib/src/input.dart | 62 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 packages/flutter_driver/lib/src/input.dart diff --git a/packages/flutter_driver/lib/src/driver.dart b/packages/flutter_driver/lib/src/driver.dart index e128ab1f7a..2f898b9bc5 100644 --- a/packages/flutter_driver/lib/src/driver.dart +++ b/packages/flutter_driver/lib/src/driver.dart @@ -12,6 +12,7 @@ import 'error.dart'; import 'find.dart'; import 'gesture.dart'; import 'health.dart'; +import 'input.dart'; import 'message.dart'; import 'timeline.dart'; @@ -214,6 +215,24 @@ class FlutterDriver { return null; } + /// Sets the text value of the `Input` widget located by [finder]. + /// + /// This command invokes the `onChanged` handler of the `Input` widget with + /// the provided [text]. + Future setInputText(SerializableFinder finder, String text) async { + await _sendCommand(new SetInputText(finder, text)); + return null; + } + + /// Submits the current text value of the `Input` widget located by [finder]. + /// + /// This command invokes the `onSubmitted` handler of the `Input` widget and + /// the returns the submitted text value. + Future submitInputText(SerializableFinder finder) async { + Map json = await _sendCommand(new SubmitInputText(finder)); + return json['text']; + } + /// Waits until [finder] locates the target. Future waitFor(SerializableFinder finder, {Duration timeout: _kDefaultTimeout}) async { await _sendCommand(new WaitFor(finder, timeout: timeout)); diff --git a/packages/flutter_driver/lib/src/extension.dart b/packages/flutter_driver/lib/src/extension.dart index d15696d0b0..4967925e9c 100644 --- a/packages/flutter_driver/lib/src/extension.dart +++ b/packages/flutter_driver/lib/src/extension.dart @@ -14,6 +14,7 @@ import 'error.dart'; import 'find.dart'; import 'gesture.dart'; import 'health.dart'; +import 'input.dart'; import 'message.dart'; const String _extensionMethodName = 'driver'; @@ -64,6 +65,8 @@ class FlutterDriverExtension { 'get_text': getText, 'scroll': scroll, 'scrollIntoView': scrollIntoView, + 'setInputText': _setInputText, + 'submitInputText': _submitInputText, 'waitFor': waitFor, }); @@ -73,6 +76,8 @@ class FlutterDriverExtension { 'get_text': GetText.deserialize, 'scroll': Scroll.deserialize, 'scrollIntoView': ScrollIntoView.deserialize, + 'setInputText': SetInputText.deserialize, + 'submitInputText': SubmitInputText.deserialize, 'waitFor': WaitFor.deserialize, }); @@ -215,6 +220,20 @@ class FlutterDriverExtension { return new ScrollResult(); } + Future _setInputText(SetInputText command) async { + Finder target = await _waitForElement(_createFinder(command.finder)); + Input input = target.evaluate().single.widget; + input.onChanged(new InputValue(text: command.text)); + return new SetInputTextResult(); + } + + Future _submitInputText(SubmitInputText command) async { + Finder target = await _waitForElement(_createFinder(command.finder)); + Input input = target.evaluate().single.widget; + input.onSubmitted(input.value); + return new SubmitInputTextResult(input.value.text); + } + Future getText(GetText command) async { Finder target = await _waitForElement(_createFinder(command.finder)); // TODO(yjbanov): support more ways to read text diff --git a/packages/flutter_driver/lib/src/input.dart b/packages/flutter_driver/lib/src/input.dart new file mode 100644 index 0000000000..10b7a36bb7 --- /dev/null +++ b/packages/flutter_driver/lib/src/input.dart @@ -0,0 +1,62 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'message.dart'; +import 'find.dart'; + +class SetInputText extends CommandWithTarget { + @override + final String kind = 'setInputText'; + + SetInputText(SerializableFinder finder, this.text) : super(finder); + + final String text; + + static SetInputText deserialize(Map json) { + String text = json['text']; + return new SetInputText(SerializableFinder.deserialize(json), text); + } + + @override + Map serialize() { + Map json = super.serialize(); + json['text'] = text; + return json; + } +} + +class SetInputTextResult extends Result { + static SetInputTextResult fromJson(Map json) { + return new SetInputTextResult(); + } + + @override + Map toJson() => {}; +} + +class SubmitInputText extends CommandWithTarget { + @override + final String kind = 'submitInputText'; + + SubmitInputText(SerializableFinder finder) : super(finder); + + static SubmitInputText deserialize(Map json) { + return new SubmitInputText(SerializableFinder.deserialize(json)); + } +} + +class SubmitInputTextResult extends Result { + SubmitInputTextResult(this.text); + + final String text; + + static SubmitInputTextResult fromJson(Map json) { + return new SubmitInputTextResult(json['text']); + } + + @override + Map toJson() => { + 'text': text + }; +}