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

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

Issue 2075503002: [Autofill] Fix the dedupe of verified profiles. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed comments Created 4 years, 6 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
« 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 2716 matching lines...) Expand 10 before | Expand all | Expand 10 after
2727 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) 2727 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2728 .WillOnce(QuitMainMessageLoop()); 2728 .WillOnce(QuitMainMessageLoop());
2729 base::MessageLoop::current()->Run(); 2729 base::MessageLoop::current()->Run();
2730 2730
2731 // The new profile should be merged into the existing one. 2731 // The new profile should be merged into the existing one.
2732 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles(); 2732 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2733 ASSERT_EQ(1U, results.size()); 2733 ASSERT_EQ(1U, results.size());
2734 EXPECT_EQ(0, new_verified_profile.Compare(*results[0])); 2734 EXPECT_EQ(0, new_verified_profile.Compare(*results[0]));
2735 } 2735 }
2736 2736
2737 // Ensure that verified profiles can be saved via SaveImportedProfile,
2738 // overwriting existing verified profiles as well.
2739 TEST_F(PersonalDataManagerTest, SaveImportedProfileWithExistingVerifiedData) {
2740 // Start with a verified profile.
2741 AutofillProfile profile(base::GenerateGUID(), kSettingsOrigin);
2742 test::SetProfileInfo(&profile,
2743 "Marion", "Mitchell", "Morrison",
2744 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2745 "91601", "US", "12345678910");
2746 EXPECT_TRUE(profile.IsVerified());
2747
2748 // Add the profile to the database.
2749 personal_data_->AddProfile(profile);
2750
2751 // Verify that the web database has been updated and the notification sent.
2752 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2753 .WillOnce(QuitMainMessageLoop());
2754 base::MessageLoop::current()->Run();
2755
2756 AutofillProfile new_verified_profile = profile;
2757 new_verified_profile.set_guid(base::GenerateGUID());
2758 new_verified_profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
2759 ASCIIToUTF16("1 234 567-8910"));
2760 EXPECT_TRUE(new_verified_profile.IsVerified());
2761
2762 personal_data_->SaveImportedProfile(new_verified_profile);
2763
2764 // Verify that the web database has been updated and the notification sent.
2765 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2766 .WillOnce(QuitMainMessageLoop());
2767 base::MessageLoop::current()->Run();
2768
2769 // The new profile should be merged into the existing one.
2770 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2771 ASSERT_EQ(1U, results.size());
2772 EXPECT_EQ(0, new_verified_profile.Compare(*results[0]));
2773 }
2774
2775 // Ensure that verified credit cards can be saved via SaveImportedCreditCard. 2737 // Ensure that verified credit cards can be saved via SaveImportedCreditCard.
2776 TEST_F(PersonalDataManagerTest, SaveImportedCreditCardWithVerifiedData) { 2738 TEST_F(PersonalDataManagerTest, SaveImportedCreditCardWithVerifiedData) {
2777 // Start with a verified credit card. 2739 // Start with a verified credit card.
2778 CreditCard credit_card(base::GenerateGUID(), kSettingsOrigin); 2740 CreditCard credit_card(base::GenerateGUID(), kSettingsOrigin);
2779 test::SetCreditCardInfo(&credit_card, 2741 test::SetCreditCardInfo(&credit_card,
2780 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2999"); 2742 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2999");
2781 EXPECT_TRUE(credit_card.IsVerified()); 2743 EXPECT_TRUE(credit_card.IsVerified());
2782 2744
2783 // Add the credit card to the database. 2745 // Add the credit card to the database.
2784 personal_data_->AddCreditCard(credit_card); 2746 personal_data_->AddCreditCard(credit_card);
(...skipping 1668 matching lines...) Expand 10 before | Expand all | Expand 10 after
4453 "", "Springfield", "IL", "91601", "US", "12345678910"); 4415 "", "Springfield", "IL", "91601", "US", "12345678910");
4454 4416
4455 base::HistogramTester histogram_tester; 4417 base::HistogramTester histogram_tester;
4456 // Save the imported profile (use it). 4418 // Save the imported profile (use it).
4457 personal_data_->SaveImportedProfile(imported_profile); 4419 personal_data_->SaveImportedProfile(imported_profile);
4458 4420
4459 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) 4421 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
4460 .WillOnce(QuitMainMessageLoop()); 4422 .WillOnce(QuitMainMessageLoop());
4461 base::MessageLoop::current()->Run(); 4423 base::MessageLoop::current()->Run();
4462 4424
4463 std::vector<AutofillProfile*> profiles = personal_data_->GetProfiles(); 4425 // Create a similar new non verified imported profile to be merged with the
4426 // saved profiles.
4427 std::vector<AutofillProfile*> profiles =
4428 personal_data_->GetProfilesToSuggest();
4464 4429
4465 // The imported profile and saved profiles 1 and 2 should be merged together. 4430 // The imported profile and saved profiles 1 and 2 should be merged together.
4466 // Therefore there should only be 3 saved profiles. 4431 // Therefore there should only be 3 saved profiles.
4467 ASSERT_EQ(3U, profiles.size()); 4432 ASSERT_EQ(3U, profiles.size());
4468 // 4 profiles were considered for dedupe. 4433 // 4 profiles were considered for dedupe.
4469 histogram_tester.ExpectUniqueSample( 4434 histogram_tester.ExpectUniqueSample(
4470 "Autofill.NumberOfProfilesConsideredForDedupe", 4, 1); 4435 "Autofill.NumberOfProfilesConsideredForDedupe", 4, 1);
4471 // 1 profile was removed. 4436 // 1 profile was removed.
4472 histogram_tester.ExpectUniqueSample( 4437 histogram_tester.ExpectUniqueSample(
4473 "Autofill.NumberOfProfilesRemovedDuringDedupe", 1, 1); 4438 "Autofill.NumberOfProfilesRemovedDuringDedupe", 1, 1);
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
4661 // saved profiles plus 1 (imported profile count). 4626 // saved profiles plus 1 (imported profile count).
4662 EXPECT_EQ(profile1.use_count() + profile2.use_count() + 4627 EXPECT_EQ(profile1.use_count() + profile2.use_count() +
4663 imported_profile.use_count(), 4628 imported_profile.use_count(),
4664 profiles[0]->use_count()); 4629 profiles[0]->use_count());
4665 // The use date that results from the merge should be the one from the 4630 // The use date that results from the merge should be the one from the
4666 // imported profile since it was used just now. 4631 // imported profile since it was used just now.
4667 EXPECT_LT(base::Time::Now() - base::TimeDelta::FromSeconds(10), 4632 EXPECT_LT(base::Time::Now() - base::TimeDelta::FromSeconds(10),
4668 profiles[0]->use_date()); 4633 profiles[0]->use_date());
4669 } 4634 }
4670 4635
4636 // Tests that FindAndMergeDuplicateProfiles only keeps the verified profile with
4637 // its original data when deduping with similar profiles, even if it has a
4638 // higher frecency score.
4639 TEST_F(PersonalDataManagerTest,
4640 FindAndMergeDuplicateProfiles_VerifiedProfileFirst) {
4641 EnableAutofillProfileCleanup();
4642
4643 // Create a verified profile with a higher frecency score.
4644 AutofillProfile profile1(base::GenerateGUID(), kSettingsOrigin);
4645 test::SetProfileInfo(&profile1, "Homer", "Jay", "Simpson",
4646 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
4647 "Springfield", "IL", "91601", "", "12345678910");
4648 profile1.set_use_count(5);
4649 profile1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
4650
4651 // Create a similar non verified profile with a lower frecency score.
4652 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
4653 test::SetProfileInfo(&profile2, "Homer", "J", "Simpson",
4654 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
4655 "", "Springfield", "IL", "91601", "", "");
4656 profile2.set_use_count(3);
4657 profile2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(5));
4658
4659 personal_data_->AddProfile(profile1);
4660 personal_data_->AddProfile(profile2);
4661
4662 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
4663 .WillOnce(QuitMainMessageLoop());
4664 base::MessageLoop::current()->Run();
4665
4666 EXPECT_EQ(2U, personal_data_->GetProfiles().size());
4667
4668 // Create a similar new non verified imported profile to be merged with the
4669 // saved profiles.
4670 AutofillProfile imported_profile(base::GenerateGUID(),
4671 "https://www.example.com");
4672 test::SetProfileInfo(&imported_profile, "Homer", "J", "Simpson",
4673 "homer.simpson@abc.com", "", "742. Evergreen Terrace",
4674 "", "Springfield", "IL", "91601", "US", "");
4675
4676 base::HistogramTester histogram_tester;
4677 personal_data_->SaveImportedProfile(imported_profile);
4678
4679 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
4680 .WillOnce(QuitMainMessageLoop());
4681 base::MessageLoop::current()->Run();
4682
4683 std::vector<AutofillProfile*> profiles = personal_data_->GetProfiles();
4684
4685 // The |imported_profile| should have merged with |profile2|. |profile2|
4686 // should then have been discarded because it is similar to the verified
4687 // |profile1|.
4688 ASSERT_EQ(1U, profiles.size());
4689 // 2 profiles were considered for dedupe (profiles 1 and 2).
4690 histogram_tester.ExpectUniqueSample(
4691 "Autofill.NumberOfProfilesConsideredForDedupe", 2, 1);
4692 // 1 profile was removed (|profile2|).
4693 histogram_tester.ExpectUniqueSample(
4694 "Autofill.NumberOfProfilesRemovedDuringDedupe", 1, 1);
4695
4696 // Only the verified |profile2| with its original data should have been kept.
4697 EXPECT_EQ(profile1.guid(), profiles[0]->guid());
4698 EXPECT_EQ(UTF8ToUTF16("742 Evergreen Terrace"),
4699 profiles[0]->GetRawInfo(ADDRESS_HOME_LINE1));
4700 EXPECT_EQ(UTF8ToUTF16("Jay"), profiles[0]->GetRawInfo(NAME_MIDDLE));
4701 EXPECT_EQ(UTF8ToUTF16("12345678910"),
4702 profiles[0]->GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
4703 EXPECT_EQ(UTF8ToUTF16(""), profiles[0]->GetRawInfo(COMPANY_NAME));
4704 EXPECT_EQ(UTF8ToUTF16(""), profiles[0]->GetRawInfo(ADDRESS_HOME_COUNTRY));
4705 EXPECT_EQ(profile1.use_count(), profiles[0]->use_count());
4706 EXPECT_LT(profile1.use_date() - TimeDelta::FromSeconds(2),
4707 profiles[0]->use_date());
4708 EXPECT_GT(profile1.use_date() + TimeDelta::FromSeconds(2),
4709 profiles[0]->use_date());
4710 }
4711
4712 // Tests that FindAndMergeDuplicateProfiles only keeps the verified profile with
4713 // it's original data when deduping with similar profiles, even if it has a
4714 // lower frecency score.
4715 TEST_F(PersonalDataManagerTest,
4716 FindAndMergeDuplicateProfiles_VerifiedProfileLast) {
4717 EnableAutofillProfileCleanup();
4718
4719 // Create a non verified profile with a higher frecency score.
4720 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
4721 test::SetProfileInfo(&profile1, "Homer", "J", "Simpson",
4722 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
4723 "", "Springfield", "IL", "91601", "", "");
4724 profile1.set_use_count(5);
4725 profile1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
4726
4727 // Create a similar verified profile with a lower frecency score.
4728 AutofillProfile profile2(base::GenerateGUID(), kSettingsOrigin);
4729 test::SetProfileInfo(&profile2, "Homer", "Jay", "Simpson",
4730 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
4731 "Springfield", "IL", "91601", "", "12345678910");
4732 profile2.set_use_count(3);
4733 profile2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(5));
4734
4735 personal_data_->AddProfile(profile1);
4736 personal_data_->AddProfile(profile2);
4737
4738 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
4739 .WillOnce(QuitMainMessageLoop());
4740 base::MessageLoop::current()->Run();
4741
4742 EXPECT_EQ(2U, personal_data_->GetProfiles().size());
4743
4744 // Create a similar non verified imported profile to be merged with the saved
4745 // profiles.
4746 AutofillProfile imported_profile(base::GenerateGUID(),
4747 "https://www.example.com");
4748 test::SetProfileInfo(&imported_profile, "Homer", "J", "Simpson",
4749 "homer.simpson@abc.com", "", "742. Evergreen Terrace",
4750 "", "Springfield", "IL", "91601", "US", "");
4751
4752 base::HistogramTester histogram_tester;
4753 personal_data_->SaveImportedProfile(imported_profile);
4754
4755 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
4756 .WillOnce(QuitMainMessageLoop());
4757 base::MessageLoop::current()->Run();
4758
4759 std::vector<AutofillProfile*> profiles = personal_data_->GetProfiles();
4760
4761 // The |imported_profile| should have merged with |profile1|. |profile1|
4762 // should then have been discarded because it is similar to the verified
4763 // |profile2|.
4764 ASSERT_EQ(1U, profiles.size());
4765 // 2 profiles were considered for dedupe (profiles 1 and 2).
4766 histogram_tester.ExpectUniqueSample(
4767 "Autofill.NumberOfProfilesConsideredForDedupe", 2, 1);
4768 // 1 profile was removed (|profile1|).
4769 histogram_tester.ExpectUniqueSample(
4770 "Autofill.NumberOfProfilesRemovedDuringDedupe", 1, 1);
4771
4772 // Only the verified |profile2| with it's original data should have been kept.
4773 EXPECT_EQ(profile2.guid(), profiles[0]->guid());
4774 EXPECT_EQ(UTF8ToUTF16("742 Evergreen Terrace"),
4775 profiles[0]->GetRawInfo(ADDRESS_HOME_LINE1));
4776 EXPECT_EQ(UTF8ToUTF16("Jay"), profiles[0]->GetRawInfo(NAME_MIDDLE));
4777 EXPECT_EQ(UTF8ToUTF16("12345678910"),
4778 profiles[0]->GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
4779 EXPECT_EQ(UTF8ToUTF16(""), profiles[0]->GetRawInfo(COMPANY_NAME));
4780 EXPECT_EQ(UTF8ToUTF16(""), profiles[0]->GetRawInfo(ADDRESS_HOME_COUNTRY));
4781 EXPECT_EQ(profile2.use_count(), profiles[0]->use_count());
4782 EXPECT_LT(profile2.use_date() - TimeDelta::FromSeconds(2),
4783 profiles[0]->use_date());
4784 EXPECT_GT(profile2.use_date() + TimeDelta::FromSeconds(2),
4785 profiles[0]->use_date());
4786 }
4787
4788 // Tests that FindAndMergeDuplicateProfiles does not merge unverified data into
4789 // a verified profile. Also tests that two verified profiles don't get merged.
4790 TEST_F(PersonalDataManagerTest,
4791 FindAndMergeDuplicateProfiles_MultipleVerifiedProfiles) {
4792 EnableAutofillProfileCleanup();
4793
4794 // Create a verified profile with a higher frecency score.
4795 AutofillProfile profile1(base::GenerateGUID(), kSettingsOrigin);
4796 test::SetProfileInfo(&profile1, "Homer", "J", "Simpson",
4797 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
4798 "", "Springfield", "IL", "91601", "", "");
4799 profile1.set_use_count(5);
4800 profile1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
4801
4802 // Create a similar verified profile with a lower frecency score.
4803 AutofillProfile profile2(base::GenerateGUID(), kSettingsOrigin);
4804 test::SetProfileInfo(&profile2, "Homer", "Jay", "Simpson",
4805 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
4806 "Springfield", "IL", "91601", "", "12345678910");
4807 profile2.set_use_count(3);
4808 profile2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(5));
4809
4810 personal_data_->AddProfile(profile1);
4811 personal_data_->AddProfile(profile2);
4812
4813 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
4814 .WillOnce(QuitMainMessageLoop());
4815 base::MessageLoop::current()->Run();
4816
4817 EXPECT_EQ(2U, personal_data_->GetProfiles().size());
4818
4819 // Create a non verified imported profile to be merged with the saved
4820 // profiles.
4821 AutofillProfile imported_profile(base::GenerateGUID(),
4822 "https://www.example.com");
4823 test::SetProfileInfo(&imported_profile, "Homer", "J", "Simpson",
4824 "homer.simpson@abc.com", "", "742. Evergreen Terrace",
4825 "", "Springfield", "IL", "91601", "US", "");
4826
4827 base::HistogramTester histogram_tester;
4828 personal_data_->SaveImportedProfile(imported_profile);
4829
4830 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
4831 .WillOnce(QuitMainMessageLoop());
4832 base::MessageLoop::current()->Run();
4833
4834 // Get the profiles, sorted by frecency to have a deterministic order.
4835 std::vector<AutofillProfile*> profiles =
4836 personal_data_->GetProfilesToSuggest();
4837
4838 // The |imported_profile| should have been discarded because the saved profile
4839 // with the highest frecency score is verified (|profile1|). Therefore, the
4840 // |imported_profile|'s data should not have been merged with |profile1|'s
4841 // data. Then |profile1| should have been compared to |profile2| but they
4842 // should not have merged because both profiles are verified.
4843 ASSERT_EQ(2U, profiles.size());
4844 // 2 profiles were considered for dedupe (the 2 saved profiles).
4845 histogram_tester.ExpectUniqueSample(
4846 "Autofill.NumberOfProfilesConsideredForDedupe", 2, 1);
4847 // No profile was removed.
4848 histogram_tester.ExpectUniqueSample(
4849 "Autofill.NumberOfProfilesRemovedDuringDedupe", 0, 1);
4850
4851 EXPECT_EQ(profile1.guid(), profiles[0]->guid());
4852 EXPECT_EQ(profile2.guid(), profiles[1]->guid());
4853 // The profiles should have kept their original data.
4854 EXPECT_EQ(UTF8ToUTF16("742 Evergreen Terrace."),
4855 profiles[0]->GetRawInfo(ADDRESS_HOME_LINE1));
4856 EXPECT_EQ(UTF8ToUTF16("742 Evergreen Terrace"),
4857 profiles[1]->GetRawInfo(ADDRESS_HOME_LINE1));
4858 EXPECT_EQ(UTF8ToUTF16("J"), profiles[0]->GetRawInfo(NAME_MIDDLE));
4859 EXPECT_EQ(UTF8ToUTF16("Jay"), profiles[1]->GetRawInfo(NAME_MIDDLE));
4860 EXPECT_EQ(UTF8ToUTF16(""), profiles[0]->GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
4861 EXPECT_EQ(UTF8ToUTF16("12345678910"),
4862 profiles[1]->GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
4863 EXPECT_EQ(profile1.use_count(), profiles[0]->use_count());
4864 EXPECT_EQ(profile2.use_count(), profiles[1]->use_count());
4865 EXPECT_LT(profile1.use_date() - TimeDelta::FromSeconds(2),
4866 profiles[0]->use_date());
4867 EXPECT_GT(profile1.use_date() + TimeDelta::FromSeconds(2),
4868 profiles[0]->use_date());
4869 EXPECT_LT(profile2.use_date() - TimeDelta::FromSeconds(2),
4870 profiles[1]->use_date());
4871 EXPECT_GT(profile2.use_date() + TimeDelta::FromSeconds(2),
4872 profiles[1]->use_date());
4873 }
4874
4671 } // namespace autofill 4875 } // 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