diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart index b8342b7b6c..b131a08fa1 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart @@ -1262,12 +1262,22 @@ class FirefoxTextEditingStrategy extends GloballyPositionedTextEditingStrategy { // Refocus on the domElement after blur, so that user can keep editing the // text field. _subscriptions.add(domElement.onBlur.listen((_) { - domElement.focus(); + _postponeFocus(); })); preventDefaultForMouseEvents(); } + void _postponeFocus() { + // Firefox does not focus on the editing element if we call the focus + // inside the blur event, therefore we postpone the focus. + // Calling focus inside a Timer for `0` milliseconds guarantee that it is + // called after blur event propagation is completed. + Timer(const Duration(milliseconds: 0), () { + domElement.focus(); + }); + } + @override void placeElement() { domElement.focus(); diff --git a/engine/src/flutter/lib/web_ui/test/text_editing_test.dart b/engine/src/flutter/lib/web_ui/test/text_editing_test.dart index 721cb7cff4..0d8a63a1e9 100644 --- a/engine/src/flutter/lib/web_ui/test/text_editing_test.dart +++ b/engine/src/flutter/lib/web_ui/test/text_editing_test.dart @@ -706,7 +706,10 @@ void testMain() { expect(spy.messages, isEmpty); }); - test('do not close connection on blur', () async { + test('focus and connection with blur', () async { + // In all the desktop browsers we are keeping the connection + // open, keep the text editing element focused if it receives a blur + // event. final MethodCall setClient = MethodCall( 'TextInput.setClient', [123, flutterSinglelineConfig]); sendFrameworkMessage(codec.encodeMethodCall(setClient)); @@ -731,19 +734,26 @@ void testMain() { // DOM element is blurred. textEditing.editingElement.domElement.blur(); - expect(spy.messages, hasLength(0)); - - // DOM element still has focus. - // Even though this passes on manual tests it does not work on - // Firefox automated unit tests. - if (browserEngine != BrowserEngine.firefox) { + // For ios-safari the connection is closed. + if (browserEngine == BrowserEngine.webkit && + operatingSystem == OperatingSystem.iOs) { + expect(spy.messages, hasLength(1)); + expect(spy.messages[0].channel, 'flutter/textinput'); + expect(spy.messages[0].methodName, + 'TextInputClient.onConnectionClosed'); + await Future.delayed(Duration.zero); + // DOM element loses the focus. + expect(document.activeElement, document.body); + } else { + // No connection close message sent. + expect(spy.messages, hasLength(0)); + await Future.delayed(Duration.zero); + // DOM element still keeps the focus. expect(document.activeElement, textEditing.editingElement.domElement); } }, - // TODO(nurhan): https://github.com/flutter/flutter/issues/50590 // TODO(nurhan): https://github.com/flutter/flutter/issues/50769 - skip: (browserEngine == BrowserEngine.webkit || - browserEngine == BrowserEngine.edge)); + skip: (browserEngine == BrowserEngine.edge)); test('finishAutofillContext closes connection no autofill element', () async {