Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(244)

Side by Side Diff: components/autofill/core/browser/personal_data_manager_unittest.cc

Issue 2607043002: [Autofill] Credit Card Autofill Last Used Date Experiment (Closed)
Patch Set: Added unittest dependency Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/autofill/core/browser/personal_data_manager.h" 5 #include "components/autofill/core/browser/personal_data_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <list> 10 #include <list>
11 #include <map> 11 #include <map>
12 #include <memory> 12 #include <memory>
13 #include <string> 13 #include <string>
14 #include <utility> 14 #include <utility>
15 #include <vector> 15 #include <vector>
16 16
17 #include "base/command_line.h" 17 #include "base/command_line.h"
18 #include "base/files/scoped_temp_dir.h" 18 #include "base/files/scoped_temp_dir.h"
19 #include "base/guid.h" 19 #include "base/guid.h"
20 #include "base/i18n/time_formatting.h"
20 #include "base/memory/ptr_util.h" 21 #include "base/memory/ptr_util.h"
21 #include "base/metrics/field_trial.h" 22 #include "base/metrics/field_trial.h"
22 #include "base/run_loop.h" 23 #include "base/run_loop.h"
23 #include "base/strings/utf_string_conversions.h" 24 #include "base/strings/utf_string_conversions.h"
24 #include "base/synchronization/waitable_event.h" 25 #include "base/synchronization/waitable_event.h"
25 #include "base/test/histogram_tester.h" 26 #include "base/test/histogram_tester.h"
26 #include "base/threading/thread_task_runner_handle.h" 27 #include "base/threading/thread_task_runner_handle.h"
27 #include "base/time/time.h" 28 #include "base/time/time.h"
28 #include "build/build_config.h" 29 #include "build/build_config.h"
29 #include "components/autofill/core/browser/autofill_experiments.h" 30 #include "components/autofill/core/browser/autofill_experiments.h"
30 #include "components/autofill/core/browser/autofill_profile.h" 31 #include "components/autofill/core/browser/autofill_profile.h"
31 #include "components/autofill/core/browser/autofill_test_utils.h" 32 #include "components/autofill/core/browser/autofill_test_utils.h"
32 #include "components/autofill/core/browser/field_types.h" 33 #include "components/autofill/core/browser/field_types.h"
33 #include "components/autofill/core/browser/form_structure.h" 34 #include "components/autofill/core/browser/form_structure.h"
34 #include "components/autofill/core/browser/personal_data_manager_observer.h" 35 #include "components/autofill/core/browser/personal_data_manager_observer.h"
35 #include "components/autofill/core/browser/webdata/autofill_table.h" 36 #include "components/autofill/core/browser/webdata/autofill_table.h"
36 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" 37 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
37 #include "components/autofill/core/common/autofill_constants.h" 38 #include "components/autofill/core/common/autofill_constants.h"
38 #include "components/autofill/core/common/autofill_pref_names.h" 39 #include "components/autofill/core/common/autofill_pref_names.h"
39 #include "components/autofill/core/common/autofill_switches.h" 40 #include "components/autofill/core/common/autofill_switches.h"
40 #include "components/autofill/core/common/form_data.h" 41 #include "components/autofill/core/common/form_data.h"
41 #include "components/os_crypt/os_crypt_mocker.h" 42 #include "components/os_crypt/os_crypt_mocker.h"
42 #include "components/prefs/pref_service.h" 43 #include "components/prefs/pref_service.h"
43 #include "components/signin/core/browser/account_tracker_service.h" 44 #include "components/signin/core/browser/account_tracker_service.h"
44 #include "components/signin/core/browser/fake_signin_manager.h" 45 #include "components/signin/core/browser/fake_signin_manager.h"
45 #include "components/signin/core/browser/test_signin_client.h" 46 #include "components/signin/core/browser/test_signin_client.h"
46 #include "components/signin/core/common/signin_pref_names.h" 47 #include "components/signin/core/common/signin_pref_names.h"
47 #include "components/variations/entropy_provider.h" 48 #include "components/variations/entropy_provider.h"
48 #include "components/variations/variations_associated_data.h" 49 #include "components/variations/variations_associated_data.h"
50 #include "components/variations/variations_params_manager.h"
49 #include "components/webdata/common/web_data_service_base.h" 51 #include "components/webdata/common/web_data_service_base.h"
50 #include "components/webdata/common/web_database_service.h" 52 #include "components/webdata/common/web_database_service.h"
51 #include "testing/gmock/include/gmock/gmock.h" 53 #include "testing/gmock/include/gmock/gmock.h"
52 #include "testing/gtest/include/gtest/gtest.h" 54 #include "testing/gtest/include/gtest/gtest.h"
53 55
54 using base::ASCIIToUTF16; 56 using base::ASCIIToUTF16;
55 using base::UTF8ToUTF16; 57 using base::UTF8ToUTF16;
56 58
57 namespace autofill { 59 namespace autofill {
58 namespace { 60 namespace {
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 "518765432109" /* Mastercard */, "12", "2999"); 285 "518765432109" /* Mastercard */, "12", "2999");
284 personal_data_->AddCreditCard(credit_card2); 286 personal_data_->AddCreditCard(credit_card2);
285 287
286 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) 288 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
287 .WillOnce(QuitMainMessageLoop()); 289 .WillOnce(QuitMainMessageLoop());
288 base::RunLoop().Run(); 290 base::RunLoop().Run();
289 291
290 ASSERT_EQ(3U, personal_data_->GetCreditCards().size()); 292 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
291 } 293 }
292 294
295 // Adds three local cards to the |personal_data_|. These cards are different:
296 // One was just added, one was already used in autofill, and the third was
297 // last used more than one year ago.
298 void SetupReferenceCreditCardsForLastUsedVariationTest() {
299 ASSERT_EQ(0U, personal_data_->GetCreditCards().size());
300 base::Time cur_time = base::Time::Now();
301
302 // Test for added to chrome.
303 CreditCard credit_card0("1141084B-72D7-4B73-90CF-3D6AC154673B",
304 "https://www.example.com");
305 credit_card0.set_use_count(1);
306 credit_card0.set_use_date(cur_time - base::TimeDelta::FromDays(1));
307 test::SetCreditCardInfo(&credit_card0, "John Dillinger",
308 "423456789012" /* Visa */, "01", "2021");
309 personal_data_->AddCreditCard(credit_card0);
310
311 // Test for last used date.
312 CreditCard credit_card1("287151C8-6AB1-487C-9095-28E80BE5DA15",
313 "https://www.example.com");
314 test::SetCreditCardInfo(&credit_card1, "Clyde Barrow",
315 "347666888555" /* American Express */, "04",
316 "2021");
317 credit_card1.set_use_count(10);
318 credit_card1.set_use_date(cur_time - base::TimeDelta::FromDays(10));
319 personal_data_->AddCreditCard(credit_card1);
320
321 // Test for last used more than one year ago.
322 CreditCard credit_card2("002149C1-EE28-4213-A3B9-DA243FFF021B",
323 "https://www.example.com");
324 credit_card2.set_use_count(5);
325 credit_card2.set_use_date(cur_time - base::TimeDelta::FromDays(366));
326 test::SetCreditCardInfo(&credit_card2, "Bonnie Parker",
327 "518765432109" /* Mastercard */, "12", "2021");
328 personal_data_->AddCreditCard(credit_card2);
329
330 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
331 .WillOnce(QuitMainMessageLoop());
332 base::RunLoop().Run();
333 }
334
293 // Helper methods that simply forward the call to the private member (to avoid 335 // Helper methods that simply forward the call to the private member (to avoid
294 // having to friend every test that needs to access the private 336 // having to friend every test that needs to access the private
295 // PersonalDataManager::ImportAddressProfile or ImportCreditCard). 337 // PersonalDataManager::ImportAddressProfile or ImportCreditCard).
296 bool ImportAddressProfiles(const FormStructure& form) { 338 bool ImportAddressProfiles(const FormStructure& form) {
297 return personal_data_->ImportAddressProfiles(form); 339 return personal_data_->ImportAddressProfiles(form);
298 } 340 }
299 bool ImportCreditCard(const FormStructure& form, 341 bool ImportCreditCard(const FormStructure& form,
300 bool should_return_local_card, 342 bool should_return_local_card,
301 std::unique_ptr<CreditCard>* imported_credit_card) { 343 std::unique_ptr<CreditCard>* imported_credit_card) {
302 return personal_data_->ImportCreditCard(form, should_return_local_card, 344 return personal_data_->ImportCreditCard(form, should_return_local_card,
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 std::unique_ptr<FakeSigninManagerBase> signin_manager_; 401 std::unique_ptr<FakeSigninManagerBase> signin_manager_;
360 std::unique_ptr<TestSigninClient> signin_client_; 402 std::unique_ptr<TestSigninClient> signin_client_;
361 scoped_refptr<AutofillWebDataService> autofill_database_service_; 403 scoped_refptr<AutofillWebDataService> autofill_database_service_;
362 scoped_refptr<WebDatabaseService> web_database_; 404 scoped_refptr<WebDatabaseService> web_database_;
363 AutofillTable* autofill_table_; // weak ref 405 AutofillTable* autofill_table_; // weak ref
364 PersonalDataLoadedObserverMock personal_data_observer_; 406 PersonalDataLoadedObserverMock personal_data_observer_;
365 std::unique_ptr<PersonalDataManager> personal_data_; 407 std::unique_ptr<PersonalDataManager> personal_data_;
366 408
367 std::unique_ptr<base::FieldTrialList> field_trial_list_; 409 std::unique_ptr<base::FieldTrialList> field_trial_list_;
368 scoped_refptr<base::FieldTrial> field_trial_; 410 scoped_refptr<base::FieldTrial> field_trial_;
411 variations::testing::VariationParamsManager variation_params_;
369 }; 412 };
370 413
371 TEST_F(PersonalDataManagerTest, AddProfile) { 414 TEST_F(PersonalDataManagerTest, AddProfile) {
372 // Add profile0 to the database. 415 // Add profile0 to the database.
373 AutofillProfile profile0(test::GetFullProfile()); 416 AutofillProfile profile0(test::GetFullProfile());
374 profile0.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("j@s.com")); 417 profile0.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("j@s.com"));
375 personal_data_->AddProfile(profile0); 418 personal_data_->AddProfile(profile0);
376 419
377 // Reload the database. 420 // Reload the database.
378 ResetPersonalDataManager(USER_MODE_NORMAL); 421 ResetPersonalDataManager(USER_MODE_NORMAL);
(...skipping 3428 matching lines...) Expand 10 before | Expand all | Expand 10 after
3807 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) 3850 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
3808 .WillOnce(QuitMainMessageLoop()); 3851 .WillOnce(QuitMainMessageLoop());
3809 base::RunLoop().Run(); 3852 base::RunLoop().Run();
3810 3853
3811 suggestions = personal_data_->GetCreditCardSuggestions( 3854 suggestions = personal_data_->GetCreditCardSuggestions(
3812 AutofillType(CREDIT_CARD_NAME_FULL), 3855 AutofillType(CREDIT_CARD_NAME_FULL),
3813 /* field_contents= */ base::string16()); 3856 /* field_contents= */ base::string16());
3814 ASSERT_EQ(3U, suggestions.size()); 3857 ASSERT_EQ(3U, suggestions.size());
3815 } 3858 }
3816 3859
3860 // Test that credit card last used date suggestion can be generated correctly
3861 // in variation 1: only show last used date.
3862 TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions_LastUsedDateOnly) {
3863 SetupReferenceCreditCardsForLastUsedVariationTest();
3864
3865 variation_params_.SetVariationParamsWithFeatureAssociations(
3866 kAutofillCreditCardLastUsedDateDisplay.name,
3867 {{kAutofillCreditCardLastUsedDateShowExpirationDateKey, "false"},
3868 {kAutofillCreditCardLastUsedDateShowTimeDetailKey, "false"}},
3869 {kAutofillCreditCardLastUsedDateDisplay.name});
3870
3871 const std::vector<CreditCard*> credit_cards =
3872 personal_data_->GetCreditCards();
3873 ASSERT_EQ(3U, credit_cards.size());
3874
3875 std::vector<Suggestion> suggestions =
3876 personal_data_->GetCreditCardSuggestions(
3877 AutofillType(CREDIT_CARD_NUMBER),
3878 /* field_contents= */ base::string16());
3879 ASSERT_EQ(3U, suggestions.size());
3880
3881 EXPECT_EQ(
3882 ASCIIToUTF16("Last used: ") +
3883 base::TimeFormatWithPattern(credit_cards[2]->use_date(), "MMMdd"),
3884 suggestions[0].label);
3885 EXPECT_EQ(
3886 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
3887 base::TimeFormatWithPattern(credit_cards[1]->use_date(), "MMMdd"),
3888 suggestions[1].label);
3889 EXPECT_EQ(ASCIIToUTF16("Last used > 1 year"), suggestions[2].label);
3890 }
3891
3892 // Test that credit card last used date suggestion can be generated correctly
3893 // in variation 2: show expiration date and last used date.
3894 TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions_ExpAndLastUsedDate) {
3895 SetupReferenceCreditCardsForLastUsedVariationTest();
3896
3897 variation_params_.SetVariationParamsWithFeatureAssociations(
3898 kAutofillCreditCardLastUsedDateDisplay.name,
3899 {{kAutofillCreditCardLastUsedDateShowExpirationDateKey, "true"},
3900 {kAutofillCreditCardLastUsedDateShowTimeDetailKey, "false"}},
3901 {kAutofillCreditCardLastUsedDateDisplay.name});
3902
3903 const std::vector<CreditCard*> credit_cards =
3904 personal_data_->GetCreditCards();
3905 ASSERT_EQ(3U, credit_cards.size());
3906
3907 std::vector<Suggestion> suggestions =
3908 personal_data_->GetCreditCardSuggestions(
3909 AutofillType(CREDIT_CARD_NUMBER),
3910 /* field_contents= */ base::string16());
3911 ASSERT_EQ(3U, suggestions.size());
3912
3913 EXPECT_EQ(
3914 ASCIIToUTF16("Exp: 04/21, last used: ") +
3915 base::TimeFormatWithPattern(credit_cards[2]->use_date(), "MMMdd"),
3916 suggestions[0].label);
3917 EXPECT_EQ(
3918 ASCIIToUTF16("Exp: 01/21, added to Chrome: ") +
3919 base::TimeFormatWithPattern(credit_cards[1]->use_date(), "MMMdd"),
3920 suggestions[1].label);
3921 EXPECT_EQ(ASCIIToUTF16("Exp: 12/21, last used > 1 year"),
3922 suggestions[2].label);
3923 }
3924
3925 // Test that credit card last used date suggestion can be generated correctly
3926 // in variation 3: show last used date detail.
3927 TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions_LastUsedDateDetail) {
3928 SetupReferenceCreditCardsForLastUsedVariationTest();
3929
3930 variation_params_.SetVariationParamsWithFeatureAssociations(
3931 kAutofillCreditCardLastUsedDateDisplay.name,
3932 {{kAutofillCreditCardLastUsedDateShowExpirationDateKey, "false"},
3933 {kAutofillCreditCardLastUsedDateShowTimeDetailKey, "true"}},
3934 {kAutofillCreditCardLastUsedDateDisplay.name});
3935
3936 const std::vector<CreditCard*> credit_cards =
3937 personal_data_->GetCreditCards();
3938 ASSERT_EQ(3U, credit_cards.size());
3939
3940 std::vector<Suggestion> suggestions =
3941 personal_data_->GetCreditCardSuggestions(
3942 AutofillType(CREDIT_CARD_NUMBER),
3943 /* field_contents= */ base::string16());
3944 ASSERT_EQ(3U, suggestions.size());
3945
3946 EXPECT_EQ(
3947 ASCIIToUTF16("Last used: ") +
3948 base::TimeFormatWithPattern(credit_cards[2]->use_date(), "MMMddjmm"),
3949 suggestions[0].label);
3950 EXPECT_EQ(
3951 ASCIIToUTF16("Added to Chrome: ") +
3952 base::TimeFormatWithPattern(credit_cards[1]->use_date(), "MMMdd"),
3953 suggestions[1].label);
3954 EXPECT_EQ(ASCIIToUTF16("Last used > 1 year"), suggestions[2].label);
3955 }
3956
3957 // Test that credit card last used date suggestion can be generated correctly
3958 // in variation 4: show expiration date and last used date detail.
3959 TEST_F(PersonalDataManagerTest,
3960 GetCreditCardSuggestions_ExpAndLastUsedDateDetail) {
3961 SetupReferenceCreditCardsForLastUsedVariationTest();
3962
3963 variation_params_.SetVariationParamsWithFeatureAssociations(
3964 kAutofillCreditCardLastUsedDateDisplay.name,
3965 {{kAutofillCreditCardLastUsedDateShowExpirationDateKey, "true"},
3966 {kAutofillCreditCardLastUsedDateShowTimeDetailKey, "true"}},
3967 {kAutofillCreditCardLastUsedDateDisplay.name});
3968
3969 const std::vector<CreditCard*> credit_cards =
3970 personal_data_->GetCreditCards();
3971 ASSERT_EQ(3U, credit_cards.size());
3972
3973 std::vector<Suggestion> suggestions =
3974 personal_data_->GetCreditCardSuggestions(
3975 AutofillType(CREDIT_CARD_NUMBER),
3976 /* field_contents= */ base::string16());
3977 ASSERT_EQ(3U, suggestions.size());
3978
3979 EXPECT_EQ(
3980 ASCIIToUTF16("Exp: 04/21, last used: ") +
3981 base::TimeFormatWithPattern(credit_cards[2]->use_date(), "MMMddjmm"),
3982 suggestions[0].label);
3983 EXPECT_EQ(
3984 ASCIIToUTF16("Exp: 01/21, added to Chrome: ") +
3985 base::TimeFormatWithPattern(credit_cards[1]->use_date(), "MMMdd"),
3986 suggestions[1].label);
3987 EXPECT_EQ(ASCIIToUTF16("Exp: 12/21, last used > 1 year"),
3988 suggestions[2].label);
3989 }
3990
3817 // Tests that only the full server card is kept when deduping with a local 3991 // Tests that only the full server card is kept when deduping with a local
3818 // duplicate of it. 3992 // duplicate of it.
3819 TEST_F(PersonalDataManagerTest, 3993 TEST_F(PersonalDataManagerTest,
3820 DedupeCreditCardToSuggest_FullServerShadowsLocal) { 3994 DedupeCreditCardToSuggest_FullServerShadowsLocal) {
3821 std::list<CreditCard*> credit_cards; 3995 std::list<CreditCard*> credit_cards;
3822 3996
3823 // Create 3 different local credit cards. 3997 // Create 3 different local credit cards.
3824 CreditCard local_card("287151C8-6AB1-487C-9095-28E80BE5DA15", 3998 CreditCard local_card("287151C8-6AB1-487C-9095-28E80BE5DA15",
3825 "https://www.example.com"); 3999 "https://www.example.com");
3826 test::SetCreditCardInfo(&local_card, "Homer Simpson", 4000 test::SetCreditCardInfo(&local_card, "Homer Simpson",
(...skipping 1812 matching lines...) Expand 10 before | Expand all | Expand 10 after
5639 EnableAutofillProfileCleanup(); 5813 EnableAutofillProfileCleanup();
5640 5814
5641 // The deduping routine should not be run. 5815 // The deduping routine should not be run.
5642 EXPECT_FALSE(personal_data_->ApplyDedupingRoutine()); 5816 EXPECT_FALSE(personal_data_->ApplyDedupingRoutine());
5643 5817
5644 // The two duplicate profiles should still be present. 5818 // The two duplicate profiles should still be present.
5645 EXPECT_EQ(2U, personal_data_->GetProfiles().size()); 5819 EXPECT_EQ(2U, personal_data_->GetProfiles().size());
5646 } 5820 }
5647 5821
5648 } // namespace autofill 5822 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698