From a96a314d63a6f8779713dc1c84c9e6a55b9865eb Mon Sep 17 00:00:00 2001 From: Renzo Olivares Date: Tue, 15 Mar 2022 13:16:19 -0700 Subject: [PATCH] =?UTF-8?q?Fix=20local=20text=20value=20and=20selection=20?= =?UTF-8?q?not=20updating=20when=20delta=20is=20sent=20to=E2=80=A6=20(flut?= =?UTF-8?q?ter/engine#31966)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix local text value and selection not updating when delta is sent to framework * readd new line * Add test * whitespace * whitespace * formatting Co-authored-by: Renzo Olivares --- .../Source/FlutterTextInputPlugin.mm | 1 + .../Source/FlutterTextInputPluginTest.mm | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+) 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 a9485ece99..6a7c98c409 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 @@ -436,6 +436,7 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base, [_channel invokeMethod:kUpdateEditStateWithDeltasResponseMethod arguments:@[ self.clientID, deltas ]]; + [self updateTextAndSelection]; } - (void)updateTextAndSelection { 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 b4866f2e50..34970d2990 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 @@ -404,6 +404,67 @@ 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 @@ TEST(FlutterTextInputPluginTest, TestOperationsThatTriggerDelta) { ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testOperationsThatTriggerDelta]); } +TEST(FlutterTextInputPluginTest, TestLocalTextAndSelectionUpdateAfterDelta) { + ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testLocalTextAndSelectionUpdateAfterDelta]); +} + TEST(FlutterTextInputPluginTest, CanWorkWithFlutterTextField) { FlutterEngine* engine = CreateTestEngine(); NSString* fixtures = @(testing::GetFixturesPath());