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

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

Issue 2113453002: [Autofill] Structure AutofillField tests a little better (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Quebec Created 4 years, 5 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "components/autofill/core/browser/autofill_field.h" 5 #include "components/autofill/core/browser/autofill_field.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 // Set semantically empty values for each option, so that only the contents 405 // Set semantically empty values for each option, so that only the contents
406 // can be used for matching. 406 // can be used for matching.
407 for (size_t i = 0; i < field.option_values.size(); ++i) 407 for (size_t i = 0; i < field.option_values.size(); ++i)
408 field.option_values[i] = base::SizeTToString16(i); 408 field.option_values[i] = base::SizeTToString16(i);
409 409
410 AutofillField::FillFormField( 410 AutofillField::FillFormField(
411 field, ASCIIToUTF16("Miney"), "en-US", "en-US", &field); 411 field, ASCIIToUTF16("Miney"), "en-US", "en-US", &field);
412 EXPECT_EQ(ASCIIToUTF16("2"), field.value); // Corresponds to "Miney". 412 EXPECT_EQ(ASCIIToUTF16("2"), field.value); // Corresponds to "Miney".
413 } 413 }
414 414
415 TEST_F(AutofillFieldTest, FillSelectControlWithFullCountryNames) { 415 TEST_F(AutofillFieldTest, FillSelectWithStates) {
416 std::vector<const char*> kCountries = {"Albania", "Canada"}; 416 typedef struct {
417 AutofillField field; 417 std::vector<const char*> select_values;
418 test::CreateTestSelectField(kCountries, &field); 418 const char* input_value;
419 field.set_heuristic_type(ADDRESS_HOME_COUNTRY); 419 const char* expected_value;
420 } TestCase;
420 421
421 AutofillField::FillFormField( 422 TestCase test_cases[] = {
422 field, ASCIIToUTF16("CA"), "en-US", "en-US", &field); 423 // Filling the abbreviation.
423 EXPECT_EQ(ASCIIToUTF16("Canada"), field.value); 424 {{"Alabama", "California"}, "CA", "California"},
424 } 425 // Attempting to fill the full name in a select full of abbreviations.
426 {{"AL", "CA"}, "California", "CA"},
427 // Different case and diacritics.
428 {{"QUÉBEC", "ALBERTA"}, "Quebec", "QUÉBEC"},
429 // Inexact state names.
430 {{"SC - South Carolina", "CA - California", "NC - North Carolina"},
431 "California",
432 "CA - California"},
433 // Don't accidentally match "Virginia" to "West Virginia".
434 {{"WV - West Virginia", "VA - Virginia", "NV - North Virginia"},
435 "Virginia",
436 "VA - Virginia"},
437 // Do accidentally match "Virginia" to "West Virginia". NB: Ideally,
sebsg 2016/06/30 09:48:16 Can you document what makes Virginia match or not
Mathieu 2016/06/30 12:21:06 Done.
438 // Chrome would fail this test. It's here to document behavior rather than
439 // enforce it.
440 {{"WV - West Virginia", "TX - Texas"}, "Virginia", "WV - West Virginia"},
441 // Tests that substring matches work for full state names (a full token
442 // match isn't required). Also tests that matches work for states with
443 // whitespace in the middle.
444 {{"California.", "North Carolina."}, "North Carolina", "North Carolina."},
445 {{"NC - North Carolina", "CA - California"}, "CA", "CA - California"},
446 // These are not states.
447 {{"NCNCA", "SCNCA"}, "NC", ""}};
425 448
426 TEST_F(AutofillFieldTest, FillSelectControlWithAbbreviatedCountryNames) { 449 for (TestCase test_case : test_cases) {
427 std::vector<const char*> kCountries = {"AL", "CA"};
428 AutofillField field;
429 test::CreateTestSelectField(kCountries, &field);
430 field.set_heuristic_type(ADDRESS_HOME_COUNTRY);
431
432 AutofillField::FillFormField(
433 field, ASCIIToUTF16("Canada"), "en-US", "en-US", &field);
434 EXPECT_EQ(ASCIIToUTF16("CA"), field.value);
435 }
436
437 TEST_F(AutofillFieldTest, FillSelectControlWithFullStateNames) {
438 std::vector<const char*> kStates = {"Alabama", "California"};
439 AutofillField field;
440 test::CreateTestSelectField(kStates, &field);
441 field.set_heuristic_type(ADDRESS_HOME_STATE);
442
443 AutofillField::FillFormField(
444 field, ASCIIToUTF16("CA"), "en-US", "en-US", &field);
445 EXPECT_EQ(ASCIIToUTF16("California"), field.value);
446 }
447
448 TEST_F(AutofillFieldTest, FillSelectControlWithAbbreviateStateNames) {
449 std::vector<const char*> kStates = {"AL", "CA"};
450 AutofillField field;
451 test::CreateTestSelectField(kStates, &field);
452 field.set_heuristic_type(ADDRESS_HOME_STATE);
453
454 AutofillField::FillFormField(
455 field, ASCIIToUTF16("California"), "en-US", "en-US", &field);
456 EXPECT_EQ(ASCIIToUTF16("CA"), field.value);
457 }
458
459 TEST_F(AutofillFieldTest, FillSelectControlWithInexactFullStateNames) {
460 {
461 std::vector<const char*> kStates = {
462 "SC - South Carolina", "CA - California", "NC - North Carolina",
463 };
464 AutofillField field; 450 AutofillField field;
465 test::CreateTestSelectField(kStates, &field); 451 test::CreateTestSelectField(test_case.select_values, &field);
466 field.set_heuristic_type(ADDRESS_HOME_STATE); 452 field.set_heuristic_type(ADDRESS_HOME_STATE);
467 453
468 AutofillField::FillFormField( 454 AutofillField::FillFormField(field, UTF8ToUTF16(test_case.input_value),
469 field, ASCIIToUTF16("California"), "en-US", "en-US", &field); 455 "en-US", "en-US", &field);
470 EXPECT_EQ(ASCIIToUTF16("CA - California"), field.value); 456 EXPECT_EQ(UTF8ToUTF16(test_case.expected_value), field.value);
471 }
472
473 // Don't accidentally match "Virginia" to "West Virginia".
474 {
475 std::vector<const char*> kStates = {
476 "WV - West Virginia", "VA - Virginia", "NV - North Virginia",
477 };
478 AutofillField field;
479 test::CreateTestSelectField(kStates, &field);
480 field.set_heuristic_type(ADDRESS_HOME_STATE);
481
482 AutofillField::FillFormField(
483 field, ASCIIToUTF16("Virginia"), "en-US", "en-US", &field);
484 EXPECT_EQ(ASCIIToUTF16("VA - Virginia"), field.value);
485 }
486
487 // Do accidentally match "Virginia" to "West Virginia". NB: Ideally, Chrome
488 // would fail this test. It's here to document behavior rather than enforce
489 // it.
490 {
491 std::vector<const char*> kStates = {
492 "WV - West Virginia", "TX - Texas",
493 };
494 AutofillField field;
495 test::CreateTestSelectField(kStates, &field);
496 field.set_heuristic_type(ADDRESS_HOME_STATE);
497
498 AutofillField::FillFormField(
499 field, ASCIIToUTF16("Virginia"), "en-US", "en-US", &field);
500 EXPECT_EQ(ASCIIToUTF16("WV - West Virginia"), field.value);
501 }
502
503 // Tests that substring matches work for full state names (a full token
504 // match isn't required). Also tests that matches work for states with
505 // whitespace in the middle.
506 {
507 std::vector<const char*> kStates = {
508 "California.", "North Carolina.",
509 };
510 AutofillField field;
511 test::CreateTestSelectField(kStates, &field);
512 field.set_heuristic_type(ADDRESS_HOME_STATE);
513
514 AutofillField::FillFormField(
515 field, ASCIIToUTF16("North Carolina"), "en-US", "en-US", &field);
516 EXPECT_EQ(ASCIIToUTF16("North Carolina."), field.value);
517 } 457 }
518 } 458 }
519 459
520 TEST_F(AutofillFieldTest, FillSelectControlWithInexactAbbreviations) { 460 TEST_F(AutofillFieldTest, FillSelectWithCountries) {
521 { 461 typedef struct {
522 std::vector<const char*> kStates = { 462 std::vector<const char*> select_values;
523 "NC - North Carolina", "CA - California", 463 const char* input_value;
524 }; 464 const char* expected_value;
465 } TestCase;
466
467 TestCase test_cases[] = {// Full country names.
468 {{"Albania", "Canada"}, "CA", "Canada"},
469 // Abbreviations.
470 {{"AL", "CA"}, "Canada", "CA"}};
471
472 for (TestCase test_case : test_cases) {
525 AutofillField field; 473 AutofillField field;
526 test::CreateTestSelectField(kStates, &field); 474 test::CreateTestSelectField(test_case.select_values, &field);
527 field.set_heuristic_type(ADDRESS_HOME_STATE); 475 field.set_heuristic_type(ADDRESS_HOME_COUNTRY);
528 476
529 AutofillField::FillFormField( 477 AutofillField::FillFormField(field, UTF8ToUTF16(test_case.input_value),
530 field, ASCIIToUTF16("CA"), "en-US", "en-US", &field); 478 "en-US", "en-US", &field);
531 EXPECT_EQ(ASCIIToUTF16("CA - California"), field.value); 479 EXPECT_EQ(UTF8ToUTF16(test_case.expected_value), field.value);
532 }
533
534 {
535 std::vector<const char*> kNotStates = {
536 "NCNCA", "SCNCA",
537 };
538 AutofillField field;
539 test::CreateTestSelectField(kNotStates, &field);
540 field.set_heuristic_type(ADDRESS_HOME_STATE);
541
542 AutofillField::FillFormField(
543 field, ASCIIToUTF16("NC"), "en-US", "en-US", &field);
544 EXPECT_EQ(base::string16(), field.value);
545 } 480 }
546 } 481 }
547 482
548 TEST_F(AutofillFieldTest, FillSelectControlWithExpirationMonth) { 483 TEST_F(AutofillFieldTest, FillSelectControlWithExpirationMonth) {
549 typedef struct { 484 typedef struct {
550 std::vector<const char*> select_values; 485 std::vector<const char*> select_values;
551 std::vector<const char*> select_contents; 486 std::vector<const char*> select_contents;
552 } TestCase; 487 } TestCase;
553 488
554 TestCase test_cases[] = { 489 TestCase test_cases[] = {
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
944 bool has_filled = AutofillField::FillFormField( 879 bool has_filled = AutofillField::FillFormField(
945 field, ASCIIToUTF16(test_case.value_to_fill), "en-US", "en-US", &field); 880 field, ASCIIToUTF16(test_case.value_to_fill), "en-US", "en-US", &field);
946 881
947 EXPECT_EQ(test_case.should_fill, has_filled); 882 EXPECT_EQ(test_case.should_fill, has_filled);
948 EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value); 883 EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value);
949 } 884 }
950 } 885 }
951 886
952 } // namespace 887 } // namespace
953 } // namespace autofill 888 } // namespace autofill
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698