Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4055)

Unified Diff: chrome/renderer/autofill/password_autofill_agent_browsertest.cc

Issue 2902113004: Autofill username when the user interacts with the password field. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/renderer/autofill/password_autofill_agent_browsertest.cc
diff --git a/chrome/renderer/autofill/password_autofill_agent_browsertest.cc b/chrome/renderer/autofill/password_autofill_agent_browsertest.cc
index efd3f73723d3708f8694e2caad81434f27fb16e7..3477f4d04a7efa6d6e1af9f7b05f9933c0544ed8 100644
--- a/chrome/renderer/autofill/password_autofill_agent_browsertest.cc
+++ b/chrome/renderer/autofill/password_autofill_agent_browsertest.cc
@@ -426,13 +426,18 @@ class PasswordAutofillAgentTest : public ChromeRenderViewTest {
const WebInputElement& password_element,
const std::string& password,
bool password_autofilled,
- bool checkSuggestedValue) {
- EXPECT_EQ(username, username_element.Value().Utf8());
+ bool check_suggested_username,
+ bool check_suggested_password) {
+ EXPECT_EQ(username, check_suggested_username
+ ? username_element.SuggestedValue().Utf8()
+ : username_element.Value().Utf8())
+ << "check_suggested_username == " << check_suggested_username;
EXPECT_EQ(username_autofilled, username_element.IsAutofilled());
- EXPECT_EQ(password, checkSuggestedValue
+
+ EXPECT_EQ(password, check_suggested_password
? password_element.SuggestedValue().Utf8()
: password_element.Value().Utf8())
- << "checkSuggestedValue == " << checkSuggestedValue;
+ << "check_suggested_password == " << check_suggested_password;
EXPECT_EQ(password_autofilled, password_element.IsAutofilled());
}
@@ -442,13 +447,9 @@ class PasswordAutofillAgentTest : public ChromeRenderViewTest {
bool username_autofilled,
const std::string& password,
bool password_autofilled) {
- CheckTextFieldsStateForElements(username_element_,
- username,
- username_autofilled,
- password_element_,
- password,
- password_autofilled,
- true);
+ CheckTextFieldsStateForElements(username_element_, username,
+ username_autofilled, password_element_,
+ password, password_autofilled, false, true);
}
// Checks the DOM-accessible value of the username element and the
@@ -457,13 +458,19 @@ class PasswordAutofillAgentTest : public ChromeRenderViewTest {
bool username_autofilled,
const std::string& password,
bool password_autofilled) {
- CheckTextFieldsStateForElements(username_element_,
- username,
- username_autofilled,
- password_element_,
- password,
- password_autofilled,
- false);
+ CheckTextFieldsStateForElements(
+ username_element_, username, username_autofilled, password_element_,
+ password, password_autofilled, false, false);
+ }
+
+ // Checks the suggested values of the |username| and |password| elements.
+ void CheckTextFieldsSuggestedState(const std::string& username,
+ bool username_autofilled,
+ const std::string& password,
+ bool password_autofilled) {
+ CheckTextFieldsStateForElements(username_element_, username,
+ username_autofilled, password_element_,
+ password, password_autofilled, true, true);
}
void CheckUsernameSelection(int start, int end) {
@@ -1139,8 +1146,57 @@ TEST_F(PasswordAutofillAgentTest, FillSuggestion) {
CheckUsernameSelection(username_length, username_length);
}
-// Tests that |FillSuggestion| properly fills the password if username is
-// read-only.
+// Tests that |FillSuggestion| properly fills username/password when interacting
+// with the password field.
+TEST_F(PasswordAutofillAgentTest, FillSuggestionFromPasswordField) {
+ // Simulate the browser sending the login info, but set |wait_for_username| to
+ // prevent the form from being immediately filled.
+ fill_data_.wait_for_username = true;
+ SimulateOnFillPasswordForm(fill_data_);
+ // Neither field should have been autocompleted.
+ CheckTextFieldsDOMState(std::string(), false, std::string(), false);
+
+ // Both fields are autocompletable and should be filled.
+ EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
+ password_element_, ASCIIToUTF16(kAliceUsername),
+ ASCIIToUTF16(kAlicePassword)));
+ CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
+
+ // Try Filling with a different password. Only password should be changed.
pkalinnikov 2017/05/24 10:43:31 selfnit: ... Both fields should be changed.
pkalinnikov 2017/05/24 13:54:36 Done.
+ EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
+ password_element_, ASCIIToUTF16(kBobUsername),
+ ASCIIToUTF16(kCarolPassword)));
+ CheckTextFieldsDOMState(kBobUsername, true, kCarolPassword, true);
+}
+
+// Tests that |FillSuggestion| doesn't change non-empty non-autofilled username
+// when interacting with the password field.
+TEST_F(PasswordAutofillAgentTest,
+ FillSuggestionFromPasswordFieldWithUsernameManuallyFilled) {
+ username_element_.SetValue(WebString::FromUTF8("user1"));
+
+ // Simulate the browser sending the login info, but set |wait_for_username| to
+ // prevent the form from being immediately filled.
+ fill_data_.wait_for_username = true;
+ SimulateOnFillPasswordForm(fill_data_);
+ // Neither field should have been autocompleted.
+ CheckTextFieldsDOMState("user1", false, std::string(), false);
+
+ // Only password field should be autocompleted.
+ EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
+ password_element_, ASCIIToUTF16(kAliceUsername),
+ ASCIIToUTF16(kAlicePassword)));
+ CheckTextFieldsDOMState("user1", false, kAlicePassword, true);
+
+ // Try Filling with a different password. Only password should be changed.
+ EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
+ password_element_, ASCIIToUTF16(kBobUsername),
+ ASCIIToUTF16(kCarolPassword)));
+ CheckTextFieldsDOMState("user1", false, kCarolPassword, true);
+}
+
+// Tests that |FillSuggestion| properly fills the password if the username field
+// is read-only.
TEST_F(PasswordAutofillAgentTest, FillSuggestionIfUsernameReadOnly) {
// Simulate the browser sending the login info.
SetElementReadOnly(username_element_, true);
@@ -1177,20 +1233,14 @@ TEST_F(PasswordAutofillAgentTest, PreviewSuggestion) {
SetElementReadOnly(password_element_, true);
EXPECT_FALSE(password_autofill_agent_->PreviewSuggestion(
username_element_, kAliceUsername, kAlicePassword));
- EXPECT_EQ(std::string(), username_element_.SuggestedValue().Utf8());
- EXPECT_FALSE(username_element_.IsAutofilled());
- EXPECT_EQ(std::string(), password_element_.SuggestedValue().Utf8());
- EXPECT_FALSE(password_element_.IsAutofilled());
+ CheckTextFieldsSuggestedState(std::string(), false, std::string(), false);
SetElementReadOnly(password_element_, false);
// After selecting the suggestion, both fields should be previewed
// with suggested values.
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
username_element_, kAliceUsername, kAlicePassword));
- EXPECT_EQ(kAliceUsername, username_element_.SuggestedValue().Utf8());
- EXPECT_TRUE(username_element_.IsAutofilled());
- EXPECT_EQ(kAlicePassword, password_element_.SuggestedValue().Utf8());
- EXPECT_TRUE(password_element_.IsAutofilled());
+ CheckTextFieldsSuggestedState(kAliceUsername, true, kAlicePassword, true);
int username_length = strlen(kAliceUsername);
CheckUsernameSelection(0, username_length);
@@ -1198,14 +1248,58 @@ TEST_F(PasswordAutofillAgentTest, PreviewSuggestion) {
// sent to the renderer.
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
username_element_, kBobUsername, kCarolPassword));
- EXPECT_EQ(kBobUsername, username_element_.SuggestedValue().Utf8());
- EXPECT_TRUE(username_element_.IsAutofilled());
- EXPECT_EQ(kCarolPassword, password_element_.SuggestedValue().Utf8());
- EXPECT_TRUE(password_element_.IsAutofilled());
+ CheckTextFieldsSuggestedState(kBobUsername, true, kCarolPassword, true);
username_length = strlen(kBobUsername);
CheckUsernameSelection(0, username_length);
}
+// Tests that |PreviewSuggestion| doesn't change non-empty non-autofilled
+// username when previewing autofills on interacting with the password field.
+TEST_F(PasswordAutofillAgentTest,
+ PreviewSuggestionFromPasswordFieldWithUsernameManuallyFilled) {
pkalinnikov 2017/05/24 10:43:31 selfnit: Move this test below PreviewSuggestionFro
pkalinnikov 2017/05/24 13:54:36 Done.
+ username_element_.SetValue(WebString::FromUTF8("user1"));
+
+ // Simulate the browser sending the login info, but set |wait_for_username| to
+ // prevent the form from being immediately filled.
+ fill_data_.wait_for_username = true;
+ SimulateOnFillPasswordForm(fill_data_);
+ // Neither field should have been autocompleted.
+ CheckTextFieldsDOMState("user1", false, std::string(), false);
+
+ // Only password field should be autocompleted.
+ EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
+ password_element_, kAliceUsername, kAlicePassword));
+ CheckTextFieldsSuggestedState(std::string(), false, kAlicePassword, true);
+ CheckTextFieldsDOMState("user1", false, std::string(), true);
+
+ // Try previewing with a different password. Only password should be changed.
+ EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
+ password_element_, kBobUsername, kCarolPassword));
+ CheckTextFieldsSuggestedState(std::string(), false, kCarolPassword, true);
+ CheckTextFieldsDOMState("user1", false, std::string(), true);
+}
+
+// Tests that |PreviewSuggestion| properly previews username/password when
+// interacting with the password field.
+TEST_F(PasswordAutofillAgentTest, PreviewSuggestionFromPasswordField) {
+ // Simulate the browser sending the login info, but set |wait_for_username| to
+ // prevent the form from being immediately filled.
+ fill_data_.wait_for_username = true;
+ SimulateOnFillPasswordForm(fill_data_);
+ // Neither field should have been autocompleted.
+ CheckTextFieldsDOMState(std::string(), false, std::string(), false);
+
+ // Only password field should be autocompleted.
pkalinnikov 2017/05/24 10:43:31 selfnit: // Both fields should be filled with sugg
pkalinnikov 2017/05/24 13:54:36 Done.
+ EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
+ password_element_, kAliceUsername, kAlicePassword));
+ CheckTextFieldsSuggestedState(kAliceUsername, true, kAlicePassword, true);
+
+ // Try previewing with a different password. Only password should be changed.
pkalinnikov 2017/05/24 10:43:31 selfnit: ... Both fields should be changed.
pkalinnikov 2017/05/24 13:54:36 Done.
+ EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
+ password_element_, kBobUsername, kCarolPassword));
+ CheckTextFieldsSuggestedState(kBobUsername, true, kCarolPassword, true);
+}
+
// Tests that |PreviewSuggestion| properly previews the password if username is
// read-only.
TEST_F(PasswordAutofillAgentTest, PreviewSuggestionIfUsernameReadOnly) {
@@ -1219,21 +1313,14 @@ TEST_F(PasswordAutofillAgentTest, PreviewSuggestionIfUsernameReadOnly) {
// Username field is not autocompletable, it should not be affected.
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
password_element_, kAliceUsername, kAlicePassword));
- EXPECT_EQ(std::string(), username_element_.SuggestedValue().Utf8());
- EXPECT_FALSE(username_element_.IsAutofilled());
-
// Password field must be autofilled.
- EXPECT_EQ(kAlicePassword, password_element_.SuggestedValue().Utf8());
- EXPECT_TRUE(password_element_.IsAutofilled());
+ CheckTextFieldsSuggestedState(std::string(), false, kAlicePassword, true);
// Try previewing with a password different from the one that was initially
// sent to the renderer.
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
password_element_, kBobUsername, kCarolPassword));
- EXPECT_EQ(std::string(), username_element_.SuggestedValue().Utf8());
- EXPECT_FALSE(username_element_.IsAutofilled());
- EXPECT_EQ(kCarolPassword, password_element_.SuggestedValue().Utf8());
- EXPECT_TRUE(password_element_.IsAutofilled());
+ CheckTextFieldsSuggestedState(std::string(), false, kCarolPassword, true);
}
// Tests that |PreviewSuggestion| properly sets the username selection range.
@@ -1251,10 +1338,7 @@ TEST_F(PasswordAutofillAgentTest, PreviewSuggestionSelectionRange) {
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
username_element_, kAliceUsername, kAlicePassword));
- EXPECT_EQ(kAliceUsername, username_element_.SuggestedValue().Utf8());
- EXPECT_TRUE(username_element_.IsAutofilled());
- EXPECT_EQ(kAlicePassword, password_element_.SuggestedValue().Utf8());
- EXPECT_TRUE(password_element_.IsAutofilled());
+ CheckTextFieldsSuggestedState(kAliceUsername, true, kAlicePassword, true);
int username_length = strlen(kAliceUsername);
CheckUsernameSelection(3, username_length);
}
@@ -1278,12 +1362,9 @@ TEST_F(PasswordAutofillAgentTest, ClearPreviewWithPasswordAutofilled) {
EXPECT_TRUE(
password_autofill_agent_->DidClearAutofillSelection(username_element_));
- EXPECT_TRUE(username_element_.Value().IsEmpty());
EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty());
- EXPECT_FALSE(username_element_.IsAutofilled());
- EXPECT_EQ(ASCIIToUTF16("sec"), password_element_.Value().Utf16());
EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
- EXPECT_TRUE(password_element_.IsAutofilled());
+ CheckTextFieldsDOMState(std::string(), false, "sec", true);
CheckUsernameSelection(0, 0);
}
@@ -1307,12 +1388,9 @@ TEST_F(PasswordAutofillAgentTest, ClearPreviewWithUsernameAutofilled) {
EXPECT_TRUE(
password_autofill_agent_->DidClearAutofillSelection(username_element_));
- EXPECT_EQ(ASCIIToUTF16("ali"), username_element_.Value().Utf16());
EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty());
- EXPECT_TRUE(username_element_.IsAutofilled());
- EXPECT_TRUE(password_element_.Value().IsEmpty());
EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
- EXPECT_FALSE(password_element_.IsAutofilled());
+ CheckTextFieldsDOMState("ali", true, std::string(), false);
CheckUsernameSelection(3, 3);
}
@@ -1339,12 +1417,37 @@ TEST_F(PasswordAutofillAgentTest,
EXPECT_TRUE(
password_autofill_agent_->DidClearAutofillSelection(username_element_));
- EXPECT_EQ(ASCIIToUTF16("ali"), username_element_.Value().Utf16());
EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty());
- EXPECT_TRUE(username_element_.IsAutofilled());
- EXPECT_EQ(ASCIIToUTF16("sec"), password_element_.Value().Utf16());
EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
- EXPECT_TRUE(password_element_.IsAutofilled());
+ CheckTextFieldsDOMState("ali", true, "sec", true);
+ CheckUsernameSelection(3, 3);
+}
+
+// Tests that, when interacting with the password field, |ClearPreview| properly
+// clears previewed username and password with both being previously autofilled.
+TEST_F(PasswordAutofillAgentTest,
+ ClearPreviewFromPasswordWithAutofilledUsernameAndPassword) {
+ username_element_.SetValue(WebString::FromUTF8("ali"));
+ username_element_.SetSelectionRange(3, 3);
+ username_element_.SetAutofilled(true);
+ password_element_.SetValue(WebString::FromUTF8("sec"));
+ password_element_.SetAutofilled(true);
+
+ // Simulate the browser sending the login info, but set |wait_for_username| to
+ // prevent the form from being immediately filled.
+ fill_data_.wait_for_username = true;
+ SimulateOnFillPasswordForm(fill_data_);
+
+ CheckTextFieldsDOMState("ali", true, "sec", true);
+
+ EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
+ username_element_, kAliceUsername, kAlicePassword));
+ EXPECT_TRUE(
+ password_autofill_agent_->DidClearAutofillSelection(password_element_));
+
+ EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty());
+ EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
+ CheckTextFieldsDOMState("ali", true, "sec", true);
CheckUsernameSelection(3, 3);
}
@@ -1365,12 +1468,9 @@ TEST_F(PasswordAutofillAgentTest,
EXPECT_TRUE(
password_autofill_agent_->DidClearAutofillSelection(username_element_));
- EXPECT_TRUE(username_element_.Value().IsEmpty());
EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty());
- EXPECT_FALSE(username_element_.IsAutofilled());
- EXPECT_TRUE(password_element_.Value().IsEmpty());
EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
- EXPECT_FALSE(password_element_.IsAutofilled());
+ CheckTextFieldsDOMState(std::string(), false, std::string(), false);
CheckUsernameSelection(0, 0);
}
@@ -2156,8 +2256,8 @@ TEST_F(PasswordAutofillAgentTest, FillSuggestionPasswordChangeForms) {
CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
}
-// Tests that a password change form is properly filled with the password when
-// the user click on the password field.
+// Tests that a password change form is properly filled with the
+// username/password when the user clicks on the password field.
TEST_F(PasswordAutofillAgentTest,
FillSuggestionPasswordChangeFormsOnlyPassword) {
LoadHTML(kPasswordChangeFormHTML);
@@ -2175,7 +2275,7 @@ TEST_F(PasswordAutofillAgentTest,
EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
password_element_, ASCIIToUTF16(kAliceUsername),
ASCIIToUTF16(kAlicePassword)));
- CheckTextFieldsDOMState("", false, kAlicePassword, true);
+ CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
}
// Tests that one user click on a username field is sufficient to bring up a

Powered by Google App Engine
This is Rietveld 408576698