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

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: Nits 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[13];
3541
3542 // Address disambiguation.
3543 // An ambiguous address line followed by a field predicted as a line 2 and
3544 // that is empty should be disambiguated as an ADDRESS_HOME_LINE1.
3545 test_cases[0].push_back({"3734 Elvis Presley Blvd.", ADDRESS_HOME_LINE1, true,
3546 ADDRESS_HOME_LINE1});
3547 test_cases[0].push_back({"", 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 test_cases[1].push_back({"3734 Elvis Presley Blvd.",
3553 ADDRESS_HOME_STREET_ADDRESS, true,
3554 ADDRESS_HOME_STREET_ADDRESS});
3555 test_cases[1].push_back(
3556 {"38116", ADDRESS_HOME_LINE2, true, ADDRESS_HOME_ZIP});
3557
3558 // An ambiguous address line followed by an empty field predicted as
3559 // something other than a line 2 should be disambiguated as an
3560 // ADDRESS_HOME_STREET_ADDRESS.
3561 test_cases[2].push_back({"3734 Elvis Presley Blvd.",
3562 ADDRESS_HOME_STREET_ADDRESS, true,
3563 ADDRESS_HOME_STREET_ADDRESS});
3564 test_cases[2].push_back({"", ADDRESS_HOME_ZIP, true, EMPTY_TYPE});
3565
3566 // An ambiguous address line followed by no other field should be
3567 // disambiguated as an ADDRESS_HOME_STREET_ADDRESS.
3568 test_cases[3].push_back({"3734 Elvis Presley Blvd.",
3569 ADDRESS_HOME_STREET_ADDRESS, true,
3570 ADDRESS_HOME_STREET_ADDRESS});
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 test_cases[4].push_back({"2345678901", PHONE_HOME_WHOLE_NUMBER, true,
3577 PHONE_HOME_CITY_AND_NUMBER});
3578
3579 // Name disambiguation.
3580 // An ambiguous name field that has no next field and that is preceded by
3581 // a non credit card field should be disambiguated as a non credit card
3582 // name.
3583 test_cases[5].push_back(
3584 {"Memphis", ADDRESS_HOME_CITY, true, ADDRESS_HOME_CITY});
3585 test_cases[5].push_back({"Elvis", CREDIT_CARD_NAME_FIRST, true, NAME_FIRST});
3586 test_cases[5].push_back({"Presley", CREDIT_CARD_NAME_LAST, true, NAME_LAST});
3587
3588 // An ambiguous name field that has no next field and that is preceded by
3589 // a credit card field should be disambiguated as a credit card name.
3590 test_cases[6].push_back(
3591 {"4234-5678-9012-3456", CREDIT_CARD_NUMBER, true, CREDIT_CARD_NUMBER});
3592 test_cases[6].push_back({"Elvis", NAME_FIRST, true, CREDIT_CARD_NAME_FIRST});
3593 test_cases[6].push_back({"Presley", NAME_LAST, true, CREDIT_CARD_NAME_LAST});
3594
3595 // An ambiguous name field that has no previous field and that is
3596 // followed by a non credit card field should be disambiguated as a non
3597 // credit card name.
3598 test_cases[7].push_back({"Elvis", CREDIT_CARD_NAME_FIRST, true, NAME_FIRST});
3599 test_cases[7].push_back({"Presley", CREDIT_CARD_NAME_LAST, true, NAME_LAST});
3600 test_cases[7].push_back(
3601 {"Memphis", ADDRESS_HOME_CITY, true, ADDRESS_HOME_CITY});
3602
3603 // An ambiguous name field that has no previous field and that is followed
3604 // by a credit card field should be disambiguated as a credit card name.
3605 test_cases[8].push_back({"Elvis", NAME_FIRST, true, CREDIT_CARD_NAME_FIRST});
3606 test_cases[8].push_back({"Presley", NAME_LAST, true, CREDIT_CARD_NAME_LAST});
3607 test_cases[8].push_back(
3608 {"4234-5678-9012-3456", CREDIT_CARD_NUMBER, true, CREDIT_CARD_NUMBER});
3609
3610 // An ambiguous name field that is preceded and followed by non credit
3611 // card fields should be disambiguated as a non credit card name.
3612 test_cases[9].push_back(
3613 {"Memphis", ADDRESS_HOME_CITY, true, ADDRESS_HOME_CITY});
3614 test_cases[9].push_back({"Elvis", CREDIT_CARD_NAME_FIRST, true, NAME_FIRST});
3615 test_cases[9].push_back({"Presley", CREDIT_CARD_NAME_LAST, true, NAME_LAST});
3616 test_cases[9].push_back(
3617 {"Tennessee", ADDRESS_HOME_STATE, true, ADDRESS_HOME_STATE});
3618
3619 // An ambiguous name field that is preceded and followed by credit card
3620 // fields should be disambiguated as a credit card name.
3621 test_cases[10].push_back(
3622 {"4234-5678-9012-3456", CREDIT_CARD_NUMBER, true, CREDIT_CARD_NUMBER});
3623 test_cases[10].push_back({"Elvis", NAME_FIRST, true, CREDIT_CARD_NAME_FIRST});
3624 test_cases[10].push_back({"Presley", NAME_LAST, true, CREDIT_CARD_NAME_LAST});
3625 test_cases[10].push_back({"2012", CREDIT_CARD_EXP_4_DIGIT_YEAR, true,
3626 CREDIT_CARD_EXP_4_DIGIT_YEAR});
3627
3628 // An ambiguous name field that is preceded by a non credit card field and
3629 // followed by a credit card field should not be disambiguated.
3630 test_cases[11].push_back(
3631 {"Memphis", ADDRESS_HOME_CITY, true, ADDRESS_HOME_CITY});
3632 test_cases[11].push_back(
3633 {"Elvis", NAME_FIRST, false, CREDIT_CARD_NAME_FIRST});
3634 test_cases[11].push_back(
3635 {"Presley", NAME_LAST, false, CREDIT_CARD_NAME_LAST});
3636 test_cases[11].push_back({"2012", CREDIT_CARD_EXP_4_DIGIT_YEAR, true,
3637 CREDIT_CARD_EXP_4_DIGIT_YEAR});
3638
3639 // An ambiguous name field that is preceded by a credit card field and
3640 // followed by a non credit card field should not be disambiguated.
3641 test_cases[12].push_back({"2012", CREDIT_CARD_EXP_4_DIGIT_YEAR, true,
3642 CREDIT_CARD_EXP_4_DIGIT_YEAR});
3643 test_cases[12].push_back(
3644 {"Elvis", NAME_FIRST, false, CREDIT_CARD_NAME_FIRST});
3645 test_cases[12].push_back(
3646 {"Presley", NAME_LAST, false, CREDIT_CARD_NAME_LAST});
3647 test_cases[12].push_back(
3648 {"Memphis", ADDRESS_HOME_CITY, true, ADDRESS_HOME_CITY});
3649
3650 for (const std::vector<TestFieldData>& test_fields : test_cases) {
3651 FormData form;
3652 form.name = ASCIIToUTF16("MyForm");
3653 form.origin = GURL("http://myform.com/form.html");
3654 form.action = GURL("http://myform.com/submit.html");
3655
3656 // Create the form fields specified in the test case.
3657 FormFieldData field;
3658 for (const TestFieldData& test_field : test_fields) {
3659 test::CreateTestFormField("", "1", "", "text", &field);
3660 field.value = ASCIIToUTF16(test_field.input_value);
3661 form.fields.push_back(field);
3662 }
3663
3664 // Assign the specified predicted type for each field in the test case.
3665 FormStructure form_structure(form);
3666 for (size_t i = 0; i < test_fields.size(); ++i) {
3667 form_structure.field(i)->set_server_type(test_fields[i].predicted_type);
3668 }
3669
3670 AutofillManager::DeterminePossibleFieldTypesForUpload(
3671 profiles, credit_cards, "en-us", &form_structure);
3672 ASSERT_EQ(test_fields.size(), form_structure.field_count());
3673
3674 // Make sure the disambiguation method selects the expected upload type.
3675 ServerFieldTypeSet possible_types;
3676 for (size_t i = 0; i < test_fields.size(); ++i) {
3677 possible_types = form_structure.field(i)->possible_types();
3678 if (test_fields[i].expect_disambiguation) {
3679 EXPECT_EQ(1U, possible_types.size());
3680 EXPECT_NE(possible_types.end(),
3681 possible_types.find(test_fields[i].expected_upload_type));
3682 } else {
3683 EXPECT_EQ(2U, possible_types.size());
3684 }
3685 }
3686 }
3687 }
3688
3509 TEST_F(AutofillManagerTest, RemoveProfile) { 3689 TEST_F(AutofillManagerTest, RemoveProfile) {
3510 // Add and remove an Autofill profile. 3690 // Add and remove an Autofill profile.
3511 AutofillProfile* profile = new AutofillProfile; 3691 AutofillProfile* profile = new AutofillProfile;
3512 const char guid[] = "00000000-0000-0000-0000-000000000102"; 3692 const char guid[] = "00000000-0000-0000-0000-000000000102";
3513 profile->set_guid(guid); 3693 profile->set_guid(guid);
3514 autofill_manager_->AddProfile(profile); 3694 autofill_manager_->AddProfile(profile);
3515 3695
3516 int id = MakeFrontendID(std::string(), guid); 3696 int id = MakeFrontendID(std::string(), guid);
3517 3697
3518 autofill_manager_->RemoveAutofillProfileOrCreditCard(id); 3698 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
(...skipping 1089 matching lines...) Expand 10 before | Expand all | Expand 10 after
4608 FormsSeen(mixed_forms); 4788 FormsSeen(mixed_forms);
4609 4789
4610 // Suggestions should always be displayed. 4790 // Suggestions should always be displayed.
4611 for (const FormFieldData& field : mixed_form.fields) { 4791 for (const FormFieldData& field : mixed_form.fields) {
4612 GetAutofillSuggestions(mixed_form, field); 4792 GetAutofillSuggestions(mixed_form, field);
4613 EXPECT_TRUE(external_delegate_->on_suggestions_returned_seen()); 4793 EXPECT_TRUE(external_delegate_->on_suggestions_returned_seen());
4614 } 4794 }
4615 } 4795 }
4616 4796
4617 } // namespace autofill 4797 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/autofill_manager.cc ('k') | components/autofill/core/browser/autofill_metrics.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698