Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1136)

Side by Side Diff: chrome/renderer/autofill/password_autofill_agent_browsertest.cc

Issue 2902113004: Autofill username when the user interacts with the password field. (Closed)
Patch Set: Address comments from kolos@. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | components/autofill/content/renderer/password_autofill_agent.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "components/autofill/content/renderer/password_autofill_agent.h" 5 #include "components/autofill/content/renderer/password_autofill_agent.h"
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 } 380 }
381 381
382 blink::WebInputElement GetInputElementByID(const std::string& id) { 382 blink::WebInputElement GetInputElementByID(const std::string& id) {
383 WebDocument document = GetMainFrame()->GetDocument(); 383 WebDocument document = GetMainFrame()->GetDocument();
384 WebElement element = 384 WebElement element =
385 document.GetElementById(WebString::FromUTF8(id.c_str())); 385 document.GetElementById(WebString::FromUTF8(id.c_str()));
386 return element.To<blink::WebInputElement>(); 386 return element.To<blink::WebInputElement>();
387 } 387 }
388 388
389 void ClearUsernameAndPasswordFields() { 389 void ClearUsernameAndPasswordFields() {
390 username_element_.SetValue(""); 390 username_element_.SetValue(WebString());
391 username_element_.SetSuggestedValue(WebString());
391 username_element_.SetAutofilled(false); 392 username_element_.SetAutofilled(false);
392 password_element_.SetValue(""); 393 password_element_.SetValue(WebString());
394 password_element_.SetSuggestedValue(WebString());
393 password_element_.SetAutofilled(false); 395 password_element_.SetAutofilled(false);
394 } 396 }
395 397
396 void SimulateSuggestionChoice(WebInputElement& username_input) { 398 void SimulateSuggestionChoice(WebInputElement& username_input) {
397 base::string16 username(base::ASCIIToUTF16(kAliceUsername)); 399 base::string16 username(base::ASCIIToUTF16(kAliceUsername));
398 base::string16 password(base::ASCIIToUTF16(kAlicePassword)); 400 base::string16 password(base::ASCIIToUTF16(kAlicePassword));
399 SimulateSuggestionChoiceOfUsernameAndPassword(username_input, username, 401 SimulateSuggestionChoiceOfUsernameAndPassword(username_input, username,
400 password); 402 password);
401 } 403 }
402 404
(...skipping 16 matching lines...) Expand all
419 void SimulatePasswordChange(const std::string& password) { 421 void SimulatePasswordChange(const std::string& password) {
420 SimulateUserInputChangeForElement(&password_element_, password); 422 SimulateUserInputChangeForElement(&password_element_, password);
421 } 423 }
422 424
423 void CheckTextFieldsStateForElements(const WebInputElement& username_element, 425 void CheckTextFieldsStateForElements(const WebInputElement& username_element,
424 const std::string& username, 426 const std::string& username,
425 bool username_autofilled, 427 bool username_autofilled,
426 const WebInputElement& password_element, 428 const WebInputElement& password_element,
427 const std::string& password, 429 const std::string& password,
428 bool password_autofilled, 430 bool password_autofilled,
429 bool checkSuggestedValue) { 431 bool check_suggested_username,
430 EXPECT_EQ(username, username_element.Value().Utf8()); 432 bool check_suggested_password) {
433 EXPECT_EQ(username, check_suggested_username
434 ? username_element.SuggestedValue().Utf8()
435 : username_element.Value().Utf8())
436 << "check_suggested_username == " << check_suggested_username;
431 EXPECT_EQ(username_autofilled, username_element.IsAutofilled()); 437 EXPECT_EQ(username_autofilled, username_element.IsAutofilled());
432 EXPECT_EQ(password, checkSuggestedValue 438
439 EXPECT_EQ(password, check_suggested_password
433 ? password_element.SuggestedValue().Utf8() 440 ? password_element.SuggestedValue().Utf8()
434 : password_element.Value().Utf8()) 441 : password_element.Value().Utf8())
435 << "checkSuggestedValue == " << checkSuggestedValue; 442 << "check_suggested_password == " << check_suggested_password;
436 EXPECT_EQ(password_autofilled, password_element.IsAutofilled()); 443 EXPECT_EQ(password_autofilled, password_element.IsAutofilled());
437 } 444 }
438 445
439 // Checks the DOM-accessible value of the username element and the 446 // Checks the DOM-accessible value of the username element and the
440 // *suggested* value of the password element. 447 // *suggested* value of the password element.
441 void CheckTextFieldsState(const std::string& username, 448 void CheckTextFieldsState(const std::string& username,
442 bool username_autofilled, 449 bool username_autofilled,
443 const std::string& password, 450 const std::string& password,
444 bool password_autofilled) { 451 bool password_autofilled) {
445 CheckTextFieldsStateForElements(username_element_, 452 CheckTextFieldsStateForElements(
446 username, 453 username_element_, username, username_autofilled, password_element_,
447 username_autofilled, 454 password, password_autofilled, false /* check_suggested_username */,
448 password_element_, 455 true /* check_suggested_password */);
449 password,
450 password_autofilled,
451 true);
452 } 456 }
453 457
454 // Checks the DOM-accessible value of the username element and the 458 // Checks the DOM-accessible value of the username element and the
455 // DOM-accessible value of the password element. 459 // DOM-accessible value of the password element.
456 void CheckTextFieldsDOMState(const std::string& username, 460 void CheckTextFieldsDOMState(const std::string& username,
457 bool username_autofilled, 461 bool username_autofilled,
458 const std::string& password, 462 const std::string& password,
459 bool password_autofilled) { 463 bool password_autofilled) {
460 CheckTextFieldsStateForElements(username_element_, 464 CheckTextFieldsStateForElements(
461 username, 465 username_element_, username, username_autofilled, password_element_,
462 username_autofilled, 466 password, password_autofilled, false /* check_suggested_username */,
463 password_element_, 467 false /* check_suggested_password */);
464 password, 468 }
465 password_autofilled, 469
466 false); 470 // Checks the suggested values of the |username| and |password| elements.
471 void CheckTextFieldsSuggestedState(const std::string& username,
472 bool username_autofilled,
473 const std::string& password,
474 bool password_autofilled) {
475 CheckTextFieldsStateForElements(
476 username_element_, username, username_autofilled, password_element_,
477 password, password_autofilled, true /* check_suggested_username */,
478 true /* check_suggested_password */);
479 }
480
481 void ResetFieldState(WebInputElement* element,
482 const std::string& value = std::string(),
483 bool is_autofilled = false) {
484 element->SetValue(WebString::FromUTF8(value));
485 element->SetSuggestedValue(WebString());
486 element->SetAutofilled(is_autofilled);
487 element->SetSelectionRange(value.size(), value.size());
467 } 488 }
468 489
469 void CheckUsernameSelection(int start, int end) { 490 void CheckUsernameSelection(int start, int end) {
470 EXPECT_EQ(start, username_element_.SelectionStart()); 491 EXPECT_EQ(start, username_element_.SelectionStart());
471 EXPECT_EQ(end, username_element_.SelectionEnd()); 492 EXPECT_EQ(end, username_element_.SelectionEnd());
472 } 493 }
473 494
474 // Checks the message sent to PasswordAutofillManager to build the suggestion 495 // Checks the message sent to PasswordAutofillManager to build the suggestion
475 // list. |username| is the expected username field value, and |show_all| is 496 // list. |username| is the expected username field value, and |show_all| is
476 // the expected flag for the PasswordAutofillManager, whether to show all 497 // the expected flag for the PasswordAutofillManager, whether to show all
(...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after
1121 CheckIfEventsAreCalled(event_checkers, true); 1142 CheckIfEventsAreCalled(event_checkers, true);
1122 } 1143 }
1123 1144
1124 // Tests that |FillSuggestion| properly fills the username and password. 1145 // Tests that |FillSuggestion| properly fills the username and password.
1125 TEST_F(PasswordAutofillAgentTest, FillSuggestion) { 1146 TEST_F(PasswordAutofillAgentTest, FillSuggestion) {
1126 // Simulate the browser sending the login info, but set |wait_for_username| 1147 // Simulate the browser sending the login info, but set |wait_for_username|
1127 // to prevent the form from being immediately filled. 1148 // to prevent the form from being immediately filled.
1128 fill_data_.wait_for_username = true; 1149 fill_data_.wait_for_username = true;
1129 SimulateOnFillPasswordForm(fill_data_); 1150 SimulateOnFillPasswordForm(fill_data_);
1130 1151
1131 // Neither field should have been autocompleted. 1152 for (const auto& selected_element : {username_element_, password_element_}) {
1132 CheckTextFieldsDOMState(std::string(), false, std::string(), false); 1153 // Neither field should be autocompleted.
1154 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1133 1155
1134 // If the password field is not autocompletable, it should not be affected. 1156 // If the password field is not autocompletable, it should not be affected.
1135 SetElementReadOnly(password_element_, true); 1157 SetElementReadOnly(password_element_, true);
1136 EXPECT_FALSE(password_autofill_agent_->FillSuggestion( 1158 EXPECT_FALSE(password_autofill_agent_->FillSuggestion(
1137 username_element_, ASCIIToUTF16(kAliceUsername), 1159 selected_element, ASCIIToUTF16(kAliceUsername),
1138 ASCIIToUTF16(kAlicePassword))); 1160 ASCIIToUTF16(kAlicePassword)));
1139 CheckTextFieldsDOMState(std::string(), false, std::string(), false); 1161 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1140 SetElementReadOnly(password_element_, false); 1162 SetElementReadOnly(password_element_, false);
1141 1163
1142 // After filling with the suggestion, both fields should be autocompleted. 1164 // After filling with the suggestion, both fields should be autocompleted.
1143 EXPECT_TRUE(password_autofill_agent_->FillSuggestion( 1165 EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
1144 username_element_, ASCIIToUTF16(kAliceUsername), 1166 selected_element, ASCIIToUTF16(kAliceUsername),
1145 ASCIIToUTF16(kAlicePassword))); 1167 ASCIIToUTF16(kAlicePassword)));
1146 CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true); 1168 CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
1147 int username_length = strlen(kAliceUsername); 1169 int username_length = strlen(kAliceUsername);
1148 CheckUsernameSelection(username_length, username_length); 1170 CheckUsernameSelection(username_length, username_length);
1149 1171
1150 // Try Filling with a suggestion with password different from the one that was 1172 // Try Filling with a suggestion with password different from the one that
1151 // initially sent to the renderer. 1173 // was initially sent to the renderer.
1152 EXPECT_TRUE(password_autofill_agent_->FillSuggestion( 1174 EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
1153 username_element_, ASCIIToUTF16(kBobUsername), 1175 selected_element, ASCIIToUTF16(kBobUsername),
1154 ASCIIToUTF16(kCarolPassword))); 1176 ASCIIToUTF16(kCarolPassword)));
1155 CheckTextFieldsDOMState(kBobUsername, true, kCarolPassword, true); 1177 CheckTextFieldsDOMState(kBobUsername, true, kCarolPassword, true);
1156 username_length = strlen(kBobUsername); 1178 username_length = strlen(kBobUsername);
1157 CheckUsernameSelection(username_length, username_length); 1179 CheckUsernameSelection(username_length, username_length);
1180
1181 ClearUsernameAndPasswordFields();
1182 }
1158 } 1183 }
1159 1184
1160 // Tests that |FillSuggestion| properly fills the password if username is 1185 // Tests that |FillSuggestion| doesn't change non-empty non-autofilled username
1161 // read-only. 1186 // when interacting with the password field.
1187 TEST_F(PasswordAutofillAgentTest,
1188 FillSuggestionFromPasswordFieldWithUsernameManuallyFilled) {
1189 username_element_.SetValue(WebString::FromUTF8("user1"));
1190
1191 // Simulate the browser sending the login info, but set |wait_for_username| to
1192 // prevent the form from being immediately filled.
1193 fill_data_.wait_for_username = true;
1194 SimulateOnFillPasswordForm(fill_data_);
1195 // Neither field should have been autocompleted.
1196 CheckTextFieldsDOMState("user1", false, std::string(), false);
1197
1198 // Only password field should be autocompleted.
1199 EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
1200 password_element_, ASCIIToUTF16(kAliceUsername),
1201 ASCIIToUTF16(kAlicePassword)));
1202 CheckTextFieldsDOMState("user1", false, kAlicePassword, true);
1203
1204 // Try Filling with a different password. Only password should be changed.
kolos1 2017/05/29 09:17:11 Thanks for adding a subtest for this.
pkalinnikov 2017/05/29 09:20:45 Welcome :)
1205 EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
1206 password_element_, ASCIIToUTF16(kBobUsername),
1207 ASCIIToUTF16(kCarolPassword)));
1208 CheckTextFieldsDOMState("user1", false, kCarolPassword, true);
1209 }
1210
1211 // Tests that |FillSuggestion| properly fills the password if the username field
1212 // is read-only.
1162 TEST_F(PasswordAutofillAgentTest, FillSuggestionIfUsernameReadOnly) { 1213 TEST_F(PasswordAutofillAgentTest, FillSuggestionIfUsernameReadOnly) {
1163 // Simulate the browser sending the login info. 1214 // Simulate the browser sending the login info.
1164 SetElementReadOnly(username_element_, true); 1215 SetElementReadOnly(username_element_, true);
1165 SimulateOnFillPasswordForm(fill_data_); 1216 SimulateOnFillPasswordForm(fill_data_);
1166 1217
1167 // Neither field should have been autocompleted. 1218 for (const auto& selected_element : {username_element_, password_element_}) {
1168 CheckTextFieldsDOMState(std::string(), false, std::string(), false); 1219 // Neither field should be autocompleted.
1220 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1169 1221
1170 // Username field is not autocompletable, it should not be affected. 1222 // Username field is not autocompletable, it should not be affected.
1171 EXPECT_TRUE(password_autofill_agent_->FillSuggestion( 1223 EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
1172 password_element_, ASCIIToUTF16(kAliceUsername), 1224 selected_element, ASCIIToUTF16(kAliceUsername),
1173 ASCIIToUTF16(kAlicePassword))); 1225 ASCIIToUTF16(kAlicePassword)));
1174 CheckTextFieldsDOMState(std::string(), false, kAlicePassword, true); 1226 CheckTextFieldsDOMState(std::string(), false, kAlicePassword, true);
1175 1227
1176 // Try Filling with a suggestion with password different from the one that was 1228 // Try Filling with a suggestion with password different from the one that
1177 // initially sent to the renderer. 1229 // was initially sent to the renderer.
1178 EXPECT_TRUE(password_autofill_agent_->FillSuggestion( 1230 EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
1179 password_element_, ASCIIToUTF16(kBobUsername), 1231 selected_element, ASCIIToUTF16(kBobUsername),
1180 ASCIIToUTF16(kCarolPassword))); 1232 ASCIIToUTF16(kCarolPassword)));
1181 CheckTextFieldsDOMState(std::string(), false, kCarolPassword, true); 1233 CheckTextFieldsDOMState(std::string(), false, kCarolPassword, true);
1234
1235 ClearUsernameAndPasswordFields();
1236 }
1182 } 1237 }
1183 1238
1184 // Tests that |PreviewSuggestion| properly previews the username and password. 1239 // Tests that |PreviewSuggestion| properly previews the username and password.
1185 TEST_F(PasswordAutofillAgentTest, PreviewSuggestion) { 1240 TEST_F(PasswordAutofillAgentTest, PreviewSuggestion) {
1186 // Simulate the browser sending the login info, but set |wait_for_username| 1241 // Simulate the browser sending the login info, but set |wait_for_username| to
1187 // to prevent the form from being immediately filled. 1242 // prevent the form from being immediately filled.
1188 fill_data_.wait_for_username = true; 1243 fill_data_.wait_for_username = true;
1189 SimulateOnFillPasswordForm(fill_data_); 1244 SimulateOnFillPasswordForm(fill_data_);
1190 1245
1246 for (const auto& selected_element : {username_element_, password_element_}) {
1247 // Neither field should be autocompleted.
1248 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1249
1250 // If the password field is not autocompletable, it should not be affected.
1251 SetElementReadOnly(password_element_, true);
1252 EXPECT_FALSE(password_autofill_agent_->PreviewSuggestion(
1253 selected_element, kAliceUsername, kAlicePassword));
1254 CheckTextFieldsSuggestedState(std::string(), false, std::string(), false);
1255 SetElementReadOnly(password_element_, false);
1256
1257 // After selecting the suggestion, both fields should be previewed with
1258 // suggested values.
1259 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1260 selected_element, kAliceUsername, kAlicePassword));
1261 CheckTextFieldsSuggestedState(kAliceUsername, true, kAlicePassword, true);
1262 int username_length = strlen(kAliceUsername);
1263 CheckUsernameSelection(0, username_length);
1264
1265 // Try previewing with a password different from the one that was initially
1266 // sent to the renderer.
1267 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1268 selected_element, kBobUsername, kCarolPassword));
1269 CheckTextFieldsSuggestedState(kBobUsername, true, kCarolPassword, true);
1270 username_length = strlen(kBobUsername);
1271 CheckUsernameSelection(0, username_length);
1272
1273 ClearUsernameAndPasswordFields();
1274 }
1275 }
1276
1277 // Tests that |PreviewSuggestion| doesn't change non-empty non-autofilled
1278 // username when previewing autofills on interacting with the password field.
1279 TEST_F(PasswordAutofillAgentTest,
1280 PreviewSuggestionFromPasswordFieldWithUsernameManuallyFilled) {
1281 username_element_.SetValue(WebString::FromUTF8("user1"));
1282
1283 // Simulate the browser sending the login info, but set |wait_for_username| to
1284 // prevent the form from being immediately filled.
1285 fill_data_.wait_for_username = true;
1286 SimulateOnFillPasswordForm(fill_data_);
1191 // Neither field should have been autocompleted. 1287 // Neither field should have been autocompleted.
1192 CheckTextFieldsDOMState(std::string(), false, std::string(), false); 1288 CheckTextFieldsDOMState("user1", false, std::string(), false);
1193 1289
1194 // If the password field is not autocompletable, it should not be affected. 1290 // Only password field should be autocompleted.
1195 SetElementReadOnly(password_element_, true); 1291 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1196 EXPECT_FALSE(password_autofill_agent_->PreviewSuggestion( 1292 password_element_, kAliceUsername, kAlicePassword));
1197 username_element_, kAliceUsername, kAlicePassword)); 1293 CheckTextFieldsSuggestedState(std::string(), false, kAlicePassword, true);
1198 EXPECT_EQ(std::string(), username_element_.SuggestedValue().Utf8()); 1294 CheckTextFieldsDOMState("user1", false, std::string(), true);
1199 EXPECT_FALSE(username_element_.IsAutofilled());
1200 EXPECT_EQ(std::string(), password_element_.SuggestedValue().Utf8());
1201 EXPECT_FALSE(password_element_.IsAutofilled());
1202 SetElementReadOnly(password_element_, false);
1203 1295
1204 // After selecting the suggestion, both fields should be previewed 1296 // Try previewing with a different password. Only password should be changed.
1205 // with suggested values.
1206 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion( 1297 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1207 username_element_, kAliceUsername, kAlicePassword)); 1298 password_element_, kBobUsername, kCarolPassword));
1208 EXPECT_EQ(kAliceUsername, username_element_.SuggestedValue().Utf8()); 1299 CheckTextFieldsSuggestedState(std::string(), false, kCarolPassword, true);
1209 EXPECT_TRUE(username_element_.IsAutofilled()); 1300 CheckTextFieldsDOMState("user1", false, std::string(), true);
1210 EXPECT_EQ(kAlicePassword, password_element_.SuggestedValue().Utf8());
1211 EXPECT_TRUE(password_element_.IsAutofilled());
1212 int username_length = strlen(kAliceUsername);
1213 CheckUsernameSelection(0, username_length);
1214
1215 // Try previewing with a password different from the one that was initially
1216 // sent to the renderer.
1217 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1218 username_element_, kBobUsername, kCarolPassword));
1219 EXPECT_EQ(kBobUsername, username_element_.SuggestedValue().Utf8());
1220 EXPECT_TRUE(username_element_.IsAutofilled());
1221 EXPECT_EQ(kCarolPassword, password_element_.SuggestedValue().Utf8());
1222 EXPECT_TRUE(password_element_.IsAutofilled());
1223 username_length = strlen(kBobUsername);
1224 CheckUsernameSelection(0, username_length);
1225 } 1301 }
1226 1302
1227 // Tests that |PreviewSuggestion| properly previews the password if username is 1303 // Tests that |PreviewSuggestion| properly previews the password if username is
1228 // read-only. 1304 // read-only.
1229 TEST_F(PasswordAutofillAgentTest, PreviewSuggestionIfUsernameReadOnly) { 1305 TEST_F(PasswordAutofillAgentTest, PreviewSuggestionIfUsernameReadOnly) {
1230 // Simulate the browser sending the login info. 1306 // Simulate the browser sending the login info.
1231 SetElementReadOnly(username_element_, true); 1307 SetElementReadOnly(username_element_, true);
1232 SimulateOnFillPasswordForm(fill_data_); 1308 SimulateOnFillPasswordForm(fill_data_);
1233 1309
1234 // Neither field should have been autocompleted. 1310 for (const auto& selected_element : {username_element_, password_element_}) {
1235 CheckTextFieldsDOMState(std::string(), false, std::string(), false); 1311 // Neither field should be autocompleted.
1312 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1236 1313
1237 // Username field is not autocompletable, it should not be affected. 1314 // Username field is not autocompletable, it should not be affected.
1238 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion( 1315 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1239 password_element_, kAliceUsername, kAlicePassword)); 1316 selected_element, kAliceUsername, kAlicePassword));
1240 EXPECT_EQ(std::string(), username_element_.SuggestedValue().Utf8()); 1317 // Password field must be autofilled.
1241 EXPECT_FALSE(username_element_.IsAutofilled()); 1318 CheckTextFieldsSuggestedState(std::string(), false, kAlicePassword, true);
1242 1319
1243 // Password field must be autofilled. 1320 // Try previewing with a password different from the one that was initially
1244 EXPECT_EQ(kAlicePassword, password_element_.SuggestedValue().Utf8()); 1321 // sent to the renderer.
1245 EXPECT_TRUE(password_element_.IsAutofilled()); 1322 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1323 selected_element, kBobUsername, kCarolPassword));
1324 CheckTextFieldsSuggestedState(std::string(), false, kCarolPassword, true);
1246 1325
1247 // Try previewing with a password different from the one that was initially 1326 ClearUsernameAndPasswordFields();
1248 // sent to the renderer. 1327 }
1249 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1250 password_element_, kBobUsername, kCarolPassword));
1251 EXPECT_EQ(std::string(), username_element_.SuggestedValue().Utf8());
1252 EXPECT_FALSE(username_element_.IsAutofilled());
1253 EXPECT_EQ(kCarolPassword, password_element_.SuggestedValue().Utf8());
1254 EXPECT_TRUE(password_element_.IsAutofilled());
1255 } 1328 }
1256 1329
1257 // Tests that |PreviewSuggestion| properly sets the username selection range. 1330 // Tests that |PreviewSuggestion| properly sets the username selection range.
1258 TEST_F(PasswordAutofillAgentTest, PreviewSuggestionSelectionRange) { 1331 TEST_F(PasswordAutofillAgentTest, PreviewSuggestionSelectionRange) {
1259 username_element_.SetValue(WebString::FromUTF8("ali"));
1260 username_element_.SetSelectionRange(3, 3);
1261 username_element_.SetAutofilled(true);
1262
1263 CheckTextFieldsDOMState("ali", true, std::string(), false);
1264
1265 // Simulate the browser sending the login info, but set |wait_for_username| 1332 // Simulate the browser sending the login info, but set |wait_for_username|
1266 // to prevent the form from being immediately filled. 1333 // to prevent the form from being immediately filled.
1267 fill_data_.wait_for_username = true; 1334 fill_data_.wait_for_username = true;
1268 SimulateOnFillPasswordForm(fill_data_); 1335 SimulateOnFillPasswordForm(fill_data_);
1269 1336
1270 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion( 1337 for (const auto& selected_element : {username_element_, password_element_}) {
1271 username_element_, kAliceUsername, kAlicePassword)); 1338 ResetFieldState(&username_element_, "ali", true);
1272 EXPECT_EQ(kAliceUsername, username_element_.SuggestedValue().Utf8()); 1339 ResetFieldState(&password_element_);
1273 EXPECT_TRUE(username_element_.IsAutofilled()); 1340
1274 EXPECT_EQ(kAlicePassword, password_element_.SuggestedValue().Utf8()); 1341 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1275 EXPECT_TRUE(password_element_.IsAutofilled()); 1342 selected_element, kAliceUsername, kAlicePassword));
1276 int username_length = strlen(kAliceUsername); 1343 CheckTextFieldsSuggestedState(kAliceUsername, true, kAlicePassword, true);
1277 CheckUsernameSelection(3, username_length); 1344 int username_length = strlen(kAliceUsername);
1345 CheckUsernameSelection(3, username_length);
1346 }
1278 } 1347 }
1279 1348
1280 // Tests that |ClearPreview| properly clears previewed username and password 1349 // Tests that |ClearPreview| properly clears previewed username and password
1281 // with password being previously autofilled. 1350 // with password being previously autofilled.
1282 TEST_F(PasswordAutofillAgentTest, ClearPreviewWithPasswordAutofilled) { 1351 TEST_F(PasswordAutofillAgentTest, ClearPreviewWithPasswordAutofilled) {
1283 password_element_.SetValue(WebString::FromUTF8("sec")); 1352 ResetFieldState(&password_element_, "sec", true);
1284 password_element_.SetAutofilled(true);
1285 1353
1286 // Simulate the browser sending the login info, but set |wait_for_username| 1354 // Simulate the browser sending the login info, but set |wait_for_username|
1287 // to prevent the form from being immediately filled. 1355 // to prevent the form from being immediately filled.
1288 fill_data_.wait_for_username = true; 1356 fill_data_.wait_for_username = true;
1289 SimulateOnFillPasswordForm(fill_data_); 1357 SimulateOnFillPasswordForm(fill_data_);
1290 1358
1291 CheckTextFieldsDOMState(std::string(), false, "sec", true); 1359 CheckTextFieldsDOMState(std::string(), false, "sec", true);
1292 1360
1293 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion( 1361 for (const auto& selected_element : {username_element_, password_element_}) {
1294 username_element_, kAliceUsername, kAlicePassword)); 1362 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1363 selected_element, kAliceUsername, kAlicePassword));
1364 EXPECT_TRUE(
1365 password_autofill_agent_->DidClearAutofillSelection(selected_element));
1295 1366
1296 EXPECT_TRUE( 1367 EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty());
1297 password_autofill_agent_->DidClearAutofillSelection(username_element_)); 1368 EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
1298 1369 CheckTextFieldsDOMState(std::string(), false, "sec", true);
1299 EXPECT_TRUE(username_element_.Value().IsEmpty()); 1370 CheckUsernameSelection(0, 0);
1300 EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty()); 1371 }
1301 EXPECT_FALSE(username_element_.IsAutofilled());
1302 EXPECT_EQ(ASCIIToUTF16("sec"), password_element_.Value().Utf16());
1303 EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
1304 EXPECT_TRUE(password_element_.IsAutofilled());
1305 CheckUsernameSelection(0, 0);
1306 } 1372 }
1307 1373
1308 // Tests that |ClearPreview| properly clears previewed username and password 1374 // Tests that |ClearPreview| properly clears previewed username and password
1309 // with username being previously autofilled. 1375 // with username being previously autofilled.
1310 TEST_F(PasswordAutofillAgentTest, ClearPreviewWithUsernameAutofilled) { 1376 TEST_F(PasswordAutofillAgentTest, ClearPreviewWithUsernameAutofilled) {
1311 username_element_.SetValue(WebString::FromUTF8("ali")); 1377 ResetFieldState(&username_element_, "ali", true);
1312 username_element_.SetSelectionRange(3, 3); 1378 username_element_.SetSelectionRange(3, 3);
1313 username_element_.SetAutofilled(true);
1314 1379
1315 // Simulate the browser sending the login info, but set |wait_for_username| 1380 // Simulate the browser sending the login info, but set |wait_for_username|
1316 // to prevent the form from being immediately filled. 1381 // to prevent the form from being immediately filled.
1317 fill_data_.wait_for_username = true; 1382 fill_data_.wait_for_username = true;
1318 SimulateOnFillPasswordForm(fill_data_); 1383 SimulateOnFillPasswordForm(fill_data_);
1319 1384
1320 CheckTextFieldsDOMState("ali", true, std::string(), false); 1385 CheckTextFieldsDOMState("ali", true, std::string(), false);
1321 1386
1322 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion( 1387 for (const auto& selected_element : {username_element_, password_element_}) {
1323 username_element_, kAliceUsername, kAlicePassword)); 1388 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1389 selected_element, kAliceUsername, kAlicePassword));
1390 EXPECT_TRUE(
1391 password_autofill_agent_->DidClearAutofillSelection(selected_element));
1324 1392
1325 EXPECT_TRUE( 1393 EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty());
1326 password_autofill_agent_->DidClearAutofillSelection(username_element_)); 1394 EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
1327 1395 CheckTextFieldsDOMState("ali", true, std::string(), false);
1328 EXPECT_EQ(ASCIIToUTF16("ali"), username_element_.Value().Utf16()); 1396 CheckUsernameSelection(3, 3);
1329 EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty()); 1397 }
1330 EXPECT_TRUE(username_element_.IsAutofilled());
1331 EXPECT_TRUE(password_element_.Value().IsEmpty());
1332 EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
1333 EXPECT_FALSE(password_element_.IsAutofilled());
1334 CheckUsernameSelection(3, 3);
1335 } 1398 }
1336 1399
1337 // Tests that |ClearPreview| properly clears previewed username and password 1400 // Tests that |ClearPreview| properly clears previewed username and password
1338 // with username and password being previously autofilled. 1401 // with username and password being previously autofilled.
1339 TEST_F(PasswordAutofillAgentTest, 1402 TEST_F(PasswordAutofillAgentTest,
1340 ClearPreviewWithAutofilledUsernameAndPassword) { 1403 ClearPreviewWithAutofilledUsernameAndPassword) {
1341 username_element_.SetValue(WebString::FromUTF8("ali")); 1404 ResetFieldState(&username_element_, "ali", true);
1342 username_element_.SetSelectionRange(3, 3); 1405 ResetFieldState(&password_element_, "sec", true);
1343 username_element_.SetAutofilled(true);
1344 password_element_.SetValue(WebString::FromUTF8("sec"));
1345 password_element_.SetAutofilled(true);
1346 1406
1347 // Simulate the browser sending the login info, but set |wait_for_username| 1407 // Simulate the browser sending the login info, but set |wait_for_username|
1348 // to prevent the form from being immediately filled. 1408 // to prevent the form from being immediately filled.
1349 fill_data_.wait_for_username = true; 1409 fill_data_.wait_for_username = true;
1350 SimulateOnFillPasswordForm(fill_data_); 1410 SimulateOnFillPasswordForm(fill_data_);
1351 1411
1352 CheckTextFieldsDOMState("ali", true, "sec", true); 1412 CheckTextFieldsDOMState("ali", true, "sec", true);
1353 1413
1354 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion( 1414 for (const auto& selected_element : {username_element_, password_element_}) {
1355 username_element_, kAliceUsername, kAlicePassword)); 1415 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1416 selected_element, kAliceUsername, kAlicePassword));
1417 EXPECT_TRUE(
1418 password_autofill_agent_->DidClearAutofillSelection(selected_element));
1356 1419
1357 EXPECT_TRUE( 1420 EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty());
1358 password_autofill_agent_->DidClearAutofillSelection(username_element_)); 1421 EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
1359 1422 CheckTextFieldsDOMState("ali", true, "sec", true);
1360 EXPECT_EQ(ASCIIToUTF16("ali"), username_element_.Value().Utf16()); 1423 CheckUsernameSelection(3, 3);
1361 EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty()); 1424 }
1362 EXPECT_TRUE(username_element_.IsAutofilled());
1363 EXPECT_EQ(ASCIIToUTF16("sec"), password_element_.Value().Utf16());
1364 EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
1365 EXPECT_TRUE(password_element_.IsAutofilled());
1366 CheckUsernameSelection(3, 3);
1367 } 1425 }
1368 1426
1369 // Tests that |ClearPreview| properly clears previewed username and password 1427 // Tests that |ClearPreview| properly clears previewed username and password
1370 // with neither username nor password being previously autofilled. 1428 // with neither username nor password being previously autofilled.
1371 TEST_F(PasswordAutofillAgentTest, 1429 TEST_F(PasswordAutofillAgentTest,
1372 ClearPreviewWithNotAutofilledUsernameAndPassword) { 1430 ClearPreviewWithNotAutofilledUsernameAndPassword) {
1373 // Simulate the browser sending the login info, but set |wait_for_username| 1431 // Simulate the browser sending the login info, but set |wait_for_username|
1374 // to prevent the form from being immediately filled. 1432 // to prevent the form from being immediately filled.
1375 fill_data_.wait_for_username = true; 1433 fill_data_.wait_for_username = true;
1376 SimulateOnFillPasswordForm(fill_data_); 1434 SimulateOnFillPasswordForm(fill_data_);
1377 1435
1378 CheckTextFieldsDOMState(std::string(), false, std::string(), false); 1436 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1379 1437
1380 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion( 1438 for (const auto& selected_element : {username_element_, password_element_}) {
1381 username_element_, kAliceUsername, kAlicePassword)); 1439 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1440 selected_element, kAliceUsername, kAlicePassword));
1441 EXPECT_TRUE(
1442 password_autofill_agent_->DidClearAutofillSelection(selected_element));
1382 1443
1383 EXPECT_TRUE( 1444 EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty());
1384 password_autofill_agent_->DidClearAutofillSelection(username_element_)); 1445 EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
1385 1446 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1386 EXPECT_TRUE(username_element_.Value().IsEmpty()); 1447 CheckUsernameSelection(0, 0);
1387 EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty()); 1448 }
1388 EXPECT_FALSE(username_element_.IsAutofilled());
1389 EXPECT_TRUE(password_element_.Value().IsEmpty());
1390 EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
1391 EXPECT_FALSE(password_element_.IsAutofilled());
1392 CheckUsernameSelection(0, 0);
1393 } 1449 }
1394 1450
1395 // Tests that logging is off by default. 1451 // Tests that logging is off by default.
1396 TEST_F(PasswordAutofillAgentTest, OnChangeLoggingState_NoMessage) { 1452 TEST_F(PasswordAutofillAgentTest, OnChangeLoggingState_NoMessage) {
1397 SendVisiblePasswordForms(); 1453 SendVisiblePasswordForms();
1398 base::RunLoop().RunUntilIdle(); 1454 base::RunLoop().RunUntilIdle();
1399 EXPECT_FALSE(fake_driver_.called_record_save_progress()); 1455 EXPECT_FALSE(fake_driver_.called_record_save_progress());
1400 } 1456 }
1401 1457
1402 // Test that logging can be turned on by a message. 1458 // Test that logging can be turned on by a message.
(...skipping 755 matching lines...) Expand 10 before | Expand all | Expand 10 after
2158 TEST_F(PasswordAutofillAgentTest, FillSuggestionPasswordChangeForms) { 2214 TEST_F(PasswordAutofillAgentTest, FillSuggestionPasswordChangeForms) {
2159 LoadHTML(kPasswordChangeFormHTML); 2215 LoadHTML(kPasswordChangeFormHTML);
2160 UpdateOriginForHTML(kPasswordChangeFormHTML); 2216 UpdateOriginForHTML(kPasswordChangeFormHTML);
2161 UpdateUsernameAndPasswordElements(); 2217 UpdateUsernameAndPasswordElements();
2162 // Simulate the browser sending the login info, but set |wait_for_username| 2218 // Simulate the browser sending the login info, but set |wait_for_username|
2163 // to prevent the form from being immediately filled. 2219 // to prevent the form from being immediately filled.
2164 fill_data_.wait_for_username = true; 2220 fill_data_.wait_for_username = true;
2165 fill_data_.is_possible_change_password_form = true; 2221 fill_data_.is_possible_change_password_form = true;
2166 SimulateOnFillPasswordForm(fill_data_); 2222 SimulateOnFillPasswordForm(fill_data_);
2167 2223
2168 // Neither field should have been autocompleted. 2224 for (const auto& selected_element : {username_element_, password_element_}) {
2169 CheckTextFieldsDOMState(std::string(), false, std::string(), false); 2225 // Neither field should be autocompleted.
2226 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
2170 2227
2171 EXPECT_TRUE(password_autofill_agent_->FillSuggestion( 2228 EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
2172 username_element_, ASCIIToUTF16(kAliceUsername), 2229 selected_element, ASCIIToUTF16(kAliceUsername),
2173 ASCIIToUTF16(kAlicePassword))); 2230 ASCIIToUTF16(kAlicePassword)));
2174 CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true); 2231 CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
2175 }
2176 2232
2177 // Tests that a password change form is properly filled with the password when 2233 ClearUsernameAndPasswordFields();
2178 // the user click on the password field. 2234 }
2179 TEST_F(PasswordAutofillAgentTest,
2180 FillSuggestionPasswordChangeFormsOnlyPassword) {
2181 LoadHTML(kPasswordChangeFormHTML);
2182 UpdateOriginForHTML(kPasswordChangeFormHTML);
2183 UpdateUsernameAndPasswordElements();
2184 // Simulate the browser sending the login info, but set |wait_for_username|
2185 // to prevent the form from being immediately filled.
2186 fill_data_.wait_for_username = true;
2187 fill_data_.is_possible_change_password_form = true;
2188 SimulateOnFillPasswordForm(fill_data_);
2189
2190 // Neither field should have been autocompleted.
2191 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
2192
2193 EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
2194 password_element_, ASCIIToUTF16(kAliceUsername),
2195 ASCIIToUTF16(kAlicePassword)));
2196 CheckTextFieldsDOMState("", false, kAlicePassword, true);
2197 } 2235 }
2198 2236
2199 // Tests that one user click on a username field is sufficient to bring up a 2237 // Tests that one user click on a username field is sufficient to bring up a
2200 // credential suggestion popup on a change password form. 2238 // credential suggestion popup on a change password form.
2201 TEST_F(PasswordAutofillAgentTest, 2239 TEST_F(PasswordAutofillAgentTest,
2202 SuggestionsOnUsernameFieldOfChangePasswordForm) { 2240 SuggestionsOnUsernameFieldOfChangePasswordForm) {
2203 LoadHTML(kPasswordChangeFormHTML); 2241 LoadHTML(kPasswordChangeFormHTML);
2204 UpdateOriginForHTML(kPasswordChangeFormHTML); 2242 UpdateOriginForHTML(kPasswordChangeFormHTML);
2205 UpdateUsernameAndPasswordElements(); 2243 UpdateUsernameAndPasswordElements();
2206 2244
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after
2919 TEST_F(PasswordAutofillAgentTest, AutocompleteWhenPageUrlIsChanged) { 2957 TEST_F(PasswordAutofillAgentTest, AutocompleteWhenPageUrlIsChanged) {
2920 // Simulate that JavaScript changes url. 2958 // Simulate that JavaScript changes url.
2921 fill_data_.origin = GURL(fill_data_.origin.possibly_invalid_spec() + "/path"); 2959 fill_data_.origin = GURL(fill_data_.origin.possibly_invalid_spec() + "/path");
2922 2960
2923 SimulateOnFillPasswordForm(fill_data_); 2961 SimulateOnFillPasswordForm(fill_data_);
2924 2962
2925 // The username and password should have been autocompleted. 2963 // The username and password should have been autocompleted.
2926 CheckTextFieldsState(kAliceUsername, true, kAlicePassword, true); 2964 CheckTextFieldsState(kAliceUsername, true, kAlicePassword, true);
2927 } 2965 }
2928 } // namespace autofill 2966 } // namespace autofill
OLDNEW
« no previous file with comments | « no previous file | components/autofill/content/renderer/password_autofill_agent.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698