OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/autofill/core/browser/personal_data_manager.h" | 5 #include "components/autofill/core/browser/personal_data_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <list> | 10 #include <list> |
(...skipping 4517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4528 | 4528 |
4529 // Erase the profiles for the next test. | 4529 // Erase the profiles for the next test. |
4530 ResetProfiles(); | 4530 ResetProfiles(); |
4531 } | 4531 } |
4532 } | 4532 } |
4533 | 4533 |
4534 // Tests that MergeProfile tries to merge the imported profile into the | 4534 // Tests that MergeProfile tries to merge the imported profile into the |
4535 // existing profile in decreasing order of frecency. | 4535 // existing profile in decreasing order of frecency. |
4536 TEST_F(PersonalDataManagerTest, MergeProfile_Frecency) { | 4536 TEST_F(PersonalDataManagerTest, MergeProfile_Frecency) { |
4537 // Create two very similar profiles except with different company names. | 4537 // Create two very similar profiles except with different company names. |
4538 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com"); | 4538 std::unique_ptr<AutofillProfile> profile1 = base::MakeUnique<AutofillProfile>( |
4539 test::SetProfileInfo(&profile1, "Homer", "Jay", "Simpson", | 4539 base::GenerateGUID(), "https://www.example.com"); |
| 4540 test::SetProfileInfo(profile1.get(), "Homer", "Jay", "Simpson", |
4540 "homer.simpson@abc.com", "SNP", "742 Evergreen Terrace", | 4541 "homer.simpson@abc.com", "SNP", "742 Evergreen Terrace", |
4541 "", "Springfield", "IL", "91601", "US", "12345678910"); | 4542 "", "Springfield", "IL", "91601", "US", "12345678910"); |
4542 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com"); | 4543 AutofillProfile* profile2 = |
4543 test::SetProfileInfo(&profile2, "Homer", "Jay", "Simpson", | 4544 new AutofillProfile(base::GenerateGUID(), "https://www.example.com"); |
| 4545 test::SetProfileInfo(profile2, "Homer", "Jay", "Simpson", |
4544 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace", | 4546 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace", |
4545 "", "Springfield", "IL", "91601", "US", "12345678910"); | 4547 "", "Springfield", "IL", "91601", "US", "12345678910"); |
4546 | 4548 |
4547 // Give the "Fox" profile a bigger frecency score. | 4549 // Give the "Fox" profile a bigger frecency score. |
4548 profile2.set_use_count(15); | 4550 profile2->set_use_count(15); |
4549 | 4551 |
4550 // Create the |existing_profiles| vector. | 4552 // Create the |existing_profiles| vector. |
4551 std::vector<AutofillProfile*> existing_profiles; | 4553 std::vector<std::unique_ptr<AutofillProfile>> existing_profiles; |
4552 existing_profiles.push_back(&profile1); | 4554 existing_profiles.push_back(std::move(profile1)); |
4553 existing_profiles.push_back(&profile2); | 4555 existing_profiles.push_back(base::WrapUnique(profile2)); |
4554 | 4556 |
4555 // Create a new imported profile with no company name. | 4557 // Create a new imported profile with no company name. |
4556 AutofillProfile imported_profile(base::GenerateGUID(), | 4558 AutofillProfile imported_profile(base::GenerateGUID(), |
4557 "https://www.example.com"); | 4559 "https://www.example.com"); |
4558 test::SetProfileInfo(&imported_profile, "Homer", "Jay", "Simpson", | 4560 test::SetProfileInfo(&imported_profile, "Homer", "Jay", "Simpson", |
4559 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "", | 4561 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "", |
4560 "Springfield", "IL", "91601", "US", "12345678910"); | 4562 "Springfield", "IL", "91601", "US", "12345678910"); |
4561 | 4563 |
4562 // Merge the imported profile into the existing profiles. | 4564 // Merge the imported profile into the existing profiles. |
4563 std::vector<AutofillProfile> profiles; | 4565 std::vector<AutofillProfile> profiles; |
4564 std::string guid = personal_data_->MergeProfile( | 4566 std::string guid = personal_data_->MergeProfile( |
4565 imported_profile, existing_profiles, "US-EN", &profiles); | 4567 imported_profile, &existing_profiles, "US-EN", &profiles); |
4566 | 4568 |
4567 // The new profile should be merged into the "fox" profile. | 4569 // The new profile should be merged into the "fox" profile. |
4568 EXPECT_EQ(profile2.guid(), guid); | 4570 EXPECT_EQ(profile2->guid(), guid); |
4569 } | 4571 } |
4570 | 4572 |
4571 // Tests that MergeProfile produces a merged profile with the expected usage | 4573 // Tests that MergeProfile produces a merged profile with the expected usage |
4572 // statistics. | 4574 // statistics. |
4573 TEST_F(PersonalDataManagerTest, MergeProfile_UsageStats) { | 4575 TEST_F(PersonalDataManagerTest, MergeProfile_UsageStats) { |
4574 // Create an initial profile with a use count of 10, an old use date and an | 4576 // Create an initial profile with a use count of 10, an old use date and an |
4575 // old modification date of 4 days ago. | 4577 // old modification date of 4 days ago. |
4576 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com"); | 4578 AutofillProfile* profile = |
4577 test::SetProfileInfo(&profile, "Homer", "Jay", "Simpson", | 4579 new AutofillProfile(base::GenerateGUID(), "https://www.example.com"); |
| 4580 test::SetProfileInfo(profile, "Homer", "Jay", "Simpson", |
4578 "homer.simpson@abc.com", "SNP", "742 Evergreen Terrace", | 4581 "homer.simpson@abc.com", "SNP", "742 Evergreen Terrace", |
4579 "", "Springfield", "IL", "91601", "US", "12345678910"); | 4582 "", "Springfield", "IL", "91601", "US", "12345678910"); |
4580 profile.set_use_count(4U); | 4583 profile->set_use_count(4U); |
4581 profile.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(4)); | 4584 profile->set_use_date(base::Time::Now() - base::TimeDelta::FromDays(4)); |
4582 profile.set_modification_date(base::Time::Now() - | 4585 profile->set_modification_date(base::Time::Now() - |
4583 base::TimeDelta::FromDays(4)); | 4586 base::TimeDelta::FromDays(4)); |
4584 | 4587 |
4585 // Create the |existing_profiles| vector. | 4588 // Create the |existing_profiles| vector. |
4586 std::vector<AutofillProfile*> existing_profiles; | 4589 std::vector<std::unique_ptr<AutofillProfile>> existing_profiles; |
4587 existing_profiles.push_back(&profile); | 4590 existing_profiles.push_back(base::WrapUnique(profile)); |
4588 | 4591 |
4589 // Create a new imported profile that will get merged with the existing one. | 4592 // Create a new imported profile that will get merged with the existing one. |
4590 AutofillProfile imported_profile(base::GenerateGUID(), | 4593 AutofillProfile imported_profile(base::GenerateGUID(), |
4591 "https://www.example.com"); | 4594 "https://www.example.com"); |
4592 test::SetProfileInfo(&imported_profile, "Homer", "Jay", "Simpson", | 4595 test::SetProfileInfo(&imported_profile, "Homer", "Jay", "Simpson", |
4593 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "", | 4596 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "", |
4594 "Springfield", "IL", "91601", "US", "12345678910"); | 4597 "Springfield", "IL", "91601", "US", "12345678910"); |
4595 | 4598 |
4596 // Merge the imported profile into the existing profiles. | 4599 // Merge the imported profile into the existing profiles. |
4597 std::vector<AutofillProfile> profiles; | 4600 std::vector<AutofillProfile> profiles; |
4598 std::string guid = personal_data_->MergeProfile( | 4601 std::string guid = personal_data_->MergeProfile( |
4599 imported_profile, existing_profiles, "US-EN", &profiles); | 4602 imported_profile, &existing_profiles, "US-EN", &profiles); |
4600 | 4603 |
4601 // The new profile should be merged into the existing profile. | 4604 // The new profile should be merged into the existing profile. |
4602 EXPECT_EQ(profile.guid(), guid); | 4605 EXPECT_EQ(profile->guid(), guid); |
4603 // The use count should have be max(4, 1) => 4. | 4606 // The use count should have be max(4, 1) => 4. |
4604 EXPECT_EQ(4U, profile.use_count()); | 4607 EXPECT_EQ(4U, profile->use_count()); |
4605 // The use date and modification dates should have been set to less than 500 | 4608 // The use date and modification dates should have been set to less than 500 |
4606 // milliseconds ago. | 4609 // milliseconds ago. |
4607 EXPECT_GT(base::TimeDelta::FromMilliseconds(500), | 4610 EXPECT_GT(base::TimeDelta::FromMilliseconds(500), |
4608 base::Time::Now() - profile.use_date()); | 4611 base::Time::Now() - profile->use_date()); |
4609 EXPECT_GT(base::TimeDelta::FromMilliseconds(500), | 4612 EXPECT_GT(base::TimeDelta::FromMilliseconds(500), |
4610 base::Time::Now() - profile.modification_date()); | 4613 base::Time::Now() - profile->modification_date()); |
4611 } | 4614 } |
4612 | 4615 |
4613 // Tests that DedupeProfiles sets the correct profile guids to | 4616 // Tests that DedupeProfiles sets the correct profile guids to |
4614 // delete after merging similar profiles. | 4617 // delete after merging similar profiles. |
4615 TEST_F(PersonalDataManagerTest, DedupeProfiles_ProfilesToDelete) { | 4618 TEST_F(PersonalDataManagerTest, DedupeProfiles_ProfilesToDelete) { |
4616 // Create the profile for which to find duplicates. It has the highest | 4619 // Create the profile for which to find duplicates. It has the highest |
4617 // frecency. | 4620 // frecency. |
4618 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com"); | 4621 AutofillProfile* profile1 = |
4619 test::SetProfileInfo(&profile1, "Homer", "Jay", "Simpson", | 4622 new AutofillProfile(base::GenerateGUID(), "https://www.example.com"); |
| 4623 test::SetProfileInfo(profile1, "Homer", "Jay", "Simpson", |
4620 "homer.simpson@abc.com", "", "742. Evergreen Terrace", | 4624 "homer.simpson@abc.com", "", "742. Evergreen Terrace", |
4621 "", "Springfield", "IL", "91601", "US", "12345678910"); | 4625 "", "Springfield", "IL", "91601", "US", "12345678910"); |
4622 profile1.set_use_count(9); | 4626 profile1->set_use_count(9); |
4623 | 4627 |
4624 // Create a different profile that should not be deduped (different address). | 4628 // Create a different profile that should not be deduped (different address). |
4625 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com"); | 4629 AutofillProfile* profile2 = |
4626 test::SetProfileInfo(&profile2, "Homer", "Jay", "Simpson", | 4630 new AutofillProfile(base::GenerateGUID(), "https://www.example.com"); |
| 4631 test::SetProfileInfo(profile2, "Homer", "Jay", "Simpson", |
4627 "homer.simpson@abc.com", "Fox", "1234 Other Street", "", | 4632 "homer.simpson@abc.com", "Fox", "1234 Other Street", "", |
4628 "Springfield", "IL", "91601", "US", "12345678910"); | 4633 "Springfield", "IL", "91601", "US", "12345678910"); |
4629 profile2.set_use_count(7); | 4634 profile2->set_use_count(7); |
4630 | 4635 |
4631 // Create a profile similar to profile1 which should be deduped. | 4636 // Create a profile similar to profile1 which should be deduped. |
4632 AutofillProfile profile3(base::GenerateGUID(), "https://www.example.com"); | 4637 AutofillProfile* profile3 = |
4633 test::SetProfileInfo(&profile3, "Homer", "Jay", "Simpson", | 4638 new AutofillProfile(base::GenerateGUID(), "https://www.example.com"); |
| 4639 test::SetProfileInfo(profile3, "Homer", "Jay", "Simpson", |
4634 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "", | 4640 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "", |
4635 "Springfield", "IL", "91601", "US", "12345678910"); | 4641 "Springfield", "IL", "91601", "US", "12345678910"); |
4636 profile3.set_use_count(5); | 4642 profile3->set_use_count(5); |
4637 | 4643 |
4638 // Create another different profile that should not be deduped (different | 4644 // Create another different profile that should not be deduped (different |
4639 // name). | 4645 // name). |
4640 AutofillProfile profile4(base::GenerateGUID(), "https://www.example.com"); | 4646 AutofillProfile* profile4 = |
4641 test::SetProfileInfo(&profile4, "Marjorie", "Jacqueline", "Simpson", | 4647 new AutofillProfile(base::GenerateGUID(), "https://www.example.com"); |
| 4648 test::SetProfileInfo(profile4, "Marjorie", "Jacqueline", "Simpson", |
4642 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace", | 4649 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace", |
4643 "", "Springfield", "IL", "91601", "US", "12345678910"); | 4650 "", "Springfield", "IL", "91601", "US", "12345678910"); |
4644 profile4.set_use_count(3); | 4651 profile4->set_use_count(3); |
4645 | 4652 |
4646 // Create another profile similar to profile1. Since that one has the lowest | 4653 // Create another profile similar to profile1. Since that one has the lowest |
4647 // frecency, the result of the merge should be in this profile at the end of | 4654 // frecency, the result of the merge should be in this profile at the end of |
4648 // the test. | 4655 // the test. |
4649 AutofillProfile profile5(base::GenerateGUID(), "https://www.example.com"); | 4656 AutofillProfile* profile5 = |
4650 test::SetProfileInfo(&profile5, "Homer", "Jay", "Simpson", | 4657 new AutofillProfile(base::GenerateGUID(), "https://www.example.com"); |
| 4658 test::SetProfileInfo(profile5, "Homer", "Jay", "Simpson", |
4651 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.", | 4659 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.", |
4652 "", "Springfield", "IL", "91601", "US", "12345678910"); | 4660 "", "Springfield", "IL", "91601", "US", "12345678910"); |
4653 profile5.set_use_count(1); | 4661 profile5->set_use_count(1); |
4654 | 4662 |
4655 // Add the profiles. | 4663 // Add the profiles. |
4656 std::vector<AutofillProfile*> existing_profiles; | 4664 std::vector<std::unique_ptr<AutofillProfile>> existing_profiles; |
4657 existing_profiles.push_back(&profile1); | 4665 existing_profiles.push_back(base::WrapUnique(profile1)); |
4658 existing_profiles.push_back(&profile2); | 4666 existing_profiles.push_back(base::WrapUnique(profile2)); |
4659 existing_profiles.push_back(&profile3); | 4667 existing_profiles.push_back(base::WrapUnique(profile3)); |
4660 existing_profiles.push_back(&profile4); | 4668 existing_profiles.push_back(base::WrapUnique(profile4)); |
4661 existing_profiles.push_back(&profile5); | 4669 existing_profiles.push_back(base::WrapUnique(profile5)); |
4662 | 4670 |
4663 // Enable the profile cleanup. | 4671 // Enable the profile cleanup. |
4664 EnableAutofillProfileCleanup(); | 4672 EnableAutofillProfileCleanup(); |
4665 | 4673 |
4666 base::HistogramTester histogram_tester; | 4674 base::HistogramTester histogram_tester; |
4667 std::unordered_set<AutofillProfile*> profiles_to_delete; | 4675 std::unordered_set<AutofillProfile*> profiles_to_delete; |
4668 personal_data_->DedupeProfiles(&existing_profiles, &profiles_to_delete); | 4676 personal_data_->DedupeProfiles(&existing_profiles, &profiles_to_delete); |
4669 // 5 profiles were considered for dedupe. | 4677 // 5 profiles were considered for dedupe. |
4670 histogram_tester.ExpectUniqueSample( | 4678 histogram_tester.ExpectUniqueSample( |
4671 "Autofill.NumberOfProfilesConsideredForDedupe", 5, 1); | 4679 "Autofill.NumberOfProfilesConsideredForDedupe", 5, 1); |
4672 // 2 profiles were removed (profiles 1 and 3). | 4680 // 2 profiles were removed (profiles 1 and 3). |
4673 histogram_tester.ExpectUniqueSample( | 4681 histogram_tester.ExpectUniqueSample( |
4674 "Autofill.NumberOfProfilesRemovedDuringDedupe", 2, 1); | 4682 "Autofill.NumberOfProfilesRemovedDuringDedupe", 2, 1); |
4675 | 4683 |
4676 // Profile1 should be deleted because it was sent as the profile to merge and | 4684 // Profile1 should be deleted because it was sent as the profile to merge and |
4677 // thus was merged into profile3 and then into profile5. | 4685 // thus was merged into profile3 and then into profile5. |
4678 EXPECT_TRUE(profiles_to_delete.count(&profile1)); | 4686 EXPECT_TRUE(profiles_to_delete.count(profile1)); |
4679 | 4687 |
4680 // Profile3 should be deleted because profile1 was merged into it and the | 4688 // Profile3 should be deleted because profile1 was merged into it and the |
4681 // resulting profile was then merged into profile5. | 4689 // resulting profile was then merged into profile5. |
4682 EXPECT_TRUE(profiles_to_delete.count(&profile3)); | 4690 EXPECT_TRUE(profiles_to_delete.count(profile3)); |
4683 | 4691 |
4684 // Only these two profiles should be deleted. | 4692 // Only these two profiles should be deleted. |
4685 EXPECT_EQ(2U, profiles_to_delete.size()); | 4693 EXPECT_EQ(2U, profiles_to_delete.size()); |
4686 | 4694 |
4687 // All profiles should still be present in |existing_profiles|. | 4695 // All profiles should still be present in |existing_profiles|. |
4688 EXPECT_EQ(5U, existing_profiles.size()); | 4696 EXPECT_EQ(5U, existing_profiles.size()); |
4689 } | 4697 } |
4690 | 4698 |
4691 // Tests that ApplyDedupingRoutine merges the profile values correctly, i.e. | 4699 // Tests that ApplyDedupingRoutine merges the profile values correctly, i.e. |
4692 // never lose information and keep the syntax of the profile with the higher | 4700 // never lose information and keep the syntax of the profile with the higher |
(...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5370 EnableAutofillProfileCleanup(); | 5378 EnableAutofillProfileCleanup(); |
5371 | 5379 |
5372 // The deduping routine should not be run. | 5380 // The deduping routine should not be run. |
5373 EXPECT_FALSE(personal_data_->ApplyDedupingRoutine()); | 5381 EXPECT_FALSE(personal_data_->ApplyDedupingRoutine()); |
5374 | 5382 |
5375 // The two duplicate profiles should still be present. | 5383 // The two duplicate profiles should still be present. |
5376 EXPECT_EQ(2U, personal_data_->GetProfiles().size()); | 5384 EXPECT_EQ(2U, personal_data_->GetProfiles().size()); |
5377 } | 5385 } |
5378 | 5386 |
5379 } // namespace autofill | 5387 } // namespace autofill |
OLD | NEW |