OLD | NEW |
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 "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 options16[i] = ASCIIToUTF16(options[i]); | 27 options16[i] = ASCIIToUTF16(options[i]); |
28 } | 28 } |
29 | 29 |
30 FormFieldData form_field; | 30 FormFieldData form_field; |
31 form_field.form_control_type = "select-one"; | 31 form_field.form_control_type = "select-one"; |
32 form_field.option_values = options16; | 32 form_field.option_values = options16; |
33 form_field.option_contents = options16; | 33 form_field.option_contents = options16; |
34 return form_field; | 34 return form_field; |
35 } | 35 } |
36 | 36 |
| 37 struct TestCase { |
| 38 std::string card_number_; |
| 39 size_t total_spilts_; |
| 40 std::vector<int> splits_; |
| 41 std::vector<std::string> expected_results_; |
| 42 }; |
| 43 |
| 44 // Returns the offset to be set within the credit card number field. |
| 45 size_t GetNumberOffset(size_t index, const TestCase& test) { |
| 46 size_t result = 0; |
| 47 for (size_t i = 0; i < index; ++i) |
| 48 result += test.splits_[i]; |
| 49 return result; |
| 50 } |
| 51 |
37 TEST(AutofillFieldTest, Type) { | 52 TEST(AutofillFieldTest, Type) { |
38 AutofillField field; | 53 AutofillField field; |
39 ASSERT_EQ(NO_SERVER_DATA, field.server_type()); | 54 ASSERT_EQ(NO_SERVER_DATA, field.server_type()); |
40 ASSERT_EQ(UNKNOWN_TYPE, field.heuristic_type()); | 55 ASSERT_EQ(UNKNOWN_TYPE, field.heuristic_type()); |
41 | 56 |
42 // |server_type_| is NO_SERVER_DATA, so |heuristic_type_| is returned. | 57 // |server_type_| is NO_SERVER_DATA, so |heuristic_type_| is returned. |
43 EXPECT_EQ(UNKNOWN_TYPE, field.Type().GetStorableType()); | 58 EXPECT_EQ(UNKNOWN_TYPE, field.Type().GetStorableType()); |
44 | 59 |
45 // Set the heuristic type and check it. | 60 // Set the heuristic type and check it. |
46 field.set_heuristic_type(NAME_FIRST); | 61 field.set_heuristic_type(NAME_FIRST); |
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 | 511 |
497 AutofillField::FillFormField(field, | 512 AutofillField::FillFormField(field, |
498 UTF8ToUTF16("桜丘町26-1\n" | 513 UTF8ToUTF16("桜丘町26-1\n" |
499 "セルリアンタワー6階"), | 514 "セルリアンタワー6階"), |
500 "ja-JP", | 515 "ja-JP", |
501 "en-US", | 516 "en-US", |
502 &field); | 517 &field); |
503 EXPECT_EQ(UTF8ToUTF16("桜丘町26-1セルリアンタワー6階"), field.value); | 518 EXPECT_EQ(UTF8ToUTF16("桜丘町26-1セルリアンタワー6階"), field.value); |
504 } | 519 } |
505 | 520 |
| 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"), |
| 527 "en-US", |
| 528 "en-US", |
| 529 &cc_number_full); |
| 530 |
| 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()); |
| 534 } |
| 535 |
| 536 TEST(AutofillFieldTest, FillCreditCardNumberWithEqualSizeSplits) { |
| 537 // Case 2: card number broken up into four equal groups, of length 4. |
| 538 TestCase test; |
| 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)); |
| 546 |
| 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); |
| 552 |
| 553 // Fill with a card-number; should fill just the card_number_part. |
| 554 AutofillField::FillFormField(cc_number_part, |
| 555 ASCIIToUTF16(test.card_number_), |
| 556 "en-US", |
| 557 "en-US", |
| 558 &cc_number_part); |
| 559 |
| 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()); |
| 564 } |
| 565 |
| 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_), |
| 571 "en-US", |
| 572 "en-US", |
| 573 &cc_number_full); |
| 574 |
| 575 // Verify for expected results. |
| 576 EXPECT_EQ(ASCIIToUTF16(test.card_number_), cc_number_full.value); |
| 577 } |
| 578 |
| 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. |
| 582 TestCase test; |
| 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)); |
| 590 |
| 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)); |
| 597 |
| 598 // Fill with a card-number; should fill just the card_number_part. |
| 599 AutofillField::FillFormField(cc_number_part, |
| 600 ASCIIToUTF16(test.card_number_), |
| 601 "en-US", |
| 602 "en-US", |
| 603 &cc_number_part); |
| 604 |
| 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()); |
| 610 } |
| 611 |
| 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_), |
| 617 "en-US", |
| 618 "en-US", |
| 619 &cc_number_full); |
| 620 |
| 621 // Verify for expected results. |
| 622 EXPECT_EQ(ASCIIToUTF16(test.card_number_), cc_number_full.value); |
| 623 } |
| 624 |
506 } // namespace | 625 } // namespace |
507 } // namespace autofill | 626 } // namespace autofill |
OLD | NEW |