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
3 // found in the LICENSE file.
5 #include "components/autofill/core/browser/autofill_field.h"
7 #include "base/format_macros.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/autofill/core/browser/autofill_type.h"
12 #include "components/autofill/core/browser/field_types.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using base::ASCIIToUTF16
;
16 using base::UTF8ToUTF16
;
21 // Returns a FormFieldData object corresponding to a <select> field populated
22 // with the given |options|.
23 FormFieldData
GenerateSelectFieldWithOptions(const char* const* options
,
24 size_t options_size
) {
25 std::vector
<base::string16
> options16(options_size
);
26 for (size_t i
= 0; i
< options_size
; ++i
) {
27 options16
[i
] = ASCIIToUTF16(options
[i
]);
30 FormFieldData form_field
;
31 form_field
.form_control_type
= "select-one";
32 form_field
.option_values
= options16
;
33 form_field
.option_contents
= options16
;
38 std::string card_number_
;
40 std::vector
<int> splits_
;
41 std::vector
<std::string
> expected_results_
;
44 // Returns the offset to be set within the credit card number field.
45 size_t GetNumberOffset(size_t index
, const TestCase
& test
) {
47 for (size_t i
= 0; i
< index
; ++i
)
48 result
+= test
.splits_
[i
];
52 TEST(AutofillFieldTest
, Type
) {
54 ASSERT_EQ(NO_SERVER_DATA
, field
.server_type());
55 ASSERT_EQ(UNKNOWN_TYPE
, field
.heuristic_type());
57 // |server_type_| is NO_SERVER_DATA, so |heuristic_type_| is returned.
58 EXPECT_EQ(UNKNOWN_TYPE
, field
.Type().GetStorableType());
60 // Set the heuristic type and check it.
61 field
.set_heuristic_type(NAME_FIRST
);
62 EXPECT_EQ(NAME_FIRST
, field
.Type().GetStorableType());
63 EXPECT_EQ(NAME
, field
.Type().group());
65 // Set the server type and check it.
66 field
.set_server_type(ADDRESS_BILLING_LINE1
);
67 EXPECT_EQ(ADDRESS_HOME_LINE1
, field
.Type().GetStorableType());
68 EXPECT_EQ(ADDRESS_BILLING
, field
.Type().group());
70 // Remove the server type to make sure the heuristic type is preserved.
71 field
.set_server_type(NO_SERVER_DATA
);
72 EXPECT_EQ(NAME_FIRST
, field
.Type().GetStorableType());
73 EXPECT_EQ(NAME
, field
.Type().group());
76 TEST(AutofillFieldTest
, IsEmpty
) {
78 ASSERT_EQ(base::string16(), field
.value
);
80 // Field value is empty.
81 EXPECT_TRUE(field
.IsEmpty());
83 // Field value is non-empty.
84 field
.value
= ASCIIToUTF16("Value");
85 EXPECT_FALSE(field
.IsEmpty());
88 TEST(AutofillFieldTest
, FieldSignature
) {
90 ASSERT_EQ(base::string16(), field
.name
);
91 ASSERT_EQ(std::string(), field
.form_control_type
);
93 // Signature is empty.
94 EXPECT_EQ("2085434232", field
.FieldSignature());
97 field
.name
= ASCIIToUTF16("Name");
98 EXPECT_EQ("1606968241", field
.FieldSignature());
100 // Field form control type is set.
101 field
.form_control_type
= "text";
102 EXPECT_EQ("502192749", field
.FieldSignature());
104 // Heuristic type does not affect FieldSignature.
105 field
.set_heuristic_type(NAME_FIRST
);
106 EXPECT_EQ("502192749", field
.FieldSignature());
108 // Server type does not affect FieldSignature.
109 field
.set_server_type(NAME_LAST
);
110 EXPECT_EQ("502192749", field
.FieldSignature());
113 TEST(AutofillFieldTest
, IsFieldFillable
) {
115 ASSERT_EQ(UNKNOWN_TYPE
, field
.Type().GetStorableType());
118 EXPECT_FALSE(field
.IsFieldFillable());
120 // Only heuristic type is set.
121 field
.set_heuristic_type(NAME_FIRST
);
122 EXPECT_TRUE(field
.IsFieldFillable());
124 // Only server type is set.
125 field
.set_heuristic_type(UNKNOWN_TYPE
);
126 field
.set_server_type(NAME_LAST
);
127 EXPECT_TRUE(field
.IsFieldFillable());
130 field
.set_heuristic_type(NAME_FIRST
);
131 field
.set_server_type(NAME_LAST
);
132 EXPECT_TRUE(field
.IsFieldFillable());
134 // Field has autocomplete="off" set. Chrome ignores the attribute.
135 field
.should_autocomplete
= false;
136 EXPECT_TRUE(field
.IsFieldFillable());
139 TEST(AutofillFieldTest
, FillPhoneNumber
) {
141 field
.SetHtmlType(HTML_TYPE_TEL_LOCAL_PREFIX
, HtmlFieldMode());
143 // Fill with a non-phone number; should fill normally.
144 AutofillField::FillFormField(
145 field
, ASCIIToUTF16("Oh hai"), "en-US", "en-US", &field
);
146 EXPECT_EQ(ASCIIToUTF16("Oh hai"), field
.value
);
148 // Fill with a phone number; should fill just the prefix.
149 AutofillField::FillFormField(
150 field
, ASCIIToUTF16("5551234"), "en-US", "en-US", &field
);
151 EXPECT_EQ(ASCIIToUTF16("555"), field
.value
);
153 // Now reset the type, and set a max-length instead.
154 field
.SetHtmlType(HTML_TYPE_UNKNOWN
, HtmlFieldMode());
155 field
.set_heuristic_type(PHONE_HOME_NUMBER
);
156 field
.max_length
= 4;
158 // Fill with a phone-number; should fill just the suffix.
159 AutofillField::FillFormField(
160 field
, ASCIIToUTF16("5551234"), "en-US", "en-US", &field
);
161 EXPECT_EQ(ASCIIToUTF16("1234"), field
.value
);
164 TEST(AutofillFieldTest
, FillSelectControlByValue
) {
165 const char* const kOptions
[] = {
166 "Eenie", "Meenie", "Miney", "Mo",
169 GenerateSelectFieldWithOptions(kOptions
, arraysize(kOptions
)),
172 // Set semantically empty contents for each option, so that only the values
173 // can be used for matching.
174 for (size_t i
= 0; i
< field
.option_contents
.size(); ++i
) {
175 field
.option_contents
[i
] = ASCIIToUTF16(base::StringPrintf("%" PRIuS
, i
));
178 AutofillField::FillFormField(
179 field
, ASCIIToUTF16("Meenie"), "en-US", "en-US", &field
);
180 EXPECT_EQ(ASCIIToUTF16("Meenie"), field
.value
);
183 TEST(AutofillFieldTest
, FillSelectControlByContents
) {
184 const char* const kOptions
[] = {
185 "Eenie", "Meenie", "Miney", "Mo",
188 GenerateSelectFieldWithOptions(kOptions
, arraysize(kOptions
)),
191 // Set semantically empty values for each option, so that only the contents
192 // can be used for matching.
193 for (size_t i
= 0; i
< field
.option_values
.size(); ++i
) {
194 field
.option_values
[i
] = ASCIIToUTF16(base::StringPrintf("%" PRIuS
, i
));
197 AutofillField::FillFormField(
198 field
, ASCIIToUTF16("Miney"), "en-US", "en-US", &field
);
199 EXPECT_EQ(ASCIIToUTF16("2"), field
.value
); // Corresponds to "Miney".
202 TEST(AutofillFieldTest
, FillSelectControlWithFullCountryNames
) {
203 const char* const kCountries
[] = {
207 GenerateSelectFieldWithOptions(kCountries
, arraysize(kCountries
)),
209 field
.set_heuristic_type(ADDRESS_HOME_COUNTRY
);
211 AutofillField::FillFormField(
212 field
, ASCIIToUTF16("CA"), "en-US", "en-US", &field
);
213 EXPECT_EQ(ASCIIToUTF16("Canada"), field
.value
);
216 TEST(AutofillFieldTest
, FillSelectControlWithAbbreviatedCountryNames
) {
217 const char* const kCountries
[] = {
221 GenerateSelectFieldWithOptions(kCountries
, arraysize(kCountries
)),
223 field
.set_heuristic_type(ADDRESS_HOME_COUNTRY
);
225 AutofillField::FillFormField(
226 field
, ASCIIToUTF16("Canada"), "en-US", "en-US", &field
);
227 EXPECT_EQ(ASCIIToUTF16("CA"), field
.value
);
230 TEST(AutofillFieldTest
, FillSelectControlWithFullStateNames
) {
231 const char* const kStates
[] = {
232 "Alabama", "California"
235 GenerateSelectFieldWithOptions(kStates
, arraysize(kStates
)),
237 field
.set_heuristic_type(ADDRESS_HOME_STATE
);
239 AutofillField::FillFormField(
240 field
, ASCIIToUTF16("CA"), "en-US", "en-US", &field
);
241 EXPECT_EQ(ASCIIToUTF16("California"), field
.value
);
244 TEST(AutofillFieldTest
, FillSelectControlWithAbbreviateStateNames
) {
245 const char* const kStates
[] = {
249 GenerateSelectFieldWithOptions(kStates
, arraysize(kStates
)),
251 field
.set_heuristic_type(ADDRESS_HOME_STATE
);
253 AutofillField::FillFormField(
254 field
, ASCIIToUTF16("California"), "en-US", "en-US", &field
);
255 EXPECT_EQ(ASCIIToUTF16("CA"), field
.value
);
258 TEST(AutofillFieldTest
, FillSelectControlWithInexactFullStateNames
) {
260 const char* const kStates
[] = {
261 "SC - South Carolina", "CA - California", "NC - North Carolina",
264 GenerateSelectFieldWithOptions(kStates
, arraysize(kStates
)),
266 field
.set_heuristic_type(ADDRESS_HOME_STATE
);
268 AutofillField::FillFormField(
269 field
, ASCIIToUTF16("California"), "en-US", "en-US", &field
);
270 EXPECT_EQ(ASCIIToUTF16("CA - California"), field
.value
);
273 // Don't accidentally match "Virginia" to "West Virginia".
275 const char* const kStates
[] = {
276 "WV - West Virginia", "VA - Virginia", "NV - North Virginia",
279 GenerateSelectFieldWithOptions(kStates
, arraysize(kStates
)),
281 field
.set_heuristic_type(ADDRESS_HOME_STATE
);
283 AutofillField::FillFormField(
284 field
, ASCIIToUTF16("Virginia"), "en-US", "en-US", &field
);
285 EXPECT_EQ(ASCIIToUTF16("VA - Virginia"), field
.value
);
288 // Do accidentally match "Virginia" to "West Virginia". NB: Ideally, Chrome
289 // would fail this test. It's here to document behavior rather than enforce
292 const char* const kStates
[] = {
293 "WV - West Virginia", "TX - Texas",
296 GenerateSelectFieldWithOptions(kStates
, arraysize(kStates
)),
298 field
.set_heuristic_type(ADDRESS_HOME_STATE
);
300 AutofillField::FillFormField(
301 field
, ASCIIToUTF16("Virginia"), "en-US", "en-US", &field
);
302 EXPECT_EQ(ASCIIToUTF16("WV - West Virginia"), field
.value
);
305 // Tests that substring matches work for full state names (a full token
306 // match isn't required). Also tests that matches work for states with
307 // whitespace in the middle.
309 const char* const kStates
[] = {
310 "California.", "North Carolina.",
313 GenerateSelectFieldWithOptions(kStates
, arraysize(kStates
)),
315 field
.set_heuristic_type(ADDRESS_HOME_STATE
);
317 AutofillField::FillFormField(
318 field
, ASCIIToUTF16("North Carolina"), "en-US", "en-US", &field
);
319 EXPECT_EQ(ASCIIToUTF16("North Carolina."), field
.value
);
323 TEST(AutofillFieldTest
, FillSelectControlWithInexactAbbreviations
) {
325 const char* const kStates
[] = {
326 "NC - North Carolina", "CA - California",
329 GenerateSelectFieldWithOptions(kStates
, arraysize(kStates
)),
331 field
.set_heuristic_type(ADDRESS_HOME_STATE
);
333 AutofillField::FillFormField(
334 field
, ASCIIToUTF16("CA"), "en-US", "en-US", &field
);
335 EXPECT_EQ(ASCIIToUTF16("CA - California"), field
.value
);
339 const char* const kNotStates
[] = {
343 GenerateSelectFieldWithOptions(kNotStates
, arraysize(kNotStates
)),
345 field
.set_heuristic_type(ADDRESS_HOME_STATE
);
347 AutofillField::FillFormField(
348 field
, ASCIIToUTF16("NC"), "en-US", "en-US", &field
);
349 EXPECT_EQ(base::string16(), field
.value
);
353 TEST(AutofillFieldTest
, FillSelectControlWithNumericMonth
) {
354 const char* const kMonthsNumeric
[] = {
355 "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
358 GenerateSelectFieldWithOptions(kMonthsNumeric
, arraysize(kMonthsNumeric
)),
360 field
.set_heuristic_type(CREDIT_CARD_EXP_MONTH
);
362 // Try with a leading zero.
363 AutofillField::FillFormField(
364 field
, ASCIIToUTF16("03"), "en-US", "en-US", &field
);
365 EXPECT_EQ(ASCIIToUTF16("03"), field
.value
);
367 // Try without a leading zero.
368 AutofillField::FillFormField(
369 field
, ASCIIToUTF16("4"), "en-US", "en-US", &field
);
370 EXPECT_EQ(ASCIIToUTF16("04"), field
.value
);
372 // Try a two-digit month.
373 AutofillField::FillFormField(
374 field
, ASCIIToUTF16("11"), "en-US", "en-US", &field
);
375 EXPECT_EQ(ASCIIToUTF16("11"), field
.value
);
378 TEST(AutofillFieldTest
, FillSelectControlWithAbbreviatedMonthName
) {
379 const char* const kMonthsAbbreviated
[] = {
380 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
381 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
384 GenerateSelectFieldWithOptions(
385 kMonthsAbbreviated
, arraysize(kMonthsAbbreviated
)),
387 field
.set_heuristic_type(CREDIT_CARD_EXP_MONTH
);
389 AutofillField::FillFormField(
390 field
, ASCIIToUTF16("04"), "en-US", "en-US", &field
);
391 EXPECT_EQ(ASCIIToUTF16("Apr"), field
.value
);
394 TEST(AutofillFieldTest
, FillSelectControlWithFullMonthName
) {
395 const char* const kMonthsFull
[] = {
396 "January","February", "March", "April", "May", "June",
397 "July", "August", "September", "October", "November", "December",
400 GenerateSelectFieldWithOptions(kMonthsFull
, arraysize(kMonthsFull
)),
402 field
.set_heuristic_type(CREDIT_CARD_EXP_MONTH
);
404 AutofillField::FillFormField(
405 field
, ASCIIToUTF16("04"), "en-US", "en-US", &field
);
406 EXPECT_EQ(ASCIIToUTF16("April"), field
.value
);
409 TEST(AutofillFieldTest
, FillSelectControlWithNumericMonthSansLeadingZero
) {
410 const char* const kMonthsNumeric
[] = {
411 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
414 GenerateSelectFieldWithOptions(kMonthsNumeric
, arraysize(kMonthsNumeric
)),
416 field
.set_heuristic_type(CREDIT_CARD_EXP_MONTH
);
418 AutofillField::FillFormField(
419 field
, ASCIIToUTF16("04"), "en-US", "en-US", &field
);
420 EXPECT_EQ(ASCIIToUTF16("4"), field
.value
);
423 TEST(AutofillFieldTest
, FillSelectControlWithTwoDigitCreditCardYear
) {
424 const char* const kYears
[] = {
425 "12", "13", "14", "15", "16", "17", "18", "19"
427 AutofillField
field(GenerateSelectFieldWithOptions(kYears
, arraysize(kYears
)),
429 field
.set_heuristic_type(CREDIT_CARD_EXP_2_DIGIT_YEAR
);
431 AutofillField::FillFormField(
432 field
, ASCIIToUTF16("2017"), "en-US", "en-US", &field
);
433 EXPECT_EQ(ASCIIToUTF16("17"), field
.value
);
436 TEST(AutofillFieldTest
, FillSelectControlWithCreditCardType
) {
437 const char* const kCreditCardTypes
[] = {
438 "Visa", "Master Card", "AmEx", "discover"
441 GenerateSelectFieldWithOptions(
442 kCreditCardTypes
, arraysize(kCreditCardTypes
)),
444 field
.set_heuristic_type(CREDIT_CARD_TYPE
);
447 AutofillField::FillFormField(
448 field
, ASCIIToUTF16("Visa"), "en-US", "en-US", &field
);
449 EXPECT_EQ(ASCIIToUTF16("Visa"), field
.value
);
451 // Filling should be able to handle intervening whitespace:
452 AutofillField::FillFormField(
453 field
, ASCIIToUTF16("MasterCard"), "en-US", "en-US", &field
);
454 EXPECT_EQ(ASCIIToUTF16("Master Card"), field
.value
);
456 // American Express is sometimes abbreviated as AmEx:
457 AutofillField::FillFormField(
458 field
, ASCIIToUTF16("American Express"), "en-US", "en-US", &field
);
459 EXPECT_EQ(ASCIIToUTF16("AmEx"), field
.value
);
461 // Case insensitivity:
462 AutofillField::FillFormField(
463 field
, ASCIIToUTF16("Discover"), "en-US", "en-US", &field
);
464 EXPECT_EQ(ASCIIToUTF16("discover"), field
.value
);
467 TEST(AutofillFieldTest
, FillMonthControl
) {
469 field
.form_control_type
= "month";
471 // Try a month with two digits.
472 AutofillField::FillFormField(
473 field
, ASCIIToUTF16("12/2017"), "en-US", "en-US", &field
);
474 EXPECT_EQ(ASCIIToUTF16("2017-12"), field
.value
);
476 // Try a month with a leading zero.
477 AutofillField::FillFormField(
478 field
, ASCIIToUTF16("03/2019"), "en-US", "en-US", &field
);
479 EXPECT_EQ(ASCIIToUTF16("2019-03"), field
.value
);
481 // Try a month without a leading zero.
482 AutofillField::FillFormField(
483 field
, ASCIIToUTF16("4/2018"), "en-US", "en-US", &field
);
484 EXPECT_EQ(ASCIIToUTF16("2018-04"), field
.value
);
487 TEST(AutofillFieldTest
, FillStreetAddressTextArea
) {
489 field
.form_control_type
= "textarea";
491 base::string16 value
= ASCIIToUTF16("123 Fake St.\n"
493 AutofillField::FillFormField(field
, value
, "en-US", "en-US", &field
);
494 EXPECT_EQ(value
, field
.value
);
496 base::string16 ja_value
= UTF8ToUTF16("桜丘町26-1\n"
498 AutofillField::FillFormField(field
, ja_value
, "ja-JP", "en-US", &field
);
499 EXPECT_EQ(ja_value
, field
.value
);
502 TEST(AutofillFieldTest
, FillStreetAddressTextField
) {
504 field
.form_control_type
= "text";
505 field
.set_server_type(ADDRESS_HOME_STREET_ADDRESS
);
507 base::string16 value
= ASCIIToUTF16("123 Fake St.\n"
509 AutofillField::FillFormField(field
, value
, "en-US", "en-US", &field
);
510 EXPECT_EQ(ASCIIToUTF16("123 Fake St., Apt. 42"), field
.value
);
512 AutofillField::FillFormField(field
,
513 UTF8ToUTF16("桜丘町26-1\n"
518 EXPECT_EQ(UTF8ToUTF16("桜丘町26-1セルリアンタワー6階"), field
.value
);
521 TEST(AutofillFieldTest
, FillCreditCardNumberWithoutSplits
) {
522 // Case 1: card number without any spilt.
523 AutofillField cc_number_full
;
524 cc_number_full
.set_heuristic_type(CREDIT_CARD_NUMBER
);
525 AutofillField::FillFormField(cc_number_full
,
526 ASCIIToUTF16("4111111111111111"),
531 // Verify that full card-number shall get fill properly.
532 EXPECT_EQ(ASCIIToUTF16("4111111111111111"), cc_number_full
.value
);
533 EXPECT_EQ(0U, cc_number_full
.credit_card_number_offset());
536 TEST(AutofillFieldTest
, FillCreditCardNumberWithEqualSizeSplits
) {
537 // Case 2: card number broken up into four equal groups, of length 4.
539 test
.card_number_
= "5187654321098765";
540 test
.total_spilts_
= 4;
541 int splits
[] = {4, 4, 4, 4};
542 test
.splits_
= std::vector
<int>(splits
, splits
+ arraysize(splits
));
543 std::string results
[] = {"5187", "6543", "2109", "8765"};
544 test
.expected_results_
=
545 std::vector
<std::string
>(results
, results
+ arraysize(results
));
547 for (size_t i
= 0; i
< test
.total_spilts_
; ++i
) {
548 AutofillField cc_number_part
;
549 cc_number_part
.set_heuristic_type(CREDIT_CARD_NUMBER
);
550 cc_number_part
.max_length
= test
.splits_
[i
];
551 cc_number_part
.set_credit_card_number_offset(4 * i
);
553 // Fill with a card-number; should fill just the card_number_part.
554 AutofillField::FillFormField(cc_number_part
,
555 ASCIIToUTF16(test
.card_number_
),
560 // Verify for expected results.
561 EXPECT_EQ(ASCIIToUTF16(test
.expected_results_
[i
]),
562 cc_number_part
.value
.substr(0, cc_number_part
.max_length
));
563 EXPECT_EQ(4 * i
, cc_number_part
.credit_card_number_offset());
566 // Verify that full card-number shall get fill properly as well.
567 AutofillField cc_number_full
;
568 cc_number_full
.set_heuristic_type(CREDIT_CARD_NUMBER
);
569 AutofillField::FillFormField(cc_number_full
,
570 ASCIIToUTF16(test
.card_number_
),
575 // Verify for expected results.
576 EXPECT_EQ(ASCIIToUTF16(test
.card_number_
), cc_number_full
.value
);
579 TEST(AutofillFieldTest
, FillCreditCardNumberWithUnequalSizeSplits
) {
580 // Case 3: card with 15 digits number, broken up into three unequal groups, of
581 // lengths 4, 6, and 5.
583 test
.card_number_
= "423456789012345";
584 test
.total_spilts_
= 3;
585 int splits
[] = {4, 6, 5};
586 test
.splits_
= std::vector
<int>(splits
, splits
+ arraysize(splits
));
587 std::string results
[] = {"4234", "567890", "12345"};
588 test
.expected_results_
=
589 std::vector
<std::string
>(results
, results
+ arraysize(results
));
591 // Start executing test cases to verify parts and full credit card number.
592 for (size_t i
= 0; i
< test
.total_spilts_
; ++i
) {
593 AutofillField cc_number_part
;
594 cc_number_part
.set_heuristic_type(CREDIT_CARD_NUMBER
);
595 cc_number_part
.max_length
= test
.splits_
[i
];
596 cc_number_part
.set_credit_card_number_offset(GetNumberOffset(i
, test
));
598 // Fill with a card-number; should fill just the card_number_part.
599 AutofillField::FillFormField(cc_number_part
,
600 ASCIIToUTF16(test
.card_number_
),
605 // Verify for expected results.
606 EXPECT_EQ(ASCIIToUTF16(test
.expected_results_
[i
]),
607 cc_number_part
.value
.substr(0, cc_number_part
.max_length
));
608 EXPECT_EQ(GetNumberOffset(i
, test
),
609 cc_number_part
.credit_card_number_offset());
612 // Verify that full card-number shall get fill properly as well.
613 AutofillField cc_number_full
;
614 cc_number_full
.set_heuristic_type(CREDIT_CARD_NUMBER
);
615 AutofillField::FillFormField(cc_number_full
,
616 ASCIIToUTF16(test
.card_number_
),
621 // Verify for expected results.
622 EXPECT_EQ(ASCIIToUTF16(test
.card_number_
), cc_number_full
.value
);
626 } // namespace autofill