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

Side by Side Diff: components/webdata/autofill/web_data_service_unittest.cc

Issue 13839013: Webdata Component tryjobs NOT FOR REVIEW (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: After WD/AF merge Created 7 years, 8 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
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 <string>
6 #include <vector>
7
8 #include "base/basictypes.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/message_loop.h"
14 #include "base/stl_util.h"
15 #include "base/string16.h"
16 #include "base/string_util.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/time.h"
19 #include "base/utf_string_conversions.h"
20 #include "components/autofill/browser/autofill_country.h"
21 #include "components/autofill/browser/autofill_profile.h"
22 #include "components/autofill/browser/credit_card.h"
23 #include "components/autofill/common/form_field_data.h"
24 #include "components/webdata/autofill/autofill_change.h"
25 #include "components/webdata/autofill/autofill_entry.h"
26 #include "components/webdata/autofill/autofill_table.h"
27 #include "components/webdata/autofill/autofill_webdata_service.h"
28 #include "components/webdata/autofill/autofill_webdata_service_observer.h"
29 #include "components/webdata/common/web_data_service_test_util.h"
30 #include "components/webdata/common/web_database_service.h"
31 #include "content/public/test/test_browser_thread.h"
32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h"
34
35 using base::Time;
36 using base::TimeDelta;
37 using base::WaitableEvent;
38 using content::BrowserThread;
39 using testing::_;
40 using testing::DoDefault;
41 using testing::ElementsAreArray;
42 using testing::Pointee;
43 using testing::Property;
44
45 static const int kWebDataServiceTimeoutSeconds = 8;
46
47 ACTION_P(SignalEvent, event) {
48 event->Signal();
49 }
50
51 class MockAutofillWebDataServiceObserver
52 : public AutofillWebDataServiceObserverOnDBThread {
53 public:
54 MOCK_METHOD1(AutofillEntriesChanged,
55 void(const AutofillChangeList& changes));
56 MOCK_METHOD1(AutofillProfileChanged,
57 void(const AutofillProfileChange& change));
58 };
59
60 class WebDataServiceTest : public testing::Test {
61 public:
62 WebDataServiceTest()
63 : ui_thread_(BrowserThread::UI, &message_loop_),
64 db_thread_(BrowserThread::DB) {}
65
66 protected:
67 virtual void SetUp() {
68 db_thread_.Start();
69
70 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
71 base::FilePath path = temp_dir_.path().AppendASCII("TestWebDB");
72
73 wdbs_ = new WebDatabaseService(path);
74 wdbs_->AddTable(scoped_ptr<WebDatabaseTable>(new AutofillTable("en-US")));
75 wdbs_->LoadDatabase(WebDatabaseService::InitCallback());
76
77 wds_ = new AutofillWebDataService(
78 wdbs_, WebDataServiceBase::ProfileErrorCallback());
79 wds_->Init();
80 }
81
82 virtual void TearDown() {
83 wds_->ShutdownOnUIThread();
84 wdbs_->ShutdownDatabase();
85 wds_ = NULL;
86 wdbs_ = NULL;
87 WaitForDatabaseThread();
88
89 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
90 MessageLoop::current()->Run();
91 db_thread_.Stop();
92 }
93
94 void WaitForDatabaseThread() {
95 base::WaitableEvent done(false, false);
96 BrowserThread::PostTask(
97 BrowserThread::DB,
98 FROM_HERE,
99 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done)));
100 done.Wait();
101 }
102
103 MessageLoopForUI message_loop_;
104 content::TestBrowserThread ui_thread_;
105 content::TestBrowserThread db_thread_;
106 base::FilePath profile_dir_;
107 scoped_refptr<AutofillWebDataService> wds_;
108 scoped_refptr<WebDatabaseService> wdbs_;
109 base::ScopedTempDir temp_dir_;
110 };
111
112 class WebDataServiceAutofillTest : public WebDataServiceTest {
113 public:
114 WebDataServiceAutofillTest()
115 : WebDataServiceTest(),
116 unique_id1_(1),
117 unique_id2_(2),
118 test_timeout_(TimeDelta::FromSeconds(kWebDataServiceTimeoutSeconds)),
119 done_event_(false, false) {}
120
121 protected:
122 virtual void SetUp() {
123 WebDataServiceTest::SetUp();
124 name1_ = ASCIIToUTF16("name1");
125 name2_ = ASCIIToUTF16("name2");
126 value1_ = ASCIIToUTF16("value1");
127 value2_ = ASCIIToUTF16("value2");
128
129 void(AutofillWebDataService::*add_observer_func)(
130 AutofillWebDataServiceObserverOnDBThread*) =
131 &AutofillWebDataService::AddObserver;
132 BrowserThread::PostTask(
133 BrowserThread::DB,
134 FROM_HERE,
135 base::Bind(add_observer_func, wds_, &observer_));
136 WaitForDatabaseThread();
137 }
138
139 virtual void TearDown() {
140 void(AutofillWebDataService::*remove_observer_func)(
141 AutofillWebDataServiceObserverOnDBThread*) =
142 &AutofillWebDataService::RemoveObserver;
143 BrowserThread::PostTask(
144 BrowserThread::DB,
145 FROM_HERE,
146 base::Bind(remove_observer_func, wds_, &observer_));
147 WaitForDatabaseThread();
148
149 WebDataServiceTest::TearDown();
150 }
151
152 void AppendFormField(const string16& name,
153 const string16& value,
154 std::vector<FormFieldData>* form_fields) {
155 FormFieldData field;
156 field.name = name;
157 field.value = value;
158 form_fields->push_back(field);
159 }
160
161 string16 name1_;
162 string16 name2_;
163 string16 value1_;
164 string16 value2_;
165 int unique_id1_, unique_id2_;
166 const TimeDelta test_timeout_;
167 testing::NiceMock<MockAutofillWebDataServiceObserver> observer_;
168 WaitableEvent done_event_;
169 };
170
171 // Simple consumer for Keywords data. Stores the result data and quits UI
172 // message loop when callback is invoked.
173 class KeywordsConsumer : public WebDataServiceConsumer {
174 public:
175 KeywordsConsumer() : load_succeeded(false) {}
176
177 virtual void OnWebDataServiceRequestDone(
178 WebDataService::Handle h,
179 const WDTypedResult* result) OVERRIDE {
180 if (result) {
181 load_succeeded = true;
182 DCHECK(result->GetType() == KEYWORDS_RESULT);
183 keywords_result =
184 reinterpret_cast<const WDResult<WDKeywordsResult>*>(
185 result)->GetValue();
186 }
187
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
189 MessageLoop::current()->Quit();
190 }
191
192 // True if keywords data was loaded successfully.
193 bool load_succeeded;
194 // Result data from completion callback.
195 WDKeywordsResult keywords_result;
196 };
197
198 TEST_F(WebDataServiceAutofillTest, FormFillAdd) {
199 const AutofillChange expected_changes[] = {
200 AutofillChange(AutofillChange::ADD, AutofillKey(name1_, value1_)),
201 AutofillChange(AutofillChange::ADD, AutofillKey(name2_, value2_))
202 };
203
204 // This will verify that the correct notification is triggered,
205 // passing the correct list of autofill keys in the details.
206 EXPECT_CALL(observer_,
207 AutofillEntriesChanged(ElementsAreArray(expected_changes)))
208 .WillOnce(SignalEvent(&done_event_));
209
210 std::vector<FormFieldData> form_fields;
211 AppendFormField(name1_, value1_, &form_fields);
212 AppendFormField(name2_, value2_, &form_fields);
213 wds_->AddFormFields(form_fields);
214
215 // The event will be signaled when the mock observer is notified.
216 done_event_.TimedWait(test_timeout_);
217
218 AutofillWebDataServiceConsumer<std::vector<string16> > consumer;
219 WebDataService::Handle handle;
220 static const int limit = 10;
221 handle = wds_->GetFormValuesForElementName(
222 name1_, string16(), limit, &consumer);
223
224 // The message loop will exit when the consumer is called.
225 MessageLoop::current()->Run();
226
227 EXPECT_EQ(handle, consumer.handle());
228 ASSERT_EQ(1U, consumer.result().size());
229 EXPECT_EQ(value1_, consumer.result()[0]);
230 }
231
232 TEST_F(WebDataServiceAutofillTest, FormFillRemoveOne) {
233 // First add some values to autofill.
234 EXPECT_CALL(observer_, AutofillEntriesChanged(_))
235 .WillOnce(SignalEvent(&done_event_));
236 std::vector<FormFieldData> form_fields;
237 AppendFormField(name1_, value1_, &form_fields);
238 wds_->AddFormFields(form_fields);
239
240 // The event will be signaled when the mock observer is notified.
241 done_event_.TimedWait(test_timeout_);
242
243 // This will verify that the correct notification is triggered,
244 // passing the correct list of autofill keys in the details.
245 const AutofillChange expected_changes[] = {
246 AutofillChange(AutofillChange::REMOVE, AutofillKey(name1_, value1_))
247 };
248 EXPECT_CALL(observer_,
249 AutofillEntriesChanged(ElementsAreArray(expected_changes)))
250 .WillOnce(SignalEvent(&done_event_));
251 wds_->RemoveFormValueForElementName(name1_, value1_);
252
253 // The event will be signaled when the mock observer is notified.
254 done_event_.TimedWait(test_timeout_);
255 }
256
257 TEST_F(WebDataServiceAutofillTest, FormFillRemoveMany) {
258 TimeDelta one_day(TimeDelta::FromDays(1));
259 Time t = Time::Now();
260
261 EXPECT_CALL(observer_, AutofillEntriesChanged(_))
262 .WillOnce(SignalEvent(&done_event_));
263
264 std::vector<FormFieldData> form_fields;
265 AppendFormField(name1_, value1_, &form_fields);
266 AppendFormField(name2_, value2_, &form_fields);
267 wds_->AddFormFields(form_fields);
268
269 // The event will be signaled when the mock observer is notified.
270 done_event_.TimedWait(test_timeout_);
271
272 // This will verify that the correct notification is triggered,
273 // passing the correct list of autofill keys in the details.
274 const AutofillChange expected_changes[] = {
275 AutofillChange(AutofillChange::REMOVE, AutofillKey(name1_, value1_)),
276 AutofillChange(AutofillChange::REMOVE, AutofillKey(name2_, value2_))
277 };
278 EXPECT_CALL(observer_,
279 AutofillEntriesChanged(ElementsAreArray(expected_changes)))
280 .WillOnce(SignalEvent(&done_event_));
281 wds_->RemoveFormElementsAddedBetween(t, t + one_day);
282
283 // The event will be signaled when the mock observer is notified.
284 done_event_.TimedWait(test_timeout_);
285 }
286
287 TEST_F(WebDataServiceAutofillTest, ProfileAdd) {
288 AutofillProfile profile;
289
290 // Check that GUID-based notification was sent.
291 const AutofillProfileChange expected_change(
292 AutofillProfileChange::ADD, profile.guid(), &profile);
293 EXPECT_CALL(observer_, AutofillProfileChanged(expected_change))
294 .WillOnce(SignalEvent(&done_event_));
295
296 wds_->AddAutofillProfile(profile);
297 done_event_.TimedWait(test_timeout_);
298
299 // Check that it was added.
300 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer;
301 WebDataService::Handle handle = wds_->GetAutofillProfiles(&consumer);
302 MessageLoop::current()->Run();
303 EXPECT_EQ(handle, consumer.handle());
304 ASSERT_EQ(1U, consumer.result().size());
305 EXPECT_EQ(profile, *consumer.result()[0]);
306 STLDeleteElements(&consumer.result());
307 }
308
309 TEST_F(WebDataServiceAutofillTest, ProfileRemove) {
310 AutofillProfile profile;
311
312 // Add a profile.
313 EXPECT_CALL(observer_, AutofillProfileChanged(_))
314 .WillOnce(SignalEvent(&done_event_));
315 wds_->AddAutofillProfile(profile);
316 done_event_.TimedWait(test_timeout_);
317
318 // Check that it was added.
319 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer;
320 WebDataService::Handle handle = wds_->GetAutofillProfiles(&consumer);
321 MessageLoop::current()->Run();
322 EXPECT_EQ(handle, consumer.handle());
323 ASSERT_EQ(1U, consumer.result().size());
324 EXPECT_EQ(profile, *consumer.result()[0]);
325 STLDeleteElements(&consumer.result());
326
327 // Check that GUID-based notification was sent.
328 const AutofillProfileChange expected_change(
329 AutofillProfileChange::REMOVE, profile.guid(), NULL);
330 EXPECT_CALL(observer_, AutofillProfileChanged(expected_change))
331 .WillOnce(SignalEvent(&done_event_));
332
333 // Remove the profile.
334 wds_->RemoveAutofillProfile(profile.guid());
335 done_event_.TimedWait(test_timeout_);
336
337 // Check that it was removed.
338 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer2;
339 WebDataService::Handle handle2 = wds_->GetAutofillProfiles(&consumer2);
340 MessageLoop::current()->Run();
341 EXPECT_EQ(handle2, consumer2.handle());
342 ASSERT_EQ(0U, consumer2.result().size());
343 }
344
345 TEST_F(WebDataServiceAutofillTest, ProfileUpdate) {
346 AutofillProfile profile1;
347 profile1.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Abe"));
348 AutofillProfile profile2;
349 profile2.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Alice"));
350
351 EXPECT_CALL(observer_, AutofillProfileChanged(_))
352 .WillOnce(DoDefault())
353 .WillOnce(SignalEvent(&done_event_));
354
355 wds_->AddAutofillProfile(profile1);
356 wds_->AddAutofillProfile(profile2);
357 done_event_.TimedWait(test_timeout_);
358
359 // Check that they were added.
360 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer;
361 WebDataService::Handle handle = wds_->GetAutofillProfiles(&consumer);
362 MessageLoop::current()->Run();
363 EXPECT_EQ(handle, consumer.handle());
364 ASSERT_EQ(2U, consumer.result().size());
365 EXPECT_EQ(profile1, *consumer.result()[0]);
366 EXPECT_EQ(profile2, *consumer.result()[1]);
367 STLDeleteElements(&consumer.result());
368
369 AutofillProfile profile1_changed(profile1);
370 profile1_changed.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bill"));
371 const AutofillProfileChange expected_change(
372 AutofillProfileChange::UPDATE, profile1.guid(), &profile1_changed);
373
374 EXPECT_CALL(observer_, AutofillProfileChanged(expected_change))
375 .WillOnce(SignalEvent(&done_event_));
376
377 // Update the profile.
378 wds_->UpdateAutofillProfile(profile1_changed);
379 done_event_.TimedWait(test_timeout_);
380
381 // Check that the updates were made.
382 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer2;
383 WebDataService::Handle handle2 = wds_->GetAutofillProfiles(&consumer2);
384 MessageLoop::current()->Run();
385 EXPECT_EQ(handle2, consumer2.handle());
386 ASSERT_EQ(2U, consumer2.result().size());
387 EXPECT_NE(profile1, *consumer2.result()[0]);
388 EXPECT_EQ(profile1_changed, *consumer2.result()[0]);
389 EXPECT_EQ(profile2, *consumer2.result()[1]);
390 STLDeleteElements(&consumer2.result());
391 }
392
393 TEST_F(WebDataServiceAutofillTest, CreditAdd) {
394 CreditCard card;
395 wds_->AddCreditCard(card);
396 WaitForDatabaseThread();
397
398 // Check that it was added.
399 AutofillWebDataServiceConsumer<std::vector<CreditCard*> > consumer;
400 WebDataService::Handle handle = wds_->GetCreditCards(&consumer);
401 MessageLoop::current()->Run();
402 EXPECT_EQ(handle, consumer.handle());
403 ASSERT_EQ(1U, consumer.result().size());
404 EXPECT_EQ(card, *consumer.result()[0]);
405 STLDeleteElements(&consumer.result());
406 }
407
408 TEST_F(WebDataServiceAutofillTest, CreditCardRemove) {
409 CreditCard credit_card;
410
411 // Add a credit card.
412 wds_->AddCreditCard(credit_card);
413 WaitForDatabaseThread();
414
415 // Check that it was added.
416 AutofillWebDataServiceConsumer<std::vector<CreditCard*> > consumer;
417 WebDataService::Handle handle = wds_->GetCreditCards(&consumer);
418 MessageLoop::current()->Run();
419 EXPECT_EQ(handle, consumer.handle());
420 ASSERT_EQ(1U, consumer.result().size());
421 EXPECT_EQ(credit_card, *consumer.result()[0]);
422 STLDeleteElements(&consumer.result());
423
424 // Remove the credit card.
425 wds_->RemoveCreditCard(credit_card.guid());
426 WaitForDatabaseThread();
427
428 // Check that it was removed.
429 AutofillWebDataServiceConsumer<std::vector<CreditCard*> > consumer2;
430 WebDataService::Handle handle2 = wds_->GetCreditCards(&consumer2);
431 MessageLoop::current()->Run();
432 EXPECT_EQ(handle2, consumer2.handle());
433 ASSERT_EQ(0U, consumer2.result().size());
434 }
435
436 TEST_F(WebDataServiceAutofillTest, CreditUpdate) {
437 CreditCard card1;
438 card1.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Abe"));
439 CreditCard card2;
440 card2.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Alice"));
441
442 wds_->AddCreditCard(card1);
443 wds_->AddCreditCard(card2);
444 WaitForDatabaseThread();
445
446 // Check that they got added.
447 AutofillWebDataServiceConsumer<std::vector<CreditCard*> > consumer;
448 WebDataService::Handle handle = wds_->GetCreditCards(&consumer);
449 MessageLoop::current()->Run();
450 EXPECT_EQ(handle, consumer.handle());
451 ASSERT_EQ(2U, consumer.result().size());
452 EXPECT_EQ(card1, *consumer.result()[0]);
453 EXPECT_EQ(card2, *consumer.result()[1]);
454 STLDeleteElements(&consumer.result());
455
456 CreditCard card1_changed(card1);
457 card1_changed.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Bill"));
458
459 wds_->UpdateCreditCard(card1_changed);
460 WaitForDatabaseThread();
461
462 // Check that the updates were made.
463 AutofillWebDataServiceConsumer<std::vector<CreditCard*> > consumer2;
464 WebDataService::Handle handle2 = wds_->GetCreditCards(&consumer2);
465 MessageLoop::current()->Run();
466 EXPECT_EQ(handle2, consumer2.handle());
467 ASSERT_EQ(2U, consumer2.result().size());
468 EXPECT_NE(card1, *consumer2.result()[0]);
469 EXPECT_EQ(card1_changed, *consumer2.result()[0]);
470 EXPECT_EQ(card2, *consumer2.result()[1]);
471 STLDeleteElements(&consumer2.result());
472 }
473
474 TEST_F(WebDataServiceAutofillTest, AutofillRemoveModifiedBetween) {
475 // Add a profile.
476 EXPECT_CALL(observer_, AutofillProfileChanged(_))
477 .WillOnce(SignalEvent(&done_event_));
478 AutofillProfile profile;
479 wds_->AddAutofillProfile(profile);
480 done_event_.TimedWait(test_timeout_);
481
482 // Check that it was added.
483 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> >
484 profile_consumer;
485 WebDataService::Handle handle = wds_->GetAutofillProfiles(&profile_consumer);
486 MessageLoop::current()->Run();
487 EXPECT_EQ(handle, profile_consumer.handle());
488 ASSERT_EQ(1U, profile_consumer.result().size());
489 EXPECT_EQ(profile, *profile_consumer.result()[0]);
490 STLDeleteElements(&profile_consumer.result());
491
492 // Add a credit card.
493 CreditCard credit_card;
494 wds_->AddCreditCard(credit_card);
495 WaitForDatabaseThread();
496
497 // Check that it was added.
498 AutofillWebDataServiceConsumer<std::vector<CreditCard*> > card_consumer;
499 handle = wds_->GetCreditCards(&card_consumer);
500 MessageLoop::current()->Run();
501 EXPECT_EQ(handle, card_consumer.handle());
502 ASSERT_EQ(1U, card_consumer.result().size());
503 EXPECT_EQ(credit_card, *card_consumer.result()[0]);
504 STLDeleteElements(&card_consumer.result());
505
506 // Check that GUID-based notification was sent for the profile.
507 const AutofillProfileChange expected_profile_change(
508 AutofillProfileChange::REMOVE, profile.guid(), NULL);
509 EXPECT_CALL(observer_, AutofillProfileChanged(expected_profile_change))
510 .WillOnce(SignalEvent(&done_event_));
511
512 // Remove the profile using time range of "all time".
513 wds_->RemoveAutofillDataModifiedBetween(Time(), Time());
514 done_event_.TimedWait(test_timeout_);
515 WaitForDatabaseThread();
516
517 // Check that the profile was removed.
518 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> >
519 profile_consumer2;
520 WebDataService::Handle handle2 =
521 wds_->GetAutofillProfiles(&profile_consumer2);
522 MessageLoop::current()->Run();
523 EXPECT_EQ(handle2, profile_consumer2.handle());
524 ASSERT_EQ(0U, profile_consumer2.result().size());
525
526 // Check that the credit card was removed.
527 AutofillWebDataServiceConsumer<std::vector<CreditCard*> > card_consumer2;
528 handle2 = wds_->GetCreditCards(&card_consumer2);
529 MessageLoop::current()->Run();
530 EXPECT_EQ(handle2, card_consumer2.handle());
531 ASSERT_EQ(0U, card_consumer2.result().size());
532 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698