Commit ff9e7c5c authored by Scott Wu's avatar Scott Wu Committed by Commit Bot

Add unit test for password filling logic

Bug: 865114
Cq-Include-Trybots: luci.chromium.try:ios-simulator-cronet;luci.chromium.try:ios-simulator-full-configs
Change-Id: I5617f95e83c661d046e1a9d148362c2b436d2240
Reviewed-on: https://chromium-review.googlesource.com/1195292Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Commit-Queue: Scott Wu <scottwu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#588528}
parent 0a43db5c
...@@ -36,6 +36,7 @@ static_library("test_support") { ...@@ -36,6 +36,7 @@ static_library("test_support") {
] ]
deps = [ deps = [
":ios",
"//base", "//base",
"//components/autofill/core/common", "//components/autofill/core/common",
"//url", "//url",
......
...@@ -33,10 +33,10 @@ class AccountSelectFillDataTest : public PlatformTest { ...@@ -33,10 +33,10 @@ class AccountSelectFillDataTest : public PlatformTest {
public: public:
AccountSelectFillDataTest() { AccountSelectFillDataTest() {
for (size_t i = 0; i < base::size(form_data_); ++i) { for (size_t i = 0; i < base::size(form_data_); ++i) {
SetPasswordFormFillData(form_data_[i], kUrl, kUrl, kUsernameElements[i], SetPasswordFormFillData(kUrl, kUrl, kUsernameElements[i], kUsernames[i],
kUsernames[i], kPasswordElements[i], kPasswordElements[i], kPasswords[i],
kPasswords[i], kAdditionalUsernames[i], kAdditionalUsernames[i], kAdditionalPasswords[i],
kAdditionalPasswords[i], false); false, &form_data_[i]);
form_data_[i].name = base::ASCIIToUTF16(kFormNames[i]); form_data_[i].name = base::ASCIIToUTF16(kFormNames[i]);
} }
......
...@@ -74,6 +74,15 @@ constexpr char kCommandPrefix[] = "passwordForm"; ...@@ -74,6 +74,15 @@ constexpr char kCommandPrefix[] = "passwordForm";
withUsername:(const base::string16&)username withUsername:(const base::string16&)username
password:(const base::string16&)password password:(const base::string16&)password
completionHandler:(nullable void (^)(BOOL))completionHandler; completionHandler:(nullable void (^)(BOOL))completionHandler;
@end
// Category for test only.
@interface PasswordControllerHelper (Testing)
// Replaces JsPasswordManager for test.
- (void)setJsPasswordManager:(JsPasswordManager*)jsPasswordManager;
@end @end
@implementation PasswordControllerHelper { @implementation PasswordControllerHelper {
...@@ -302,6 +311,12 @@ constexpr char kCommandPrefix[] = "passwordForm"; ...@@ -302,6 +311,12 @@ constexpr char kCommandPrefix[] = "passwordForm";
}]; }];
} }
#pragma mark - Private methods for test only
- (void)setJsPasswordManager:(JsPasswordManager*)jsPasswordManager {
_jsPasswordManager = jsPasswordManager;
}
#pragma mark - Public methods #pragma mark - Public methods
- (void)findPasswordFormsWithCompletionHandler: - (void)findPasswordFormsWithCompletionHandler:
......
...@@ -6,15 +6,15 @@ ...@@ -6,15 +6,15 @@
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/common/password_form_fill_data.h" #include "components/autofill/core/common/password_form_fill_data.h"
#include "components/password_manager/ios/account_select_fill_data.h"
#include "url/gurl.h" #include "url/gurl.h"
using autofill::PasswordFormFillData; using autofill::PasswordFormFillData;
using password_manager::FillData;
namespace test_helpers { namespace test_helpers {
// Populates |form_data| with test values. void SetPasswordFormFillData(const std::string& origin,
void SetPasswordFormFillData(PasswordFormFillData& form_data,
const std::string& origin,
const std::string& action, const std::string& action,
const char* username_field, const char* username_field,
const char* username_value, const char* username_value,
...@@ -22,28 +22,45 @@ void SetPasswordFormFillData(PasswordFormFillData& form_data, ...@@ -22,28 +22,45 @@ void SetPasswordFormFillData(PasswordFormFillData& form_data,
const char* password_value, const char* password_value,
const char* additional_username, const char* additional_username,
const char* additional_password, const char* additional_password,
bool wait_for_username) { bool wait_for_username,
form_data.origin = GURL(origin); PasswordFormFillData* form_data) {
form_data.action = GURL(action); form_data->origin = GURL(origin);
form_data->action = GURL(action);
autofill::FormFieldData username; autofill::FormFieldData username;
username.name = base::UTF8ToUTF16(username_field); username.name = base::UTF8ToUTF16(username_field);
username.id = base::UTF8ToUTF16(username_field); username.id = base::UTF8ToUTF16(username_field);
username.value = base::UTF8ToUTF16(username_value); username.value = base::UTF8ToUTF16(username_value);
form_data.username_field = username; form_data->username_field = username;
autofill::FormFieldData password; autofill::FormFieldData password;
password.name = base::UTF8ToUTF16(password_field); password.name = base::UTF8ToUTF16(password_field);
password.id = base::UTF8ToUTF16(password_field); password.id = base::UTF8ToUTF16(password_field);
password.value = base::UTF8ToUTF16(password_value); password.value = base::UTF8ToUTF16(password_value);
form_data.password_field = password; form_data->password_field = password;
if (additional_username) { if (additional_username) {
autofill::PasswordAndRealm additional_password_data; autofill::PasswordAndRealm additional_password_data;
additional_password_data.password = base::UTF8ToUTF16(additional_password); additional_password_data.password = base::UTF8ToUTF16(additional_password);
additional_password_data.realm.clear(); additional_password_data.realm.clear();
form_data.additional_logins.insert( form_data->additional_logins.insert(
std::pair<base::string16, autofill::PasswordAndRealm>( std::pair<base::string16, autofill::PasswordAndRealm>(
base::UTF8ToUTF16(additional_username), additional_password_data)); base::UTF8ToUTF16(additional_username), additional_password_data));
} }
form_data.wait_for_username = wait_for_username; form_data->wait_for_username = wait_for_username;
}
void SetFillData(const std::string& origin,
const std::string& action,
const char* username_field,
const char* username_value,
const char* password_field,
const char* password_value,
FillData* fill_data) {
DCHECK(fill_data);
fill_data->origin = GURL(origin);
fill_data->action = GURL(action);
fill_data->username_element = base::UTF8ToUTF16(username_field);
fill_data->username_value = base::UTF8ToUTF16(username_value);
fill_data->password_element = base::UTF8ToUTF16(password_field);
fill_data->password_value = base::UTF8ToUTF16(password_value);
} }
} // namespace test_helpers } // namespace test_helpers
...@@ -9,13 +9,16 @@ ...@@ -9,13 +9,16 @@
namespace autofill { namespace autofill {
struct PasswordFormFillData; struct PasswordFormFillData;
} } // namespace autofill
namespace password_manager {
struct FillData;
} // namespace password_manager
namespace test_helpers { namespace test_helpers {
// Populates |form_data| with test values. // Populates |form_data| with test values.
void SetPasswordFormFillData(autofill::PasswordFormFillData& form_data, void SetPasswordFormFillData(const std::string& origin,
const std::string& origin,
const std::string& action, const std::string& action,
const char* username_field, const char* username_field,
const char* username_value, const char* username_value,
...@@ -23,7 +26,17 @@ void SetPasswordFormFillData(autofill::PasswordFormFillData& form_data, ...@@ -23,7 +26,17 @@ void SetPasswordFormFillData(autofill::PasswordFormFillData& form_data,
const char* password_value, const char* password_value,
const char* additional_username, const char* additional_username,
const char* additional_password, const char* additional_password,
bool wait_for_username); bool wait_for_username,
autofill::PasswordFormFillData* form_data);
// Populates |fill_data| with test values.
void SetFillData(const std::string& origin,
const std::string& action,
const char* username_field,
const char* username_value,
const char* password_field,
const char* password_value,
password_manager::FillData* fill_data);
} // namespace test_helpers } // namespace test_helpers
......
...@@ -727,10 +727,10 @@ TEST_F(PasswordControllerTest, FillPasswordForm) { ...@@ -727,10 +727,10 @@ TEST_F(PasswordControllerTest, FillPasswordForm) {
ExecuteJavaScript(kClearInputFieldsScript); ExecuteJavaScript(kClearInputFieldsScript);
PasswordFormFillData form_data; PasswordFormFillData form_data;
SetPasswordFormFillData(form_data, data.origin, data.action, SetPasswordFormFillData(data.origin, data.action, data.username_field,
data.username_field, data.username_value, data.username_value, data.password_field,
data.password_field, data.password_value, nullptr, data.password_value, nullptr, nullptr, false,
nullptr, false); &form_data);
__block BOOL block_was_called = NO; __block BOOL block_was_called = NO;
[passwordController_ fillPasswordForm:form_data [passwordController_ fillPasswordForm:form_data
...@@ -810,8 +810,8 @@ BOOL PasswordControllerTest::BasicFormFill(NSString* html) { ...@@ -810,8 +810,8 @@ BOOL PasswordControllerTest::BasicFormFill(NSString* html) {
LoadHtml(html); LoadHtml(html);
const std::string base_url = BaseUrl(); const std::string base_url = BaseUrl();
PasswordFormFillData form_data; PasswordFormFillData form_data;
SetPasswordFormFillData(form_data, base_url, base_url, "un0", "test_user", SetPasswordFormFillData(base_url, base_url, "un0", "test_user", "pw0",
"pw0", "test_password", nullptr, nullptr, false); "test_password", nullptr, nullptr, false, &form_data);
__block BOOL block_was_called = NO; __block BOOL block_was_called = NO;
__block BOOL return_value = NO; __block BOOL return_value = NO;
[passwordController_ fillPasswordForm:form_data [passwordController_ fillPasswordForm:form_data
...@@ -938,8 +938,8 @@ TEST_F(PasswordControllerTest, SuggestionUpdateTests) { ...@@ -938,8 +938,8 @@ TEST_F(PasswordControllerTest, SuggestionUpdateTests) {
// we can test with an initially-empty username field. Testing with a // we can test with an initially-empty username field. Testing with a
// username field that contains input is performed by a specific test below. // username field that contains input is performed by a specific test below.
PasswordFormFillData form_data; PasswordFormFillData form_data;
SetPasswordFormFillData(form_data, base_url, base_url, "un", "user0", "pw", SetPasswordFormFillData(base_url, base_url, "un", "user0", "pw", "password0",
"password0", "abc", "def", true); "abc", "def", true, &form_data);
form_data.name = base::ASCIIToUTF16(FormName(0)); form_data.name = base::ASCIIToUTF16(FormName(0));
__block BOOL block_was_called = NO; __block BOOL block_was_called = NO;
...@@ -1035,9 +1035,9 @@ TEST_F(PasswordControllerTest, SelectingSuggestionShouldFillPasswordForm) { ...@@ -1035,9 +1035,9 @@ TEST_F(PasswordControllerTest, SelectingSuggestionShouldFillPasswordForm) {
const auto& test_data = kTestData[form_i]; const auto& test_data = kTestData[form_i];
PasswordFormFillData form_data; PasswordFormFillData form_data;
SetPasswordFormFillData( SetPasswordFormFillData(base_url, base_url, test_data.username_element,
form_data, base_url, base_url, test_data.username_element, "user0", "user0", test_data.password_element, "password0",
test_data.password_element, "password0", "abc", "def", true); "abc", "def", true, &form_data);
form_data.name = base::ASCIIToUTF16(test_data.form_name); form_data.name = base::ASCIIToUTF16(test_data.form_name);
__block BOOL block_was_called = NO; __block BOOL block_was_called = NO;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment