From 97cf355b9d6b769ee585f6403f0985ec6ad6173a Mon Sep 17 00:00:00 2001 From: Nurhan Turgut <50856934+nturgut@users.noreply.github.com> Date: Mon, 4 Nov 2019 13:35:18 -0800 Subject: [PATCH] =?UTF-8?q?Adding=20handling=20of=20TextInputClient.onConn?= =?UTF-8?q?ectionClosed=20messages=20handli=E2=80=A6=20(#43466)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adding handling of TextInputClient.onConnectionClosed messages handling to Framework * Adding more test cases for closing connection to editable_text_test * fixing analyze error. * Fixing analyze error in the test file * Fixing comments on the new method * Adding more closing connection examples. * Indentation change * Remove auto-add white space * Changing the oncloseconnection behaviour to stop editing. Updating the tests * Addressing PR comments. Added explicit log for method channnels to the tests. Added comments to the interfaces. * add more documentation --- .../lib/src/services/system_channels.dart | 8 + .../flutter/lib/src/services/text_input.dart | 16 ++ .../lib/src/widgets/editable_text.dart | 10 ++ .../test/services/text_input_test.dart | 46 +++++ .../test/widgets/editable_text_test.dart | 169 ++++++++++++++++++ .../flutter_test/lib/src/test_text_input.dart | 29 +++ 6 files changed, 278 insertions(+) diff --git a/packages/flutter/lib/src/services/system_channels.dart b/packages/flutter/lib/src/services/system_channels.dart index 456b67a13d..5acac9057a 100644 --- a/packages/flutter/lib/src/services/system_channels.dart +++ b/packages/flutter/lib/src/services/system_channels.dart @@ -159,6 +159,14 @@ class SystemChannels { /// second argument is a [String] consisting of the stringification of one /// of the values of the [TextInputAction] enum. /// + /// * `TextInputClient.onConnectionClosed`: The text input connection closed + /// on the platform side. For example the application is moved to + /// background or used closed the virtual keyboard. This method informs + /// [TextInputClient] to clear connection and finalize editing. + /// `TextInput.clearClient` and `TextInput.hide` is not called after + /// clearing the connection since on the platform side the connection is + /// already finalized. + /// /// Calls to methods that are not implemented on the shell side are ignored /// (so it is safe to call methods when the relevant plugin might be missing). static const MethodChannel textInput = OptionalMethodChannel( diff --git a/packages/flutter/lib/src/services/text_input.dart b/packages/flutter/lib/src/services/text_input.dart index 8dc841b3dd..a1c30f3226 100644 --- a/packages/flutter/lib/src/services/text_input.dart +++ b/packages/flutter/lib/src/services/text_input.dart @@ -650,6 +650,11 @@ abstract class TextInputClient { /// Updates the floating cursor position and state. void updateFloatingCursor(RawFloatingCursorPoint point); + + /// Platform notified framework of closed connection. + /// + /// [TextInputClient] should cleanup its connection and finalize editing. + void connectionClosed(); } /// An interface for interacting with a text input control. @@ -751,6 +756,14 @@ class TextInputConnection { } assert(!attached); } + + /// Platform sent a notification informing the connection is closed. + /// + /// [TextInputConnection] should clean current client connection. + void connectionClosedReceived() { + _clientHandler._currentConnection = null; + assert(!attached); + } } TextInputAction _toTextInputAction(String action) { @@ -831,6 +844,9 @@ class _TextInputClientHandler { case 'TextInputClient.updateFloatingCursor': _currentConnection._client.updateFloatingCursor(_toTextPoint(_toTextCursorAction(args[1]), args[2])); break; + case 'TextInputClient.onConnectionClosed': + _currentConnection._client.connectionClosed(); + break; default: throw MissingPluginException(); } diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index b341e632bb..04bf54f770 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -1444,6 +1444,16 @@ class EditableTextState extends State with AutomaticKeepAliveClien } } + @override + void connectionClosed() { + if (_hasInputConnection) { + _textInputConnection.connectionClosedReceived(); + _textInputConnection = null; + _lastKnownRemoteTextEditingValue = null; + _finalizeEditing(true); + } + } + /// Express interest in interacting with the keyboard. /// /// If this control is already attached to the keyboard, this function will diff --git a/packages/flutter/test/services/text_input_test.dart b/packages/flutter/test/services/text_input_test.dart index 5eb828272e..93bd1109a2 100644 --- a/packages/flutter/test/services/text_input_test.dart +++ b/packages/flutter/test/services/text_input_test.dart @@ -88,5 +88,51 @@ void main() { expect(signed.hashCode == signedDecimal.hashCode, false); expect(decimal.hashCode == signedDecimal.hashCode, false); }); + + test('TextInputClient onConnectionClosed method is called', () async { + // Assemble a TextInputConnection so we can verify its change in state. + final FakeTextInputClient client = FakeTextInputClient(); + const TextInputConfiguration configuration = TextInputConfiguration(); + TextInput.attach(client, configuration); + + expect(client.latestMethodCall, isEmpty); + + // Send onConnectionClosed message. + final ByteData messageBytes = const JSONMessageCodec().encodeMessage({ + 'args': [1], + 'method': 'TextInputClient.onConnectionClosed', + }); + await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage( + 'flutter/textinput', + messageBytes, + (ByteData _) {}, + ); + + expect(client.latestMethodCall, 'connectionClosed'); + }); }); } + +class FakeTextInputClient extends TextInputClient { + String latestMethodCall = ''; + + @override + void performAction(TextInputAction action) { + latestMethodCall = 'performAction'; + } + + @override + void updateEditingValue(TextEditingValue value) { + latestMethodCall = 'updateEditingValue'; + } + + @override + void updateFloatingCursor(RawFloatingCursorPoint point) { + latestMethodCall = 'updateFloatingCursor'; + } + + @override + void connectionClosed() { + latestMethodCall = 'connectionClosed'; + } +} diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 1fda2aa1c4..cee5cf3f0b 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -585,6 +585,175 @@ void main() { equals('TextInputAction.done')); }); + testWidgets('connection is closed when TextInputClient.onConnectionClosed message received', (WidgetTester tester) async { + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: FocusScope( + node: focusScopeNode, + autofocus: true, + child: EditableText( + backgroundCursorColor: Colors.grey, + controller: controller, + focusNode: focusNode, + maxLines: 1, // Sets text keyboard implicitly. + style: textStyle, + cursorColor: cursorColor, + ), + ), + ), + ), + ); + + await tester.tap(find.byType(EditableText)); + await tester.showKeyboard(find.byType(EditableText)); + controller.text = 'test'; + await tester.idle(); + + final EditableTextState state = + tester.state(find.byType(EditableText)); + expect(tester.testTextInput.editingState['text'], equals('test')); + expect(state.wantKeepAlive, true); + + tester.testTextInput.log.clear(); + tester.testTextInput.closeConnection(); + await tester.idle(); + + // Widget does not have focus anymore. + expect(state.wantKeepAlive, false); + // No method calls are sent from the framework. + // This makes sure hide/clearClient methods are not called after connection + // closed. + expect(tester.testTextInput.log, isEmpty); + }); + + testWidgets('closed connection reopened when user focused', (WidgetTester tester) async { + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: FocusScope( + node: focusScopeNode, + autofocus: true, + child: EditableText( + backgroundCursorColor: Colors.grey, + controller: controller, + focusNode: focusNode, + maxLines: 1, // Sets text keyboard implicitly. + style: textStyle, + cursorColor: cursorColor, + ), + ), + ), + ), + ); + + await tester.tap(find.byType(EditableText)); + await tester.showKeyboard(find.byType(EditableText)); + controller.text = 'test3'; + await tester.idle(); + + final EditableTextState state = + tester.state(find.byType(EditableText)); + expect(tester.testTextInput.editingState['text'], equals('test3')); + expect(state.wantKeepAlive, true); + + tester.testTextInput.log.clear(); + tester.testTextInput.closeConnection(); + await tester.pumpAndSettle(); + + // Widget does not have focus anymore. + expect(state.wantKeepAlive, false); + // No method calls are sent from the framework. + // This makes sure hide/clearClient methods are not called after connection + // closed. + expect(tester.testTextInput.log, isEmpty); + + await tester.tap(find.byType(EditableText)); + await tester.showKeyboard(find.byType(EditableText)); + await tester.pump(); + controller.text = 'test2'; + expect(tester.testTextInput.editingState['text'], equals('test2')); + // Widget regained the focus. + expect(state.wantKeepAlive, true); + }); + + testWidgets('closed connection reopened when user focused on another field', (WidgetTester tester) async { + final EditableText testNameField = + EditableText( + backgroundCursorColor: Colors.grey, + controller: controller, + focusNode: focusNode, + maxLines: null, + keyboardType: TextInputType.text, + style: textStyle, + cursorColor: cursorColor, + ); + + final EditableText testPhoneField = + EditableText( + backgroundCursorColor: Colors.grey, + controller: controller, + focusNode: focusNode, + keyboardType: TextInputType.phone, + maxLines: 3, + style: textStyle, + cursorColor: cursorColor, + ); + + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: FocusScope( + node: focusScopeNode, + autofocus: true, + child: ListView( + children: [ + testNameField, + testPhoneField, + ], + ), + ), + ), + ), + ); + + // Tap, enter text. + await tester.tap(find.byWidget(testNameField)); + await tester.showKeyboard(find.byWidget(testNameField)); + controller.text = 'test'; + await tester.idle(); + + expect(tester.testTextInput.editingState['text'], equals('test')); + final EditableTextState state = + tester.state(find.byWidget(testNameField)); + expect(state.wantKeepAlive, true); + + tester.testTextInput.log.clear(); + tester.testTextInput.closeConnection(); + + // Widget does not have focus anymore. + expect(state.wantKeepAlive, false); + // No method calls are sent from the framework. + // This makes sure hide/clearClient methods are not called after connection + // closed. + expect(tester.testTextInput.log, isEmpty); + + // For the next fields, tap, enter text. + await tester.tap(find.byWidget(testPhoneField)); + await tester.showKeyboard(find.byWidget(testPhoneField)); + controller.text = '650123123'; + await tester.idle(); + expect(tester.testTextInput.editingState['text'], equals('650123123')); + // Widget regained the focus. + expect(state.wantKeepAlive, true); + }); + /// Toolbar is not used in Flutter Web. Skip this check. /// /// Web is using native dom elements (it is also used as platform input) diff --git a/packages/flutter_test/lib/src/test_text_input.dart b/packages/flutter_test/lib/src/test_text_input.dart index 00b0eaf1f3..a594e4b1e5 100644 --- a/packages/flutter_test/lib/src/test_text_input.dart +++ b/packages/flutter_test/lib/src/test_text_input.dart @@ -55,6 +55,12 @@ class TestTextInput { _isRegistered = false; } + /// Log for method calls. + /// + /// For all registered channels, handled calls are added to the list. Can + /// be cleaned using [clearLog]. + final List log = []; + /// Whether this [TestTextInput] is registered with [SystemChannels.textInput]. /// /// Use [register] and [unregister] methods to control this value. @@ -78,6 +84,7 @@ class TestTextInput { Map editingState; Future _handleTextInputCall(MethodCall methodCall) async { + log.add(methodCall); switch (methodCall.method) { case 'TextInput.setClient': _client = methodCall.arguments[0]; @@ -123,6 +130,28 @@ class TestTextInput { ); } + /// Simulates the user closing the text input connection. + /// + /// For example: + /// - User pressed the home button and sent the application to background. + /// - User closed the virtual keyboard. + void closeConnection() { + // Not using the `expect` function because in the case of a FlutterDriver + // test this code does not run in a package:test test zone. + if (_client == 0) + throw TestFailure('Tried to use TestTextInput with no keyboard attached. You must use WidgetTester.showKeyboard() first.'); + _binaryMessenger.handlePlatformMessage( + SystemChannels.textInput.name, + SystemChannels.textInput.codec.encodeMethodCall( + MethodCall( + 'TextInputClient.onConnectionClosed', + [_client,] + ), + ), + (ByteData data) { /* response from framework is discarded */ }, + ); + } + /// Simulates the user typing the given text. void enterText(String text) { updateEditingValue(TextEditingValue(