Index: components/autofill/core/browser/autofill_field_unittest.cc |
diff --git a/components/autofill/core/browser/autofill_field_unittest.cc b/components/autofill/core/browser/autofill_field_unittest.cc |
index fa10822aa1c0210ac82fb724c0bd9faad825953d..d95af5fd67728df4ec7c45e33a05d2a2cf4db6e0 100644 |
--- a/components/autofill/core/browser/autofill_field_unittest.cc |
+++ b/components/autofill/core/browser/autofill_field_unittest.cc |
@@ -49,6 +49,32 @@ size_t GetNumberOffset(size_t index, const TestCase& test) { |
return result; |
} |
+// Helper function to test |GetCreditCardNumberValue()|. |
+void TestGetCreditCardNumberValue(const TestCase& test) { |
+ size_t offset = 0; |
+ 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]; |
+ offset += (i == 0) ? 0 : test.splits_[i - 1]; |
+ cc_number_part.set_credit_card_number_offset(offset); |
+ |
+ // Retrive the value for |cc_number_part|. |
+ base::string16 actual = AutofillField::GetCreditCardNumberValue( |
+ cc_number_part, ASCIIToUTF16(test.card_number_)); |
+ |
+ // Enforce the field's |max_length| constraint. Please note that, normally |
+ // this would be handled on renderer, which is why it needs to be specially |
+ // handled in the unit test. |
+ actual = (offset < test.card_number_.length()) |
+ ? actual.substr(0, test.splits_[i]) |
+ : base::string16(); |
+ |
+ // Verify for expected results. |
+ EXPECT_EQ(ASCIIToUTF16(test.expected_results_[i]), actual); |
+ } |
+} |
+ |
TEST(AutofillFieldTest, Type) { |
AutofillField field; |
ASSERT_EQ(NO_SERVER_DATA, field.server_type()); |
@@ -622,5 +648,59 @@ TEST(AutofillFieldTest, FillCreditCardNumberWithUnequalSizeSplits) { |
EXPECT_EQ(ASCIIToUTF16(test.card_number_), cc_number_full.value); |
} |
+TEST(AutofillFieldTest, GetCreditCardNumberValue_OffsetsLessThanDigits) { |
+ TestCase test; |
+ test.card_number_ = "5187654321098765"; |
+ |
+ // Offset for all card number input fields are less than the size of digits on |
+ // the card. |
+ size_t max_lengths[] = {4, 4, 4, 4}; |
+ std::string expectations[] = {"5187", "6543", "2109", "8765"}; |
+ test.total_spilts_ = arraysize(max_lengths); |
+ test.splits_ = |
+ std::vector<int>(max_lengths, max_lengths + arraysize(max_lengths)); |
+ test.expected_results_ = std::vector<std::string>( |
+ expectations, expectations + arraysize(expectations)); |
+ |
+ // Verify against the expectations. |
+ TestGetCreditCardNumberValue(test); |
+} |
+ |
+TEST(AutofillFieldTest, GetCreditCardNumberValue_OffsetsEqualsDigits) { |
+ TestCase test; |
+ test.card_number_ = "5187654321098765"; |
+ |
+ // Offset for second card number input field is equals to the size of digits |
+ // on the card. |
+ size_t max_lengths[] = {16, 4}; |
+ std::string expectations[] = {"5187654321098765", std::string()}; |
+ test.total_spilts_ = arraysize(max_lengths); |
+ test.splits_ = |
+ std::vector<int>(max_lengths, max_lengths + arraysize(max_lengths)); |
+ test.expected_results_ = std::vector<std::string>( |
+ expectations, expectations + arraysize(expectations)); |
+ |
+ // Verify against the expectations. |
+ TestGetCreditCardNumberValue(test); |
+} |
+ |
+TEST(AutofillFieldTest, GetCreditCardNumberValue_OffsetsGreaterThanDigits) { |
+ TestCase test; |
+ test.card_number_ = "5187654321098765"; |
+ |
+ // Offset for second card number input field is greater than the size of |
+ // digits on the card. |
+ size_t max_lengths[] = {20, 16}; |
+ std::string expectations[] = {"5187654321098765", std::string()}; |
+ test.total_spilts_ = arraysize(max_lengths); |
+ test.splits_ = |
+ std::vector<int>(max_lengths, max_lengths + arraysize(max_lengths)); |
+ test.expected_results_ = std::vector<std::string>( |
+ expectations, expectations + arraysize(expectations)); |
+ |
+ // Verify against the expectations. |
+ TestGetCreditCardNumberValue(test); |
+} |
+ |
} // namespace |
} // namespace autofill |