From 813e5874cf2e0dd9e6060f26ad3622ddd7cb8052 Mon Sep 17 00:00:00 2001 From: Renzo Olivares Date: Thu, 10 Mar 2022 12:52:13 -0800 Subject: [PATCH 1/6] Fix local text value and selection not updating when delta is sent to framework --- .../darwin/macos/framework/Source/FlutterTextInputPlugin.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm index 55f7064b46770..81b4f630a9c66 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 { @@ -632,7 +633,6 @@ - (void)setMarkedText:(id)string BOOL isAttributedString = [string isKindOfClass:[NSAttributedString class]]; std::string marked_text = isAttributedString ? [[string string] UTF8String] : [string UTF8String]; _activeModel->UpdateComposingText(marked_text); - if (_enableDeltaModel) { flutter::TextRange composing = _activeModel->composing_range(); [self updateEditStateWithDelta:flutter::TextEditingDelta(textBeforeChange, composing, From e5c480f20c60272d4e8b90b0a93b2c63d0de117f Mon Sep 17 00:00:00 2001 From: Renzo Olivares Date: Thu, 10 Mar 2022 16:47:39 -0800 Subject: [PATCH 2/6] readd new line --- .../darwin/macos/framework/Source/FlutterTextInputPlugin.mm | 1 + 1 file changed, 1 insertion(+) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm index 81b4f630a9c66..0b677c9e63944 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm @@ -633,6 +633,7 @@ - (void)setMarkedText:(id)string BOOL isAttributedString = [string isKindOfClass:[NSAttributedString class]]; std::string marked_text = isAttributedString ? [[string string] UTF8String] : [string UTF8String]; _activeModel->UpdateComposingText(marked_text); + if (_enableDeltaModel) { flutter::TextRange composing = _activeModel->composing_range(); [self updateEditStateWithDelta:flutter::TextEditingDelta(textBeforeChange, composing, From af49cc39ef3ea3443cbb1f9dfa3b709b2f64ed01 Mon Sep 17 00:00:00 2001 From: Renzo Olivares Date: Tue, 15 Mar 2022 11:31:00 -0700 Subject: [PATCH 3/6] Add test --- .../Source/FlutterTextInputPluginTest.mm | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm index b4866f2e50ce3..915ebc04ebe86 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm @@ -404,6 +404,66 @@ - (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 +499,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()); From dc86e40b89feb44c5867b4c133b9bef0c178bb42 Mon Sep 17 00:00:00 2001 From: Renzo Olivares Date: Tue, 15 Mar 2022 11:32:29 -0700 Subject: [PATCH 4/6] whitespace --- .../darwin/macos/framework/Source/FlutterTextInputPluginTest.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm index 915ebc04ebe86..be2d84f4e78ad 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm @@ -459,7 +459,7 @@ - (bool)testLocalTextAndSelectionUpdateAfterDelta { return false; } - bool localTextAndSelectionUpdated = [plugin.string isEqualToString:@"text to insert"] && NSEqualRanges(plugin.selectedRange, NSMakeRange(14, 0)); +bool localTextAndSelectionUpdated = [plugin.string isEqualToString:@"text to insert"] && NSEqualRanges(plugin.selectedRange, NSMakeRange(14, 0)); return localTextAndSelectionUpdated; } From fa337c987182a18f86a2d492cb3567c70d113ace Mon Sep 17 00:00:00 2001 From: Renzo Olivares Date: Tue, 15 Mar 2022 11:33:07 -0700 Subject: [PATCH 5/6] whitespace --- .../macos/framework/Source/FlutterTextInputPluginTest.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm index be2d84f4e78ad..f1705a4e21e4a 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm @@ -459,8 +459,8 @@ - (bool)testLocalTextAndSelectionUpdateAfterDelta { return false; } -bool localTextAndSelectionUpdated = [plugin.string isEqualToString:@"text to insert"] && NSEqualRanges(plugin.selectedRange, NSMakeRange(14, 0)); - + bool localTextAndSelectionUpdated = [plugin.string isEqualToString:@"text to insert"] && NSEqualRanges(plugin.selectedRange, NSMakeRange(14, 0)); + return localTextAndSelectionUpdated; } From b3febe9262b2cd26fdd47ae67bbc725930a56897 Mon Sep 17 00:00:00 2001 From: Renzo Olivares Date: Tue, 15 Mar 2022 11:35:06 -0700 Subject: [PATCH 6/6] formatting --- .../macos/framework/Source/FlutterTextInputPluginTest.mm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm index f1705a4e21e4a..34970d2990650 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm @@ -459,7 +459,8 @@ - (bool)testLocalTextAndSelectionUpdateAfterDelta { return false; } - bool localTextAndSelectionUpdated = [plugin.string isEqualToString:@"text to insert"] && NSEqualRanges(plugin.selectedRange, NSMakeRange(14, 0)); + bool localTextAndSelectionUpdated = [plugin.string isEqualToString:@"text to insert"] && + NSEqualRanges(plugin.selectedRange, NSMakeRange(14, 0)); return localTextAndSelectionUpdated; }