| 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 <memory> | 10 #include <memory> |
| (...skipping 4348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4359 // The use count should have been incremented by one. | 4359 // The use count should have been incremented by one. |
| 4360 EXPECT_EQ(5U, profile.use_count()); | 4360 EXPECT_EQ(5U, profile.use_count()); |
| 4361 // The use date and modification dates should have been set to less than 500 | 4361 // The use date and modification dates should have been set to less than 500 |
| 4362 // milliseconds ago. | 4362 // milliseconds ago. |
| 4363 EXPECT_GT(base::TimeDelta::FromMilliseconds(500), | 4363 EXPECT_GT(base::TimeDelta::FromMilliseconds(500), |
| 4364 base::Time::Now() - profile.use_date()); | 4364 base::Time::Now() - profile.use_date()); |
| 4365 EXPECT_GT(base::TimeDelta::FromMilliseconds(500), | 4365 EXPECT_GT(base::TimeDelta::FromMilliseconds(500), |
| 4366 base::Time::Now() - profile.modification_date()); | 4366 base::Time::Now() - profile.modification_date()); |
| 4367 } | 4367 } |
| 4368 | 4368 |
| 4369 // Tests that using a profile results in a merge of with all similar profiles |
| 4370 // and that all but the resulting profile gets deleted. Also tests that |
| 4371 // non-similar profiles are not affected by the merge or the delete. |
| 4372 TEST_F(PersonalDataManagerTest, DedupeOnInsert) { |
| 4373 // Create saved profiles. |
| 4374 // Create two very similar profiles that should be deduped. The first one has |
| 4375 // no company name, while the second has one. The second profile also has |
| 4376 // punctuation at the end of its first address line and no middle name. |
| 4377 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com"); |
| 4378 test::SetProfileInfo(&profile1, "Homer", "Jay", "Simpson", |
| 4379 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "", |
| 4380 "Springfield", "IL", "91601", "US", "12345678910"); |
| 4381 profile1.set_use_count(2); |
| 4382 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com"); |
| 4383 test::SetProfileInfo(&profile2, "Homer", "", "Simpson", |
| 4384 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.", |
| 4385 "", "Springfield", "IL", "91601", "US", "12345678910"); |
| 4386 profile2.set_use_count(3); |
| 4387 |
| 4388 // Create a different profile that should not be deduped (different address). |
| 4389 AutofillProfile profile3(base::GenerateGUID(), "https://www.example.com"); |
| 4390 test::SetProfileInfo(&profile3, "Homer", "Jay", "Simpson", |
| 4391 "homer.simpson@abc.com", "Fox", "1234 Other Street", "", |
| 4392 "Springfield", "IL", "91601", "US", "12345678910"); |
| 4393 profile3.set_use_count(4); |
| 4394 |
| 4395 // Create another different profile that should not be deduped (different |
| 4396 // name). |
| 4397 AutofillProfile profile4(base::GenerateGUID(), "https://www.example.com"); |
| 4398 test::SetProfileInfo(&profile4, "Marjorie", "Jacqueline", "Simpson", |
| 4399 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace", |
| 4400 "", "Springfield", "IL", "91601", "US", "12345678910"); |
| 4401 profile4.set_use_count(1); |
| 4402 |
| 4403 personal_data_->AddProfile(profile1); |
| 4404 personal_data_->AddProfile(profile2); |
| 4405 personal_data_->AddProfile(profile3); |
| 4406 personal_data_->AddProfile(profile4); |
| 4407 |
| 4408 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 4409 .WillOnce(QuitMainMessageLoop()); |
| 4410 base::MessageLoop::current()->Run(); |
| 4411 |
| 4412 EXPECT_EQ(4U, personal_data_->GetProfiles().size()); |
| 4413 |
| 4414 // Create a new imported profile with no company name. It is similar to |
| 4415 // profiles 1 and 2 and should be merged with them. |
| 4416 AutofillProfile imported_profile(base::GenerateGUID(), |
| 4417 "https://www.example.com"); |
| 4418 test::SetProfileInfo(&imported_profile, "Homer", "Jay", "Simpson", |
| 4419 "homer.simpson@abc.com", "", "742. Evergreen Terrace", |
| 4420 "", "Springfield", "IL", "91601", "US", "12345678910"); |
| 4421 |
| 4422 // Save the imported profile (use it). |
| 4423 personal_data_->SaveImportedProfile(imported_profile); |
| 4424 |
| 4425 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 4426 .WillOnce(QuitMainMessageLoop()); |
| 4427 base::MessageLoop::current()->Run(); |
| 4428 |
| 4429 std::vector<AutofillProfile*> profiles = personal_data_->GetProfiles(); |
| 4430 |
| 4431 // The imported profile and saved profiles 1 and 2 should be merged together. |
| 4432 // Therefore there should only be 3 saved profiles. |
| 4433 ASSERT_EQ(3U, profiles.size()); |
| 4434 |
| 4435 // Sort the profiles by frecency to have a deterministic order. |
| 4436 base::Time comparison_time = base::Time::Now(); |
| 4437 std::sort(profiles.begin(), profiles.end(), |
| 4438 [comparison_time](const AutofillDataModel* a, |
| 4439 const AutofillDataModel* b) { |
| 4440 return a->CompareFrecency(b, comparison_time); |
| 4441 }); |
| 4442 |
| 4443 // Since profiles with higher frecency scores are merged into profiles with |
| 4444 // lower frecency scores, the result of the merge should be contained in |
| 4445 // profile1 since it had a lower frecency score compared to profile2. |
| 4446 EXPECT_EQ(profile1.guid(), profiles[0]->guid()); |
| 4447 // Even though one of the merged profiles had no middle name (|profile2|), the |
| 4448 // result of the merge should have kept the middle name from the other |
| 4449 // profiles. |
| 4450 EXPECT_EQ(UTF8ToUTF16("Jay"), profiles[0]->GetRawInfo(NAME_MIDDLE)); |
| 4451 // The address syntax that results from the merge should be the one from the |
| 4452 // imported profile (most recent). |
| 4453 EXPECT_EQ(UTF8ToUTF16("742. Evergreen Terrace"), |
| 4454 profiles[0]->GetRawInfo(ADDRESS_HOME_LINE1)); |
| 4455 // Even though the imported profile had no company name, a merge should not |
| 4456 // result in a loss of information. Therefore the company name present in |
| 4457 // |profile2| should be kept. |
| 4458 EXPECT_EQ(UTF8ToUTF16("Fox"), profiles[0]->GetRawInfo(COMPANY_NAME)); |
| 4459 |
| 4460 // Make sure the two other remaining profiles are the expected ones. |
| 4461 EXPECT_EQ(profile3.guid(), profiles[1]->guid()); |
| 4462 EXPECT_EQ(UTF8ToUTF16("1234 Other Street"), |
| 4463 profiles[1]->GetRawInfo(ADDRESS_HOME_LINE1)); |
| 4464 EXPECT_EQ(profile4.guid(), profiles[2]->guid()); |
| 4465 EXPECT_EQ(UTF8ToUTF16("Marjorie"), profiles[2]->GetRawInfo(NAME_FIRST)); |
| 4466 } |
| 4467 |
| 4468 // Tests that FindAndMergeDuplicateProfiles sets the correct profile guids to |
| 4469 // delete after merging similar profiles. |
| 4470 TEST_F(PersonalDataManagerTest, |
| 4471 FindAndMergeDuplicateProfiles_ProfilesToDelete) { |
| 4472 // Create the profile for which to find duplicates. |
| 4473 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com"); |
| 4474 test::SetProfileInfo(&profile1, "Homer", "Jay", "Simpson", |
| 4475 "homer.simpson@abc.com", "", "742. Evergreen Terrace", |
| 4476 "", "Springfield", "IL", "91601", "US", "12345678910"); |
| 4477 |
| 4478 // Create a different profile that should not be deduped (different address). |
| 4479 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com"); |
| 4480 test::SetProfileInfo(&profile2, "Homer", "Jay", "Simpson", |
| 4481 "homer.simpson@abc.com", "Fox", "1234 Other Street", "", |
| 4482 "Springfield", "IL", "91601", "US", "12345678910"); |
| 4483 |
| 4484 // Create a profile similar to profile1 which should be deduped. |
| 4485 AutofillProfile profile3(base::GenerateGUID(), "https://www.example.com"); |
| 4486 test::SetProfileInfo(&profile3, "Homer", "Jay", "Simpson", |
| 4487 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "", |
| 4488 "Springfield", "IL", "91601", "US", "12345678910"); |
| 4489 |
| 4490 // Create another different profile that should not be deduped (different |
| 4491 // name). |
| 4492 AutofillProfile profile4(base::GenerateGUID(), "https://www.example.com"); |
| 4493 test::SetProfileInfo(&profile4, "Marjorie", "Jacqueline", "Simpson", |
| 4494 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace", |
| 4495 "", "Springfield", "IL", "91601", "US", "12345678910"); |
| 4496 |
| 4497 // Create another profile similar to profile1. Since that one is last, the |
| 4498 // result of the merge should be in this profile at the end of the test. |
| 4499 AutofillProfile profile5(base::GenerateGUID(), "https://www.example.com"); |
| 4500 test::SetProfileInfo(&profile5, "Homer", "Jay", "Simpson", |
| 4501 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.", |
| 4502 "", "Springfield", "IL", "91601", "US", "12345678910"); |
| 4503 |
| 4504 std::vector<AutofillProfile*> existing_profiles; |
| 4505 existing_profiles.push_back(&profile1); |
| 4506 existing_profiles.push_back(&profile2); |
| 4507 existing_profiles.push_back(&profile3); |
| 4508 existing_profiles.push_back(&profile4); |
| 4509 existing_profiles.push_back(&profile5); |
| 4510 |
| 4511 std::vector<std::string> guids_to_delete; |
| 4512 personal_data_->FindAndMergeDuplicateProfiles(existing_profiles, &profile1, |
| 4513 &guids_to_delete); |
| 4514 |
| 4515 // Profile1 should be deleted because it was sent as the profile to merge and |
| 4516 // thus was merged into profile3 and then into profile5. |
| 4517 EXPECT_TRUE(std::find(guids_to_delete.begin(), guids_to_delete.end(), |
| 4518 profile1.guid()) != guids_to_delete.end()); |
| 4519 |
| 4520 // Profile3 should be deleted because profile1 was merged into it and the |
| 4521 // resulting profile was then merged into profile5. |
| 4522 EXPECT_TRUE(std::find(guids_to_delete.begin(), guids_to_delete.end(), |
| 4523 profile3.guid()) != guids_to_delete.end()); |
| 4524 |
| 4525 // Only these two profiles should be deleted. |
| 4526 EXPECT_EQ(2U, guids_to_delete.size()); |
| 4527 |
| 4528 // All profiles should still be present in |existing_profiles|. |
| 4529 EXPECT_EQ(5U, existing_profiles.size()); |
| 4530 } |
| 4531 |
| 4532 // Tests that FindAndMergeDuplicateProfiles merges the profile values correctly, |
| 4533 // ie: never lose information and keep the syntax of the profile with the higher |
| 4534 // frecency score. |
| 4535 TEST_F(PersonalDataManagerTest, |
| 4536 FindAndMergeDuplicateProfiles_MergedProfileValues) { |
| 4537 // Create a saved profile with a higher frecency score. |
| 4538 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com"); |
| 4539 test::SetProfileInfo(&profile1, "Homer", "Jay", "Simpson", |
| 4540 "homer.simpson@abc.com", "", "742 Evergreen Terrace", "", |
| 4541 "Springfield", "IL", "91601", "", "12345678910"); |
| 4542 profile1.set_use_count(5); |
| 4543 profile1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3)); |
| 4544 |
| 4545 // Create a saved profile with a lower frecency score. |
| 4546 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com"); |
| 4547 test::SetProfileInfo(&profile2, "Homer", "J", "Simpson", |
| 4548 "homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.", |
| 4549 "", "Springfield", "IL", "91601", "", ""); |
| 4550 profile2.set_use_count(3); |
| 4551 profile2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(5)); |
| 4552 |
| 4553 personal_data_->AddProfile(profile1); |
| 4554 personal_data_->AddProfile(profile2); |
| 4555 |
| 4556 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 4557 .WillOnce(QuitMainMessageLoop()); |
| 4558 base::MessageLoop::current()->Run(); |
| 4559 |
| 4560 EXPECT_EQ(2U, personal_data_->GetProfiles().size()); |
| 4561 |
| 4562 // Create a new imported profile to be merged with the saved profiles. |
| 4563 AutofillProfile imported_profile(base::GenerateGUID(), |
| 4564 "https://www.example.com"); |
| 4565 test::SetProfileInfo(&imported_profile, "Homer", "J", "Simpson", |
| 4566 "homer.simpson@abc.com", "", "742. Evergreen Terrace", |
| 4567 "", "Springfield", "IL", "91601", "US", ""); |
| 4568 |
| 4569 personal_data_->SaveImportedProfile(imported_profile); |
| 4570 |
| 4571 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 4572 .WillOnce(QuitMainMessageLoop()); |
| 4573 base::MessageLoop::current()->Run(); |
| 4574 |
| 4575 std::vector<AutofillProfile*> profiles = personal_data_->GetProfiles(); |
| 4576 |
| 4577 // The imported profile and saved profiles 1 and 2 should be merged together. |
| 4578 // Therefore there should only be 1 saved profile. |
| 4579 ASSERT_EQ(1U, profiles.size()); |
| 4580 |
| 4581 // Since profiles with higher frecency scores are merged into profiles with |
| 4582 // lower frecency scores, the result of the merge should be contained in |
| 4583 // profile2 since it had a lower frecency score compared to profile1. |
| 4584 EXPECT_EQ(profile2.guid(), profiles[0]->guid()); |
| 4585 // The address syntax that results from the merge should be the one from the |
| 4586 // imported profile (highest frecency). |
| 4587 EXPECT_EQ(UTF8ToUTF16("742. Evergreen Terrace"), |
| 4588 profiles[0]->GetRawInfo(ADDRESS_HOME_LINE1)); |
| 4589 // The middle name should be full, even if the profile with the higher |
| 4590 // frecency only had an initial (no loss of information). |
| 4591 EXPECT_EQ(UTF8ToUTF16("Jay"), profiles[0]->GetRawInfo(NAME_MIDDLE)); |
| 4592 // The specified phone number from profile1 should be kept (no loss of |
| 4593 // information). |
| 4594 EXPECT_EQ(UTF8ToUTF16("12345678910"), |
| 4595 profiles[0]->GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); |
| 4596 // The specified company name from profile2 should be kept (no loss of |
| 4597 // information). |
| 4598 EXPECT_EQ(UTF8ToUTF16("Fox"), profiles[0]->GetRawInfo(COMPANY_NAME)); |
| 4599 // The specified country from the imported profile shoudl be kept (no loss of |
| 4600 // information). |
| 4601 EXPECT_EQ(UTF8ToUTF16("US"), profiles[0]->GetRawInfo(ADDRESS_HOME_COUNTRY)); |
| 4602 // The use count that results from the merge should be the sum of the two |
| 4603 // saved profiles plus 1 (imported profile count). |
| 4604 EXPECT_EQ(profile1.use_count() + profile2.use_count() + |
| 4605 imported_profile.use_count(), |
| 4606 profiles[0]->use_count()); |
| 4607 // The use date that results from the merge should be the one from the |
| 4608 // imported profile since it was used just now. |
| 4609 EXPECT_LT(base::Time::Now() - base::TimeDelta::FromSeconds(10), |
| 4610 profiles[0]->use_date()); |
| 4611 } |
| 4612 |
| 4369 } // namespace autofill | 4613 } // namespace autofill |
| OLD | NEW |