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