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
|
// Copyright (c) 2010 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 "base/ref_counted.h"
#include "base/scoped_ptr.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/autofill/autofill_common_test.h"
#include "chrome/browser/autofill/autofill_manager.h"
#include "chrome/browser/autofill/autofill_metrics.h"
#include "chrome/browser/autofill/personal_data_manager.h"
#include "chrome/browser/renderer_host/test/test_render_view_host.h"
#include "chrome/browser/tab_contents/test_tab_contents.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/glue/form_data.h"
#include "webkit/glue/form_field.h"
using webkit_glue::FormData;
using webkit_glue::FormField;
namespace {
// TODO(isherman): Move this into autofill_common_test.h?
class TestPersonalDataManager : public PersonalDataManager {
public:
TestPersonalDataManager() {
CreateTestAutoFillProfiles(&web_profiles_);
CreateTestCreditCards(&credit_cards_);
}
virtual void InitializeIfNeeded() {}
virtual void SaveImportedFormData() {}
virtual bool IsDataLoaded() const { return true; }
private:
void CreateTestAutoFillProfiles(ScopedVector<AutoFillProfile>* profiles) {
AutoFillProfile* profile = new AutoFillProfile;
autofill_test::SetProfileInfo(profile, "Home", "Elvis", "Aaron",
"Presley", "theking@gmail.com", "RCA",
"3734 Elvis Presley Blvd.", "Apt. 10",
"Memphis", "Tennessee", "38116", "USA",
"12345678901", "");
profile->set_guid("00000000-0000-0000-0000-000000000001");
profiles->push_back(profile);
profile = new AutoFillProfile;
autofill_test::SetProfileInfo(profile, "Work", "Charles", "Hardin",
"Holley", "buddy@gmail.com", "Decca",
"123 Apple St.", "unit 6", "Lubbock",
"Texas", "79401", "USA", "23456789012",
"");
profile->set_guid("00000000-0000-0000-0000-000000000002");
profiles->push_back(profile);
profile = new AutoFillProfile;
autofill_test::SetProfileInfo(profile, "Empty", "", "", "", "", "", "", "",
"", "", "", "", "", "");
profile->set_guid("00000000-0000-0000-0000-000000000003");
profiles->push_back(profile);
}
void CreateTestCreditCards(ScopedVector<CreditCard>* credit_cards) {
CreditCard* credit_card = new CreditCard;
autofill_test::SetCreditCardInfo(credit_card, "First", "Elvis Presley",
"4234567890123456", // Visa
"04", "2012");
credit_card->set_guid("00000000-0000-0000-0000-000000000004");
credit_cards->push_back(credit_card);
credit_card = new CreditCard;
autofill_test::SetCreditCardInfo(credit_card, "Second", "Buddy Holly",
"5187654321098765", // Mastercard
"10", "2014");
credit_card->set_guid("00000000-0000-0000-0000-000000000005");
credit_cards->push_back(credit_card);
credit_card = new CreditCard;
autofill_test::SetCreditCardInfo(credit_card, "Empty", "", "", "", "");
credit_card->set_guid("00000000-0000-0000-0000-000000000006");
credit_cards->push_back(credit_card);
}
DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
};
class MockAutoFillMetrics : public AutoFillMetrics {
public:
MockAutoFillMetrics() {}
MOCK_CONST_METHOD1(Log, void(ServerQueryMetric metric));
MOCK_CONST_METHOD1(Log, void(QualityMetric metric));
private:
DISALLOW_COPY_AND_ASSIGN(MockAutoFillMetrics);
};
class TestAutoFillManager : public AutoFillManager {
public:
TestAutoFillManager(TabContents* tab_contents,
TestPersonalDataManager* personal_manager)
: AutoFillManager(tab_contents, personal_manager),
autofill_enabled_(true) {
set_metric_logger(new MockAutoFillMetrics);
}
virtual ~TestAutoFillManager() {}
virtual bool IsAutoFillEnabled() const { return autofill_enabled_; }
void set_autofill_enabled(bool autofill_enabled) {
autofill_enabled_ = autofill_enabled;
}
const MockAutoFillMetrics* metric_logger() const {
return static_cast<const MockAutoFillMetrics*>(
AutoFillManager::metric_logger());
}
private:
bool autofill_enabled_;
DISALLOW_COPY_AND_ASSIGN(TestAutoFillManager);
};
} // namespace
class AutoFillMetricsTest : public RenderViewHostTestHarness {
public:
AutoFillMetricsTest() {}
virtual ~AutoFillMetricsTest() {
// Order of destruction is important as AutoFillManager relies on
// PersonalDataManager to be around when it gets destroyed.
autofill_manager_.reset(NULL);
test_personal_data_ = NULL;
}
virtual void SetUp() {
RenderViewHostTestHarness::SetUp();
test_personal_data_ = new TestPersonalDataManager();
autofill_manager_.reset(new TestAutoFillManager(contents(),
test_personal_data_.get()));
}
protected:
scoped_ptr<TestAutoFillManager> autofill_manager_;
scoped_refptr<TestPersonalDataManager> test_personal_data_;
private:
DISALLOW_COPY_AND_ASSIGN(AutoFillMetricsTest);
};
// Test that we log quality metrics appropriately.
TEST_F(AutoFillMetricsTest, QualityMetrics) {
// Set up our form data.
FormData form;
form.name = ASCIIToUTF16("TestForm");
form.method = ASCIIToUTF16("POST");
form.origin = GURL("http://example.com/form.html");
form.action = GURL("http://example.com/submit.html");
form.user_submitted = true;
FormField field;
autofill_test::CreateTestFormField(
"Autofilled", "autofilled", "Elvis Presley", "text", &field);
field.set_autofilled(true);
form.fields.push_back(field);
autofill_test::CreateTestFormField(
"Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field);
form.fields.push_back(field);
autofill_test::CreateTestFormField(
"Empty", "empty", "", "text", &field);
form.fields.push_back(field);
autofill_test::CreateTestFormField(
"Unknown", "unknown", "garbage", "text", &field);
form.fields.push_back(field);
autofill_test::CreateTestFormField(
"Select", "select", "USA", "select-one", &field);
form.fields.push_back(field);
// Establish our expectations.
::testing::InSequence dummy;
EXPECT_CALL(*autofill_manager_->metric_logger(),
Log(AutoFillMetrics::FIELD_SUBMITTED));
EXPECT_CALL(*autofill_manager_->metric_logger(),
Log(AutoFillMetrics::FIELD_AUTOFILLED));
EXPECT_CALL(*autofill_manager_->metric_logger(),
Log(AutoFillMetrics::FIELD_SUBMITTED));
EXPECT_CALL(*autofill_manager_->metric_logger(),
Log(AutoFillMetrics::FIELD_AUTOFILL_FAILED ));
EXPECT_CALL(*autofill_manager_->metric_logger(),
Log(AutoFillMetrics::FIELD_SUBMITTED));
EXPECT_CALL(*autofill_manager_->metric_logger(),
Log(AutoFillMetrics::FIELD_SUBMITTED));
// Simulate form submission.
EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form));
}
// Test that we don't log quality metrics for non-autofillable forms.
TEST_F(AutoFillMetricsTest, NoQualityMetricsForNonAutoFillableForms) {
// Forms must include at least three fields to be auto-fillable.
FormData form;
form.name = ASCIIToUTF16("TestForm");
form.method = ASCIIToUTF16("POST");
form.origin = GURL("http://example.com/form.html");
form.action = GURL("http://example.com/submit.html");
form.user_submitted = true;
FormField field;
autofill_test::CreateTestFormField(
"Autofilled", "autofilled", "Elvis Presley", "text", &field);
field.set_autofilled(true);
form.fields.push_back(field);
autofill_test::CreateTestFormField(
"Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field);
form.fields.push_back(field);
// Simulate form submission.
EXPECT_CALL(*autofill_manager_->metric_logger(),
Log(AutoFillMetrics::FIELD_SUBMITTED)).Times(0);
EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form));
// Search forms are not auto-fillable.
form.action = GURL("http://example.com/search?q=Elvis%20Presley");
autofill_test::CreateTestFormField(
"Empty", "empty", "", "text", &field);
form.fields.push_back(field);
// Simulate form submission.
EXPECT_CALL(*autofill_manager_->metric_logger(),
Log(AutoFillMetrics::FIELD_SUBMITTED)).Times(0);
EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form));
}
|