From d1e918fa102038bc2d3a3248e483753a8105d5a5 Mon Sep 17 00:00:00 2001 From: Yegor Date: Wed, 6 Dec 2017 19:59:07 -0800 Subject: [PATCH] support disabling text entry emulation (#13410) --- .../ui/test_driver/keyboard_resize_test.dart | 1 + .../flutter_driver/lib/src/common/text.dart | 36 +++++++++++++++++++ .../flutter_driver/lib/src/driver/driver.dart | 9 +++++ .../lib/src/extension/extension.dart | 12 +++++++ .../flutter_test/lib/src/test_text_input.dart | 9 +++++ 5 files changed, 67 insertions(+) diff --git a/dev/integration_tests/ui/test_driver/keyboard_resize_test.dart b/dev/integration_tests/ui/test_driver/keyboard_resize_test.dart index f10dff665f..a6a7710e67 100644 --- a/dev/integration_tests/ui/test_driver/keyboard_resize_test.dart +++ b/dev/integration_tests/ui/test_driver/keyboard_resize_test.dart @@ -21,6 +21,7 @@ void main() { }); test('Ensure keyboard dismissal resizes the view to original size', () async { + await driver.setTextEntryEmulation(enabled: false); final SerializableFinder heightText = find.byValueKey(keys.kHeightText); await driver.waitFor(heightText); diff --git a/packages/flutter_driver/lib/src/common/text.dart b/packages/flutter_driver/lib/src/common/text.dart index bf9990c37d..352dd77998 100644 --- a/packages/flutter_driver/lib/src/common/text.dart +++ b/packages/flutter_driver/lib/src/common/text.dart @@ -71,3 +71,39 @@ class EnterTextResult extends Result { @override Map toJson() => const {}; } + +/// A Flutter Driver command that enables and disables text entry emulation. +class SetTextEntryEmulation extends Command { + /// Creates a command that enables and disables text entry emulation. + SetTextEntryEmulation(this.enabled, { Duration timeout }) : super(timeout: timeout); + + /// Whether text entry emulation should be enabled. + final bool enabled; + + /// Deserializes this command from the value generated by [serialize]. + SetTextEntryEmulation.deserialize(Map json) + : enabled = json['enabled'] == 'true', + super.deserialize(json); + + @override + final String kind = 'set_text_entry_emulation'; + + @override + Map serialize() => super.serialize()..addAll({ + 'enabled': '$enabled', + }); +} + +/// The result of the [SetTextEntryEmulation] command. +class SetTextEntryEmulationResult extends Result { + /// Creates a successful result. + SetTextEntryEmulationResult(); + + /// Deserializes the result from JSON. + static SetTextEntryEmulationResult fromJson(Map json) { + return new SetTextEntryEmulationResult(); + } + + @override + Map toJson() => const {}; +} diff --git a/packages/flutter_driver/lib/src/driver/driver.dart b/packages/flutter_driver/lib/src/driver/driver.dart index 8acb350517..95f8856a4a 100644 --- a/packages/flutter_driver/lib/src/driver/driver.dart +++ b/packages/flutter_driver/lib/src/driver/driver.dart @@ -451,6 +451,15 @@ class FlutterDriver { await _sendCommand(new EnterText(text, timeout: timeout)); } + /// If `enabled` is true, enables text entry emulation via [enterText]. If + /// `enabled` is false, disables it. + /// + /// By default text entry emulation is enabled. + Future setTextEntryEmulation({ @required bool enabled, Duration timeout }) async { + assert(enabled != null); + await _sendCommand(new SetTextEntryEmulation(enabled, timeout: timeout)); + } + /// Sends a string and returns a string. /// /// This enables generic communication between the driver and the application. diff --git a/packages/flutter_driver/lib/src/extension/extension.dart b/packages/flutter_driver/lib/src/extension/extension.dart index 851fb9a085..1daebb22ee 100644 --- a/packages/flutter_driver/lib/src/extension/extension.dart +++ b/packages/flutter_driver/lib/src/extension/extension.dart @@ -100,6 +100,7 @@ class FlutterDriverExtension { 'scrollIntoView': _scrollIntoView, 'set_frame_sync': _setFrameSync, 'set_semantics': _setSemantics, + 'set_text_entry_emulation': _setTextEntryEmulation, 'tap': _tap, 'waitFor': _waitFor, 'waitForAbsent': _waitForAbsent, @@ -116,6 +117,7 @@ class FlutterDriverExtension { 'scrollIntoView': (Map params) => new ScrollIntoView.deserialize(params), 'set_frame_sync': (Map params) => new SetFrameSync.deserialize(params), 'set_semantics': (Map params) => new SetSemantics.deserialize(params), + 'set_text_entry_emulation': (Map params) => new SetTextEntryEmulation.deserialize(params), 'tap': (Map params) => new Tap.deserialize(params), 'waitFor': (Map params) => new WaitFor.deserialize(params), 'waitForAbsent': (Map params) => new WaitForAbsent.deserialize(params), @@ -332,6 +334,16 @@ class FlutterDriverExtension { return new GetTextResult(text.data); } + Future _setTextEntryEmulation(Command command) async { + final SetTextEntryEmulation setTextEntryEmulationCommand = command; + if (setTextEntryEmulationCommand.enabled) { + _testTextInput.register(); + } else { + _testTextInput.unregister(); + } + return new SetTextEntryEmulationResult(); + } + Future _enterText(Command command) async { final EnterText enterTextCommand = command; _testTextInput.enterText(enterTextCommand.text); diff --git a/packages/flutter_test/lib/src/test_text_input.dart b/packages/flutter_test/lib/src/test_text_input.dart index 4288bacc3f..bb182a03d6 100644 --- a/packages/flutter_test/lib/src/test_text_input.dart +++ b/packages/flutter_test/lib/src/test_text_input.dart @@ -24,6 +24,15 @@ class TestTextInput { SystemChannels.textInput.setMockMethodCallHandler(_handleTextInputCall); } + /// Removes this object as a mock handler for [SystemChannels.textInput]. + /// + /// After calling this method, the channel will exchange messages with the + /// Flutter engine. Use this with [FlutterDriver] tests that need to display + /// on-screen keyboard provided by the operating system. + void unregister() { + SystemChannels.textInput.setMockMethodCallHandler(null); + } + int _client = 0; /// Arguments supplied to the TextInput.setClient method call.