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

Side by Side Diff: chrome/browser/autofill/autofill_cc_infobar_delegate_unittest.cc

Issue 177923004: Moved CCInfobarDelegate tests out of AutofillMetricsTest (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h"
6
7 #include <vector>
8
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/autofill/personal_data_manager_factory.h"
14 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h"
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "components/autofill/core/browser/autofill_metrics.h"
18 #include "components/autofill/core/browser/autofill_test_utils.h"
19 #include "components/autofill/core/browser/form_structure.h"
Ilya Sherman 2014/03/06 23:12:02 nit: This include is no longer needed. I bet that
blundell 2014/03/07 14:12:32 Done.
20 #include "components/autofill/core/browser/personal_data_manager.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 using base::ASCIIToUTF16;
25 using base::TimeDelta;
26 using base::TimeTicks;
Ilya Sherman 2014/03/06 23:12:02 nit: I don't think these three are needed.
blundell 2014/03/07 14:12:32 Done.
27 using testing::_;
28 using testing::AnyNumber;
29 using testing::Mock;
Ilya Sherman 2014/03/06 23:12:02 nit: Are these two still needed?
blundell 2014/03/07 14:12:32 Done.
30
31 namespace autofill {
32
33 namespace {
34
35 class MockAutofillMetrics : public AutofillMetrics {
36 public:
37 MockAutofillMetrics() {}
38 MOCK_CONST_METHOD1(LogCreditCardInfoBarMetric, void(InfoBarMetric metric));
39 MOCK_CONST_METHOD1(LogDeveloperEngagementMetric,
40 void(DeveloperEngagementMetric metric));
41 MOCK_CONST_METHOD2(LogHeuristicTypePrediction,
42 void(FieldTypeQualityMetric metric,
43 ServerFieldType field_type));
44 MOCK_CONST_METHOD2(LogOverallTypePrediction,
45 void(FieldTypeQualityMetric metric,
46 ServerFieldType field_type));
47 MOCK_CONST_METHOD2(LogServerTypePrediction,
48 void(FieldTypeQualityMetric metric,
49 ServerFieldType field_type));
50 MOCK_CONST_METHOD1(LogServerQueryMetric, void(ServerQueryMetric metric));
51 MOCK_CONST_METHOD1(LogUserHappinessMetric, void(UserHappinessMetric metric));
52 MOCK_CONST_METHOD1(LogFormFillDurationFromLoadWithAutofill,
53 void(const TimeDelta& duration));
54 MOCK_CONST_METHOD1(LogFormFillDurationFromLoadWithoutAutofill,
55 void(const TimeDelta& duration));
56 MOCK_CONST_METHOD1(LogFormFillDurationFromInteractionWithAutofill,
57 void(const TimeDelta& duration));
58 MOCK_CONST_METHOD1(LogFormFillDurationFromInteractionWithoutAutofill,
59 void(const TimeDelta& duration));
60 MOCK_CONST_METHOD1(LogIsAutofillEnabledAtPageLoad, void(bool enabled));
61 MOCK_CONST_METHOD1(LogIsAutofillEnabledAtStartup, void(bool enabled));
62 MOCK_CONST_METHOD1(LogStoredProfileCount, void(size_t num_profiles));
63 MOCK_CONST_METHOD1(LogAddressSuggestionsCount, void(size_t num_suggestions));
64
65 private:
66 DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics);
67 };
Ilya Sherman 2014/03/06 23:12:02 I've been meaning to kill this class entirely, act
blundell 2014/03/07 14:12:32 I kept the usage of AutofillMetrics but trimmed it
68
69 class TestPersonalDataManager : public PersonalDataManager {
70 public:
71 TestPersonalDataManager()
72 : PersonalDataManager("en-US"), autofill_enabled_(true) {
73 set_metric_logger(new testing::NiceMock<MockAutofillMetrics>());
Ilya Sherman 2014/03/06 23:12:02 This shouldn't be needed anymore.
blundell 2014/03/07 14:12:32 Done.
74 CreateTestAutofillProfiles(&web_profiles_);
Ilya Sherman 2014/03/06 23:12:02 Nor should this.
blundell 2014/03/07 14:12:32 Done.
75 }
76
77 using PersonalDataManager::set_database;
78 using PersonalDataManager::SetPrefService;
79
80 // Overridden to avoid a trip to the database. This should be a no-op except
81 // for the side-effect of logging the profile count.
82 virtual void LoadProfiles() OVERRIDE {
83 std::vector<AutofillProfile*> profiles;
84 web_profiles_.release(&profiles);
85 WDResult<std::vector<AutofillProfile*> > result(AUTOFILL_PROFILES_RESULT,
86 profiles);
87 ReceiveLoadedProfiles(0, &result);
Ilya Sherman 2014/03/06 23:12:02 It should be fine to empty this out.
blundell 2014/03/07 14:12:32 Done.
88 }
89
90 // Overridden to avoid a trip to the database.
91 virtual void LoadCreditCards() OVERRIDE {}
92
93 const MockAutofillMetrics* metric_logger() const {
94 return static_cast<const MockAutofillMetrics*>(
95 PersonalDataManager::metric_logger());
96 }
Ilya Sherman 2014/03/06 23:12:02 This shouldn't be needed anymore.
blundell 2014/03/07 14:12:32 Done.
97
98 void set_autofill_enabled(bool autofill_enabled) {
99 autofill_enabled_ = autofill_enabled;
100 }
101
102 virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; }
Ilya Sherman 2014/03/06 23:12:02 Ditto.
blundell 2014/03/07 14:12:32 Done.
103
104 MOCK_METHOD1(SaveImportedCreditCard,
105 std::string(const CreditCard& imported_credit_card));
106
107 private:
108 void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) {
109 AutofillProfile* profile = new AutofillProfile;
110 test::SetProfileInfo(profile,
111 "Elvis",
112 "Aaron",
113 "Presley",
114 "theking@gmail.com",
115 "RCA",
116 "3734 Elvis Presley Blvd.",
117 "Apt. 10",
118 "Memphis",
119 "Tennessee",
120 "38116",
121 "US",
122 "12345678901");
123 profile->set_guid("00000000-0000-0000-0000-000000000001");
124 profiles->push_back(profile);
125 profile = new AutofillProfile;
126 test::SetProfileInfo(profile,
127 "Charles",
128 "Hardin",
129 "Holley",
130 "buddy@gmail.com",
131 "Decca",
132 "123 Apple St.",
133 "unit 6",
134 "Lubbock",
135 "Texas",
136 "79401",
137 "US",
138 "2345678901");
139 profile->set_guid("00000000-0000-0000-0000-000000000002");
140 profiles->push_back(profile);
141 }
Ilya Sherman 2014/03/06 23:12:02 Ditto.
blundell 2014/03/07 14:12:32 Done.
142
143 bool autofill_enabled_;
Ilya Sherman 2014/03/06 23:12:02 Ditto.
blundell 2014/03/07 14:12:32 Done.
144
145 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
146 };
147
148 class TestFormStructure : public FormStructure {
149 public:
150 explicit TestFormStructure(const FormData& form) : FormStructure(form) {}
151 virtual ~TestFormStructure() {}
152
153 void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types,
154 const std::vector<ServerFieldType>& server_types) {
155 ASSERT_EQ(field_count(), heuristic_types.size());
156 ASSERT_EQ(field_count(), server_types.size());
157
158 for (size_t i = 0; i < field_count(); ++i) {
159 AutofillField* form_field = field(i);
160 ASSERT_TRUE(form_field);
161 form_field->set_heuristic_type(heuristic_types[i]);
162 form_field->set_server_type(server_types[i]);
163 }
164
165 UpdateAutofillCount();
166 }
167
168 private:
169 DISALLOW_COPY_AND_ASSIGN(TestFormStructure);
170 };
Ilya Sherman 2014/03/06 23:12:02 nit: This class doesn't seem to be used anywhere.
blundell 2014/03/07 14:12:32 Done.
171
172 } // namespace
173
174 class AutofillCCInfobarDelegateTest : public ChromeRenderViewHostTestHarness {
175 public:
176 virtual ~AutofillCCInfobarDelegateTest();
177
178 virtual void SetUp() OVERRIDE;
179 virtual void TearDown() OVERRIDE;
180
181 protected:
182 scoped_ptr<ConfirmInfoBarDelegate> CreateDelegate(
183 MockAutofillMetrics* metric_logger);
184
185 scoped_ptr<TestPersonalDataManager> personal_data_;
186 };
187
188 AutofillCCInfobarDelegateTest::~AutofillCCInfobarDelegateTest() {}
189
190 void AutofillCCInfobarDelegateTest::SetUp() {
191 ChromeRenderViewHostTestHarness::SetUp();
192
193 // Ensure Mac OS X does not pop up a modal dialog for the Address Book.
194 autofill::test::DisableSystemServices(profile());
195
196 PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL);
197
198 TabAutofillManagerDelegate::CreateForWebContents(web_contents());
199 autofill::TabAutofillManagerDelegate* manager_delegate =
200 autofill::TabAutofillManagerDelegate::FromWebContents(web_contents());
201
202 personal_data_.reset(new TestPersonalDataManager());
203 personal_data_->set_database(manager_delegate->GetDatabase());
204 personal_data_->SetPrefService(profile()->GetPrefs());
205 }
206
207 void AutofillCCInfobarDelegateTest::TearDown() {
208 personal_data_.reset();
209 ChromeRenderViewHostTestHarness::TearDown();
210 }
211
212 scoped_ptr<ConfirmInfoBarDelegate>
213 AutofillCCInfobarDelegateTest::CreateDelegate(
214 MockAutofillMetrics* metric_logger) {
215 EXPECT_CALL(*metric_logger,
216 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN));
217
218 CreditCard credit_card;
219 return AutofillCCInfoBarDelegate::Create(
220 metric_logger,
221 base::Bind(
222 base::IgnoreResult(&TestPersonalDataManager::SaveImportedCreditCard),
223 base::Unretained(personal_data_.get()),
224 credit_card));
225 }
226
227 // Test that credit card infobar metrics are logged correctly.
228 TEST_F(AutofillCCInfobarDelegateTest, Metrics) {
229 testing::NiceMock<MockAutofillMetrics> metric_logger;
Ilya Sherman 2014/03/06 23:12:02 nit: Shouldn't be a need for a NiceMock anymore on
blundell 2014/03/07 14:12:32 Done.
230 ::testing::InSequence dummy;
231
232 // Accept the infobar.
233 {
234 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
235 ASSERT_TRUE(infobar);
236 EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_));
237 EXPECT_CALL(metric_logger,
238 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_ACCEPTED));
239
240 EXPECT_CALL(metric_logger,
241 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
242 .Times(0);
243 EXPECT_TRUE(infobar->Accept());
244 }
245
246 // Cancel the infobar.
247 {
248 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
249 ASSERT_TRUE(infobar);
250 EXPECT_CALL(metric_logger,
251 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED))
252 .Times(1);
253 EXPECT_CALL(metric_logger,
254 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
255 .Times(0);
256 EXPECT_TRUE(infobar->Cancel());
257 }
258
259 // Dismiss the infobar.
260 {
261 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
262 ASSERT_TRUE(infobar);
263 EXPECT_CALL(metric_logger,
264 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED))
265 .Times(1);
266 EXPECT_CALL(metric_logger,
267 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
268 .Times(0);
269 infobar->InfoBarDismissed();
270 }
271
272 // Ignore the infobar.
273 {
274 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
275 ASSERT_TRUE(infobar);
276 EXPECT_CALL(metric_logger,
277 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
278 .Times(1);
279 }
280 }
281
282 } // namespace autofill
OLDNEW
« no previous file with comments | « no previous file | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698