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
|
// 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/ref_counted.h"
#include "base/scoped_ptr.h"
#include "base/string16.h"
#include "base/string_util.h"
#include "base/time.h"
#include "base/waitable_event.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 "third_party/WebKit/WebKit/chromium/public/WebInputElement.h"
#include "webkit/glue/form_field.h"
using base::Time;
using base::TimeDelta;
using base::WaitableEvent;
using testing::_;
using testing::ElementsAreArray;
using testing::Pointee;
using testing::Property;
typedef std::vector<AutofillChange> AutofillChangeList;
ACTION_P(SignalEvent, event) {
event->Signal();
}
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 handle() { return handle_; }
const std::vector<string16>& values() { return values_; }
private:
WebDataService::Handle handle_;
std::vector<string16> values_;
DISALLOW_COPY_AND_ASSIGN(AutofillWebDataServiceConsumer);
};
// This class will add and remove a mock notification observer from
// the DB thread.
class DBThreadObserverHelper :
public base::RefCountedThreadSafe<DBThreadObserverHelper,
ChromeThread::DeleteOnDBThread> {
public:
DBThreadObserverHelper() : done_event_(true, false) {}
void Init() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
ChromeThread::PostTask(
ChromeThread::DB,
FROM_HERE,
NewRunnableMethod(this, &DBThreadObserverHelper::AddObserverTask));
done_event_.Wait();
}
virtual ~DBThreadObserverHelper() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::DB));
registrar_.Remove(&observer_,
NotificationType::AUTOFILL_ENTRIES_CHANGED,
NotificationService::AllSources());
}
NotificationObserverMock* observer() {
return &observer_;
}
private:
friend class base::RefCountedThreadSafe<DBThreadObserverHelper>;
void AddObserverTask() {
registrar_.Add(&observer_,
NotificationType::AUTOFILL_ENTRIES_CHANGED,
NotificationService::AllSources());
done_event_.Signal();
}
WaitableEvent done_event_;
NotificationRegistrar registrar_;
NotificationObserverMock observer_;
};
class WebDataServiceTest : public testing::Test {
public:
WebDataServiceTest()
: ui_thread_(ChromeThread::UI, &message_loop_),
db_thread_(ChromeThread::DB) {}
protected:
virtual void SetUp() {
db_thread_.Start();
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();
}
MessageLoopForUI message_loop_;
ChromeThread ui_thread_;
ChromeThread db_thread_;
FilePath profile_dir_;
scoped_refptr<WebDataService> wds_;
};
class WebDataServiceAutofillTest : public WebDataServiceTest {
public:
WebDataServiceAutofillTest()
: WebDataServiceTest(), done_event_(true, false) {}
protected:
virtual void SetUp() {
WebDataServiceTest::SetUp();
name1_ = ASCIIToUTF16("name1");
name2_ = ASCIIToUTF16("name2");
value1_ = ASCIIToUTF16("value1");
value2_ = ASCIIToUTF16("value2");
observer_helper_ = new DBThreadObserverHelper();
observer_helper_->Init();
}
virtual void TearDown() {
// Release this first so it can get destructed on the db thread.
observer_helper_ = NULL;
WebDataServiceTest::TearDown();
}
void AppendFormField(const string16& name,
const string16& value,
std::vector<webkit_glue::FormField>* form_fields) {
form_fields->push_back(
webkit_glue::FormField(string16(),
name,
value,
string16(),
WebKit::WebInputElement::Text));
}
string16 name1_;
string16 name2_;
string16 value1_;
string16 value2_;
scoped_refptr<DBThreadObserverHelper> observer_helper_;
WaitableEvent done_event_;
};
TEST_F(WebDataServiceAutofillTest, Add) {
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_helper_->observer(),
Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_CHANGED),
NotificationService::AllSources(),
Property(&Details<const AutofillChangeList>::ptr,
Pointee(ElementsAreArray(expected_changes))))).
WillOnce(SignalEvent(&done_event_));
std::vector<webkit_glue::FormField> form_fields;
AppendFormField(name1_, value1_, &form_fields);
AppendFormField(name2_, value2_, &form_fields);
wds_->AddFormFieldValues(form_fields);
// The event will be signaled when the mock observer is notified.
done_event_.Wait();
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(WebDataServiceAutofillTest, RemoveOne) {
// First add some values to autofill.
EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)).
WillOnce(SignalEvent(&done_event_));
std::vector<webkit_glue::FormField> form_fields;
AppendFormField(name1_, value1_, &form_fields);
wds_->AddFormFieldValues(form_fields);
// The event will be signaled when the mock observer is notified.
done_event_.Wait();
// 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_helper_->observer(),
Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_CHANGED),
NotificationService::AllSources(),
Property(&Details<const AutofillChangeList>::ptr,
Pointee(ElementsAreArray(expected_changes))))).
WillOnce(SignalEvent(&done_event_));
wds_->RemoveFormValueForElementName(name1_, value1_);
// The event will be signaled when the mock observer is notified.
done_event_.Wait();
}
TEST_F(WebDataServiceAutofillTest,RemoveMany) {
TimeDelta one_day(TimeDelta::FromDays(1));
Time t = Time::Now();
EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)).
WillOnce(SignalEvent(&done_event_));
std::vector<webkit_glue::FormField> form_fields;
AppendFormField(name1_, value1_, &form_fields);
AppendFormField(name2_, value2_, &form_fields);
wds_->AddFormFieldValues(form_fields);
// The event will be signaled when the mock observer is notified.
done_event_.Wait();
// 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_helper_->observer(),
Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_CHANGED),
NotificationService::AllSources(),
Property(&Details<const AutofillChangeList>::ptr,
Pointee(ElementsAreArray(expected_changes))))).
WillOnce(SignalEvent(&done_event_));
wds_->RemoveFormElementsAddedBetween(t, t + one_day);
// The event will be signaled when the mock observer is notified.
done_event_.Wait();
}
|