From c4415fb8e05dd2e8973e472add998937026b7398 Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Wed, 1 May 2024 13:57:39 -0700 Subject: [PATCH] macOS fluttertextinputplugin drops selector called if no client (flutter/engine#52495) The observed behavior is that if a keypress both reach framework and flutter textinputplugin selector and the keypress handler in framework causes the the textinputplugin to resign first responder, the selector will still go through even if the textinputplugin has already resigned. The pr makes it so textinputplugin will ignore these selector call fixes https://github.com/flutter/flutter/issues/143270 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../Source/FlutterTextInputPlugin.mm | 4 ++ .../Source/FlutterTextInputPluginTest.mm | 42 ++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm index 33adfa909d..7f88494360 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm @@ -777,6 +777,10 @@ static char markerKey; void (*func)(id, SEL, id) = reinterpret_cast(imp); func(self, selector, nil); } + if (self.clientID == nil) { + // The macOS may still call selector even if it is no longer a first responder. + return; + } if (selector == @selector(insertNewline:)) { // Already handled through text insertion (multiline) or action. diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm index e3a203803d..b3b4a8a096 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm @@ -1801,6 +1801,42 @@ return true; } +- (bool)testSelectorsNotForwardedToFrameworkIfNoClient { + id engineMock = flutter::testing::CreateMockFlutterEngine(@""); + id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger)); + OCMStub( // NOLINT(google-objc-avoid-throwing-exception) + [engineMock binaryMessenger]) + .andReturn(binaryMessengerMock); + // Make sure the selectors are not forwarded to the framework. + OCMReject([binaryMessengerMock sendOnChannel:@"flutter/textinput" message:[OCMArg any]]); + FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock + nibName:@"" + bundle:nil]; + + FlutterTextInputPlugin* plugin = + [[FlutterTextInputPlugin alloc] initWithViewController:viewController]; + + // Can't run CFRunLoop in default mode because it causes crashes from scheduled + // sources from other tests. + NSString* runLoopMode = @"FlutterTestRunLoopMode"; + plugin.customRunLoopMode = runLoopMode; + + // Call selectors without setting a client. + [plugin doCommandBySelector:@selector(moveUp:)]; + [plugin doCommandBySelector:@selector(moveRightAndModifySelection:)]; + + __block bool done = false; + CFRunLoopPerformBlock(CFRunLoopGetMain(), (__bridge CFStringRef)runLoopMode, ^{ + done = true; + }); + + while (!done) { + CFRunLoopRunInMode((__bridge CFStringRef)runLoopMode, 0, true); + } + // At this point the selectors should be dropped; otherwise, OCMReject will throw. + return true; +} + @end namespace flutter::testing { @@ -1886,7 +1922,7 @@ TEST(FlutterTextInputPluginTest, TestComposingWithDelta) { ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testComposingWithDelta]); } -TEST(FlutterTextInputPluginTest, testComposingWithDeltasWhenSelectionIsActive) { +TEST(FlutterTextInputPluginTest, TestComposingWithDeltasWhenSelectionIsActive) { ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testComposingWithDeltasWhenSelectionIsActive]); } @@ -1910,6 +1946,10 @@ TEST(FlutterTextInputPluginTest, TestSelectorsAreForwardedToFramework) { ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSelectorsAreForwardedToFramework]); } +TEST(FlutterTextInputPluginTest, TestSelectorsNotForwardedToFrameworkIfNoClient) { + ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSelectorsNotForwardedToFrameworkIfNoClient]); +} + TEST(FlutterTextInputPluginTest, TestInsertNewLine) { ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testInsertNewLine]); }