From a7a95bb5306fe7d9b67ba6b184ae8d877333f163 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Mon, 18 May 2020 10:01:55 -0700 Subject: [PATCH] echo text selection changes back to framework (flutter/engine#18408) --- .../Source/FlutterTextInputPlugin.mm | 16 +++- .../Source/FlutterTextInputPluginTest.m | 93 +++++++++++++++++++ 2 files changed, 106 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm index 84e4f5f228..225b1e51bd 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm @@ -264,6 +264,9 @@ static NSString* uniqueIdFromDictionary(NSDictionary* dictionary) { return [[FlutterTextRange allocWithZone:zone] initWithNSRange:self.range]; } +- (BOOL)isEqualTo:(FlutterTextRange*)other { + return NSEqualRanges(self.range, other.range); +} @end @interface FlutterTextInputView () @@ -329,14 +332,19 @@ static NSString* uniqueIdFromDictionary(NSDictionary* dictionary) { [self.inputDelegate textWillChange:self]; [self.text setString:newText]; } - + BOOL needsEditingStateUpdate = textChanged; NSInteger composingBase = [state[@"composingBase"] intValue]; NSInteger composingExtent = [state[@"composingExtent"] intValue]; NSRange composingRange = [self clampSelection:NSMakeRange(MIN(composingBase, composingExtent), ABS(composingBase - composingExtent)) forText:self.text]; - self.markedTextRange = + FlutterTextRange* newMarkedRange = composingRange.length > 0 ? [FlutterTextRange rangeWithNSRange:composingRange] : nil; + needsEditingStateUpdate = + needsEditingStateUpdate || newMarkedRange == nil + ? self.markedTextRange == nil + : [newMarkedRange isEqualTo:(FlutterTextRange*)self.markedTextRange]; + self.markedTextRange = newMarkedRange; NSInteger selectionBase = [state[@"selectionBase"] intValue]; NSInteger selectionExtent = [state[@"selectionExtent"] intValue]; @@ -346,6 +354,7 @@ static NSString* uniqueIdFromDictionary(NSDictionary* dictionary) { NSRange oldSelectedRange = [(FlutterTextRange*)self.selectedTextRange range]; if (selectedRange.location != oldSelectedRange.location || selectedRange.length != oldSelectedRange.length) { + needsEditingStateUpdate = YES; [self.inputDelegate selectionWillChange:self]; [self setSelectedTextRange:[FlutterTextRange rangeWithNSRange:selectedRange] updateEditingState:NO]; @@ -357,7 +366,8 @@ static NSString* uniqueIdFromDictionary(NSDictionary* dictionary) { if (textChanged) { [self.inputDelegate textDidChange:self]; - + } + if (needsEditingStateUpdate) { // For consistency with Android behavior, send an update to the framework. [self updateEditingState]; } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.m b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.m index fb59ff03d3..678ed1543c 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.m +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.m @@ -13,6 +13,10 @@ FLUTTER_ASSERT_ARC @interface FlutterTextInputPluginTest : XCTestCase @end +@interface FlutterTextInputView () +- (void)setTextInputState:(NSDictionary*)state; +@end + @implementation FlutterTextInputPluginTest - (void)testSecureInput { // Setup test. @@ -63,6 +67,95 @@ FLUTTER_ASSERT_ARC [[[[textInputPlugin textInputView] superview] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; } + +- (void)testTextChangesTriggerUpdateEditingClient { + // Setup test. + id engine = OCMClassMock([FlutterEngine class]); + + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + inputView.textInputDelegate = engine; + + [inputView.text setString:@"BEFORE"]; + inputView.markedTextRange = nil; + inputView.selectedTextRange = nil; + + // Text changes trigger update. + [inputView setTextInputState:@{@"text" : @"AFTER"}]; + OCMVerify([engine updateEditingClient:0 withState:[OCMArg isNotNil]]); + + // Don't send anything if there's nothing new. + [inputView setTextInputState:@{@"text" : @"AFTER"}]; + OCMReject([engine updateEditingClient:0 withState:[OCMArg any]]); + + // Clean up. + [engine stopMocking]; +} + +- (void)testSelectionChangeTriggersUpdateEditingClient { + // Setup test. + id engine = OCMClassMock([FlutterEngine class]); + + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + inputView.textInputDelegate = engine; + + [inputView.text setString:@"SELECTION"]; + inputView.markedTextRange = nil; + inputView.selectedTextRange = nil; + + [inputView + setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @0, @"selectionExtent" : @3}]; + OCMVerify([engine updateEditingClient:0 withState:[OCMArg isNotNil]]); + + [inputView + setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @1, @"selectionExtent" : @3}]; + OCMVerify([engine updateEditingClient:0 withState:[OCMArg isNotNil]]); + + [inputView + setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @1, @"selectionExtent" : @2}]; + OCMVerify([engine updateEditingClient:0 withState:[OCMArg isNotNil]]); + + // Don't send anything if there's nothing new. + [inputView + setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @1, @"selectionExtent" : @2}]; + OCMReject([engine updateEditingClient:0 withState:[OCMArg any]]); + + // Clean up. + [engine stopMocking]; +} + +- (void)testComposingChangeTriggersUpdateEditingClient { + // Setup test. + id engine = OCMClassMock([FlutterEngine class]); + + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + inputView.textInputDelegate = engine; + + // Reset to test marked text. + [inputView.text setString:@"COMPOSING"]; + inputView.markedTextRange = nil; + inputView.selectedTextRange = nil; + + [inputView + setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @0, @"composingExtent" : @3}]; + OCMVerify([engine updateEditingClient:0 withState:[OCMArg isNotNil]]); + + [inputView + setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @3}]; + OCMVerify([engine updateEditingClient:0 withState:[OCMArg isNotNil]]); + + [inputView + setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @2}]; + OCMVerify([engine updateEditingClient:0 withState:[OCMArg isNotNil]]); + + // Don't send anything if there's nothing new. + [inputView + setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @2}]; + OCMReject([engine updateEditingClient:0 withState:[OCMArg any]]); + + // Clean up. + [engine stopMocking]; +} + - (void)testAutofillInputViews { // Setup test. id engine = OCMClassMock([FlutterEngine class]);