forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautofill_save_card_infobar_delegate_mobile_unittest.cc
334 lines (280 loc) · 11.6 KB
/
autofill_save_card_infobar_delegate_mobile_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/core/browser/autofill_save_card_infobar_delegate_mobile.h"
#include <memory>
#include "base/json/json_reader.h"
#include "base/macros.h"
#include "base/test/histogram_tester.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "components/prefs/pref_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
namespace autofill {
namespace {
class TestPersonalDataManager : public PersonalDataManager {
public:
TestPersonalDataManager() : PersonalDataManager("en-US") {}
using PersonalDataManager::set_database;
using PersonalDataManager::SetPrefService;
// Overridden to avoid a trip to the database.
void LoadProfiles() override {}
void LoadCreditCards() override {}
MOCK_METHOD1(SaveImportedCreditCard,
std::string(const CreditCard& imported_credit_card));
private:
DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
};
} // namespace
class AutofillSaveCardInfoBarDelegateMobileTest
: public ChromeRenderViewHostTestHarness {
public:
AutofillSaveCardInfoBarDelegateMobileTest();
~AutofillSaveCardInfoBarDelegateMobileTest() override;
void SetUp() override;
void TearDown() override;
protected:
std::unique_ptr<ConfirmInfoBarDelegate> CreateDelegate(
bool is_uploading,
prefs::PreviousSaveCreditCardPromptUserDecision
previous_save_credit_card_prompt_user_decision);
std::unique_ptr<ConfirmInfoBarDelegate> CreateDelegateWithLegalMessage(
bool is_uploading,
std::string legal_message_string,
prefs::PreviousSaveCreditCardPromptUserDecision
previous_save_credit_card_prompt_user_decision);
std::unique_ptr<TestPersonalDataManager> personal_data_;
private:
DISALLOW_COPY_AND_ASSIGN(AutofillSaveCardInfoBarDelegateMobileTest);
};
AutofillSaveCardInfoBarDelegateMobileTest::
AutofillSaveCardInfoBarDelegateMobileTest() {}
AutofillSaveCardInfoBarDelegateMobileTest::
~AutofillSaveCardInfoBarDelegateMobileTest() {}
void AutofillSaveCardInfoBarDelegateMobileTest::SetUp() {
ChromeRenderViewHostTestHarness::SetUp();
PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL);
ChromeAutofillClient::CreateForWebContents(web_contents());
ChromeAutofillClient* autofill_client =
ChromeAutofillClient::FromWebContents(web_contents());
personal_data_.reset(new TestPersonalDataManager());
personal_data_->set_database(autofill_client->GetDatabase());
personal_data_->SetPrefService(profile()->GetPrefs());
profile()->GetPrefs()->SetInteger(
prefs::kAutofillAcceptSaveCreditCardPromptState,
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_NONE);
}
void AutofillSaveCardInfoBarDelegateMobileTest::TearDown() {
personal_data_.reset();
ChromeRenderViewHostTestHarness::TearDown();
}
std::unique_ptr<ConfirmInfoBarDelegate>
AutofillSaveCardInfoBarDelegateMobileTest::CreateDelegate(
bool is_uploading,
prefs::PreviousSaveCreditCardPromptUserDecision
previous_save_credit_card_prompt_user_decision) {
return CreateDelegateWithLegalMessage(
is_uploading, "", previous_save_credit_card_prompt_user_decision);
}
std::unique_ptr<ConfirmInfoBarDelegate>
AutofillSaveCardInfoBarDelegateMobileTest::CreateDelegateWithLegalMessage(
bool is_uploading,
std::string legal_message_string,
prefs::PreviousSaveCreditCardPromptUserDecision
previous_save_credit_card_prompt_user_decision) {
CreditCard credit_card;
std::unique_ptr<base::DictionaryValue> legal_message;
if (!legal_message_string.empty()) {
std::unique_ptr<base::Value> value(
base::JSONReader::Read(legal_message_string));
EXPECT_TRUE(value);
base::DictionaryValue* dictionary;
EXPECT_TRUE(value->GetAsDictionary(&dictionary));
legal_message = dictionary->CreateDeepCopy();
}
std::unique_ptr<ConfirmInfoBarDelegate> delegate(
new AutofillSaveCardInfoBarDelegateMobile(
is_uploading, credit_card, std::move(legal_message),
base::Bind(base::IgnoreResult(
&TestPersonalDataManager::SaveImportedCreditCard),
base::Unretained(personal_data_.get()), credit_card),
profile()->GetPrefs()));
std::string destination = is_uploading ? ".Server" : ".Local";
std::string previous_response;
switch (previous_save_credit_card_prompt_user_decision) {
case prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_ACCEPTED:
previous_response = ".PreviouslyAccepted";
break;
case prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_DENIED:
previous_response = ".PreviouslyDenied";
break;
default:
EXPECT_EQ(previous_save_credit_card_prompt_user_decision,
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_NONE);
break;
}
return delegate;
}
// Test that local credit card save infobar metrics are logged correctly.
TEST_F(AutofillSaveCardInfoBarDelegateMobileTest, Metrics_Local) {
::testing::InSequence dummy;
// Infobar is shown.
{
base::HistogramTester histogram_tester;
std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
/* is_uploading= */ false,
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_NONE));
histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar.Local",
AutofillMetrics::INFOBAR_SHOWN, 1);
}
// Accept the infobar.
{
std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
/* is_uploading= */ false,
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_DENIED));
EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_));
base::HistogramTester histogram_tester;
EXPECT_TRUE(infobar->Accept());
histogram_tester.ExpectUniqueSample(
"Autofill.CreditCardInfoBar.Local.PreviouslyDenied",
AutofillMetrics::INFOBAR_ACCEPTED, 1);
}
// Cancel the infobar.
{
std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
/* is_uploading= */ false,
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_ACCEPTED));
base::HistogramTester histogram_tester;
EXPECT_TRUE(infobar->Cancel());
histogram_tester.ExpectUniqueSample(
"Autofill.CreditCardInfoBar.Local.PreviouslyAccepted",
AutofillMetrics::INFOBAR_DENIED, 1);
}
// Dismiss the infobar.
{
std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
/* is_uploading= */ false,
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_DENIED));
base::HistogramTester histogram_tester;
infobar->InfoBarDismissed();
histogram_tester.ExpectUniqueSample(
"Autofill.CreditCardInfoBar.Local.PreviouslyDenied",
AutofillMetrics::INFOBAR_DENIED, 1);
}
// Ignore the infobar.
{
std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
/* is_uploading= */ false,
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_DENIED));
base::HistogramTester histogram_tester;
infobar.reset();
histogram_tester.ExpectUniqueSample(
"Autofill.CreditCardInfoBar.Local.PreviouslyDenied",
AutofillMetrics::INFOBAR_IGNORED, 1);
}
}
// Test that server credit card save infobar metrics are logged correctly.
TEST_F(AutofillSaveCardInfoBarDelegateMobileTest, Metrics_Server) {
::testing::InSequence dummy;
// Infobar is shown.
{
base::HistogramTester histogram_tester;
std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
/* is_uploading= */ true,
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_NONE));
histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar.Server",
AutofillMetrics::INFOBAR_SHOWN, 1);
}
// Infobar is still shown when the legal message is successfully parsed.
{
base::HistogramTester histogram_tester;
std::string good_legal_message =
"{"
" \"line\" : [ {"
" \"template\": \"This is the entire message.\""
" } ]"
"}";
std::unique_ptr<ConfirmInfoBarDelegate> infobar(
CreateDelegateWithLegalMessage(
/* is_uploading= */ true, std::move(good_legal_message),
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_DENIED));
histogram_tester.ExpectUniqueSample(
"Autofill.CreditCardInfoBar.Server.PreviouslyDenied",
AutofillMetrics::INFOBAR_SHOWN, 1);
}
// Infobar is not shown because the provided legal message is invalid.
{
base::HistogramTester histogram_tester;
// Legal message is invalid because it's missing the url.
std::string bad_legal_message =
"{"
" \"line\" : [ {"
" \"template\": \"Panda {0}.\","
" \"template_parameter\": [ {"
" \"display_text\": \"bear\""
" } ]"
" } ]"
"}";
std::unique_ptr<ConfirmInfoBarDelegate> infobar(
CreateDelegateWithLegalMessage(
/* is_uploading= */ true, std::move(bad_legal_message),
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_DENIED));
histogram_tester.ExpectUniqueSample(
"Autofill.CreditCardInfoBar.Server.PreviouslyDenied",
AutofillMetrics::INFOBAR_NOT_SHOWN_INVALID_LEGAL_MESSAGE, 1);
}
// Accept the infobar.
{
std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
/* is_uploading= */ true,
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_NONE));
EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_));
base::HistogramTester histogram_tester;
EXPECT_TRUE(infobar->Accept());
histogram_tester.ExpectUniqueSample(
"Autofill.CreditCardInfoBar.Server.PreviouslyDenied",
AutofillMetrics::INFOBAR_ACCEPTED, 1);
}
// Cancel the infobar.
{
std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
/* is_uploading= */ true,
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_ACCEPTED));
base::HistogramTester histogram_tester;
EXPECT_TRUE(infobar->Cancel());
histogram_tester.ExpectUniqueSample(
"Autofill.CreditCardInfoBar.Server.PreviouslyAccepted",
AutofillMetrics::INFOBAR_DENIED, 1);
}
// Dismiss the infobar.
{
std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
/* is_uploading= */ true,
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_DENIED));
base::HistogramTester histogram_tester;
infobar->InfoBarDismissed();
histogram_tester.ExpectUniqueSample(
"Autofill.CreditCardInfoBar.Server.PreviouslyDenied",
AutofillMetrics::INFOBAR_DENIED, 1);
}
// Ignore the infobar.
{
std::unique_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(
/* is_uploading= */ true,
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_DENIED));
base::HistogramTester histogram_tester;
infobar.reset();
histogram_tester.ExpectUniqueSample(
"Autofill.CreditCardInfoBar.Server.PreviouslyDenied",
AutofillMetrics::INFOBAR_IGNORED, 1);
}
}
} // namespace autofill