Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Side by Side Diff: components/autofill/core/browser/personal_data_manager_unittest.cc

Issue 2658843004: [Autofill] Convert wallet addresses to local autofill profiles. (Closed)
Patch Set: Rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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
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 std::string server_address_id = "server_address1";
Mathieu 2017/02/02 01:22:53 nit: const std::string kServerAddressId("server_ad
sebsg 2017/02/02 15:18:52 Done.
5656
5657 // Add two different profile, a local and a server one. Set the use stats so
Mathieu 2017/02/02 01:22:53 *profiles
sebsg 2017/02/02 15:18:52 Done.
5658 // the server card has a higher frecency, to have a predicatble ordering to
Mathieu 2017/02/02 01:22:53 card -> profile? *predictable
sebsg 2017/02/02 15:18:52 Done.
5659 // validate results.
5660 // Add a local profile.
Mathieu 2017/02/02 01:22:53 remove line
sebsg 2017/02/02 15:18:52 Done.
5661 AutofillProfile local_profile(base::GenerateGUID(),
5662 "https://www.example.com");
5663 test::SetProfileInfo(&local_profile, "Josephine", "Alicia", "Saenz",
5664 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5",
5665 "Orlando", "FL", "32801", "US", "19482937549");
5666 local_profile.set_use_count(1);
5667 personal_data_->AddProfile(local_profile);
5668
5669 // Add a different server profile.
5670 std::vector<AutofillProfile> server_profiles;
5671 server_profiles.push_back(
5672 AutofillProfile(AutofillProfile::SERVER_PROFILE, server_address_id));
5673 test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe", "",
5674 "ACME Corp", "500 Oak View", "Apt 8", "Houston", "TX",
5675 "77401", "US", "");
5676 // Wallet only provides a full name, so the above first and last names
5677 // will be ignored when the profile is written to the DB.
5678 server_profiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe"));
5679 server_profiles.back().set_use_count(100);
5680 autofill_table_->SetServerProfiles(server_profiles);
5681
5682 // Add a server and a local card that have the server address as billing
5683 // address.
5684 CreditCard local_card("287151C8-6AB1-487C-9095-28E80BE5DA15",
5685 "https://www.example.com");
5686 test::SetCreditCardInfo(&local_card, "Clyde Barrow",
5687 "347666888555" /* American Express */, "04", "2999");
5688 local_card.set_billing_address_id(server_address_id);
5689 personal_data_->AddCreditCard(local_card);
5690
5691 std::vector<CreditCard> server_cards;
5692 server_cards.push_back(
5693 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_card1"));
5694 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
5695 "1111" /* Visa */, "01", "2999");
5696 server_cards.back().SetTypeForMaskedCard(kVisaCard);
5697 server_cards.back().set_billing_address_id(server_address_id);
5698 test::SetServerCreditCards(autofill_table_, server_cards);
5699
5700 // Make sure everything is setup correctly.
5701 personal_data_->Refresh();
5702 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
5703 .WillOnce(QuitMainMessageLoop());
5704 base::RunLoop().Run();
5705 EXPECT_EQ(1U, personal_data_->web_profiles().size());
5706 EXPECT_EQ(1U, personal_data_->server_profiles().size());
5707 EXPECT_EQ(2U, personal_data_->GetCreditCards().size());
5708
5709 ///////////////////////////////////////////////////////////////////////
5710 // Tested method.
5711 ///////////////////////////////////////////////////////////////////////
5712 personal_data_->ConvertWalletAddressesToLocalProfiles();
5713
5714 ///////////////////////////////////////////////////////////////////////
5715 // Validation.
5716 ///////////////////////////////////////////////////////////////////////
5717 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
5718 .WillOnce(QuitMainMessageLoop());
5719 base::RunLoop().Run();
5720
5721 // The Wallet address should have been added as a new local profile.
5722 EXPECT_EQ(2U, personal_data_->web_profiles().size());
5723 EXPECT_EQ(1U, personal_data_->server_profiles().size());
5724 histogram_tester.ExpectUniqueSample("Autofill.WalletAddressConversionType",
5725 AutofillMetrics::CONVERTED_ADDRESS_ADDED,
5726 1);
5727
5728 // The conversion should be recorded in the Wallet address.
5729 EXPECT_TRUE(personal_data_->server_profiles().back()->has_converted());
5730
5731 // Get the profiles, sorted by frecency to have a deterministic order.
5732 std::vector<AutofillProfile*> profiles =
5733 personal_data_->GetProfilesToSuggest();
5734
5735 // Make sure that the two profiles have not merged.
5736 ASSERT_EQ(2U, profiles.size());
5737 EXPECT_EQ(UTF8ToUTF16("John"), profiles[0]->GetRawInfo(NAME_FIRST));
5738 EXPECT_EQ(local_profile, *profiles[1]);
5739
5740 // Make sure that the billing address id of the two cards now point to the
5741 // converted profile.
5742 EXPECT_EQ(profiles[0]->guid(),
5743 personal_data_->GetCreditCards()[0]->billing_address_id());
5744 EXPECT_EQ(profiles[0]->guid(),
5745 personal_data_->GetCreditCards()[1]->billing_address_id());
5746 }
5747
5748 // Tests that the converted wallet address is merged into an existing local
5749 // profile if they are considered equivalent. Also tests that the billing
5750 // address relationship was transferred to the converted address.
5751 TEST_F(PersonalDataManagerTest,
5752 ConvertWalletAddressesToLocalProfiles_MergedProfile) {
5753 ///////////////////////////////////////////////////////////////////////
5754 // Setup.
5755 ///////////////////////////////////////////////////////////////////////
5756 EnableWalletCardImport();
5757 base::HistogramTester histogram_tester;
5758 std::string server_address_id = "server_address1";
5759
5760 // Add two similar profile, a local and a server one. Set the use stats so
5761 // the server card has a higher frecency, to have a predicatble ordering to
5762 // validate results.
5763 // Add a local profile.
5764 AutofillProfile local_profile(base::GenerateGUID(),
5765 "https://www.example.com");
5766 test::SetProfileInfo(&local_profile, "John", "", "Doe", "john@doe.com", "",
5767 "1212 Center.", "Bld. 5", "Orlando", "FL", "32801", "US",
5768 "19482937549");
5769 local_profile.set_use_count(1);
5770 personal_data_->AddProfile(local_profile);
5771
5772 // Add a different server profile.
5773 std::vector<AutofillProfile> server_profiles;
5774 server_profiles.push_back(
5775 AutofillProfile(AutofillProfile::SERVER_PROFILE, server_address_id));
5776 test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe", "", "Fox",
5777 "1212 Center", "Bld. 5", "Orlando", "FL", "", "US", "");
5778 // Wallet only provides a full name, so the above first and last names
5779 // will be ignored when the profile is written to the DB.
5780 server_profiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe"));
5781 server_profiles.back().set_use_count(100);
5782 autofill_table_->SetServerProfiles(server_profiles);
5783
5784 // Add a server and a local card that have the server address as billing
5785 // address.
5786 CreditCard local_card("287151C8-6AB1-487C-9095-28E80BE5DA15",
5787 "https://www.example.com");
5788 test::SetCreditCardInfo(&local_card, "Clyde Barrow",
5789 "347666888555" /* American Express */, "04", "2999");
5790 local_card.set_billing_address_id(server_address_id);
5791 personal_data_->AddCreditCard(local_card);
5792
5793 std::vector<CreditCard> server_cards;
5794 server_cards.push_back(
5795 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_card1"));
5796 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
5797 "1111" /* Visa */, "01", "2999");
5798 server_cards.back().SetTypeForMaskedCard(kVisaCard);
5799 server_cards.back().set_billing_address_id(server_address_id);
5800 test::SetServerCreditCards(autofill_table_, server_cards);
5801
5802 // Make sure everything is setup correctly.
5803 personal_data_->Refresh();
5804 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
5805 .WillOnce(QuitMainMessageLoop());
5806 base::RunLoop().Run();
5807 EXPECT_EQ(1U, personal_data_->web_profiles().size());
5808 EXPECT_EQ(1U, personal_data_->server_profiles().size());
5809 EXPECT_EQ(2U, personal_data_->GetCreditCards().size());
5810
5811 ///////////////////////////////////////////////////////////////////////
5812 // Tested method.
5813 ///////////////////////////////////////////////////////////////////////
5814 personal_data_->ConvertWalletAddressesToLocalProfiles();
5815
5816 ///////////////////////////////////////////////////////////////////////
5817 // Validation.
5818 ///////////////////////////////////////////////////////////////////////
5819 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
5820 .WillOnce(QuitMainMessageLoop());
5821 base::RunLoop().Run();
5822
5823 // The Wallet address should have been merged with the existing local profile.
5824 EXPECT_EQ(1U, personal_data_->web_profiles().size());
5825 EXPECT_EQ(1U, personal_data_->server_profiles().size());
5826 histogram_tester.ExpectUniqueSample("Autofill.WalletAddressConversionType",
5827 AutofillMetrics::CONVERTED_ADDRESS_MERGED,
5828 1);
5829
5830 // The conversion should be recorded in the Wallet address.
5831 EXPECT_TRUE(personal_data_->server_profiles().back()->has_converted());
5832
5833 // Get the profiles, sorted by frecency to have a deterministic order.
5834 std::vector<AutofillProfile*> profiles =
5835 personal_data_->GetProfilesToSuggest();
5836
5837 // Make sure that the two profiles have merged.
5838 ASSERT_EQ(1U, profiles.size());
5839
5840 // Check that the values were merged.
5841 EXPECT_EQ(UTF8ToUTF16("john@doe.com"),
5842 profiles[0]->GetRawInfo(EMAIL_ADDRESS));
5843 EXPECT_EQ(UTF8ToUTF16("Fox"), profiles[0]->GetRawInfo(COMPANY_NAME));
5844 EXPECT_EQ(UTF8ToUTF16("32801"), profiles[0]->GetRawInfo(ADDRESS_HOME_ZIP));
5845
5846 // Make sure that the billing address id of the two cards now point to the
5847 // converted profile.
5848 EXPECT_EQ(profiles[0]->guid(),
5849 personal_data_->GetCreditCards()[0]->billing_address_id());
5850 EXPECT_EQ(profiles[0]->guid(),
5851 personal_data_->GetCreditCards()[1]->billing_address_id());
5852 }
5853
5854 // Tests that a Wallet address that has already converted does not get converted
5855 // a second time.
5856 TEST_F(PersonalDataManagerTest,
5857 ConvertWalletAddressesToLocalProfiles_AlreadyConverted) {
5858 ///////////////////////////////////////////////////////////////////////
5859 // Setup.
5860 ///////////////////////////////////////////////////////////////////////
5861 EnableWalletCardImport();
5862 base::HistogramTester histogram_tester;
5863 std::string server_address_id = "server_address1";
5864
5865 // Add a server profile that has already been converted.
5866 std::vector<AutofillProfile> server_profiles;
5867 server_profiles.push_back(
5868 AutofillProfile(AutofillProfile::SERVER_PROFILE, server_address_id));
5869 test::SetProfileInfo(&server_profiles.back(), "John", "Ray", "Doe",
5870 "john@doe.com", "Fox", "1212 Center", "Bld. 5",
5871 "Orlando", "FL", "32801", "US", "");
5872 server_profiles.back().set_has_converted(true);
5873 // Wallet only provides a full name, so the above first and last names
5874 // will be ignored when the profile is written to the DB.
5875 autofill_table_->SetServerProfiles(server_profiles);
5876
5877 // Make sure everything is setup correctly.
5878 personal_data_->Refresh();
5879 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
5880 .WillOnce(QuitMainMessageLoop());
5881 base::RunLoop().Run();
5882 EXPECT_EQ(0U, personal_data_->web_profiles().size());
5883 EXPECT_EQ(1U, personal_data_->server_profiles().size());
5884
5885 ///////////////////////////////////////////////////////////////////////
5886 // Tested method.
5887 ///////////////////////////////////////////////////////////////////////
5888 personal_data_->ConvertWalletAddressesToLocalProfiles();
5889
5890 ///////////////////////////////////////////////////////////////////////
5891 // Validation.
5892 ///////////////////////////////////////////////////////////////////////
5893 // Since there should be no change in data, OnPersonalDataChanged should not
5894 // have been called.
5895 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(0);
5896
5897 personal_data_->Refresh();
5898 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
5899 .WillOnce(QuitMainMessageLoop());
5900 base::RunLoop().Run();
5901
5902 // There should be no local profiles added.
5903 EXPECT_EQ(0U, personal_data_->web_profiles().size());
5904 EXPECT_EQ(1U, personal_data_->server_profiles().size());
5905 }
5906
5907 // Tests that when the user has multiple similar Wallet addresses, they get
5908 // merged into a single local profile, and that the billing address relationship
5909 // is merged too.
5910 TEST_F(PersonalDataManagerTest,
5911 ConvertWalletAddressesToLocalProfiles_MultipleSimilarWalletAddresses) {
5912 ///////////////////////////////////////////////////////////////////////
5913 // Setup.
5914 ///////////////////////////////////////////////////////////////////////
5915 EnableWalletCardImport();
5916 base::HistogramTester histogram_tester;
5917 std::string server_address_1_id = "server_address1";
5918 std::string server_address_2_id = "server_address2";
5919
5920 // Add a unique local profile and two similar server profiles. Set the use
5921 // stats to have a predicatble ordering to validate results.
5922 // Add a local profile.
5923 AutofillProfile local_profile(base::GenerateGUID(),
5924 "https://www.example.com");
5925 test::SetProfileInfo(&local_profile, "Bob", "", "Doe", "", "Fox",
5926 "1212 Center.", "Bld. 5", "Orlando", "FL", "32801", "US",
5927 "19482937549");
5928 local_profile.set_use_count(1);
5929 personal_data_->AddProfile(local_profile);
5930
5931 // Add a server profile.
5932 std::vector<AutofillProfile> server_profiles;
5933 server_profiles.push_back(
5934 AutofillProfile(AutofillProfile::SERVER_PROFILE, server_address_1_id));
5935 test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe", "", "",
5936 "1212 Center", "Bld. 5", "Orlando", "FL", "32801", "US",
5937 "");
5938 // Wallet only provides a full name, so the above first and last names
5939 // will be ignored when the profile is written to the DB.
5940 server_profiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe"));
5941 server_profiles.back().set_use_count(100);
5942
5943 // Add a similar server profile.
5944 server_profiles.push_back(
5945 AutofillProfile(AutofillProfile::SERVER_PROFILE, server_address_2_id));
5946 test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe",
5947 "john@doe.com", "Fox", "1212 Center", "Bld. 5",
5948 "Orlando", "FL", "", "US", "");
5949 // Wallet only provides a full name, so the above first and last names
5950 // will be ignored when the profile is written to the DB.
5951 server_profiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe"));
5952 server_profiles.back().set_use_count(200);
5953 autofill_table_->SetServerProfiles(server_profiles);
5954
5955 // Add a server and a local card that have the first and second Wallet address
5956 // as a billing address.
5957 CreditCard local_card("287151C8-6AB1-487C-9095-28E80BE5DA15",
5958 "https://www.example.com");
5959 test::SetCreditCardInfo(&local_card, "Clyde Barrow",
5960 "347666888555" /* American Express */, "04", "2999");
5961 local_card.set_billing_address_id(server_address_1_id);
5962 personal_data_->AddCreditCard(local_card);
5963
5964 std::vector<CreditCard> server_cards;
5965 server_cards.push_back(
5966 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_card1"));
5967 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
5968 "1111" /* Visa */, "01", "2999");
5969 server_cards.back().SetTypeForMaskedCard(kVisaCard);
5970 server_cards.back().set_billing_address_id(server_address_2_id);
5971 test::SetServerCreditCards(autofill_table_, server_cards);
5972
5973 // Make sure everything is setup correctly.
5974 personal_data_->Refresh();
5975 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
5976 .WillOnce(QuitMainMessageLoop());
5977 base::RunLoop().Run();
5978 EXPECT_EQ(1U, personal_data_->web_profiles().size());
5979 EXPECT_EQ(2U, personal_data_->server_profiles().size());
5980 EXPECT_EQ(2U, personal_data_->GetCreditCards().size());
5981
5982 ///////////////////////////////////////////////////////////////////////
5983 // Tested method.
5984 ///////////////////////////////////////////////////////////////////////
5985 personal_data_->ConvertWalletAddressesToLocalProfiles();
5986
5987 ///////////////////////////////////////////////////////////////////////
5988 // Validation.
5989 ///////////////////////////////////////////////////////////////////////
5990 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
5991 .WillOnce(QuitMainMessageLoop());
5992 base::RunLoop().Run();
5993
5994 // The first Wallet address should have been added as a new local profile and
5995 // the second one should have merged with the first.
5996 EXPECT_EQ(2U, personal_data_->web_profiles().size());
5997 EXPECT_EQ(2U, personal_data_->server_profiles().size());
5998 histogram_tester.ExpectBucketCount("Autofill.WalletAddressConversionType",
5999 AutofillMetrics::CONVERTED_ADDRESS_ADDED,
6000 1);
6001 histogram_tester.ExpectBucketCount("Autofill.WalletAddressConversionType",
6002 AutofillMetrics::CONVERTED_ADDRESS_MERGED,
6003 1);
6004
6005 // The conversion should be recorded in the Wallet addresses.
6006 EXPECT_TRUE(personal_data_->server_profiles()[0]->has_converted());
6007 EXPECT_TRUE(personal_data_->server_profiles()[1]->has_converted());
6008
6009 // Get the profiles, sorted by frecency to have a deterministic order.
6010 std::vector<AutofillProfile*> profiles =
6011 personal_data_->GetProfilesToSuggest();
6012
6013 // Make sure that the two Wallet addresses merged together and were added as
6014 // a new local profile.
6015 ASSERT_EQ(2U, profiles.size());
6016 EXPECT_EQ(UTF8ToUTF16("John"), profiles[0]->GetRawInfo(NAME_FIRST));
6017 EXPECT_EQ(local_profile, *profiles[1]);
6018
6019 // Check that the values were merged.
6020 EXPECT_EQ(UTF8ToUTF16("Fox"), profiles[0]->GetRawInfo(COMPANY_NAME));
6021 EXPECT_EQ(UTF8ToUTF16("32801"), profiles[0]->GetRawInfo(ADDRESS_HOME_ZIP));
6022
6023 // Make sure that the billing address id of the two cards now point to the
6024 // converted profile.
6025 EXPECT_EQ(profiles[0]->guid(),
6026 personal_data_->GetCreditCards()[0]->billing_address_id());
6027 EXPECT_EQ(profiles[0]->guid(),
6028 personal_data_->GetCreditCards()[1]->billing_address_id());
6029 }
6030
5704 } // namespace autofill 6031 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698