[macOS] Set textfield autofill type (flutter/engine#39632)
This commit is contained in:
@@ -91,11 +91,36 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
}
|
||||
|
||||
// Returns the autofill hint content type, if specified; otherwise nil.
|
||||
static NSString* GetAutofillContentType(NSDictionary* autofill) {
|
||||
static NSString* GetAutofillHint(NSDictionary* autofill) {
|
||||
NSArray<NSString*>* hints = autofill[kAutofillHints];
|
||||
return hints.count > 0 ? hints[0] : nil;
|
||||
}
|
||||
|
||||
// Returns the text content type for the specified TextInputConfiguration.
|
||||
// NSTextContentType is only available for macOS 11.0 and later.
|
||||
static NSTextContentType GetTextContentType(NSDictionary* configuration)
|
||||
API_AVAILABLE(macos(11.0)) {
|
||||
// Check autofill hints.
|
||||
NSDictionary* autofill = configuration[kAutofillProperties];
|
||||
if (autofill) {
|
||||
NSString* hint = GetAutofillHint(autofill);
|
||||
if ([hint isEqualToString:@"username"]) {
|
||||
return NSTextContentTypeUsername;
|
||||
}
|
||||
if ([hint isEqualToString:@"password"]) {
|
||||
return NSTextContentTypePassword;
|
||||
}
|
||||
if ([hint isEqualToString:@"oneTimeCode"]) {
|
||||
return NSTextContentTypeOneTimeCode;
|
||||
}
|
||||
}
|
||||
// If no autofill hints, guess based on other attributes.
|
||||
if ([configuration[kSecureTextEntry] boolValue]) {
|
||||
return NSTextContentTypePassword;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
// Returns YES if configuration describes a field for which autocomplete should be enabled for
|
||||
// the specified TextInputConfiguration. Autocomplete is enabled by default, but will be disabled
|
||||
// if the field is password-related, or if the configuration contains no autofill settings.
|
||||
@@ -113,8 +138,8 @@ static BOOL EnableAutocompleteForTextInputConfiguration(NSDictionary* configurat
|
||||
|
||||
// Disable if autofill properties indicate a username/password.
|
||||
// See: https://github.com/flutter/flutter/issues/119824
|
||||
NSString* contentType = GetAutofillContentType(autofill);
|
||||
if ([contentType isEqualToString:@"password"] || [contentType isEqualToString:@"username"]) {
|
||||
NSString* hint = GetAutofillHint(autofill);
|
||||
if ([hint isEqualToString:@"password"] || [hint isEqualToString:@"username"]) {
|
||||
return NO;
|
||||
}
|
||||
return YES;
|
||||
@@ -386,7 +411,9 @@ static char markerKey;
|
||||
_inputType = inputTypeInfo[kTextInputTypeName];
|
||||
self.textAffinity = kFlutterTextAffinityUpstream;
|
||||
self.automaticTextCompletionEnabled = EnableAutocomplete(config);
|
||||
// TODO(cbracken): support text content types https://github.com/flutter/flutter/issues/120252
|
||||
if (@available(macOS 11.0, *)) {
|
||||
self.contentType = GetTextContentType(config);
|
||||
}
|
||||
|
||||
_activeModel = std::make_unique<flutter::TextInputModel>();
|
||||
}
|
||||
|
||||
@@ -449,6 +449,12 @@
|
||||
|
||||
// Verify autocomplete is enabled.
|
||||
EXPECT_TRUE([plugin isAutomaticTextCompletionEnabled]);
|
||||
|
||||
// Verify content type is nil for unsupported content types.
|
||||
if (@available(macOS 11.0, *)) {
|
||||
EXPECT_EQ([plugin contentType], nil);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -510,7 +516,6 @@
|
||||
@"obscureText" : @YES,
|
||||
@"autofill" : @{
|
||||
@"uniqueIdentifier" : @"field1",
|
||||
@"hints" : @[ @"name" ],
|
||||
@"editingValue" : @{@"text" : @""},
|
||||
}
|
||||
}
|
||||
@@ -555,6 +560,11 @@
|
||||
|
||||
// Verify autocomplete is disabled.
|
||||
EXPECT_FALSE([plugin isAutomaticTextCompletionEnabled]);
|
||||
|
||||
// Verify content type is password.
|
||||
if (@available(macOS 11.0, *)) {
|
||||
EXPECT_EQ([plugin contentType], NSTextContentTypePassword);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -608,6 +618,86 @@
|
||||
return true;
|
||||
}
|
||||
|
||||
- (bool)testContentTypeWhenAutofillTypeIsUsername {
|
||||
// Set up FlutterTextInputPlugin.
|
||||
id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
|
||||
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
|
||||
OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[engineMock binaryMessenger])
|
||||
.andReturn(binaryMessengerMock);
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
|
||||
nibName:@""
|
||||
bundle:nil];
|
||||
FlutterTextInputPlugin* plugin =
|
||||
[[FlutterTextInputPlugin alloc] initWithViewController:viewController];
|
||||
|
||||
// Set input client 1.
|
||||
[plugin handleMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInput.setClient"
|
||||
arguments:@[
|
||||
@(1), @{
|
||||
@"inputAction" : @"action",
|
||||
@"inputType" : @{@"name" : @"inputName"},
|
||||
@"autofill" : @{
|
||||
@"uniqueIdentifier" : @"field1",
|
||||
@"hints" : @[ @"username" ],
|
||||
@"editingValue" : @{@"text" : @""},
|
||||
}
|
||||
}
|
||||
]]
|
||||
result:^(id){
|
||||
}];
|
||||
|
||||
// Verify autocomplete is disabled.
|
||||
EXPECT_FALSE([plugin isAutomaticTextCompletionEnabled]);
|
||||
|
||||
// Verify content type is username.
|
||||
if (@available(macOS 11.0, *)) {
|
||||
EXPECT_EQ([plugin contentType], NSTextContentTypeUsername);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
- (bool)testContentTypeWhenAutofillTypeIsOneTimeCode {
|
||||
// Set up FlutterTextInputPlugin.
|
||||
id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
|
||||
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
|
||||
OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[engineMock binaryMessenger])
|
||||
.andReturn(binaryMessengerMock);
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
|
||||
nibName:@""
|
||||
bundle:nil];
|
||||
FlutterTextInputPlugin* plugin =
|
||||
[[FlutterTextInputPlugin alloc] initWithViewController:viewController];
|
||||
|
||||
// Set input client 1.
|
||||
[plugin handleMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInput.setClient"
|
||||
arguments:@[
|
||||
@(1), @{
|
||||
@"inputAction" : @"action",
|
||||
@"inputType" : @{@"name" : @"inputName"},
|
||||
@"autofill" : @{
|
||||
@"uniqueIdentifier" : @"field1",
|
||||
@"hints" : @[ @"oneTimeCode" ],
|
||||
@"editingValue" : @{@"text" : @""},
|
||||
}
|
||||
}
|
||||
]]
|
||||
result:^(id){
|
||||
}];
|
||||
|
||||
// Verify autocomplete is disabled.
|
||||
EXPECT_FALSE([plugin isAutomaticTextCompletionEnabled]);
|
||||
|
||||
// Verify content type is username.
|
||||
if (@available(macOS 11.0, *)) {
|
||||
EXPECT_EQ([plugin contentType], NSTextContentTypeOneTimeCode);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
- (bool)testFirstRectForCharacterRange {
|
||||
id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
|
||||
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
|
||||
|
||||
Reference in New Issue
Block a user