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/WebFormElement.h" | 23 #include "third_party/WebKit/public/web/WebFormElement.h" |
24 #include "third_party/WebKit/public/web/WebFormControlElement.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 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.
| |
282 WebDocument document = GetMainFrame()->document(); | |
283 WebElement element = document.getElementById(WebString::fromUTF8(id)); | |
284 return element.to<blink::WebInputElement>(); | |
285 } | |
286 | |
270 void ClearUsernameAndPasswordFields() { | 287 void ClearUsernameAndPasswordFields() { |
271 username_element_.setValue(""); | 288 username_element_.setValue(""); |
272 username_element_.setAutofilled(false); | 289 username_element_.setAutofilled(false); |
273 password_element_.setValue(""); | 290 password_element_.setValue(""); |
274 password_element_.setAutofilled(false); | 291 password_element_.setAutofilled(false); |
275 } | 292 } |
276 | 293 |
277 void SimulateDidEndEditing(WebFrame* input_frame, WebInputElement& input) { | 294 void SimulateDidEndEditing(WebFrame* input_frame, WebInputElement& input) { |
278 static_cast<blink::WebAutofillClient*>(autofill_agent_) | 295 static_cast<blink::WebAutofillClient*>(autofill_agent_) |
279 ->textFieldDidEndEditing(input); | 296 ->textFieldDidEndEditing(input); |
(...skipping 1567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1847 autofill::switches::kEnableFillOnAccountSelect); | 1864 autofill::switches::kEnableFillOnAccountSelect); |
1848 | 1865 |
1849 ClearUsernameAndPasswordFields(); | 1866 ClearUsernameAndPasswordFields(); |
1850 | 1867 |
1851 username_element_.setValue("foobar"); | 1868 username_element_.setValue("foobar"); |
1852 SetElementReadOnly(username_element_, true); | 1869 SetElementReadOnly(username_element_, true); |
1853 | 1870 |
1854 CheckTextFieldsState(std::string("foobar"), false, std::string(), false); | 1871 CheckTextFieldsState(std::string("foobar"), false, std::string(), false); |
1855 } | 1872 } |
1856 | 1873 |
1874 // Test that the last plain text field before a password field is chosen as a | |
1875 // username, in a form with 2 plain text fields without username predictions. | |
1876 TEST_F(PasswordAutofillAgentTest, FindingUsernameWithoutAutofillPredictions) { | |
1877 LoadHTML(kFormHTMLWithTwoTextFields); | |
1878 UpdateUsernameAndPasswordElements(); | |
1879 blink::WebInputElement email_element = GetInputElementByID(kEmailName); | |
1880 SimulateInputChangeForElement("temp", true, GetMainFrame(), username_element_, | |
1881 true); | |
1882 SimulateInputChangeForElement("temp@google.com", true, GetMainFrame(), | |
1883 email_element, true); | |
1884 SimulateInputChangeForElement("random", true, GetMainFrame(), | |
1885 password_element_, true); | |
1886 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) | |
1887 ->WillSubmitForm(username_element_.form()); | |
1888 | |
1889 // Observe that the PasswordAutofillAgent identifies the second field (e-mail) | |
1890 // as username. | |
1891 ExpectFormSubmittedWithUsernameAndPasswords("temp@google.com", "random", ""); | |
1892 } | |
1893 | |
1894 // Tests that username predictions are followed when identifying the username | |
1895 // in a password form with two plain text fields. | |
1896 TEST_F(PasswordAutofillAgentTest, FindingUsernameWithAutofillPredictions) { | |
1897 LoadHTML(kFormHTMLWithTwoTextFields); | |
1898 UpdateUsernameAndPasswordElements(); | |
1899 blink::WebInputElement email_element = GetInputElementByID(kEmailName); | |
1900 SimulateInputChangeForElement("temp", true, GetMainFrame(), username_element_, | |
1901 true); | |
1902 SimulateInputChangeForElement("temp@google.com", true, GetMainFrame(), | |
1903 email_element, true); | |
1904 SimulateInputChangeForElement("random", true, GetMainFrame(), | |
1905 password_element_, true); | |
1906 | |
1907 // Find FormData for visible password form. | |
1908 blink::WebFormElement form_element = username_element_.form(); | |
1909 FormData form_data; | |
1910 ASSERT_TRUE(WebFormElementToFormData( | |
1911 form_element, blink::WebFormControlElement(), REQUIRE_NONE, EXTRACT_NONE, | |
1912 &form_data, nullptr)); | |
1913 // Simulate Autofill predictions: the first field is username. | |
1914 std::map<autofill::FormData, autofill::FormFieldData> predictions; | |
1915 predictions[form_data] = form_data.fields[0]; | |
1916 AutofillMsg_AutofillUsernameDataReceived msg(0, predictions); | |
1917 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) | |
1918 ->OnMessageReceived(msg); | |
1919 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) | |
1920 ->WillSubmitForm(username_element_.form()); | |
1921 | |
1922 // Observe that the PasswordAutofillAgent identifies the first field as | |
1923 // username. | |
1924 ExpectFormSubmittedWithUsernameAndPasswords("temp", "random", ""); | |
1925 } | |
1926 | |
1857 } // namespace autofill | 1927 } // namespace autofill |
OLD | NEW |