[iOS] TextInputPlugin should batch TextEditingDeltas and dispatch on the next runLoop (flutter/engine#43267)

Fixes https://github.com/flutter/flutter/issues/129061
This commit is contained in:
Renzo Olivares
2023-07-27 13:56:07 -07:00
committed by GitHub
parent bb37c0eb12
commit e29f713d01
3 changed files with 198 additions and 87 deletions

View File

@@ -133,6 +133,7 @@ FLUTTER_DARWIN_EXPORT
@property(nonatomic, strong) UITextRange* markedTextRange;
@property(nonatomic, copy) NSDictionary* markedTextStyle;
@property(nonatomic, weak) id<UITextInputDelegate> inputDelegate;
@property(nonatomic, strong) NSMutableArray* pendingDeltas;
// UITextInputTraits
@property(nonatomic) UITextAutocapitalizationType autocapitalizationType;

View File

@@ -803,6 +803,7 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point,
_markedRect = kInvalidFirstRect;
_cachedFirstRect = kInvalidFirstRect;
_scribbleInteractionStatus = FlutterScribbleInteractionStatusNone;
_pendingDeltas = [[NSMutableArray alloc] init];
// Initialize with the zero matrix which is not
// an affine transform.
_editableTransform = CATransform3D();
@@ -1251,7 +1252,7 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point,
- (BOOL)shouldChangeTextInRange:(UITextRange*)range replacementText:(NSString*)text {
// `temporarilyDeletedComposedCharacter` should only be used during a single text change session.
// So it needs to be cleared at the start of each text editting session.
// So it needs to be cleared at the start of each text editing session.
self.temporarilyDeletedComposedCharacter = nil;
if (self.returnKeyType == UIReturnKeyDefault && [text isEqualToString:@"\n"]) {
@@ -1965,13 +1966,24 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point,
@"composingExtent" : @(composingExtent),
};
NSDictionary* deltas = @{
@"deltas" : @[ deltaToFramework ],
};
[_pendingDeltas addObject:deltaToFramework];
[self.textInputDelegate flutterTextInputView:self
updateEditingClient:_textInputClient
withDelta:deltas];
if (_pendingDeltas.count == 1) {
__weak FlutterTextInputView* weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong FlutterTextInputView* strongSelf = weakSelf;
if (strongSelf && strongSelf.pendingDeltas.count > 0) {
NSDictionary* deltas = @{
@"deltas" : strongSelf.pendingDeltas,
};
[strongSelf.textInputDelegate flutterTextInputView:strongSelf
updateEditingClient:strongSelf->_textInputClient
withDelta:deltas];
[strongSelf.pendingDeltas removeAllObjects];
}
});
}
}
- (BOOL)hasText {

View File

@@ -133,6 +133,20 @@ FLUTTER_ASSERT_ARC
}];
}
- (void)flushScheduledAsyncBlocks {
__block bool done = false;
XCTestExpectation* expectation =
[[XCTestExpectation alloc] initWithDescription:@"Testing on main queue"];
dispatch_async(dispatch_get_main_queue(), ^{
done = true;
});
dispatch_async(dispatch_get_main_queue(), ^{
XCTAssertTrue(done);
[expectation fulfill];
});
[self waitForExpectations:@[ expectation ] timeout:10];
}
- (NSMutableDictionary*)mutableTemplateCopy {
if (!_template) {
_template = @{
@@ -707,102 +721,164 @@ FLUTTER_ASSERT_ARC
inputView.enableDeltaModel = YES;
__block int updateCount = 0;
OCMStub([engine flutterTextInputView:inputView updateEditingClient:0 withDelta:[OCMArg isNotNil]])
[inputView insertText:@"text to insert"];
OCMExpect(
[engine
flutterTextInputView:inputView
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);
}]])
.andDo(^(NSInvocation* invocation) {
updateCount++;
});
XCTAssertEqual(updateCount, 0);
[self flushScheduledAsyncBlocks];
[inputView insertText:@"text to insert"];
// Update the framework exactly once.
XCTAssertEqual(updateCount, 1);
// Verify correct delta is generated.
OCMVerify([engine
flutterTextInputView:inputView
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];
OCMExpect([engine flutterTextInputView:inputView
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);
}]])
.andDo(^(NSInvocation* invocation) {
updateCount++;
});
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 2);
OCMVerify([engine
flutterTextInputView:inputView
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)];
OCMExpect([engine flutterTextInputView:inputView
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);
}]])
.andDo(^(NSInvocation* invocation) {
updateCount++;
});
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 3);
OCMVerify([engine
flutterTextInputView:inputView
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"];
OCMExpect(
[engine
flutterTextInputView:inputView
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);
}]])
.andDo(^(NSInvocation* invocation) {
updateCount++;
});
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 4);
OCMVerify([engine
flutterTextInputView:inputView
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)];
OCMExpect([engine flutterTextInputView:inputView
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);
}]])
.andDo(^(NSInvocation* invocation) {
updateCount++;
});
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 5);
OCMVerify([engine
flutterTextInputView:inputView
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);
OCMExpect([engine
flutterTextInputView:inputView
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);
}]])
.andDo(^(NSInvocation* invocation) {
updateCount++;
});
[self flushScheduledAsyncBlocks];
OCMVerify([engine
flutterTextInputView:inputView
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);
}]]);
XCTAssertEqual(updateCount, 6);
OCMVerifyAll(engine);
}
- (void)testTextEditingDeltasAreBatchedAndForwardedToFramework {
// Setup
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
inputView.enableDeltaModel = YES;
// Expected call.
OCMExpect([engine flutterTextInputView:inputView
updateEditingClient:0
withDelta:[OCMArg checkWithBlock:^BOOL(NSDictionary* state) {
NSArray* deltas = state[@"deltas"];
NSDictionary* firstDelta = deltas[0];
NSDictionary* secondDelta = deltas[1];
NSDictionary* thirdDelta = deltas[2];
return [firstDelta[@"oldText"] isEqualToString:@""] &&
[firstDelta[@"deltaText"] isEqualToString:@"-"] &&
[firstDelta[@"deltaStart"] intValue] == 0 &&
[firstDelta[@"deltaEnd"] intValue] == 0 &&
[secondDelta[@"oldText"] isEqualToString:@"-"] &&
[secondDelta[@"deltaText"] isEqualToString:@""] &&
[secondDelta[@"deltaStart"] intValue] == 0 &&
[secondDelta[@"deltaEnd"] intValue] == 1 &&
[thirdDelta[@"oldText"] isEqualToString:@""] &&
[thirdDelta[@"deltaText"] isEqualToString:@"—"] &&
[thirdDelta[@"deltaStart"] intValue] == 0 &&
[thirdDelta[@"deltaEnd"] intValue] == 0;
}]]);
// Simulate user input.
[inputView insertText:@"-"];
[inputView deleteBackward];
[inputView insertText:@"—"];
[self flushScheduledAsyncBlocks];
OCMVerifyAll(engine);
}
- (void)testTextEditingDeltasAreGeneratedOnSetMarkedTextReplacement {
@@ -821,11 +897,10 @@ FLUTTER_ASSERT_ARC
UITextRange* range = [FlutterTextRange rangeWithNSRange:NSMakeRange(13, 4)];
inputView.markedTextRange = range;
inputView.selectedTextRange = nil;
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 1);
[inputView setMarkedText:@"new marked text." selectedRange:NSMakeRange(0, 1)];
XCTAssertEqual(updateCount, 2);
OCMVerify([engine
flutterTextInputView:inputView
updateEditingClient:0
@@ -837,6 +912,8 @@ FLUTTER_ASSERT_ARC
([[state[@"deltas"] objectAtIndex:0][@"deltaStart"] intValue] == 13) &&
([[state[@"deltas"] objectAtIndex:0][@"deltaEnd"] intValue] == 17);
}]]);
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 2);
}
- (void)testTextEditingDeltasAreGeneratedOnSetMarkedTextInsertion {
@@ -850,16 +927,16 @@ FLUTTER_ASSERT_ARC
});
[inputView.text setString:@"Some initial text"];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 0);
UITextRange* range = [FlutterTextRange rangeWithNSRange:NSMakeRange(13, 4)];
inputView.markedTextRange = range;
inputView.selectedTextRange = nil;
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 1);
[inputView setMarkedText:@"text." selectedRange:NSMakeRange(0, 1)];
XCTAssertEqual(updateCount, 2);
OCMVerify([engine
flutterTextInputView:inputView
updateEditingClient:0
@@ -871,6 +948,8 @@ FLUTTER_ASSERT_ARC
([[state[@"deltas"] objectAtIndex:0][@"deltaStart"] intValue] == 13) &&
([[state[@"deltas"] objectAtIndex:0][@"deltaEnd"] intValue] == 17);
}]]);
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 2);
}
- (void)testTextEditingDeltasAreGeneratedOnSetMarkedTextDeletion {
@@ -884,16 +963,16 @@ FLUTTER_ASSERT_ARC
});
[inputView.text setString:@"Some initial text"];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 0);
UITextRange* range = [FlutterTextRange rangeWithNSRange:NSMakeRange(13, 4)];
inputView.markedTextRange = range;
inputView.selectedTextRange = nil;
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 1);
[inputView setMarkedText:@"tex" selectedRange:NSMakeRange(0, 1)];
XCTAssertEqual(updateCount, 2);
OCMVerify([engine
flutterTextInputView:inputView
updateEditingClient:0
@@ -905,6 +984,8 @@ FLUTTER_ASSERT_ARC
([[state[@"deltas"] objectAtIndex:0][@"deltaStart"] intValue] == 13) &&
([[state[@"deltas"] objectAtIndex:0][@"deltaEnd"] intValue] == 17);
}]]);
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 2);
}
#pragma mark - EditingState tests
@@ -950,23 +1031,29 @@ FLUTTER_ASSERT_ARC
});
[inputView insertText:@"text to insert"];
[self flushScheduledAsyncBlocks];
// Update the framework exactly once.
XCTAssertEqual(updateCount, 1);
[inputView deleteBackward];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 2);
inputView.selectedTextRange = [FlutterTextRange rangeWithNSRange:NSMakeRange(0, 1)];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 3);
[inputView replaceRange:[FlutterTextRange rangeWithNSRange:NSMakeRange(0, 1)]
withText:@"replace text"];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 4);
[inputView setMarkedText:@"marked text" selectedRange:NSMakeRange(0, 1)];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 5);
[inputView unmarkText];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 6);
}
@@ -1021,33 +1108,44 @@ FLUTTER_ASSERT_ARC
});
[inputView.text setString:@"BEFORE"];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 0);
inputView.markedTextRange = nil;
inputView.selectedTextRange = nil;
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 1);
// Text changes don't trigger an update.
XCTAssertEqual(updateCount, 1);
[inputView setTextInputState:@{@"text" : @"AFTER"}];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 1);
[inputView setTextInputState:@{@"text" : @"AFTER"}];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 1);
// Selection changes don't trigger an update.
[inputView
setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @0, @"selectionExtent" : @3}];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 1);
[inputView
setTextInputState:@{@"text" : @"SELECTION", @"selectionBase" : @1, @"selectionExtent" : @3}];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 1);
// Composing region changes don't trigger an update.
[inputView
setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @2}];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 1);
[inputView
setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @3}];
[self flushScheduledAsyncBlocks];
XCTAssertEqual(updateCount, 1);
}