| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "chrome/browser/autofill/personal_data_manager_factory.h" | 19 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
| 20 #include "chrome/browser/infobars/infobar_service.h" | |
| 21 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 23 #include "chrome/browser/ui/browser_navigator_params.h" | 22 #include "chrome/browser/ui/browser_navigator_params.h" |
| 24 #include "chrome/browser/ui/browser_window.h" | 23 #include "chrome/browser/ui/browser_window.h" |
| 25 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 26 #include "chrome/test/base/in_process_browser_test.h" | 25 #include "chrome/test/base/in_process_browser_test.h" |
| 27 #include "chrome/test/base/test_switches.h" | 26 #include "chrome/test/base/test_switches.h" |
| 28 #include "chrome/test/base/ui_test_utils.h" | 27 #include "chrome/test/base/ui_test_utils.h" |
| 29 #include "components/autofill/content/browser/content_autofill_driver.h" | 28 #include "components/autofill/content/browser/content_autofill_driver.h" |
| 30 #include "components/autofill/content/browser/content_autofill_driver_factory.h" | 29 #include "components/autofill/content/browser/content_autofill_driver_factory.h" |
| 31 #include "components/autofill/core/browser/autofill_profile.h" | 30 #include "components/autofill/core/browser/autofill_profile.h" |
| 32 #include "components/autofill/core/browser/autofill_test_utils.h" | 31 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 33 #include "components/autofill/core/browser/credit_card.h" | 32 #include "components/autofill/core/browser/credit_card.h" |
| 34 #include "components/autofill/core/browser/personal_data_manager.h" | 33 #include "components/autofill/core/browser/personal_data_manager.h" |
| 35 #include "components/autofill/core/browser/personal_data_manager_observer.h" | 34 #include "components/autofill/core/browser/personal_data_manager_observer.h" |
| 36 #include "components/autofill/core/browser/validation.h" | 35 #include "components/autofill/core/browser/validation.h" |
| 37 #include "components/infobars/core/confirm_infobar_delegate.h" | |
| 38 #include "components/infobars/core/infobar.h" | |
| 39 #include "components/infobars/core/infobar_manager.h" | |
| 40 #include "content/public/browser/navigation_controller.h" | 36 #include "content/public/browser/navigation_controller.h" |
| 41 #include "content/public/browser/render_view_host.h" | 37 #include "content/public/browser/render_view_host.h" |
| 42 #include "content/public/browser/web_contents.h" | 38 #include "content/public/browser/web_contents.h" |
| 43 #include "content/public/test/browser_test_utils.h" | 39 #include "content/public/test/browser_test_utils.h" |
| 44 #include "content/public/test/test_renderer_host.h" | 40 #include "content/public/test/test_renderer_host.h" |
| 45 #include "content/public/test/test_utils.h" | 41 #include "content/public/test/test_utils.h" |
| 46 #include "net/test/embedded_test_server/embedded_test_server.h" | 42 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 47 #include "net/url_request/test_url_fetcher_factory.h" | 43 #include "net/url_request/test_url_fetcher_factory.h" |
| 48 #include "testing/gmock/include/gmock/gmock.h" | 44 #include "testing/gmock/include/gmock/gmock.h" |
| 49 #include "testing/gtest/include/gtest/gtest.h" | 45 #include "testing/gtest/include/gtest/gtest.h" |
| 50 #include "ui/events/keycodes/keyboard_codes.h" | 46 #include "ui/events/keycodes/keyboard_codes.h" |
| 51 | 47 |
| 52 using base::ASCIIToUTF16; | 48 using base::ASCIIToUTF16; |
| 53 using base::UTF16ToASCII; | 49 using base::UTF16ToASCII; |
| 54 using base::WideToUTF16; | 50 using base::WideToUTF16; |
| 55 | 51 |
| 56 namespace autofill { | 52 namespace autofill { |
| 57 | 53 |
| 58 // Default JavaScript code used to submit the forms. | 54 // Default JavaScript code used to submit the forms. |
| 59 const char kDocumentClickHandlerSubmitJS[] = | 55 const char kDocumentClickHandlerSubmitJS[] = |
| 60 "document.onclick = function() {" | 56 "document.onclick = function() {" |
| 61 " document.getElementById('testform').submit();" | 57 " document.getElementById('testform').submit();" |
| 62 "};"; | 58 "};"; |
| 63 | 59 |
| 64 // TODO(bondd): PdmChangeWaiter in autofill_uitest_util.cc is a replacement for | 60 // TODO(bondd): PdmChangeWaiter in autofill_uitest_util.cc is a replacement for |
| 65 // this class. Remove this class and use helper functions in that file instead. | 61 // this class. Remove this class and use helper functions in that file instead. |
| 66 class WindowedPersonalDataManagerObserver | 62 class WindowedPersonalDataManagerObserver : public PersonalDataManagerObserver { |
| 67 : public PersonalDataManagerObserver, | |
| 68 public infobars::InfoBarManager::Observer { | |
| 69 public: | 63 public: |
| 70 explicit WindowedPersonalDataManagerObserver(Browser* browser) | 64 explicit WindowedPersonalDataManagerObserver(Browser* browser) |
| 71 : alerted_(false), | 65 : alerted_(false), has_run_message_loop_(false), browser_(browser) { |
| 72 has_run_message_loop_(false), | |
| 73 browser_(browser), | |
| 74 infobar_service_(InfoBarService::FromWebContents( | |
| 75 browser_->tab_strip_model()->GetActiveWebContents())) { | |
| 76 PersonalDataManagerFactory::GetForProfile(browser_->profile())-> | 66 PersonalDataManagerFactory::GetForProfile(browser_->profile())-> |
| 77 AddObserver(this); | 67 AddObserver(this); |
| 78 infobar_service_->AddObserver(this); | |
| 79 } | 68 } |
| 80 | 69 |
| 81 ~WindowedPersonalDataManagerObserver() override { | 70 ~WindowedPersonalDataManagerObserver() override {} |
| 82 infobar_service_->RemoveObserver(this); | |
| 83 | |
| 84 if (infobar_service_->infobar_count() > 0) { | |
| 85 infobar_service_->RemoveInfoBar(infobar_service_->infobar_at(0)); | |
| 86 } | |
| 87 } | |
| 88 | 71 |
| 89 void Wait() { | 72 void Wait() { |
| 90 if (!alerted_) { | 73 if (!alerted_) { |
| 91 has_run_message_loop_ = true; | 74 has_run_message_loop_ = true; |
| 92 content::RunMessageLoop(); | 75 content::RunMessageLoop(); |
| 93 } | 76 } |
| 94 PersonalDataManagerFactory::GetForProfile(browser_->profile())-> | 77 PersonalDataManagerFactory::GetForProfile(browser_->profile())-> |
| 95 RemoveObserver(this); | 78 RemoveObserver(this); |
| 96 } | 79 } |
| 97 | 80 |
| 98 // PersonalDataManagerObserver: | 81 // PersonalDataManagerObserver: |
| 99 void OnPersonalDataChanged() override { | 82 void OnPersonalDataChanged() override { |
| 100 if (has_run_message_loop_) { | 83 if (has_run_message_loop_) { |
| 101 base::MessageLoopForUI::current()->QuitWhenIdle(); | 84 base::MessageLoopForUI::current()->QuitWhenIdle(); |
| 102 has_run_message_loop_ = false; | 85 has_run_message_loop_ = false; |
| 103 } | 86 } |
| 104 alerted_ = true; | 87 alerted_ = true; |
| 105 } | 88 } |
| 106 | 89 |
| 107 void OnInsufficientFormData() override { OnPersonalDataChanged(); } | 90 void OnInsufficientFormData() override { OnPersonalDataChanged(); } |
| 108 | 91 |
| 109 // infobars::InfoBarManager::Observer: | |
| 110 void OnInfoBarAdded(infobars::InfoBar* infobar) override { | |
| 111 ConfirmInfoBarDelegate* infobar_delegate = | |
| 112 infobar_service_->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate(); | |
| 113 ASSERT_TRUE(infobar_delegate); | |
| 114 infobar_delegate->Accept(); | |
| 115 } | |
| 116 | |
| 117 private: | 92 private: |
| 118 bool alerted_; | 93 bool alerted_; |
| 119 bool has_run_message_loop_; | 94 bool has_run_message_loop_; |
| 120 Browser* browser_; | 95 Browser* browser_; |
| 121 InfoBarService* infobar_service_; | |
| 122 }; | 96 }; |
| 123 | 97 |
| 124 class AutofillTest : public InProcessBrowserTest { | 98 class AutofillTest : public InProcessBrowserTest { |
| 125 protected: | 99 protected: |
| 126 AutofillTest() {} | 100 AutofillTest() {} |
| 127 | 101 |
| 128 void SetUpOnMainThread() override { | 102 void SetUpOnMainThread() override { |
| 129 // Don't want Keychain coming up on Mac. | 103 // Don't want Keychain coming up on Mac. |
| 130 test::DisableSystemServices(browser()->profile()->GetPrefs()); | 104 test::DisableSystemServices(browser()->profile()->GetPrefs()); |
| 131 | 105 |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 // TODO(isherman): rewrite as WebUI test? | 481 // TODO(isherman): rewrite as WebUI test? |
| 508 IN_PROC_BROWSER_TEST_F(AutofillTest, PrefsStringSavedAsIs) { | 482 IN_PROC_BROWSER_TEST_F(AutofillTest, PrefsStringSavedAsIs) { |
| 509 CreditCard card; | 483 CreditCard card; |
| 510 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("Not_0123-5Checked")); | 484 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("Not_0123-5Checked")); |
| 511 SetCard(card); | 485 SetCard(card); |
| 512 | 486 |
| 513 ASSERT_EQ(1u, personal_data_manager()->GetCreditCards().size()); | 487 ASSERT_EQ(1u, personal_data_manager()->GetCreditCards().size()); |
| 514 ASSERT_EQ(card, *personal_data_manager()->GetCreditCards()[0]); | 488 ASSERT_EQ(card, *personal_data_manager()->GetCreditCards()[0]); |
| 515 } | 489 } |
| 516 | 490 |
| 517 // Test credit card info with an invalid number is not aggregated. | |
| 518 // When filling out a form with an invalid credit card number (one that does not | |
| 519 // pass the Luhn test) the credit card info should not be saved into Autofill | |
| 520 // preferences. | |
| 521 IN_PROC_BROWSER_TEST_F(AutofillTest, InvalidCreditCardNumberIsNotAggregated) { | |
| 522 #if defined(OS_WIN) && defined(USE_ASH) | |
| 523 // Disable this test in Metro+Ash for now (http://crbug.com/262796). | |
| 524 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 525 switches::kAshBrowserTests)) | |
| 526 return; | |
| 527 #endif | |
| 528 | |
| 529 std::string card("4408 0412 3456 7890"); | |
| 530 ASSERT_FALSE(autofill::IsValidCreditCardNumber(ASCIIToUTF16(card))); | |
| 531 SubmitCreditCard("Bob Smith", card.c_str(), "12", "2014"); | |
| 532 InfoBarService* infobar_service = InfoBarService::FromWebContents( | |
| 533 browser()->tab_strip_model()->GetActiveWebContents()); | |
| 534 ASSERT_EQ(0u, infobar_service->infobar_count()); | |
| 535 } | |
| 536 | |
| 537 // Test whitespaces and separator chars are stripped for valid CC numbers. | |
| 538 // The credit card numbers used in this test pass the Luhn test. For reference: | |
| 539 // http://www.merriampark.com/anatomycc.htm | |
| 540 IN_PROC_BROWSER_TEST_F(AutofillTest, | |
| 541 WhitespacesAndSeparatorCharsStrippedForValidCCNums) { | |
| 542 #if defined(OS_WIN) && defined(USE_ASH) | |
| 543 // Disable this test in Metro+Ash for now (http://crbug.com/262796). | |
| 544 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 545 switches::kAshBrowserTests)) | |
| 546 return; | |
| 547 #endif | |
| 548 | |
| 549 SubmitCreditCard("Bob Smith", "4408 0412 3456 7893", "12", "2014"); | |
| 550 SubmitCreditCard("Jane Doe", "4417-1234-5678-9113", "10", "2013"); | |
| 551 | |
| 552 ASSERT_EQ(2u, personal_data_manager()->GetCreditCards().size()); | |
| 553 base::string16 cc1 = personal_data_manager()->GetCreditCards()[0]->GetRawInfo( | |
| 554 CREDIT_CARD_NUMBER); | |
| 555 ASSERT_TRUE(autofill::IsValidCreditCardNumber(cc1)); | |
| 556 base::string16 cc2 = personal_data_manager()->GetCreditCards()[1]->GetRawInfo( | |
| 557 CREDIT_CARD_NUMBER); | |
| 558 ASSERT_TRUE(autofill::IsValidCreditCardNumber(cc2)); | |
| 559 } | |
| 560 | |
| 561 // Test that Autofill aggregates a minimum valid profile. | 491 // Test that Autofill aggregates a minimum valid profile. |
| 562 // The minimum required address fields must be specified: First Name, Last Name, | 492 // The minimum required address fields must be specified: First Name, Last Name, |
| 563 // Address Line 1, City, Zip Code, and State. | 493 // Address Line 1, City, Zip Code, and State. |
| 564 IN_PROC_BROWSER_TEST_F(AutofillTest, AggregatesMinValidProfile) { | 494 IN_PROC_BROWSER_TEST_F(AutofillTest, AggregatesMinValidProfile) { |
| 565 FormMap data; | 495 FormMap data; |
| 566 data["NAME_FIRST"] = "Bob"; | 496 data["NAME_FIRST"] = "Bob"; |
| 567 data["NAME_LAST"] = "Smith"; | 497 data["NAME_LAST"] = "Smith"; |
| 568 data["ADDRESS_HOME_LINE1"] = "1234 H St."; | 498 data["ADDRESS_HOME_LINE1"] = "1234 H St."; |
| 569 data["ADDRESS_HOME_CITY"] = "Mountain View"; | 499 data["ADDRESS_HOME_CITY"] = "Mountain View"; |
| 570 data["ADDRESS_HOME_STATE"] = "CA"; | 500 data["ADDRESS_HOME_STATE"] = "CA"; |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 else if (name == "Jane") | 784 else if (name == "Jane") |
| 855 expectation = "+447624123456"; | 785 expectation = "+447624123456"; |
| 856 else if (name == "Bob") | 786 else if (name == "Bob") |
| 857 expectation = "14088714567"; | 787 expectation = "14088714567"; |
| 858 | 788 |
| 859 EXPECT_EQ(ASCIIToUTF16(expectation), | 789 EXPECT_EQ(ASCIIToUTF16(expectation), |
| 860 profile->GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), "")); | 790 profile->GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), "")); |
| 861 } | 791 } |
| 862 } | 792 } |
| 863 | 793 |
| 864 // Test CC info not offered to be saved when autocomplete=off for CC field. | |
| 865 // If the credit card number field has autocomplete turned off, then the credit | |
| 866 // card infobar should not offer to save the credit card info. The credit card | |
| 867 // number must be a valid Luhn number. | |
| 868 IN_PROC_BROWSER_TEST_F(AutofillTest, CCInfoNotStoredWhenAutocompleteOff) { | |
| 869 #if defined(OS_WIN) && defined(USE_ASH) | |
| 870 // Disable this test in Metro+Ash for now (http://crbug.com/262796). | |
| 871 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 872 switches::kAshBrowserTests)) | |
| 873 return; | |
| 874 #endif | |
| 875 | |
| 876 FormMap data; | |
| 877 data["CREDIT_CARD_NAME"] = "Bob Smith"; | |
| 878 data["CREDIT_CARD_NUMBER"] = "4408041234567893"; | |
| 879 data["CREDIT_CARD_EXP_MONTH"] = "12"; | |
| 880 data["CREDIT_CARD_EXP_4_DIGIT_YEAR"] = "2014"; | |
| 881 FillFormAndSubmit("cc_autocomplete_off_test.html", data); | |
| 882 | |
| 883 InfoBarService* infobar_service = InfoBarService::FromWebContents( | |
| 884 browser()->tab_strip_model()->GetActiveWebContents()); | |
| 885 ASSERT_EQ(0u, infobar_service->infobar_count()); | |
| 886 } | |
| 887 | |
| 888 // Test profile not aggregated if email found in non-email field. | 794 // Test profile not aggregated if email found in non-email field. |
| 889 IN_PROC_BROWSER_TEST_F(AutofillTest, ProfileWithEmailInOtherFieldNotSaved) { | 795 IN_PROC_BROWSER_TEST_F(AutofillTest, ProfileWithEmailInOtherFieldNotSaved) { |
| 890 FormMap data; | 796 FormMap data; |
| 891 data["NAME_FIRST"] = "Bob"; | 797 data["NAME_FIRST"] = "Bob"; |
| 892 data["NAME_LAST"] = "Smith"; | 798 data["NAME_LAST"] = "Smith"; |
| 893 data["ADDRESS_HOME_LINE1"] = "bsmith@gmail.com"; | 799 data["ADDRESS_HOME_LINE1"] = "bsmith@gmail.com"; |
| 894 data["ADDRESS_HOME_CITY"] = "San Jose"; | 800 data["ADDRESS_HOME_CITY"] = "San Jose"; |
| 895 data["ADDRESS_HOME_STATE"] = "CA"; | 801 data["ADDRESS_HOME_STATE"] = "CA"; |
| 896 data["ADDRESS_HOME_ZIP"] = "95110"; | 802 data["ADDRESS_HOME_ZIP"] = "95110"; |
| 897 data["COMPANY_NAME"] = "Company X"; | 803 data["COMPANY_NAME"] = "Company X"; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 932 IN_PROC_BROWSER_TEST_F(AutofillTest, | 838 IN_PROC_BROWSER_TEST_F(AutofillTest, |
| 933 DISABLED_MergeAggregatedDuplicatedProfiles) { | 839 DISABLED_MergeAggregatedDuplicatedProfiles) { |
| 934 int num_of_profiles = | 840 int num_of_profiles = |
| 935 AggregateProfilesIntoAutofillPrefs("dataset_duplicated_profiles.txt"); | 841 AggregateProfilesIntoAutofillPrefs("dataset_duplicated_profiles.txt"); |
| 936 | 842 |
| 937 ASSERT_GT(num_of_profiles, | 843 ASSERT_GT(num_of_profiles, |
| 938 static_cast<int>(personal_data_manager()->GetProfiles().size())); | 844 static_cast<int>(personal_data_manager()->GetProfiles().size())); |
| 939 } | 845 } |
| 940 | 846 |
| 941 } // namespace autofill | 847 } // namespace autofill |
| OLD | NEW |