| 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" |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 // Result of assignment should be logically equal to the original profile. | 160 // Result of assignment should be logically equal to the original profile. |
| 161 CreditCard b(base::GenerateGUID(), "some other origin"); | 161 CreditCard b(base::GenerateGUID(), "some other origin"); |
| 162 b = a; | 162 b = a; |
| 163 EXPECT_TRUE(a == b); | 163 EXPECT_TRUE(a == b); |
| 164 | 164 |
| 165 // Assignment to self should not change the profile value. | 165 // Assignment to self should not change the profile value. |
| 166 a = a; | 166 a = a; |
| 167 EXPECT_TRUE(a == b); | 167 EXPECT_TRUE(a == b); |
| 168 } | 168 } |
| 169 | 169 |
| 170 TEST(CreditCardTest, SetExpirationYearFromString) { |
| 171 static const struct { |
| 172 std::string expiration_year; |
| 173 int expected_year; |
| 174 } kTestCases[] = { |
| 175 // Valid values. |
| 176 {"2040", 2040}, |
| 177 {"45", 2045}, |
| 178 {"045", 2045}, |
| 179 {"9", 2009}, |
| 180 |
| 181 // Unrecognized year values. |
| 182 {"052045", 0}, |
| 183 {"123", 0}, |
| 184 {"y2045", 0}, |
| 185 }; |
| 186 |
| 187 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
| 188 CreditCard card(base::GenerateGUID(), "some origin"); |
| 189 card.SetExpirationYearFromString( |
| 190 ASCIIToUTF16(kTestCases[i].expiration_year)); |
| 191 |
| 192 EXPECT_EQ(kTestCases[i].expected_year, card.expiration_year()) |
| 193 << kTestCases[i].expiration_year << " " << kTestCases[i].expected_year; |
| 194 } |
| 195 } |
| 196 |
| 197 TEST(CreditCardTest, SetExpirationDateFromString) { |
| 198 static const struct { |
| 199 std::string expiration_date; |
| 200 int expected_month; |
| 201 int expected_year; |
| 202 } kTestCases[] = {{"10", 0, 0}, // Too small. |
| 203 {"1020451", 0, 0}, // Too long. |
| 204 |
| 205 // No separators. |
| 206 {"105", 0, 0}, // Too ambiguous. |
| 207 {"0545", 5, 2045}, |
| 208 {"52045", 0, 0}, // Too ambiguous. |
| 209 {"052045", 5, 2045}, |
| 210 |
| 211 // "/" separator. |
| 212 {"05/45", 5, 2045}, |
| 213 {"5/2045", 5, 2045}, |
| 214 {"05/2045", 5, 2045}, |
| 215 |
| 216 // "-" separator. |
| 217 {"05-45", 5, 2045}, |
| 218 {"5-2045", 5, 2045}, |
| 219 {"05-2045", 5, 2045}, |
| 220 |
| 221 // "|" separator. |
| 222 {"05|45", 5, 2045}, |
| 223 {"5|2045", 5, 2045}, |
| 224 {"05|2045", 5, 2045}, |
| 225 |
| 226 // Invalid values. |
| 227 {"13/2016", 0, 2016}, |
| 228 {"16/13", 0, 2013}, |
| 229 {"May-2015", 0, 0}, |
| 230 {"05-/2045", 0, 0}, |
| 231 {"05_2045", 0, 0}}; |
| 232 |
| 233 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
| 234 CreditCard card(base::GenerateGUID(), "some origin"); |
| 235 card.SetExpirationDateFromString( |
| 236 ASCIIToUTF16(kTestCases[i].expiration_date)); |
| 237 |
| 238 EXPECT_EQ(kTestCases[i].expected_month, card.expiration_month()); |
| 239 EXPECT_EQ(kTestCases[i].expected_year, card.expiration_year()); |
| 240 } |
| 241 } |
| 242 |
| 170 TEST(CreditCardTest, Copy) { | 243 TEST(CreditCardTest, Copy) { |
| 171 CreditCard a(base::GenerateGUID(), "https://www.example.com"); | 244 CreditCard a(base::GenerateGUID(), "https://www.example.com"); |
| 172 test::SetCreditCardInfo(&a, "John Dillinger", "123456789012", "01", "2010"); | 245 test::SetCreditCardInfo(&a, "John Dillinger", "123456789012", "01", "2010"); |
| 173 | 246 |
| 174 // Clone should be logically equal to the original. | 247 // Clone should be logically equal to the original. |
| 175 CreditCard b(a); | 248 CreditCard b(a); |
| 176 EXPECT_TRUE(a == b); | 249 EXPECT_TRUE(a == b); |
| 177 } | 250 } |
| 178 | 251 |
| 179 TEST(CreditCardTest, IsLocalDuplicateOfServerCard) { | 252 TEST(CreditCardTest, IsLocalDuplicateOfServerCard) { |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 now.LocalExplode(&now_exploded); | 531 now.LocalExplode(&now_exploded); |
| 459 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, | 532 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, |
| 460 base::IntToString16(now_exploded.month)); | 533 base::IntToString16(now_exploded.month)); |
| 461 card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, | 534 card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, |
| 462 base::IntToString16(now_exploded.year - 1)); | 535 base::IntToString16(now_exploded.year - 1)); |
| 463 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("4111111111111111")); | 536 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("4111111111111111")); |
| 464 EXPECT_FALSE(card.IsValid()); | 537 EXPECT_FALSE(card.IsValid()); |
| 465 | 538 |
| 466 // Invalid because card number is not complete | 539 // Invalid because card number is not complete |
| 467 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("12")); | 540 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("12")); |
| 468 card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("9999")); | 541 card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2999")); |
| 469 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("41111")); | 542 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("41111")); |
| 470 EXPECT_FALSE(card.IsValid()); | 543 EXPECT_FALSE(card.IsValid()); |
| 471 | 544 |
| 472 for (size_t i = 0; i < arraysize(kValidNumbers); ++i) { | 545 for (size_t i = 0; i < arraysize(kValidNumbers); ++i) { |
| 473 SCOPED_TRACE(kValidNumbers[i]); | 546 SCOPED_TRACE(kValidNumbers[i]); |
| 474 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16(kValidNumbers[i])); | 547 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16(kValidNumbers[i])); |
| 475 EXPECT_TRUE(card.IsValid()); | 548 EXPECT_TRUE(card.IsValid()); |
| 476 } | 549 } |
| 477 for (size_t i = 0; i < arraysize(kInvalidNumbers); ++i) { | 550 for (size_t i = 0; i < arraysize(kInvalidNumbers); ++i) { |
| 478 SCOPED_TRACE(kInvalidNumbers[i]); | 551 SCOPED_TRACE(kInvalidNumbers[i]); |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("3489")); | 793 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("3489")); |
| 721 ASSERT_EQ(base::ASCIIToUTF16("3489"), card.LastFourDigits()); | 794 ASSERT_EQ(base::ASCIIToUTF16("3489"), card.LastFourDigits()); |
| 722 | 795 |
| 723 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("489")); | 796 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("489")); |
| 724 ASSERT_EQ(base::ASCIIToUTF16("489"), card.LastFourDigits()); | 797 ASSERT_EQ(base::ASCIIToUTF16("489"), card.LastFourDigits()); |
| 725 } | 798 } |
| 726 | 799 |
| 727 TEST(CreditCardTest, CanBuildFromCardNumberAndExpirationDate) { | 800 TEST(CreditCardTest, CanBuildFromCardNumberAndExpirationDate) { |
| 728 base::string16 card_number = base::ASCIIToUTF16("test"); | 801 base::string16 card_number = base::ASCIIToUTF16("test"); |
| 729 int month = 1; | 802 int month = 1; |
| 730 int year = 3000; | 803 int year = 2999; |
| 731 CreditCard card(card_number, month, year); | 804 CreditCard card(card_number, month, year); |
| 732 EXPECT_EQ(card_number, card.number()); | 805 EXPECT_EQ(card_number, card.number()); |
| 733 EXPECT_EQ(month, card.expiration_month()); | 806 EXPECT_EQ(month, card.expiration_month()); |
| 734 EXPECT_EQ(year, card.expiration_year()); | 807 EXPECT_EQ(year, card.expiration_year()); |
| 735 } | 808 } |
| 736 | 809 |
| 737 // Verifies that a credit card should be updated. | 810 // Verifies that a credit card should be updated. |
| 738 TEST(CreditCardTest, ShouldUpdateExpiration) { | 811 TEST(CreditCardTest, ShouldUpdateExpiration) { |
| 739 base::Time now = base::Time::Now(); | 812 base::Time now = base::Time::Now(); |
| 740 | 813 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 826 card.set_record_type(kTestCases[i].record_type); | 899 card.set_record_type(kTestCases[i].record_type); |
| 827 if (card.record_type() != CreditCard::LOCAL_CARD) | 900 if (card.record_type() != CreditCard::LOCAL_CARD) |
| 828 card.SetServerStatus(kTestCases[i].server_status); | 901 card.SetServerStatus(kTestCases[i].server_status); |
| 829 | 902 |
| 830 EXPECT_EQ(kTestCases[i].should_update_expiration, | 903 EXPECT_EQ(kTestCases[i].should_update_expiration, |
| 831 card.ShouldUpdateExpiration(now)); | 904 card.ShouldUpdateExpiration(now)); |
| 832 } | 905 } |
| 833 } | 906 } |
| 834 | 907 |
| 835 } // namespace autofill | 908 } // namespace autofill |
| OLD | NEW |