| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/core/browser/personal_data_manager.h" | 5 #include "components/autofill/core/browser/personal_data_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <list> | 10 #include <list> |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "base/metrics/field_trial.h" | 21 #include "base/metrics/field_trial.h" |
| 22 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
| 24 #include "base/synchronization/waitable_event.h" | 24 #include "base/synchronization/waitable_event.h" |
| 25 #include "base/test/histogram_tester.h" | 25 #include "base/test/histogram_tester.h" |
| 26 #include "base/test/simple_test_clock.h" | 26 #include "base/test/simple_test_clock.h" |
| 27 #include "base/threading/thread_task_runner_handle.h" | 27 #include "base/threading/thread_task_runner_handle.h" |
| 28 #include "base/time/time.h" | 28 #include "base/time/time.h" |
| 29 #include "build/build_config.h" | 29 #include "build/build_config.h" |
| 30 #include "components/autofill/core/browser/autofill_experiments.h" | 30 #include "components/autofill/core/browser/autofill_experiments.h" |
| 31 #include "components/autofill/core/browser/autofill_metrics.h" |
| 31 #include "components/autofill/core/browser/autofill_profile.h" | 32 #include "components/autofill/core/browser/autofill_profile.h" |
| 32 #include "components/autofill/core/browser/autofill_test_utils.h" | 33 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 33 #include "components/autofill/core/browser/field_types.h" | 34 #include "components/autofill/core/browser/field_types.h" |
| 34 #include "components/autofill/core/browser/form_structure.h" | 35 #include "components/autofill/core/browser/form_structure.h" |
| 35 #include "components/autofill/core/browser/personal_data_manager_observer.h" | 36 #include "components/autofill/core/browser/personal_data_manager_observer.h" |
| 36 #include "components/autofill/core/browser/test_autofill_clock.h" | 37 #include "components/autofill/core/browser/test_autofill_clock.h" |
| 37 #include "components/autofill/core/browser/webdata/autofill_table.h" | 38 #include "components/autofill/core/browser/webdata/autofill_table.h" |
| 38 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 39 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
| 39 #include "components/autofill/core/common/autofill_clock.h" | 40 #include "components/autofill/core/common/autofill_clock.h" |
| 40 #include "components/autofill/core/common/autofill_constants.h" | 41 #include "components/autofill/core/common/autofill_constants.h" |
| (...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles(); | 443 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles(); |
| 443 ASSERT_EQ(1U, results.size()); | 444 ASSERT_EQ(1U, results.size()); |
| 444 EXPECT_EQ(0, profile.Compare(*results[0])); | 445 EXPECT_EQ(0, profile.Compare(*results[0])); |
| 445 | 446 |
| 446 // Make sure the use count and use date were set. | 447 // Make sure the use count and use date were set. |
| 447 EXPECT_EQ(1U, results[0]->use_count()); | 448 EXPECT_EQ(1U, results[0]->use_count()); |
| 448 EXPECT_EQ(kArbitraryTime, results[0]->use_date()); | 449 EXPECT_EQ(kArbitraryTime, results[0]->use_date()); |
| 449 EXPECT_EQ(kArbitraryTime, results[0]->modification_date()); | 450 EXPECT_EQ(kArbitraryTime, results[0]->modification_date()); |
| 450 } | 451 } |
| 451 | 452 |
| 452 TEST_F(PersonalDataManagerTest, DontDuplicateServerProfile) { | |
| 453 EnableWalletCardImport(); | |
| 454 | |
| 455 std::vector<AutofillProfile> server_profiles; | |
| 456 server_profiles.push_back( | |
| 457 AutofillProfile(AutofillProfile::SERVER_PROFILE, "a123")); | |
| 458 test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe", "", | |
| 459 "ACME Corp", "500 Oak View", "Apt 8", "Houston", "TX", | |
| 460 "77401", "US", ""); | |
| 461 // Wallet only provides a full name, so the above first and last names | |
| 462 // will be ignored when the profile is written to the DB. | |
| 463 server_profiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe")); | |
| 464 autofill_table_->SetServerProfiles(server_profiles); | |
| 465 personal_data_->Refresh(); | |
| 466 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) | |
| 467 .WillOnce(QuitMainMessageLoop()); | |
| 468 base::RunLoop().Run(); | |
| 469 EXPECT_EQ(1U, personal_data_->GetProfiles().size()); | |
| 470 | |
| 471 // Add profile with identical values. Duplicates should not get saved. | |
| 472 AutofillProfile scraped_profile(base::GenerateGUID(), | |
| 473 "https://www.example.com"); | |
| 474 test::SetProfileInfo(&scraped_profile, "John", "", "Doe", "", "ACME Corp", | |
| 475 "500 Oak View", "Apt 8", "Houston", "TX", "77401", "US", | |
| 476 ""); | |
| 477 EXPECT_TRUE(scraped_profile.IsSubsetOf(server_profiles.back(), "en-US")); | |
| 478 | |
| 479 // Create the test clock and set the time to a specific value. | |
| 480 TestAutofillClock test_clock; | |
| 481 test_clock.SetNow(kArbitraryTime); | |
| 482 | |
| 483 std::string saved_guid = personal_data_->SaveImportedProfile(scraped_profile); | |
| 484 EXPECT_NE(scraped_profile.guid(), saved_guid); | |
| 485 | |
| 486 personal_data_->Refresh(); | |
| 487 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) | |
| 488 .WillOnce(QuitMainMessageLoop()); | |
| 489 base::RunLoop().Run(); | |
| 490 | |
| 491 // Verify the non-addition. | |
| 492 EXPECT_EQ(0U, personal_data_->web_profiles().size()); | |
| 493 ASSERT_EQ(1U, personal_data_->GetProfiles().size()); | |
| 494 | |
| 495 // Verify that the server profile's use date was updated with the specified | |
| 496 // value. | |
| 497 const AutofillProfile* server_profile = personal_data_->GetProfiles()[0]; | |
| 498 EXPECT_EQ(kArbitraryTime, server_profile->use_date()); | |
| 499 } | |
| 500 | |
| 501 // Tests that SaveImportedProfile sets the modification date on new profiles. | 453 // Tests that SaveImportedProfile sets the modification date on new profiles. |
| 502 TEST_F(PersonalDataManagerTest, SaveImportedProfileSetModificationDate) { | 454 TEST_F(PersonalDataManagerTest, SaveImportedProfileSetModificationDate) { |
| 503 AutofillProfile profile(test::GetFullProfile()); | 455 AutofillProfile profile(test::GetFullProfile()); |
| 504 EXPECT_NE(base::Time(), profile.modification_date()); | 456 EXPECT_NE(base::Time(), profile.modification_date()); |
| 505 | 457 |
| 506 personal_data_->SaveImportedProfile(profile); | 458 personal_data_->SaveImportedProfile(profile); |
| 507 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles(); | 459 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles(); |
| 508 ASSERT_EQ(1U, profiles.size()); | 460 ASSERT_EQ(1U, profiles.size()); |
| 509 EXPECT_GT(base::TimeDelta::FromMilliseconds(500), | 461 EXPECT_GT(base::TimeDelta::FromMilliseconds(500), |
| 510 AutofillClock::Now() - profiles[0]->modification_date()); | 462 AutofillClock::Now() - profiles[0]->modification_date()); |
| (...skipping 3646 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4157 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123")); | 4109 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123")); |
| 4158 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", | 4110 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", |
| 4159 "9012" /* Visa */, "01", "2999"); | 4111 "9012" /* Visa */, "01", "2999"); |
| 4160 server_cards.back().SetTypeForMaskedCard(kVisaCard); | 4112 server_cards.back().SetTypeForMaskedCard(kVisaCard); |
| 4161 test::SetServerCreditCards(autofill_table_, server_cards); | 4113 test::SetServerCreditCards(autofill_table_, server_cards); |
| 4162 personal_data_->Refresh(); | 4114 personal_data_->Refresh(); |
| 4163 | 4115 |
| 4164 // Need to set the google services username | 4116 // Need to set the google services username |
| 4165 EnableWalletCardImport(); | 4117 EnableWalletCardImport(); |
| 4166 | 4118 |
| 4167 // Add a server profile. | |
| 4168 std::vector<AutofillProfile> server_profiles; | |
| 4169 server_profiles.push_back( | |
| 4170 AutofillProfile(AutofillProfile::SERVER_PROFILE, "a123")); | |
| 4171 test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe", "", | |
| 4172 "ACME Corp", "500 Oak View", "Apt 8", "Houston", "TX", | |
| 4173 "77401", "US", ""); | |
| 4174 autofill_table_->SetServerProfiles(server_profiles); | |
| 4175 | |
| 4176 // The card and profile should be there. | 4119 // The card and profile should be there. |
| 4177 ResetPersonalDataManager(USER_MODE_NORMAL); | 4120 ResetPersonalDataManager(USER_MODE_NORMAL); |
| 4178 EXPECT_FALSE(personal_data_->GetCreditCards().empty()); | 4121 EXPECT_FALSE(personal_data_->GetCreditCards().empty()); |
| 4179 EXPECT_FALSE(personal_data_->GetProfiles().empty()); | |
| 4180 | 4122 |
| 4181 personal_data_->ClearAllServerData(); | 4123 personal_data_->ClearAllServerData(); |
| 4182 | 4124 |
| 4183 // Reload the database, everything should be gone. | 4125 // Reload the database, everything should be gone. |
| 4184 ResetPersonalDataManager(USER_MODE_NORMAL); | 4126 ResetPersonalDataManager(USER_MODE_NORMAL); |
| 4185 EXPECT_TRUE(personal_data_->GetCreditCards().empty()); | 4127 EXPECT_TRUE(personal_data_->GetCreditCards().empty()); |
| 4186 EXPECT_TRUE(personal_data_->GetProfiles().empty()); | |
| 4187 } | 4128 } |
| 4188 | 4129 |
| 4189 TEST_F(PersonalDataManagerTest, DontDuplicateServerCard) { | 4130 TEST_F(PersonalDataManagerTest, DontDuplicateServerCard) { |
| 4190 EnableWalletCardImport(); | 4131 EnableWalletCardImport(); |
| 4191 | 4132 |
| 4192 std::vector<CreditCard> server_cards; | 4133 std::vector<CreditCard> server_cards; |
| 4193 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123")); | 4134 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123")); |
| 4194 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", | 4135 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", |
| 4195 "1881" /* Visa */, "01", "2999"); | 4136 "1881" /* Visa */, "01", "2999"); |
| 4196 server_cards.back().SetTypeForMaskedCard(kVisaCard); | 4137 server_cards.back().SetTypeForMaskedCard(kVisaCard); |
| (...skipping 1497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5694 // Re-enable the profile cleanup now that the profile was added. | 5635 // Re-enable the profile cleanup now that the profile was added. |
| 5695 EnableAutofillProfileCleanup(); | 5636 EnableAutofillProfileCleanup(); |
| 5696 | 5637 |
| 5697 // The deduping routine should not be run. | 5638 // The deduping routine should not be run. |
| 5698 EXPECT_FALSE(personal_data_->ApplyDedupingRoutine()); | 5639 EXPECT_FALSE(personal_data_->ApplyDedupingRoutine()); |
| 5699 | 5640 |
| 5700 // The two duplicate profiles should still be present. | 5641 // The two duplicate profiles should still be present. |
| 5701 EXPECT_EQ(2U, personal_data_->GetProfiles().size()); | 5642 EXPECT_EQ(2U, personal_data_->GetProfiles().size()); |
| 5702 } | 5643 } |
| 5703 | 5644 |
| 5645 // Tests that a new local profile is created if no existing one is a duplicate |
| 5646 // of the server address. Also tests that the billing address relationship was |
| 5647 // transferred to the converted address. |
| 5648 TEST_F(PersonalDataManagerTest, |
| 5649 ConvertWalletAddressesToLocalProfiles_NewProfile) { |
| 5650 /////////////////////////////////////////////////////////////////////// |
| 5651 // Setup. |
| 5652 /////////////////////////////////////////////////////////////////////// |
| 5653 EnableWalletCardImport(); |
| 5654 base::HistogramTester histogram_tester; |
| 5655 const std::string kServerAddressId("server_address1"); |
| 5656 |
| 5657 // Add two different profiles, a local and a server one. Set the use stats so |
| 5658 // the server profile has a higher frecency, to have a predictable ordering to |
| 5659 // validate results. |
| 5660 AutofillProfile local_profile(base::GenerateGUID(), |
| 5661 "https://www.example.com"); |
| 5662 test::SetProfileInfo(&local_profile, "Josephine", "Alicia", "Saenz", |
| 5663 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", |
| 5664 "Orlando", "FL", "32801", "US", "19482937549"); |
| 5665 local_profile.set_use_count(1); |
| 5666 personal_data_->AddProfile(local_profile); |
| 5667 |
| 5668 // Add a different server profile. |
| 5669 std::vector<AutofillProfile> GetServerProfiles; |
| 5670 GetServerProfiles.push_back( |
| 5671 AutofillProfile(AutofillProfile::SERVER_PROFILE, kServerAddressId)); |
| 5672 test::SetProfileInfo(&GetServerProfiles.back(), "John", "", "Doe", "", |
| 5673 "ACME Corp", "500 Oak View", "Apt 8", "Houston", "TX", |
| 5674 "77401", "US", ""); |
| 5675 // Wallet only provides a full name, so the above first and last names |
| 5676 // will be ignored when the profile is written to the DB. |
| 5677 GetServerProfiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe")); |
| 5678 GetServerProfiles.back().set_use_count(100); |
| 5679 autofill_table_->SetServerProfiles(GetServerProfiles); |
| 5680 |
| 5681 // Add a server and a local card that have the server address as billing |
| 5682 // address. |
| 5683 CreditCard local_card("287151C8-6AB1-487C-9095-28E80BE5DA15", |
| 5684 "https://www.example.com"); |
| 5685 test::SetCreditCardInfo(&local_card, "Clyde Barrow", |
| 5686 "347666888555" /* American Express */, "04", "2999"); |
| 5687 local_card.set_billing_address_id(kServerAddressId); |
| 5688 personal_data_->AddCreditCard(local_card); |
| 5689 |
| 5690 std::vector<CreditCard> server_cards; |
| 5691 server_cards.push_back( |
| 5692 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_card1")); |
| 5693 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", |
| 5694 "1111" /* Visa */, "01", "2999"); |
| 5695 server_cards.back().SetTypeForMaskedCard(kVisaCard); |
| 5696 server_cards.back().set_billing_address_id(kServerAddressId); |
| 5697 test::SetServerCreditCards(autofill_table_, server_cards); |
| 5698 |
| 5699 // Make sure everything is setup correctly. |
| 5700 personal_data_->Refresh(); |
| 5701 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5702 .WillOnce(QuitMainMessageLoop()); |
| 5703 base::RunLoop().Run(); |
| 5704 EXPECT_EQ(1U, personal_data_->web_profiles().size()); |
| 5705 EXPECT_EQ(1U, personal_data_->GetServerProfiles().size()); |
| 5706 EXPECT_EQ(2U, personal_data_->GetCreditCards().size()); |
| 5707 |
| 5708 /////////////////////////////////////////////////////////////////////// |
| 5709 // Tested method. |
| 5710 /////////////////////////////////////////////////////////////////////// |
| 5711 personal_data_->ConvertWalletAddressesToLocalProfiles(); |
| 5712 |
| 5713 /////////////////////////////////////////////////////////////////////// |
| 5714 // Validation. |
| 5715 /////////////////////////////////////////////////////////////////////// |
| 5716 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5717 .WillOnce(QuitMainMessageLoop()); |
| 5718 base::RunLoop().Run(); |
| 5719 |
| 5720 // The Wallet address should have been added as a new local profile. |
| 5721 EXPECT_EQ(2U, personal_data_->web_profiles().size()); |
| 5722 EXPECT_EQ(1U, personal_data_->GetServerProfiles().size()); |
| 5723 histogram_tester.ExpectUniqueSample("Autofill.WalletAddressConversionType", |
| 5724 AutofillMetrics::CONVERTED_ADDRESS_ADDED, |
| 5725 1); |
| 5726 |
| 5727 // The conversion should be recorded in the Wallet address. |
| 5728 EXPECT_TRUE(personal_data_->GetServerProfiles().back()->has_converted()); |
| 5729 |
| 5730 // Get the profiles, sorted by frecency to have a deterministic order. |
| 5731 std::vector<AutofillProfile*> profiles = |
| 5732 personal_data_->GetProfilesToSuggest(); |
| 5733 |
| 5734 // Make sure that the two profiles have not merged. |
| 5735 ASSERT_EQ(2U, profiles.size()); |
| 5736 EXPECT_EQ(UTF8ToUTF16("John"), profiles[0]->GetRawInfo(NAME_FIRST)); |
| 5737 EXPECT_EQ(local_profile, *profiles[1]); |
| 5738 |
| 5739 // Make sure that the billing address id of the two cards now point to the |
| 5740 // converted profile. |
| 5741 EXPECT_EQ(profiles[0]->guid(), |
| 5742 personal_data_->GetCreditCards()[0]->billing_address_id()); |
| 5743 EXPECT_EQ(profiles[0]->guid(), |
| 5744 personal_data_->GetCreditCards()[1]->billing_address_id()); |
| 5745 } |
| 5746 |
| 5747 // Tests that the converted wallet address is merged into an existing local |
| 5748 // profile if they are considered equivalent. Also tests that the billing |
| 5749 // address relationship was transferred to the converted address. |
| 5750 TEST_F(PersonalDataManagerTest, |
| 5751 ConvertWalletAddressesToLocalProfiles_MergedProfile) { |
| 5752 /////////////////////////////////////////////////////////////////////// |
| 5753 // Setup. |
| 5754 /////////////////////////////////////////////////////////////////////// |
| 5755 EnableWalletCardImport(); |
| 5756 base::HistogramTester histogram_tester; |
| 5757 const std::string kServerAddressId("server_address1"); |
| 5758 |
| 5759 // Add two similar profile, a local and a server one. Set the use stats so |
| 5760 // the server card has a higher frecency, to have a predicatble ordering to |
| 5761 // validate results. |
| 5762 // Add a local profile. |
| 5763 AutofillProfile local_profile(base::GenerateGUID(), |
| 5764 "https://www.example.com"); |
| 5765 test::SetProfileInfo(&local_profile, "John", "", "Doe", "john@doe.com", "", |
| 5766 "1212 Center.", "Bld. 5", "Orlando", "FL", "32801", "US", |
| 5767 "19482937549"); |
| 5768 local_profile.set_use_count(1); |
| 5769 personal_data_->AddProfile(local_profile); |
| 5770 |
| 5771 // Add a different server profile. |
| 5772 std::vector<AutofillProfile> GetServerProfiles; |
| 5773 GetServerProfiles.push_back( |
| 5774 AutofillProfile(AutofillProfile::SERVER_PROFILE, kServerAddressId)); |
| 5775 test::SetProfileInfo(&GetServerProfiles.back(), "John", "", "Doe", "", "Fox", |
| 5776 "1212 Center", "Bld. 5", "Orlando", "FL", "", "US", ""); |
| 5777 // Wallet only provides a full name, so the above first and last names |
| 5778 // will be ignored when the profile is written to the DB. |
| 5779 GetServerProfiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe")); |
| 5780 GetServerProfiles.back().set_use_count(100); |
| 5781 autofill_table_->SetServerProfiles(GetServerProfiles); |
| 5782 |
| 5783 // Add a server and a local card that have the server address as billing |
| 5784 // address. |
| 5785 CreditCard local_card("287151C8-6AB1-487C-9095-28E80BE5DA15", |
| 5786 "https://www.example.com"); |
| 5787 test::SetCreditCardInfo(&local_card, "Clyde Barrow", |
| 5788 "347666888555" /* American Express */, "04", "2999"); |
| 5789 local_card.set_billing_address_id(kServerAddressId); |
| 5790 personal_data_->AddCreditCard(local_card); |
| 5791 |
| 5792 std::vector<CreditCard> server_cards; |
| 5793 server_cards.push_back( |
| 5794 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_card1")); |
| 5795 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", |
| 5796 "1111" /* Visa */, "01", "2999"); |
| 5797 server_cards.back().SetTypeForMaskedCard(kVisaCard); |
| 5798 server_cards.back().set_billing_address_id(kServerAddressId); |
| 5799 test::SetServerCreditCards(autofill_table_, server_cards); |
| 5800 |
| 5801 // Make sure everything is setup correctly. |
| 5802 personal_data_->Refresh(); |
| 5803 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5804 .WillOnce(QuitMainMessageLoop()); |
| 5805 base::RunLoop().Run(); |
| 5806 EXPECT_EQ(1U, personal_data_->web_profiles().size()); |
| 5807 EXPECT_EQ(1U, personal_data_->GetServerProfiles().size()); |
| 5808 EXPECT_EQ(2U, personal_data_->GetCreditCards().size()); |
| 5809 |
| 5810 /////////////////////////////////////////////////////////////////////// |
| 5811 // Tested method. |
| 5812 /////////////////////////////////////////////////////////////////////// |
| 5813 personal_data_->ConvertWalletAddressesToLocalProfiles(); |
| 5814 |
| 5815 /////////////////////////////////////////////////////////////////////// |
| 5816 // Validation. |
| 5817 /////////////////////////////////////////////////////////////////////// |
| 5818 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5819 .WillOnce(QuitMainMessageLoop()); |
| 5820 base::RunLoop().Run(); |
| 5821 |
| 5822 // The Wallet address should have been merged with the existing local profile. |
| 5823 EXPECT_EQ(1U, personal_data_->web_profiles().size()); |
| 5824 EXPECT_EQ(1U, personal_data_->GetServerProfiles().size()); |
| 5825 histogram_tester.ExpectUniqueSample("Autofill.WalletAddressConversionType", |
| 5826 AutofillMetrics::CONVERTED_ADDRESS_MERGED, |
| 5827 1); |
| 5828 |
| 5829 // The conversion should be recorded in the Wallet address. |
| 5830 EXPECT_TRUE(personal_data_->GetServerProfiles().back()->has_converted()); |
| 5831 |
| 5832 // Get the profiles, sorted by frecency to have a deterministic order. |
| 5833 std::vector<AutofillProfile*> profiles = |
| 5834 personal_data_->GetProfilesToSuggest(); |
| 5835 |
| 5836 // Make sure that the two profiles have merged. |
| 5837 ASSERT_EQ(1U, profiles.size()); |
| 5838 |
| 5839 // Check that the values were merged. |
| 5840 EXPECT_EQ(UTF8ToUTF16("john@doe.com"), |
| 5841 profiles[0]->GetRawInfo(EMAIL_ADDRESS)); |
| 5842 EXPECT_EQ(UTF8ToUTF16("Fox"), profiles[0]->GetRawInfo(COMPANY_NAME)); |
| 5843 EXPECT_EQ(UTF8ToUTF16("32801"), profiles[0]->GetRawInfo(ADDRESS_HOME_ZIP)); |
| 5844 |
| 5845 // Make sure that the billing address id of the two cards now point to the |
| 5846 // converted profile. |
| 5847 EXPECT_EQ(profiles[0]->guid(), |
| 5848 personal_data_->GetCreditCards()[0]->billing_address_id()); |
| 5849 EXPECT_EQ(profiles[0]->guid(), |
| 5850 personal_data_->GetCreditCards()[1]->billing_address_id()); |
| 5851 } |
| 5852 |
| 5853 // Tests that a Wallet address that has already converted does not get converted |
| 5854 // a second time. |
| 5855 TEST_F(PersonalDataManagerTest, |
| 5856 ConvertWalletAddressesToLocalProfiles_AlreadyConverted) { |
| 5857 /////////////////////////////////////////////////////////////////////// |
| 5858 // Setup. |
| 5859 /////////////////////////////////////////////////////////////////////// |
| 5860 EnableWalletCardImport(); |
| 5861 base::HistogramTester histogram_tester; |
| 5862 const std::string kServerAddressId("server_address1"); |
| 5863 |
| 5864 // Add a server profile that has already been converted. |
| 5865 std::vector<AutofillProfile> GetServerProfiles; |
| 5866 GetServerProfiles.push_back( |
| 5867 AutofillProfile(AutofillProfile::SERVER_PROFILE, kServerAddressId)); |
| 5868 test::SetProfileInfo(&GetServerProfiles.back(), "John", "Ray", "Doe", |
| 5869 "john@doe.com", "Fox", "1212 Center", "Bld. 5", |
| 5870 "Orlando", "FL", "32801", "US", ""); |
| 5871 GetServerProfiles.back().set_has_converted(true); |
| 5872 // Wallet only provides a full name, so the above first and last names |
| 5873 // will be ignored when the profile is written to the DB. |
| 5874 autofill_table_->SetServerProfiles(GetServerProfiles); |
| 5875 |
| 5876 // Make sure everything is setup correctly. |
| 5877 personal_data_->Refresh(); |
| 5878 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5879 .WillOnce(QuitMainMessageLoop()); |
| 5880 base::RunLoop().Run(); |
| 5881 EXPECT_EQ(0U, personal_data_->web_profiles().size()); |
| 5882 EXPECT_EQ(1U, personal_data_->GetServerProfiles().size()); |
| 5883 |
| 5884 /////////////////////////////////////////////////////////////////////// |
| 5885 // Tested method. |
| 5886 /////////////////////////////////////////////////////////////////////// |
| 5887 personal_data_->ConvertWalletAddressesToLocalProfiles(); |
| 5888 |
| 5889 /////////////////////////////////////////////////////////////////////// |
| 5890 // Validation. |
| 5891 /////////////////////////////////////////////////////////////////////// |
| 5892 // Since there should be no change in data, OnPersonalDataChanged should not |
| 5893 // have been called. |
| 5894 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(0); |
| 5895 |
| 5896 personal_data_->Refresh(); |
| 5897 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5898 .WillOnce(QuitMainMessageLoop()); |
| 5899 base::RunLoop().Run(); |
| 5900 |
| 5901 // There should be no local profiles added. |
| 5902 EXPECT_EQ(0U, personal_data_->web_profiles().size()); |
| 5903 EXPECT_EQ(1U, personal_data_->GetServerProfiles().size()); |
| 5904 } |
| 5905 |
| 5906 // Tests that when the user has multiple similar Wallet addresses, they get |
| 5907 // merged into a single local profile, and that the billing address relationship |
| 5908 // is merged too. |
| 5909 TEST_F(PersonalDataManagerTest, |
| 5910 ConvertWalletAddressesToLocalProfiles_MultipleSimilarWalletAddresses) { |
| 5911 /////////////////////////////////////////////////////////////////////// |
| 5912 // Setup. |
| 5913 /////////////////////////////////////////////////////////////////////// |
| 5914 EnableWalletCardImport(); |
| 5915 base::HistogramTester histogram_tester; |
| 5916 const std::string kServerAddressId("server_address1"); |
| 5917 const std::string kServerAddressId2("server_address2"); |
| 5918 |
| 5919 // Add a unique local profile and two similar server profiles. Set the use |
| 5920 // stats to have a predicatble ordering to validate results. |
| 5921 // Add a local profile. |
| 5922 AutofillProfile local_profile(base::GenerateGUID(), |
| 5923 "https://www.example.com"); |
| 5924 test::SetProfileInfo(&local_profile, "Bob", "", "Doe", "", "Fox", |
| 5925 "1212 Center.", "Bld. 5", "Orlando", "FL", "32801", "US", |
| 5926 "19482937549"); |
| 5927 local_profile.set_use_count(1); |
| 5928 personal_data_->AddProfile(local_profile); |
| 5929 |
| 5930 // Add a server profile. |
| 5931 std::vector<AutofillProfile> GetServerProfiles; |
| 5932 GetServerProfiles.push_back( |
| 5933 AutofillProfile(AutofillProfile::SERVER_PROFILE, kServerAddressId)); |
| 5934 test::SetProfileInfo(&GetServerProfiles.back(), "John", "", "Doe", "", "", |
| 5935 "1212 Center", "Bld. 5", "Orlando", "FL", "32801", "US", |
| 5936 ""); |
| 5937 // Wallet only provides a full name, so the above first and last names |
| 5938 // will be ignored when the profile is written to the DB. |
| 5939 GetServerProfiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe")); |
| 5940 GetServerProfiles.back().set_use_count(100); |
| 5941 |
| 5942 // Add a similar server profile. |
| 5943 GetServerProfiles.push_back( |
| 5944 AutofillProfile(AutofillProfile::SERVER_PROFILE, kServerAddressId2)); |
| 5945 test::SetProfileInfo(&GetServerProfiles.back(), "John", "", "Doe", |
| 5946 "john@doe.com", "Fox", "1212 Center", "Bld. 5", |
| 5947 "Orlando", "FL", "", "US", ""); |
| 5948 // Wallet only provides a full name, so the above first and last names |
| 5949 // will be ignored when the profile is written to the DB. |
| 5950 GetServerProfiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe")); |
| 5951 GetServerProfiles.back().set_use_count(200); |
| 5952 autofill_table_->SetServerProfiles(GetServerProfiles); |
| 5953 |
| 5954 // Add a server and a local card that have the first and second Wallet address |
| 5955 // as a billing address. |
| 5956 CreditCard local_card("287151C8-6AB1-487C-9095-28E80BE5DA15", |
| 5957 "https://www.example.com"); |
| 5958 test::SetCreditCardInfo(&local_card, "Clyde Barrow", |
| 5959 "347666888555" /* American Express */, "04", "2999"); |
| 5960 local_card.set_billing_address_id(kServerAddressId); |
| 5961 personal_data_->AddCreditCard(local_card); |
| 5962 |
| 5963 std::vector<CreditCard> server_cards; |
| 5964 server_cards.push_back( |
| 5965 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_card1")); |
| 5966 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", |
| 5967 "1111" /* Visa */, "01", "2999"); |
| 5968 server_cards.back().SetTypeForMaskedCard(kVisaCard); |
| 5969 server_cards.back().set_billing_address_id(kServerAddressId2); |
| 5970 test::SetServerCreditCards(autofill_table_, server_cards); |
| 5971 |
| 5972 // Make sure everything is setup correctly. |
| 5973 personal_data_->Refresh(); |
| 5974 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5975 .WillOnce(QuitMainMessageLoop()); |
| 5976 base::RunLoop().Run(); |
| 5977 EXPECT_EQ(1U, personal_data_->web_profiles().size()); |
| 5978 EXPECT_EQ(2U, personal_data_->GetServerProfiles().size()); |
| 5979 EXPECT_EQ(2U, personal_data_->GetCreditCards().size()); |
| 5980 |
| 5981 /////////////////////////////////////////////////////////////////////// |
| 5982 // Tested method. |
| 5983 /////////////////////////////////////////////////////////////////////// |
| 5984 personal_data_->ConvertWalletAddressesToLocalProfiles(); |
| 5985 |
| 5986 /////////////////////////////////////////////////////////////////////// |
| 5987 // Validation. |
| 5988 /////////////////////////////////////////////////////////////////////// |
| 5989 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5990 .WillOnce(QuitMainMessageLoop()); |
| 5991 base::RunLoop().Run(); |
| 5992 |
| 5993 // The first Wallet address should have been added as a new local profile and |
| 5994 // the second one should have merged with the first. |
| 5995 EXPECT_EQ(2U, personal_data_->web_profiles().size()); |
| 5996 EXPECT_EQ(2U, personal_data_->GetServerProfiles().size()); |
| 5997 histogram_tester.ExpectBucketCount("Autofill.WalletAddressConversionType", |
| 5998 AutofillMetrics::CONVERTED_ADDRESS_ADDED, |
| 5999 1); |
| 6000 histogram_tester.ExpectBucketCount("Autofill.WalletAddressConversionType", |
| 6001 AutofillMetrics::CONVERTED_ADDRESS_MERGED, |
| 6002 1); |
| 6003 |
| 6004 // The conversion should be recorded in the Wallet addresses. |
| 6005 EXPECT_TRUE(personal_data_->GetServerProfiles()[0]->has_converted()); |
| 6006 EXPECT_TRUE(personal_data_->GetServerProfiles()[1]->has_converted()); |
| 6007 |
| 6008 // Get the profiles, sorted by frecency to have a deterministic order. |
| 6009 std::vector<AutofillProfile*> profiles = |
| 6010 personal_data_->GetProfilesToSuggest(); |
| 6011 |
| 6012 // Make sure that the two Wallet addresses merged together and were added as |
| 6013 // a new local profile. |
| 6014 ASSERT_EQ(2U, profiles.size()); |
| 6015 EXPECT_EQ(UTF8ToUTF16("John"), profiles[0]->GetRawInfo(NAME_FIRST)); |
| 6016 EXPECT_EQ(local_profile, *profiles[1]); |
| 6017 |
| 6018 // Check that the values were merged. |
| 6019 EXPECT_EQ(UTF8ToUTF16("Fox"), profiles[0]->GetRawInfo(COMPANY_NAME)); |
| 6020 EXPECT_EQ(UTF8ToUTF16("32801"), profiles[0]->GetRawInfo(ADDRESS_HOME_ZIP)); |
| 6021 |
| 6022 // Make sure that the billing address id of the two cards now point to the |
| 6023 // converted profile. |
| 6024 EXPECT_EQ(profiles[0]->guid(), |
| 6025 personal_data_->GetCreditCards()[0]->billing_address_id()); |
| 6026 EXPECT_EQ(profiles[0]->guid(), |
| 6027 personal_data_->GetCreditCards()[1]->billing_address_id()); |
| 6028 } |
| 6029 |
| 5704 } // namespace autofill | 6030 } // namespace autofill |
| OLD | NEW |