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

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

Issue 2744933004: [Autofill] Rewrite Autofill unitttests to use INSTANTIATE_TEST_CASE_P (Closed)
Patch Set: More unit tests rewrites Created 3 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 "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 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 276
277 // Credit card related field. 277 // Credit card related field.
278 field.set_heuristic_type(CREDIT_CARD_NUMBER); 278 field.set_heuristic_type(CREDIT_CARD_NUMBER);
279 AutofillField::FillFormField(field, ASCIIToUTF16("4111111111111111"), "en-US", 279 AutofillField::FillFormField(field, ASCIIToUTF16("4111111111111111"), "en-US",
280 "en-US", &field); 280 "en-US", &field);
281 281
282 // Verify that the field is filled. 282 // Verify that the field is filled.
283 EXPECT_EQ(ASCIIToUTF16("4111111111111111"), field.value); 283 EXPECT_EQ(ASCIIToUTF16("4111111111111111"), field.value);
284 } 284 }
285 285
286 struct AutofillFieldTestCase {
287 HtmlFieldType field_type;
288 size_t field_max_length;
289 std::string value_to_fill;
290 std::string expected_value;
291 };
292
293 class AutofillFieldPhoneNumberTest
294 : public testing::TestWithParam<AutofillFieldTestCase> {
295 public:
296 AutofillFieldPhoneNumberTest() { CountryNames::SetLocaleString("en-US"); }
297 };
298
286 // TODO(crbug.com/581514): Add support for filling only the prefix/suffix for 299 // TODO(crbug.com/581514): Add support for filling only the prefix/suffix for
287 // phone numbers with 10 or 11 digits. 300 // phone numbers with 10 or 11 digits.
288 TEST_F(AutofillFieldTest, FillPhoneNumber) { 301 TEST_P(AutofillFieldPhoneNumberTest, FillPhoneNumber) {
289 typedef struct { 302 auto test_case = GetParam();
290 HtmlFieldType field_type; 303 AutofillField field;
291 size_t field_max_length; 304 field.SetHtmlType(test_case.field_type, HtmlFieldMode());
292 std::string value_to_fill; 305 field.max_length = test_case.field_max_length;
293 std::string expected_value; 306
294 } TestCase; 307 AutofillField::FillFormField(field, ASCIIToUTF16(test_case.value_to_fill),
295 308 "en-US", "en-US", &field);
296 TestCase test_cases[] = { 309 EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value);
297 // Filling a phone type field with text should fill the text as is. 310 }
298 {HTML_TYPE_TEL, /* default value */ 0, "Oh hai", "Oh hai"}, 311
299 // Filling a prefix type field with a phone number of 7 digits should just 312 INSTANTIATE_TEST_CASE_P(
300 // fill the prefix. 313 AutofillField,
sebsg 2017/03/13 14:56:10 Could you replace by "AutofillFieldTest" here and
wuandy 2017/03/14 19:05:36 Done.
301 {HTML_TYPE_TEL_LOCAL_PREFIX, /* default value */ 0, "5551234", "555"}, 314 AutofillFieldPhoneNumberTest,
302 // Filling a suffix type field with a phone number of 7 digits should just 315 testing::Values(
303 // fill the suffix. 316 // Filling a phone type field with text should fill the text as is.
304 {HTML_TYPE_TEL_LOCAL_SUFFIX, /* default value */ 0, "5551234", "1234"}, 317 AutofillFieldTestCase{HTML_TYPE_TEL, /* default value */ 0, "Oh hai",
305 // Filling a phone type field with a max length of 3 with a phone number 318 "Oh hai"},
306 // of 319 // Filling a prefix type field with a phone number of 7 digits should
307 // 7 digits should fill only the prefix. 320 // just fill the prefix.
308 {HTML_TYPE_TEL, 3, "5551234", "555"}, 321 AutofillFieldTestCase{HTML_TYPE_TEL_LOCAL_PREFIX, /* default value */ 0,
309 // Filling a phone type field with a max length of 4 with a phone number 322 "5551234", "555"},
310 // of 323 // Filling a suffix type field with a phone number of 7 digits should
311 // 7 digits should fill only the suffix. 324 // just fill the suffix.
312 {HTML_TYPE_TEL, 4, "5551234", "1234"}, 325 AutofillFieldTestCase{HTML_TYPE_TEL_LOCAL_SUFFIX, /* default value */ 0,
313 // Filling a phone type field with a max length of 10 with a phone number 326 "5551234", "1234"},
314 // including the country code should fill the phone number without the 327 // Filling a phone type field with a max length of 3 with a phone number
315 // country code. 328 // of
316 {HTML_TYPE_TEL, 10, "15141254578", "5141254578"}, 329 // 7 digits should fill only the prefix.
317 // Filling a phone type field with a max length of 5 with a phone number 330 AutofillFieldTestCase{HTML_TYPE_TEL, 3, "5551234", "555"},
318 // should fill with the last 5 digits of that phone number. 331 // Filling a phone type field with a max length of 4 with a phone number
319 {HTML_TYPE_TEL, 5, "5141254578", "54578"}}; 332 // of
320 333 // 7 digits should fill only the suffix.
321 for (TestCase test_case : test_cases) { 334 AutofillFieldTestCase{HTML_TYPE_TEL, 4, "5551234", "1234"},
322 AutofillField field; 335 // Filling a phone type field with a max length of 10 with a phone
323 field.SetHtmlType(test_case.field_type, HtmlFieldMode()); 336 // number including the country code should fill the phone number
324 field.max_length = test_case.field_max_length; 337 // without the country code.
325 338 AutofillFieldTestCase{HTML_TYPE_TEL, 10, "15141254578", "5141254578"},
326 AutofillField::FillFormField(field, ASCIIToUTF16(test_case.value_to_fill), 339 // Filling a phone type field with a max length of 5 with a phone number
327 "en-US", "en-US", &field); 340 // should fill with the last 5 digits of that phone number.
328 EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value); 341 AutofillFieldTestCase{HTML_TYPE_TEL, 5, "5141254578", "54578"}));
329 } 342
330 } 343 class AutofillFieldExpirationYearTest
331 344 : public testing::TestWithParam<AutofillFieldTestCase> {
332 TEST_F(AutofillFieldTest, FillExpirationYearInput) { 345 public:
333 typedef struct { 346 AutofillFieldExpirationYearTest() { CountryNames::SetLocaleString("en-US"); }
334 HtmlFieldType field_type; 347 };
335 size_t field_max_length; 348
336 std::string value_to_fill; 349 TEST_P(AutofillFieldExpirationYearTest, FillExpirationYearInput) {
337 std::string expected_value; 350 auto test_case = GetParam();
338 } TestCase; 351 AutofillField field;
339 352 field.form_control_type = "text";
340 TestCase test_cases[] = { 353 field.SetHtmlType(test_case.field_type, HtmlFieldMode());
341 // A field predicted as a 2 digits expiration year should fill the last 2 354 field.max_length = test_case.field_max_length;
342 // digits of the expiration year if the field has an unspecified max 355
343 // length (0) or if it's greater than 1. 356 AutofillField::FillFormField(field, ASCIIToUTF16(test_case.value_to_fill),
344 {HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR, /* default value */ 0, "2023", 357 "en-US", "en-US", &field);
345 "23"}, 358 EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value);
346 {HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR, 2, "2023", "23"}, 359 }
347 {HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR, 12, "2023", "23"}, 360
348 // A field predicted as a 2 digit expiration year should fill the last 361 INSTANTIATE_TEST_CASE_P(
349 // digit of the expiration year if the field has a max length of 1. 362 AutofillField,
350 {HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR, 1, "2023", "3"}, 363 AutofillFieldExpirationYearTest,
351 // A field predicted as a 4 digit expiration year should fill the 4 364 testing::Values(
352 // digits of the expiration year if the field has an unspecified max 365 // A field predicted as a 2 digits expiration year should fill the last
353 // length (0) or if it's greater than 3 . 366 // 2 digits of the expiration year if the field has an unspecified max
354 {HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, /* default value */ 0, "2023", 367 // length (0) or if it's greater than 1.
355 "2023"}, 368 AutofillFieldTestCase{HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR,
356 {HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, 4, "2023", "2023"}, 369 /* default value */ 0, "2023", "23"},
357 {HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, 12, "2023", "2023"}, 370 AutofillFieldTestCase{HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR, 2, "2023",
358 // A field predicted as a 4 digits expiration year should fill the last 2 371 "23"},
359 // digits of the expiration year if the field has a max length of 2. 372 AutofillFieldTestCase{HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR, 12,
360 {HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, 2, "2023", "23"}, 373 "2023", "23"},
361 // A field predicted as a 4 digits expiration year should fill the last 374 // A field predicted as a 2 digit expiration year should fill the last
362 // digit of the expiration year if the field has a max length of 1. 375 // digit of the expiration year if the field has a max length of 1.
363 {HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, 1, "2023", "3"}, 376 AutofillFieldTestCase{HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR, 1, "2023",
364 }; 377 "3"},
365 378 // A field predicted as a 4 digit expiration year should fill the 4
366 for (TestCase test_case : test_cases) { 379 // digits of the expiration year if the field has an unspecified max
367 AutofillField field; 380 // length (0) or if it's greater than 3 .
368 field.form_control_type = "text"; 381 AutofillFieldTestCase{HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR,
369 field.SetHtmlType(test_case.field_type, HtmlFieldMode()); 382 /* default value */ 0, "2023", "2023"},
370 field.max_length = test_case.field_max_length; 383 AutofillFieldTestCase{HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, 4, "2023",
371 384 "2023"},
372 AutofillField::FillFormField(field, ASCIIToUTF16(test_case.value_to_fill), 385 AutofillFieldTestCase{HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, 12,
373 "en-US", "en-US", &field); 386 "2023", "2023"},
374 EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value); 387 // A field predicted as a 4 digits expiration year should fill the last
375 } 388 // 2 digits of the expiration year if the field has a max length of 2.
376 } 389 AutofillFieldTestCase{HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, 2, "2023",
377 390 "23"},
378 TEST_F(AutofillFieldTest, FillExpirationDateInput) { 391 // A field predicted as a 4 digits expiration year should fill the last
379 typedef struct { 392 // digit of the expiration year if the field has a max length of 1.
380 HtmlFieldType field_type; 393 AutofillFieldTestCase{HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR, 1, "2023",
381 size_t field_max_length; 394 "3"}));
382 std::string value_to_fill; 395
383 std::string expected_value; 396 struct AutofillFieldExpirationDateTestCase {
384 bool expected_response; 397 HtmlFieldType field_type;
385 } TestCase; 398 size_t field_max_length;
386 399 std::string value_to_fill;
387 TestCase test_cases[] = { 400 std::string expected_value;
388 // Test invalid inputs 401 bool expected_response;
389 {HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 0, "1/21", "", false}, 402 };
390 {HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 0, "01-21", "", false}, 403
391 {HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 0, "1/2021", "", false}, 404 class AutofillFieldExpirationDateTest
392 {HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 0, "01-2021", "", false}, 405 : public testing::TestWithParam<AutofillFieldExpirationDateTestCase> {
393 // Trim whitespace 406 public:
394 {HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 0, "01/22 ", "01/22", 407 AutofillFieldExpirationDateTest() { CountryNames::SetLocaleString("en-US"); }
395 true}, 408 };
396 {HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 0, "01/2022 ", "01/2022", 409
397 true}, 410 TEST_P(AutofillFieldExpirationDateTest, FillExpirationDateInput) {
398 // A field predicted as a expiration date w/ 2 digit year should fill 411 auto test_case = GetParam();
399 // with a format of MM/YY unless it has max-length of: 412 AutofillField field;
400 // 4: Use format MMYY 413 field.form_control_type = "text";
401 // 6: Use format MMYYYY 414 field.SetHtmlType(test_case.field_type, HtmlFieldMode());
402 // 7: Use format MM/YYYY 415 field.max_length = test_case.field_max_length;
403 {HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, /* default value */ 0, 416
404 "01/23", "01/23", true}, 417 bool response = AutofillField::FillFormField(
405 // Unsupported max lengths of 1-3, fail
406 {HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 1, "01/23", "", false},
407 {HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 2, "02/23", "", false},
408 {HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 3, "03/23", "", false},
409 // A max length of 4 indicates a format of MMYY.
410 {HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 4, "02/23", "0223", true},
411 // A max length of 6 indicates a format of MMYYYY, the 21st century is
412 // assumed.
413 // Desired case of proper max length >= 5
414 {HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 5, "03/23", "03/23", true},
415 {HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 6, "04/23", "042023", true},
416 // A max length of 7 indicates a format of MM/YYYY, the 21st century is
417 // assumed.
418 {HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 7, "05/23", "05/2023",
419 true},
420 {HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 12, "06/23", "06/23", true},
421
422 // A field predicted as a expiration date w/ 4 digit year should fill
423 // with a format of MM/YYYY unless it has max-length of:
424 // 4: Use format MMYY
425 // 5: Use format MM/YY
426 // 6: Use format MMYYYY
427 {HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, /* default value */ 0,
428 "01/2024", "01/2024", true},
429 // Unsupported max lengths of 1-3, fail
430 {HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 1, "01/2024", "", false},
431 {HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 2, "02/2024", "", false},
432 {HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 3, "03/2024", "", false},
433 // A max length of 4 indicates a format of MMYY.
434 {HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 4, "02/2024", "0224", true},
435 // A max length of 5 indicates a format of MM/YY.
436 {HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 5, "03/2024", "03/24",
437 true},
438 // A max length of 6 indicates a format of MMYYYY.
439 {HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 6, "04/2024", "042024",
440 true},
441 // Desired case of proper max length >= 7
442 {HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 7, "05/2024", "05/2024",
443 true},
444 {HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 12, "06/2024", "06/2024",
445 true},
446 };
447
448 for (TestCase test_case : test_cases) {
449 AutofillField field;
450 field.form_control_type = "text";
451 field.SetHtmlType(test_case.field_type, HtmlFieldMode());
452 field.max_length = test_case.field_max_length;
453
454 bool response = AutofillField::FillFormField(
455 field, ASCIIToUTF16(test_case.value_to_fill), "en-US", "en-US", &field); 418 field, ASCIIToUTF16(test_case.value_to_fill), "en-US", "en-US", &field);
456 EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value); 419 EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value);
457 EXPECT_EQ(response, test_case.expected_response); 420 EXPECT_EQ(response, test_case.expected_response);
458 } 421 }
459 } 422
423 INSTANTIATE_TEST_CASE_P(
424 AutofillField,
425 AutofillFieldExpirationDateTest,
426 testing::Values(
427 // Test invalid inputs
428 AutofillFieldExpirationDateTestCase{
429 HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 0, "1/21", "", false},
430 AutofillFieldExpirationDateTestCase{
431 HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 0, "01-21", "", false},
432 AutofillFieldExpirationDateTestCase{
433 HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 0, "1/2021", "",
434 false},
435 AutofillFieldExpirationDateTestCase{
436 HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 0, "01-2021", "",
437 false},
438 // Trim whitespace
439 AutofillFieldExpirationDateTestCase{
440 HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 0, "01/22 ", "01/22",
441 true},
442 AutofillFieldExpirationDateTestCase{
443 HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 0, "01/2022 ",
444 "01/2022", true},
445 // A field predicted as a expiration date w/ 2 digit year should fill
446 // with a format of MM/YY unless it has max-length of:
447 // 4: Use format MMYY
448 // 6: Use format MMYYYY
449 // 7: Use format MM/YYYY
450 AutofillFieldExpirationDateTestCase{
451 HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, /* default value */ 0,
452 "01/23", "01/23", true},
453 // Unsupported max lengths of 1-3, fail
454 AutofillFieldExpirationDateTestCase{
455 HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 1, "01/23", "", false},
456 AutofillFieldExpirationDateTestCase{
457 HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 2, "02/23", "", false},
458 AutofillFieldExpirationDateTestCase{
459 HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 3, "03/23", "", false},
460 // A max length of 4 indicates a format of MMYY.
461 AutofillFieldExpirationDateTestCase{
462 HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 4, "02/23", "0223",
463 true},
464 // A max length of 6 indicates a format of MMYYYY, the 21st century is
465 // assumed.
466 // Desired case of proper max length >= 5
467 AutofillFieldExpirationDateTestCase{
468 HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 5, "03/23", "03/23",
469 true},
470 AutofillFieldExpirationDateTestCase{
471 HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 6, "04/23", "042023",
472 true},
473 // A max length of 7 indicates a format of MM/YYYY, the 21st century is
474 // assumed.
475 AutofillFieldExpirationDateTestCase{
476 HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 7, "05/23", "05/2023",
477 true},
478 AutofillFieldExpirationDateTestCase{
479 HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, 12, "06/23", "06/23",
480 true},
481
482 // A field predicted as a expiration date w/ 4 digit year should fill
483 // with a format of MM/YYYY unless it has max-length of:
484 // 4: Use format MMYY
485 // 5: Use format MM/YY
486 // 6: Use format MMYYYY
487 AutofillFieldExpirationDateTestCase{
488 HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, /* default value */ 0,
489 "01/2024", "01/2024", true},
490 // Unsupported max lengths of 1-3, fail
491 AutofillFieldExpirationDateTestCase{
492 HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 1, "01/2024", "",
493 false},
494 AutofillFieldExpirationDateTestCase{
495 HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 2, "02/2024", "",
496 false},
497 AutofillFieldExpirationDateTestCase{
498 HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 3, "03/2024", "",
499 false},
500 // A max length of 4 indicates a format of MMYY.
501 AutofillFieldExpirationDateTestCase{
502 HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 4, "02/2024", "0224",
503 true},
504 // A max length of 5 indicates a format of MM/YY.
505 AutofillFieldExpirationDateTestCase{
506 HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 5, "03/2024", "03/24",
507 true},
508 // A max length of 6 indicates a format of MMYYYY.
509 AutofillFieldExpirationDateTestCase{
510 HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 6, "04/2024", "042024",
511 true},
512 // Desired case of proper max length >= 7
513 AutofillFieldExpirationDateTestCase{
514 HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 7, "05/2024",
515 "05/2024", true},
516 AutofillFieldExpirationDateTestCase{
517 HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, 12, "06/2024",
518 "06/2024", true}));
460 519
461 TEST_F(AutofillFieldTest, FillSelectControlByValue) { 520 TEST_F(AutofillFieldTest, FillSelectControlByValue) {
462 std::vector<const char*> kOptions = { 521 std::vector<const char*> kOptions = {
463 "Eenie", "Meenie", "Miney", "Mo", 522 "Eenie", "Meenie", "Miney", "Mo",
464 }; 523 };
465 524
466 AutofillField field; 525 AutofillField field;
467 test::CreateTestSelectField(kOptions, &field); 526 test::CreateTestSelectField(kOptions, &field);
468 527
469 // Set semantically empty contents for each option, so that only the values 528 // Set semantically empty contents for each option, so that only the values
(...skipping 16 matching lines...) Expand all
486 // Set semantically empty values for each option, so that only the contents 545 // Set semantically empty values for each option, so that only the contents
487 // can be used for matching. 546 // can be used for matching.
488 for (size_t i = 0; i < field.option_values.size(); ++i) 547 for (size_t i = 0; i < field.option_values.size(); ++i)
489 field.option_values[i] = base::SizeTToString16(i); 548 field.option_values[i] = base::SizeTToString16(i);
490 549
491 AutofillField::FillFormField( 550 AutofillField::FillFormField(
492 field, ASCIIToUTF16("Miney"), "en-US", "en-US", &field); 551 field, ASCIIToUTF16("Miney"), "en-US", "en-US", &field);
493 EXPECT_EQ(ASCIIToUTF16("2"), field.value); // Corresponds to "Miney". 552 EXPECT_EQ(ASCIIToUTF16("2"), field.value); // Corresponds to "Miney".
494 } 553 }
495 554
496 TEST_F(AutofillFieldTest, FillSelectWithStates) { 555 struct FillSelectTestCase {
497 typedef struct { 556 std::vector<const char*> select_values;
498 std::vector<const char*> select_values; 557 const char* input_value;
499 const char* input_value; 558 const char* expected_value;
500 const char* expected_value; 559 };
501 } TestCase;
502 560
503 TestCase test_cases[] = { 561 class AutofillSelectWithStatesTest
504 // Filling the abbreviation. 562 : public testing::TestWithParam<FillSelectTestCase> {
505 {{"Alabama", "California"}, "CA", "California"}, 563 public:
506 // Attempting to fill the full name in a select full of abbreviations. 564 AutofillSelectWithStatesTest() { CountryNames::SetLocaleString("en-US"); }
507 {{"AL", "CA"}, "California", "CA"}, 565 };
508 // Different case and diacritics.
509 {{"QUÉBEC", "ALBERTA"}, "Quebec", "QUÉBEC"},
510 // Inexact state names.
511 {{"SC - South Carolina", "CA - California", "NC - North Carolina"},
512 "California",
513 "CA - California"},
514 // Don't accidentally match "Virginia" to "West Virginia".
515 {{"WV - West Virginia", "VA - Virginia", "NV - North Virginia"},
516 "Virginia",
517 "VA - Virginia"},
518 // Do accidentally match "Virginia" to "West Virginia".
519 // TODO(crbug.com/624770): This test should not pass, but it does because
520 // "Virginia" is a substring of "West Virginia".
521 {{"WV - West Virginia", "TX - Texas"}, "Virginia", "WV - West Virginia"},
522 // Tests that substring matches work for full state names (a full token
523 // match isn't required). Also tests that matches work for states with
524 // whitespace in the middle.
525 {{"California.", "North Carolina."}, "North Carolina", "North Carolina."},
526 {{"NC - North Carolina", "CA - California"}, "CA", "CA - California"},
527 // These are not states.
528 {{"NCNCA", "SCNCA"}, "NC", ""}};
529 566
530 for (TestCase test_case : test_cases) { 567 TEST_P(AutofillSelectWithStatesTest, FillSelectWithStates) {
531 AutofillField field; 568 auto test_case = GetParam();
532 test::CreateTestSelectField(test_case.select_values, &field); 569 AutofillField field;
533 field.set_heuristic_type(ADDRESS_HOME_STATE); 570 test::CreateTestSelectField(test_case.select_values, &field);
571 field.set_heuristic_type(ADDRESS_HOME_STATE);
534 572
535 AutofillField::FillFormField(field, UTF8ToUTF16(test_case.input_value), 573 AutofillField::FillFormField(field, UTF8ToUTF16(test_case.input_value),
536 "en-US", "en-US", &field); 574 "en-US", "en-US", &field);
537 EXPECT_EQ(UTF8ToUTF16(test_case.expected_value), field.value); 575 EXPECT_EQ(UTF8ToUTF16(test_case.expected_value), field.value);
538 }
539 } 576 }
540 577
541 TEST_F(AutofillFieldTest, FillSelectWithCountries) { 578 INSTANTIATE_TEST_CASE_P(
542 typedef struct { 579 AutofillField,
543 std::vector<const char*> select_values; 580 AutofillSelectWithStatesTest,
544 const char* input_value; 581 testing::Values(
545 const char* expected_value; 582 // Filling the abbreviation.
546 } TestCase; 583 FillSelectTestCase{{"Alabama", "California"}, "CA", "California"},
584 // Attempting to fill the full name in a select full of abbreviations.
585 FillSelectTestCase{{"AL", "CA"}, "California", "CA"},
586 // Different case and diacritics.
587 FillSelectTestCase{{"QUÉBEC", "ALBERTA"}, "Quebec", "QUÉBEC"},
588 // Inexact state names.
589 FillSelectTestCase{
590 {"SC - South Carolina", "CA - California", "NC - North Carolina"},
591 "California",
592 "CA - California"},
593 // Don't accidentally match "Virginia" to "West Virginia".
594 FillSelectTestCase{
595 {"WV - West Virginia", "VA - Virginia", "NV - North Virginia"},
596 "Virginia",
597 "VA - Virginia"},
598 // Do accidentally match "Virginia" to "West Virginia".
599 // TODO(crbug.com/624770): This test should not pass, but it does
600 // because "Virginia" is a substring of "West Virginia".
601 FillSelectTestCase{{"WV - West Virginia", "TX - Texas"},
602 "Virginia",
603 "WV - West Virginia"},
604 // Tests that substring matches work for full state names (a full token
605 // match isn't required). Also tests that matches work for states with
606 // whitespace in the middle.
607 FillSelectTestCase{{"California.", "North Carolina."},
608 "North Carolina",
609 "North Carolina."},
610 FillSelectTestCase{{"NC - North Carolina", "CA - California"},
611 "CA",
612 "CA - California"},
613 // These are not states.
614 FillSelectTestCase{{"NCNCA", "SCNCA"}, "NC", ""}));
547 615
548 TestCase test_cases[] = {// Full country names. 616 class AutofillSelectWithCountriesTest
549 {{"Albania", "Canada"}, "CA", "Canada"}, 617 : public testing::TestWithParam<FillSelectTestCase> {
550 // Abbreviations. 618 public:
551 {{"AL", "CA"}, "Canada", "CA"}}; 619 AutofillSelectWithCountriesTest() { CountryNames::SetLocaleString("en-US"); }
620 };
552 621
553 for (TestCase test_case : test_cases) { 622 TEST_P(AutofillSelectWithCountriesTest, FillSelectWithCountries) {
554 AutofillField field; 623 auto test_case = GetParam();
555 test::CreateTestSelectField(test_case.select_values, &field); 624 AutofillField field;
556 field.set_heuristic_type(ADDRESS_HOME_COUNTRY); 625 test::CreateTestSelectField(test_case.select_values, &field);
626 field.set_heuristic_type(ADDRESS_HOME_COUNTRY);
557 627
558 AutofillField::FillFormField(field, UTF8ToUTF16(test_case.input_value), 628 AutofillField::FillFormField(field, UTF8ToUTF16(test_case.input_value),
559 "en-US", "en-US", &field); 629 "en-US", "en-US", &field);
560 EXPECT_EQ(UTF8ToUTF16(test_case.expected_value), field.value); 630 EXPECT_EQ(UTF8ToUTF16(test_case.expected_value), field.value);
561 }
562 } 631 }
563 632
564 TEST_F(AutofillFieldTest, FillSelectControlWithExpirationMonth) { 633 INSTANTIATE_TEST_CASE_P(
565 typedef struct { 634 AutofillField,
566 std::vector<const char*> select_values; 635 AutofillSelectWithCountriesTest,
567 std::vector<const char*> select_contents; 636 testing::Values(
568 } TestCase; 637 // Full country names.
638 FillSelectTestCase{{"Albania", "Canada"}, "CA", "Canada"},
639 // Abbreviations.
640 FillSelectTestCase{{"AL", "CA"}, "Canada", "CA"}));
569 641
570 TestCase test_cases[] = { 642 struct FillWithExpirationMonthTestCase {
571 // Values start at 1. 643 std::vector<const char*> select_values;
572 {{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}, 644 std::vector<const char*> select_contents;
573 NotNumericMonthsContentsNoPlaceholder()}, 645 };
574 // Values start at 1 but single digits are whitespace padded!
575 {{" 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", "11", "12"},
576 NotNumericMonthsContentsNoPlaceholder()},
577 // Values start at 0.
578 {{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"},
579 NotNumericMonthsContentsNoPlaceholder()},
580 // Values start at 00.
581 {{"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11"},
582 NotNumericMonthsContentsNoPlaceholder()},
583 // The AngularJS framework adds a prefix to number types. Test that it is
584 // removed.
585 {{"number:1", "number:2", "number:3", "number:4", "number:5", "number:6",
586 "number:7", "number:8", "number:9", "number:10", "number:11",
587 "number:12"},
588 NotNumericMonthsContentsNoPlaceholder()},
589 // Values start at 0 and the first content is a placeholder.
590 {{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
591 NotNumericMonthsContentsWithPlaceholder()},
592 // Values start at 1 and the first content is a placeholder.
593 {{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"},
594 NotNumericMonthsContentsWithPlaceholder()},
595 // Values start at 01 and the first content is a placeholder.
596 {{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
597 "13"},
598 NotNumericMonthsContentsWithPlaceholder()},
599 // Values start at 0 after a placeholder.
600 {{"?", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"},
601 NotNumericMonthsContentsWithPlaceholder()},
602 // Values start at 1 after a placeholder.
603 {{"?", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
604 NotNumericMonthsContentsWithPlaceholder()},
605 // Values start at 0 after a negative number.
606 {{"-1", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"},
607 NotNumericMonthsContentsWithPlaceholder()},
608 // Values start at 1 after a negative number.
609 {{"-1", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
610 NotNumericMonthsContentsWithPlaceholder()}};
611 646
612 for (TestCase test_case : test_cases) { 647 class AutofillSelectWithExpirationMonthTest
613 ASSERT_EQ(test_case.select_values.size(), test_case.select_contents.size()); 648 : public testing::TestWithParam<FillWithExpirationMonthTestCase> {
649 public:
650 AutofillSelectWithExpirationMonthTest() {
651 CountryNames::SetLocaleString("en-US");
652 }
653 };
614 654
615 TestFillingExpirationMonth(test_case.select_values, 655 TEST_P(AutofillSelectWithExpirationMonthTest,
616 test_case.select_contents, 656 FillSelectControlWithExpirationMonth) {
617 test_case.select_values.size()); 657 auto test_case = GetParam();
618 } 658 ASSERT_EQ(test_case.select_values.size(), test_case.select_contents.size());
659
660 TestFillingExpirationMonth(test_case.select_values, test_case.select_contents,
661 test_case.select_values.size());
619 } 662 }
620 663
664 INSTANTIATE_TEST_CASE_P(
665 AutofillField,
666 AutofillSelectWithExpirationMonthTest,
667 testing::Values(
668 // Values start at 1.
669 FillWithExpirationMonthTestCase{
670 {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
671 NotNumericMonthsContentsNoPlaceholder()},
672 // Values start at 1 but single digits are whitespace padded!
673 FillWithExpirationMonthTestCase{
674 {" 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", "11",
675 "12"},
676 NotNumericMonthsContentsNoPlaceholder()},
677 // Values start at 0.
678 FillWithExpirationMonthTestCase{
679 {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"},
680 NotNumericMonthsContentsNoPlaceholder()},
681 // Values start at 00.
682 FillWithExpirationMonthTestCase{
683 {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
684 "11"},
685 NotNumericMonthsContentsNoPlaceholder()},
686 // The AngularJS framework adds a prefix to number types. Test that it
687 // is removed.
688 FillWithExpirationMonthTestCase{
689 {"number:1", "number:2", "number:3", "number:4", "number:5",
690 "number:6", "number:7", "number:8", "number:9", "number:10",
691 "number:11", "number:12"},
692 NotNumericMonthsContentsNoPlaceholder()},
693 // Values start at 0 and the first content is a placeholder.
694 FillWithExpirationMonthTestCase{
695 {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
696 "12"},
697 NotNumericMonthsContentsWithPlaceholder()},
698 // Values start at 1 and the first content is a placeholder.
699 FillWithExpirationMonthTestCase{
700 {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
701 "13"},
702 NotNumericMonthsContentsWithPlaceholder()},
703 // Values start at 01 and the first content is a placeholder.
704 FillWithExpirationMonthTestCase{
705 {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11",
706 "12", "13"},
707 NotNumericMonthsContentsWithPlaceholder()},
708 // Values start at 0 after a placeholder.
709 FillWithExpirationMonthTestCase{
710 {"?", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"},
711 NotNumericMonthsContentsWithPlaceholder()},
712 // Values start at 1 after a placeholder.
713 FillWithExpirationMonthTestCase{
714 {"?", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
715 "12"},
716 NotNumericMonthsContentsWithPlaceholder()},
717 // Values start at 0 after a negative number.
718 FillWithExpirationMonthTestCase{
719 {"-1", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
720 "11"},
721 NotNumericMonthsContentsWithPlaceholder()},
722 // Values start at 1 after a negative number.
723 FillWithExpirationMonthTestCase{
724 {"-1", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
725 "12"},
726 NotNumericMonthsContentsWithPlaceholder()}));
727
621 TEST_F(AutofillFieldTest, FillSelectControlWithAbbreviatedMonthName) { 728 TEST_F(AutofillFieldTest, FillSelectControlWithAbbreviatedMonthName) {
622 std::vector<const char*> kMonthsAbbreviated = { 729 std::vector<const char*> kMonthsAbbreviated = {
623 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 730 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
624 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 731 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
625 }; 732 };
626 AutofillField field; 733 AutofillField field;
627 test::CreateTestSelectField(kMonthsAbbreviated, &field); 734 test::CreateTestSelectField(kMonthsAbbreviated, &field);
628 field.set_heuristic_type(CREDIT_CARD_EXP_MONTH); 735 field.set_heuristic_type(CREDIT_CARD_EXP_MONTH);
629 736
630 AutofillField::FillFormField( 737 AutofillField::FillFormField(
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
913 EXPECT_EQ(-1, ret); 1020 EXPECT_EQ(-1, ret);
914 1021
915 // Case 7: No match (not present) 1022 // Case 7: No match (not present)
916 ret = AutofillField::FindShortestSubstringMatchInSelect( 1023 ret = AutofillField::FindShortestSubstringMatchInSelect(
917 ASCIIToUTF16("Canadia"), true, &field); 1024 ASCIIToUTF16("Canadia"), true, &field);
918 EXPECT_EQ(-1, ret); 1025 EXPECT_EQ(-1, ret);
919 } 1026 }
920 1027
921 // Tests that text state fields are filled correctly depending on their 1028 // Tests that text state fields are filled correctly depending on their
922 // maxlength attribute value. 1029 // maxlength attribute value.
923 TEST_F(AutofillFieldTest, FillStateText) { 1030 struct FillStateTextTestCase {
924 typedef struct { 1031 HtmlFieldType field_type;
925 HtmlFieldType field_type; 1032 size_t field_max_length;
926 size_t field_max_length; 1033 std::string value_to_fill;
927 std::string value_to_fill; 1034 std::string expected_value;
928 std::string expected_value; 1035 bool should_fill;
929 bool should_fill; 1036 };
930 } TestCase;
931 1037
932 TestCase test_cases[] = { 1038 class AutofillStateTextTest
933 // Filling a state to a text field with the default maxlength value should 1039 : public testing::TestWithParam<FillStateTextTestCase> {
934 // fill the state value as is. 1040 public:
935 {HTML_TYPE_ADDRESS_LEVEL1, /* default value */ 0, "New York", "New York", 1041 AutofillStateTextTest() { CountryNames::SetLocaleString("en-US"); }
936 true}, 1042 };
937 {HTML_TYPE_ADDRESS_LEVEL1, /* default value */ 0, "NY", "NY", true},
938 // Filling a state to a text field with a maxlength value equal to the
939 // value's length should fill the state value as is.
940 {HTML_TYPE_ADDRESS_LEVEL1, 8, "New York", "New York", true},
941 // Filling a state to a text field with a maxlength value lower than the
942 // value's length but higher than the value's abbreviation should fill the
943 // state abbreviation.
944 {HTML_TYPE_ADDRESS_LEVEL1, 2, "New York", "NY", true},
945 {HTML_TYPE_ADDRESS_LEVEL1, 2, "NY", "NY", true},
946 // Filling a state to a text field with a maxlength value lower than the
947 // value's length and the value's abbreviation should not fill at all.
948 {HTML_TYPE_ADDRESS_LEVEL1, 1, "New York", "", false},
949 {HTML_TYPE_ADDRESS_LEVEL1, 1, "NY", "", false},
950 // Filling a state to a text field with a maxlength value lower than the
951 // value's length and that has no associated abbreviation should not fill
952 // at all.
953 {HTML_TYPE_ADDRESS_LEVEL1, 3, "Quebec", "", false}};
954 1043
955 for (const TestCase& test_case : test_cases) { 1044 TEST_P(AutofillStateTextTest, FillStateText) {
956 AutofillField field; 1045 auto test_case = GetParam();
957 field.SetHtmlType(test_case.field_type, HtmlFieldMode()); 1046 AutofillField field;
958 field.max_length = test_case.field_max_length; 1047 field.SetHtmlType(test_case.field_type, HtmlFieldMode());
1048 field.max_length = test_case.field_max_length;
959 1049
960 bool has_filled = AutofillField::FillFormField( 1050 bool has_filled = AutofillField::FillFormField(
961 field, ASCIIToUTF16(test_case.value_to_fill), "en-US", "en-US", &field); 1051 field, ASCIIToUTF16(test_case.value_to_fill), "en-US", "en-US", &field);
962 1052
963 EXPECT_EQ(test_case.should_fill, has_filled); 1053 EXPECT_EQ(test_case.should_fill, has_filled);
964 EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value); 1054 EXPECT_EQ(ASCIIToUTF16(test_case.expected_value), field.value);
965 }
966 } 1055 }
967 1056
1057 INSTANTIATE_TEST_CASE_P(
1058 AutofillField,
1059 AutofillStateTextTest,
1060 testing::Values(
1061 // Filling a state to a text field with the default maxlength value
1062 // should
1063 // fill the state value as is.
1064 FillStateTextTestCase{HTML_TYPE_ADDRESS_LEVEL1, /* default value */ 0,
1065 "New York", "New York", true},
1066 FillStateTextTestCase{HTML_TYPE_ADDRESS_LEVEL1, /* default value */ 0,
1067 "NY", "NY", true},
1068 // Filling a state to a text field with a maxlength value equal to the
1069 // value's length should fill the state value as is.
1070 FillStateTextTestCase{HTML_TYPE_ADDRESS_LEVEL1, 8, "New York",
1071 "New York", true},
1072 // Filling a state to a text field with a maxlength value lower than the
1073 // value's length but higher than the value's abbreviation should fill
1074 // the state abbreviation.
1075 FillStateTextTestCase{HTML_TYPE_ADDRESS_LEVEL1, 2, "New York", "NY",
1076 true},
1077 FillStateTextTestCase{HTML_TYPE_ADDRESS_LEVEL1, 2, "NY", "NY", true},
1078 // Filling a state to a text field with a maxlength value lower than the
1079 // value's length and the value's abbreviation should not fill at all.
1080 FillStateTextTestCase{HTML_TYPE_ADDRESS_LEVEL1, 1, "New York", "",
1081 false},
1082 FillStateTextTestCase{HTML_TYPE_ADDRESS_LEVEL1, 1, "NY", "", false},
1083 // Filling a state to a text field with a maxlength value lower than the
1084 // value's length and that has no associated abbreviation should not
1085 // fill at all.
1086 FillStateTextTestCase{HTML_TYPE_ADDRESS_LEVEL1, 3, "Quebec", "",
1087 false}));
1088
968 } // namespace 1089 } // namespace
969 } // namespace autofill 1090 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698