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

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

Issue 2146823003: Autofill Credit Card Signin Promo: Put behind Feature (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed comments Created 4 years, 5 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/autofill_manager.h" 5 #include "components/autofill/core/browser/autofill_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <memory> 10 #include <memory>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/feature_list.h"
14 #include "base/format_macros.h" 15 #include "base/format_macros.h"
15 #include "base/macros.h" 16 #include "base/macros.h"
16 #include "base/memory/ref_counted.h" 17 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_vector.h" 18 #include "base/memory/scoped_vector.h"
19 #include "base/metrics/field_trial.h"
18 #include "base/run_loop.h" 20 #include "base/run_loop.h"
19 #include "base/strings/string16.h" 21 #include "base/strings/string16.h"
20 #include "base/strings/string_number_conversions.h" 22 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_util.h" 23 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h" 24 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h" 25 #include "base/strings/utf_string_conversions.h"
24 #include "base/test/histogram_tester.h" 26 #include "base/test/histogram_tester.h"
25 #include "base/threading/thread_task_runner_handle.h" 27 #include "base/threading/thread_task_runner_handle.h"
26 #include "base/time/time.h" 28 #include "base/time/time.h"
27 #include "build/build_config.h" 29 #include "build/build_config.h"
28 #include "components/autofill/core/browser/autocomplete_history_manager.h" 30 #include "components/autofill/core/browser/autocomplete_history_manager.h"
29 #include "components/autofill/core/browser/autofill_download_manager.h" 31 #include "components/autofill/core/browser/autofill_download_manager.h"
32 #include "components/autofill/core/browser/autofill_experiments.h"
30 #include "components/autofill/core/browser/autofill_profile.h" 33 #include "components/autofill/core/browser/autofill_profile.h"
31 #include "components/autofill/core/browser/autofill_test_utils.h" 34 #include "components/autofill/core/browser/autofill_test_utils.h"
32 #include "components/autofill/core/browser/credit_card.h" 35 #include "components/autofill/core/browser/credit_card.h"
33 #include "components/autofill/core/browser/personal_data_manager.h" 36 #include "components/autofill/core/browser/personal_data_manager.h"
34 #include "components/autofill/core/browser/popup_item_ids.h" 37 #include "components/autofill/core/browser/popup_item_ids.h"
35 #include "components/autofill/core/browser/test_autofill_client.h" 38 #include "components/autofill/core/browser/test_autofill_client.h"
36 #include "components/autofill/core/browser/test_autofill_driver.h" 39 #include "components/autofill/core/browser/test_autofill_driver.h"
37 #include "components/autofill/core/browser/test_autofill_external_delegate.h" 40 #include "components/autofill/core/browser/test_autofill_external_delegate.h"
38 #include "components/autofill/core/browser/validation.h" 41 #include "components/autofill/core/browser/validation.h"
39 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" 42 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
40 #include "components/autofill/core/common/autofill_pref_names.h" 43 #include "components/autofill/core/common/autofill_pref_names.h"
41 #include "components/autofill/core/common/autofill_switches.h" 44 #include "components/autofill/core/common/autofill_switches.h"
42 #include "components/autofill/core/common/autofill_util.h" 45 #include "components/autofill/core/common/autofill_util.h"
43 #include "components/autofill/core/common/form_data.h" 46 #include "components/autofill/core/common/form_data.h"
44 #include "components/autofill/core/common/form_field_data.h" 47 #include "components/autofill/core/common/form_field_data.h"
45 #include "components/prefs/pref_service.h" 48 #include "components/prefs/pref_service.h"
46 #include "components/rappor/test_rappor_service.h" 49 #include "components/rappor/test_rappor_service.h"
50 #include "components/variations/variations_associated_data.h"
47 #include "grit/components_strings.h" 51 #include "grit/components_strings.h"
48 #include "net/url_request/url_request_test_util.h" 52 #include "net/url_request/url_request_test_util.h"
49 #include "testing/gmock/include/gmock/gmock.h" 53 #include "testing/gmock/include/gmock/gmock.h"
50 #include "testing/gtest/include/gtest/gtest.h" 54 #include "testing/gtest/include/gtest/gtest.h"
51 #include "ui/base/l10n/l10n_util.h" 55 #include "ui/base/l10n/l10n_util.h"
52 #include "ui/gfx/geometry/rect.h" 56 #include "ui/gfx/geometry/rect.h"
53 #include "url/gurl.h" 57 #include "url/gurl.h"
54 58
55 using base::ASCIIToUTF16; 59 using base::ASCIIToUTF16;
56 using base::UTF8ToUTF16; 60 using base::UTF8ToUTF16;
(...skipping 667 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 // The results returned by the most recent Autofill query. 728 // The results returned by the most recent Autofill query.
725 std::vector<Suggestion> suggestions_; 729 std::vector<Suggestion> suggestions_;
726 730
727 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate); 731 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate);
728 }; 732 };
729 733
730 } // namespace 734 } // namespace
731 735
732 class AutofillManagerTest : public testing::Test { 736 class AutofillManagerTest : public testing::Test {
733 public: 737 public:
738 AutofillManagerTest() : field_trial_list_(nullptr) {}
739
734 void SetUp() override { 740 void SetUp() override {
735 autofill_client_.SetPrefs(test::PrefServiceForTesting()); 741 autofill_client_.SetPrefs(test::PrefServiceForTesting());
736 personal_data_.set_database(autofill_client_.GetDatabase()); 742 personal_data_.set_database(autofill_client_.GetDatabase());
737 personal_data_.SetPrefService(autofill_client_.GetPrefs()); 743 personal_data_.SetPrefService(autofill_client_.GetPrefs());
738 autofill_driver_.reset(new MockAutofillDriver()); 744 autofill_driver_.reset(new MockAutofillDriver());
739 request_context_ = new net::TestURLRequestContextGetter( 745 request_context_ = new net::TestURLRequestContextGetter(
740 base::ThreadTaskRunnerHandle::Get()); 746 base::ThreadTaskRunnerHandle::Get());
741 autofill_driver_->SetURLRequestContext(request_context_.get()); 747 autofill_driver_->SetURLRequestContext(request_context_.get());
742 autofill_manager_.reset(new TestAutofillManager( 748 autofill_manager_.reset(new TestAutofillManager(
743 autofill_driver_.get(), &autofill_client_, &personal_data_)); 749 autofill_driver_.get(), &autofill_client_, &personal_data_));
744 download_manager_ = new TestAutofillDownloadManager( 750 download_manager_ = new TestAutofillDownloadManager(
745 autofill_driver_.get(), autofill_manager_.get()); 751 autofill_driver_.get(), autofill_manager_.get());
746 // AutofillManager takes ownership of |download_manager_|. 752 // AutofillManager takes ownership of |download_manager_|.
747 autofill_manager_->set_download_manager(download_manager_); 753 autofill_manager_->set_download_manager(download_manager_);
748 external_delegate_.reset(new TestAutofillExternalDelegate( 754 external_delegate_.reset(new TestAutofillExternalDelegate(
749 autofill_manager_.get(), 755 autofill_manager_.get(),
750 autofill_driver_.get())); 756 autofill_driver_.get()));
751 autofill_manager_->SetExternalDelegate(external_delegate_.get()); 757 autofill_manager_->SetExternalDelegate(external_delegate_.get());
758
759 // Clear all the things.
760 base::FeatureList::ClearInstanceForTesting();
761 variations::testing::ClearAllVariationParams();
752 } 762 }
753 763
754 void TearDown() override { 764 void TearDown() override {
755 // Order of destruction is important as AutofillManager relies on 765 // Order of destruction is important as AutofillManager relies on
756 // PersonalDataManager to be around when it gets destroyed. 766 // PersonalDataManager to be around when it gets destroyed.
757 autofill_manager_.reset(); 767 autofill_manager_.reset();
758 autofill_driver_.reset(); 768 autofill_driver_.reset();
759 769
760 // Remove the AutofillWebDataService so TestPersonalDataManager does not 770 // Remove the AutofillWebDataService so TestPersonalDataManager does not
761 // need to care about removing self as an observer in destruction. 771 // need to care about removing self as an observer in destruction.
762 personal_data_.set_database(scoped_refptr<AutofillWebDataService>(NULL)); 772 personal_data_.set_database(scoped_refptr<AutofillWebDataService>(NULL));
763 personal_data_.SetPrefService(NULL); 773 personal_data_.SetPrefService(NULL);
764 774
765 request_context_ = nullptr; 775 request_context_ = nullptr;
766 } 776 }
767 777
778 void EnableCreditCardSigninPromoFeatureWithLimit(int impression_limit) {
779 const std::string kTrialName = "MyTrial";
780 const std::string kGroupName = "Group1";
781
782 scoped_refptr<base::FieldTrial> trial(
783 base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName));
784 std::map<std::string, std::string> params;
785 params[kCreditCardSigninPromoImpressionLimitParamKey] =
786 base::IntToString(impression_limit);
787 ASSERT_TRUE(
788 variations::AssociateVariationParams(kTrialName, kGroupName, params));
789
790 // Enable the feature.
791 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
792 feature_list->RegisterFieldTrialOverride(
793 kAutofillCreditCardSigninPromo.name,
794 base::FeatureList::OVERRIDE_ENABLE_FEATURE, trial.get());
795 base::FeatureList::SetInstance(std::move(feature_list));
796
797 // Double-checking our params made it.
798 std::map<std::string, std::string> actualParams;
799 EXPECT_TRUE(variations::GetVariationParamsByFeature(
800 kAutofillCreditCardSigninPromo, &actualParams));
801 EXPECT_EQ(params, actualParams);
802 }
803
768 void GetAutofillSuggestions(int query_id, 804 void GetAutofillSuggestions(int query_id,
769 const FormData& form, 805 const FormData& form,
770 const FormFieldData& field) { 806 const FormFieldData& field) {
771 autofill_manager_->OnQueryFormFieldAutofill(query_id, form, field, 807 autofill_manager_->OnQueryFormFieldAutofill(query_id, form, field,
772 gfx::RectF()); 808 gfx::RectF());
773 } 809 }
774 810
775 void GetAutofillSuggestions(const FormData& form, 811 void GetAutofillSuggestions(const FormData& form,
776 const FormFieldData& field) { 812 const FormFieldData& field) {
777 GetAutofillSuggestions(kDefaultPageID, form, field); 813 GetAutofillSuggestions(kDefaultPageID, form, field);
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
949 protected: 985 protected:
950 base::MessageLoop message_loop_; 986 base::MessageLoop message_loop_;
951 MockAutofillClient autofill_client_; 987 MockAutofillClient autofill_client_;
952 std::unique_ptr<MockAutofillDriver> autofill_driver_; 988 std::unique_ptr<MockAutofillDriver> autofill_driver_;
953 std::unique_ptr<TestAutofillManager> autofill_manager_; 989 std::unique_ptr<TestAutofillManager> autofill_manager_;
954 std::unique_ptr<TestAutofillExternalDelegate> external_delegate_; 990 std::unique_ptr<TestAutofillExternalDelegate> external_delegate_;
955 scoped_refptr<net::TestURLRequestContextGetter> request_context_; 991 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
956 TestPaymentsClient* payments_client_; 992 TestPaymentsClient* payments_client_;
957 TestAutofillDownloadManager* download_manager_; 993 TestAutofillDownloadManager* download_manager_;
958 TestPersonalDataManager personal_data_; 994 TestPersonalDataManager personal_data_;
995 base::FieldTrialList field_trial_list_;
959 }; 996 };
960 997
961 class TestFormStructure : public FormStructure { 998 class TestFormStructure : public FormStructure {
962 public: 999 public:
963 explicit TestFormStructure(const FormData& form) 1000 explicit TestFormStructure(const FormData& form)
964 : FormStructure(form) {} 1001 : FormStructure(form) {}
965 ~TestFormStructure() override {} 1002 ~TestFormStructure() override {}
966 1003
967 void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types, 1004 void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types,
968 const std::vector<ServerFieldType>& server_types) { 1005 const std::vector<ServerFieldType>& server_types) {
(...skipping 3934 matching lines...) Expand 10 before | Expand all | Expand 10 after
4903 4940
4904 FormFieldData field; 4941 FormFieldData field;
4905 test::CreateTestFormField("Name on Card", "nameoncard", "lvis", "text", 4942 test::CreateTestFormField("Name on Card", "nameoncard", "lvis", "text",
4906 &field); 4943 &field);
4907 GetAutofillSuggestions(form, field); 4944 GetAutofillSuggestions(form, field);
4908 // Autocomplete suggestions are queried, but not Autofill. 4945 // Autocomplete suggestions are queried, but not Autofill.
4909 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen()); 4946 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
4910 } 4947 }
4911 4948
4912 // Test that ShouldShowCreditCardSigninPromo behaves as expected for a credit 4949 // Test that ShouldShowCreditCardSigninPromo behaves as expected for a credit
4913 // card form. 4950 // card form, with no impression limit and the feature enabled.
4914 TEST_F(AutofillManagerTest, ShouldShowCreditCardSigninPromo_CreditCardField) { 4951 TEST_F(AutofillManagerTest,
4952 ShouldShowCreditCardSigninPromo_CreditCardField_NoLimit) {
4953 // Enable the feature with no impression limit.
4954 EnableCreditCardSigninPromoFeatureWithLimit(0);
4955
4915 // Set up our form data. 4956 // Set up our form data.
4916 FormData form; 4957 FormData form;
4917 CreateTestCreditCardFormData(&form, true, false); 4958 CreateTestCreditCardFormData(&form, true, false);
4959 std::vector<FormData> forms(1, form);
4960 FormsSeen(forms);
4961
4962 FormFieldData field;
4963 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field);
4964
4965 // The result will depend on what ShouldShowSigninPromo(). We control its
4966 // output and verify that both cases behave as expected.
4967 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo())
4968 .WillOnce(testing::Return(true));
4969 EXPECT_TRUE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
4970
4971 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo())
4972 .WillOnce(testing::Return(false));
4973 EXPECT_FALSE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
4974 }
4975
4976 // Test that ShouldShowCreditCardSigninPromo doesn't show for a credit card form
4977 // when the feature is off.
4978 TEST_F(AutofillManagerTest,
4979 ShouldShowCreditCardSigninPromo_CreditCardField_FeatureOff) {
4980 // Set up our form data.
4981 FormData form;
4982 CreateTestCreditCardFormData(&form, true, false);
4983 std::vector<FormData> forms(1, form);
4984 FormsSeen(forms);
4985
4986 FormFieldData field;
4987 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field);
4988
4989 // Returns false without calling ShouldShowSigninPromo().
4990 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo()).Times(0);
4991 EXPECT_FALSE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
4992 }
4993
4994 // Test that ShouldShowCreditCardSigninPromo behaves as expected for a credit
4995 // card form with an impression limit and no impressions yet.
4996 TEST_F(AutofillManagerTest,
4997 ShouldShowCreditCardSigninPromo_CreditCardField_UnmetLimit) {
4998 // Enable the feature with an impression limit.
4999 EnableCreditCardSigninPromoFeatureWithLimit(10);
5000 // No impressions yet.
5001 ASSERT_EQ(0, autofill_client_.GetPrefs()->GetInteger(
5002 prefs::kAutofillCreditCardSigninPromoImpressionCount));
5003
5004 // Set up our form data.
5005 FormData form;
5006 CreateTestCreditCardFormData(&form, true, false);
4918 std::vector<FormData> forms(1, form); 5007 std::vector<FormData> forms(1, form);
4919 FormsSeen(forms); 5008 FormsSeen(forms);
4920 5009
4921 FormFieldData field; 5010 FormFieldData field;
4922 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field); 5011 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field);
4923 5012
5013 // The mock implementation of ShouldShowSigninPromo() will return true here,
5014 // creating an impression, and false below, preventing an impression.
4924 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo()) 5015 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo())
4925 .WillOnce(testing::Return(true)); 5016 .WillOnce(testing::Return(true));
4926 EXPECT_TRUE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field)); 5017 EXPECT_TRUE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
4927 5018
5019 // Expect to now have an impression.
5020 EXPECT_EQ(1, autofill_client_.GetPrefs()->GetInteger(
5021 prefs::kAutofillCreditCardSigninPromoImpressionCount));
5022
4928 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo()) 5023 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo())
4929 .WillOnce(testing::Return(false)); 5024 .WillOnce(testing::Return(false));
4930 EXPECT_FALSE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field)); 5025 EXPECT_FALSE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
5026
5027 // No additional impression.
5028 EXPECT_EQ(1, autofill_client_.GetPrefs()->GetInteger(
5029 prefs::kAutofillCreditCardSigninPromoImpressionCount));
5030 }
5031
5032 // Test that ShouldShowCreditCardSigninPromo behaves as expected for a credit
5033 // card form with an impression limit that has been attained already.
5034 TEST_F(AutofillManagerTest,
5035 ShouldShowCreditCardSigninPromo_CreditCardField_WithAttainedLimit) {
5036 // Enable the feature with an impression limit.
5037 EnableCreditCardSigninPromoFeatureWithLimit(10);
5038
5039 // Set up our form data.
5040 FormData form;
5041 CreateTestCreditCardFormData(&form, true, false);
5042 std::vector<FormData> forms(1, form);
5043 FormsSeen(forms);
5044
5045 FormFieldData field;
5046 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field);
5047
5048 // Set the impression count to the same value as the limit.
5049 autofill_client_.GetPrefs()->SetInteger(
5050 prefs::kAutofillCreditCardSigninPromoImpressionCount, 10);
5051
5052 // Both calls will now return false, regardless of the mock implementation of
5053 // ShouldShowSigninPromo().
5054 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo())
5055 .WillOnce(testing::Return(true));
5056 EXPECT_FALSE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
5057
5058 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo())
5059 .WillOnce(testing::Return(false));
5060 EXPECT_FALSE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
5061
5062 // Number of impressions stay the same.
5063 EXPECT_EQ(10, autofill_client_.GetPrefs()->GetInteger(
5064 prefs::kAutofillCreditCardSigninPromoImpressionCount));
4931 } 5065 }
4932 5066
4933 // Test that ShouldShowCreditCardSigninPromo behaves as expected for an address 5067 // Test that ShouldShowCreditCardSigninPromo behaves as expected for an address
4934 // form. 5068 // form.
4935 TEST_F(AutofillManagerTest, ShouldShowCreditCardSigninPromo_AddressField) { 5069 TEST_F(AutofillManagerTest, ShouldShowCreditCardSigninPromo_AddressField) {
4936 // Set up our form data. 5070 // Set up our form data.
4937 FormData form; 5071 FormData form;
4938 test::CreateTestAddressFormData(&form); 5072 test::CreateTestAddressFormData(&form);
4939 std::vector<FormData> forms(1, form); 5073 std::vector<FormData> forms(1, form);
4940 FormsSeen(forms); 5074 FormsSeen(forms);
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
5116 FormsSeen(mixed_forms); 5250 FormsSeen(mixed_forms);
5117 5251
5118 // Suggestions should always be displayed. 5252 // Suggestions should always be displayed.
5119 for (const FormFieldData& field : mixed_form.fields) { 5253 for (const FormFieldData& field : mixed_form.fields) {
5120 GetAutofillSuggestions(mixed_form, field); 5254 GetAutofillSuggestions(mixed_form, field);
5121 EXPECT_TRUE(external_delegate_->on_suggestions_returned_seen()); 5255 EXPECT_TRUE(external_delegate_->on_suggestions_returned_seen());
5122 } 5256 }
5123 } 5257 }
5124 5258
5125 } // namespace autofill 5259 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/autofill_manager.cc ('k') | components/autofill/core/common/autofill_pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698