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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 }; | 42 }; |
43 | 43 |
44 // Returns the offset to be set within the credit card number field. | 44 // Returns the offset to be set within the credit card number field. |
45 size_t GetNumberOffset(size_t index, const TestCase& test) { | 45 size_t GetNumberOffset(size_t index, const TestCase& test) { |
46 size_t result = 0; | 46 size_t result = 0; |
47 for (size_t i = 0; i < index; ++i) | 47 for (size_t i = 0; i < index; ++i) |
48 result += test.splits_[i]; | 48 result += test.splits_[i]; |
49 return result; | 49 return result; |
50 } | 50 } |
51 | 51 |
| 52 // Helper function to test |GetCreditCardNumberValue()|. |
| 53 void TestGetCreditCardNumberValue(const TestCase& test) { |
| 54 size_t offset = 0; |
| 55 for (size_t i = 0; i < test.total_spilts_; ++i) { |
| 56 AutofillField cc_number_part; |
| 57 cc_number_part.set_heuristic_type(CREDIT_CARD_NUMBER); |
| 58 cc_number_part.max_length = test.splits_[i]; |
| 59 offset += (i == 0) ? 0 : test.splits_[i - 1]; |
| 60 cc_number_part.set_credit_card_number_offset(offset); |
| 61 |
| 62 // Retrive the value for |cc_number_part|. |
| 63 base::string16 actual = AutofillField::GetCreditCardNumberValue( |
| 64 cc_number_part, ASCIIToUTF16(test.card_number_)); |
| 65 |
| 66 // Enforce the field's |max_length| constraint. Please note that, normally |
| 67 // this would be handled on renderer, which is why it needs to be specially |
| 68 // handled in the unit test. |
| 69 actual = (offset < test.card_number_.length()) |
| 70 ? actual.substr(0, test.splits_[i]) |
| 71 : base::string16(); |
| 72 |
| 73 // Verify for expected results. |
| 74 EXPECT_EQ(ASCIIToUTF16(test.expected_results_[i]), actual); |
| 75 } |
| 76 } |
| 77 |
52 TEST(AutofillFieldTest, Type) { | 78 TEST(AutofillFieldTest, Type) { |
53 AutofillField field; | 79 AutofillField field; |
54 ASSERT_EQ(NO_SERVER_DATA, field.server_type()); | 80 ASSERT_EQ(NO_SERVER_DATA, field.server_type()); |
55 ASSERT_EQ(UNKNOWN_TYPE, field.heuristic_type()); | 81 ASSERT_EQ(UNKNOWN_TYPE, field.heuristic_type()); |
56 | 82 |
57 // |server_type_| is NO_SERVER_DATA, so |heuristic_type_| is returned. | 83 // |server_type_| is NO_SERVER_DATA, so |heuristic_type_| is returned. |
58 EXPECT_EQ(UNKNOWN_TYPE, field.Type().GetStorableType()); | 84 EXPECT_EQ(UNKNOWN_TYPE, field.Type().GetStorableType()); |
59 | 85 |
60 // Set the heuristic type and check it. | 86 // Set the heuristic type and check it. |
61 field.set_heuristic_type(NAME_FIRST); | 87 field.set_heuristic_type(NAME_FIRST); |
(...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 AutofillField::FillFormField(cc_number_full, | 641 AutofillField::FillFormField(cc_number_full, |
616 ASCIIToUTF16(test.card_number_), | 642 ASCIIToUTF16(test.card_number_), |
617 "en-US", | 643 "en-US", |
618 "en-US", | 644 "en-US", |
619 &cc_number_full); | 645 &cc_number_full); |
620 | 646 |
621 // Verify for expected results. | 647 // Verify for expected results. |
622 EXPECT_EQ(ASCIIToUTF16(test.card_number_), cc_number_full.value); | 648 EXPECT_EQ(ASCIIToUTF16(test.card_number_), cc_number_full.value); |
623 } | 649 } |
624 | 650 |
| 651 TEST(AutofillFieldTest, GetCreditCardNumberValue_OffsetsLessThanDigits) { |
| 652 TestCase test; |
| 653 test.card_number_ = "5187654321098765"; |
| 654 |
| 655 // Offset for all card number input fields are less than the size of digits on |
| 656 // the card. |
| 657 size_t max_lengths[] = {4, 4, 4, 4}; |
| 658 std::string expectations[] = {"5187", "6543", "2109", "8765"}; |
| 659 test.total_spilts_ = arraysize(max_lengths); |
| 660 test.splits_ = |
| 661 std::vector<int>(max_lengths, max_lengths + arraysize(max_lengths)); |
| 662 test.expected_results_ = std::vector<std::string>( |
| 663 expectations, expectations + arraysize(expectations)); |
| 664 |
| 665 // Verify against the expectations. |
| 666 TestGetCreditCardNumberValue(test); |
| 667 } |
| 668 |
| 669 TEST(AutofillFieldTest, GetCreditCardNumberValue_OffsetsEqualsDigits) { |
| 670 TestCase test; |
| 671 test.card_number_ = "5187654321098765"; |
| 672 |
| 673 // Offset for second card number input field is equals to the size of digits |
| 674 // on the card. |
| 675 size_t max_lengths[] = {16, 4}; |
| 676 std::string expectations[] = {"5187654321098765", std::string()}; |
| 677 test.total_spilts_ = arraysize(max_lengths); |
| 678 test.splits_ = |
| 679 std::vector<int>(max_lengths, max_lengths + arraysize(max_lengths)); |
| 680 test.expected_results_ = std::vector<std::string>( |
| 681 expectations, expectations + arraysize(expectations)); |
| 682 |
| 683 // Verify against the expectations. |
| 684 TestGetCreditCardNumberValue(test); |
| 685 } |
| 686 |
| 687 TEST(AutofillFieldTest, GetCreditCardNumberValue_OffsetsGreaterThanDigits) { |
| 688 TestCase test; |
| 689 test.card_number_ = "5187654321098765"; |
| 690 |
| 691 // Offset for second card number input field is greater than the size of |
| 692 // digits on the card. |
| 693 size_t max_lengths[] = {20, 16}; |
| 694 std::string expectations[] = {"5187654321098765", std::string()}; |
| 695 test.total_spilts_ = arraysize(max_lengths); |
| 696 test.splits_ = |
| 697 std::vector<int>(max_lengths, max_lengths + arraysize(max_lengths)); |
| 698 test.expected_results_ = std::vector<std::string>( |
| 699 expectations, expectations + arraysize(expectations)); |
| 700 |
| 701 // Verify against the expectations. |
| 702 TestGetCreditCardNumberValue(test); |
| 703 } |
| 704 |
625 } // namespace | 705 } // namespace |
626 } // namespace autofill | 706 } // namespace autofill |
OLD | NEW |