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, TestCase test) { | |
Ilya Sherman
2014/09/03 01:07:51
nit: Please pass by const reference.
Pritam Nikam
2014/09/03 12:21:50
Done.
| |
46 size_t ret = 0; | |
Ilya Sherman
2014/09/03 01:07:51
nit: "ret" -> "result"
Pritam Nikam
2014/09/03 12:21:50
Done.
| |
47 for (size_t i = 0; i < index; ++i) | |
48 ret += test.splits_[i]; | |
49 return ret; | |
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[] = {"5187654321098765", "654321098765", "21098765", | |
544 "8765"}; | |
545 test.expected_results_ = | |
546 std::vector<std::string>(results, results + arraysize(results)); | |
547 | |
548 for (size_t i = 0; i < test.total_spilts_; ++i) { | |
549 AutofillField cc_number_part; | |
550 cc_number_part.set_heuristic_type(CREDIT_CARD_NUMBER); | |
551 cc_number_part.max_length = test.splits_[i]; | |
552 cc_number_part.set_credit_card_number_offset(4 * i); | |
553 | |
554 // Fill with a card-number; should fill just the card_number_part. | |
555 AutofillField::FillFormField(cc_number_part, | |
556 ASCIIToUTF16(test.card_number_), | |
557 "en-US", | |
558 "en-US", | |
559 &cc_number_part); | |
560 | |
561 // Verify for expected results. | |
562 EXPECT_EQ(ASCIIToUTF16(test.expected_results_[i]), cc_number_part.value); | |
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[] = {"423456789012345", "56789012345", "12345"}; | |
Ilya Sherman
2014/09/03 01:07:51
These values do not make sense to me. Shouldn't t
Pritam Nikam
2014/09/03 12:21:50
Done.
We leave the field will enforce max_length
Ilya Sherman
2014/09/03 23:25:26
Have you verified that the max_length is enforced
| |
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]), cc_number_part.value); | |
607 EXPECT_EQ(GetNumberOffset(i, test), | |
608 cc_number_part.credit_card_number_offset()); | |
609 } | |
610 | |
611 // Verify that full card-number shall get fill properly as well. | |
612 AutofillField cc_number_full; | |
613 cc_number_full.set_heuristic_type(CREDIT_CARD_NUMBER); | |
614 AutofillField::FillFormField(cc_number_full, | |
615 ASCIIToUTF16(test.card_number_), | |
616 "en-US", | |
617 "en-US", | |
618 &cc_number_full); | |
619 | |
620 // Verify for expected results. | |
621 EXPECT_EQ(ASCIIToUTF16(test.card_number_), cc_number_full.value); | |
622 } | |
623 | |
506 } // namespace | 624 } // namespace |
507 } // namespace autofill | 625 } // namespace autofill |
OLD | NEW |