echo text selection changes back to framework (flutter/engine#18408)

This commit is contained in:
LongCatIsLooong
2020-05-18 10:01:55 -07:00
committed by GitHub
parent 533296f161
commit a7a95bb530
2 changed files with 106 additions and 3 deletions

View File

@@ -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];
}

View File

@@ -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]);