Chromium Code Reviews| 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 7b310372848d9f2f916f9030edf7c41cdc5425e8..11446ba5e18a440b9f2b39b1fdecb02dc66ac384 100644 |
| --- a/chrome/renderer/autofill/password_autofill_agent_browsertest.cc |
| +++ b/chrome/renderer/autofill/password_autofill_agent_browsertest.cc |
| @@ -21,6 +21,7 @@ |
| #include "third_party/WebKit/public/web/WebDocument.h" |
| #include "third_party/WebKit/public/web/WebElement.h" |
| #include "third_party/WebKit/public/web/WebFormElement.h" |
| +#include "third_party/WebKit/public/web/WebFormControlElement.h" |
| #include "third_party/WebKit/public/web/WebInputElement.h" |
| #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| #include "third_party/WebKit/public/web/WebNode.h" |
| @@ -44,6 +45,7 @@ const int kPasswordFillFormDataId = 1234; |
| // The name of the username/password element in the form. |
| const char kUsernameName[] = "username"; |
| const char kPasswordName[] = "password"; |
| +const char kEmailName[] = "email"; |
| const char kAliceUsername[] = "alice"; |
| const char kAlicePassword[] = "password"; |
| @@ -160,6 +162,15 @@ const char kOnChangeDetectionScript[] = |
| " };" |
| "</script>"; |
| +const char kFormHTMLWithTwoTextFields[] = |
| + "<FORM name='LoginTestForm' id='LoginTestForm' " |
| + "action='http://www.bidule.com'>" |
| + " <INPUT type='text' id='username'/>" |
| + " <INPUT type='text' id='email'/>" |
| + " <INPUT type='password' id='password'/>" |
| + " <INPUT type='submit' value='Login'/>" |
| + "</FORM>"; |
| + |
| // Sets the "readonly" attribute of |element| to the value given by |read_only|. |
| void SetElementReadOnly(WebInputElement& element, bool read_only) { |
| element.setAttribute(WebString::fromUTF8("readonly"), |
| @@ -267,6 +278,12 @@ class PasswordAutofillAgentTest : public ChromeRenderViewTest { |
| password_element_ = element.to<blink::WebInputElement>(); |
| } |
| + blink::WebInputElement GetInputElementByID(const char* id) { |
|
Ilya Sherman
2015/03/26 00:30:04
nit: Please pass either a StringPiece or a "const
dvadym
2015/03/27 18:49:01
Done.
|
| + WebDocument document = GetMainFrame()->document(); |
| + WebElement element = document.getElementById(WebString::fromUTF8(id)); |
| + return element.to<blink::WebInputElement>(); |
| + } |
| + |
| void ClearUsernameAndPasswordFields() { |
| username_element_.setValue(""); |
| username_element_.setAutofilled(false); |
| @@ -1854,4 +1871,57 @@ TEST_F(PasswordAutofillAgentTest, |
| CheckTextFieldsState(std::string("foobar"), false, std::string(), false); |
| } |
| +// Test that the last plain text field before a password field is chosen as a |
| +// username, in a form with 2 plain text fields without username predictions. |
| +TEST_F(PasswordAutofillAgentTest, FindingUsernameWithoutAutofillPredictions) { |
| + LoadHTML(kFormHTMLWithTwoTextFields); |
| + UpdateUsernameAndPasswordElements(); |
| + blink::WebInputElement email_element = GetInputElementByID(kEmailName); |
| + SimulateInputChangeForElement("temp", true, GetMainFrame(), username_element_, |
| + true); |
| + SimulateInputChangeForElement("temp@google.com", true, GetMainFrame(), |
| + email_element, true); |
| + SimulateInputChangeForElement("random", true, GetMainFrame(), |
| + password_element_, true); |
| + static_cast<content::RenderFrameObserver*>(password_autofill_agent_) |
| + ->WillSubmitForm(username_element_.form()); |
| + |
| + // Observe that the PasswordAutofillAgent identifies the second field (e-mail) |
| + // as username. |
| + ExpectFormSubmittedWithUsernameAndPasswords("temp@google.com", "random", ""); |
| +} |
| + |
| +// Tests that username predictions are followed when identifying the username |
| +// in a password form with two plain text fields. |
| +TEST_F(PasswordAutofillAgentTest, FindingUsernameWithAutofillPredictions) { |
| + LoadHTML(kFormHTMLWithTwoTextFields); |
| + UpdateUsernameAndPasswordElements(); |
| + blink::WebInputElement email_element = GetInputElementByID(kEmailName); |
| + SimulateInputChangeForElement("temp", true, GetMainFrame(), username_element_, |
| + true); |
| + SimulateInputChangeForElement("temp@google.com", true, GetMainFrame(), |
| + email_element, true); |
| + SimulateInputChangeForElement("random", true, GetMainFrame(), |
| + password_element_, true); |
| + |
| + // Find FormData for visible password form. |
| + blink::WebFormElement form_element = username_element_.form(); |
| + FormData form_data; |
| + ASSERT_TRUE(WebFormElementToFormData( |
| + form_element, blink::WebFormControlElement(), REQUIRE_NONE, EXTRACT_NONE, |
| + &form_data, nullptr)); |
| + // Simulate Autofill predictions: the first field is username. |
| + std::map<autofill::FormData, autofill::FormFieldData> predictions; |
| + predictions[form_data] = form_data.fields[0]; |
| + AutofillMsg_AutofillUsernameDataReceived msg(0, predictions); |
| + static_cast<content::RenderFrameObserver*>(password_autofill_agent_) |
| + ->OnMessageReceived(msg); |
| + static_cast<content::RenderFrameObserver*>(password_autofill_agent_) |
| + ->WillSubmitForm(username_element_.form()); |
| + |
| + // Observe that the PasswordAutofillAgent identifies the first field as |
| + // username. |
| + ExpectFormSubmittedWithUsernameAndPasswords("temp", "random", ""); |
| +} |
| + |
| } // namespace autofill |