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

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: Initial 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, ShouldShowCreditCardSigninPromo_CreditCardField) {
sebsg 2016/07/14 15:32:13 Nit: add _NoLimit as a suffix to be more consisten
Mathieu 2016/07/14 21:51:44 Done.
4952 // Enable the feature with no impression limit.
4953 EnableCreditCardSigninPromoFeatureWithLimit(0);
4954
4915 // Set up our form data. 4955 // Set up our form data.
4916 FormData form; 4956 FormData form;
4917 CreateTestCreditCardFormData(&form, true, false); 4957 CreateTestCreditCardFormData(&form, true, false);
4918 std::vector<FormData> forms(1, form); 4958 std::vector<FormData> forms(1, form);
4919 FormsSeen(forms); 4959 FormsSeen(forms);
4920 4960
4921 FormFieldData field; 4961 FormFieldData field;
4922 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field); 4962 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field);
4923 4963
4924 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo()) 4964 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo())
4925 .WillOnce(testing::Return(true)); 4965 .WillOnce(testing::Return(true));
4926 EXPECT_TRUE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field)); 4966 EXPECT_TRUE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
4927 4967
4928 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo()) 4968 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo())
4929 .WillOnce(testing::Return(false)); 4969 .WillOnce(testing::Return(false));
4930 EXPECT_FALSE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field)); 4970 EXPECT_FALSE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
4931 } 4971 }
4932 4972
4973 // Test that ShouldShowCreditCardSigninPromo doesn't show for a credit card form
4974 // when the feature is off.
4975 TEST_F(AutofillManagerTest,
4976 ShouldShowCreditCardSigninPromo_CreditCardField_FeatureOff) {
4977 // Set up our form data.
4978 FormData form;
4979 CreateTestCreditCardFormData(&form, true, false);
4980 std::vector<FormData> forms(1, form);
4981 FormsSeen(forms);
4982
4983 FormFieldData field;
4984 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field);
4985
4986 // Returns false without calling ShouldShowSigninPromo().
4987 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo()).Times(0);
4988 EXPECT_FALSE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
4989 }
4990
4991 // Test that ShouldShowCreditCardSigninPromo behaves as expected for a credit
4992 // card form with an impression limit and no impressions yet.
4993 TEST_F(AutofillManagerTest,
4994 ShouldShowCreditCardSigninPromo_CreditCardField_WithUnmetLimit) {
sebsg 2016/07/14 15:32:13 Nit: I think ...Field_UnmetLimit gives the same in
Mathieu 2016/07/14 21:51:44 Done.
4995 // Enable the feature with an impression limit.
4996 EnableCreditCardSigninPromoFeatureWithLimit(10);
4997 // No impressions yet.
4998 ASSERT_EQ(0, autofill_client_.GetPrefs()->GetInteger(
4999 prefs::kAutofillCreditCardSigninPromoImpressionCount));
5000
5001 // Set up our form data.
5002 FormData form;
5003 CreateTestCreditCardFormData(&form, true, false);
5004 std::vector<FormData> forms(1, form);
5005 FormsSeen(forms);
5006
5007 FormFieldData field;
5008 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field);
5009
5010 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo())
5011 .WillOnce(testing::Return(true));
5012 EXPECT_TRUE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
5013
5014 // Expect to now have an impression.
5015 EXPECT_EQ(1, autofill_client_.GetPrefs()->GetInteger(
5016 prefs::kAutofillCreditCardSigninPromoImpressionCount));
5017
5018 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo())
5019 .WillOnce(testing::Return(false));
sebsg 2016/07/14 15:32:13 Could you clarify that you are testing 2 separate
Mathieu 2016/07/14 21:51:44 Done.
5020 EXPECT_FALSE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
5021
5022 // No additional impression.
5023 EXPECT_EQ(1, autofill_client_.GetPrefs()->GetInteger(
5024 prefs::kAutofillCreditCardSigninPromoImpressionCount));
5025 }
5026
5027 // Test that ShouldShowCreditCardSigninPromo behaves as expected for a credit
5028 // card form with an impression limit that has been attained already.
5029 TEST_F(AutofillManagerTest,
5030 ShouldShowCreditCardSigninPromo_CreditCardField_WithAttainedLimit) {
5031 // Enable the feature with an impression limit.
5032 EnableCreditCardSigninPromoFeatureWithLimit(10);
5033
5034 // Set up our form data.
5035 FormData form;
5036 CreateTestCreditCardFormData(&form, true, false);
5037 std::vector<FormData> forms(1, form);
5038 FormsSeen(forms);
5039
5040 FormFieldData field;
5041 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field);
5042
5043 // Set the impression count to the same value as the limit.
5044 autofill_client_.GetPrefs()->SetInteger(
5045 prefs::kAutofillCreditCardSigninPromoImpressionCount, 10);
5046
5047 // Both calls will now return false.
5048 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo())
5049 .WillOnce(testing::Return(true));
5050 EXPECT_FALSE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
5051
5052 EXPECT_CALL(autofill_client_, ShouldShowSigninPromo())
5053 .WillOnce(testing::Return(false));
5054 EXPECT_FALSE(autofill_manager_->ShouldShowCreditCardSigninPromo(form, field));
5055
5056 // Number of impressions stay the same.
5057 EXPECT_EQ(10, autofill_client_.GetPrefs()->GetInteger(
5058 prefs::kAutofillCreditCardSigninPromoImpressionCount));
5059 }
5060
4933 // Test that ShouldShowCreditCardSigninPromo behaves as expected for an address 5061 // Test that ShouldShowCreditCardSigninPromo behaves as expected for an address
4934 // form. 5062 // form.
4935 TEST_F(AutofillManagerTest, ShouldShowCreditCardSigninPromo_AddressField) { 5063 TEST_F(AutofillManagerTest, ShouldShowCreditCardSigninPromo_AddressField) {
4936 // Set up our form data. 5064 // Set up our form data.
4937 FormData form; 5065 FormData form;
4938 test::CreateTestAddressFormData(&form); 5066 test::CreateTestAddressFormData(&form);
4939 std::vector<FormData> forms(1, form); 5067 std::vector<FormData> forms(1, form);
4940 FormsSeen(forms); 5068 FormsSeen(forms);
4941 5069
4942 FormFieldData field; 5070 FormFieldData field;
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
5116 FormsSeen(mixed_forms); 5244 FormsSeen(mixed_forms);
5117 5245
5118 // Suggestions should always be displayed. 5246 // Suggestions should always be displayed.
5119 for (const FormFieldData& field : mixed_form.fields) { 5247 for (const FormFieldData& field : mixed_form.fields) {
5120 GetAutofillSuggestions(mixed_form, field); 5248 GetAutofillSuggestions(mixed_form, field);
5121 EXPECT_TRUE(external_delegate_->on_suggestions_returned_seen()); 5249 EXPECT_TRUE(external_delegate_->on_suggestions_returned_seen());
5122 } 5250 }
5123 } 5251 }
5124 5252
5125 } // namespace autofill 5253 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698