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

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

Issue 2533303007: [Autofill] Transfer cards' billing address id when deduping profiles. (Closed)
Patch Set: Addressed Roger's comment Created 4 years 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
« no previous file with comments | « components/autofill/core/browser/personal_data_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 4654 matching lines...) Expand 10 before | Expand all | Expand 10 after
4665 existing_profiles.push_back(base::WrapUnique(profile1)); 4665 existing_profiles.push_back(base::WrapUnique(profile1));
4666 existing_profiles.push_back(base::WrapUnique(profile2)); 4666 existing_profiles.push_back(base::WrapUnique(profile2));
4667 existing_profiles.push_back(base::WrapUnique(profile3)); 4667 existing_profiles.push_back(base::WrapUnique(profile3));
4668 existing_profiles.push_back(base::WrapUnique(profile4)); 4668 existing_profiles.push_back(base::WrapUnique(profile4));
4669 existing_profiles.push_back(base::WrapUnique(profile5)); 4669 existing_profiles.push_back(base::WrapUnique(profile5));
4670 4670
4671 // Enable the profile cleanup. 4671 // Enable the profile cleanup.
4672 EnableAutofillProfileCleanup(); 4672 EnableAutofillProfileCleanup();
4673 4673
4674 base::HistogramTester histogram_tester; 4674 base::HistogramTester histogram_tester;
4675 std::unordered_map<std::string, std::string> guids_merge_map;
4675 std::unordered_set<AutofillProfile*> profiles_to_delete; 4676 std::unordered_set<AutofillProfile*> profiles_to_delete;
4676 personal_data_->DedupeProfiles(&existing_profiles, &profiles_to_delete); 4677 personal_data_->DedupeProfiles(&existing_profiles, &profiles_to_delete,
4678 &guids_merge_map);
4677 // 5 profiles were considered for dedupe. 4679 // 5 profiles were considered for dedupe.
4678 histogram_tester.ExpectUniqueSample( 4680 histogram_tester.ExpectUniqueSample(
4679 "Autofill.NumberOfProfilesConsideredForDedupe", 5, 1); 4681 "Autofill.NumberOfProfilesConsideredForDedupe", 5, 1);
4680 // 2 profiles were removed (profiles 1 and 3). 4682 // 2 profiles were removed (profiles 1 and 3).
4681 histogram_tester.ExpectUniqueSample( 4683 histogram_tester.ExpectUniqueSample(
4682 "Autofill.NumberOfProfilesRemovedDuringDedupe", 2, 1); 4684 "Autofill.NumberOfProfilesRemovedDuringDedupe", 2, 1);
4683 4685
4684 // Profile1 should be deleted because it was sent as the profile to merge and 4686 // Profile1 should be deleted because it was sent as the profile to merge and
4685 // thus was merged into profile3 and then into profile5. 4687 // thus was merged into profile3 and then into profile5.
4686 EXPECT_TRUE(profiles_to_delete.count(profile1)); 4688 EXPECT_TRUE(profiles_to_delete.count(profile1));
4687 4689
4688 // Profile3 should be deleted because profile1 was merged into it and the 4690 // Profile3 should be deleted because profile1 was merged into it and the
4689 // resulting profile was then merged into profile5. 4691 // resulting profile was then merged into profile5.
4690 EXPECT_TRUE(profiles_to_delete.count(profile3)); 4692 EXPECT_TRUE(profiles_to_delete.count(profile3));
4691 4693
4692 // Only these two profiles should be deleted. 4694 // Only these two profiles should be deleted.
4693 EXPECT_EQ(2U, profiles_to_delete.size()); 4695 EXPECT_EQ(2U, profiles_to_delete.size());
4694 4696
4695 // All profiles should still be present in |existing_profiles|. 4697 // All profiles should still be present in |existing_profiles|.
4696 EXPECT_EQ(5U, existing_profiles.size()); 4698 EXPECT_EQ(5U, existing_profiles.size());
4697 } 4699 }
4698 4700
4701 // Tests that DedupeProfiles sets the correct merge mapping for billing address
4702 // id references.
4703 TEST_F(PersonalDataManagerTest, DedupeProfiles_GuidsMergeMap) {
4704 // Create the profile for which to find duplicates. It has the highest
4705 // frecency.
4706 AutofillProfile* profile1 =
4707 new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
4708 test::SetProfileInfo(profile1, "Homer", "Jay", "Simpson",
4709 "homer.simpson@abc.com", "", "742. Evergreen Terrace",
4710 "", "Springfield", "IL", "91601", "US", "12345678910");
4711 profile1->set_use_count(9);
4712
4713 // Create a different profile that should not be deduped (different address).
4714 AutofillProfile* profile2 =
4715 new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
4716 test::SetProfileInfo(profile2, "Homer", "Jay", "Simpson",
4717 "homer.simpson@abc.com", "Fox", "1234 Other Street", "",
4718 "Springfield", "IL", "91601", "US", "12345678910");
4719 profile2->set_use_count(7);
4720
4721 // Create a profile similar to profile1 which should be deduped.
4722 AutofillProfile* profile3 =
4723 new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
4724 test::SetProfileInfo(profile3, "Homer", "Jay", "Simpson",
4725 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
4726 "Springfield", "IL", "91601", "US", "12345678910");
4727 profile3->set_use_count(5);
4728
4729 // Create another different profile that should not be deduped (different
4730 // name).
4731 AutofillProfile* profile4 =
4732 new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
4733 test::SetProfileInfo(profile4, "Marjorie", "Jacqueline", "Simpson",
4734 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace",
4735 "", "Springfield", "IL", "91601", "US", "12345678910");
4736 profile4->set_use_count(3);
4737
4738 // Create another profile similar to profile1. Since that one has the lowest
4739 // frecency, the result of the merge should be in this profile at the end of
4740 // the test.
4741 AutofillProfile* profile5 =
4742 new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
4743 test::SetProfileInfo(profile5, "Homer", "Jay", "Simpson",
4744 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
4745 "", "Springfield", "IL", "91601", "US", "12345678910");
4746 profile5->set_use_count(1);
4747
4748 // Add the profiles.
4749 std::vector<std::unique_ptr<AutofillProfile>> existing_profiles;
4750 existing_profiles.push_back(base::WrapUnique(profile1));
4751 existing_profiles.push_back(base::WrapUnique(profile2));
4752 existing_profiles.push_back(base::WrapUnique(profile3));
4753 existing_profiles.push_back(base::WrapUnique(profile4));
4754 existing_profiles.push_back(base::WrapUnique(profile5));
4755
4756 // Enable the profile cleanup.
4757 EnableAutofillProfileCleanup();
4758
4759 std::unordered_map<std::string, std::string> guids_merge_map;
4760 std::unordered_set<AutofillProfile*> profiles_to_delete;
4761 personal_data_->DedupeProfiles(&existing_profiles, &profiles_to_delete,
4762 &guids_merge_map);
4763
4764 // The two profile merges should be recorded in the map.
4765 EXPECT_EQ(2U, guids_merge_map.size());
4766
4767 // Profile 1 was merged into profile 3.
4768 ASSERT_TRUE(guids_merge_map.count(profile1->guid()));
4769 EXPECT_TRUE(guids_merge_map.at(profile1->guid()) == profile3->guid());
4770
4771 // Profile 3 was merged into profile 5.
4772 ASSERT_TRUE(guids_merge_map.count(profile3->guid()));
4773 EXPECT_TRUE(guids_merge_map.at(profile3->guid()) == profile5->guid());
4774 }
4775
4776 // Tests that UpdateCardsBillingAddressReference sets the correct billing
4777 // address id as specified in the map.
4778 TEST_F(PersonalDataManagerTest, UpdateCardsBillingAddressReference) {
4779 /* The merges will be as follow:
4780
4781 A -> B F (not merged)
4782 \
4783 -> E
4784 /
4785 C -> D
4786 */
4787
4788 std::unordered_map<std::string, std::string> guids_merge_map;
4789 guids_merge_map.insert(std::pair<std::string, std::string>("A", "B"));
4790 guids_merge_map.insert(std::pair<std::string, std::string>("C", "D"));
4791 guids_merge_map.insert(std::pair<std::string, std::string>("B", "E"));
4792 guids_merge_map.insert(std::pair<std::string, std::string>("D", "E"));
4793
4794 // Create cards that use A, D, E and F as their billing address id.
4795 CreditCard* credit_card1 =
4796 new CreditCard(base::GenerateGUID(), "https://www.example.com");
4797 credit_card1->set_billing_address_id("A");
4798 CreditCard* credit_card2 =
4799 new CreditCard(base::GenerateGUID(), "https://www.example.com");
4800 credit_card2->set_billing_address_id("D");
4801 CreditCard* credit_card3 =
4802 new CreditCard(base::GenerateGUID(), "https://www.example.com");
4803 credit_card3->set_billing_address_id("E");
4804 CreditCard* credit_card4 =
4805 new CreditCard(base::GenerateGUID(), "https://www.example.com");
4806 credit_card4->set_billing_address_id("F");
4807
4808 // Add the credit cards to the database.
4809 personal_data_->local_credit_cards_.push_back(base::WrapUnique(credit_card1));
4810 personal_data_->local_credit_cards_.push_back(base::WrapUnique(credit_card2));
4811 personal_data_->local_credit_cards_.push_back(base::WrapUnique(credit_card3));
4812 personal_data_->local_credit_cards_.push_back(base::WrapUnique(credit_card4));
4813
4814 personal_data_->UpdateCardsBillingAddressReference(guids_merge_map);
4815
4816 // The first card's billing address should now be E.
4817 EXPECT_EQ("E", credit_card1->billing_address_id());
4818 // The second card's billing address should now be E.
4819 EXPECT_EQ("E", credit_card2->billing_address_id());
4820 // The third card's billing address should still be E.
4821 EXPECT_EQ("E", credit_card3->billing_address_id());
4822 // The fourth card's billing address should still be F.
4823 EXPECT_EQ("F", credit_card4->billing_address_id());
4824 }
4825
4826 // Tests that ApplyDedupingRoutine updates the credit cards' billing address id
4827 // based on the deduped profiles.
4828 TEST_F(PersonalDataManagerTest,
4829 ApplyDedupingRoutine_CardsBillingAddressIdUpdated) {
4830 // A set of 6 profiles will be created. They should merge in this way:
4831 // 1 -> 2 -> 3
4832 // 4 -> 5
4833 // 6
4834 // Set their frencency score so that profile 3 has a higher score than 5, and
4835 // 5 has a higher score than 6. This will ensure a deterministic order when
4836 // verifying results.
4837
4838 // Create a set of 3 profiles to be merged together.
4839 // Create a profile with a higher frecency score.
4840 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
4841 test::SetProfileInfo(&profile1, "Homer", "J", "Simpson",
4842 "homer.simpson@abc.com", "", "742. Evergreen Terrace",
4843 "", "Springfield", "IL", "91601", "US", "");
4844 profile1.set_use_count(12);
4845 profile1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
4846
4847 // Create a profile with a medium frecency score.
4848 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
4849 test::SetProfileInfo(&profile2, "Homer", "Jay", "Simpson",
4850 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
4851 "Springfield", "IL", "91601", "", "12345678910");
4852 profile2.set_use_count(5);
4853 profile2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
4854
4855 // Create a profile with a lower frecency score.
4856 AutofillProfile profile3(base::GenerateGUID(), "https://www.example.com");
4857 test::SetProfileInfo(&profile3, "Homer", "J", "Simpson",
4858 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
4859 "", "Springfield", "IL", "91601", "", "");
4860 profile3.set_use_count(3);
4861 profile3.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(5));
4862
4863 // Create a set of two profiles to be merged together.
4864 // Create a profile with a higher frecency score.
4865 AutofillProfile profile4(base::GenerateGUID(), "https://www.example.com");
4866 test::SetProfileInfo(&profile4, "Marge", "B", "Simpson",
4867 "marge.simpson@abc.com", "", "742. Evergreen Terrace",
4868 "", "Springfield", "IL", "91601", "US", "");
4869 profile4.set_use_count(11);
4870 profile4.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
4871
4872 // Create a profile with a lower frecency score.
4873 AutofillProfile profile5(base::GenerateGUID(), "https://www.example.com");
4874 test::SetProfileInfo(&profile5, "Marge", "B", "Simpson",
4875 "marge.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
4876 "", "Springfield", "IL", "91601", "", "");
4877 profile5.set_use_count(5);
4878 profile5.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
4879
4880 // Create a unique profile.
4881 AutofillProfile profile6(base::GenerateGUID(), "https://www.example.com");
4882 test::SetProfileInfo(&profile6, "Bart", "J", "Simpson",
4883 "bart.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
4884 "", "Springfield", "IL", "91601", "", "");
4885 profile6.set_use_count(10);
4886 profile6.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
4887
4888 // Add three credit cards. Give them a frecency score so that they are
4889 // suggested in order (1, 2, 3). This will ensure a deterministic order for
4890 // verifying results.
4891 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
4892 test::SetCreditCardInfo(&credit_card1, "Clyde Barrow",
4893 "347666888555" /* American Express */, "04", "2999");
4894 credit_card1.set_use_count(10);
4895
4896 CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com");
4897 test::SetCreditCardInfo(&credit_card2, "John Dillinger",
4898 "423456789012" /* Visa */, "01", "2999");
4899 credit_card2.set_use_count(5);
4900
4901 CreditCard credit_card3(base::GenerateGUID(), "https://www.example.com");
4902 test::SetCreditCardInfo(&credit_card3, "Bonnie Parker",
4903 "518765432109" /* Mastercard */, "12", "2999");
4904 credit_card3.set_use_count(1);
4905
4906 // Associate the first card with profile1.
4907 credit_card1.set_billing_address_id(profile1.guid());
4908 // Associate the second card with profile4.
4909 credit_card2.set_billing_address_id(profile4.guid());
4910 // Associate the third card with profile6.
4911 credit_card3.set_billing_address_id(profile6.guid());
4912
4913 personal_data_->AddProfile(profile1);
4914 personal_data_->AddProfile(profile2);
4915 personal_data_->AddProfile(profile3);
4916 personal_data_->AddProfile(profile4);
4917 personal_data_->AddProfile(profile5);
4918 personal_data_->AddProfile(profile6);
4919 personal_data_->AddCreditCard(credit_card1);
4920 personal_data_->AddCreditCard(credit_card2);
4921 personal_data_->AddCreditCard(credit_card3);
4922 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
4923 .WillOnce(QuitMainMessageLoop());
4924 base::RunLoop().Run();
4925
4926 // Make sure the 6 profiles and 3 credit cards were saved.
4927 EXPECT_EQ(6U, personal_data_->GetProfiles().size());
4928 EXPECT_EQ(3U, personal_data_->GetCreditCards().size());
4929
4930 // Enable the profile cleanup now. Otherwise it would be triggered by the
4931 // calls to AddProfile.
4932 EnableAutofillProfileCleanup();
4933
4934 EXPECT_TRUE(personal_data_->ApplyDedupingRoutine());
4935 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
4936 .WillOnce(QuitMainMessageLoop());
4937 base::RunLoop().Run();
4938
4939 // Get the profiles and cards sorted by frecency to have a deterministic
4940 // order.
4941 std::vector<AutofillProfile*> profiles =
4942 personal_data_->GetProfilesToSuggest();
4943 std::vector<CreditCard*> credit_cards =
4944 personal_data_->GetCreditCardsToSuggest();
4945
4946 // |profile1| should have been merged into |profile2| which should then have
4947 // been merged into |profile3|. |profile4| should have been merged into
4948 // |profile5| and |profile6| should not have merged. Therefore there should be
4949 // 3 profile left.
4950 ASSERT_EQ(3U, profiles.size());
4951
4952 // Make sure the remaining profiles are the expected ones.
4953 EXPECT_EQ(profile3.guid(), profiles[0]->guid());
4954 EXPECT_EQ(profile5.guid(), profiles[1]->guid());
4955 EXPECT_EQ(profile6.guid(), profiles[2]->guid());
4956
4957 // |credit_card1|'s billing address should now be profile 3.
4958 EXPECT_EQ(profile3.guid(), credit_cards[0]->billing_address_id());
4959
4960 // |credit_card2|'s billing address should now be profile 5.
4961 EXPECT_EQ(profile5.guid(), credit_cards[1]->billing_address_id());
4962
4963 // |credit_card3|'s billing address should still be profile 6.
4964 EXPECT_EQ(profile6.guid(), credit_cards[2]->billing_address_id());
4965 }
4966
4699 // Tests that ApplyDedupingRoutine merges the profile values correctly, i.e. 4967 // Tests that ApplyDedupingRoutine merges the profile values correctly, i.e.
4700 // never lose information and keep the syntax of the profile with the higher 4968 // never lose information and keep the syntax of the profile with the higher
4701 // frecency score. 4969 // frecency score.
4702 TEST_F(PersonalDataManagerTest, ApplyDedupingRoutine_MergedProfileValues) { 4970 TEST_F(PersonalDataManagerTest, ApplyDedupingRoutine_MergedProfileValues) {
4703 // Create a profile with a higher frecency score. 4971 // Create a profile with a higher frecency score.
4704 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com"); 4972 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
4705 test::SetProfileInfo(&profile1, "Homer", "J", "Simpson", 4973 test::SetProfileInfo(&profile1, "Homer", "J", "Simpson",
4706 "homer.simpson@abc.com", "", "742. Evergreen Terrace", 4974 "homer.simpson@abc.com", "", "742. Evergreen Terrace",
4707 "", "Springfield", "IL", "91601", "US", ""); 4975 "", "Springfield", "IL", "91601", "US", "");
4708 profile1.set_use_count(10); 4976 profile1.set_use_count(10);
(...skipping 669 matching lines...) Expand 10 before | Expand all | Expand 10 after
5378 EnableAutofillProfileCleanup(); 5646 EnableAutofillProfileCleanup();
5379 5647
5380 // The deduping routine should not be run. 5648 // The deduping routine should not be run.
5381 EXPECT_FALSE(personal_data_->ApplyDedupingRoutine()); 5649 EXPECT_FALSE(personal_data_->ApplyDedupingRoutine());
5382 5650
5383 // The two duplicate profiles should still be present. 5651 // The two duplicate profiles should still be present.
5384 EXPECT_EQ(2U, personal_data_->GetProfiles().size()); 5652 EXPECT_EQ(2U, personal_data_->GetProfiles().size());
5385 } 5653 }
5386 5654
5387 } // namespace autofill 5655 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/personal_data_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698