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..37ab84ceb5ee8c250f273af05a73ee316fac0409 100644 |
--- a/chrome/renderer/autofill/password_autofill_agent_browsertest.cc |
+++ b/chrome/renderer/autofill/password_autofill_agent_browsertest.cc |
@@ -20,6 +20,7 @@ |
#include "third_party/WebKit/public/platform/WebVector.h" |
#include "third_party/WebKit/public/web/WebDocument.h" |
#include "third_party/WebKit/public/web/WebElement.h" |
+#include "third_party/WebKit/public/web/WebFormControlElement.h" |
#include "third_party/WebKit/public/web/WebFormElement.h" |
#include "third_party/WebKit/public/web/WebInputElement.h" |
#include "third_party/WebKit/public/web/WebLocalFrame.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,13 @@ class PasswordAutofillAgentTest : public ChromeRenderViewTest { |
password_element_ = element.to<blink::WebInputElement>(); |
} |
+ blink::WebInputElement GetInputElementByID(const std::string& id) { |
+ WebDocument document = GetMainFrame()->document(); |
+ WebElement element = |
+ document.getElementById(WebString::fromUTF8(id.c_str())); |
+ return element.to<blink::WebInputElement>(); |
+ } |
+ |
void ClearUsernameAndPasswordFields() { |
username_element_.setValue(""); |
username_element_.setAutofilled(false); |
@@ -1854,4 +1872,61 @@ 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_) |
+ ->WillSendSubmitEvent(username_element_.form()); |
+ 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_) |
+ ->WillSendSubmitEvent(username_element_.form()); |
+ 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 |