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
|
// 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 <vector>
#include "base/basictypes.h"
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/scoped_ptr.h"
#include "base/string16.h"
#include "base/string_util.h"
#include "base/time.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/webdata/autofill_change.h"
#include "chrome/browser/webdata/autofill_entry.h"
#include "chrome/browser/webdata/web_data_service.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_observer_mock.h"
#include "chrome/common/notification_registrar.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/glue/form_field.h"
using base::Time;
using base::TimeDelta;
using testing::_;
using testing::ElementsAreArray;
using testing::Pointee;
using testing::Property;
typedef std::vector<AutofillChange> AutofillChangeList;
ACTION(QuitUIMessageLoop) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
MessageLoop::current()->Quit();
}
class AutofillWebDataServiceConsumer: public WebDataServiceConsumer {
public:
AutofillWebDataServiceConsumer() : handle_(0) {}
virtual ~AutofillWebDataServiceConsumer() {}
virtual void OnWebDataServiceRequestDone(WebDataService::Handle handle,
const WDTypedResult* result) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
DCHECK(result->GetType() == AUTOFILL_VALUE_RESULT);
handle_ = handle;
const WDResult<std::vector<string16> >* autofill_result =
static_cast<const WDResult<std::vector<string16> >*>(result);
// Copy the values.
values_ = autofill_result->GetValue();
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
MessageLoop::current()->Quit();
}
WebDataService::Handle const handle() { return handle_; }
const std::vector<string16>& values() { return values_; }
private:
WebDataService::Handle handle_;
std::vector<string16> values_;
DISALLOW_COPY_AND_ASSIGN(AutofillWebDataServiceConsumer);
};
class WebDataServiceTest : public testing::Test {
public:
WebDataServiceTest()
: ui_thread_(ChromeThread::UI, &message_loop_),
db_thread_(ChromeThread::DB) {}
protected:
virtual void SetUp() {
db_thread_.Start();
name1_ = ASCIIToUTF16("name1");
name2_ = ASCIIToUTF16("name2");
value1_ = ASCIIToUTF16("value1");
value2_ = ASCIIToUTF16("value2");
PathService::Get(chrome::DIR_TEST_DATA, &profile_dir_);
const std::string test_profile = "WebDataServiceTest";
profile_dir_ = profile_dir_.AppendASCII(test_profile);
file_util::Delete(profile_dir_, true);
file_util::CreateDirectory(profile_dir_);
wds_ = new WebDataService();
wds_->Init(profile_dir_);
}
virtual void TearDown() {
if (wds_.get())
wds_->Shutdown();
file_util::Delete(profile_dir_, true);
db_thread_.Stop();
MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask);
MessageLoop::current()->Run();
}
void AppendFormField(const string16& name,
const string16& value,
std::vector<webkit_glue::FormField>* form_fields) {
form_fields->push_back(
webkit_glue::FormField(string16(), name, string16(), value));
}
MessageLoopForUI message_loop_;
ChromeThread ui_thread_;
ChromeThread db_thread_;
string16 name1_;
string16 name2_;
string16 value1_;
string16 value2_;
FilePath profile_dir_;
scoped_refptr<WebDataService> wds_;
NotificationRegistrar registrar_;
NotificationObserverMock observer_;
};
TEST_F(WebDataServiceTest, AutofillAdd) {
const AutofillChange expected_changes[] = {
AutofillChange(AutofillChange::ADD, AutofillKey(name1_, value1_)),
AutofillChange(AutofillChange::ADD, AutofillKey(name2_, value2_))
};
// This will verify that the correct notification is triggered,
// passing the correct list of autofill keys in the details.
EXPECT_CALL(
observer_,
Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_CHANGED),
NotificationService::AllSources(),
Property(&Details<const AutofillChangeList>::ptr,
Pointee(ElementsAreArray(expected_changes))))).
WillOnce(QuitUIMessageLoop());
registrar_.Add(&observer_,
NotificationType::AUTOFILL_ENTRIES_CHANGED,
NotificationService::AllSources());
std::vector<webkit_glue::FormField> form_fields;
AppendFormField(name1_, value1_, &form_fields);
AppendFormField(name2_, value2_, &form_fields);
wds_->AddFormFieldValues(form_fields);
// The message loop will exit when the mock observer is notified.
MessageLoop::current()->Run();
AutofillWebDataServiceConsumer consumer;
WebDataService::Handle handle;
static const int limit = 10;
handle = wds_->GetFormValuesForElementName(
name1_, string16(), limit, &consumer);
// The message loop will exit when the consumer is called.
MessageLoop::current()->Run();
EXPECT_EQ(handle, consumer.handle());
ASSERT_EQ(1U, consumer.values().size());
EXPECT_EQ(value1_, consumer.values()[0]);
}
TEST_F(WebDataServiceTest, AutofillRemoveOne) {
// First add some values to autofill.
EXPECT_CALL(observer_, Observe(_, _, _)).WillOnce(QuitUIMessageLoop());
registrar_.Add(&observer_,
NotificationType::AUTOFILL_ENTRIES_CHANGED,
NotificationService::AllSources());
std::vector<webkit_glue::FormField> form_fields;
AppendFormField(name1_, value1_, &form_fields);
wds_->AddFormFieldValues(form_fields);
// The message loop will exit when the mock observer is notified.
MessageLoop::current()->Run();
// This will verify that the correct notification is triggered,
// passing the correct list of autofill keys in the details.
const AutofillChange expected_changes[] = {
AutofillChange(AutofillChange::REMOVE, AutofillKey(name1_, value1_))
};
EXPECT_CALL(
observer_,
Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_CHANGED),
NotificationService::AllSources(),
Property(&Details<const AutofillChangeList>::ptr,
Pointee(ElementsAreArray(expected_changes))))).
WillOnce(QuitUIMessageLoop());
wds_->RemoveFormValueForElementName(name1_, value1_);
// The message loop will exit when the mock observer is notified.
MessageLoop::current()->Run();
}
TEST_F(WebDataServiceTest, AutofillRemoveMany) {
TimeDelta one_day(TimeDelta::FromDays(1));
Time t = Time::Now();
EXPECT_CALL(observer_, Observe(_, _, _)).WillOnce(QuitUIMessageLoop());
registrar_.Add(&observer_,
NotificationType::AUTOFILL_ENTRIES_CHANGED,
NotificationService::AllSources());
std::vector<webkit_glue::FormField> form_fields;
AppendFormField(name1_, value1_, &form_fields);
AppendFormField(name2_, value2_, &form_fields);
wds_->AddFormFieldValues(form_fields);
// The message loop will exit when the mock observer is notified.
MessageLoop::current()->Run();
// This will verify that the correct notification is triggered,
// passing the correct list of autofill keys in the details.
const AutofillChange expected_changes[] = {
AutofillChange(AutofillChange::REMOVE, AutofillKey(name1_, value1_)),
AutofillChange(AutofillChange::REMOVE, AutofillKey(name2_, value2_))
};
EXPECT_CALL(
observer_,
Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_CHANGED),
NotificationService::AllSources(),
Property(&Details<const AutofillChangeList>::ptr,
Pointee(ElementsAreArray(expected_changes))))).
WillOnce(QuitUIMessageLoop());
wds_->RemoveFormElementsAddedBetween(t, t + one_day);
// The message loop will exit when the mock observer is notified.
MessageLoop::current()->Run();
}
|