Chromium Code Reviews| Index: components/autofill/core/browser/personal_data_manager_unittest.cc |
| diff --git a/components/autofill/core/browser/personal_data_manager_unittest.cc b/components/autofill/core/browser/personal_data_manager_unittest.cc |
| index 18bb0aebdd43692e5a59a927b3175659b6b0fd1f..9cf4befbf6e824bd735c54c86c47a0c8491a611f 100644 |
| --- a/components/autofill/core/browser/personal_data_manager_unittest.cc |
| +++ b/components/autofill/core/browser/personal_data_manager_unittest.cc |
| @@ -17,6 +17,7 @@ |
| #include "base/command_line.h" |
| #include "base/files/scoped_temp_dir.h" |
| #include "base/guid.h" |
| +#include "base/i18n/time_formatting.h" |
| #include "base/memory/ptr_util.h" |
| #include "base/metrics/field_trial.h" |
| #include "base/run_loop.h" |
| @@ -46,6 +47,7 @@ |
| #include "components/signin/core/common/signin_pref_names.h" |
| #include "components/variations/entropy_provider.h" |
| #include "components/variations/variations_associated_data.h" |
| +#include "components/variations/variations_params_manager.h" |
| #include "components/webdata/common/web_data_service_base.h" |
| #include "components/webdata/common/web_database_service.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| @@ -290,6 +292,46 @@ class PersonalDataManagerTest : public testing::Test { |
| ASSERT_EQ(3U, personal_data_->GetCreditCards().size()); |
| } |
| + // Adds three local cards to the |personal_data_|. These cards are different: |
| + // One was just added, one was already used in autofill, and the third was |
| + // last used more than one year ago. |
| + void SetupReferenceCreditCardsForLastUsedVariationTest() { |
| + ASSERT_EQ(0U, personal_data_->GetCreditCards().size()); |
| + base::Time cur_time = base::Time::Now(); |
| + |
| + // Test for added to chrome. |
| + CreditCard credit_card0("1141084B-72D7-4B73-90CF-3D6AC154673B", |
| + "https://www.example.com"); |
| + credit_card0.set_use_count(1); |
| + credit_card0.set_use_date(cur_time - base::TimeDelta::FromDays(1)); |
| + test::SetCreditCardInfo(&credit_card0, "John Dillinger", |
| + "423456789012" /* Visa */, "01", "2021"); |
| + personal_data_->AddCreditCard(credit_card0); |
| + |
| + // Test for last used date. |
| + CreditCard credit_card1("287151C8-6AB1-487C-9095-28E80BE5DA15", |
| + "https://www.example.com"); |
| + test::SetCreditCardInfo(&credit_card1, "Clyde Barrow", |
| + "347666888555" /* American Express */, "04", |
| + "2021"); |
| + credit_card1.set_use_count(10); |
| + credit_card1.set_use_date(cur_time - base::TimeDelta::FromDays(10)); |
| + personal_data_->AddCreditCard(credit_card1); |
| + |
| + // Test for last used more than one year ago. |
| + CreditCard credit_card2("002149C1-EE28-4213-A3B9-DA243FFF021B", |
| + "https://www.example.com"); |
| + credit_card2.set_use_count(5); |
| + credit_card2.set_use_date(cur_time - base::TimeDelta::FromDays(366)); |
| + test::SetCreditCardInfo(&credit_card2, "Bonnie Parker", |
| + "518765432109" /* Mastercard */, "12", "2021"); |
| + personal_data_->AddCreditCard(credit_card2); |
| + |
| + EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| + .WillOnce(QuitMainMessageLoop()); |
| + base::RunLoop().Run(); |
| + } |
| + |
| // Helper methods that simply forward the call to the private member (to avoid |
| // having to friend every test that needs to access the private |
| // PersonalDataManager::ImportAddressProfile or ImportCreditCard). |
| @@ -366,6 +408,7 @@ class PersonalDataManagerTest : public testing::Test { |
| std::unique_ptr<base::FieldTrialList> field_trial_list_; |
| scoped_refptr<base::FieldTrial> field_trial_; |
| + variations::testing::VariationParamsManager variation_params_; |
| }; |
| TEST_F(PersonalDataManagerTest, AddProfile) { |
| @@ -3814,6 +3857,137 @@ TEST_F(PersonalDataManagerTest, |
| ASSERT_EQ(3U, suggestions.size()); |
| } |
| +// Test that credit card last used date suggestion can be generated correctly |
| +// in variation 1: only show last used date. |
| +TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions_LastUsedDateOnly) { |
| + SetupReferenceCreditCardsForLastUsedVariationTest(); |
| + |
| + variation_params_.SetVariationParamsWithFeatureAssociations( |
| + kAutofillCreditCardLastUsedDateDisplay.name, |
| + {{kAutofillCreditCardLastUsedDateShowExpirationDateKey, "false"}, |
| + {kAutofillCreditCardLastUsedDateShowTimeDetailKey, "false"}}, |
| + {kAutofillCreditCardLastUsedDateDisplay.name}); |
| + |
| + const std::vector<CreditCard*> credit_cards = |
| + personal_data_->GetCreditCards(); |
| + ASSERT_EQ(3U, credit_cards.size()); |
| + |
| + std::vector<Suggestion> suggestions = |
| + personal_data_->GetCreditCardSuggestions( |
| + AutofillType(CREDIT_CARD_NUMBER), |
| + /* field_contents= */ base::string16()); |
| + ASSERT_EQ(3U, suggestions.size()); |
| + |
| + EXPECT_EQ( |
| + ASCIIToUTF16("Last used: ") + |
| + base::TimeFormatWithPattern(credit_cards[2]->use_date(), "MMMdd"), |
| + suggestions[0].label); |
| + EXPECT_EQ( |
| + ASCIIToUTF16("Added to Chrome: ") + |
|
Jared Saul
2017/01/25 18:22:17
Should "Chrome" be hard-coded in here or is there
jiahuiguo
2017/01/26 05:46:13
Good point, added platform checking to distinguish
|
| + base::TimeFormatWithPattern(credit_cards[1]->use_date(), "MMMdd"), |
| + suggestions[1].label); |
| + EXPECT_EQ(ASCIIToUTF16("Last used > 1 year"), suggestions[2].label); |
| +} |
| + |
| +// Test that credit card last used date suggestion can be generated correctly |
| +// in variation 2: show expiration date and last used date. |
| +TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions_ExpAndLastUsedDate) { |
| + SetupReferenceCreditCardsForLastUsedVariationTest(); |
| + |
| + variation_params_.SetVariationParamsWithFeatureAssociations( |
| + kAutofillCreditCardLastUsedDateDisplay.name, |
| + {{kAutofillCreditCardLastUsedDateShowExpirationDateKey, "true"}, |
| + {kAutofillCreditCardLastUsedDateShowTimeDetailKey, "false"}}, |
| + {kAutofillCreditCardLastUsedDateDisplay.name}); |
| + |
| + const std::vector<CreditCard*> credit_cards = |
| + personal_data_->GetCreditCards(); |
| + ASSERT_EQ(3U, credit_cards.size()); |
| + |
| + std::vector<Suggestion> suggestions = |
| + personal_data_->GetCreditCardSuggestions( |
| + AutofillType(CREDIT_CARD_NUMBER), |
| + /* field_contents= */ base::string16()); |
| + ASSERT_EQ(3U, suggestions.size()); |
| + |
| + EXPECT_EQ( |
| + ASCIIToUTF16("Exp: 04/21, last used: ") + |
| + base::TimeFormatWithPattern(credit_cards[2]->use_date(), "MMMdd"), |
| + suggestions[0].label); |
| + EXPECT_EQ( |
| + ASCIIToUTF16("Exp: 01/21, added to Chrome: ") + |
| + base::TimeFormatWithPattern(credit_cards[1]->use_date(), "MMMdd"), |
| + suggestions[1].label); |
| + EXPECT_EQ(ASCIIToUTF16("Exp: 12/21, last used > 1 year"), |
| + suggestions[2].label); |
| +} |
| + |
| +// Test that credit card last used date suggestion can be generated correctly |
| +// in variation 3: show last used date detail. |
| +TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions_LastUsedDateDetail) { |
| + SetupReferenceCreditCardsForLastUsedVariationTest(); |
| + |
| + variation_params_.SetVariationParamsWithFeatureAssociations( |
| + kAutofillCreditCardLastUsedDateDisplay.name, |
| + {{kAutofillCreditCardLastUsedDateShowExpirationDateKey, "false"}, |
| + {kAutofillCreditCardLastUsedDateShowTimeDetailKey, "true"}}, |
| + {kAutofillCreditCardLastUsedDateDisplay.name}); |
| + |
| + const std::vector<CreditCard*> credit_cards = |
| + personal_data_->GetCreditCards(); |
| + ASSERT_EQ(3U, credit_cards.size()); |
| + |
| + std::vector<Suggestion> suggestions = |
| + personal_data_->GetCreditCardSuggestions( |
| + AutofillType(CREDIT_CARD_NUMBER), |
| + /* field_contents= */ base::string16()); |
| + ASSERT_EQ(3U, suggestions.size()); |
| + |
| + EXPECT_EQ( |
| + ASCIIToUTF16("Last used: ") + |
| + base::TimeFormatWithPattern(credit_cards[2]->use_date(), "MMMddjmm"), |
| + suggestions[0].label); |
| + EXPECT_EQ( |
| + ASCIIToUTF16("Added to Chrome: ") + |
| + base::TimeFormatWithPattern(credit_cards[1]->use_date(), "MMMdd"), |
| + suggestions[1].label); |
| + EXPECT_EQ(ASCIIToUTF16("Last used > 1 year"), suggestions[2].label); |
| +} |
| + |
| +// Test that credit card last used date suggestion can be generated correctly |
| +// in variation 4: show expiration date and last used date detail. |
| +TEST_F(PersonalDataManagerTest, |
| + GetCreditCardSuggestions_ExpAndLastUsedDateDetail) { |
| + SetupReferenceCreditCardsForLastUsedVariationTest(); |
| + |
| + variation_params_.SetVariationParamsWithFeatureAssociations( |
| + kAutofillCreditCardLastUsedDateDisplay.name, |
| + {{kAutofillCreditCardLastUsedDateShowExpirationDateKey, "true"}, |
| + {kAutofillCreditCardLastUsedDateShowTimeDetailKey, "true"}}, |
| + {kAutofillCreditCardLastUsedDateDisplay.name}); |
| + |
| + const std::vector<CreditCard*> credit_cards = |
| + personal_data_->GetCreditCards(); |
| + ASSERT_EQ(3U, credit_cards.size()); |
| + |
| + std::vector<Suggestion> suggestions = |
| + personal_data_->GetCreditCardSuggestions( |
| + AutofillType(CREDIT_CARD_NUMBER), |
| + /* field_contents= */ base::string16()); |
| + ASSERT_EQ(3U, suggestions.size()); |
| + |
| + EXPECT_EQ( |
| + ASCIIToUTF16("Exp: 04/21, last used: ") + |
| + base::TimeFormatWithPattern(credit_cards[2]->use_date(), "MMMddjmm"), |
| + suggestions[0].label); |
| + EXPECT_EQ( |
| + ASCIIToUTF16("Exp: 01/21, added to Chrome: ") + |
| + base::TimeFormatWithPattern(credit_cards[1]->use_date(), "MMMdd"), |
| + suggestions[1].label); |
| + EXPECT_EQ(ASCIIToUTF16("Exp: 12/21, last used > 1 year"), |
| + suggestions[2].label); |
| +} |
| + |
| // Tests that only the full server card is kept when deduping with a local |
| // duplicate of it. |
| TEST_F(PersonalDataManagerTest, |