diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 74833e588d..ee34aba0ce 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -1107,6 +1107,9 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterRestora FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterRestorationPluginTest.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.mm +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.h +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.mm +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDeltaTest.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm diff --git a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn index fdc3e9fd1a..1b6921c684 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn @@ -78,6 +78,8 @@ source_set("flutter_framework_source") { "framework/Source/FlutterRestorationPlugin.mm", "framework/Source/FlutterSemanticsScrollView.h", "framework/Source/FlutterSemanticsScrollView.mm", + "framework/Source/FlutterTextEditingDelta.h", + "framework/Source/FlutterTextEditingDelta.mm", "framework/Source/FlutterTextInputDelegate.h", "framework/Source/FlutterTextInputPlugin.h", "framework/Source/FlutterTextInputPlugin.mm", @@ -247,6 +249,7 @@ shared_library("ios_test_flutter") { "framework/Source/FlutterKeyboardManagerTest.mm", "framework/Source/FlutterPluginAppLifeCycleDelegateTest.mm", "framework/Source/FlutterRestorationPluginTest.mm", + "framework/Source/FlutterTextEditingDeltaTest.mm", "framework/Source/FlutterTextInputPluginTest.mm", "framework/Source/FlutterViewControllerTest.mm", "framework/Source/SemanticsObjectTest.mm", diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index c8f3d690e8..818b5d39fd 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -735,6 +735,11 @@ static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSS arguments:@[ @(client), @{tag : state} ]]; } +- (void)updateEditingClient:(int)client withDelta:(NSDictionary*)delta { + [_textInputChannel.get() invokeMethod:@"TextInputClient.updateEditingStateWithDeltas" + arguments:@[ @(client), delta ]]; +} + - (void)updateFloatingCursor:(FlutterFloatingCursorDragState)state withClient:(int)client withPosition:(NSDictionary*)position { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.h new file mode 100644 index 0000000000..0e2762e2a1 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.h @@ -0,0 +1,24 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERTEXTEDITINGDELTA_H_ +#define SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERTEXTEDITINGDELTA_H_ + +#import + +@interface FlutterTextEditingDelta : NSObject + +@property(nonatomic, readonly) NSString* oldText; +@property(nonatomic, readonly) NSString* deltaText; +@property(nonatomic, readonly) NSInteger deltaStart; +@property(nonatomic, readonly) NSInteger deltaEnd; + ++ (instancetype)textEditingDelta:(NSString*)textBeforeChange + replacedRange:(NSRange)range + updatedText:(NSString*)text; + ++ (instancetype)deltaWithNonText:(NSString*)text; + +@end +#endif // SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERTEXTEDITINGDELTA_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.mm new file mode 100644 index 0000000000..148960183b --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.mm @@ -0,0 +1,61 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.h" + +@implementation FlutterTextEditingDelta + +- (instancetype)initTextEditingDelta:(NSString*)textBeforeChange + replacedRange:(NSRange)range + updatedText:(NSString*)text { + self = [super init]; + + if (self) { + NSInteger start = range.location; + NSInteger end = range.location + range.length; + [self setDeltas:textBeforeChange newText:text deltaStart:start deltaEnd:end]; + } + + return self; +} + +- (instancetype)initWithNonText:(NSString*)text { + self = [super init]; + + if (self) { + [self setDeltas:text newText:@"" deltaStart:-1 deltaEnd:-1]; + } + + return self; +} + +- (void)setDeltas:(NSString*)oldText + newText:(NSString*)newTxt + deltaStart:(NSInteger)newStart + deltaEnd:(NSInteger)newEnd { + _oldText = [oldText retain]; + _deltaText = [newTxt retain]; + _deltaStart = newStart; + _deltaEnd = newEnd; +} + +- (void)dealloc { + [_oldText release]; + [_deltaText release]; + [super dealloc]; +} + ++ (instancetype)textEditingDelta:(NSString*)textBeforeChange + replacedRange:(NSRange)range + updatedText:(NSString*)text { + return [[[FlutterTextEditingDelta alloc] initTextEditingDelta:textBeforeChange + replacedRange:range + updatedText:text] autorelease]; +} + ++ (instancetype)deltaWithNonText:(NSString*)text { + return [[[FlutterTextEditingDelta alloc] initWithNonText:text] autorelease]; +} + +@end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDeltaTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDeltaTest.mm new file mode 100644 index 0000000000..dd48123ec7 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDeltaTest.mm @@ -0,0 +1,46 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.h" + +#import + +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" + +FLUTTER_ASSERT_ARC + +@interface FlutterTextEditingDeltaTest : XCTestCase +@end + +@implementation FlutterTextEditingDeltaTest + +- (void)testTextEditingDeltaConstructor { + // Here we are simulating inserting an "o" at the end of "hell". + NSString* oldText = @"hell"; + NSString* replacementText = @"hello"; + NSRange range = NSMakeRange(0, 4); + + FlutterTextEditingDelta* delta = [FlutterTextEditingDelta textEditingDelta:oldText + replacedRange:range + updatedText:replacementText]; + + XCTAssertEqual(delta.oldText, oldText); + XCTAssertEqualObjects(delta.deltaText, @"hello"); + XCTAssertEqual(delta.deltaStart, 0); + XCTAssertEqual(delta.deltaEnd, 4); +} + +- (void)testTextEditingDeltaNonTextConstructor { + // Here we are simulating inserting an "o" at the end of "hell". + NSString* oldText = @"hello"; + + FlutterTextEditingDelta* delta = [FlutterTextEditingDelta deltaWithNonText:oldText]; + + XCTAssertEqual(delta.oldText, oldText); + XCTAssertEqualObjects(delta.deltaText, @""); + XCTAssertEqual(delta.deltaStart, -1); + XCTAssertEqual(delta.deltaEnd, -1); +} + +@end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h index cb5491a9db..b6f2ac5ca9 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h @@ -33,6 +33,7 @@ typedef NS_ENUM(NSInteger, FlutterFloatingCursorDragState) { nextAction:(void (^)())next API_AVAILABLE(ios(13.4)); - (void)updateEditingClient:(int)client withState:(NSDictionary*)state; - (void)updateEditingClient:(int)client withState:(NSDictionary*)state withTag:(NSString*)tag; +- (void)updateEditingClient:(int)client withDelta:(NSDictionary*)state; - (void)performAction:(FlutterTextInputAction)action withClient:(int)client; - (void)updateFloatingCursor:(FlutterFloatingCursorDragState)state withClient:(int)client diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h index 579a5a7e07..c17423f3a8 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h @@ -9,6 +9,7 @@ #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterKeySecondaryResponder.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h" @interface FlutterTextInputPlugin : NSObject @@ -71,6 +72,7 @@ FLUTTER_DARWIN_EXPORT @property(nonatomic) UIKeyboardType keyboardType; @property(nonatomic) UIReturnKeyType returnKeyType; @property(nonatomic, getter=isSecureTextEntry) BOOL secureTextEntry; +@property(nonatomic, getter=isEnableDeltaModel) BOOL enableDeltaModel; @property(nonatomic) UITextSmartQuotesType smartQuotesType API_AVAILABLE(ios(11.0)); @property(nonatomic) UITextSmartDashesType smartDashesType API_AVAILABLE(ios(11.0)); @property(nonatomic, copy) UITextContentType textContentType API_AVAILABLE(ios(10.0)); 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 555a4de0e7..f5b00e33a0 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 @@ -51,6 +51,7 @@ static NSString* const kSecureTextEntry = @"obscureText"; static NSString* const kKeyboardType = @"inputType"; static NSString* const kKeyboardAppearance = @"keyboardAppearance"; static NSString* const kInputAction = @"inputAction"; +static NSString* const kEnableDeltaModel = @"enableDeltaModel"; static NSString* const kSmartDashesType = @"smartDashesType"; static NSString* const kSmartQuotesType = @"smartQuotesType"; @@ -617,6 +618,7 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { _keyboardType = UIKeyboardTypeDefault; _returnKeyType = UIReturnKeyDone; _secureTextEntry = NO; + _enableDeltaModel = NO; _accessibilityEnabled = NO; _decommissioned = NO; if (@available(iOS 11.0, *)) { @@ -645,6 +647,7 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { NSDictionary* autofill = configuration[kAutofillProperties]; self.secureTextEntry = [configuration[kSecureTextEntry] boolValue]; + self.enableDeltaModel = [configuration[kEnableDeltaModel] boolValue]; _isSystemKeyboardEnabled = shouldShowSystemKeyboard(inputType); self.keyboardType = ToUIKeyboardType(inputType); @@ -923,7 +926,12 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { - (void)setSelectedTextRange:(UITextRange*)selectedTextRange { [self setSelectedTextRangeLocal:selectedTextRange]; - [self updateEditingState]; + + if (_enableDeltaModel) { + [self updateEditingStateWithDelta:[FlutterTextEditingDelta deltaWithNonText:self.text]]; + } else { + [self updateEditingState]; + } } - (id)insertDictationResultPlaceholder { @@ -972,9 +980,18 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { } - (void)replaceRange:(UITextRange*)range withText:(NSString*)text { + NSString* textBeforeChange = [[self.text copy] autorelease]; NSRange replaceRange = ((FlutterTextRange*)range).range; [self replaceRangeLocal:replaceRange withText:text]; - [self updateEditingState]; + if (_enableDeltaModel) { + [self updateEditingStateWithDelta:[FlutterTextEditingDelta + textEditingDelta:textBeforeChange + replacedRange:[self clampSelection:replaceRange + forText:textBeforeChange] + updatedText:text]]; + } else { + [self updateEditingState]; + } } - (BOOL)shouldChangeTextInRange:(UITextRange*)range replacementText:(NSString*)text { @@ -1029,8 +1046,10 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { } - (void)setMarkedText:(NSString*)markedText selectedRange:(NSRange)markedSelectedRange { + NSString* textBeforeChange = [[self.text copy] autorelease]; NSRange selectedRange = _selectedTextRange.range; NSRange markedTextRange = ((FlutterTextRange*)self.markedTextRange).range; + NSRange actualReplacedRange; if (markedText == nil) markedText = @""; @@ -1038,9 +1057,11 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { if (markedTextRange.length > 0) { // Replace text in the marked range with the new text. [self replaceRangeLocal:markedTextRange withText:markedText]; + actualReplacedRange = markedTextRange; markedTextRange.length = markedText.length; } else { // Replace text in the selected range with the new text. + actualReplacedRange = selectedRange; [self replaceRangeLocal:selectedRange withText:markedText]; markedTextRange = NSMakeRange(selectedRange.location, markedText.length); } @@ -1053,14 +1074,26 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { [self setSelectedTextRangeLocal:[FlutterTextRange rangeWithNSRange:[self clampSelection:selectedRange forText:self.text]]]; - [self updateEditingState]; + if (_enableDeltaModel) { + [self updateEditingStateWithDelta:[FlutterTextEditingDelta + textEditingDelta:textBeforeChange + replacedRange:[self clampSelection:actualReplacedRange + forText:textBeforeChange] + updatedText:markedText]]; + } else { + [self updateEditingState]; + } } - (void)unmarkText { if (!self.markedTextRange) return; self.markedTextRange = nil; - [self updateEditingState]; + if (_enableDeltaModel) { + [self updateEditingStateWithDelta:[FlutterTextEditingDelta deltaWithNonText:self.text]]; + } else { + [self updateEditingState]; + } } - (UITextRange*)textRangeFromPosition:(UITextPosition*)fromPosition @@ -1392,6 +1425,38 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { } } +- (void)updateEditingStateWithDelta:(FlutterTextEditingDelta*)delta { + NSUInteger selectionBase = ((FlutterTextPosition*)_selectedTextRange.start).index; + NSUInteger selectionExtent = ((FlutterTextPosition*)_selectedTextRange.end).index; + + // Empty compositing range is represented by the framework's TextRange.empty. + NSInteger composingBase = -1; + NSInteger composingExtent = -1; + if (self.markedTextRange != nil) { + composingBase = ((FlutterTextPosition*)self.markedTextRange.start).index; + composingExtent = ((FlutterTextPosition*)self.markedTextRange.end).index; + } + + NSDictionary* deltaToFramework = @{ + @"oldText" : delta.oldText, + @"deltaText" : delta.deltaText, + @"deltaStart" : @(delta.deltaStart), + @"deltaEnd" : @(delta.deltaEnd), + @"selectionBase" : @(selectionBase), + @"selectionExtent" : @(selectionExtent), + @"selectionAffinity" : @(_selectionAffinity), + @"selectionIsDirectional" : @(false), + @"composingBase" : @(composingBase), + @"composingExtent" : @(composingExtent), + }; + + NSDictionary* deltas = @{ + @"deltas" : @[ deltaToFramework ], + }; + + [self.textInputDelegate updateEditingClient:_textInputClient withDelta:deltas]; +} + - (BOOL)hasText { return self.text.length > 0; } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm index b96325b5e9..de6e549d10 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm @@ -304,6 +304,204 @@ FLUTTER_ASSERT_ARC } } +#pragma mark - TextEditingDelta tests +- (void)testTextEditingDeltasAreGeneratedOnTextInput { + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + inputView.textInputDelegate = engine; + inputView.enableDeltaModel = YES; + + __block int updateCount = 0; + OCMStub([engine updateEditingClient:0 withDelta:[OCMArg isNotNil]]) + .andDo(^(NSInvocation* invocation) { + updateCount++; + }); + + [inputView insertText:@"text to insert"]; + // Update the framework exactly once. + XCTAssertEqual(updateCount, 1); + + // Verify correct delta is generated. + OCMVerify([engine + updateEditingClient:0 + withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) { + return ([[state[@"deltas"] objectAtIndex:0][@"oldText"] isEqualToString:@""]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaText"] + isEqualToString:@"text to insert"]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaStart"] intValue] == 0) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaEnd"] intValue] == 0); + }]]); + + [inputView deleteBackward]; + XCTAssertEqual(updateCount, 2); + + OCMVerify([engine + updateEditingClient:0 + withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) { + return ([[state[@"deltas"] objectAtIndex:0][@"oldText"] + isEqualToString:@"text to insert"]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaText"] isEqualToString:@""]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaStart"] intValue] == 13) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaEnd"] intValue] == 14); + }]]); + + inputView.selectedTextRange = [FlutterTextRange rangeWithNSRange:NSMakeRange(0, 1)]; + XCTAssertEqual(updateCount, 3); + + OCMVerify([engine + updateEditingClient:0 + withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) { + return ([[state[@"deltas"] objectAtIndex:0][@"oldText"] + isEqualToString:@"text to inser"]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaText"] isEqualToString:@""]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaStart"] intValue] == -1) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaEnd"] intValue] == -1); + }]]); + + [inputView replaceRange:[FlutterTextRange rangeWithNSRange:NSMakeRange(0, 1)] + withText:@"replace text"]; + XCTAssertEqual(updateCount, 4); + + OCMVerify([engine + updateEditingClient:0 + withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) { + return ([[state[@"deltas"] objectAtIndex:0][@"oldText"] + isEqualToString:@"text to inser"]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaText"] + isEqualToString:@"replace text"]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaStart"] intValue] == 0) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaEnd"] intValue] == 1); + }]]); + + [inputView setMarkedText:@"marked text" selectedRange:NSMakeRange(0, 1)]; + XCTAssertEqual(updateCount, 5); + + OCMVerify([engine + updateEditingClient:0 + withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) { + return ([[state[@"deltas"] objectAtIndex:0][@"oldText"] + isEqualToString:@"replace textext to inser"]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaText"] + isEqualToString:@"marked text"]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaStart"] intValue] == 12) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaEnd"] intValue] == 12); + }]]); + + [inputView unmarkText]; + XCTAssertEqual(updateCount, 6); + + OCMVerify([engine + updateEditingClient:0 + withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) { + return ([[state[@"deltas"] objectAtIndex:0][@"oldText"] + isEqualToString:@"replace textmarked textext to inser"]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaText"] isEqualToString:@""]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaStart"] intValue] == -1) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaEnd"] intValue] == -1); + }]]); +} + +- (void)testTextEditingDeltasAreGeneratedOnSetMarkedTextReplacement { + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + inputView.textInputDelegate = engine; + inputView.enableDeltaModel = YES; + + __block int updateCount = 0; + OCMStub([engine updateEditingClient:0 withDelta:[OCMArg isNotNil]]) + .andDo(^(NSInvocation* invocation) { + updateCount++; + }); + + [inputView.text setString:@"Some initial text"]; + XCTAssertEqual(updateCount, 0); + + UITextRange* range = [FlutterTextRange rangeWithNSRange:NSMakeRange(13, 4)]; + inputView.markedTextRange = range; + inputView.selectedTextRange = nil; + XCTAssertEqual(updateCount, 1); + + [inputView setMarkedText:@"new marked text." selectedRange:NSMakeRange(0, 1)]; + XCTAssertEqual(updateCount, 2); + + OCMVerify([engine + updateEditingClient:0 + withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) { + return ([[state[@"deltas"] objectAtIndex:0][@"oldText"] + isEqualToString:@"Some initial text"]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaText"] + isEqualToString:@"new marked text."]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaStart"] intValue] == 13) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaEnd"] intValue] == 17); + }]]); +} + +- (void)testTextEditingDeltasAreGeneratedOnSetMarkedTextInsertion { + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + inputView.textInputDelegate = engine; + inputView.enableDeltaModel = YES; + + __block int updateCount = 0; + OCMStub([engine updateEditingClient:0 withDelta:[OCMArg isNotNil]]) + .andDo(^(NSInvocation* invocation) { + updateCount++; + }); + + [inputView.text setString:@"Some initial text"]; + XCTAssertEqual(updateCount, 0); + + UITextRange* range = [FlutterTextRange rangeWithNSRange:NSMakeRange(13, 4)]; + inputView.markedTextRange = range; + inputView.selectedTextRange = nil; + XCTAssertEqual(updateCount, 1); + + [inputView setMarkedText:@"text." selectedRange:NSMakeRange(0, 1)]; + XCTAssertEqual(updateCount, 2); + + OCMVerify([engine + updateEditingClient:0 + withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) { + return ([[state[@"deltas"] objectAtIndex:0][@"oldText"] + isEqualToString:@"Some initial text"]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaText"] + isEqualToString:@"text."]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaStart"] intValue] == 13) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaEnd"] intValue] == 17); + }]]); +} + +- (void)testTextEditingDeltasAreGeneratedOnSetMarkedTextDeletion { + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + inputView.textInputDelegate = engine; + inputView.enableDeltaModel = YES; + + __block int updateCount = 0; + OCMStub([engine updateEditingClient:0 withDelta:[OCMArg isNotNil]]) + .andDo(^(NSInvocation* invocation) { + updateCount++; + }); + + [inputView.text setString:@"Some initial text"]; + XCTAssertEqual(updateCount, 0); + + UITextRange* range = [FlutterTextRange rangeWithNSRange:NSMakeRange(13, 4)]; + inputView.markedTextRange = range; + inputView.selectedTextRange = nil; + XCTAssertEqual(updateCount, 1); + + [inputView setMarkedText:@"tex" selectedRange:NSMakeRange(0, 1)]; + XCTAssertEqual(updateCount, 2); + + OCMVerify([engine + updateEditingClient:0 + withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) { + return ([[state[@"deltas"] objectAtIndex:0][@"oldText"] + isEqualToString:@"Some initial text"]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaText"] + isEqualToString:@"tex"]) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaStart"] intValue] == 13) && + ([[state[@"deltas"] objectAtIndex:0][@"deltaEnd"] intValue] == 17); + }]]); +} + #pragma mark - EditingState tests - (void)testUITextInputCallsUpdateEditingStateOnce { @@ -337,6 +535,38 @@ FLUTTER_ASSERT_ARC XCTAssertEqual(updateCount, 6); } +- (void)testUITextInputCallsUpdateEditingStateWithDeltaOnce { + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + inputView.textInputDelegate = engine; + inputView.enableDeltaModel = YES; + + __block int updateCount = 0; + OCMStub([engine updateEditingClient:0 withDelta:[OCMArg isNotNil]]) + .andDo(^(NSInvocation* invocation) { + updateCount++; + }); + + [inputView insertText:@"text to insert"]; + // Update the framework exactly once. + XCTAssertEqual(updateCount, 1); + + [inputView deleteBackward]; + XCTAssertEqual(updateCount, 2); + + inputView.selectedTextRange = [FlutterTextRange rangeWithNSRange:NSMakeRange(0, 1)]; + XCTAssertEqual(updateCount, 3); + + [inputView replaceRange:[FlutterTextRange rangeWithNSRange:NSMakeRange(0, 1)] + withText:@"replace text"]; + XCTAssertEqual(updateCount, 4); + + [inputView setMarkedText:@"marked text" selectedRange:NSMakeRange(0, 1)]; + XCTAssertEqual(updateCount, 5); + + [inputView unmarkText]; + XCTAssertEqual(updateCount, 6); +} + - (void)testTextChangesDoNotTriggerUpdateEditingClient { FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; inputView.textInputDelegate = engine; @@ -378,6 +608,48 @@ FLUTTER_ASSERT_ARC XCTAssertEqual(updateCount, 1); } +- (void)testTextChangesDoNotTriggerUpdateEditingClientWithDelta { + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + inputView.textInputDelegate = engine; + inputView.enableDeltaModel = YES; + + __block int updateCount = 0; + OCMStub([engine updateEditingClient:0 withDelta:[OCMArg isNotNil]]) + .andDo(^(NSInvocation* invocation) { + updateCount++; + }); + + [inputView.text setString:@"BEFORE"]; + XCTAssertEqual(updateCount, 0); + + inputView.markedTextRange = nil; + inputView.selectedTextRange = nil; + XCTAssertEqual(updateCount, 1); + + // Text changes don't trigger an update. + XCTAssertEqual(updateCount, 1); + [inputView setTextInputState:@{@"text" : @"AFTER"}]; + XCTAssertEqual(updateCount, 1); + [inputView setTextInputState:@{@"text" : @"AFTER"}]; + XCTAssertEqual(updateCount, 1); + + // Selection changes don't trigger an update. + [inputView + setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @0, @"selectionExtent" : @3}]; + XCTAssertEqual(updateCount, 1); + [inputView + setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @1, @"selectionExtent" : @3}]; + XCTAssertEqual(updateCount, 1); + + // Composing region changes don't trigger an update. + [inputView + setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @2}]; + XCTAssertEqual(updateCount, 1); + [inputView + setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @3}]; + XCTAssertEqual(updateCount, 1); +} + - (void)testUITextInputAvoidUnnecessaryUndateEditingClientCalls { FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; inputView.textInputDelegate = engine;