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

Side by Side Diff: chrome/renderer/autofill/password_autofill_agent_browsertest.cc

Issue 2889393002: Add username field discovery heuristic. (Closed)
Patch Set: Address comments; add tests. Created 3 years, 6 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/autofill/content/renderer/password_autofill_agent.h" 5 #include "components/autofill/content/renderer/password_autofill_agent.h"
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 void UpdateOriginForHTML(const std::string& html) { 366 void UpdateOriginForHTML(const std::string& html) {
367 std::string origin = "data:text/html;charset=utf-8," + html; 367 std::string origin = "data:text/html;charset=utf-8," + html;
368 fill_data_.origin = GURL(origin); 368 fill_data_.origin = GURL(origin);
369 } 369 }
370 370
371 void UpdateUsernameAndPasswordElements() { 371 void UpdateUsernameAndPasswordElements() {
372 WebDocument document = GetMainFrame()->GetDocument(); 372 WebDocument document = GetMainFrame()->GetDocument();
373 WebElement element = 373 WebElement element =
374 document.GetElementById(WebString::FromUTF8(kUsernameName)); 374 document.GetElementById(WebString::FromUTF8(kUsernameName));
375 ASSERT_FALSE(element.IsNull()); 375 ASSERT_FALSE(element.IsNull());
376 username_element_ = element.To<blink::WebInputElement>(); 376 username_element_ = element.To<WebInputElement>();
377 element = document.GetElementById(WebString::FromUTF8(kPasswordName)); 377 element = document.GetElementById(WebString::FromUTF8(kPasswordName));
378 ASSERT_FALSE(element.IsNull()); 378 ASSERT_FALSE(element.IsNull());
379 password_element_ = element.To<blink::WebInputElement>(); 379 password_element_ = element.To<WebInputElement>();
380 } 380 }
381 381
382 blink::WebInputElement GetInputElementByID(const std::string& id) { 382 void UpdateOnlyPasswordElement() {
383 WebDocument document = GetMainFrame()->GetDocument();
384 WebElement element =
385 document.GetElementById(WebString::FromUTF8(kPasswordName));
386 ASSERT_FALSE(element.IsNull());
387 password_element_ = element.To<WebInputElement>();
388 username_element_.Reset();
389 }
390
391 WebInputElement GetInputElementByID(const std::string& id) {
383 WebDocument document = GetMainFrame()->GetDocument(); 392 WebDocument document = GetMainFrame()->GetDocument();
384 WebElement element = 393 WebElement element =
385 document.GetElementById(WebString::FromUTF8(id.c_str())); 394 document.GetElementById(WebString::FromUTF8(id.c_str()));
386 return element.To<blink::WebInputElement>(); 395 return element.To<WebInputElement>();
387 } 396 }
388 397
389 void ClearUsernameAndPasswordFields() { 398 void ClearUsernameAndPasswordFields() {
390 username_element_.SetValue(WebString()); 399 username_element_.SetValue(WebString());
391 username_element_.SetSuggestedValue(WebString()); 400 username_element_.SetSuggestedValue(WebString());
392 username_element_.SetAutofilled(false); 401 username_element_.SetAutofilled(false);
393 password_element_.SetValue(WebString()); 402 password_element_.SetValue(WebString());
394 password_element_.SetSuggestedValue(WebString()); 403 password_element_.SetSuggestedValue(WebString());
395 password_element_.SetAutofilled(false); 404 password_element_.SetAutofilled(false);
396 } 405 }
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 TEST_F(PasswordAutofillAgentTest, InitialAutocompleteForEmptyAction) { 671 TEST_F(PasswordAutofillAgentTest, InitialAutocompleteForEmptyAction) {
663 const char kEmptyActionFormHTML[] = 672 const char kEmptyActionFormHTML[] =
664 "<FORM name='LoginTestForm'>" 673 "<FORM name='LoginTestForm'>"
665 " <INPUT type='text' id='username'/>" 674 " <INPUT type='text' id='username'/>"
666 " <INPUT type='password' id='password'/>" 675 " <INPUT type='password' id='password'/>"
667 " <INPUT type='submit' value='Login'/>" 676 " <INPUT type='submit' value='Login'/>"
668 "</FORM>"; 677 "</FORM>";
669 LoadHTML(kEmptyActionFormHTML); 678 LoadHTML(kEmptyActionFormHTML);
670 679
671 // Retrieve the input elements so the test can access them. 680 // Retrieve the input elements so the test can access them.
672 WebDocument document = GetMainFrame()->GetDocument(); 681 UpdateUsernameAndPasswordElements();
673 WebElement element =
674 document.GetElementById(WebString::FromUTF8(kUsernameName));
675 ASSERT_FALSE(element.IsNull());
676 username_element_ = element.To<blink::WebInputElement>();
677 element = document.GetElementById(WebString::FromUTF8(kPasswordName));
678 ASSERT_FALSE(element.IsNull());
679 password_element_ = element.To<blink::WebInputElement>();
680 682
681 // Set the expected form origin and action URLs. 683 // Set the expected form origin and action URLs.
682 UpdateOriginForHTML(kEmptyActionFormHTML); 684 UpdateOriginForHTML(kEmptyActionFormHTML);
683 fill_data_.action = GURL(); 685 fill_data_.action = GURL();
684 686
685 // Simulate the browser sending back the login info, it triggers the 687 // Simulate the browser sending back the login info, it triggers the
686 // autocomplete. 688 // autocomplete.
687 SimulateOnFillPasswordForm(fill_data_); 689 SimulateOnFillPasswordForm(fill_data_);
688 690
689 // The username and password should have been autocompleted. 691 // The username and password should have been autocompleted.
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 TEST_F(PasswordAutofillAgentTest, NoAutocompleteForTextFieldPasswords) { 764 TEST_F(PasswordAutofillAgentTest, NoAutocompleteForTextFieldPasswords) {
763 const char kTextFieldPasswordFormHTML[] = 765 const char kTextFieldPasswordFormHTML[] =
764 "<FORM name='LoginTestForm' action='http://www.bidule.com'>" 766 "<FORM name='LoginTestForm' action='http://www.bidule.com'>"
765 " <INPUT type='text' id='username'/>" 767 " <INPUT type='text' id='username'/>"
766 " <INPUT type='text' id='password'/>" 768 " <INPUT type='text' id='password'/>"
767 " <INPUT type='submit' value='Login'/>" 769 " <INPUT type='submit' value='Login'/>"
768 "</FORM>"; 770 "</FORM>";
769 LoadHTML(kTextFieldPasswordFormHTML); 771 LoadHTML(kTextFieldPasswordFormHTML);
770 772
771 // Retrieve the input elements so the test can access them. 773 // Retrieve the input elements so the test can access them.
772 WebDocument document = GetMainFrame()->GetDocument(); 774 UpdateUsernameAndPasswordElements();
773 WebElement element =
774 document.GetElementById(WebString::FromUTF8(kUsernameName));
775 ASSERT_FALSE(element.IsNull());
776 username_element_ = element.To<blink::WebInputElement>();
777 element = document.GetElementById(WebString::FromUTF8(kPasswordName));
778 ASSERT_FALSE(element.IsNull());
779 password_element_ = element.To<blink::WebInputElement>();
780 775
781 // Set the expected form origin URL. 776 // Set the expected form origin URL.
782 UpdateOriginForHTML(kTextFieldPasswordFormHTML); 777 UpdateOriginForHTML(kTextFieldPasswordFormHTML);
783 778
784 SimulateOnFillPasswordForm(fill_data_); 779 SimulateOnFillPasswordForm(fill_data_);
785 780
786 // Fields should still be empty. 781 // Fields should still be empty.
787 CheckTextFieldsState(std::string(), false, std::string(), false); 782 CheckTextFieldsState(std::string(), false, std::string(), false);
788 } 783 }
789 784
790 TEST_F(PasswordAutofillAgentTest, NoAutocompleteForPasswordFieldUsernames) { 785 TEST_F(PasswordAutofillAgentTest, NoAutocompleteForPasswordFieldUsernames) {
791 const char kPasswordFieldUsernameFormHTML[] = 786 const char kPasswordFieldUsernameFormHTML[] =
792 "<FORM name='LoginTestForm' action='http://www.bidule.com'>" 787 "<FORM name='LoginTestForm' action='http://www.bidule.com'>"
793 " <INPUT type='password' id='username'/>" 788 " <INPUT type='password' id='username'/>"
794 " <INPUT type='password' id='password'/>" 789 " <INPUT type='password' id='password'/>"
795 " <INPUT type='submit' value='Login'/>" 790 " <INPUT type='submit' value='Login'/>"
796 "</FORM>"; 791 "</FORM>";
797 LoadHTML(kPasswordFieldUsernameFormHTML); 792 LoadHTML(kPasswordFieldUsernameFormHTML);
798 793
799 // Retrieve the input elements so the test can access them. 794 // Retrieve the input elements so the test can access them.
800 WebDocument document = GetMainFrame()->GetDocument(); 795 UpdateUsernameAndPasswordElements();
801 WebElement element =
802 document.GetElementById(WebString::FromUTF8(kUsernameName));
803 ASSERT_FALSE(element.IsNull());
804 username_element_ = element.To<blink::WebInputElement>();
805 element = document.GetElementById(WebString::FromUTF8(kPasswordName));
806 ASSERT_FALSE(element.IsNull());
807 password_element_ = element.To<blink::WebInputElement>();
808 796
809 // Set the expected form origin URL. 797 // Set the expected form origin URL.
810 UpdateOriginForHTML(kPasswordFieldUsernameFormHTML); 798 UpdateOriginForHTML(kPasswordFieldUsernameFormHTML);
811 799
812 SimulateOnFillPasswordForm(fill_data_); 800 SimulateOnFillPasswordForm(fill_data_);
813 801
814 // Fields should still be empty. 802 // Fields should still be empty.
815 CheckTextFieldsState(std::string(), false, std::string(), false); 803 CheckTextFieldsState(std::string(), false, std::string(), false);
816 } 804 }
817 805
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
1175 selected_element, ASCIIToUTF16(kBobUsername), 1163 selected_element, ASCIIToUTF16(kBobUsername),
1176 ASCIIToUTF16(kCarolPassword))); 1164 ASCIIToUTF16(kCarolPassword)));
1177 CheckTextFieldsDOMState(kBobUsername, true, kCarolPassword, true); 1165 CheckTextFieldsDOMState(kBobUsername, true, kCarolPassword, true);
1178 username_length = strlen(kBobUsername); 1166 username_length = strlen(kBobUsername);
1179 CheckUsernameSelection(username_length, username_length); 1167 CheckUsernameSelection(username_length, username_length);
1180 1168
1181 ClearUsernameAndPasswordFields(); 1169 ClearUsernameAndPasswordFields();
1182 } 1170 }
1183 } 1171 }
1184 1172
1173 // Tests that |FillSuggestion| properly fills the username and password when the
1174 // username field is created dynamically in JavaScript.
1175 TEST_F(PasswordAutofillAgentTest, FillSuggestionWithDynamicUsernameField) {
1176 LoadHTML(kVisibleFormWithNoUsernameHTML);
1177 UpdateOnlyPasswordElement();
1178
1179 // Simulate the browser sending the login info, but set |wait_for_username|
1180 // to prevent the form from being immediately filled.
1181 fill_data_.wait_for_username = true;
1182 SimulateOnFillPasswordForm(fill_data_);
1183
1184 constexpr const char* kAddUsernameToFormScript =
1185 "var new_input = document.createElement('input');"
1186 "new_input.setAttribute('type', 'text');"
1187 "new_input.setAttribute('id', 'username');"
1188 "password_field = document.getElementById('password');"
1189 "password_field.parentNode.insertBefore(new_input, password_field);";
1190 ExecuteJavaScriptForTests(kAddUsernameToFormScript);
1191 UpdateUsernameAndPasswordElements();
1192 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1193
1194 // After filling with the suggestion, both fields should be autocompleted.
1195 EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
1196 password_element_, ASCIIToUTF16(kAliceUsername),
1197 ASCIIToUTF16(kAlicePassword)));
1198
1199 CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
1200 }
1201
1185 // Tests that |FillSuggestion| doesn't change non-empty non-autofilled username 1202 // Tests that |FillSuggestion| doesn't change non-empty non-autofilled username
1186 // when interacting with the password field. 1203 // when interacting with the password field.
1187 TEST_F(PasswordAutofillAgentTest, 1204 TEST_F(PasswordAutofillAgentTest,
1188 FillSuggestionFromPasswordFieldWithUsernameManuallyFilled) { 1205 FillSuggestionFromPasswordFieldWithUsernameManuallyFilled) {
1189 username_element_.SetValue(WebString::FromUTF8("user1")); 1206 username_element_.SetValue(WebString::FromUTF8("user1"));
1190 1207
1191 // Simulate the browser sending the login info, but set |wait_for_username| to 1208 // Simulate the browser sending the login info, but set |wait_for_username| to
1192 // prevent the form from being immediately filled. 1209 // prevent the form from being immediately filled.
1193 fill_data_.wait_for_username = true; 1210 fill_data_.wait_for_username = true;
1194 SimulateOnFillPasswordForm(fill_data_); 1211 SimulateOnFillPasswordForm(fill_data_);
(...skipping 721 matching lines...) Expand 10 before | Expand all | Expand 10 after
1916 SimulateOnShowInitialPasswordAccountSuggestions(fill_data_); 1933 SimulateOnShowInitialPasswordAccountSuggestions(fill_data_);
1917 1934
1918 CheckTextFieldsState(std::string("Carol"), false, std::string(), false); 1935 CheckTextFieldsState(std::string("Carol"), false, std::string(), false);
1919 } 1936 }
1920 1937
1921 TEST_F(PasswordAutofillAgentTest, FillOnAccountSelectOnlyNoUsername) { 1938 TEST_F(PasswordAutofillAgentTest, FillOnAccountSelectOnlyNoUsername) {
1922 SetFillOnAccountSelect(); 1939 SetFillOnAccountSelect();
1923 1940
1924 // Load a form with no username and update test data. 1941 // Load a form with no username and update test data.
1925 LoadHTML(kVisibleFormWithNoUsernameHTML); 1942 LoadHTML(kVisibleFormWithNoUsernameHTML);
1926 username_element_.Reset(); 1943 UpdateOnlyPasswordElement();
1927 WebDocument document = GetMainFrame()->GetDocument();
1928 WebElement element =
1929 document.GetElementById(WebString::FromUTF8(kPasswordName));
1930 ASSERT_FALSE(element.IsNull());
1931 password_element_ = element.To<blink::WebInputElement>();
1932 fill_data_.username_field = FormFieldData(); 1944 fill_data_.username_field = FormFieldData();
1933 UpdateOriginForHTML(kVisibleFormWithNoUsernameHTML); 1945 UpdateOriginForHTML(kVisibleFormWithNoUsernameHTML);
1934 fill_data_.additional_logins.clear(); 1946 fill_data_.additional_logins.clear();
1935 fill_data_.other_possible_usernames.clear(); 1947 fill_data_.other_possible_usernames.clear();
1936 1948
1937 password_element_.SetValue(""); 1949 password_element_.SetValue("");
1938 password_element_.SetAutofilled(false); 1950 password_element_.SetAutofilled(false);
1939 1951
1940 // Simulate the browser sending back the login info for an initial page load. 1952 // Simulate the browser sending back the login info for an initial page load.
1941 SimulateOnShowInitialPasswordAccountSuggestions(fill_data_); 1953 SimulateOnShowInitialPasswordAccountSuggestions(fill_data_);
1942 1954
1943 EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty()); 1955 EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
1944 EXPECT_FALSE(password_element_.IsAutofilled()); 1956 EXPECT_FALSE(password_element_.IsAutofilled());
1945 CheckSuggestions(std::string(), false); 1957 CheckSuggestions(std::string(), false);
1946 } 1958 }
1947 1959
1948 TEST_F(PasswordAutofillAgentTest, ShowPopupOnEmptyPasswordField) { 1960 TEST_F(PasswordAutofillAgentTest, ShowPopupOnEmptyPasswordField) {
1949 // Load a form with no username and update test data. 1961 // Load a form with no username and update test data.
1950 LoadHTML(kVisibleFormWithNoUsernameHTML); 1962 LoadHTML(kVisibleFormWithNoUsernameHTML);
1951 username_element_.Reset(); 1963 UpdateOnlyPasswordElement();
1952 WebDocument document = GetMainFrame()->GetDocument();
1953 WebElement element =
1954 document.GetElementById(WebString::FromUTF8(kPasswordName));
1955 ASSERT_FALSE(element.IsNull());
1956 password_element_ = element.To<blink::WebInputElement>();
1957 fill_data_.username_field = FormFieldData(); 1964 fill_data_.username_field = FormFieldData();
1958 UpdateOriginForHTML(kVisibleFormWithNoUsernameHTML); 1965 UpdateOriginForHTML(kVisibleFormWithNoUsernameHTML);
1959 fill_data_.additional_logins.clear(); 1966 fill_data_.additional_logins.clear();
1960 fill_data_.other_possible_usernames.clear(); 1967 fill_data_.other_possible_usernames.clear();
1961 1968
1962 password_element_.SetValue(""); 1969 password_element_.SetValue("");
1963 password_element_.SetAutofilled(false); 1970 password_element_.SetAutofilled(false);
1964 1971
1965 // Simulate the browser sending back the login info for an initial page load. 1972 // Simulate the browser sending back the login info for an initial page load.
1966 SimulateOnFillPasswordForm(fill_data_); 1973 SimulateOnFillPasswordForm(fill_data_);
1967 1974
1968 // Show popup suggesstion when the password field is empty. 1975 // Show popup suggesstion when the password field is empty.
1969 password_element_.SetValue(""); 1976 password_element_.SetValue("");
1970 password_element_.SetAutofilled(false); 1977 password_element_.SetAutofilled(false);
1971 1978
1972 SimulateSuggestionChoiceOfUsernameAndPassword( 1979 SimulateSuggestionChoiceOfUsernameAndPassword(
1973 password_element_, base::string16(), ASCIIToUTF16(kAlicePassword)); 1980 password_element_, base::string16(), ASCIIToUTF16(kAlicePassword));
1974 CheckSuggestions(std::string(), false); 1981 CheckSuggestions(std::string(), false);
1975 EXPECT_EQ(ASCIIToUTF16(kAlicePassword), password_element_.Value().Utf16()); 1982 EXPECT_EQ(ASCIIToUTF16(kAlicePassword), password_element_.Value().Utf16());
1976 EXPECT_TRUE(password_element_.IsAutofilled()); 1983 EXPECT_TRUE(password_element_.IsAutofilled());
1977 } 1984 }
1978 1985
1979 TEST_F(PasswordAutofillAgentTest, ShowPopupOnAutofilledPasswordField) { 1986 TEST_F(PasswordAutofillAgentTest, ShowPopupOnAutofilledPasswordField) {
1980 // Load a form with no username and update test data. 1987 // Load a form with no username and update test data.
1981 LoadHTML(kVisibleFormWithNoUsernameHTML); 1988 LoadHTML(kVisibleFormWithNoUsernameHTML);
1982 username_element_.Reset(); 1989 UpdateOnlyPasswordElement();
1983 WebDocument document = GetMainFrame()->GetDocument();
1984 WebElement element =
1985 document.GetElementById(WebString::FromUTF8(kPasswordName));
1986 ASSERT_FALSE(element.IsNull());
1987 password_element_ = element.To<blink::WebInputElement>();
1988 fill_data_.username_field = FormFieldData(); 1990 fill_data_.username_field = FormFieldData();
1989 UpdateOriginForHTML(kVisibleFormWithNoUsernameHTML); 1991 UpdateOriginForHTML(kVisibleFormWithNoUsernameHTML);
1990 fill_data_.additional_logins.clear(); 1992 fill_data_.additional_logins.clear();
1991 fill_data_.other_possible_usernames.clear(); 1993 fill_data_.other_possible_usernames.clear();
1992 1994
1993 password_element_.SetValue(""); 1995 password_element_.SetValue("");
1994 password_element_.SetAutofilled(false); 1996 password_element_.SetAutofilled(false);
1995 1997
1996 // Simulate the browser sending back the login info for an initial page load. 1998 // Simulate the browser sending back the login info for an initial page load.
1997 SimulateOnFillPasswordForm(fill_data_); 1999 SimulateOnFillPasswordForm(fill_data_);
1998 2000
1999 // Show popup suggesstion when the password field is autofilled. 2001 // Show popup suggesstion when the password field is autofilled.
2000 password_element_.SetValue("123"); 2002 password_element_.SetValue("123");
2001 password_element_.SetAutofilled(true); 2003 password_element_.SetAutofilled(true);
2002 2004
2003 SimulateSuggestionChoiceOfUsernameAndPassword( 2005 SimulateSuggestionChoiceOfUsernameAndPassword(
2004 password_element_, base::string16(), ASCIIToUTF16(kAlicePassword)); 2006 password_element_, base::string16(), ASCIIToUTF16(kAlicePassword));
2005 CheckSuggestions(std::string(), false); 2007 CheckSuggestions(std::string(), false);
2006 EXPECT_EQ(ASCIIToUTF16(kAlicePassword), password_element_.Value().Utf16()); 2008 EXPECT_EQ(ASCIIToUTF16(kAlicePassword), password_element_.Value().Utf16());
2007 EXPECT_TRUE(password_element_.IsAutofilled()); 2009 EXPECT_TRUE(password_element_.IsAutofilled());
2008 } 2010 }
2009 2011
2010 TEST_F(PasswordAutofillAgentTest, NotShowPopupPasswordField) { 2012 TEST_F(PasswordAutofillAgentTest, NotShowPopupPasswordField) {
2011 // Load a form with no username and update test data. 2013 // Load a form with no username and update test data.
2012 LoadHTML(kVisibleFormWithNoUsernameHTML); 2014 LoadHTML(kVisibleFormWithNoUsernameHTML);
2013 username_element_.Reset(); 2015 UpdateOnlyPasswordElement();
2014 WebDocument document = GetMainFrame()->GetDocument();
2015 WebElement element =
2016 document.GetElementById(WebString::FromUTF8(kPasswordName));
2017 ASSERT_FALSE(element.IsNull());
2018 password_element_ = element.To<blink::WebInputElement>();
2019 fill_data_.username_field = FormFieldData(); 2016 fill_data_.username_field = FormFieldData();
2020 UpdateOriginForHTML(kVisibleFormWithNoUsernameHTML); 2017 UpdateOriginForHTML(kVisibleFormWithNoUsernameHTML);
2021 fill_data_.additional_logins.clear(); 2018 fill_data_.additional_logins.clear();
2022 fill_data_.other_possible_usernames.clear(); 2019 fill_data_.other_possible_usernames.clear();
2023 2020
2024 password_element_.SetValue(""); 2021 password_element_.SetValue("");
2025 password_element_.SetAutofilled(false); 2022 password_element_.SetAutofilled(false);
2026 2023
2027 // Simulate the browser sending back the login info for an initial page load. 2024 // Simulate the browser sending back the login info for an initial page load.
2028 SimulateOnFillPasswordForm(fill_data_); 2025 SimulateOnFillPasswordForm(fill_data_);
(...skipping 21 matching lines...) Expand all
2050 SetElementReadOnly(username_element_, true); 2047 SetElementReadOnly(username_element_, true);
2051 2048
2052 CheckTextFieldsState(std::string("foobar"), false, std::string(), false); 2049 CheckTextFieldsState(std::string("foobar"), false, std::string(), false);
2053 } 2050 }
2054 2051
2055 // Test that the last plain text field before a password field is chosen as a 2052 // Test that the last plain text field before a password field is chosen as a
2056 // username, in a form with 2 plain text fields without username predictions. 2053 // username, in a form with 2 plain text fields without username predictions.
2057 TEST_F(PasswordAutofillAgentTest, FindingUsernameWithoutAutofillPredictions) { 2054 TEST_F(PasswordAutofillAgentTest, FindingUsernameWithoutAutofillPredictions) {
2058 LoadHTML(kFormHTMLWithTwoTextFields); 2055 LoadHTML(kFormHTMLWithTwoTextFields);
2059 UpdateUsernameAndPasswordElements(); 2056 UpdateUsernameAndPasswordElements();
2060 blink::WebInputElement email_element = GetInputElementByID(kEmailName); 2057 WebInputElement email_element = GetInputElementByID(kEmailName);
2061 SimulateUsernameChange("temp"); 2058 SimulateUsernameChange("temp");
2062 SimulateUserInputChangeForElement(&email_element, "temp@google.com"); 2059 SimulateUserInputChangeForElement(&email_element, "temp@google.com");
2063 SimulatePasswordChange("random"); 2060 SimulatePasswordChange("random");
2064 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) 2061 static_cast<content::RenderFrameObserver*>(password_autofill_agent_)
2065 ->WillSendSubmitEvent(username_element_.Form()); 2062 ->WillSendSubmitEvent(username_element_.Form());
2066 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) 2063 static_cast<content::RenderFrameObserver*>(password_autofill_agent_)
2067 ->WillSubmitForm(username_element_.Form()); 2064 ->WillSubmitForm(username_element_.Form());
2068 2065
2069 // Observe that the PasswordAutofillAgent identifies the second field (e-mail) 2066 // Observe that the PasswordAutofillAgent identifies the second field (e-mail)
2070 // as username. 2067 // as username.
2071 ExpectFormSubmittedWithUsernameAndPasswords("temp@google.com", "random", ""); 2068 ExpectFormSubmittedWithUsernameAndPasswords("temp@google.com", "random", "");
2072 } 2069 }
2073 2070
2074 // Tests that field predictions are followed when identifying the username 2071 // Tests that field predictions are followed when identifying the username
2075 // and password in a password form with two plain text fields. 2072 // and password in a password form with two plain text fields.
2076 TEST_F(PasswordAutofillAgentTest, FindingFieldsWithAutofillPredictions) { 2073 TEST_F(PasswordAutofillAgentTest, FindingFieldsWithAutofillPredictions) {
2077 LoadHTML(kFormHTMLWithTwoTextFields); 2074 LoadHTML(kFormHTMLWithTwoTextFields);
2078 UpdateUsernameAndPasswordElements(); 2075 UpdateUsernameAndPasswordElements();
2079 blink::WebInputElement email_element = GetInputElementByID(kEmailName); 2076 WebInputElement email_element = GetInputElementByID(kEmailName);
2080 SimulateUsernameChange("temp"); 2077 SimulateUsernameChange("temp");
2081 SimulateUserInputChangeForElement(&email_element, "temp@google.com"); 2078 SimulateUserInputChangeForElement(&email_element, "temp@google.com");
2082 SimulatePasswordChange("random"); 2079 SimulatePasswordChange("random");
2083 // Find FormData for visible password form. 2080 // Find FormData for visible password form.
2084 WebFormElement form_element = username_element_.Form(); 2081 WebFormElement form_element = username_element_.Form();
2085 FormData form_data; 2082 FormData form_data;
2086 ASSERT_TRUE(WebFormElementToFormData( 2083 ASSERT_TRUE(WebFormElementToFormData(
2087 form_element, blink::WebFormControlElement(), nullptr, 2084 form_element, blink::WebFormControlElement(), nullptr,
2088 form_util::EXTRACT_NONE, &form_data, nullptr)); 2085 form_util::EXTRACT_NONE, &form_data, nullptr));
2089 // Simulate Autofill predictions: the first field is username, the third 2086 // Simulate Autofill predictions: the first field is username, the third
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
2177 // If password generation is enabled for a field, password autofill should not 2174 // If password generation is enabled for a field, password autofill should not
2178 // show UI. 2175 // show UI.
2179 TEST_F(PasswordAutofillAgentTest, PasswordGenerationSupersedesAutofill) { 2176 TEST_F(PasswordAutofillAgentTest, PasswordGenerationSupersedesAutofill) {
2180 LoadHTML(kSignupFormHTML); 2177 LoadHTML(kSignupFormHTML);
2181 2178
2182 // Update password_element_; 2179 // Update password_element_;
2183 WebDocument document = GetMainFrame()->GetDocument(); 2180 WebDocument document = GetMainFrame()->GetDocument();
2184 WebElement element = 2181 WebElement element =
2185 document.GetElementById(WebString::FromUTF8("new_password")); 2182 document.GetElementById(WebString::FromUTF8("new_password"));
2186 ASSERT_FALSE(element.IsNull()); 2183 ASSERT_FALSE(element.IsNull());
2187 password_element_ = element.To<blink::WebInputElement>(); 2184 password_element_ = element.To<WebInputElement>();
2188 2185
2189 // Update fill_data_ for the new form and simulate filling. Pretend as if 2186 // Update fill_data_ for the new form and simulate filling. Pretend as if
2190 // the password manager didn't detect a username field so it will try to 2187 // the password manager didn't detect a username field so it will try to
2191 // show UI when the password field is focused. 2188 // show UI when the password field is focused.
2192 fill_data_.wait_for_username = true; 2189 fill_data_.wait_for_username = true;
2193 fill_data_.username_field = FormFieldData(); 2190 fill_data_.username_field = FormFieldData();
2194 fill_data_.password_field.name = base::ASCIIToUTF16("new_password"); 2191 fill_data_.password_field.name = base::ASCIIToUTF16("new_password");
2195 UpdateOriginForHTML(kSignupFormHTML); 2192 UpdateOriginForHTML(kSignupFormHTML);
2196 SimulateOnFillPasswordForm(fill_data_); 2193 SimulateOnFillPasswordForm(fill_data_);
2197 2194
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
2269 // message. 2266 // message.
2270 static_cast<PageClickListener*>(autofill_agent_) 2267 static_cast<PageClickListener*>(autofill_agent_)
2271 ->FormControlElementClicked(password_element_, true); 2268 ->FormControlElementClicked(password_element_, true);
2272 CheckSuggestions("", false); 2269 CheckSuggestions("", false);
2273 } 2270 }
2274 2271
2275 // Tests that NOT_PASSWORD field predictions are followed so that no password 2272 // Tests that NOT_PASSWORD field predictions are followed so that no password
2276 // form is submitted. 2273 // form is submitted.
2277 TEST_F(PasswordAutofillAgentTest, IgnoreNotPasswordFields) { 2274 TEST_F(PasswordAutofillAgentTest, IgnoreNotPasswordFields) {
2278 LoadHTML(kCreditCardFormHTML); 2275 LoadHTML(kCreditCardFormHTML);
2279 blink::WebInputElement credit_card_owner_element = 2276 WebInputElement credit_card_owner_element =
2280 GetInputElementByID(kCreditCardOwnerName); 2277 GetInputElementByID(kCreditCardOwnerName);
2281 blink::WebInputElement credit_card_number_element = 2278 WebInputElement credit_card_number_element =
2282 GetInputElementByID(kCreditCardNumberName); 2279 GetInputElementByID(kCreditCardNumberName);
2283 blink::WebInputElement credit_card_verification_element = 2280 WebInputElement credit_card_verification_element =
2284 GetInputElementByID(kCreditCardVerificationName); 2281 GetInputElementByID(kCreditCardVerificationName);
2285 SimulateUserInputChangeForElement(&credit_card_owner_element, "JohnSmith"); 2282 SimulateUserInputChangeForElement(&credit_card_owner_element, "JohnSmith");
2286 SimulateUserInputChangeForElement(&credit_card_number_element, 2283 SimulateUserInputChangeForElement(&credit_card_number_element,
2287 "1234123412341234"); 2284 "1234123412341234");
2288 SimulateUserInputChangeForElement(&credit_card_verification_element, "123"); 2285 SimulateUserInputChangeForElement(&credit_card_verification_element, "123");
2289 // Find FormData for visible form. 2286 // Find FormData for visible form.
2290 WebFormElement form_element = credit_card_number_element.Form(); 2287 WebFormElement form_element = credit_card_number_element.Form();
2291 FormData form_data; 2288 FormData form_data;
2292 ASSERT_TRUE(WebFormElementToFormData( 2289 ASSERT_TRUE(WebFormElementToFormData(
2293 form_element, blink::WebFormControlElement(), nullptr, 2290 form_element, blink::WebFormControlElement(), nullptr,
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after
2629 // Get the username and password form input elelments. 2626 // Get the username and password form input elelments.
2630 blink::WebDocument document = GetMainFrame()->GetDocument(); 2627 blink::WebDocument document = GetMainFrame()->GetDocument();
2631 blink::WebVector<WebFormElement> forms; 2628 blink::WebVector<WebFormElement> forms;
2632 document.Forms(forms); 2629 document.Forms(forms);
2633 WebFormElement form_element = forms[0]; 2630 WebFormElement form_element = forms[0];
2634 std::vector<blink::WebFormControlElement> control_elements = 2631 std::vector<blink::WebFormControlElement> control_elements =
2635 form_util::ExtractAutofillableElementsInForm(form_element); 2632 form_util::ExtractAutofillableElementsInForm(form_element);
2636 bool has_fillable_username = 2633 bool has_fillable_username =
2637 (kEmpty != test_case.fill_data_username_field_name); 2634 (kEmpty != test_case.fill_data_username_field_name);
2638 if (has_fillable_username) { 2635 if (has_fillable_username) {
2639 username_element_ = control_elements[0].To<blink::WebInputElement>(); 2636 username_element_ = control_elements[0].To<WebInputElement>();
2640 password_element_ = control_elements[1].To<blink::WebInputElement>(); 2637 password_element_ = control_elements[1].To<WebInputElement>();
2641 } else { 2638 } else {
2642 password_element_ = control_elements[0].To<blink::WebInputElement>(); 2639 password_element_ = control_elements[0].To<WebInputElement>();
2643 } 2640 }
2644 2641
2645 UpdateOriginForHTML(test_case.html_form); 2642 UpdateOriginForHTML(test_case.html_form);
2646 if (test_case.does_trigger_autocomplete_on_fill) { 2643 if (test_case.does_trigger_autocomplete_on_fill) {
2647 // Prepare |fill_data_| to trigger autocomplete. 2644 // Prepare |fill_data_| to trigger autocomplete.
2648 fill_data_.is_possible_change_password_form = 2645 fill_data_.is_possible_change_password_form =
2649 test_case.is_possible_change_password_form; 2646 test_case.is_possible_change_password_form;
2650 fill_data_.username_field.name = 2647 fill_data_.username_field.name =
2651 ASCIIToUTF16(test_case.fill_data_username_field_name); 2648 ASCIIToUTF16(test_case.fill_data_username_field_name);
2652 fill_data_.password_field.name = 2649 fill_data_.password_field.name =
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
2957 TEST_F(PasswordAutofillAgentTest, AutocompleteWhenPageUrlIsChanged) { 2954 TEST_F(PasswordAutofillAgentTest, AutocompleteWhenPageUrlIsChanged) {
2958 // Simulate that JavaScript changes url. 2955 // Simulate that JavaScript changes url.
2959 fill_data_.origin = GURL(fill_data_.origin.possibly_invalid_spec() + "/path"); 2956 fill_data_.origin = GURL(fill_data_.origin.possibly_invalid_spec() + "/path");
2960 2957
2961 SimulateOnFillPasswordForm(fill_data_); 2958 SimulateOnFillPasswordForm(fill_data_);
2962 2959
2963 // The username and password should have been autocompleted. 2960 // The username and password should have been autocompleted.
2964 CheckTextFieldsState(kAliceUsername, true, kAlicePassword, true); 2961 CheckTextFieldsState(kAliceUsername, true, kAlicePassword, true);
2965 } 2962 }
2966 } // namespace autofill 2963 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698