| 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 "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 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 const std::string& password_value, | 500 const std::string& password_value, |
| 501 const std::string& new_password_value) { | 501 const std::string& new_password_value) { |
| 502 base::RunLoop().RunUntilIdle(); | 502 base::RunLoop().RunUntilIdle(); |
| 503 ASSERT_TRUE(fake_driver_.called_password_form_submitted()); | 503 ASSERT_TRUE(fake_driver_.called_password_form_submitted()); |
| 504 ASSERT_TRUE(static_cast<bool>(fake_driver_.password_form_submitted())); | 504 ASSERT_TRUE(static_cast<bool>(fake_driver_.password_form_submitted())); |
| 505 const autofill::PasswordForm& form = | 505 const autofill::PasswordForm& form = |
| 506 *(fake_driver_.password_form_submitted()); | 506 *(fake_driver_.password_form_submitted()); |
| 507 EXPECT_EQ(ASCIIToUTF16(username_value), form.username_value); | 507 EXPECT_EQ(ASCIIToUTF16(username_value), form.username_value); |
| 508 EXPECT_EQ(ASCIIToUTF16(password_value), form.password_value); | 508 EXPECT_EQ(ASCIIToUTF16(password_value), form.password_value); |
| 509 EXPECT_EQ(ASCIIToUTF16(new_password_value), form.new_password_value); | 509 EXPECT_EQ(ASCIIToUTF16(new_password_value), form.new_password_value); |
| 510 EXPECT_EQ(PasswordForm::SubmissionEvent::HTML_FORM_SUBMISSION, |
| 511 form.submission_event); |
| 510 } | 512 } |
| 511 | 513 |
| 512 void ExpectFieldPropertiesMasks( | 514 void ExpectFieldPropertiesMasks( |
| 513 PasswordFormSourceType expected_type, | 515 PasswordFormSourceType expected_type, |
| 514 const std::map<base::string16, FieldPropertiesMask>& | 516 const std::map<base::string16, FieldPropertiesMask>& |
| 515 expected_properties_masks) { | 517 expected_properties_masks) { |
| 516 base::RunLoop().RunUntilIdle(); | 518 base::RunLoop().RunUntilIdle(); |
| 517 autofill::PasswordForm form; | 519 autofill::PasswordForm form; |
| 518 if (expected_type == PasswordFormSubmitted) { | 520 if (expected_type == PasswordFormSubmitted) { |
| 519 ASSERT_TRUE(fake_driver_.called_password_form_submitted()); | 521 ASSERT_TRUE(fake_driver_.called_password_form_submitted()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 536 << "Wrong mask for the field " << field.name; | 538 << "Wrong mask for the field " << field.name; |
| 537 unchecked_masks--; | 539 unchecked_masks--; |
| 538 } | 540 } |
| 539 EXPECT_TRUE(unchecked_masks == 0) | 541 EXPECT_TRUE(unchecked_masks == 0) |
| 540 << "Some expected masks are missed in FormData"; | 542 << "Some expected masks are missed in FormData"; |
| 541 } | 543 } |
| 542 | 544 |
| 543 void ExpectInPageNavigationWithUsernameAndPasswords( | 545 void ExpectInPageNavigationWithUsernameAndPasswords( |
| 544 const std::string& username_value, | 546 const std::string& username_value, |
| 545 const std::string& password_value, | 547 const std::string& password_value, |
| 546 const std::string& new_password_value) { | 548 const std::string& new_password_value, |
| 549 PasswordForm::SubmissionEvent event) { |
| 547 base::RunLoop().RunUntilIdle(); | 550 base::RunLoop().RunUntilIdle(); |
| 548 ASSERT_TRUE(fake_driver_.called_inpage_navigation()); | 551 ASSERT_TRUE(fake_driver_.called_inpage_navigation()); |
| 549 ASSERT_TRUE( | 552 ASSERT_TRUE( |
| 550 static_cast<bool>(fake_driver_.password_form_inpage_navigation())); | 553 static_cast<bool>(fake_driver_.password_form_inpage_navigation())); |
| 551 const autofill::PasswordForm& form = | 554 const autofill::PasswordForm& form = |
| 552 *(fake_driver_.password_form_inpage_navigation()); | 555 *(fake_driver_.password_form_inpage_navigation()); |
| 553 EXPECT_EQ(ASCIIToUTF16(username_value), form.username_value); | 556 EXPECT_EQ(ASCIIToUTF16(username_value), form.username_value); |
| 554 EXPECT_EQ(ASCIIToUTF16(password_value), form.password_value); | 557 EXPECT_EQ(ASCIIToUTF16(password_value), form.password_value); |
| 555 EXPECT_EQ(ASCIIToUTF16(new_password_value), form.new_password_value); | 558 EXPECT_EQ(ASCIIToUTF16(new_password_value), form.new_password_value); |
| 559 EXPECT_EQ(event, form.submission_event); |
| 556 } | 560 } |
| 557 | 561 |
| 558 void CheckIfEventsAreCalled(const std::vector<base::string16>& checkers, | 562 void CheckIfEventsAreCalled(const std::vector<base::string16>& checkers, |
| 559 bool expected) { | 563 bool expected) { |
| 560 for (const base::string16& variable : checkers) { | 564 for (const base::string16& variable : checkers) { |
| 561 int value; | 565 int value; |
| 562 EXPECT_TRUE(ExecuteJavaScriptAndReturnIntValue(variable, &value)) | 566 EXPECT_TRUE(ExecuteJavaScriptAndReturnIntValue(variable, &value)) |
| 563 << variable; | 567 << variable; |
| 564 EXPECT_EQ(expected, value == 1) << variable; | 568 EXPECT_EQ(expected, value == 1) << variable; |
| 565 } | 569 } |
| (...skipping 1676 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2242 UpdateUsernameAndPasswordElements(); | 2246 UpdateUsernameAndPasswordElements(); |
| 2243 | 2247 |
| 2244 SimulateUsernameChange("Bob"); | 2248 SimulateUsernameChange("Bob"); |
| 2245 SimulatePasswordChange("mypassword"); | 2249 SimulatePasswordChange("mypassword"); |
| 2246 | 2250 |
| 2247 username_element_.SetAttribute("style", "display:none;"); | 2251 username_element_.SetAttribute("style", "display:none;"); |
| 2248 password_element_.SetAttribute("style", "display:none;"); | 2252 password_element_.SetAttribute("style", "display:none;"); |
| 2249 | 2253 |
| 2250 password_autofill_agent_->AJAXSucceeded(); | 2254 password_autofill_agent_->AJAXSucceeded(); |
| 2251 | 2255 |
| 2252 ExpectInPageNavigationWithUsernameAndPasswords("Bob", "mypassword", ""); | 2256 ExpectInPageNavigationWithUsernameAndPasswords( |
| 2257 "Bob", "mypassword", "", PasswordForm::SubmissionEvent::XHR_SUCCEEDED); |
| 2253 } | 2258 } |
| 2254 | 2259 |
| 2255 TEST_F(PasswordAutofillAgentTest, | 2260 TEST_F(PasswordAutofillAgentTest, |
| 2256 NoForm_NoPromptForAJAXSubmitWithoutNavigationAndElementsVisible) { | 2261 NoForm_NoPromptForAJAXSubmitWithoutNavigationAndElementsVisible) { |
| 2257 LoadHTML(kNoFormHTML); | 2262 LoadHTML(kNoFormHTML); |
| 2258 UpdateUsernameAndPasswordElements(); | 2263 UpdateUsernameAndPasswordElements(); |
| 2259 | 2264 |
| 2260 SimulateUsernameChange("Bob"); | 2265 SimulateUsernameChange("Bob"); |
| 2261 SimulatePasswordChange("mypassword"); | 2266 SimulatePasswordChange("mypassword"); |
| 2262 | 2267 |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2533 SimulateUsernameChange("Bob"); | 2538 SimulateUsernameChange("Bob"); |
| 2534 SimulatePasswordChange("mypassword"); | 2539 SimulatePasswordChange("mypassword"); |
| 2535 SimulateUsernameChange("Alice"); | 2540 SimulateUsernameChange("Alice"); |
| 2536 | 2541 |
| 2537 // Hide form elements to simulate successful login. | 2542 // Hide form elements to simulate successful login. |
| 2538 username_element_.SetAttribute("style", "display:none;"); | 2543 username_element_.SetAttribute("style", "display:none;"); |
| 2539 password_element_.SetAttribute("style", "display:none;"); | 2544 password_element_.SetAttribute("style", "display:none;"); |
| 2540 | 2545 |
| 2541 password_autofill_agent_->AJAXSucceeded(); | 2546 password_autofill_agent_->AJAXSucceeded(); |
| 2542 | 2547 |
| 2543 ExpectInPageNavigationWithUsernameAndPasswords("Alice", "mypassword", ""); | 2548 ExpectInPageNavigationWithUsernameAndPasswords( |
| 2549 "Alice", "mypassword", "", PasswordForm::SubmissionEvent::XHR_SUCCEEDED); |
| 2544 } | 2550 } |
| 2545 | 2551 |
| 2546 TEST_F(PasswordAutofillAgentTest, | 2552 TEST_F(PasswordAutofillAgentTest, |
| 2547 UsernameChangedAfterPasswordInput_FormSubmitted) { | 2553 UsernameChangedAfterPasswordInput_FormSubmitted) { |
| 2548 SimulateUsernameChange("Bob"); | 2554 SimulateUsernameChange("Bob"); |
| 2549 SimulatePasswordChange("mypassword"); | 2555 SimulatePasswordChange("mypassword"); |
| 2550 SimulateUsernameChange("Alice"); | 2556 SimulateUsernameChange("Alice"); |
| 2551 | 2557 |
| 2552 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) | 2558 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) |
| 2553 ->WillSendSubmitEvent(username_element_.Form()); | 2559 ->WillSendSubmitEvent(username_element_.Form()); |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2695 | 2701 |
| 2696 // Simulate that JavaScript removes the submitted form from DOM. That means | 2702 // Simulate that JavaScript removes the submitted form from DOM. That means |
| 2697 // that a submission was successful. | 2703 // that a submission was successful. |
| 2698 std::string remove_form = | 2704 std::string remove_form = |
| 2699 "var form = document.getElementById('LoginTestForm');" | 2705 "var form = document.getElementById('LoginTestForm');" |
| 2700 "form.parentNode.removeChild(form);"; | 2706 "form.parentNode.removeChild(form);"; |
| 2701 ExecuteJavaScriptForTests(remove_form.c_str()); | 2707 ExecuteJavaScriptForTests(remove_form.c_str()); |
| 2702 | 2708 |
| 2703 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) | 2709 static_cast<content::RenderFrameObserver*>(password_autofill_agent_) |
| 2704 ->DidCommitProvisionalLoad(false, true); | 2710 ->DidCommitProvisionalLoad(false, true); |
| 2705 base::RunLoop().RunUntilIdle(); | 2711 ExpectInPageNavigationWithUsernameAndPasswords( |
| 2706 | 2712 std::string(), "random", std::string(), |
| 2707 // Chect that the form was submitted with in-page navigation. | 2713 PasswordForm::SubmissionEvent::INPAGE_NAVIGATION); |
| 2708 EXPECT_TRUE(fake_driver_.called_inpage_navigation()); | |
| 2709 EXPECT_EQ(base::string16(), | |
| 2710 fake_driver_.password_form_inpage_navigation()->username_value); | |
| 2711 EXPECT_EQ(ASCIIToUTF16("random"), | |
| 2712 fake_driver_.password_form_inpage_navigation()->password_value); | |
| 2713 } | 2714 } |
| 2714 | 2715 |
| 2715 #if defined(SAFE_BROWSING_DB_LOCAL) | 2716 #if defined(SAFE_BROWSING_DB_LOCAL) |
| 2716 // Verify CheckSafeBrowsingReputation() is called when user starts filling | 2717 // Verify CheckSafeBrowsingReputation() is called when user starts filling |
| 2717 // username or password field, and that this function is only called once. | 2718 // username or password field, and that this function is only called once. |
| 2718 TEST_F(PasswordAutofillAgentTest, | 2719 TEST_F(PasswordAutofillAgentTest, |
| 2719 CheckSafeBrowsingReputationWhenUserStartsFillingUsernamePassword) { | 2720 CheckSafeBrowsingReputationWhenUserStartsFillingUsernamePassword) { |
| 2720 ASSERT_EQ(0, fake_driver_.called_check_safe_browsing_reputation_cnt()); | 2721 ASSERT_EQ(0, fake_driver_.called_check_safe_browsing_reputation_cnt()); |
| 2721 // Simulate a click on password field to set its on focus, | 2722 // Simulate a click on password field to set its on focus, |
| 2722 // CheckSafeBrowsingReputation() should be called. | 2723 // CheckSafeBrowsingReputation() should be called. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 2734 | 2735 |
| 2735 // Navigate to another page and click on username field, | 2736 // Navigate to another page and click on username field, |
| 2736 // CheckSafeBrowsingReputation() should be triggered again. | 2737 // CheckSafeBrowsingReputation() should be triggered again. |
| 2737 LoadHTML(kFormHTML); | 2738 LoadHTML(kFormHTML); |
| 2738 SimulateElementClick(kUsernameName); | 2739 SimulateElementClick(kUsernameName); |
| 2739 base::RunLoop().RunUntilIdle(); | 2740 base::RunLoop().RunUntilIdle(); |
| 2740 EXPECT_EQ(2, fake_driver_.called_check_safe_browsing_reputation_cnt()); | 2741 EXPECT_EQ(2, fake_driver_.called_check_safe_browsing_reputation_cnt()); |
| 2741 } | 2742 } |
| 2742 #endif | 2743 #endif |
| 2743 } // namespace autofill | 2744 } // namespace autofill |
| OLD | NEW |