OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_save_card_infobar_delegate_m
obile.h" | 5 #include "components/autofill/core/browser/autofill_save_card_infobar_delegate_m
obile.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/test/histogram_tester.h" | 10 #include "base/test/histogram_tester.h" |
11 #include "chrome/browser/autofill/personal_data_manager_factory.h" | 11 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
12 #include "chrome/browser/ui/autofill/chrome_autofill_client.h" | 12 #include "chrome/browser/ui/autofill/chrome_autofill_client.h" |
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
15 #include "components/autofill/core/browser/autofill_test_utils.h" | 15 #include "components/autofill/core/browser/autofill_test_utils.h" |
16 #include "components/autofill/core/browser/personal_data_manager.h" | 16 #include "components/autofill/core/browser/personal_data_manager.h" |
| 17 #include "components/autofill/core/common/autofill_pref_names.h" |
17 #include "components/infobars/core/confirm_infobar_delegate.h" | 18 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 19 #include "components/prefs/pref_service.h" |
| 20 #include "components/user_prefs/user_prefs.h" |
18 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
20 | 23 |
21 using testing::_; | 24 using testing::_; |
22 | 25 |
23 namespace autofill { | 26 namespace autofill { |
24 | 27 |
25 namespace { | 28 namespace { |
26 | 29 |
27 class TestPersonalDataManager : public PersonalDataManager { | 30 class TestPersonalDataManager : public PersonalDataManager { |
(...skipping 19 matching lines...) Expand all Loading... |
47 class AutofillSaveCardInfoBarDelegateMobileTest | 50 class AutofillSaveCardInfoBarDelegateMobileTest |
48 : public ChromeRenderViewHostTestHarness { | 51 : public ChromeRenderViewHostTestHarness { |
49 public: | 52 public: |
50 AutofillSaveCardInfoBarDelegateMobileTest(); | 53 AutofillSaveCardInfoBarDelegateMobileTest(); |
51 ~AutofillSaveCardInfoBarDelegateMobileTest() override; | 54 ~AutofillSaveCardInfoBarDelegateMobileTest() override; |
52 | 55 |
53 void SetUp() override; | 56 void SetUp() override; |
54 void TearDown() override; | 57 void TearDown() override; |
55 | 58 |
56 protected: | 59 protected: |
57 std::unique_ptr<ConfirmInfoBarDelegate> CreateDelegate(bool is_uploading); | 60 std::unique_ptr<ConfirmInfoBarDelegate> CreateDelegate( |
| 61 bool is_uploading, |
| 62 bool did_user_accept_previous_save_credit_card_prompt); |
58 | 63 |
59 std::unique_ptr<TestPersonalDataManager> personal_data_; | 64 std::unique_ptr<TestPersonalDataManager> personal_data_; |
60 | 65 |
61 private: | 66 private: |
| 67 // Weak reference to read & write |kAutofillAcceptSaveCreditCardPromptState|. |
| 68 PrefService* pref_service_{nullptr}; |
| 69 |
62 DISALLOW_COPY_AND_ASSIGN(AutofillSaveCardInfoBarDelegateMobileTest); | 70 DISALLOW_COPY_AND_ASSIGN(AutofillSaveCardInfoBarDelegateMobileTest); |
63 }; | 71 }; |
64 | 72 |
65 AutofillSaveCardInfoBarDelegateMobileTest:: | 73 AutofillSaveCardInfoBarDelegateMobileTest:: |
66 AutofillSaveCardInfoBarDelegateMobileTest() {} | 74 AutofillSaveCardInfoBarDelegateMobileTest() {} |
67 | 75 |
68 AutofillSaveCardInfoBarDelegateMobileTest:: | 76 AutofillSaveCardInfoBarDelegateMobileTest:: |
69 ~AutofillSaveCardInfoBarDelegateMobileTest() {} | 77 ~AutofillSaveCardInfoBarDelegateMobileTest() {} |
70 | 78 |
71 void AutofillSaveCardInfoBarDelegateMobileTest::SetUp() { | 79 void AutofillSaveCardInfoBarDelegateMobileTest::SetUp() { |
72 ChromeRenderViewHostTestHarness::SetUp(); | 80 ChromeRenderViewHostTestHarness::SetUp(); |
73 | 81 |
74 PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL); | 82 PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL); |
75 | 83 |
76 ChromeAutofillClient::CreateForWebContents(web_contents()); | 84 ChromeAutofillClient::CreateForWebContents(web_contents()); |
77 ChromeAutofillClient* autofill_client = | 85 ChromeAutofillClient* autofill_client = |
78 ChromeAutofillClient::FromWebContents(web_contents()); | 86 ChromeAutofillClient::FromWebContents(web_contents()); |
79 | 87 |
80 personal_data_.reset(new TestPersonalDataManager()); | 88 personal_data_.reset(new TestPersonalDataManager()); |
81 personal_data_->set_database(autofill_client->GetDatabase()); | 89 personal_data_->set_database(autofill_client->GetDatabase()); |
82 personal_data_->SetPrefService(profile()->GetPrefs()); | 90 personal_data_->SetPrefService(profile()->GetPrefs()); |
| 91 |
| 92 pref_service_ = |
| 93 user_prefs::UserPrefs::Get(web_contents()->GetBrowserContext()); |
| 94 pref_service_->SetBoolean(prefs::kAutofillAcceptSaveCreditCardPromptState, |
| 95 true); |
83 } | 96 } |
84 | 97 |
85 void AutofillSaveCardInfoBarDelegateMobileTest::TearDown() { | 98 void AutofillSaveCardInfoBarDelegateMobileTest::TearDown() { |
86 personal_data_.reset(); | 99 personal_data_.reset(); |
87 ChromeRenderViewHostTestHarness::TearDown(); | 100 ChromeRenderViewHostTestHarness::TearDown(); |
88 } | 101 } |
89 | 102 |
90 std::unique_ptr<ConfirmInfoBarDelegate> | 103 std::unique_ptr<ConfirmInfoBarDelegate> |
91 AutofillSaveCardInfoBarDelegateMobileTest::CreateDelegate(bool is_uploading) { | 104 AutofillSaveCardInfoBarDelegateMobileTest::CreateDelegate( |
| 105 bool is_uploading, |
| 106 bool did_user_accept_previous_save_credit_card_prompt) { |
92 base::HistogramTester histogram_tester; | 107 base::HistogramTester histogram_tester; |
93 CreditCard credit_card; | 108 CreditCard credit_card; |
94 std::unique_ptr<base::DictionaryValue> legal_message; | 109 std::unique_ptr<base::DictionaryValue> legal_message; |
95 std::unique_ptr<ConfirmInfoBarDelegate> delegate( | 110 std::unique_ptr<ConfirmInfoBarDelegate> delegate( |
96 new AutofillSaveCardInfoBarDelegateMobile( | 111 new AutofillSaveCardInfoBarDelegateMobile( |
97 is_uploading, credit_card, std::move(legal_message), | 112 is_uploading, credit_card, std::move(legal_message), |
98 base::Bind(base::IgnoreResult( | 113 base::Bind(base::IgnoreResult( |
99 &TestPersonalDataManager::SaveImportedCreditCard), | 114 &TestPersonalDataManager::SaveImportedCreditCard), |
100 base::Unretained(personal_data_.get()), credit_card))); | 115 base::Unretained(personal_data_.get()), credit_card))); |
101 std::string destination = is_uploading ? ".Server" : ".Local"; | 116 std::string destination = is_uploading ? ".Server" : ".Local"; |
102 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar" | 117 std::string previous_response = |
103 + destination, | 118 did_user_accept_previous_save_credit_card_prompt ? ".PreviouslyAccepted" |
104 AutofillMetrics::INFOBAR_SHOWN, 1); | 119 : ".PreviouslyDenied"; |
| 120 histogram_tester.ExpectUniqueSample( |
| 121 "Autofill.CreditCardInfoBar" + destination + previous_response, |
| 122 AutofillMetrics::INFOBAR_SHOWN, 1); |
105 return delegate; | 123 return delegate; |
106 } | 124 } |
107 | 125 |
108 // Test that local credit card save infobar metrics are logged correctly. | 126 // Test that local credit card save infobar metrics are logged correctly. |
109 TEST_F(AutofillSaveCardInfoBarDelegateMobileTest, Metrics_Local) { | 127 TEST_F(AutofillSaveCardInfoBarDelegateMobileTest, Metrics_Local) { |
110 ::testing::InSequence dummy; | 128 ::testing::InSequence dummy; |
111 | 129 |
112 // Accept the infobar. | 130 // Accept the infobar. |
113 { | 131 { |
114 std::unique_ptr<ConfirmInfoBarDelegate> infobar( | 132 std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate( |
115 CreateDelegate(/* is_uploading= */ false)); | 133 /* is_uploading= */ false, |
| 134 /* did_user_accept_previous_save_credit_card_prompt= */ true)); |
116 EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_)); | 135 EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_)); |
117 | 136 |
118 base::HistogramTester histogram_tester; | 137 base::HistogramTester histogram_tester; |
119 EXPECT_TRUE(infobar->Accept()); | 138 EXPECT_TRUE(infobar->Accept()); |
120 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar.Local", | 139 histogram_tester.ExpectUniqueSample( |
121 AutofillMetrics::INFOBAR_ACCEPTED, 1); | 140 "Autofill.CreditCardInfoBar.Local.PreviouslyAccepted", |
| 141 AutofillMetrics::INFOBAR_ACCEPTED, 1); |
122 } | 142 } |
123 | 143 |
124 // Cancel the infobar. | 144 // Cancel the infobar. |
125 { | 145 { |
126 std::unique_ptr<ConfirmInfoBarDelegate> infobar( | 146 std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate( |
127 CreateDelegate(/* is_uploading= */ false)); | 147 /* is_uploading= */ false, |
| 148 /* did_user_accept_previous_save_credit_card_prompt= */ true)); |
128 | 149 |
129 base::HistogramTester histogram_tester; | 150 base::HistogramTester histogram_tester; |
130 EXPECT_TRUE(infobar->Cancel()); | 151 EXPECT_TRUE(infobar->Cancel()); |
131 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar.Local", | 152 histogram_tester.ExpectUniqueSample( |
132 AutofillMetrics::INFOBAR_DENIED, 1); | 153 "Autofill.CreditCardInfoBar.Local.PreviouslyAccepted", |
| 154 AutofillMetrics::INFOBAR_DENIED, 1); |
133 } | 155 } |
134 | 156 |
135 // Dismiss the infobar. | 157 // Dismiss the infobar. |
136 { | 158 { |
137 std::unique_ptr<ConfirmInfoBarDelegate> infobar( | 159 std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate( |
138 CreateDelegate(/* is_uploading= */ false)); | 160 /* is_uploading= */ false, |
| 161 /* did_user_accept_previous_save_credit_card_prompt= */ false)); |
139 | 162 |
140 base::HistogramTester histogram_tester; | 163 base::HistogramTester histogram_tester; |
141 infobar->InfoBarDismissed(); | 164 infobar->InfoBarDismissed(); |
142 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar.Local", | 165 histogram_tester.ExpectUniqueSample( |
143 AutofillMetrics::INFOBAR_DENIED, 1); | 166 "Autofill.CreditCardInfoBar.Local.PreviouslyDenied", |
| 167 AutofillMetrics::INFOBAR_DENIED, 1); |
144 } | 168 } |
145 | 169 |
146 // Ignore the infobar. | 170 // Ignore the infobar. |
147 { | 171 { |
148 std::unique_ptr<ConfirmInfoBarDelegate> infobar( | 172 std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate( |
149 CreateDelegate(/* is_uploading= */ false)); | 173 /* is_uploading= */ false, |
150 | 174 /* did_user_accept_previous_save_credit_card_prompt= */ false)); |
151 base::HistogramTester histogram_tester; | 175 base::HistogramTester histogram_tester; |
152 infobar.reset(); | 176 infobar.reset(); |
153 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar.Local", | 177 histogram_tester.ExpectUniqueSample( |
154 AutofillMetrics::INFOBAR_IGNORED, 1); | 178 "Autofill.CreditCardInfoBar.Local.PreviouslyDenied", |
| 179 AutofillMetrics::INFOBAR_IGNORED, 1); |
155 } | 180 } |
156 } | 181 } |
157 | 182 |
158 // Test that server credit card save infobar metrics are logged correctly. | 183 // Test that server credit card save infobar metrics are logged correctly. |
159 TEST_F(AutofillSaveCardInfoBarDelegateMobileTest, Metrics_Server) { | 184 TEST_F(AutofillSaveCardInfoBarDelegateMobileTest, Metrics_Server) { |
160 ::testing::InSequence dummy; | 185 ::testing::InSequence dummy; |
161 | 186 |
162 // Accept the infobar. | 187 // Accept the infobar. |
163 { | 188 { |
164 std::unique_ptr<ConfirmInfoBarDelegate> infobar( | 189 std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate( |
165 CreateDelegate(/* is_uploading= */ true)); | 190 /* is_uploading= */ true, |
| 191 /* did_user_accept_previous_save_credit_card_prompt= */ true)); |
166 EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_)); | 192 EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_)); |
167 | 193 |
168 base::HistogramTester histogram_tester; | 194 base::HistogramTester histogram_tester; |
169 EXPECT_TRUE(infobar->Accept()); | 195 EXPECT_TRUE(infobar->Accept()); |
170 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar.Server", | 196 histogram_tester.ExpectUniqueSample( |
171 AutofillMetrics::INFOBAR_ACCEPTED, 1); | 197 "Autofill.CreditCardInfoBar.Server.PreviouslyAccepted", |
| 198 AutofillMetrics::INFOBAR_ACCEPTED, 1); |
172 } | 199 } |
173 | 200 |
174 // Cancel the infobar. | 201 // Cancel the infobar. |
175 { | 202 { |
176 std::unique_ptr<ConfirmInfoBarDelegate> infobar( | 203 std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate( |
177 CreateDelegate(/* is_uploading= */ true)); | 204 /* is_uploading= */ true, |
| 205 /* did_user_accept_previous_save_credit_card_prompt= */ true)); |
178 | 206 |
179 base::HistogramTester histogram_tester; | 207 base::HistogramTester histogram_tester; |
180 EXPECT_TRUE(infobar->Cancel()); | 208 EXPECT_TRUE(infobar->Cancel()); |
181 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar.Server", | 209 histogram_tester.ExpectUniqueSample( |
182 AutofillMetrics::INFOBAR_DENIED, 1); | 210 "Autofill.CreditCardInfoBar.Server.PreviouslyAccepted", |
| 211 AutofillMetrics::INFOBAR_DENIED, 1); |
183 } | 212 } |
184 | 213 |
185 // Dismiss the infobar. | 214 // Dismiss the infobar. |
186 { | 215 { |
187 std::unique_ptr<ConfirmInfoBarDelegate> infobar( | 216 std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate( |
188 CreateDelegate(/* is_uploading= */ true)); | 217 /* is_uploading= */ true, |
| 218 /* did_user_accept_previous_save_credit_card_prompt= */ true)); |
189 | 219 |
190 base::HistogramTester histogram_tester; | 220 base::HistogramTester histogram_tester; |
191 infobar->InfoBarDismissed(); | 221 infobar->InfoBarDismissed(); |
192 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar.Server", | 222 histogram_tester.ExpectUniqueSample( |
193 AutofillMetrics::INFOBAR_DENIED, 1); | 223 "Autofill.CreditCardInfoBar.Server.PreviouslyAccepted", |
| 224 AutofillMetrics::INFOBAR_DENIED, 1); |
194 } | 225 } |
195 | 226 |
196 // Ignore the infobar. | 227 // Ignore the infobar. |
197 { | 228 { |
198 std::unique_ptr<ConfirmInfoBarDelegate> infobar( | 229 std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate( |
199 CreateDelegate(/* is_uploading= */ true)); | 230 /* is_uploading= */ true, |
| 231 /* did_user_accept_previous_save_credit_card_prompt= */ false)); |
200 | 232 |
201 base::HistogramTester histogram_tester; | 233 base::HistogramTester histogram_tester; |
202 infobar.reset(); | 234 infobar.reset(); |
203 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar.Server", | 235 histogram_tester.ExpectUniqueSample( |
204 AutofillMetrics::INFOBAR_IGNORED, 1); | 236 "Autofill.CreditCardInfoBar.Server.PreviouslyDenied", |
| 237 AutofillMetrics::INFOBAR_IGNORED, 1); |
205 } | 238 } |
206 } | 239 } |
207 | 240 |
208 } // namespace autofill | 241 } // namespace autofill |
OLD | NEW |