Chromium Code Reviews| Index: components/autofill/core/browser/credit_card_unittest.cc |
| diff --git a/components/autofill/core/browser/credit_card_unittest.cc b/components/autofill/core/browser/credit_card_unittest.cc |
| index aaee3655bd2c07ab269a209a29e6772e1d3f6442..e1f1d0097c1984db65327d3d7b77bb59cc3a1c5c 100644 |
| --- a/components/autofill/core/browser/credit_card_unittest.cc |
| +++ b/components/autofill/core/browser/credit_card_unittest.cc |
| @@ -8,6 +8,7 @@ |
| #include "base/macros.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/utf_string_conversions.h" |
| +#include "base/time/time.h" |
| #include "build/build_config.h" |
| #include "components/autofill/core/browser/autofill_test_utils.h" |
| #include "components/autofill/core/browser/autofill_type.h" |
| @@ -732,4 +733,156 @@ TEST(CreditCardTest, CanBuildFromCardNumberAndExpirationDate) { |
| EXPECT_EQ(year, card.expiration_year()); |
| } |
| +// A test case for credit card expiration year and month. |
| +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.
|
| + public: |
| + ExpTestCase(const base::Time& now, |
| + bool update, |
| + int month, |
| + int year, |
| + CreditCard::RecordType type) |
| + : current_time_(now), |
| + should_update_expiration_(update), |
| + month_(month), |
| + year_(year), |
| + record_type_(type), |
| + server_status_(CreditCard::OK) {} |
| + |
| + ExpTestCase(const base::Time& now, |
| + bool update, |
| + int month, |
| + int year, |
| + CreditCard::RecordType type, |
| + CreditCard::ServerStatus status) |
| + : current_time_(now), |
| + should_update_expiration_(update), |
| + month_(month), |
| + year_(year), |
| + record_type_(type), |
| + server_status_(status) {} |
| + |
| + ~ExpTestCase() {} |
| + |
| + const base::Time& current_time() const { return current_time_; } |
| + bool should_update_expiration() const { return should_update_expiration_; } |
| + int month() const { return month_; } |
| + int year() const { return year_; } |
| + CreditCard::RecordType record_type() const { return record_type_; } |
| + CreditCard::ServerStatus server_status() const { return server_status_; } |
| + |
| + private: |
| + base::Time current_time_; |
| + bool should_update_expiration_; |
| + int month_; |
| + int year_; |
| + CreditCard::RecordType record_type_; |
| + CreditCard::ServerStatus server_status_; |
| +}; |
| + |
| +// A parameterized test fixture for credit card expiration. |
| +class CreditCardExpTest : public testing::TestWithParam<ExpTestCase> { |
| + public: |
| + static std::vector<ExpTestCase> GenerateTestCases() { |
| + std::vector<ExpTestCase> cases; |
| + |
| + base::Time now = base::Time::Now(); |
| + base::Time::Exploded last_year; |
| + (now - base::TimeDelta::FromDays(365)).LocalExplode(&last_year); |
| + |
| + // Cards that expired last year should always be updated. |
| + cases.emplace_back(now, true, last_year.month, last_year.year, |
| + CreditCard::LOCAL_CARD); |
| + cases.emplace_back(now, true, last_year.month, last_year.year, |
| + CreditCard::FULL_SERVER_CARD, CreditCard::OK); |
| + cases.emplace_back(now, true, last_year.month, last_year.year, |
| + CreditCard::MASKED_SERVER_CARD, CreditCard::OK); |
| + cases.emplace_back(now, true, last_year.month, last_year.year, |
| + CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED); |
| + cases.emplace_back(now, true, last_year.month, last_year.year, |
| + CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED); |
| + |
| + base::Time::Exploded last_month; |
| + (now - base::TimeDelta::FromDays(31)).LocalExplode(&last_month); |
| + |
| + // Cards that expired last month should always be updated. |
| + cases.emplace_back(now, true, last_month.month, last_month.year, |
| + CreditCard::LOCAL_CARD); |
| + cases.emplace_back(now, true, last_month.month, last_month.year, |
| + CreditCard::FULL_SERVER_CARD, CreditCard::OK); |
| + cases.emplace_back(now, true, last_month.month, last_month.year, |
| + CreditCard::MASKED_SERVER_CARD, CreditCard::OK); |
| + cases.emplace_back(now, true, last_month.month, last_month.year, |
| + CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED); |
| + cases.emplace_back(now, true, last_month.month, last_month.year, |
| + CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED); |
| + |
| + base::Time::Exploded current; |
| + now.LocalExplode(¤t); |
| + |
| + // Cards that expire this month should be updated only if the server status |
| + // is EXPIRED. |
| + cases.emplace_back(now, false, current.month, current.year, |
| + CreditCard::LOCAL_CARD); |
| + cases.emplace_back(now, false, current.month, current.year, |
| + CreditCard::FULL_SERVER_CARD, CreditCard::OK); |
| + cases.emplace_back(now, false, current.month, current.year, |
| + CreditCard::MASKED_SERVER_CARD, CreditCard::OK); |
| + cases.emplace_back(now, true, current.month, current.year, |
| + CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED); |
| + cases.emplace_back(now, true, current.month, current.year, |
| + CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED); |
| + |
| + base::Time::Exploded next_month; |
| + (now + base::TimeDelta::FromDays(31)).LocalExplode(&next_month); |
| + |
| + // Cards that expire next month should be updated only if the server status |
| + // is EXPIRED. |
| + cases.emplace_back(now, false, next_month.month, next_month.year, |
| + CreditCard::LOCAL_CARD); |
| + cases.emplace_back(now, false, next_month.month, next_month.year, |
| + CreditCard::MASKED_SERVER_CARD, CreditCard::OK); |
| + cases.emplace_back(now, false, next_month.month, next_month.year, |
| + CreditCard::FULL_SERVER_CARD, CreditCard::OK); |
| + cases.emplace_back(now, true, next_month.month, next_month.year, |
| + CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED); |
| + cases.emplace_back(now, true, next_month.month, next_month.year, |
| + CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED); |
| + |
| + base::Time::Exploded next_year; |
| + (now + base::TimeDelta::FromDays(365)).LocalExplode(&next_year); |
| + |
| + // Cards that expire next year should be updated only if the server status |
| + // is EXPIRED. |
| + cases.emplace_back(now, false, next_year.month, next_year.year, |
| + CreditCard::LOCAL_CARD); |
| + cases.emplace_back(now, false, next_year.month, next_year.year, |
| + CreditCard::MASKED_SERVER_CARD, CreditCard::OK); |
| + cases.emplace_back(now, false, next_year.month, next_year.year, |
| + CreditCard::FULL_SERVER_CARD, CreditCard::OK); |
| + cases.emplace_back(now, true, next_year.month, next_year.year, |
| + CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED); |
| + cases.emplace_back(now, true, next_year.month, next_year.year, |
| + CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED); |
| + |
| + return cases; |
| + } |
| +}; |
| + |
| +// Verifies that a credit card should be updated. |
| +TEST_P(CreditCardExpTest, ShouldUpdateExpiration) { |
| + CreditCard card(base::ASCIIToUTF16("1234"), GetParam().month(), |
| + GetParam().year()); |
| + card.set_record_type(GetParam().record_type()); |
| + if (card.record_type() != CreditCard::LOCAL_CARD) |
| + card.SetServerStatus(GetParam().server_status()); |
| + |
| + EXPECT_EQ(GetParam().should_update_expiration(), |
| + card.ShouldUpdateExpiration(GetParam().current_time())); |
| +} |
| + |
| +INSTANTIATE_TEST_CASE_P( |
| + Expiration, |
| + CreditCardExpTest, |
| + testing::ValuesIn(CreditCardExpTest::GenerateTestCases())); |
| + |
| } // namespace autofill |