| 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> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <utility> | 14 #include <utility> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 18 #include "base/files/scoped_temp_dir.h" | 18 #include "base/files/scoped_temp_dir.h" |
| 19 #include "base/guid.h" | 19 #include "base/guid.h" |
| 20 #include "base/memory/ptr_util.h" | 20 #include "base/memory/ptr_util.h" |
| 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/threading/thread_task_runner_handle.h" | 26 #include "base/threading/thread_task_runner_handle.h" |
| 27 #include "base/time/time.h" | 27 #include "base/time/time.h" |
| 28 #include "build/build_config.h" | 28 #include "build/build_config.h" |
| 29 #include "components/autofill/core/browser/autofill_experiments.h" | 29 #include "components/autofill/core/browser/autofill_experiments.h" |
| 30 #include "components/autofill/core/browser/autofill_metrics.h" |
| 30 #include "components/autofill/core/browser/autofill_profile.h" | 31 #include "components/autofill/core/browser/autofill_profile.h" |
| 31 #include "components/autofill/core/browser/autofill_test_utils.h" | 32 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 32 #include "components/autofill/core/browser/field_types.h" | 33 #include "components/autofill/core/browser/field_types.h" |
| 33 #include "components/autofill/core/browser/form_structure.h" | 34 #include "components/autofill/core/browser/form_structure.h" |
| 34 #include "components/autofill/core/browser/personal_data_manager_observer.h" | 35 #include "components/autofill/core/browser/personal_data_manager_observer.h" |
| 35 #include "components/autofill/core/browser/webdata/autofill_table.h" | 36 #include "components/autofill/core/browser/webdata/autofill_table.h" |
| 36 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 37 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
| 37 #include "components/autofill/core/common/autofill_constants.h" | 38 #include "components/autofill/core/common/autofill_constants.h" |
| 38 #include "components/autofill/core/common/autofill_pref_names.h" | 39 #include "components/autofill/core/common/autofill_pref_names.h" |
| 39 #include "components/autofill/core/common/autofill_switches.h" | 40 #include "components/autofill/core/common/autofill_switches.h" |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles(); | 430 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles(); |
| 430 ASSERT_EQ(1U, results.size()); | 431 ASSERT_EQ(1U, results.size()); |
| 431 EXPECT_EQ(0, profile.Compare(*results[0])); | 432 EXPECT_EQ(0, profile.Compare(*results[0])); |
| 432 | 433 |
| 433 // Make sure the use count and use date were set. | 434 // Make sure the use count and use date were set. |
| 434 EXPECT_EQ(1U, results[0]->use_count()); | 435 EXPECT_EQ(1U, results[0]->use_count()); |
| 435 EXPECT_NE(base::Time(), results[0]->use_date()); | 436 EXPECT_NE(base::Time(), results[0]->use_date()); |
| 436 EXPECT_NE(base::Time(), results[0]->modification_date()); | 437 EXPECT_NE(base::Time(), results[0]->modification_date()); |
| 437 } | 438 } |
| 438 | 439 |
| 439 TEST_F(PersonalDataManagerTest, DontDuplicateServerProfile) { | |
| 440 EnableWalletCardImport(); | |
| 441 | |
| 442 std::vector<AutofillProfile> server_profiles; | |
| 443 server_profiles.push_back( | |
| 444 AutofillProfile(AutofillProfile::SERVER_PROFILE, "a123")); | |
| 445 test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe", "", | |
| 446 "ACME Corp", "500 Oak View", "Apt 8", "Houston", "TX", | |
| 447 "77401", "US", ""); | |
| 448 // Wallet only provides a full name, so the above first and last names | |
| 449 // will be ignored when the profile is written to the DB. | |
| 450 server_profiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe")); | |
| 451 autofill_table_->SetServerProfiles(server_profiles); | |
| 452 personal_data_->Refresh(); | |
| 453 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) | |
| 454 .WillOnce(QuitMainMessageLoop()); | |
| 455 base::RunLoop().Run(); | |
| 456 EXPECT_EQ(1U, personal_data_->GetProfiles().size()); | |
| 457 | |
| 458 // Add profile with identical values. Duplicates should not get saved. | |
| 459 AutofillProfile scraped_profile(base::GenerateGUID(), | |
| 460 "https://www.example.com"); | |
| 461 test::SetProfileInfo(&scraped_profile, "John", "", "Doe", "", "ACME Corp", | |
| 462 "500 Oak View", "Apt 8", "Houston", "TX", "77401", "US", | |
| 463 ""); | |
| 464 EXPECT_TRUE(scraped_profile.IsSubsetOf(server_profiles.back(), "en-US")); | |
| 465 std::string saved_guid = personal_data_->SaveImportedProfile(scraped_profile); | |
| 466 EXPECT_NE(scraped_profile.guid(), saved_guid); | |
| 467 | |
| 468 personal_data_->Refresh(); | |
| 469 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) | |
| 470 .WillOnce(QuitMainMessageLoop()); | |
| 471 base::RunLoop().Run(); | |
| 472 | |
| 473 // Verify the non-addition. | |
| 474 EXPECT_EQ(0U, personal_data_->web_profiles().size()); | |
| 475 ASSERT_EQ(1U, personal_data_->GetProfiles().size()); | |
| 476 | |
| 477 // Verify that the server profile's use date was updated. | |
| 478 const AutofillProfile* server_profile = personal_data_->GetProfiles()[0]; | |
| 479 EXPECT_GT(base::TimeDelta::FromMilliseconds(500), | |
| 480 base::Time::Now() - server_profile->use_date()); | |
| 481 } | |
| 482 | |
| 483 // Tests that SaveImportedProfile sets the modification date on new profiles. | 440 // Tests that SaveImportedProfile sets the modification date on new profiles. |
| 484 TEST_F(PersonalDataManagerTest, SaveImportedProfileSetModificationDate) { | 441 TEST_F(PersonalDataManagerTest, SaveImportedProfileSetModificationDate) { |
| 485 AutofillProfile profile(test::GetFullProfile()); | 442 AutofillProfile profile(test::GetFullProfile()); |
| 486 EXPECT_NE(base::Time(), profile.modification_date()); | 443 EXPECT_NE(base::Time(), profile.modification_date()); |
| 487 | 444 |
| 488 personal_data_->SaveImportedProfile(profile); | 445 personal_data_->SaveImportedProfile(profile); |
| 489 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles(); | 446 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles(); |
| 490 ASSERT_EQ(1U, profiles.size()); | 447 ASSERT_EQ(1U, profiles.size()); |
| 491 EXPECT_GT(base::TimeDelta::FromMilliseconds(500), | 448 EXPECT_GT(base::TimeDelta::FromMilliseconds(500), |
| 492 base::Time::Now() - profiles[0]->modification_date()); | 449 base::Time::Now() - profiles[0]->modification_date()); |
| (...skipping 3621 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4114 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123")); | 4071 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123")); |
| 4115 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", | 4072 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", |
| 4116 "9012" /* Visa */, "01", "2999"); | 4073 "9012" /* Visa */, "01", "2999"); |
| 4117 server_cards.back().SetTypeForMaskedCard(kVisaCard); | 4074 server_cards.back().SetTypeForMaskedCard(kVisaCard); |
| 4118 test::SetServerCreditCards(autofill_table_, server_cards); | 4075 test::SetServerCreditCards(autofill_table_, server_cards); |
| 4119 personal_data_->Refresh(); | 4076 personal_data_->Refresh(); |
| 4120 | 4077 |
| 4121 // Need to set the google services username | 4078 // Need to set the google services username |
| 4122 EnableWalletCardImport(); | 4079 EnableWalletCardImport(); |
| 4123 | 4080 |
| 4124 // Add a server profile. | |
| 4125 std::vector<AutofillProfile> server_profiles; | |
| 4126 server_profiles.push_back( | |
| 4127 AutofillProfile(AutofillProfile::SERVER_PROFILE, "a123")); | |
| 4128 test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe", "", | |
| 4129 "ACME Corp", "500 Oak View", "Apt 8", "Houston", "TX", | |
| 4130 "77401", "US", ""); | |
| 4131 autofill_table_->SetServerProfiles(server_profiles); | |
| 4132 | |
| 4133 // The card and profile should be there. | 4081 // The card and profile should be there. |
| 4134 ResetPersonalDataManager(USER_MODE_NORMAL); | 4082 ResetPersonalDataManager(USER_MODE_NORMAL); |
| 4135 EXPECT_FALSE(personal_data_->GetCreditCards().empty()); | 4083 EXPECT_FALSE(personal_data_->GetCreditCards().empty()); |
| 4136 EXPECT_FALSE(personal_data_->GetProfiles().empty()); | |
| 4137 | 4084 |
| 4138 personal_data_->ClearAllServerData(); | 4085 personal_data_->ClearAllServerData(); |
| 4139 | 4086 |
| 4140 // Reload the database, everything should be gone. | 4087 // Reload the database, everything should be gone. |
| 4141 ResetPersonalDataManager(USER_MODE_NORMAL); | 4088 ResetPersonalDataManager(USER_MODE_NORMAL); |
| 4142 EXPECT_TRUE(personal_data_->GetCreditCards().empty()); | 4089 EXPECT_TRUE(personal_data_->GetCreditCards().empty()); |
| 4143 EXPECT_TRUE(personal_data_->GetProfiles().empty()); | |
| 4144 } | 4090 } |
| 4145 | 4091 |
| 4146 TEST_F(PersonalDataManagerTest, DontDuplicateServerCard) { | 4092 TEST_F(PersonalDataManagerTest, DontDuplicateServerCard) { |
| 4147 EnableWalletCardImport(); | 4093 EnableWalletCardImport(); |
| 4148 | 4094 |
| 4149 std::vector<CreditCard> server_cards; | 4095 std::vector<CreditCard> server_cards; |
| 4150 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123")); | 4096 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123")); |
| 4151 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", | 4097 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", |
| 4152 "1881" /* Visa */, "01", "2999"); | 4098 "1881" /* Visa */, "01", "2999"); |
| 4153 server_cards.back().SetTypeForMaskedCard(kVisaCard); | 4099 server_cards.back().SetTypeForMaskedCard(kVisaCard); |
| (...skipping 1486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5640 // Re-enable the profile cleanup now that the profile was added. | 5586 // Re-enable the profile cleanup now that the profile was added. |
| 5641 EnableAutofillProfileCleanup(); | 5587 EnableAutofillProfileCleanup(); |
| 5642 | 5588 |
| 5643 // The deduping routine should not be run. | 5589 // The deduping routine should not be run. |
| 5644 EXPECT_FALSE(personal_data_->ApplyDedupingRoutine()); | 5590 EXPECT_FALSE(personal_data_->ApplyDedupingRoutine()); |
| 5645 | 5591 |
| 5646 // The two duplicate profiles should still be present. | 5592 // The two duplicate profiles should still be present. |
| 5647 EXPECT_EQ(2U, personal_data_->GetProfiles().size()); | 5593 EXPECT_EQ(2U, personal_data_->GetProfiles().size()); |
| 5648 } | 5594 } |
| 5649 | 5595 |
| 5596 // Tests that a new local profile is created if no existing one is a duplicate |
| 5597 // of the server address. Also tests that the billing address relationship was |
| 5598 // transferred to the converted address. |
| 5599 TEST_F(PersonalDataManagerTest, |
| 5600 ConvertWalletAddressesToLocalProfiles_NewProfile) { |
| 5601 /////////////////////////////////////////////////////////////////////// |
| 5602 // Setup. |
| 5603 /////////////////////////////////////////////////////////////////////// |
| 5604 EnableWalletCardImport(); |
| 5605 base::HistogramTester histogram_tester; |
| 5606 const std::string kServerAddressId("server_address1"); |
| 5607 |
| 5608 // Add two different profiles, a local and a server one. Set the use stats so |
| 5609 // the server profile has a higher frecency, to have a predictable ordering to |
| 5610 // validate results. |
| 5611 AutofillProfile local_profile(base::GenerateGUID(), |
| 5612 "https://www.example.com"); |
| 5613 test::SetProfileInfo(&local_profile, "Josephine", "Alicia", "Saenz", |
| 5614 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", |
| 5615 "Orlando", "FL", "32801", "US", "19482937549"); |
| 5616 local_profile.set_use_count(1); |
| 5617 personal_data_->AddProfile(local_profile); |
| 5618 |
| 5619 // Add a different server profile. |
| 5620 std::vector<AutofillProfile> GetServerProfiles; |
| 5621 GetServerProfiles.push_back( |
| 5622 AutofillProfile(AutofillProfile::SERVER_PROFILE, kServerAddressId)); |
| 5623 test::SetProfileInfo(&GetServerProfiles.back(), "John", "", "Doe", "", |
| 5624 "ACME Corp", "500 Oak View", "Apt 8", "Houston", "TX", |
| 5625 "77401", "US", ""); |
| 5626 // Wallet only provides a full name, so the above first and last names |
| 5627 // will be ignored when the profile is written to the DB. |
| 5628 GetServerProfiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe")); |
| 5629 GetServerProfiles.back().set_use_count(100); |
| 5630 autofill_table_->SetServerProfiles(GetServerProfiles); |
| 5631 |
| 5632 // Add a server and a local card that have the server address as billing |
| 5633 // address. |
| 5634 CreditCard local_card("287151C8-6AB1-487C-9095-28E80BE5DA15", |
| 5635 "https://www.example.com"); |
| 5636 test::SetCreditCardInfo(&local_card, "Clyde Barrow", |
| 5637 "347666888555" /* American Express */, "04", "2999"); |
| 5638 local_card.set_billing_address_id(kServerAddressId); |
| 5639 personal_data_->AddCreditCard(local_card); |
| 5640 |
| 5641 std::vector<CreditCard> server_cards; |
| 5642 server_cards.push_back( |
| 5643 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_card1")); |
| 5644 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", |
| 5645 "1111" /* Visa */, "01", "2999"); |
| 5646 server_cards.back().SetTypeForMaskedCard(kVisaCard); |
| 5647 server_cards.back().set_billing_address_id(kServerAddressId); |
| 5648 test::SetServerCreditCards(autofill_table_, server_cards); |
| 5649 |
| 5650 // Make sure everything is setup correctly. |
| 5651 personal_data_->Refresh(); |
| 5652 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5653 .WillOnce(QuitMainMessageLoop()); |
| 5654 base::RunLoop().Run(); |
| 5655 EXPECT_EQ(1U, personal_data_->web_profiles().size()); |
| 5656 EXPECT_EQ(1U, personal_data_->GetServerProfiles().size()); |
| 5657 EXPECT_EQ(2U, personal_data_->GetCreditCards().size()); |
| 5658 |
| 5659 /////////////////////////////////////////////////////////////////////// |
| 5660 // Tested method. |
| 5661 /////////////////////////////////////////////////////////////////////// |
| 5662 personal_data_->ConvertWalletAddressesToLocalProfiles(); |
| 5663 |
| 5664 /////////////////////////////////////////////////////////////////////// |
| 5665 // Validation. |
| 5666 /////////////////////////////////////////////////////////////////////// |
| 5667 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5668 .WillOnce(QuitMainMessageLoop()); |
| 5669 base::RunLoop().Run(); |
| 5670 |
| 5671 // The Wallet address should have been added as a new local profile. |
| 5672 EXPECT_EQ(2U, personal_data_->web_profiles().size()); |
| 5673 EXPECT_EQ(1U, personal_data_->GetServerProfiles().size()); |
| 5674 histogram_tester.ExpectUniqueSample("Autofill.WalletAddressConversionType", |
| 5675 AutofillMetrics::CONVERTED_ADDRESS_ADDED, |
| 5676 1); |
| 5677 |
| 5678 // The conversion should be recorded in the Wallet address. |
| 5679 EXPECT_TRUE(personal_data_->GetServerProfiles().back()->has_converted()); |
| 5680 |
| 5681 // Get the profiles, sorted by frecency to have a deterministic order. |
| 5682 std::vector<AutofillProfile*> profiles = |
| 5683 personal_data_->GetProfilesToSuggest(); |
| 5684 |
| 5685 // Make sure that the two profiles have not merged. |
| 5686 ASSERT_EQ(2U, profiles.size()); |
| 5687 EXPECT_EQ(UTF8ToUTF16("John"), profiles[0]->GetRawInfo(NAME_FIRST)); |
| 5688 EXPECT_EQ(local_profile, *profiles[1]); |
| 5689 |
| 5690 // Make sure that the billing address id of the two cards now point to the |
| 5691 // converted profile. |
| 5692 EXPECT_EQ(profiles[0]->guid(), |
| 5693 personal_data_->GetCreditCards()[0]->billing_address_id()); |
| 5694 EXPECT_EQ(profiles[0]->guid(), |
| 5695 personal_data_->GetCreditCards()[1]->billing_address_id()); |
| 5696 } |
| 5697 |
| 5698 // Tests that the converted wallet address is merged into an existing local |
| 5699 // profile if they are considered equivalent. Also tests that the billing |
| 5700 // address relationship was transferred to the converted address. |
| 5701 TEST_F(PersonalDataManagerTest, |
| 5702 ConvertWalletAddressesToLocalProfiles_MergedProfile) { |
| 5703 /////////////////////////////////////////////////////////////////////// |
| 5704 // Setup. |
| 5705 /////////////////////////////////////////////////////////////////////// |
| 5706 EnableWalletCardImport(); |
| 5707 base::HistogramTester histogram_tester; |
| 5708 const std::string kServerAddressId("server_address1"); |
| 5709 |
| 5710 // Add two similar profile, a local and a server one. Set the use stats so |
| 5711 // the server card has a higher frecency, to have a predicatble ordering to |
| 5712 // validate results. |
| 5713 // Add a local profile. |
| 5714 AutofillProfile local_profile(base::GenerateGUID(), |
| 5715 "https://www.example.com"); |
| 5716 test::SetProfileInfo(&local_profile, "John", "", "Doe", "john@doe.com", "", |
| 5717 "1212 Center.", "Bld. 5", "Orlando", "FL", "32801", "US", |
| 5718 "19482937549"); |
| 5719 local_profile.set_use_count(1); |
| 5720 personal_data_->AddProfile(local_profile); |
| 5721 |
| 5722 // Add a different server profile. |
| 5723 std::vector<AutofillProfile> GetServerProfiles; |
| 5724 GetServerProfiles.push_back( |
| 5725 AutofillProfile(AutofillProfile::SERVER_PROFILE, kServerAddressId)); |
| 5726 test::SetProfileInfo(&GetServerProfiles.back(), "John", "", "Doe", "", "Fox", |
| 5727 "1212 Center", "Bld. 5", "Orlando", "FL", "", "US", ""); |
| 5728 // Wallet only provides a full name, so the above first and last names |
| 5729 // will be ignored when the profile is written to the DB. |
| 5730 GetServerProfiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe")); |
| 5731 GetServerProfiles.back().set_use_count(100); |
| 5732 autofill_table_->SetServerProfiles(GetServerProfiles); |
| 5733 |
| 5734 // Add a server and a local card that have the server address as billing |
| 5735 // address. |
| 5736 CreditCard local_card("287151C8-6AB1-487C-9095-28E80BE5DA15", |
| 5737 "https://www.example.com"); |
| 5738 test::SetCreditCardInfo(&local_card, "Clyde Barrow", |
| 5739 "347666888555" /* American Express */, "04", "2999"); |
| 5740 local_card.set_billing_address_id(kServerAddressId); |
| 5741 personal_data_->AddCreditCard(local_card); |
| 5742 |
| 5743 std::vector<CreditCard> server_cards; |
| 5744 server_cards.push_back( |
| 5745 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_card1")); |
| 5746 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", |
| 5747 "1111" /* Visa */, "01", "2999"); |
| 5748 server_cards.back().SetTypeForMaskedCard(kVisaCard); |
| 5749 server_cards.back().set_billing_address_id(kServerAddressId); |
| 5750 test::SetServerCreditCards(autofill_table_, server_cards); |
| 5751 |
| 5752 // Make sure everything is setup correctly. |
| 5753 personal_data_->Refresh(); |
| 5754 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5755 .WillOnce(QuitMainMessageLoop()); |
| 5756 base::RunLoop().Run(); |
| 5757 EXPECT_EQ(1U, personal_data_->web_profiles().size()); |
| 5758 EXPECT_EQ(1U, personal_data_->GetServerProfiles().size()); |
| 5759 EXPECT_EQ(2U, personal_data_->GetCreditCards().size()); |
| 5760 |
| 5761 /////////////////////////////////////////////////////////////////////// |
| 5762 // Tested method. |
| 5763 /////////////////////////////////////////////////////////////////////// |
| 5764 personal_data_->ConvertWalletAddressesToLocalProfiles(); |
| 5765 |
| 5766 /////////////////////////////////////////////////////////////////////// |
| 5767 // Validation. |
| 5768 /////////////////////////////////////////////////////////////////////// |
| 5769 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5770 .WillOnce(QuitMainMessageLoop()); |
| 5771 base::RunLoop().Run(); |
| 5772 |
| 5773 // The Wallet address should have been merged with the existing local profile. |
| 5774 EXPECT_EQ(1U, personal_data_->web_profiles().size()); |
| 5775 EXPECT_EQ(1U, personal_data_->GetServerProfiles().size()); |
| 5776 histogram_tester.ExpectUniqueSample("Autofill.WalletAddressConversionType", |
| 5777 AutofillMetrics::CONVERTED_ADDRESS_MERGED, |
| 5778 1); |
| 5779 |
| 5780 // The conversion should be recorded in the Wallet address. |
| 5781 EXPECT_TRUE(personal_data_->GetServerProfiles().back()->has_converted()); |
| 5782 |
| 5783 // Get the profiles, sorted by frecency to have a deterministic order. |
| 5784 std::vector<AutofillProfile*> profiles = |
| 5785 personal_data_->GetProfilesToSuggest(); |
| 5786 |
| 5787 // Make sure that the two profiles have merged. |
| 5788 ASSERT_EQ(1U, profiles.size()); |
| 5789 |
| 5790 // Check that the values were merged. |
| 5791 EXPECT_EQ(UTF8ToUTF16("john@doe.com"), |
| 5792 profiles[0]->GetRawInfo(EMAIL_ADDRESS)); |
| 5793 EXPECT_EQ(UTF8ToUTF16("Fox"), profiles[0]->GetRawInfo(COMPANY_NAME)); |
| 5794 EXPECT_EQ(UTF8ToUTF16("32801"), profiles[0]->GetRawInfo(ADDRESS_HOME_ZIP)); |
| 5795 |
| 5796 // Make sure that the billing address id of the two cards now point to the |
| 5797 // converted profile. |
| 5798 EXPECT_EQ(profiles[0]->guid(), |
| 5799 personal_data_->GetCreditCards()[0]->billing_address_id()); |
| 5800 EXPECT_EQ(profiles[0]->guid(), |
| 5801 personal_data_->GetCreditCards()[1]->billing_address_id()); |
| 5802 } |
| 5803 |
| 5804 // Tests that a Wallet address that has already converted does not get converted |
| 5805 // a second time. |
| 5806 TEST_F(PersonalDataManagerTest, |
| 5807 ConvertWalletAddressesToLocalProfiles_AlreadyConverted) { |
| 5808 /////////////////////////////////////////////////////////////////////// |
| 5809 // Setup. |
| 5810 /////////////////////////////////////////////////////////////////////// |
| 5811 EnableWalletCardImport(); |
| 5812 base::HistogramTester histogram_tester; |
| 5813 const std::string kServerAddressId("server_address1"); |
| 5814 |
| 5815 // Add a server profile that has already been converted. |
| 5816 std::vector<AutofillProfile> GetServerProfiles; |
| 5817 GetServerProfiles.push_back( |
| 5818 AutofillProfile(AutofillProfile::SERVER_PROFILE, kServerAddressId)); |
| 5819 test::SetProfileInfo(&GetServerProfiles.back(), "John", "Ray", "Doe", |
| 5820 "john@doe.com", "Fox", "1212 Center", "Bld. 5", |
| 5821 "Orlando", "FL", "32801", "US", ""); |
| 5822 GetServerProfiles.back().set_has_converted(true); |
| 5823 // Wallet only provides a full name, so the above first and last names |
| 5824 // will be ignored when the profile is written to the DB. |
| 5825 autofill_table_->SetServerProfiles(GetServerProfiles); |
| 5826 |
| 5827 // Make sure everything is setup correctly. |
| 5828 personal_data_->Refresh(); |
| 5829 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5830 .WillOnce(QuitMainMessageLoop()); |
| 5831 base::RunLoop().Run(); |
| 5832 EXPECT_EQ(0U, personal_data_->web_profiles().size()); |
| 5833 EXPECT_EQ(1U, personal_data_->GetServerProfiles().size()); |
| 5834 |
| 5835 /////////////////////////////////////////////////////////////////////// |
| 5836 // Tested method. |
| 5837 /////////////////////////////////////////////////////////////////////// |
| 5838 personal_data_->ConvertWalletAddressesToLocalProfiles(); |
| 5839 |
| 5840 /////////////////////////////////////////////////////////////////////// |
| 5841 // Validation. |
| 5842 /////////////////////////////////////////////////////////////////////// |
| 5843 // Since there should be no change in data, OnPersonalDataChanged should not |
| 5844 // have been called. |
| 5845 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(0); |
| 5846 |
| 5847 personal_data_->Refresh(); |
| 5848 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5849 .WillOnce(QuitMainMessageLoop()); |
| 5850 base::RunLoop().Run(); |
| 5851 |
| 5852 // There should be no local profiles added. |
| 5853 EXPECT_EQ(0U, personal_data_->web_profiles().size()); |
| 5854 EXPECT_EQ(1U, personal_data_->GetServerProfiles().size()); |
| 5855 } |
| 5856 |
| 5857 // Tests that when the user has multiple similar Wallet addresses, they get |
| 5858 // merged into a single local profile, and that the billing address relationship |
| 5859 // is merged too. |
| 5860 TEST_F(PersonalDataManagerTest, |
| 5861 ConvertWalletAddressesToLocalProfiles_MultipleSimilarWalletAddresses) { |
| 5862 /////////////////////////////////////////////////////////////////////// |
| 5863 // Setup. |
| 5864 /////////////////////////////////////////////////////////////////////// |
| 5865 EnableWalletCardImport(); |
| 5866 base::HistogramTester histogram_tester; |
| 5867 const std::string kServerAddressId("server_address1"); |
| 5868 const std::string kServerAddressId2("server_address2"); |
| 5869 |
| 5870 // Add a unique local profile and two similar server profiles. Set the use |
| 5871 // stats to have a predicatble ordering to validate results. |
| 5872 // Add a local profile. |
| 5873 AutofillProfile local_profile(base::GenerateGUID(), |
| 5874 "https://www.example.com"); |
| 5875 test::SetProfileInfo(&local_profile, "Bob", "", "Doe", "", "Fox", |
| 5876 "1212 Center.", "Bld. 5", "Orlando", "FL", "32801", "US", |
| 5877 "19482937549"); |
| 5878 local_profile.set_use_count(1); |
| 5879 personal_data_->AddProfile(local_profile); |
| 5880 |
| 5881 // Add a server profile. |
| 5882 std::vector<AutofillProfile> GetServerProfiles; |
| 5883 GetServerProfiles.push_back( |
| 5884 AutofillProfile(AutofillProfile::SERVER_PROFILE, kServerAddressId)); |
| 5885 test::SetProfileInfo(&GetServerProfiles.back(), "John", "", "Doe", "", "", |
| 5886 "1212 Center", "Bld. 5", "Orlando", "FL", "32801", "US", |
| 5887 ""); |
| 5888 // Wallet only provides a full name, so the above first and last names |
| 5889 // will be ignored when the profile is written to the DB. |
| 5890 GetServerProfiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe")); |
| 5891 GetServerProfiles.back().set_use_count(100); |
| 5892 |
| 5893 // Add a similar server profile. |
| 5894 GetServerProfiles.push_back( |
| 5895 AutofillProfile(AutofillProfile::SERVER_PROFILE, kServerAddressId2)); |
| 5896 test::SetProfileInfo(&GetServerProfiles.back(), "John", "", "Doe", |
| 5897 "john@doe.com", "Fox", "1212 Center", "Bld. 5", |
| 5898 "Orlando", "FL", "", "US", ""); |
| 5899 // Wallet only provides a full name, so the above first and last names |
| 5900 // will be ignored when the profile is written to the DB. |
| 5901 GetServerProfiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe")); |
| 5902 GetServerProfiles.back().set_use_count(200); |
| 5903 autofill_table_->SetServerProfiles(GetServerProfiles); |
| 5904 |
| 5905 // Add a server and a local card that have the first and second Wallet address |
| 5906 // as a billing address. |
| 5907 CreditCard local_card("287151C8-6AB1-487C-9095-28E80BE5DA15", |
| 5908 "https://www.example.com"); |
| 5909 test::SetCreditCardInfo(&local_card, "Clyde Barrow", |
| 5910 "347666888555" /* American Express */, "04", "2999"); |
| 5911 local_card.set_billing_address_id(kServerAddressId); |
| 5912 personal_data_->AddCreditCard(local_card); |
| 5913 |
| 5914 std::vector<CreditCard> server_cards; |
| 5915 server_cards.push_back( |
| 5916 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_card1")); |
| 5917 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", |
| 5918 "1111" /* Visa */, "01", "2999"); |
| 5919 server_cards.back().SetTypeForMaskedCard(kVisaCard); |
| 5920 server_cards.back().set_billing_address_id(kServerAddressId2); |
| 5921 test::SetServerCreditCards(autofill_table_, server_cards); |
| 5922 |
| 5923 // Make sure everything is setup correctly. |
| 5924 personal_data_->Refresh(); |
| 5925 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5926 .WillOnce(QuitMainMessageLoop()); |
| 5927 base::RunLoop().Run(); |
| 5928 EXPECT_EQ(1U, personal_data_->web_profiles().size()); |
| 5929 EXPECT_EQ(2U, personal_data_->GetServerProfiles().size()); |
| 5930 EXPECT_EQ(2U, personal_data_->GetCreditCards().size()); |
| 5931 |
| 5932 /////////////////////////////////////////////////////////////////////// |
| 5933 // Tested method. |
| 5934 /////////////////////////////////////////////////////////////////////// |
| 5935 personal_data_->ConvertWalletAddressesToLocalProfiles(); |
| 5936 |
| 5937 /////////////////////////////////////////////////////////////////////// |
| 5938 // Validation. |
| 5939 /////////////////////////////////////////////////////////////////////// |
| 5940 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 5941 .WillOnce(QuitMainMessageLoop()); |
| 5942 base::RunLoop().Run(); |
| 5943 |
| 5944 // The first Wallet address should have been added as a new local profile and |
| 5945 // the second one should have merged with the first. |
| 5946 EXPECT_EQ(2U, personal_data_->web_profiles().size()); |
| 5947 EXPECT_EQ(2U, personal_data_->GetServerProfiles().size()); |
| 5948 histogram_tester.ExpectBucketCount("Autofill.WalletAddressConversionType", |
| 5949 AutofillMetrics::CONVERTED_ADDRESS_ADDED, |
| 5950 1); |
| 5951 histogram_tester.ExpectBucketCount("Autofill.WalletAddressConversionType", |
| 5952 AutofillMetrics::CONVERTED_ADDRESS_MERGED, |
| 5953 1); |
| 5954 |
| 5955 // The conversion should be recorded in the Wallet addresses. |
| 5956 EXPECT_TRUE(personal_data_->GetServerProfiles()[0]->has_converted()); |
| 5957 EXPECT_TRUE(personal_data_->GetServerProfiles()[1]->has_converted()); |
| 5958 |
| 5959 // Get the profiles, sorted by frecency to have a deterministic order. |
| 5960 std::vector<AutofillProfile*> profiles = |
| 5961 personal_data_->GetProfilesToSuggest(); |
| 5962 |
| 5963 // Make sure that the two Wallet addresses merged together and were added as |
| 5964 // a new local profile. |
| 5965 ASSERT_EQ(2U, profiles.size()); |
| 5966 EXPECT_EQ(UTF8ToUTF16("John"), profiles[0]->GetRawInfo(NAME_FIRST)); |
| 5967 EXPECT_EQ(local_profile, *profiles[1]); |
| 5968 |
| 5969 // Check that the values were merged. |
| 5970 EXPECT_EQ(UTF8ToUTF16("Fox"), profiles[0]->GetRawInfo(COMPANY_NAME)); |
| 5971 EXPECT_EQ(UTF8ToUTF16("32801"), profiles[0]->GetRawInfo(ADDRESS_HOME_ZIP)); |
| 5972 |
| 5973 // Make sure that the billing address id of the two cards now point to the |
| 5974 // converted profile. |
| 5975 EXPECT_EQ(profiles[0]->guid(), |
| 5976 personal_data_->GetCreditCards()[0]->billing_address_id()); |
| 5977 EXPECT_EQ(profiles[0]->guid(), |
| 5978 personal_data_->GetCreditCards()[1]->billing_address_id()); |
| 5979 } |
| 5980 |
| 5650 } // namespace autofill | 5981 } // namespace autofill |
| OLD | NEW |