1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/core/browser/autofill_field.h"
#include <stddef.h>
#include "base/macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/country_names.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/common/autofill_util.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::ASCIIToUTF16;
using base::StringToInt;
using base::UTF8ToUTF16;
namespace autofill {
namespace {
const std::vector<const char*> NotNumericMonthsContentsNoPlaceholder()
{
const std::vector<const char*> result = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
return result;
}
const std::vector<const char*> NotNumericMonthsContentsWithPlaceholder()
{
const std::vector<const char*> result = {
"Select a Month",
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"};
return result;
}
// Returns the index of |value| in |values|.
void GetIndexOfValue(const std::vector<base::string16>& values,
const base::string16& value,
size_t* index) {
for (size_t i = 0; i < values.size(); ++i) {
if (values[i] == value) {
*index = i;
return;
}
}
ASSERT_TRUE(false) << "Passing invalid arguments to GetIndexOfValue";
}
// Creates the select field from the specified |values| and |contents| and tests
// filling with 3 different values.
void TestFillingExpirationMonth(const std::vector<const char*>& values,
const std::vector<const char*>& contents,
size_t select_size) {
// Create the select field.
AutofillField field;
test::CreateTestSelectField("", "", "", values, contents, select_size,
&field);
field.set_heuristic_type(CREDIT_CARD_EXP_MONTH);
size_t content_index = 0;
// Try with a leading zero.
AutofillField::FillFormField(field, ASCIIToUTF16("03"), "en-US", "en-US",
&field);
GetIndexOfValue(field.option_values, field.value, &content_index);
EXPECT_EQ(ASCIIToUTF16("Mar"), field.option_contents[content_index]);
// Try without a leading zero.
AutofillField::FillFormField(field, ASCIIToUTF16("4"), "en-US", "en-US",
&field);
GetIndexOfValue(field.option_values, field.value, &content_index);
EXPECT_EQ(ASCIIToUTF16("Apr"), field.option_contents[content_index]);
// Try a two-digit month.
AutofillField::FillFormField(field, ASCIIToUTF16("11"), "en-US", "en-US",
&field);
GetIndexOfValue(field.option_values, field.value, &content_index);
EXPECT_EQ(ASCIIToUTF16("Nov"), field.option_contents[content_index]);
}
struct TestCase {
std::string card_number_;
size_t total_spilts_;
std::vector<int> splits_;
std::vector<std::string> expected_results_;
};
// Returns the offset to be set within the credit card number field.
size_t GetNumberOffset(size_t index, const TestCase& test) {
size_t result = 0;
for (size_t i = 0; i < index; ++i)
result += test.splits_[i];
return result;
}
class AutofillFieldTest : public testing::Test {
public:
AutofillFieldTest() { CountryNames::SetLocaleString("en-US"); }
};
TEST_F(AutofillFieldTest, Type) {
AutofillField field;
ASSERT_EQ(NO_SERVER_DATA, field.server_type());
ASSERT_EQ(UNKNOWN_TYPE, field.heuristic_type());
// |server_type_| is NO_SERVER_DATA, so |heuristic_type_| is returned.
EXPECT_EQ(UNKNOWN_TYPE, field.Type().GetStorableType());
// Set the heuristic type and check it.
field.set_heuristic_type(NAME_FIRST);
EXPECT_EQ(NAME_FIRST, field.Type().GetStorableType());
EXPECT_EQ(NAME, field.Type().group());
// Set the server type and check it.
field.set_server_type(ADDRESS_BILLING_LINE1);
EXPECT_EQ(ADDRESS_HOME_LINE1, field.Type().GetStorableType());
EXPECT_EQ(ADDRESS_BILLING, field.Type().group());
// Remove the server type to make sure the heuristic type is preserved.
field.set_server_type(NO_SERVER_DATA);
EXPECT_EQ(NAME_FIRST, field.Type().GetStorableType());
EXPECT_EQ(NAME, field.Type().group());
}
// Tests that a credit card related prediction made by the heuristics overrides
// an unrecognized autocomplete attribute.
TEST_F(AutofillFieldTest, Type_CreditCardOverrideHtml_Heuristics) {
AutofillField field;
field.SetHtmlType(HTML_TYPE_UNRECOGNIZED, HTML_MODE_NONE);
// A credit card heuristic prediction overrides the unrecognized type.
field.set_heuristic_type(CREDIT_CARD_NUMBER);
EXPECT_EQ(CREDIT_CARD_NUMBER, field.Type().GetStorableType());
// A non credit card heuristic prediction doesn't override the unrecognized
// type.
field.set_heuristic_type(NAME_FIRST);
EXPECT_EQ(UNKNOWN_TYPE, field.Type().GetStorableType());
// A credit card heuristic prediction doesn't override a known specified html
// type.
field.SetHtmlType(HTML_TYPE_NAME, HTML_MODE_NONE);
field.set_heuristic_type(CREDIT_CARD_NUMBER);
EXPECT_EQ(NAME_FULL, field.Type().GetStorableType());
}
// Tests that a credit card related prediction made by the server overrides an
// unrecognized autocomplete attribute.
TEST_F(AutofillFieldTest, Type_CreditCardOverrideHtml_ServerPredicitons) {
AutofillField field;
field.SetHtmlType(HTML_TYPE_UNRECOGNIZED, HTML_MODE_NONE);
// A credit card server prediction overrides the unrecognized type.
field.set_server_type(CREDIT_CARD_NUMBER);
EXPECT_EQ(CREDIT_CARD_NUMBER, field.Type().GetStorableType());
// A non credit card server prediction doesn't override the unrecognized
// type.
field.set_server_type(NAME_FIRST);
EXPECT_EQ(UNKNOWN_TYPE, field.Type().GetStorableType());
// A credit card server prediction doesn't override a known specified html
// type.
field.SetHtmlType(HTML_TYPE_NAME, HTML_MODE_NONE);
field.set_server_type(CREDIT_CARD_NUMBER);
EXPECT_EQ(NAME_FULL, field.Type().GetStorableType());
}
TEST_F(AutofillFieldTest, IsEmpty) {
AutofillField field;
ASSERT_EQ(base::string16(), field.value);
// Field value is empty.
EXPECT_TRUE(field.IsEmpty());
// Field value is non-empty.
field.value = ASCIIToUTF16("Value");
EXPECT_FALSE(field.IsEmpty());
}
TEST_F(AutofillFieldTest, FieldSignatureAsStr) {
AutofillField field;
ASSERT_EQ(base::string16(), field.name);
ASSERT_EQ(std::string(), field.form_control_type);
// Signature is empty.
EXPECT_EQ("2085434232", field.FieldSignatureAsStr());
// Field name is set.
field.name = ASCIIToUTF16("Name");
EXPECT_EQ("1606968241", field.FieldSignatureAsStr());
// Field form control type is set.
field.form_control_type = "text";
EXPECT_EQ("502192749", field.FieldSignatureAsStr());
// Heuristic type does not affect FieldSignature.
field.set_heuristic_type(NAME_FIRST);
EXPECT_EQ("502192749", field.FieldSignatureAsStr());
// Server type does not affect FieldSignature.
field.set_server_type(NAME_LAST);
EXPECT_EQ("502192749", field.FieldSignatureAsStr());
}
TEST_F(AutofillFieldTest, IsFieldFillable) {
AutofillField field;
ASSERT_EQ(UNKNOWN_TYPE, field.Type().GetStorableType());
// Type is unknown.
EXPECT_FALSE(field.IsFieldFillable());
// Only heuristic type is set.
field.set_heuristic_type(NAME_FIRST);
EXPECT_TRUE(field.IsFieldFillable());
// Only server type is set.
field.set_heuristic_type(UNKNOWN_TYPE);
field.set_server_type(NAME_LAST);
EXPECT_TRUE(field.IsFieldFillable());
// Both types set.
field.set_heuristic_type(NAME_FIRST);
field.set_server_type(NAME_LAST);
EXPECT_TRUE(field.IsFieldFillable());
// Field has autocomplete="off" set. Since autofill was able to make a
// prediction, it is still considered a fillable field.
field.should_autocomplete = false;
EXPECT_TRUE(field.IsFieldFillable());
}
// Verify that non credit card related fields with the autocomplete attribute
// set to off don't get filled on desktop.
TEST_F(AutofillFieldTest, FillFormField_AutocompleteOff_AddressField) {
AutofillField field;
field.should_autocomplete = false;
// Non credit card related field.
AutofillField::FillFormField(field, ASCIIToUTF16("Test"), "en-US", "en-US",
&field);
// Verifiy that the field is filled on mobile but not on desktop.
if (IsDesktopPlatform()) {
EXPECT_EQ(base::string16(), field.value);
} else {
EXPECT_EQ(ASCIIToUTF16("Test"), field.value);
}
}
// Verify that credit card related fields with the autocomplete attribute
// set to off get filled.
TEST_F(AutofillFieldTest, FillFormField_AutocompleteOff_CreditCardField) {
AutofillField field;
field.should_autocomplete = false;
// Credit card related field.
field.set_heuristic_type(CREDIT_CARD_NUMBER);
AutofillField::FillFormField(field, ASCIIToUTF16("4111111111111111"), "en-US",
"en-US", &field);
// Verify that the field is filled.
EXPECT_EQ(ASCIIToUTF16("4111111111111111"), field.value);
}
// TODO(crbug.com/581514): Add support for filling only the prefix/suffix for
// phone numbers with 10 or 11 digits.
TEST_F(AutofillFieldTest, FillPhoneNumber) {
typedef struct {
HtmlFieldType field_type;
size_t field_max_length;
std::string value_to_fill;
std::string expected_value;
} TestCase;
TestCase test_cases[] = {
// Filling a phone type field with text should fill the text as is.
{HTML_TYPE_TEL, /* default value */ 0, "Oh hai", "Oh hai"},
// Filling a prefix type field with a phone number of 7 digits should just
// fill the prefix.
{HTML_TYPE_TEL_LOCAL_PREFIX, /* default value */ 0, "5551234", "555"},
// Filling a suffix type field with a phone number of 7 digits should just
// fill the suffix.
{HTML_TYPE_TEL_LOCAL_SUFFIX, /* default value */ 0, "5551234", "1234"},
// Filling a phone type field with a max length of 3 with a phone number
// of
// 7 digits should fill only the prefix.
{HTML_TYPE_TEL, 3, "5551234", "555"},
// Filling a phone type field with a max length of 4 with a phone number
// of
// 7 digits should fill only the suffix.
{HTML_TYPE_TEL, 4, "5551234", "1234"},
// Filling a phone type field with a max length of 10 with a phone number
// including the country code should fill the phone number without the
// country code.
{HTML_TYPE_TEL, 10, "15141254578", "5141254578"},
// Filling a phone type field with a max length of 5 with a phone number
// should fill with the last 5 digits of that phone number.
{HTML_TYPE_TEL, 5, "5141254578", "54578"}};
for (TestCase test_case : test_cases) {
AutofillField field;
field.SetHtmlType(test_case.field_type, HtmlFieldMode());
field.max_length = test_case.field_max_length;
AutofillField::FillFormField(field, ASCIIToUTF16(test_case.value_to_fill),
"en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value);
}
}
TEST_F(AutofillFieldTest, FillExpirationYearInput) {
typedef struct {
HtmlFieldType field_type;
size_t field_max_length;
std::string value_to_fill;
std::string expected_value;
} TestCase;
TestCase test_cases[] = {
// A field predicted as a 2 digits expiration year should fill the last 2
// digits of the expiration year if the field has an unspecified max
// length (0) or if it's greater than 1.
{HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR, /* default value */ 0, "2023",
"23"},
{HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR, 2, "2023", "23"},
{HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR, 12, "2023", "23"},
// A field predicted as a 2 digit expiration year should fill the last
// digit of the expiration year if the field has a max length of 1.
{HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR, 1, "2023", "3"},
// A field predicted as a 4 digit expiration year should fill the 4
// digits of the expiration year if the field has an unspecified max
// length (0) or if it's greater than 3 .
{HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, /* default value */ 0, "2023",
"2023"},
{HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, 4, "2023", "2023"},
{HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, 12, "2023", "2023"},
// A field predicted as a 4 digits expiration year should fill the last 2
// digits of the expiration year if the field has a max length of 2.
{HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, 2, "2023", "23"},
// A field predicted as a 4 digits expiration year should fill the last
// digit of the expiration year if the field has a max length of 1.
{HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, 1, "2023", "3"},
};
for (TestCase test_case : test_cases) {
AutofillField field;
field.form_control_type = "text";
field.SetHtmlType(test_case.field_type, HtmlFieldMode());
field.max_length = test_case.field_max_length;
AutofillField::FillFormField(field, ASCIIToUTF16(test_case.value_to_fill),
"en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value);
}
}
TEST_F(AutofillFieldTest, FillExpirationDateInput) {
typedef struct {
HtmlFieldType field_type;
size_t field_max_length;
std::string value_to_fill;
std::string expected_value;
bool expected_response;
} TestCase;
TestCase test_cases[] = {
// Test invalid inputs
{HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 0, "1/21", "", false},
{HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 0, "01-21", "", false},
{HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 0, "1/2021", "", false},
{HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 0, "01-2021", "", false},
// Trim whitespace
{HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 0, "01/22 ", "01/22",
true},
{HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 0, "01/2022 ", "01/2022",
true},
// A field predicted as a expiration date w/ 2 digit year should fill
// with a format of MM/YY unless it has max-length of:
// 4: Use format MMYY
// 6: Use format MMYYYY
// 7: Use format MM/YYYY
{HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, /* default value */ 0,
"01/23", "01/23", true},
// Unsupported max lengths of 1-3, fail
{HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 1, "01/23", "", false},
{HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 2, "02/23", "", false},
{HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 3, "03/23", "", false},
// A max length of 4 indicates a format of MMYY.
{HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 4, "02/23", "0223", true},
// A max length of 6 indicates a format of MMYYYY, the 21st century is
// assumed.
// Desired case of proper max length >= 5
{HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 5, "03/23", "03/23", true},
{HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 6, "04/23", "042023", true},
// A max length of 7 indicates a format of MM/YYYY, the 21st century is
// assumed.
{HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 7, "05/23", "05/2023",
true},
{HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 12, "06/23", "06/23", true},
// A field predicted as a expiration date w/ 4 digit year should fill
// with a format of MM/YYYY unless it has max-length of:
// 4: Use format MMYY
// 5: Use format MM/YY
// 6: Use format MMYYYY
{HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, /* default value */ 0,
"01/2024", "01/2024", true},
// Unsupported max lengths of 1-3, fail
{HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 1, "01/2024", "", false},
{HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 2, "02/2024", "", false},
{HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 3, "03/2024", "", false},
// A max length of 4 indicates a format of MMYY.
{HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 4, "02/2024", "0224", true},
// A max length of 5 indicates a format of MM/YY.
{HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 5, "03/2024", "03/24",
true},
// A max length of 6 indicates a format of MMYYYY.
{HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 6, "04/2024", "042024",
true},
// Desired case of proper max length >= 7
{HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 7, "05/2024", "05/2024",
true},
{HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 12, "06/2024", "06/2024",
true},
};
for (TestCase test_case : test_cases) {
AutofillField field;
field.form_control_type = "text";
field.SetHtmlType(test_case.field_type, HtmlFieldMode());
field.max_length = test_case.field_max_length;
bool response = AutofillField::FillFormField(
field, ASCIIToUTF16(test_case.value_to_fill), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value);
EXPECT_EQ(response, test_case.expected_response);
}
}
TEST_F(AutofillFieldTest, FillSelectControlByValue) {
std::vector<const char*> kOptions = {
"Eenie", "Meenie", "Miney", "Mo",
};
AutofillField field;
test::CreateTestSelectField(kOptions, &field);
// Set semantically empty contents for each option, so that only the values
// can be used for matching.
for (size_t i = 0; i < field.option_contents.size(); ++i)
field.option_contents[i] = base::SizeTToString16(i);
AutofillField::FillFormField(
field, ASCIIToUTF16("Meenie"), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("Meenie"), field.value);
}
TEST_F(AutofillFieldTest, FillSelectControlByContents) {
std::vector<const char*> kOptions = {
"Eenie", "Meenie", "Miney", "Mo",
};
AutofillField field;
test::CreateTestSelectField(kOptions, &field);
// Set semantically empty values for each option, so that only the contents
// can be used for matching.
for (size_t i = 0; i < field.option_values.size(); ++i)
field.option_values[i] = base::SizeTToString16(i);
AutofillField::FillFormField(
field, ASCIIToUTF16("Miney"), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("2"), field.value); // Corresponds to "Miney".
}
TEST_F(AutofillFieldTest, FillSelectWithStates) {
typedef struct {
std::vector<const char*> select_values;
const char* input_value;
const char* expected_value;
} TestCase;
TestCase test_cases[] = {
// Filling the abbreviation.
{{"Alabama", "California"}, "CA", "California"},
// Attempting to fill the full name in a select full of abbreviations.
{{"AL", "CA"}, "California", "CA"},
// Different case and diacritics.
{{"QUÉBEC", "ALBERTA"}, "Quebec", "QUÉBEC"},
// Inexact state names.
{{"SC - South Carolina", "CA - California", "NC - North Carolina"},
"California",
"CA - California"},
// Don't accidentally match "Virginia" to "West Virginia".
{{"WV - West Virginia", "VA - Virginia", "NV - North Virginia"},
"Virginia",
"VA - Virginia"},
// Do accidentally match "Virginia" to "West Virginia".
// TODO(crbug.com/624770): This test should not pass, but it does because
// "Virginia" is a substring of "West Virginia".
{{"WV - West Virginia", "TX - Texas"}, "Virginia", "WV - West Virginia"},
// Tests that substring matches work for full state names (a full token
// match isn't required). Also tests that matches work for states with
// whitespace in the middle.
{{"California.", "North Carolina."}, "North Carolina", "North Carolina."},
{{"NC - North Carolina", "CA - California"}, "CA", "CA - California"},
// These are not states.
{{"NCNCA", "SCNCA"}, "NC", ""}};
for (TestCase test_case : test_cases) {
AutofillField field;
test::CreateTestSelectField(test_case.select_values, &field);
field.set_heuristic_type(ADDRESS_HOME_STATE);
AutofillField::FillFormField(field, UTF8ToUTF16(test_case.input_value),
"en-US", "en-US", &field);
EXPECT_EQ(UTF8ToUTF16(test_case.expected_value), field.value);
}
}
TEST_F(AutofillFieldTest, FillSelectWithCountries) {
typedef struct {
std::vector<const char*> select_values;
const char* input_value;
const char* expected_value;
} TestCase;
TestCase test_cases[] = {// Full country names.
{{"Albania", "Canada"}, "CA", "Canada"},
// Abbreviations.
{{"AL", "CA"}, "Canada", "CA"}};
for (TestCase test_case : test_cases) {
AutofillField field;
test::CreateTestSelectField(test_case.select_values, &field);
field.set_heuristic_type(ADDRESS_HOME_COUNTRY);
AutofillField::FillFormField(field, UTF8ToUTF16(test_case.input_value),
"en-US", "en-US", &field);
EXPECT_EQ(UTF8ToUTF16(test_case.expected_value), field.value);
}
}
TEST_F(AutofillFieldTest, FillSelectControlWithExpirationMonth) {
typedef struct {
std::vector<const char*> select_values;
std::vector<const char*> select_contents;
} TestCase;
TestCase test_cases[] = {
// Values start at 1.
{{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
NotNumericMonthsContentsNoPlaceholder()},
// Values start at 1 but single digits are whitespace padded!
{{" 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", "11", "12"},
NotNumericMonthsContentsNoPlaceholder()},
// Values start at 0.
{{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"},
NotNumericMonthsContentsNoPlaceholder()},
// Values start at 00.
{{"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11"},
NotNumericMonthsContentsNoPlaceholder()},
// The AngularJS framework adds a prefix to number types. Test that it is
// removed.
{{"number:1", "number:2", "number:3", "number:4", "number:5", "number:6",
"number:7", "number:8", "number:9", "number:10", "number:11",
"number:12"},
NotNumericMonthsContentsNoPlaceholder()},
// Values start at 0 and the first content is a placeholder.
{{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
NotNumericMonthsContentsWithPlaceholder()},
// Values start at 1 and the first content is a placeholder.
{{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"},
NotNumericMonthsContentsWithPlaceholder()},
// Values start at 01 and the first content is a placeholder.
{{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
"13"},
NotNumericMonthsContentsWithPlaceholder()},
// Values start at 0 after a placeholder.
{{"?", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"},
NotNumericMonthsContentsWithPlaceholder()},
// Values start at 1 after a placeholder.
{{"?", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
NotNumericMonthsContentsWithPlaceholder()},
// Values start at 0 after a negative number.
{{"-1", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"},
NotNumericMonthsContentsWithPlaceholder()},
// Values start at 1 after a negative number.
{{"-1", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
NotNumericMonthsContentsWithPlaceholder()}};
for (TestCase test_case : test_cases) {
ASSERT_EQ(test_case.select_values.size(), test_case.select_contents.size());
TestFillingExpirationMonth(test_case.select_values,
test_case.select_contents,
test_case.select_values.size());
}
}
TEST_F(AutofillFieldTest, FillSelectControlWithAbbreviatedMonthName) {
std::vector<const char*> kMonthsAbbreviated = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
};
AutofillField field;
test::CreateTestSelectField(kMonthsAbbreviated, &field);
field.set_heuristic_type(CREDIT_CARD_EXP_MONTH);
AutofillField::FillFormField(
field, ASCIIToUTF16("04"), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("Apr"), field.value);
}
TEST_F(AutofillFieldTest, FillSelectControlWithFullMonthName) {
std::vector<const char*> kMonthsFull = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
};
AutofillField field;
test::CreateTestSelectField(kMonthsFull, &field);
field.set_heuristic_type(CREDIT_CARD_EXP_MONTH);
AutofillField::FillFormField(
field, ASCIIToUTF16("04"), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("April"), field.value);
}
TEST_F(AutofillFieldTest, FillSelectControlWithFrenchMonthName) {
std::vector<const char*> kMonthsFrench = {"JANV", "FÉVR.", "MARS",
"décembre"};
AutofillField field;
test::CreateTestSelectField(kMonthsFrench, &field);
field.set_heuristic_type(CREDIT_CARD_EXP_MONTH);
AutofillField::FillFormField(
field, ASCIIToUTF16("02"), "fr-FR", "fr-FR", &field);
EXPECT_EQ(UTF8ToUTF16("FÉVR."), field.value);
AutofillField::FillFormField(
field, ASCIIToUTF16("01"), "fr-FR", "fr-FR", &field);
EXPECT_EQ(UTF8ToUTF16("JANV"), field.value);
AutofillField::FillFormField(
field, ASCIIToUTF16("12"), "fr-FR", "fr-FR", &field);
EXPECT_EQ(UTF8ToUTF16("décembre"), field.value);
}
TEST_F(AutofillFieldTest, FillSelectControlWithNumericMonthSansLeadingZero) {
std::vector<const char*> kMonthsNumeric = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
};
AutofillField field;
test::CreateTestSelectField(kMonthsNumeric, &field);
field.set_heuristic_type(CREDIT_CARD_EXP_MONTH);
AutofillField::FillFormField(
field, ASCIIToUTF16("04"), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("4"), field.value);
}
TEST_F(AutofillFieldTest, FillSelectControlWithTwoDigitCreditCardYear) {
std::vector<const char*> kYears = {"12", "13", "14", "15",
"16", "17", "18", "19"};
AutofillField field;
test::CreateTestSelectField(kYears, &field);
field.set_heuristic_type(CREDIT_CARD_EXP_2_DIGIT_YEAR);
AutofillField::FillFormField(
field, ASCIIToUTF16("2017"), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("17"), field.value);
}
TEST_F(AutofillFieldTest, FillSelectControlWithCreditCardType) {
std::vector<const char*> kCreditCardTypes = {"Visa", "Master Card", "AmEx",
"discover"};
AutofillField field;
test::CreateTestSelectField(kCreditCardTypes, &field);
field.set_heuristic_type(CREDIT_CARD_TYPE);
// Normal case:
AutofillField::FillFormField(
field, ASCIIToUTF16("Visa"), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("Visa"), field.value);
// Filling should be able to handle intervening whitespace:
AutofillField::FillFormField(
field, ASCIIToUTF16("MasterCard"), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("Master Card"), field.value);
// American Express is sometimes abbreviated as AmEx:
AutofillField::FillFormField(
field, ASCIIToUTF16("American Express"), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("AmEx"), field.value);
// Case insensitivity:
AutofillField::FillFormField(
field, ASCIIToUTF16("Discover"), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("discover"), field.value);
}
TEST_F(AutofillFieldTest, FillMonthControl) {
AutofillField field;
field.form_control_type = "month";
// Try a month with two digits.
AutofillField::FillFormField(
field, ASCIIToUTF16("12/2017"), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("2017-12"), field.value);
// Try a month with a leading zero.
AutofillField::FillFormField(
field, ASCIIToUTF16("03/2019"), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("2019-03"), field.value);
// Try a month without a leading zero.
AutofillField::FillFormField(
field, ASCIIToUTF16("4/2018"), "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("2018-04"), field.value);
}
TEST_F(AutofillFieldTest, FillStreetAddressTextArea) {
AutofillField field;
field.form_control_type = "textarea";
base::string16 value = ASCIIToUTF16("123 Fake St.\n"
"Apt. 42");
AutofillField::FillFormField(field, value, "en-US", "en-US", &field);
EXPECT_EQ(value, field.value);
base::string16 ja_value = UTF8ToUTF16("桜丘町26-1\n"
"セルリアンタワー6階");
AutofillField::FillFormField(field, ja_value, "ja-JP", "en-US", &field);
EXPECT_EQ(ja_value, field.value);
}
TEST_F(AutofillFieldTest, FillStreetAddressTextField) {
AutofillField field;
field.form_control_type = "text";
field.set_server_type(ADDRESS_HOME_STREET_ADDRESS);
base::string16 value = ASCIIToUTF16("123 Fake St.\n"
"Apt. 42");
AutofillField::FillFormField(field, value, "en-US", "en-US", &field);
EXPECT_EQ(ASCIIToUTF16("123 Fake St., Apt. 42"), field.value);
AutofillField::FillFormField(field,
UTF8ToUTF16("桜丘町26-1\n"
"セルリアンタワー6階"),
"ja-JP",
"en-US",
&field);
EXPECT_EQ(UTF8ToUTF16("桜丘町26-1セルリアンタワー6階"), field.value);
}
TEST_F(AutofillFieldTest, FillCreditCardNumberWithoutSplits) {
// Case 1: card number without any spilt.
AutofillField cc_number_full;
cc_number_full.set_heuristic_type(CREDIT_CARD_NUMBER);
AutofillField::FillFormField(cc_number_full,
ASCIIToUTF16("4111111111111111"),
"en-US",
"en-US",
&cc_number_full);
// Verify that full card-number shall get fill properly.
EXPECT_EQ(ASCIIToUTF16("4111111111111111"), cc_number_full.value);
EXPECT_EQ(0U, cc_number_full.credit_card_number_offset());
}
TEST_F(AutofillFieldTest, FillCreditCardNumberWithEqualSizeSplits) {
// Case 2: card number broken up into four equal groups, of length 4.
TestCase test;
test.card_number_ = "5187654321098765";
test.total_spilts_ = 4;
int splits[] = {4, 4, 4, 4};
test.splits_ = std::vector<int>(splits, splits + arraysize(splits));
std::string results[] = {"5187", "6543", "2109", "8765"};
test.expected_results_ =
std::vector<std::string>(results, results + arraysize(results));
for (size_t i = 0; i < test.total_spilts_; ++i) {
AutofillField cc_number_part;
cc_number_part.set_heuristic_type(CREDIT_CARD_NUMBER);
cc_number_part.max_length = test.splits_[i];
cc_number_part.set_credit_card_number_offset(4 * i);
// Fill with a card-number; should fill just the card_number_part.
AutofillField::FillFormField(cc_number_part,
ASCIIToUTF16(test.card_number_),
"en-US",
"en-US",
&cc_number_part);
// Verify for expected results.
EXPECT_EQ(ASCIIToUTF16(test.expected_results_[i]),
cc_number_part.value.substr(0, cc_number_part.max_length));
EXPECT_EQ(4 * i, cc_number_part.credit_card_number_offset());
}
// Verify that full card-number shall get fill properly as well.
AutofillField cc_number_full;
cc_number_full.set_heuristic_type(CREDIT_CARD_NUMBER);
AutofillField::FillFormField(cc_number_full,
ASCIIToUTF16(test.card_number_),
"en-US",
"en-US",
&cc_number_full);
// Verify for expected results.
EXPECT_EQ(ASCIIToUTF16(test.card_number_), cc_number_full.value);
}
TEST_F(AutofillFieldTest, FillCreditCardNumberWithUnequalSizeSplits) {
// Case 3: card with 15 digits number, broken up into three unequal groups, of
// lengths 4, 6, and 5.
TestCase test;
test.card_number_ = "423456789012345";
test.total_spilts_ = 3;
int splits[] = {4, 6, 5};
test.splits_ = std::vector<int>(splits, splits + arraysize(splits));
std::string results[] = {"4234", "567890", "12345"};
test.expected_results_ =
std::vector<std::string>(results, results + arraysize(results));
// Start executing test cases to verify parts and full credit card number.
for (size_t i = 0; i < test.total_spilts_; ++i) {
AutofillField cc_number_part;
cc_number_part.set_heuristic_type(CREDIT_CARD_NUMBER);
cc_number_part.max_length = test.splits_[i];
cc_number_part.set_credit_card_number_offset(GetNumberOffset(i, test));
// Fill with a card-number; should fill just the card_number_part.
AutofillField::FillFormField(cc_number_part,
ASCIIToUTF16(test.card_number_),
"en-US",
"en-US",
&cc_number_part);
// Verify for expected results.
EXPECT_EQ(ASCIIToUTF16(test.expected_results_[i]),
cc_number_part.value.substr(0, cc_number_part.max_length));
EXPECT_EQ(GetNumberOffset(i, test),
cc_number_part.credit_card_number_offset());
}
// Verify that full card-number shall get fill properly as well.
AutofillField cc_number_full;
cc_number_full.set_heuristic_type(CREDIT_CARD_NUMBER);
AutofillField::FillFormField(cc_number_full,
ASCIIToUTF16(test.card_number_),
"en-US",
"en-US",
&cc_number_full);
// Verify for expected results.
EXPECT_EQ(ASCIIToUTF16(test.card_number_), cc_number_full.value);
}
TEST_F(AutofillFieldTest, FindShortestSubstringMatchInSelect) {
std::vector<const char*> kCountries = {"États-Unis", "Canada"};
AutofillField field;
test::CreateTestSelectField(kCountries, &field);
// Case 1: Exact match
int ret = AutofillField::FindShortestSubstringMatchInSelect(
ASCIIToUTF16("Canada"), false, &field);
EXPECT_EQ(1, ret);
// Case 2: Case-insensitive
ret = AutofillField::FindShortestSubstringMatchInSelect(
ASCIIToUTF16("CANADA"), false, &field);
EXPECT_EQ(1, ret);
// Case 3: Proper substring
ret = AutofillField::FindShortestSubstringMatchInSelect(UTF8ToUTF16("États"),
false, &field);
EXPECT_EQ(0, ret);
// Case 4: Accent-insensitive
ret = AutofillField::FindShortestSubstringMatchInSelect(
UTF8ToUTF16("Etats-Unis"), false, &field);
EXPECT_EQ(0, ret);
// Case 5: Whitespace-insensitive
ret = AutofillField::FindShortestSubstringMatchInSelect(
ASCIIToUTF16("Ca na da"), true, &field);
EXPECT_EQ(1, ret);
// Case 6: No match (whitespace-sensitive)
ret = AutofillField::FindShortestSubstringMatchInSelect(
ASCIIToUTF16("Ca Na Da"), false, &field);
EXPECT_EQ(-1, ret);
// Case 7: No match (not present)
ret = AutofillField::FindShortestSubstringMatchInSelect(
ASCIIToUTF16("Canadia"), true, &field);
EXPECT_EQ(-1, ret);
}
// Tests that text state fields are filled correctly depending on their
// maxlength attribute value.
TEST_F(AutofillFieldTest, FillStateText) {
typedef struct {
HtmlFieldType field_type;
size_t field_max_length;
std::string value_to_fill;
std::string expected_value;
bool should_fill;
} TestCase;
TestCase test_cases[] = {
// Filling a state to a text field with the default maxlength value should
// fill the state value as is.
{HTML_TYPE_ADDRESS_LEVEL1, /* default value */ 0, "New York", "New York",
true},
{HTML_TYPE_ADDRESS_LEVEL1, /* default value */ 0, "NY", "NY", true},
// Filling a state to a text field with a maxlength value equal to the
// value's length should fill the state value as is.
{HTML_TYPE_ADDRESS_LEVEL1, 8, "New York", "New York", true},
// Filling a state to a text field with a maxlength value lower than the
// value's length but higher than the value's abbreviation should fill the
// state abbreviation.
{HTML_TYPE_ADDRESS_LEVEL1, 2, "New York", "NY", true},
{HTML_TYPE_ADDRESS_LEVEL1, 2, "NY", "NY", true},
// Filling a state to a text field with a maxlength value lower than the
// value's length and the value's abbreviation should not fill at all.
{HTML_TYPE_ADDRESS_LEVEL1, 1, "New York", "", false},
{HTML_TYPE_ADDRESS_LEVEL1, 1, "NY", "", false},
// Filling a state to a text field with a maxlength value lower than the
// value's length and that has no associated abbreviation should not fill
// at all.
{HTML_TYPE_ADDRESS_LEVEL1, 3, "Quebec", "", false}};
for (const TestCase& test_case : test_cases) {
AutofillField field;
field.SetHtmlType(test_case.field_type, HtmlFieldMode());
field.max_length = test_case.field_max_length;
bool has_filled = AutofillField::FillFormField(
field, ASCIIToUTF16(test_case.value_to_fill), "en-US", "en-US", &field);
EXPECT_EQ(test_case.should_fill, has_filled);
EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value);
}
}
} // namespace
} // namespace autofill
|