| 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 // Verifies that a credit card should be updated. |
| 737 TEST(CreditCardTest, ShouldUpdateExpiration) { |
| 738 base::Time now = base::Time::Now(); |
| 739 |
| 740 base::Time::Exploded last_year; |
| 741 (now - base::TimeDelta::FromDays(365)).LocalExplode(&last_year); |
| 742 |
| 743 base::Time::Exploded last_month; |
| 744 (now - base::TimeDelta::FromDays(31)).LocalExplode(&last_month); |
| 745 |
| 746 base::Time::Exploded current; |
| 747 now.LocalExplode(¤t); |
| 748 |
| 749 base::Time::Exploded next_month; |
| 750 (now + base::TimeDelta::FromDays(31)).LocalExplode(&next_month); |
| 751 |
| 752 base::Time::Exploded next_year; |
| 753 (now + base::TimeDelta::FromDays(365)).LocalExplode(&next_year); |
| 754 |
| 755 static const struct { |
| 756 bool should_update_expiration; |
| 757 int month; |
| 758 int year; |
| 759 CreditCard::RecordType record_type; |
| 760 CreditCard::ServerStatus server_status; |
| 761 } kTestCases[] = { |
| 762 |
| 763 // Cards that expired last year should always be updated. |
| 764 {true, last_year.month, last_year.year, CreditCard::LOCAL_CARD}, |
| 765 {true, last_year.month, last_year.year, CreditCard::FULL_SERVER_CARD, |
| 766 CreditCard::OK}, |
| 767 {true, last_year.month, last_year.year, CreditCard::MASKED_SERVER_CARD, |
| 768 CreditCard::OK}, |
| 769 {true, last_year.month, last_year.year, CreditCard::FULL_SERVER_CARD, |
| 770 CreditCard::EXPIRED}, |
| 771 {true, last_year.month, last_year.year, CreditCard::MASKED_SERVER_CARD, |
| 772 CreditCard::EXPIRED}, |
| 773 |
| 774 // Cards that expired last month should always be updated. |
| 775 {true, last_month.month, last_month.year, CreditCard::LOCAL_CARD}, |
| 776 {true, last_month.month, last_month.year, CreditCard::FULL_SERVER_CARD, |
| 777 CreditCard::OK}, |
| 778 {true, last_month.month, last_month.year, CreditCard::MASKED_SERVER_CARD, |
| 779 CreditCard::OK}, |
| 780 {true, last_month.month, last_month.year, CreditCard::FULL_SERVER_CARD, |
| 781 CreditCard::EXPIRED}, |
| 782 {true, last_month.month, last_month.year, CreditCard::MASKED_SERVER_CARD, |
| 783 CreditCard::EXPIRED}, |
| 784 |
| 785 // Cards that expire this month should be updated only if the server |
| 786 // status is EXPIRED. |
| 787 {false, current.month, current.year, CreditCard::LOCAL_CARD}, |
| 788 {false, current.month, current.year, CreditCard::FULL_SERVER_CARD, |
| 789 CreditCard::OK}, |
| 790 {false, current.month, current.year, CreditCard::MASKED_SERVER_CARD, |
| 791 CreditCard::OK}, |
| 792 {true, current.month, current.year, CreditCard::FULL_SERVER_CARD, |
| 793 CreditCard::EXPIRED}, |
| 794 {true, current.month, current.year, CreditCard::MASKED_SERVER_CARD, |
| 795 CreditCard::EXPIRED}, |
| 796 |
| 797 // Cards that expire next month should be updated only if the server |
| 798 // status is EXPIRED. |
| 799 {false, next_month.month, next_month.year, CreditCard::LOCAL_CARD}, |
| 800 {false, next_month.month, next_month.year, CreditCard::MASKED_SERVER_CARD, |
| 801 CreditCard::OK}, |
| 802 {false, next_month.month, next_month.year, CreditCard::FULL_SERVER_CARD, |
| 803 CreditCard::OK}, |
| 804 {true, next_month.month, next_month.year, CreditCard::MASKED_SERVER_CARD, |
| 805 CreditCard::EXPIRED}, |
| 806 {true, next_month.month, next_month.year, CreditCard::FULL_SERVER_CARD, |
| 807 CreditCard::EXPIRED}, |
| 808 |
| 809 // Cards that expire next year should be updated only if the server status |
| 810 // is EXPIRED. |
| 811 {false, next_year.month, next_year.year, CreditCard::LOCAL_CARD}, |
| 812 {false, next_year.month, next_year.year, CreditCard::MASKED_SERVER_CARD, |
| 813 CreditCard::OK}, |
| 814 {false, next_year.month, next_year.year, CreditCard::FULL_SERVER_CARD, |
| 815 CreditCard::OK}, |
| 816 {true, next_year.month, next_year.year, CreditCard::MASKED_SERVER_CARD, |
| 817 CreditCard::EXPIRED}, |
| 818 {true, next_year.month, next_year.year, CreditCard::FULL_SERVER_CARD, |
| 819 CreditCard::EXPIRED}, |
| 820 }; |
| 821 |
| 822 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
| 823 CreditCard card(base::ASCIIToUTF16("1234"), kTestCases[i].month, |
| 824 kTestCases[i].year); |
| 825 card.set_record_type(kTestCases[i].record_type); |
| 826 if (card.record_type() != CreditCard::LOCAL_CARD) |
| 827 card.SetServerStatus(kTestCases[i].server_status); |
| 828 |
| 829 EXPECT_EQ(kTestCases[i].should_update_expiration, |
| 830 card.ShouldUpdateExpiration(now)); |
| 831 } |
| 832 } |
| 833 |
| 735 } // namespace autofill | 834 } // namespace autofill |
| OLD | NEW |