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

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

Issue 1821053002: [Autofill] Disambiguate similar types before uploading. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 3165 matching lines...) Expand 10 before | Expand all | Expand 10 after
3176 const char guid[] = "00000000-0000-0000-0000-000000000001"; 3176 const char guid[] = "00000000-0000-0000-0000-000000000001";
3177 int response_page_id = 0; 3177 int response_page_id = 0;
3178 FormData response_data; 3178 FormData response_data;
3179 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0], 3179 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
3180 MakeFrontendID(std::string(), guid), 3180 MakeFrontendID(std::string(), guid),
3181 &response_page_id, &response_data); 3181 &response_page_id, &response_data);
3182 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID, 3182 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
3183 false); 3183 false);
3184 3184
3185 personal_data_.ClearAutofillProfiles(); 3185 personal_data_.ClearAutofillProfiles();
3186 // The default credit card is a Elvis card. It must be removed because name
3187 // fields would be detected. However at least one profile or card is needed to
3188 // start the upload process, which is why this other card is created.
3189 personal_data_.ClearCreditCards();
3190 personal_data_.CreateTestCreditCardsYearAndMonth("2012", "04");
3186 ASSERT_EQ(0u, personal_data_.GetProfiles().size()); 3191 ASSERT_EQ(0u, personal_data_.GetProfiles().size());
3187 3192
3188 // Simulate form submission. The first submission should not count the data 3193 // Simulate form submission. The first submission should not count the data
3189 // towards possible types. Therefore we expect all UNKNOWN_TYPE entries. 3194 // towards possible types. Therefore we expect all UNKNOWN_TYPE entries.
3190 ServerFieldTypeSet type_set; 3195 ServerFieldTypeSet type_set;
3191 type_set.insert(UNKNOWN_TYPE); 3196 type_set.insert(UNKNOWN_TYPE);
3192 std::vector<ServerFieldTypeSet> unknown_types(expected_types.size(), 3197 std::vector<ServerFieldTypeSet> unknown_types(expected_types.size(),
3193 type_set); 3198 type_set);
3194 autofill_manager_->set_expected_submitted_field_types(unknown_types); 3199 autofill_manager_->set_expected_submitted_field_types(unknown_types);
3195 FormSubmitted(response_data); 3200 FormSubmitted(response_data);
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
3333 test::SetProfileInfo(&profile, "Charles", "", "Baudelaire", 3338 test::SetProfileInfo(&profile, "Charles", "", "Baudelaire",
3334 "lesfleursdumal@gmail.com", "", "108 Rue Saint-Lazare", 3339 "lesfleursdumal@gmail.com", "", "108 Rue Saint-Lazare",
3335 "Apt. 10", "Paris", "Ile de France", "75008", "FR", 3340 "Apt. 10", "Paris", "Ile de France", "75008", "FR",
3336 "+33 2 49 19 70 70"); 3341 "+33 2 49 19 70 70");
3337 profile.set_guid("00000000-0000-0000-0000-000000000001"); 3342 profile.set_guid("00000000-0000-0000-0000-000000000001");
3338 profiles.push_back(profile); 3343 profiles.push_back(profile);
3339 3344
3340 // Set up the test credit cards. 3345 // Set up the test credit cards.
3341 std::vector<CreditCard> credit_cards; 3346 std::vector<CreditCard> credit_cards;
3342 CreditCard credit_card; 3347 CreditCard credit_card;
3343 test::SetCreditCardInfo(&credit_card, "Elvis Presley", "4234-5678-9012-3456", 3348 test::SetCreditCardInfo(&credit_card, "John Doe", "4234-5678-9012-3456", "04",
3344 "04", "2012"); 3349 "2012");
3345 credit_card.set_guid("00000000-0000-0000-0000-000000000003"); 3350 credit_card.set_guid("00000000-0000-0000-0000-000000000003");
3346 credit_cards.push_back(credit_card); 3351 credit_cards.push_back(credit_card);
3347 3352
3348 typedef struct { 3353 typedef struct {
3349 std::string input_value; // The value to input in the field. 3354 std::string input_value; // The value to input in the field.
3350 ServerFieldType field_type; // The expected field type to be determined. 3355 ServerFieldType field_type; // The expected field type to be determined.
3351 } TestCase; 3356 } TestCase;
3352 3357
3353 TestCase test_cases[] = { 3358 TestCase test_cases[] = {
3354 // Profile fields matches. 3359 // Profile fields matches.
(...skipping 28 matching lines...) Expand all
3383 {"FR", ADDRESS_HOME_COUNTRY}, 3388 {"FR", ADDRESS_HOME_COUNTRY},
3384 {"France", ADDRESS_HOME_COUNTRY}, 3389 {"France", ADDRESS_HOME_COUNTRY},
3385 {"33249197070", PHONE_HOME_WHOLE_NUMBER}, 3390 {"33249197070", PHONE_HOME_WHOLE_NUMBER},
3386 {"+33 2 49 19 70 70", PHONE_HOME_WHOLE_NUMBER}, 3391 {"+33 2 49 19 70 70", PHONE_HOME_WHOLE_NUMBER},
3387 {"2 49 19 70 70", PHONE_HOME_CITY_AND_NUMBER}, 3392 {"2 49 19 70 70", PHONE_HOME_CITY_AND_NUMBER},
3388 {"249197070", PHONE_HOME_CITY_AND_NUMBER}, 3393 {"249197070", PHONE_HOME_CITY_AND_NUMBER},
3389 {"33", PHONE_HOME_COUNTRY_CODE}, 3394 {"33", PHONE_HOME_COUNTRY_CODE},
3390 {"2", PHONE_HOME_CITY_CODE}, 3395 {"2", PHONE_HOME_CITY_CODE},
3391 3396
3392 // Credit card fields matches. 3397 // Credit card fields matches.
3393 {"Elvis Presley", CREDIT_CARD_NAME_FULL}, 3398 {"John Doe", CREDIT_CARD_NAME_FULL},
3399 {"John", CREDIT_CARD_NAME_FIRST},
3400 {"Doe", CREDIT_CARD_NAME_LAST},
3394 {"4234-5678-9012-3456", CREDIT_CARD_NUMBER}, 3401 {"4234-5678-9012-3456", CREDIT_CARD_NUMBER},
3395 {"04", CREDIT_CARD_EXP_MONTH}, 3402 {"04", CREDIT_CARD_EXP_MONTH},
3396 {"April", CREDIT_CARD_EXP_MONTH}, 3403 {"April", CREDIT_CARD_EXP_MONTH},
3397 {"2012", CREDIT_CARD_EXP_4_DIGIT_YEAR}, 3404 {"2012", CREDIT_CARD_EXP_4_DIGIT_YEAR},
3398 {"12", CREDIT_CARD_EXP_2_DIGIT_YEAR}, 3405 {"12", CREDIT_CARD_EXP_2_DIGIT_YEAR},
3399 {"04/2012", CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR}, 3406 {"04/2012", CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR},
3400 3407
3401 // Make sure whitespaces and invalid characters are handled properly. 3408 // Make sure whitespaces and invalid characters are handled properly.
3402 {"", EMPTY_TYPE}, 3409 {"", EMPTY_TYPE},
3403 {" ", EMPTY_TYPE}, 3410 {" ", EMPTY_TYPE},
(...skipping 23 matching lines...) Expand all
3427 {"56", UNKNOWN_TYPE}, 3434 {"56", UNKNOWN_TYPE},
3428 {"901", UNKNOWN_TYPE}}; 3435 {"901", UNKNOWN_TYPE}};
3429 3436
3430 for (TestCase test_case : test_cases) { 3437 for (TestCase test_case : test_cases) {
3431 FormData form; 3438 FormData form;
3432 form.name = ASCIIToUTF16("MyForm"); 3439 form.name = ASCIIToUTF16("MyForm");
3433 form.origin = GURL("http://myform.com/form.html"); 3440 form.origin = GURL("http://myform.com/form.html");
3434 form.action = GURL("http://myform.com/submit.html"); 3441 form.action = GURL("http://myform.com/submit.html");
3435 3442
3436 FormFieldData field; 3443 FormFieldData field;
3437 ServerFieldTypeSet types;
3438 test::CreateTestFormField("", "1", "", "text", &field); 3444 test::CreateTestFormField("", "1", "", "text", &field);
3439 field.value = ASCIIToUTF16(test_case.input_value); 3445 field.value = ASCIIToUTF16(test_case.input_value);
3440 types.clear();
3441 types.insert(test_case.field_type);
3442 form.fields.push_back(field); 3446 form.fields.push_back(field);
3443 3447
3444 FormStructure form_structure(form); 3448 FormStructure form_structure(form);
3445 3449
3446 AutofillManager::DeterminePossibleFieldTypesForUpload( 3450 AutofillManager::DeterminePossibleFieldTypesForUpload(
3447 profiles, credit_cards, "en-us", &form_structure); 3451 profiles, credit_cards, "en-us", &form_structure);
3448 3452
3449 ASSERT_EQ(1U, form_structure.field_count()); 3453 ASSERT_EQ(1U, form_structure.field_count());
3450 ServerFieldTypeSet possible_types = 3454 ServerFieldTypeSet possible_types =
3451 form_structure.field(0)->possible_types(); 3455 form_structure.field(0)->possible_types();
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
3499 // Once the form is cached, fill the values. 3503 // Once the form is cached, fill the values.
3500 EXPECT_EQ(form.fields.size(), expected_values.size()); 3504 EXPECT_EQ(form.fields.size(), expected_values.size());
3501 for (size_t i = 0; i < expected_values.size(); i++) { 3505 for (size_t i = 0; i < expected_values.size(); i++) {
3502 form.fields[i].value = expected_values[i]; 3506 form.fields[i].value = expected_values[i];
3503 } 3507 }
3504 3508
3505 autofill_manager_->set_expected_submitted_field_types(expected_types); 3509 autofill_manager_->set_expected_submitted_field_types(expected_types);
3506 FormSubmitted(form); 3510 FormSubmitted(form);
3507 } 3511 }
3508 3512
3513 // Tests that DisambiguateUploadTypes makes the correct choices.
3514 TEST_F(AutofillManagerTest, DisambiguateUploadTypes) {
3515 // Set up the test profile.
3516 std::vector<AutofillProfile> profiles;
3517 AutofillProfile profile;
3518 test::SetProfileInfo(&profile, "Elvis", "Aaron", "Presley",
3519 "theking@gmail.com", "RCA", "3734 Elvis Presley Blvd.",
3520 "", "Memphis", "Tennessee", "38116", "US",
3521 "(234) 567-8901");
3522 profile.set_guid("00000000-0000-0000-0000-000000000001");
3523 profiles.push_back(profile);
3524
3525 // Set up the test credit card.
3526 std::vector<CreditCard> credit_cards;
3527 CreditCard credit_card;
3528 test::SetCreditCardInfo(&credit_card, "Elvis Presley", "4234-5678-9012-3456",
3529 "04", "2012");
3530 credit_card.set_guid("00000000-0000-0000-0000-000000000003");
3531 credit_cards.push_back(credit_card);
3532
3533 typedef struct {
3534 std::string input_value;
3535 ServerFieldType predicted_type;
3536 bool expect_disambiguation;
3537 ServerFieldType expected_upload_type;
3538 } TestFieldData;
3539
3540 std::vector<TestFieldData> test_cases[] = {
3541 // Address disambiguation.
3542 // An ambiguous address line followed by a field predicted as a line 2 and
3543 // that is empty should be disambiguated as an ADDRESS_HOME_LINE1.
3544 {
vabr (Chromium) 2016/03/22 17:08:10 This is an initializer list, IIUC, and those are c
sebsg 2016/03/22 18:30:26 I initialized the vector manually. I think it was
3545 {"3734 Elvis Presley Blvd.", ADDRESS_HOME_LINE1, true,
3546 ADDRESS_HOME_LINE1},
3547 {"", ADDRESS_HOME_LINE2, true, EMPTY_TYPE},
3548 },
3549 // An ambiguous address line followed by a field predicted as a line 2 but
3550 // filled with another know profile value should be disambiguated as an
3551 // ADDRESS_HOME_STREET_ADDRESS.
3552 {
3553 {"3734 Elvis Presley Blvd.", ADDRESS_HOME_STREET_ADDRESS, true,
3554 ADDRESS_HOME_STREET_ADDRESS},
3555 {"38116", ADDRESS_HOME_LINE2, true, ADDRESS_HOME_ZIP},
3556 },
3557 // An ambiguous address line followed by an empty field predicted as
3558 // something other than a line 2 should be disambiguated as an
3559 // ADDRESS_HOME_STREET_ADDRESS.
3560 {
3561 {"3734 Elvis Presley Blvd.", ADDRESS_HOME_STREET_ADDRESS, true,
3562 ADDRESS_HOME_STREET_ADDRESS},
3563 {"", ADDRESS_HOME_ZIP, true, EMPTY_TYPE},
3564 },
3565 // An ambiguous address line followed by no other field should be
3566 // disambiguated as an ADDRESS_HOME_STREET_ADDRESS.
3567 {
3568 {"3734 Elvis Presley Blvd.", ADDRESS_HOME_STREET_ADDRESS, true,
3569 ADDRESS_HOME_STREET_ADDRESS},
3570 },
3571
3572 // Phone number disambiguation.
3573 // A field with possible types PHONE_HOME_CITY_AND_NUMBER and
3574 // PHONE_HOME_WHOLE_NUMBER should be disambiguated as
3575 // PHONE_HOME_CITY_AND_NUMBER
3576 {
3577 {"2345678901", PHONE_HOME_WHOLE_NUMBER, true,
3578 PHONE_HOME_CITY_AND_NUMBER},
3579 },
3580
3581 // Name disambiguation.
3582 // An ambiguous name field that has no next field and that is preceded by
3583 // a non credit card field should be disambiguated as a non credit card
3584 // name.
3585 {
3586 {"Memphis", ADDRESS_HOME_CITY, true, ADDRESS_HOME_CITY},
3587 {"Elvis", CREDIT_CARD_NAME_FIRST, true, NAME_FIRST},
3588 {"Presley", CREDIT_CARD_NAME_LAST, true, NAME_LAST},
3589 },
3590 // An ambiguous name field that has no next field and that is preceded by
3591 // a credit card field should be disambiguated as a credit card name.
3592 {
3593 {"4234-5678-9012-3456", CREDIT_CARD_NUMBER, true, CREDIT_CARD_NUMBER},
3594 {"Elvis", NAME_FIRST, true, CREDIT_CARD_NAME_FIRST},
3595 {"Presley", NAME_LAST, true, CREDIT_CARD_NAME_LAST},
3596 },
3597 // An ambiguous name field that has no previous field and that is
3598 // followed by a non credit card field should be disambiguated as a non
3599 // credit card name.
3600 {
3601 {"Elvis", CREDIT_CARD_NAME_FIRST, true, NAME_FIRST},
3602 {"Presley", CREDIT_CARD_NAME_LAST, true, NAME_LAST},
3603 {"Memphis", ADDRESS_HOME_CITY, true, ADDRESS_HOME_CITY},
3604 },
3605 // An ambiguous name field that has no previous field and that is followed
3606 // by a credit card field should be disambiguated as a credit card name.
3607 {
3608 {"Elvis", NAME_FIRST, true, CREDIT_CARD_NAME_FIRST},
3609 {"Presley", NAME_LAST, true, CREDIT_CARD_NAME_LAST},
3610 {"4234-5678-9012-3456", CREDIT_CARD_NUMBER, true, CREDIT_CARD_NUMBER},
3611 },
3612 // An ambiguous name field that is preceded and followed by non credit
3613 // card fields should be disambiguated as a non credit card name.
3614 {
3615 {"Memphis", ADDRESS_HOME_CITY, true, ADDRESS_HOME_CITY},
3616 {"Elvis", CREDIT_CARD_NAME_FIRST, true, NAME_FIRST},
3617 {"Presley", CREDIT_CARD_NAME_LAST, true, NAME_LAST},
3618 {"Tennessee", ADDRESS_HOME_STATE, true, ADDRESS_HOME_STATE},
3619 },
3620 // An ambiguous name field that is preceded and followed by credit card
3621 // fields should be disambiguated as a credit card name.
3622 {
3623 {"4234-5678-9012-3456", CREDIT_CARD_NUMBER, true, CREDIT_CARD_NUMBER},
3624 {"Elvis", NAME_FIRST, true, CREDIT_CARD_NAME_FIRST},
3625 {"Presley", NAME_LAST, true, CREDIT_CARD_NAME_LAST},
3626 {"2012", CREDIT_CARD_EXP_4_DIGIT_YEAR, true,
3627 CREDIT_CARD_EXP_4_DIGIT_YEAR},
3628 },
3629 // An ambiguous name field that is preceded by a non credit card field and
3630 // followed by a credit card field should not be disambiguated.
3631 {
3632 {"Memphis", ADDRESS_HOME_CITY, true, ADDRESS_HOME_CITY},
3633 {"Elvis", NAME_FIRST, false, CREDIT_CARD_NAME_FIRST},
3634 {"Presley", NAME_LAST, false, CREDIT_CARD_NAME_LAST},
3635 {"2012", CREDIT_CARD_EXP_4_DIGIT_YEAR, true,
3636 CREDIT_CARD_EXP_4_DIGIT_YEAR},
3637 },
3638 // An ambiguous name field that is preceded by a credit card field and
3639 // followed by a non credit card field should not be disambiguated.
3640 {
3641 {"2012", CREDIT_CARD_EXP_4_DIGIT_YEAR, true,
3642 CREDIT_CARD_EXP_4_DIGIT_YEAR},
3643 {"Elvis", NAME_FIRST, false, CREDIT_CARD_NAME_FIRST},
3644 {"Presley", NAME_LAST, false, CREDIT_CARD_NAME_LAST},
3645 {"Memphis", ADDRESS_HOME_CITY, true, ADDRESS_HOME_CITY},
3646 },
3647 };
3648
3649 for (std::vector<TestFieldData> test_fields : test_cases) {
vabr (Chromium) 2016/03/22 17:08:10 nit: Please do not copy into test_fields, use a co
sebsg 2016/03/22 18:30:26 Done.
3650 FormData form;
3651 form.name = ASCIIToUTF16("MyForm");
3652 form.origin = GURL("http://myform.com/form.html");
3653 form.action = GURL("http://myform.com/submit.html");
3654
3655 // Create the form fields specified in the test case.
3656 FormFieldData field;
3657 for (size_t i = 0; i < test_fields.size(); ++i) {
vabr (Chromium) 2016/03/22 17:08:10 Let's use a range-based loop here, it's less clutt
sebsg 2016/03/22 18:30:26 Done.
3658 test::CreateTestFormField("", "1", "", "text", &field);
3659 field.value = ASCIIToUTF16(test_fields[i].input_value);
3660 form.fields.push_back(field);
3661 }
3662
3663 // Assign the specified predicted type for each field in the test case.
3664 FormStructure form_structure(form);
3665 for (size_t i = 0; i < test_fields.size(); ++i) {
3666 form_structure.field(i)->set_server_type(test_fields[i].predicted_type);
3667 }
3668
3669 AutofillManager::DeterminePossibleFieldTypesForUpload(
3670 profiles, credit_cards, "en-us", &form_structure);
3671 ASSERT_EQ(test_fields.size(), form_structure.field_count());
3672
3673 // Make sure the disambiguation method selects the expected upload type.
3674 ServerFieldTypeSet possible_types;
3675 for (size_t i = 0; i < test_fields.size(); ++i) {
3676 possible_types = form_structure.field(i)->possible_types();
3677 if (test_fields[i].expect_disambiguation) {
3678 EXPECT_EQ(1U, possible_types.size());
3679 EXPECT_NE(possible_types.end(),
3680 possible_types.find(test_fields[i].expected_upload_type));
3681 } else {
3682 EXPECT_EQ(2U, possible_types.size());
3683 }
3684 }
3685 }
3686 }
3687
3509 TEST_F(AutofillManagerTest, RemoveProfile) { 3688 TEST_F(AutofillManagerTest, RemoveProfile) {
3510 // Add and remove an Autofill profile. 3689 // Add and remove an Autofill profile.
3511 AutofillProfile* profile = new AutofillProfile; 3690 AutofillProfile* profile = new AutofillProfile;
3512 const char guid[] = "00000000-0000-0000-0000-000000000102"; 3691 const char guid[] = "00000000-0000-0000-0000-000000000102";
3513 profile->set_guid(guid); 3692 profile->set_guid(guid);
3514 autofill_manager_->AddProfile(profile); 3693 autofill_manager_->AddProfile(profile);
3515 3694
3516 int id = MakeFrontendID(std::string(), guid); 3695 int id = MakeFrontendID(std::string(), guid);
3517 3696
3518 autofill_manager_->RemoveAutofillProfileOrCreditCard(id); 3697 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
(...skipping 1089 matching lines...) Expand 10 before | Expand all | Expand 10 after
4608 FormsSeen(mixed_forms); 4787 FormsSeen(mixed_forms);
4609 4788
4610 // Suggestions should always be displayed. 4789 // Suggestions should always be displayed.
4611 for (const FormFieldData& field : mixed_form.fields) { 4790 for (const FormFieldData& field : mixed_form.fields) {
4612 GetAutofillSuggestions(mixed_form, field); 4791 GetAutofillSuggestions(mixed_form, field);
4613 EXPECT_TRUE(external_delegate_->on_suggestions_returned_seen()); 4792 EXPECT_TRUE(external_delegate_->on_suggestions_returned_seen());
4614 } 4793 }
4615 } 4794 }
4616 4795
4617 } // namespace autofill 4796 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698