From ba3492c50acb689fff18a6084456d5638cc8fdbf Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Wed, 15 Feb 2023 09:01:39 -0800 Subject: [PATCH] [macOS] Set textfield autofill type (flutter/engine#39632) --- .../Source/FlutterTextInputPlugin.mm | 35 ++++++- .../Source/FlutterTextInputPluginTest.mm | 92 ++++++++++++++++++- 2 files changed, 122 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm index a583bc3edd..7129b5e7cb 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm @@ -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* 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(); } diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm index 3bf1bef645..9355a06645 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm @@ -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));