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

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

Issue 5703002: Add some basic success/failure UMA logging for autofill. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Single AutoFillMetrics class, more mocks Created 10 years 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
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 "base/ref_counted.h"
6 #include "base/scoped_ptr.h"
7 #include "base/string16.h"
8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/autofill/autofill_common_test.h"
10 #include "chrome/browser/autofill/autofill_manager.h"
11 #include "chrome/browser/autofill/autofill_metrics.h"
12 #include "chrome/browser/autofill/personal_data_manager.h"
13 #include "chrome/browser/renderer_host/test/test_render_view_host.h"
14 #include "chrome/browser/tab_contents/test_tab_contents.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "webkit/glue/form_data.h"
18 #include "webkit/glue/form_field.h"
19
20 using webkit_glue::FormData;
21 using webkit_glue::FormField;
22
23 namespace {
24
25 // TODO(isherman): Move this into autofill_common_test.h?
26 class TestPersonalDataManager : public PersonalDataManager {
27 public:
28 TestPersonalDataManager() {
29 CreateTestAutoFillProfiles(&web_profiles_);
30 CreateTestCreditCards(&credit_cards_);
31 }
32
33 virtual void InitializeIfNeeded() {}
34 virtual void SaveImportedFormData() {}
35 virtual bool IsDataLoaded() const { return true; }
36
37 private:
38 void CreateTestAutoFillProfiles(ScopedVector<AutoFillProfile>* profiles) {
39 AutoFillProfile* profile = new AutoFillProfile;
40 autofill_test::SetProfileInfo(profile, "Home", "Elvis", "Aaron",
41 "Presley", "theking@gmail.com", "RCA",
42 "3734 Elvis Presley Blvd.", "Apt. 10",
43 "Memphis", "Tennessee", "38116", "USA",
44 "12345678901", "");
45 profile->set_guid("00000000-0000-0000-0000-000000000001");
46 profiles->push_back(profile);
47 profile = new AutoFillProfile;
48 autofill_test::SetProfileInfo(profile, "Work", "Charles", "Hardin",
49 "Holley", "buddy@gmail.com", "Decca",
50 "123 Apple St.", "unit 6", "Lubbock",
51 "Texas", "79401", "USA", "23456789012",
52 "");
53 profile->set_guid("00000000-0000-0000-0000-000000000002");
54 profiles->push_back(profile);
55 profile = new AutoFillProfile;
56 autofill_test::SetProfileInfo(profile, "Empty", "", "", "", "", "", "", "",
57 "", "", "", "", "", "");
58 profile->set_guid("00000000-0000-0000-0000-000000000003");
59 profiles->push_back(profile);
60 }
61
62 void CreateTestCreditCards(ScopedVector<CreditCard>* credit_cards) {
63 CreditCard* credit_card = new CreditCard;
64 autofill_test::SetCreditCardInfo(credit_card, "First", "Elvis Presley",
65 "4234567890123456", // Visa
66 "04", "2012");
67 credit_card->set_guid("00000000-0000-0000-0000-000000000004");
68 credit_cards->push_back(credit_card);
69 credit_card = new CreditCard;
70 autofill_test::SetCreditCardInfo(credit_card, "Second", "Buddy Holly",
71 "5187654321098765", // Mastercard
72 "10", "2014");
73 credit_card->set_guid("00000000-0000-0000-0000-000000000005");
74 credit_cards->push_back(credit_card);
75 credit_card = new CreditCard;
76 autofill_test::SetCreditCardInfo(credit_card, "Empty", "", "", "", "");
77 credit_card->set_guid("00000000-0000-0000-0000-000000000006");
78 credit_cards->push_back(credit_card);
79 }
80
81 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
82 };
83
84 class MockAutoFillMetrics : public AutoFillMetrics {
85 public:
86 MOCK_CONST_METHOD1(Log, void(ServerQueryMetric metric));
87 MOCK_CONST_METHOD1(Log, void(QualityMetric metric));
88 };
89
90 class TestAutoFillManager : public AutoFillManager {
91 public:
92 TestAutoFillManager(TabContents* tab_contents,
93 TestPersonalDataManager* personal_manager)
94 : AutoFillManager(tab_contents, personal_manager),
95 autofill_enabled_(true) {
96 set_metric_logger(new MockAutoFillMetrics);
97 }
98 virtual ~TestAutoFillManager() {}
99
100 virtual bool IsAutoFillEnabled() const { return autofill_enabled_; }
101
102 void set_autofill_enabled(bool autofill_enabled) {
103 autofill_enabled_ = autofill_enabled;
104 }
105
106 const MockAutoFillMetrics* metric_logger() const {
107 return static_cast<const MockAutoFillMetrics*>(
108 AutoFillManager::metric_logger());
109 }
110
111 private:
112 bool autofill_enabled_;
113
114 DISALLOW_COPY_AND_ASSIGN(TestAutoFillManager);
115 };
116
117 } // namespace
118
119 class AutoFillMetricsTest : public RenderViewHostTestHarness {
120 public:
121 AutoFillMetricsTest() {}
122 virtual ~AutoFillMetricsTest() {
123 // Order of destruction is important as AutoFillManager relies on
124 // PersonalDataManager to be around when it gets destroyed.
125 autofill_manager_.reset(NULL);
126 test_personal_data_ = NULL;
127 }
128
129 virtual void SetUp() {
130 RenderViewHostTestHarness::SetUp();
131 test_personal_data_ = new TestPersonalDataManager();
132 autofill_manager_.reset(new TestAutoFillManager(contents(),
133 test_personal_data_.get()));
134 }
135
136 protected:
137 scoped_ptr<TestAutoFillManager> autofill_manager_;
138 scoped_refptr<TestPersonalDataManager> test_personal_data_;
139
140 private:
141 DISALLOW_COPY_AND_ASSIGN(AutoFillMetricsTest);
142 };
143
144 // Test that we log quality metrics appropriately.
145 TEST_F(AutoFillMetricsTest, QualityMetrics) {
146 // Set up our form data.
147 FormData form;
148 form.name = ASCIIToUTF16("TestForm");
149 form.method = ASCIIToUTF16("POST");
150 form.origin = GURL("http://example.com/form.html");
151 form.action = GURL("http://example.com/submit.html");
152 form.user_submitted = true;
153
154 FormField field;
155 autofill_test::CreateTestFormField(
156 "Autofilled", "autofilled", "Elvis Presley", "text", &field);
157 field.set_autofilled(true);
158 form.fields.push_back(field);
159 autofill_test::CreateTestFormField(
160 "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field);
161 form.fields.push_back(field);
162 autofill_test::CreateTestFormField(
163 "Empty", "empty", "", "text", &field);
164 form.fields.push_back(field);
165 autofill_test::CreateTestFormField(
166 "Unknown", "unknown", "garbage", "text", &field);
167 form.fields.push_back(field);
168 autofill_test::CreateTestFormField(
169 "Select", "select", "USA", "select-one", &field);
170 form.fields.push_back(field);
171
172 // Establish our expectations.
173 ::testing::InSequence dummy;
174 EXPECT_CALL(*autofill_manager_->metric_logger(),
175 Log(AutoFillMetrics::FIELD_SUBMITTED));
176 EXPECT_CALL(*autofill_manager_->metric_logger(),
177 Log(AutoFillMetrics::FIELD_AUTOFILLED));
178 EXPECT_CALL(*autofill_manager_->metric_logger(),
179 Log(AutoFillMetrics::FIELD_SUBMITTED));
180 EXPECT_CALL(*autofill_manager_->metric_logger(),
181 Log(AutoFillMetrics::FIELD_AUTOFILL_FAILED ));
182 EXPECT_CALL(*autofill_manager_->metric_logger(),
183 Log(AutoFillMetrics::FIELD_SUBMITTED));
184 EXPECT_CALL(*autofill_manager_->metric_logger(),
185 Log(AutoFillMetrics::FIELD_SUBMITTED));
186
187 // Simulate form submission.
188 EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form));
189 }
190
191 // Test that we don't log quality metrics for non-autofillable forms.
192 TEST_F(AutoFillMetricsTest, NoQualityMetricsForNonAutoFillableForms) {
193 // Forms must include at least three fields to be auto-fillable.
194 FormData form;
195 form.name = ASCIIToUTF16("TestForm");
196 form.method = ASCIIToUTF16("POST");
197 form.origin = GURL("http://example.com/form.html");
198 form.action = GURL("http://example.com/submit.html");
199 form.user_submitted = true;
200
201 FormField field;
202 autofill_test::CreateTestFormField(
203 "Autofilled", "autofilled", "Elvis Presley", "text", &field);
204 field.set_autofilled(true);
205 form.fields.push_back(field);
206 autofill_test::CreateTestFormField(
207 "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field);
208 form.fields.push_back(field);
209
210 // Simulate form submission.
211 EXPECT_CALL(*autofill_manager_->metric_logger(),
212 Log(AutoFillMetrics::FIELD_SUBMITTED)).Times(0);
213 EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form));
214
215 // Search forms are not auto-fillable.
216 form.action = GURL("http://example.com/search?q=Elvis%20Presley");
217 autofill_test::CreateTestFormField(
218 "Empty", "empty", "", "text", &field);
219 form.fields.push_back(field);
220
221 // Simulate form submission.
222 EXPECT_CALL(*autofill_manager_->metric_logger(),
223 Log(AutoFillMetrics::FIELD_SUBMITTED)).Times(0);
224 EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form));
225 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698