Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
macOS fluttertextinputplugin drops selector called if no client
  • Loading branch information
chunhtai committed May 1, 2024
commit fe5ba6700e3b725fc7b9907c00f17318a31bb4bc
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,6 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
_activeModel->EndComposing();
}
[_textInputContext discardMarkedText];

_clientID = nil;
_inputAction = nil;
_enableDeltaModel = NO;
Expand Down Expand Up @@ -777,6 +776,10 @@ - (void)doCommandBySelector:(SEL)selector {
void (*func)(id, SEL, id) = reinterpret_cast<void (*)(id, SEL, id)>(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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,49 @@ - (bool)testSelectorsAreForwardedToFramework {
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;

// Ensure both selectors are grouped in one platform channel call.
[plugin doCommandBySelector:@selector(moveUp:)];
[plugin doCommandBySelector:@selector(moveRightAndModifySelection:)];

// Clear the client before the CFRunLoop is run.
[plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.clearClient"
arguments:@[]]
result:^(id){
}];

__block bool done = false;
CFRunLoopPerformBlock(CFRunLoopGetMain(), (__bridge CFStringRef)runLoopMode, ^{
done = true;
});

while (!done) {
// Each invocation will handle one source.
CFRunLoopRunInMode((__bridge CFStringRef)runLoopMode, 0, true);
}
// At this point the selectors should be dropped; otherwise, OCMReject will throw.
return true;
}

@end

namespace flutter::testing {
Expand Down Expand Up @@ -1886,7 +1929,7 @@ - (bool)testSelectorsAreForwardedToFramework {
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testComposingWithDelta]);
}

TEST(FlutterTextInputPluginTest, testComposingWithDeltasWhenSelectionIsActive) {
TEST(FlutterTextInputPluginTest, TestComposingWithDeltasWhenSelectionIsActive) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching that!

ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testComposingWithDeltasWhenSelectionIsActive]);
}

Expand All @@ -1910,6 +1953,10 @@ - (bool)testSelectorsAreForwardedToFramework {
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSelectorsAreForwardedToFramework]);
}

TEST(FlutterTextInputPluginTest, TestSelectorsNotForwardedToFrameworkIfNoClient) {
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSelectorsNotForwardedToFrameworkIfNoClient]);
}

TEST(FlutterTextInputPluginTest, TestInsertNewLine) {
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testInsertNewLine]);
}
Expand Down