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" |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 password, | 410 password, |
411 password_autofilled, | 411 password_autofilled, |
412 false); | 412 false); |
413 } | 413 } |
414 | 414 |
415 void CheckUsernameSelection(int start, int end) { | 415 void CheckUsernameSelection(int start, int end) { |
416 EXPECT_EQ(start, username_element_.selectionStart()); | 416 EXPECT_EQ(start, username_element_.selectionStart()); |
417 EXPECT_EQ(end, username_element_.selectionEnd()); | 417 EXPECT_EQ(end, username_element_.selectionEnd()); |
418 } | 418 } |
419 | 419 |
420 void ExpectOneCredential(const base::string16& username) { | 420 // Checks the message sent to PasswordAutofillManager to build the suggestion |
| 421 // list. |username| is the expected username field value, and |show_all| is |
| 422 // the expected flag for the PasswordAutofillManager, whether to show all |
| 423 // suggestions, or only those starting with |username|. |
| 424 void CheckSuggestions(const std::string& username, bool show_all) { |
421 const IPC::Message* message = | 425 const IPC::Message* message = |
422 render_thread_->sink().GetFirstMessageMatching( | 426 render_thread_->sink().GetFirstMessageMatching( |
423 AutofillHostMsg_ShowPasswordSuggestions::ID); | 427 AutofillHostMsg_ShowPasswordSuggestions::ID); |
424 ASSERT_TRUE(message); | 428 EXPECT_TRUE(message); |
425 Tuple4<autofill::FormFieldData, | 429 Tuple4<autofill::FormFieldData, base::string16, bool, gfx::RectF> args; |
426 gfx::RectF, | |
427 std::vector<base::string16>, | |
428 std::vector<base::string16> > args; | |
429 AutofillHostMsg_ShowPasswordSuggestions::Read(message, &args); | 430 AutofillHostMsg_ShowPasswordSuggestions::Read(message, &args); |
430 ASSERT_EQ(1u, args.c.size()); | 431 EXPECT_EQ(2u, fill_data_.basic_data.fields.size()); |
431 EXPECT_TRUE(args.c[0] == username); | 432 EXPECT_EQ(fill_data_.basic_data.fields[0].name, args.a.name); |
432 } | 433 EXPECT_EQ(ASCIIToUTF16(username), args.a.value); |
433 | 434 EXPECT_EQ(ASCIIToUTF16(username), args.b); |
434 void ExpectAllCredentials() { | 435 EXPECT_EQ(show_all, args.c); |
435 std::set<base::string16> usernames; | |
436 usernames.insert(username1_); | |
437 usernames.insert(username2_); | |
438 usernames.insert(username3_); | |
439 usernames.insert(alternate_username3_); | |
440 | |
441 const IPC::Message* message = | |
442 render_thread_->sink().GetFirstMessageMatching( | |
443 AutofillHostMsg_ShowPasswordSuggestions::ID); | |
444 ASSERT_TRUE(message); | |
445 Tuple4<autofill::FormFieldData, | |
446 gfx::RectF, | |
447 std::vector<base::string16>, | |
448 std::vector<base::string16> > args; | |
449 AutofillHostMsg_ShowPasswordSuggestions::Read(message, &args); | |
450 ASSERT_EQ(4u, args.c.size()); | |
451 std::set<base::string16>::iterator it; | |
452 | |
453 for (int i = 0; i < 4; i++) { | |
454 it = usernames.find(args.c[i]); | |
455 EXPECT_TRUE(it != usernames.end()); | |
456 if (it != usernames.end()) | |
457 usernames.erase(it); | |
458 } | |
459 | |
460 EXPECT_TRUE(usernames.empty()); | |
461 | 436 |
462 render_thread_->sink().ClearMessages(); | 437 render_thread_->sink().ClearMessages(); |
463 } | 438 } |
464 | 439 |
465 void ExpectFormSubmittedWithPasswords(const std::string& password_value, | 440 void ExpectFormSubmittedWithPasswords(const std::string& password_value, |
466 const std::string& new_password_value) { | 441 const std::string& new_password_value) { |
467 const IPC::Message* message = | 442 const IPC::Message* message = |
468 render_thread_->sink().GetFirstMessageMatching( | 443 render_thread_->sink().GetFirstMessageMatching( |
469 AutofillHostMsg_PasswordFormSubmitted::ID); | 444 AutofillHostMsg_PasswordFormSubmitted::ID); |
470 ASSERT_TRUE(message); | 445 ASSERT_TRUE(message); |
(...skipping 977 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1448 TEST_F(PasswordAutofillAgentTest, ClickAndSelect) { | 1423 TEST_F(PasswordAutofillAgentTest, ClickAndSelect) { |
1449 // SimulateElementClick() is called so that a user gesture is actually made | 1424 // SimulateElementClick() is called so that a user gesture is actually made |
1450 // and the password can be filled. However, SimulateElementClick() does not | 1425 // and the password can be filled. However, SimulateElementClick() does not |
1451 // actually lead to the AutofillAgent's InputElementClicked() method being | 1426 // actually lead to the AutofillAgent's InputElementClicked() method being |
1452 // called, so SimulateSuggestionChoice has to manually call | 1427 // called, so SimulateSuggestionChoice has to manually call |
1453 // InputElementClicked(). | 1428 // InputElementClicked(). |
1454 ClearUsernameAndPasswordFields(); | 1429 ClearUsernameAndPasswordFields(); |
1455 SimulateOnFillPasswordForm(fill_data_); | 1430 SimulateOnFillPasswordForm(fill_data_); |
1456 SimulateElementClick(kUsernameName); | 1431 SimulateElementClick(kUsernameName); |
1457 SimulateSuggestionChoice(username_element_); | 1432 SimulateSuggestionChoice(username_element_); |
1458 ExpectAllCredentials(); | 1433 CheckSuggestions(kAliceUsername, true); |
1459 | 1434 |
1460 CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true); | 1435 CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true); |
1461 } | 1436 } |
1462 | 1437 |
1463 // Tests the autosuggestions that are given when the element is clicked. | 1438 // Tests the autosuggestions that are given when the element is clicked. |
1464 // Specifically, tests when the user clicks on the username element after page | 1439 // Specifically, tests when the user clicks on the username element after page |
1465 // load and the element is autofilled, when the user clicks on an element that | 1440 // load and the element is autofilled, when the user clicks on an element that |
1466 // has a non-matching username, and when the user clicks on an element that's | 1441 // has a non-matching username, and when the user clicks on an element that's |
1467 // already been autofilled and they've already modified. | 1442 // already been autofilled and they've already modified. |
1468 TEST_F(PasswordAutofillAgentTest, CredentialsOnClick) { | 1443 TEST_F(PasswordAutofillAgentTest, CredentialsOnClick) { |
1469 // Simulate the browser sending back the login info. | 1444 // Simulate the browser sending back the login info. |
1470 SimulateOnFillPasswordForm(fill_data_); | 1445 SimulateOnFillPasswordForm(fill_data_); |
1471 | 1446 |
1472 // Clear the text fields to start fresh. | 1447 // Clear the text fields to start fresh. |
1473 ClearUsernameAndPasswordFields(); | 1448 ClearUsernameAndPasswordFields(); |
1474 | 1449 |
1475 // Call SimulateElementClick() to produce a user gesture on the page so | 1450 // Call SimulateElementClick() to produce a user gesture on the page so |
1476 // autofill will actually fill. | 1451 // autofill will actually fill. |
1477 SimulateElementClick(kUsernameName); | 1452 SimulateElementClick(kUsernameName); |
1478 | 1453 |
1479 // Simulate a user clicking on the username element. This should produce a | 1454 // Simulate a user clicking on the username element. This should produce a |
1480 // message with all the usernames. | 1455 // message with all the usernames. |
1481 render_thread_->sink().ClearMessages(); | 1456 render_thread_->sink().ClearMessages(); |
1482 static_cast<PageClickListener*>(autofill_agent_) | 1457 static_cast<PageClickListener*>(autofill_agent_) |
1483 ->FormControlElementClicked(username_element_, false); | 1458 ->FormControlElementClicked(username_element_, false); |
1484 ExpectAllCredentials(); | 1459 CheckSuggestions(std::string(), false); |
1485 | 1460 |
1486 // Now simulate a user typing in an unrecognized username and then | 1461 // Now simulate a user typing in an unrecognized username and then |
1487 // clicking on the username element. This should also produce a message with | 1462 // clicking on the username element. This should also produce a message with |
1488 // all the usernames. | 1463 // all the usernames. |
1489 SimulateUsernameChange("baz", true); | 1464 SimulateUsernameChange("baz", true); |
1490 render_thread_->sink().ClearMessages(); | 1465 render_thread_->sink().ClearMessages(); |
1491 static_cast<PageClickListener*>(autofill_agent_) | 1466 static_cast<PageClickListener*>(autofill_agent_) |
1492 ->FormControlElementClicked(username_element_, true); | 1467 ->FormControlElementClicked(username_element_, true); |
1493 ExpectAllCredentials(); | 1468 CheckSuggestions("baz", true); |
1494 | 1469 |
1495 // Now simulate a user typing in the first letter of the username and then | 1470 // Now simulate a user typing in the first letter of the username and then |
1496 // clicking on the username element. While the typing of the first letter will | 1471 // clicking on the username element. While the typing of the first letter will |
1497 // inline autocomplete, clicking on the element should still produce a full | 1472 // inline autocomplete, clicking on the element should still produce a full |
1498 // suggestion list. | 1473 // suggestion list. |
1499 SimulateUsernameChange("a", true); | 1474 SimulateUsernameChange("a", true); |
1500 render_thread_->sink().ClearMessages(); | 1475 render_thread_->sink().ClearMessages(); |
1501 static_cast<PageClickListener*>(autofill_agent_) | 1476 static_cast<PageClickListener*>(autofill_agent_) |
1502 ->FormControlElementClicked(username_element_, true); | 1477 ->FormControlElementClicked(username_element_, true); |
1503 ExpectAllCredentials(); | 1478 CheckSuggestions(kAliceUsername, true); |
1504 } | 1479 } |
1505 | 1480 |
1506 // The user types in a password, but then just before sending the form off, a | 1481 // The user types in a password, but then just before sending the form off, a |
1507 // script clears that password. This test checks that PasswordAutofillAgent can | 1482 // script clears that password. This test checks that PasswordAutofillAgent can |
1508 // still remember the password typed by the user. | 1483 // still remember the password typed by the user. |
1509 TEST_F(PasswordAutofillAgentTest, | 1484 TEST_F(PasswordAutofillAgentTest, |
1510 RememberLastNonEmptyPasswordOnSubmit_ScriptCleared) { | 1485 RememberLastNonEmptyPasswordOnSubmit_ScriptCleared) { |
1511 SimulateInputChangeForElement( | 1486 SimulateInputChangeForElement( |
1512 "temp", true, GetMainFrame(), username_element_, true); | 1487 "temp", true, GetMainFrame(), username_element_, true); |
1513 SimulateInputChangeForElement( | 1488 SimulateInputChangeForElement( |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1626 password_element_, | 1601 password_element_, |
1627 /*is_user_input=*/true); | 1602 /*is_user_input=*/true); |
1628 | 1603 |
1629 // Simulate the user typing a stored username. | 1604 // Simulate the user typing a stored username. |
1630 SimulateUsernameChange(kAliceUsername, true); | 1605 SimulateUsernameChange(kAliceUsername, true); |
1631 // The autofileld password should replace the typed one. | 1606 // The autofileld password should replace the typed one. |
1632 CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true); | 1607 CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true); |
1633 } | 1608 } |
1634 | 1609 |
1635 } // namespace autofill | 1610 } // namespace autofill |
OLD | NEW |