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/string_util.h" | 5 #include "base/string_util.h" |
6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
7 #include "chrome/common/autofill_messages.h" | 7 #include "chrome/common/autofill_messages.h" |
8 #include "chrome/renderer/autofill/autofill_agent.h" | 8 #include "chrome/renderer/autofill/autofill_agent.h" |
9 #include "chrome/renderer/autofill/password_autofill_manager.h" | 9 #include "chrome/renderer/autofill/password_autofill_manager.h" |
10 #include "chrome/test/base/chrome_render_view_test.h" | 10 #include "chrome/test/base/chrome_render_view_test.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 ChromeRenderViewTest::SetUp(); | 73 ChromeRenderViewTest::SetUp(); |
74 | 74 |
75 // Add a preferred login and an additional login to the FillData. | 75 // Add a preferred login and an additional login to the FillData. |
76 username1_ = ASCIIToUTF16(kAliceUsername); | 76 username1_ = ASCIIToUTF16(kAliceUsername); |
77 password1_ = ASCIIToUTF16(kAlicePassword); | 77 password1_ = ASCIIToUTF16(kAlicePassword); |
78 username2_ = ASCIIToUTF16(kBobUsername); | 78 username2_ = ASCIIToUTF16(kBobUsername); |
79 password2_ = ASCIIToUTF16(kBobPassword); | 79 password2_ = ASCIIToUTF16(kBobPassword); |
80 username3_ = ASCIIToUTF16(kCarolUsername); | 80 username3_ = ASCIIToUTF16(kCarolUsername); |
81 password3_ = ASCIIToUTF16(kCarolPassword); | 81 password3_ = ASCIIToUTF16(kCarolPassword); |
82 | 82 |
83 FormField password_field; | |
84 password_field.name = ASCIIToUTF16(kPasswordName); | |
85 password_field.value = password1_; | |
86 fill_data_.basic_data.fields.push_back(password_field); | |
87 fill_data_only_password_.basic_data.fields.push_back(password_field); | |
88 | |
83 FormField username_field; | 89 FormField username_field; |
84 username_field.name = ASCIIToUTF16(kUsernameName); | 90 username_field.name = ASCIIToUTF16(kUsernameName); |
85 username_field.value = username1_; | 91 username_field.value = username1_; |
86 fill_data_.basic_data.fields.push_back(username_field); | 92 fill_data_.basic_data.fields.push_back(username_field); |
87 | 93 |
88 FormField password_field; | |
89 password_field.name = ASCIIToUTF16(kPasswordName); | |
90 password_field.value = password1_; | |
91 fill_data_.basic_data.fields.push_back(password_field); | |
92 | |
93 fill_data_.additional_logins[username2_] = password2_; | 94 fill_data_.additional_logins[username2_] = password2_; |
94 fill_data_.additional_logins[username3_] = password3_; | 95 fill_data_.additional_logins[username3_] = password3_; |
95 | 96 |
96 // We need to set the origin so it matches the frame URL and the action so | 97 // We need to set the origin so it matches the frame URL and the action so |
97 // it matches the form action, otherwise we won't autocomplete. | 98 // it matches the form action, otherwise we won't autocomplete. |
98 std::string origin("data:text/html;charset=utf-8,"); | 99 std::string origin("data:text/html;charset=utf-8,"); |
99 origin += kFormHTML; | 100 origin += kFormHTML; |
100 fill_data_.basic_data.origin = GURL(origin); | 101 fill_data_.basic_data.origin = GURL(origin); |
101 fill_data_.basic_data.action = GURL("http://www.bidule.com"); | 102 fill_data_.basic_data.action = GURL("http://www.bidule.com"); |
103 fill_data_only_password_.basic_data.origin = GURL(origin); | |
104 fill_data_only_password_.basic_data.action = GURL("http://www.bidule.com"); | |
102 | 105 |
103 LoadHTML(kFormHTML); | 106 LoadHTML(kFormHTML); |
104 | 107 |
105 // Now retrieves the input elements so the test can access them. | 108 // Now retrieves the input elements so the test can access them. |
106 WebDocument document = GetMainFrame()->document(); | 109 WebDocument document = GetMainFrame()->document(); |
107 WebElement element = | 110 WebElement element = |
108 document.getElementById(WebString::fromUTF8(kUsernameName)); | 111 document.getElementById(WebString::fromUTF8(kUsernameName)); |
109 ASSERT_FALSE(element.isNull()); | 112 ASSERT_FALSE(element.isNull()); |
110 username_element_ = element.to<WebKit::WebInputElement>(); | 113 username_element_ = element.to<WebKit::WebInputElement>(); |
111 element = document.getElementById(WebString::fromUTF8(kPasswordName)); | 114 element = document.getElementById(WebString::fromUTF8(kPasswordName)); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 const std::string& password, | 146 const std::string& password, |
144 bool password_autofilled) { | 147 bool password_autofilled) { |
145 EXPECT_EQ(username, | 148 EXPECT_EQ(username, |
146 static_cast<std::string>(username_element_.value().utf8())); | 149 static_cast<std::string>(username_element_.value().utf8())); |
147 EXPECT_EQ(username_autofilled, username_element_.isAutofilled()); | 150 EXPECT_EQ(username_autofilled, username_element_.isAutofilled()); |
148 EXPECT_EQ(password, | 151 EXPECT_EQ(password, |
149 static_cast<std::string>(password_element_.value().utf8())); | 152 static_cast<std::string>(password_element_.value().utf8())); |
150 EXPECT_EQ(password_autofilled, password_element_.isAutofilled()); | 153 EXPECT_EQ(password_autofilled, password_element_.isAutofilled()); |
151 } | 154 } |
152 | 155 |
156 void CheckPasswordTextFieldState(const std::string& password, | |
157 bool password_autofilled) { | |
158 EXPECT_EQ(password, | |
159 static_cast<std::string>(password_element_.value().utf8())); | |
160 EXPECT_EQ(password_autofilled, password_element_.isAutofilled()); | |
161 } | |
Ilya Sherman
2012/04/19 00:27:37
nit: Since this function is used only in a single
| |
162 | |
153 void CheckUsernameSelection(int start, int end) { | 163 void CheckUsernameSelection(int start, int end) { |
154 EXPECT_EQ(start, username_element_.selectionStart()); | 164 EXPECT_EQ(start, username_element_.selectionStart()); |
155 EXPECT_EQ(end, username_element_.selectionEnd()); | 165 EXPECT_EQ(end, username_element_.selectionEnd()); |
156 } | 166 } |
157 | 167 |
158 string16 username1_; | 168 string16 username1_; |
159 string16 username2_; | 169 string16 username2_; |
160 string16 username3_; | 170 string16 username3_; |
161 string16 password1_; | 171 string16 password1_; |
162 string16 password2_; | 172 string16 password2_; |
163 string16 password3_; | 173 string16 password3_; |
164 PasswordFormFillData fill_data_; | 174 PasswordFormFillData fill_data_; |
175 PasswordFormFillData fill_data_only_password_; | |
Ilya Sherman
2012/04/19 00:27:37
nit: Since this is used only in a single test, ple
| |
165 | 176 |
166 WebInputElement username_element_; | 177 WebInputElement username_element_; |
167 WebInputElement password_element_; | 178 WebInputElement password_element_; |
168 | 179 |
169 private: | 180 private: |
170 DISALLOW_COPY_AND_ASSIGN(PasswordAutofillManagerTest); | 181 DISALLOW_COPY_AND_ASSIGN(PasswordAutofillManagerTest); |
171 }; | 182 }; |
172 | 183 |
173 // Tests that the password login is autocompleted as expected when the browser | 184 // Tests that the password login is autocompleted as expected when the browser |
174 // sends back the password info. | 185 // sends back the password info. |
(...skipping 21 matching lines...) Expand all Loading... | |
196 */ | 207 */ |
197 | 208 |
198 // Simulate the browser sending back the login info, it triggers the | 209 // Simulate the browser sending back the login info, it triggers the |
199 // autocomplete. | 210 // autocomplete. |
200 SimulateOnFillPasswordForm(fill_data_); | 211 SimulateOnFillPasswordForm(fill_data_); |
201 | 212 |
202 // The username and password should have been autocompleted. | 213 // The username and password should have been autocompleted. |
203 CheckTextFieldsState(kAliceUsername, true, kAlicePassword, true); | 214 CheckTextFieldsState(kAliceUsername, true, kAlicePassword, true); |
204 } | 215 } |
205 | 216 |
217 // Tests that the password login is autocompleted as expected when the browser | |
218 // sends back the password info if there is only a password field. | |
219 TEST_F(PasswordAutofillManagerTest, InitialAutocompleteOnlyPassword) { | |
220 // Simulate the browser sending back the login info, it triggers the | |
221 // autocomplete. | |
222 SimulateOnFillPasswordForm(fill_data_only_password_); | |
223 | |
224 // Only the password should have been autocompleted. | |
225 CheckPasswordTextFieldState(kAlicePassword, true); | |
226 } | |
227 | |
206 // Tests that we correctly fill forms having an empty 'action' attribute. | 228 // Tests that we correctly fill forms having an empty 'action' attribute. |
207 TEST_F(PasswordAutofillManagerTest, InitialAutocompleteForEmptyAction) { | 229 TEST_F(PasswordAutofillManagerTest, InitialAutocompleteForEmptyAction) { |
208 const char kEmptyActionFormHTML[] = | 230 const char kEmptyActionFormHTML[] = |
209 "<FORM name='LoginTestForm'>" | 231 "<FORM name='LoginTestForm'>" |
210 " <INPUT type='text' id='username'/>" | 232 " <INPUT type='text' id='username'/>" |
211 " <INPUT type='password' id='password'/>" | 233 " <INPUT type='password' id='password'/>" |
212 " <INPUT type='submit' value='Login'/>" | 234 " <INPUT type='submit' value='Login'/>" |
213 "</FORM>"; | 235 "</FORM>"; |
214 LoadHTML(kEmptyActionFormHTML); | 236 LoadHTML(kEmptyActionFormHTML); |
215 | 237 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
413 // didSelectAutofillSuggestion on the renderer. | 435 // didSelectAutofillSuggestion on the renderer. |
414 autofill_agent_->didSelectAutofillSuggestion(username_element_, | 436 autofill_agent_->didSelectAutofillSuggestion(username_element_, |
415 ASCIIToUTF16(kAliceUsername), | 437 ASCIIToUTF16(kAliceUsername), |
416 WebKit::WebString(), | 438 WebKit::WebString(), |
417 0); | 439 0); |
418 // Autocomplete should not have kicked in. | 440 // Autocomplete should not have kicked in. |
419 CheckTextFieldsState("", false, "", false); | 441 CheckTextFieldsState("", false, "", false); |
420 } | 442 } |
421 | 443 |
422 } // namespace autofill | 444 } // namespace autofill |
OLD | NEW |