Chromium Code Reviews| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/guid.h" | 7 #include "base/guid.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/time/time.h" | |
| 11 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 12 #include "components/autofill/core/browser/autofill_test_utils.h" | 13 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 13 #include "components/autofill/core/browser/autofill_type.h" | 14 #include "components/autofill/core/browser/autofill_type.h" |
| 14 #include "components/autofill/core/browser/credit_card.h" | 15 #include "components/autofill/core/browser/credit_card.h" |
| 15 #include "components/autofill/core/browser/validation.h" | 16 #include "components/autofill/core/browser/validation.h" |
| 16 #include "components/autofill/core/common/form_field_data.h" | 17 #include "components/autofill/core/common/form_field_data.h" |
| 17 #include "grit/components_scaled_resources.h" | 18 #include "grit/components_scaled_resources.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 using base::ASCIIToUTF16; | 21 using base::ASCIIToUTF16; |
| (...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 725 TEST(CreditCardTest, CanBuildFromCardNumberAndExpirationDate) { | 726 TEST(CreditCardTest, CanBuildFromCardNumberAndExpirationDate) { |
| 726 base::string16 card_number = base::ASCIIToUTF16("test"); | 727 base::string16 card_number = base::ASCIIToUTF16("test"); |
| 727 int month = 1; | 728 int month = 1; |
| 728 int year = 3000; | 729 int year = 3000; |
| 729 CreditCard card(card_number, month, year); | 730 CreditCard card(card_number, month, year); |
| 730 EXPECT_EQ(card_number, card.number()); | 731 EXPECT_EQ(card_number, card.number()); |
| 731 EXPECT_EQ(month, card.expiration_month()); | 732 EXPECT_EQ(month, card.expiration_month()); |
| 732 EXPECT_EQ(year, card.expiration_year()); | 733 EXPECT_EQ(year, card.expiration_year()); |
| 733 } | 734 } |
| 734 | 735 |
| 736 // A test case for credit card expiration year and month. | |
| 737 class ExpTestCase { | |
|
Mathieu
2016/04/27 19:54:50
would a struct simplify things here? possibly less
please use gerrit instead
2016/04/28 00:30:24
Done.
| |
| 738 public: | |
| 739 ExpTestCase(const base::Time& now, | |
| 740 bool update, | |
| 741 int month, | |
| 742 int year, | |
| 743 CreditCard::RecordType type) | |
| 744 : current_time_(now), | |
| 745 should_update_expiration_(update), | |
| 746 month_(month), | |
| 747 year_(year), | |
| 748 record_type_(type), | |
| 749 server_status_(CreditCard::OK) {} | |
| 750 | |
| 751 ExpTestCase(const base::Time& now, | |
| 752 bool update, | |
| 753 int month, | |
| 754 int year, | |
| 755 CreditCard::RecordType type, | |
| 756 CreditCard::ServerStatus status) | |
| 757 : current_time_(now), | |
| 758 should_update_expiration_(update), | |
| 759 month_(month), | |
| 760 year_(year), | |
| 761 record_type_(type), | |
| 762 server_status_(status) {} | |
| 763 | |
| 764 ~ExpTestCase() {} | |
| 765 | |
| 766 const base::Time& current_time() const { return current_time_; } | |
| 767 bool should_update_expiration() const { return should_update_expiration_; } | |
| 768 int month() const { return month_; } | |
| 769 int year() const { return year_; } | |
| 770 CreditCard::RecordType record_type() const { return record_type_; } | |
| 771 CreditCard::ServerStatus server_status() const { return server_status_; } | |
| 772 | |
| 773 private: | |
| 774 base::Time current_time_; | |
| 775 bool should_update_expiration_; | |
| 776 int month_; | |
| 777 int year_; | |
| 778 CreditCard::RecordType record_type_; | |
| 779 CreditCard::ServerStatus server_status_; | |
| 780 }; | |
| 781 | |
| 782 // A parameterized test fixture for credit card expiration. | |
| 783 class CreditCardExpTest : public testing::TestWithParam<ExpTestCase> { | |
| 784 public: | |
| 785 static std::vector<ExpTestCase> GenerateTestCases() { | |
| 786 std::vector<ExpTestCase> cases; | |
| 787 | |
| 788 base::Time now = base::Time::Now(); | |
| 789 base::Time::Exploded last_year; | |
| 790 (now - base::TimeDelta::FromDays(365)).LocalExplode(&last_year); | |
| 791 | |
| 792 // Cards that expired last year should always be updated. | |
| 793 cases.emplace_back(now, true, last_year.month, last_year.year, | |
| 794 CreditCard::LOCAL_CARD); | |
| 795 cases.emplace_back(now, true, last_year.month, last_year.year, | |
| 796 CreditCard::FULL_SERVER_CARD, CreditCard::OK); | |
| 797 cases.emplace_back(now, true, last_year.month, last_year.year, | |
| 798 CreditCard::MASKED_SERVER_CARD, CreditCard::OK); | |
| 799 cases.emplace_back(now, true, last_year.month, last_year.year, | |
| 800 CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED); | |
| 801 cases.emplace_back(now, true, last_year.month, last_year.year, | |
| 802 CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED); | |
| 803 | |
| 804 base::Time::Exploded last_month; | |
| 805 (now - base::TimeDelta::FromDays(31)).LocalExplode(&last_month); | |
| 806 | |
| 807 // Cards that expired last month should always be updated. | |
| 808 cases.emplace_back(now, true, last_month.month, last_month.year, | |
| 809 CreditCard::LOCAL_CARD); | |
| 810 cases.emplace_back(now, true, last_month.month, last_month.year, | |
| 811 CreditCard::FULL_SERVER_CARD, CreditCard::OK); | |
| 812 cases.emplace_back(now, true, last_month.month, last_month.year, | |
| 813 CreditCard::MASKED_SERVER_CARD, CreditCard::OK); | |
| 814 cases.emplace_back(now, true, last_month.month, last_month.year, | |
| 815 CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED); | |
| 816 cases.emplace_back(now, true, last_month.month, last_month.year, | |
| 817 CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED); | |
| 818 | |
| 819 base::Time::Exploded current; | |
| 820 now.LocalExplode(¤t); | |
| 821 | |
| 822 // Cards that expire this month should be updated only if the server status | |
| 823 // is EXPIRED. | |
| 824 cases.emplace_back(now, false, current.month, current.year, | |
| 825 CreditCard::LOCAL_CARD); | |
| 826 cases.emplace_back(now, false, current.month, current.year, | |
| 827 CreditCard::FULL_SERVER_CARD, CreditCard::OK); | |
| 828 cases.emplace_back(now, false, current.month, current.year, | |
| 829 CreditCard::MASKED_SERVER_CARD, CreditCard::OK); | |
| 830 cases.emplace_back(now, true, current.month, current.year, | |
| 831 CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED); | |
| 832 cases.emplace_back(now, true, current.month, current.year, | |
| 833 CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED); | |
| 834 | |
| 835 base::Time::Exploded next_month; | |
| 836 (now + base::TimeDelta::FromDays(31)).LocalExplode(&next_month); | |
| 837 | |
| 838 // Cards that expire next month should be updated only if the server status | |
| 839 // is EXPIRED. | |
| 840 cases.emplace_back(now, false, next_month.month, next_month.year, | |
| 841 CreditCard::LOCAL_CARD); | |
| 842 cases.emplace_back(now, false, next_month.month, next_month.year, | |
| 843 CreditCard::MASKED_SERVER_CARD, CreditCard::OK); | |
| 844 cases.emplace_back(now, false, next_month.month, next_month.year, | |
| 845 CreditCard::FULL_SERVER_CARD, CreditCard::OK); | |
| 846 cases.emplace_back(now, true, next_month.month, next_month.year, | |
| 847 CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED); | |
| 848 cases.emplace_back(now, true, next_month.month, next_month.year, | |
| 849 CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED); | |
| 850 | |
| 851 base::Time::Exploded next_year; | |
| 852 (now + base::TimeDelta::FromDays(365)).LocalExplode(&next_year); | |
| 853 | |
| 854 // Cards that expire next year should be updated only if the server status | |
| 855 // is EXPIRED. | |
| 856 cases.emplace_back(now, false, next_year.month, next_year.year, | |
| 857 CreditCard::LOCAL_CARD); | |
| 858 cases.emplace_back(now, false, next_year.month, next_year.year, | |
| 859 CreditCard::MASKED_SERVER_CARD, CreditCard::OK); | |
| 860 cases.emplace_back(now, false, next_year.month, next_year.year, | |
| 861 CreditCard::FULL_SERVER_CARD, CreditCard::OK); | |
| 862 cases.emplace_back(now, true, next_year.month, next_year.year, | |
| 863 CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED); | |
| 864 cases.emplace_back(now, true, next_year.month, next_year.year, | |
| 865 CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED); | |
| 866 | |
| 867 return cases; | |
| 868 } | |
| 869 }; | |
| 870 | |
| 871 // Verifies that a credit card should be updated. | |
| 872 TEST_P(CreditCardExpTest, ShouldUpdateExpiration) { | |
| 873 CreditCard card(base::ASCIIToUTF16("1234"), GetParam().month(), | |
| 874 GetParam().year()); | |
| 875 card.set_record_type(GetParam().record_type()); | |
| 876 if (card.record_type() != CreditCard::LOCAL_CARD) | |
| 877 card.SetServerStatus(GetParam().server_status()); | |
| 878 | |
| 879 EXPECT_EQ(GetParam().should_update_expiration(), | |
| 880 card.ShouldUpdateExpiration(GetParam().current_time())); | |
| 881 } | |
| 882 | |
| 883 INSTANTIATE_TEST_CASE_P( | |
| 884 Expiration, | |
| 885 CreditCardExpTest, | |
| 886 testing::ValuesIn(CreditCardExpTest::GenerateTestCases())); | |
| 887 | |
| 735 } // namespace autofill | 888 } // namespace autofill |
| OLD | NEW |