OLD | NEW |
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 "base/strings/string_util.h" | 5 #include "base/strings/string_util.h" |
6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
7 #include "chrome/test/base/chrome_render_view_test.h" | 7 #include "chrome/test/base/chrome_render_view_test.h" |
8 #include "components/autofill/content/common/autofill_messages.h" | 8 #include "components/autofill/content/common/autofill_messages.h" |
9 #include "components/autofill/content/renderer/autofill_agent.h" | 9 #include "components/autofill/content/renderer/autofill_agent.h" |
10 #include "components/autofill/content/renderer/form_autofill_util.h" | 10 #include "components/autofill/content/renderer/form_autofill_util.h" |
11 #include "components/autofill/content/renderer/password_autofill_agent.h" | 11 #include "components/autofill/content/renderer/password_autofill_agent.h" |
12 #include "components/autofill/content/renderer/test_password_autofill_agent.h" | 12 #include "components/autofill/content/renderer/test_password_autofill_agent.h" |
13 #include "components/autofill/core/common/autofill_constants.h" | 13 #include "components/autofill/core/common/autofill_constants.h" |
14 #include "components/autofill/core/common/autofill_switches.h" | 14 #include "components/autofill/core/common/autofill_switches.h" |
15 #include "components/autofill/core/common/form_data.h" | 15 #include "components/autofill/core/common/form_data.h" |
16 #include "components/autofill/core/common/form_field_data.h" | 16 #include "components/autofill/core/common/form_field_data.h" |
17 #include "content/public/renderer/render_frame.h" | 17 #include "content/public/renderer/render_frame.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/WebKit/public/platform/WebString.h" | 19 #include "third_party/WebKit/public/platform/WebString.h" |
20 #include "third_party/WebKit/public/platform/WebVector.h" | 20 #include "third_party/WebKit/public/platform/WebVector.h" |
21 #include "third_party/WebKit/public/web/WebDocument.h" | 21 #include "third_party/WebKit/public/web/WebDocument.h" |
22 #include "third_party/WebKit/public/web/WebElement.h" | 22 #include "third_party/WebKit/public/web/WebElement.h" |
| 23 #include "third_party/WebKit/public/web/WebFormControlElement.h" |
23 #include "third_party/WebKit/public/web/WebFormElement.h" | 24 #include "third_party/WebKit/public/web/WebFormElement.h" |
24 #include "third_party/WebKit/public/web/WebInputElement.h" | 25 #include "third_party/WebKit/public/web/WebInputElement.h" |
25 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 26 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
26 #include "third_party/WebKit/public/web/WebNode.h" | 27 #include "third_party/WebKit/public/web/WebNode.h" |
27 #include "third_party/WebKit/public/web/WebView.h" | 28 #include "third_party/WebKit/public/web/WebView.h" |
28 #include "ui/events/keycodes/keyboard_codes.h" | 29 #include "ui/events/keycodes/keyboard_codes.h" |
29 | 30 |
30 using autofill::PasswordForm; | 31 using autofill::PasswordForm; |
31 using base::ASCIIToUTF16; | 32 using base::ASCIIToUTF16; |
32 using base::UTF16ToUTF8; | 33 using base::UTF16ToUTF8; |
33 using blink::WebDocument; | 34 using blink::WebDocument; |
34 using blink::WebElement; | 35 using blink::WebElement; |
35 using blink::WebFrame; | 36 using blink::WebFrame; |
36 using blink::WebInputElement; | 37 using blink::WebInputElement; |
37 using blink::WebString; | 38 using blink::WebString; |
38 using blink::WebView; | 39 using blink::WebView; |
39 | 40 |
40 namespace { | 41 namespace { |
41 | 42 |
42 const int kPasswordFillFormDataId = 1234; | 43 const int kPasswordFillFormDataId = 1234; |
43 | 44 |
44 // The name of the username/password element in the form. | 45 // The name of the username/password element in the form. |
45 const char kUsernameName[] = "username"; | 46 const char kUsernameName[] = "username"; |
46 const char kPasswordName[] = "password"; | 47 const char kPasswordName[] = "password"; |
| 48 const char kEmailName[] = "email"; |
47 | 49 |
48 const char kAliceUsername[] = "alice"; | 50 const char kAliceUsername[] = "alice"; |
49 const char kAlicePassword[] = "password"; | 51 const char kAlicePassword[] = "password"; |
50 const char kBobUsername[] = "bob"; | 52 const char kBobUsername[] = "bob"; |
51 const char kBobPassword[] = "secret"; | 53 const char kBobPassword[] = "secret"; |
52 const char kCarolUsername[] = "Carol"; | 54 const char kCarolUsername[] = "Carol"; |
53 const char kCarolPassword[] = "test"; | 55 const char kCarolPassword[] = "test"; |
54 const char kCarolAlternateUsername[] = "RealCarolUsername"; | 56 const char kCarolAlternateUsername[] = "RealCarolUsername"; |
55 | 57 |
56 const char kFormHTML[] = | 58 const char kFormHTML[] = |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 " usernameOnchangeCalled = false;" | 155 " usernameOnchangeCalled = false;" |
154 " passwordOnchangeCalled = false;" | 156 " passwordOnchangeCalled = false;" |
155 " document.getElementById('username').onchange = function() {" | 157 " document.getElementById('username').onchange = function() {" |
156 " usernameOnchangeCalled = true;" | 158 " usernameOnchangeCalled = true;" |
157 " };" | 159 " };" |
158 " document.getElementById('password').onchange = function() {" | 160 " document.getElementById('password').onchange = function() {" |
159 " passwordOnchangeCalled = true;" | 161 " passwordOnchangeCalled = true;" |
160 " };" | 162 " };" |
161 "</script>"; | 163 "</script>"; |
162 | 164 |
| 165 const char kFormHTMLWithTwoTextFields[] = |
| 166 "<FORM name='LoginTestForm' id='LoginTestForm' " |
| 167 "action='http://www.bidule.com'>" |
| 168 " <INPUT type='text' id='username'/>" |
| 169 " <INPUT type='text' id='email'/>" |
| 170 " <INPUT type='password' id='password'/>" |
| 171 " <INPUT type='submit' value='Login'/>" |
| 172 "</FORM>"; |
| 173 |
163 // Sets the "readonly" attribute of |element| to the value given by |read_only|. | 174 // Sets the "readonly" attribute of |element| to the value given by |read_only|. |
164 void SetElementReadOnly(WebInputElement& element, bool read_only) { | 175 void SetElementReadOnly(WebInputElement& element, bool read_only) { |
165 element.setAttribute(WebString::fromUTF8("readonly"), | 176 element.setAttribute(WebString::fromUTF8("readonly"), |
166 read_only ? WebString::fromUTF8("true") : WebString()); | 177 read_only ? WebString::fromUTF8("true") : WebString()); |
167 } | 178 } |
168 | 179 |
169 } // namespace | 180 } // namespace |
170 | 181 |
171 namespace autofill { | 182 namespace autofill { |
172 | 183 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 WebDocument document = GetMainFrame()->document(); | 271 WebDocument document = GetMainFrame()->document(); |
261 WebElement element = | 272 WebElement element = |
262 document.getElementById(WebString::fromUTF8(kUsernameName)); | 273 document.getElementById(WebString::fromUTF8(kUsernameName)); |
263 ASSERT_FALSE(element.isNull()); | 274 ASSERT_FALSE(element.isNull()); |
264 username_element_ = element.to<blink::WebInputElement>(); | 275 username_element_ = element.to<blink::WebInputElement>(); |
265 element = document.getElementById(WebString::fromUTF8(kPasswordName)); | 276 element = document.getElementById(WebString::fromUTF8(kPasswordName)); |
266 ASSERT_FALSE(element.isNull()); | 277 ASSERT_FALSE(element.isNull()); |
267 password_element_ = element.to<blink::WebInputElement>(); | 278 password_element_ = element.to<blink::WebInputElement>(); |
268 } | 279 } |
269 | 280 |
| 281 blink::WebInputElement GetInputElementByID(const std::string& id) { |
| 282 WebDocument document = GetMainFrame()->document(); |
| 283 WebElement element = |
| 284 document.getElementById(WebString::fromUTF8(id.c_str())); |
| 285 return element.to<blink::WebInputElement>(); |
| 286 } |
| 287 |
270 void ClearUsernameAndPasswordFields() { | 288 void ClearUsernameAndPasswordFields() { |
271 username_element_.setValue(""); | 289 username_element_.setValue(""); |
272 username_element_.setAutofilled(false); | 290 username_element_.setAutofilled(false); |
273 password_element_.setValue(""); | 291 password_element_.setValue(""); |
274 password_element_.setAutofilled(false); | 292 password_element_.setAutofilled(false); |
275 } | 293 } |
276 | 294 |
277 void SimulateDidEndEditing(WebFrame* input_frame, WebInputElement& input) { | 295 void SimulateDidEndEditing(WebFrame* input_frame, WebInputElement& input) { |
278 static_cast<blink::WebAutofillClient*>(autofill_agent_) | 296 static_cast<blink::WebAutofillClient*>(autofill_agent_) |
279 ->textFieldDidEndEditing(input); | 297 ->textFieldDidEndEditing(input); |
(...skipping 1567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1847 autofill::switches::kEnableFillOnAccountSelect); | 1865 autofill::switches::kEnableFillOnAccountSelect); |
1848 | 1866 |
1849 ClearUsernameAndPasswordFields(); | 1867 ClearUsernameAndPasswordFields(); |
1850 | 1868 |
1851 username_element_.setValue("foobar"); | 1869 username_element_.setValue("foobar"); |
1852 SetElementReadOnly(username_element_, true); | 1870 SetElementReadOnly(username_element_, true); |
1853 | 1871 |
1854 CheckTextFieldsState(std::string("foobar"), false, std::string(), false); | 1872 CheckTextFieldsState(std::string("foobar"), false, std::string(), false); |
1855 } | 1873 } |
1856 | 1874 |
| 1875 // Test that the last plain text field before a password field is chosen as a |
| 1876 // username, in a form with 2 plain text fields without username predictions. |
| 1877 TEST_F(PasswordAutofillAgentTest, FindingUsernameWithoutAutofillPredictions) { |
| 1878 LoadHTML(kFormHTMLWithTwoTextFields); |
| 1879 UpdateUsernameAndPasswordElements(); |
| 1880 blink::WebInputElement email_element = GetInputElementByID(kEmailName); |
| 1881 SimulateInputChangeForElement("temp", true, GetMainFrame(), username_element_, |
| 1882 true); |
| 1883 SimulateInputChangeForElement("temp@google.com", true, GetMainFrame(), |
| 1884 email_element, true); |
| 1885 SimulateInputChangeForElement("random", true, GetMainFrame(), |
| 1886 password_element_, true); |
| 1887 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) |
| 1888 ->WillSendSubmitEvent(username_element_.form()); |
| 1889 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) |
| 1890 ->WillSubmitForm(username_element_.form()); |
| 1891 |
| 1892 // Observe that the PasswordAutofillAgent identifies the second field (e-mail) |
| 1893 // as username. |
| 1894 ExpectFormSubmittedWithUsernameAndPasswords("temp@google.com", "random", ""); |
| 1895 } |
| 1896 |
| 1897 // Tests that username predictions are followed when identifying the username |
| 1898 // in a password form with two plain text fields. |
| 1899 TEST_F(PasswordAutofillAgentTest, FindingUsernameWithAutofillPredictions) { |
| 1900 LoadHTML(kFormHTMLWithTwoTextFields); |
| 1901 UpdateUsernameAndPasswordElements(); |
| 1902 blink::WebInputElement email_element = GetInputElementByID(kEmailName); |
| 1903 SimulateInputChangeForElement("temp", true, GetMainFrame(), username_element_, |
| 1904 true); |
| 1905 SimulateInputChangeForElement("temp@google.com", true, GetMainFrame(), |
| 1906 email_element, true); |
| 1907 SimulateInputChangeForElement("random", true, GetMainFrame(), |
| 1908 password_element_, true); |
| 1909 |
| 1910 // Find FormData for visible password form. |
| 1911 blink::WebFormElement form_element = username_element_.form(); |
| 1912 FormData form_data; |
| 1913 ASSERT_TRUE(WebFormElementToFormData( |
| 1914 form_element, blink::WebFormControlElement(), REQUIRE_NONE, EXTRACT_NONE, |
| 1915 &form_data, nullptr)); |
| 1916 // Simulate Autofill predictions: the first field is username. |
| 1917 std::map<autofill::FormData, autofill::FormFieldData> predictions; |
| 1918 predictions[form_data] = form_data.fields[0]; |
| 1919 AutofillMsg_AutofillUsernameDataReceived msg(0, predictions); |
| 1920 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) |
| 1921 ->OnMessageReceived(msg); |
| 1922 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) |
| 1923 ->WillSendSubmitEvent(username_element_.form()); |
| 1924 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) |
| 1925 ->WillSubmitForm(username_element_.form()); |
| 1926 |
| 1927 // Observe that the PasswordAutofillAgent identifies the first field as |
| 1928 // username. |
| 1929 ExpectFormSubmittedWithUsernameAndPasswords("temp", "random", ""); |
| 1930 } |
| 1931 |
1857 } // namespace autofill | 1932 } // namespace autofill |
OLD | NEW |