[macOS - TextInput] Insert new line only when TextInputAction.newline (flutter/engine#41977)
## Description This PR updates the macOS text input plugin to avoid adding a new line on a multiline text field when action is not set to `TextInputAction.newline`. ## Related Issue macOS implementation for https://github.com/flutter/flutter/issues/125879. (similar to the Linux implementation in https://github.com/flutter/engine/pull/41895). ## Tests Adds 2 tests.
This commit is contained in:
@@ -61,6 +61,9 @@ static NSString* const kAutofillHints = @"hints";
|
||||
static NSString* const kTextAffinityDownstream = @"TextAffinity.downstream";
|
||||
static NSString* const kTextAffinityUpstream = @"TextAffinity.upstream";
|
||||
|
||||
// TextInputAction types
|
||||
static NSString* const kInputActionNewline = @"TextInputAction.newline";
|
||||
|
||||
#pragma mark - Enums
|
||||
/**
|
||||
* The affinity of the current cursor position. If the cursor is at a position representing
|
||||
@@ -820,7 +823,8 @@ static char markerKey;
|
||||
_activeModel->CommitComposing();
|
||||
_activeModel->EndComposing();
|
||||
}
|
||||
if ([self.inputType isEqualToString:kMultilineInputType]) {
|
||||
if ([self.inputType isEqualToString:kMultilineInputType] &&
|
||||
[self.inputAction isEqualToString:kInputActionNewline]) {
|
||||
[self insertText:@"\n" replacementRange:self.selectedRange];
|
||||
}
|
||||
[_channel invokeMethod:kPerformAction arguments:@[ self.clientID, self.inputAction ]];
|
||||
|
||||
@@ -1470,6 +1470,196 @@
|
||||
return true;
|
||||
}
|
||||
|
||||
- (bool)testInsertNewLine {
|
||||
id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
|
||||
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
|
||||
OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[engineMock binaryMessenger])
|
||||
.andReturn(binaryMessengerMock);
|
||||
OCMStub([[engineMock ignoringNonObjectArgs] sendKeyEvent:FlutterKeyEvent {}
|
||||
callback:nil
|
||||
userData:nil]);
|
||||
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
|
||||
nibName:@""
|
||||
bundle:nil];
|
||||
|
||||
FlutterTextInputPlugin* plugin =
|
||||
[[FlutterTextInputPlugin alloc] initWithViewController:viewController];
|
||||
|
||||
NSDictionary* setClientConfig = @{
|
||||
@"inputType" : @{@"name" : @"TextInputType.multiline"},
|
||||
@"inputAction" : @"TextInputAction.newline",
|
||||
};
|
||||
[plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
|
||||
arguments:@[ @(1), setClientConfig ]]
|
||||
result:^(id){
|
||||
}];
|
||||
|
||||
FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
|
||||
arguments:@{
|
||||
@"text" : @"Text",
|
||||
@"selectionBase" : @(4),
|
||||
@"selectionExtent" : @(4),
|
||||
@"composingBase" : @(-1),
|
||||
@"composingExtent" : @(-1),
|
||||
}];
|
||||
|
||||
NSDictionary* expectedState = @{
|
||||
@"selectionBase" : @(4),
|
||||
@"selectionExtent" : @(4),
|
||||
@"selectionAffinity" : @"TextAffinity.upstream",
|
||||
@"selectionIsDirectional" : @(NO),
|
||||
@"composingBase" : @(-1),
|
||||
@"composingExtent" : @(-1),
|
||||
@"text" : @"Text",
|
||||
};
|
||||
|
||||
NSData* updateCall = [[FlutterJSONMethodCodec sharedInstance]
|
||||
encodeMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInputClient.updateEditingState"
|
||||
arguments:@[ @(1), expectedState ]]];
|
||||
|
||||
OCMExpect( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock sendOnChannel:@"flutter/textinput" message:updateCall]);
|
||||
|
||||
[plugin handleMethodCall:call
|
||||
result:^(id){
|
||||
}];
|
||||
|
||||
@try {
|
||||
OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock sendOnChannel:@"flutter/textinput" message:updateCall]);
|
||||
} @catch (...) {
|
||||
return false;
|
||||
}
|
||||
|
||||
[plugin doCommandBySelector:@selector(insertNewline:)];
|
||||
|
||||
NSDictionary* updatedState = @{
|
||||
@"selectionBase" : @(5),
|
||||
@"selectionExtent" : @(5),
|
||||
@"selectionAffinity" : @"TextAffinity.upstream",
|
||||
@"selectionIsDirectional" : @(NO),
|
||||
@"composingBase" : @(-1),
|
||||
@"composingExtent" : @(-1),
|
||||
@"text" : @"Text\n",
|
||||
};
|
||||
|
||||
updateCall = [[FlutterJSONMethodCodec sharedInstance]
|
||||
encodeMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInputClient.updateEditingState"
|
||||
arguments:@[ @(1), updatedState ]]];
|
||||
|
||||
@try {
|
||||
OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock sendOnChannel:@"flutter/textinput" message:updateCall]);
|
||||
} @catch (...) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
- (bool)testSendActionDoNotInsertNewLine {
|
||||
id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
|
||||
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
|
||||
OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[engineMock binaryMessenger])
|
||||
.andReturn(binaryMessengerMock);
|
||||
OCMStub([[engineMock ignoringNonObjectArgs] sendKeyEvent:FlutterKeyEvent {}
|
||||
callback:nil
|
||||
userData:nil]);
|
||||
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
|
||||
nibName:@""
|
||||
bundle:nil];
|
||||
|
||||
FlutterTextInputPlugin* plugin =
|
||||
[[FlutterTextInputPlugin alloc] initWithViewController:viewController];
|
||||
|
||||
NSDictionary* setClientConfig = @{
|
||||
@"inputType" : @{@"name" : @"TextInputType.multiline"},
|
||||
@"inputAction" : @"TextInputAction.send",
|
||||
};
|
||||
[plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
|
||||
arguments:@[ @(1), setClientConfig ]]
|
||||
result:^(id){
|
||||
}];
|
||||
|
||||
FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
|
||||
arguments:@{
|
||||
@"text" : @"Text",
|
||||
@"selectionBase" : @(4),
|
||||
@"selectionExtent" : @(4),
|
||||
@"composingBase" : @(-1),
|
||||
@"composingExtent" : @(-1),
|
||||
}];
|
||||
|
||||
NSDictionary* expectedState = @{
|
||||
@"selectionBase" : @(4),
|
||||
@"selectionExtent" : @(4),
|
||||
@"selectionAffinity" : @"TextAffinity.upstream",
|
||||
@"selectionIsDirectional" : @(NO),
|
||||
@"composingBase" : @(-1),
|
||||
@"composingExtent" : @(-1),
|
||||
@"text" : @"Text",
|
||||
};
|
||||
|
||||
NSData* updateCall = [[FlutterJSONMethodCodec sharedInstance]
|
||||
encodeMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInputClient.updateEditingState"
|
||||
arguments:@[ @(1), expectedState ]]];
|
||||
|
||||
OCMExpect( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock sendOnChannel:@"flutter/textinput" message:updateCall]);
|
||||
|
||||
[plugin handleMethodCall:call
|
||||
result:^(id){
|
||||
}];
|
||||
|
||||
[plugin doCommandBySelector:@selector(insertNewline:)];
|
||||
|
||||
NSData* performActionCall = [[FlutterJSONMethodCodec sharedInstance]
|
||||
encodeMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInputClient.performAction"
|
||||
arguments:@[ @(1), @"TextInputAction.send" ]]];
|
||||
|
||||
// Input action should be notified.
|
||||
@try {
|
||||
OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock sendOnChannel:@"flutter/textinput" message:performActionCall]);
|
||||
} @catch (...) {
|
||||
return false;
|
||||
}
|
||||
|
||||
NSDictionary* updatedState = @{
|
||||
@"selectionBase" : @(5),
|
||||
@"selectionExtent" : @(5),
|
||||
@"selectionAffinity" : @"TextAffinity.upstream",
|
||||
@"selectionIsDirectional" : @(NO),
|
||||
@"composingBase" : @(-1),
|
||||
@"composingExtent" : @(-1),
|
||||
@"text" : @"Text\n",
|
||||
};
|
||||
|
||||
updateCall = [[FlutterJSONMethodCodec sharedInstance]
|
||||
encodeMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInputClient.updateEditingState"
|
||||
arguments:@[ @(1), updatedState ]]];
|
||||
|
||||
// Verify that editing state was not be updated.
|
||||
@try {
|
||||
OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock sendOnChannel:@"flutter/textinput" message:updateCall]);
|
||||
return false;
|
||||
} @catch (...) {
|
||||
// Expected.
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
- (bool)testLocalTextAndSelectionUpdateAfterDelta {
|
||||
id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
|
||||
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
|
||||
@@ -1694,6 +1884,14 @@ TEST(FlutterTextInputPluginTest, TestSelectorsAreForwardedToFramework) {
|
||||
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSelectorsAreForwardedToFramework]);
|
||||
}
|
||||
|
||||
TEST(FlutterTextInputPluginTest, TestInsertNewLine) {
|
||||
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testInsertNewLine]);
|
||||
}
|
||||
|
||||
TEST(FlutterTextInputPluginTest, TestSendActionDoNotInsertNewLine) {
|
||||
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSendActionDoNotInsertNewLine]);
|
||||
}
|
||||
|
||||
TEST(FlutterTextInputPluginTest, CanWorkWithFlutterTextField) {
|
||||
FlutterEngine* engine = CreateTestEngine();
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
|
||||
|
||||
Reference in New Issue
Block a user