diff --git a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm index 55f7064b46770..0b677c9e63944 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm @@ -436,6 +436,7 @@ - (void)updateEditStateWithDelta:(const flutter::TextEditingDelta)delta { [_channel invokeMethod:kUpdateEditStateWithDeltasResponseMethod arguments:@[ self.clientID, deltas ]]; + [self updateTextAndSelection]; } - (void)updateTextAndSelection { diff --git a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm index b4866f2e50ce3..34970d2990650 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm @@ -404,6 +404,67 @@ - (bool)testOperationsThatTriggerDelta { return true; } +- (bool)testLocalTextAndSelectionUpdateAfterDelta { + id engineMock = OCMClassMock([FlutterEngine class]); + id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger)); + OCMStub( // NOLINT(google-objc-avoid-throwing-exception) + [engineMock binaryMessenger]) + .andReturn(binaryMessengerMock); + + FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock + nibName:@"" + bundle:nil]; + + FlutterTextInputPlugin* plugin = + [[FlutterTextInputPlugin alloc] initWithViewController:viewController]; + + [plugin handleMethodCall:[FlutterMethodCall + methodCallWithMethodName:@"TextInput.setClient" + arguments:@[ + @(1), @{ + @"inputAction" : @"action", + @"enableDeltaModel" : @"true", + @"inputType" : @{@"name" : @"inputName"}, + } + ]] + result:^(id){ + }]; + [plugin insertText:@"text to insert"]; + + NSDictionary* deltaToFramework = @{ + @"oldText" : @"", + @"deltaText" : @"text to insert", + @"deltaStart" : @(0), + @"deltaEnd" : @(0), + @"selectionBase" : @(14), + @"selectionExtent" : @(14), + @"selectionAffinity" : @"TextAffinity.upstream", + @"selectionIsDirectional" : @(false), + @"composingBase" : @(-1), + @"composingExtent" : @(-1), + }; + NSDictionary* expectedState = @{ + @"deltas" : @[ deltaToFramework ], + }; + + NSData* updateCall = [[FlutterJSONMethodCodec sharedInstance] + encodeMethodCall:[FlutterMethodCall + methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas" + arguments:@[ @(1), expectedState ]]]; + + @try { + OCMVerify( // NOLINT(google-objc-avoid-throwing-exception) + [binaryMessengerMock sendOnChannel:@"flutter/textinput" message:updateCall]); + } @catch (...) { + return false; + } + + bool localTextAndSelectionUpdated = [plugin.string isEqualToString:@"text to insert"] && + NSEqualRanges(plugin.selectedRange, NSMakeRange(14, 0)); + + return localTextAndSelectionUpdated; +} + @end namespace flutter::testing { @@ -439,6 +500,10 @@ - (bool)testOperationsThatTriggerDelta { ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testOperationsThatTriggerDelta]); } +TEST(FlutterTextInputPluginTest, TestLocalTextAndSelectionUpdateAfterDelta) { + ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testLocalTextAndSelectionUpdateAfterDelta]); +} + TEST(FlutterTextInputPluginTest, CanWorkWithFlutterTextField) { FlutterEngine* engine = CreateTestEngine(); NSString* fixtures = @(testing::GetFixturesPath());