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

Side by Side Diff: chrome/browser/webdata/web_data_service_unittest.cc

Issue 12476031: Refactor notifications of chrome/browser/webdata (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 9 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
« no previous file with comments | « chrome/browser/webdata/web_data_service_factory.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h" 12 #include "base/memory/scoped_vector.h"
13 #include "base/message_loop.h" 13 #include "base/message_loop.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "base/string16.h" 15 #include "base/string16.h"
16 #include "base/string_util.h" 16 #include "base/string_util.h"
17 #include "base/synchronization/waitable_event.h" 17 #include "base/synchronization/waitable_event.h"
18 #include "base/time.h" 18 #include "base/time.h"
19 #include "base/utf_string_conversions.h" 19 #include "base/utf_string_conversions.h"
20 #include "chrome/browser/webdata/autofill_change.h" 20 #include "chrome/browser/webdata/autofill_change.h"
21 #include "chrome/browser/webdata/autofill_entry.h" 21 #include "chrome/browser/webdata/autofill_entry.h"
22 #include "chrome/browser/webdata/autofill_table.h" 22 #include "chrome/browser/webdata/autofill_table.h"
23 #include "chrome/browser/webdata/autofill_web_data_service.h" 23 #include "chrome/browser/webdata/autofill_web_data_service.h"
24 #include "chrome/browser/webdata/autofill_web_data_service_observer.h"
24 #include "chrome/browser/webdata/web_data_service_test_util.h" 25 #include "chrome/browser/webdata/web_data_service_test_util.h"
25 #include "chrome/browser/webdata/web_database_service.h" 26 #include "chrome/browser/webdata/web_database_service.h"
26 #include "chrome/common/chrome_notification_types.h"
27 #include "chrome/test/base/thread_observer_helper.h"
28 #include "components/autofill/browser/autofill_country.h" 27 #include "components/autofill/browser/autofill_country.h"
29 #include "components/autofill/browser/autofill_profile.h" 28 #include "components/autofill/browser/autofill_profile.h"
30 #include "components/autofill/browser/credit_card.h" 29 #include "components/autofill/browser/credit_card.h"
31 #include "components/autofill/common/form_field_data.h" 30 #include "components/autofill/common/form_field_data.h"
32 #include "content/public/browser/notification_details.h"
33 #include "content/public/browser/notification_service.h"
34 #include "content/public/test/test_browser_thread.h" 31 #include "content/public/test/test_browser_thread.h"
35 #include "testing/gmock/include/gmock/gmock.h" 32 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
37 34
38 using base::Time; 35 using base::Time;
39 using base::TimeDelta; 36 using base::TimeDelta;
40 using base::WaitableEvent; 37 using base::WaitableEvent;
41 using content::BrowserThread; 38 using content::BrowserThread;
42 using testing::_; 39 using testing::_;
43 using testing::DoDefault; 40 using testing::DoDefault;
44 using testing::ElementsAreArray; 41 using testing::ElementsAreArray;
45 using testing::Pointee; 42 using testing::Pointee;
46 using testing::Property; 43 using testing::Property;
47 44
48 typedef std::vector<AutofillChange> AutofillChangeList;
49
50 static const int kWebDataServiceTimeoutSeconds = 8; 45 static const int kWebDataServiceTimeoutSeconds = 8;
51 46
52 ACTION_P(SignalEvent, event) { 47 ACTION_P(SignalEvent, event) {
53 event->Signal(); 48 event->Signal();
54 } 49 }
55 50
56 class AutofillDBThreadObserverHelper : public DBThreadObserverHelper { 51 class MockAutofillWebDataServiceObserver
57 protected: 52 : public AutofillWebDataServiceObserverOnDBThread {
58 virtual ~AutofillDBThreadObserverHelper() {} 53 public:
59 54 MOCK_METHOD1(AutofillEntriesChanged,
60 virtual void RegisterObservers() OVERRIDE { 55 void(const AutofillChangeList& changes));
61 registrar_.Add(&observer_, 56 MOCK_METHOD1(AutofillProfileChanged,
62 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, 57 void(const AutofillProfileChange& change));
63 content::NotificationService::AllSources());
64 registrar_.Add(&observer_,
65 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED,
66 content::NotificationService::AllSources());
67 }
68 }; 58 };
69 59
70 class WebDataServiceTest : public testing::Test { 60 class WebDataServiceTest : public testing::Test {
71 public: 61 public:
72 WebDataServiceTest() 62 WebDataServiceTest()
73 : ui_thread_(BrowserThread::UI, &message_loop_), 63 : ui_thread_(BrowserThread::UI, &message_loop_),
74 db_thread_(BrowserThread::DB) {} 64 db_thread_(BrowserThread::DB) {}
75 65
76 protected: 66 protected:
77 virtual void SetUp() { 67 virtual void SetUp() {
78 db_thread_.Start(); 68 db_thread_.Start();
79 69
80 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 70 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
81 base::FilePath path = temp_dir_.path().AppendASCII("TestWebDB"); 71 base::FilePath path = temp_dir_.path().AppendASCII("TestWebDB");
82 72
83 wdbs_ = new WebDatabaseService(path, AutofillCountry::ApplicationLocale()); 73 wdbs_ = new WebDatabaseService(path, AutofillCountry::ApplicationLocale());
84 wdbs_->AddTable(scoped_ptr<WebDatabaseTable>(new AutofillTable())); 74 wdbs_->AddTable(scoped_ptr<WebDatabaseTable>(new AutofillTable()));
85 wdbs_->LoadDatabase(WebDatabaseService::InitCallback()); 75 wdbs_->LoadDatabase(WebDatabaseService::InitCallback());
86 76
87 wds_ = new AutofillWebDataService(wdbs_, 77 wds_ = new AutofillWebDataService(
88 WebDataServiceBase::ProfileErrorCallback()); 78 wdbs_, WebDataServiceBase::ProfileErrorCallback());
89 wds_->Init(); 79 wds_->Init();
90 } 80 }
91 81
92 virtual void TearDown() { 82 virtual void TearDown() {
93 wds_->ShutdownOnUIThread(); 83 wds_->ShutdownOnUIThread();
94 wdbs_->ShutdownDatabase(); 84 wdbs_->ShutdownDatabase();
95 wds_ = NULL; 85 wds_ = NULL;
96 wdbs_ = NULL; 86 wdbs_ = NULL;
97 WaitForDatabaseThread(); 87 WaitForDatabaseThread();
98 88
(...skipping 29 matching lines...) Expand all
128 test_timeout_(TimeDelta::FromSeconds(kWebDataServiceTimeoutSeconds)), 118 test_timeout_(TimeDelta::FromSeconds(kWebDataServiceTimeoutSeconds)),
129 done_event_(false, false) {} 119 done_event_(false, false) {}
130 120
131 protected: 121 protected:
132 virtual void SetUp() { 122 virtual void SetUp() {
133 WebDataServiceTest::SetUp(); 123 WebDataServiceTest::SetUp();
134 name1_ = ASCIIToUTF16("name1"); 124 name1_ = ASCIIToUTF16("name1");
135 name2_ = ASCIIToUTF16("name2"); 125 name2_ = ASCIIToUTF16("name2");
136 value1_ = ASCIIToUTF16("value1"); 126 value1_ = ASCIIToUTF16("value1");
137 value2_ = ASCIIToUTF16("value2"); 127 value2_ = ASCIIToUTF16("value2");
138 observer_helper_ = new AutofillDBThreadObserverHelper(); 128
139 observer_helper_->Init(); 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();
140 } 137 }
141 138
142 virtual void TearDown() { 139 virtual void TearDown() {
143 // Release this first so it can get destructed on the db thread. 140 void(AutofillWebDataService::*remove_observer_func)(
144 observer_helper_ = NULL; 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
145 WebDataServiceTest::TearDown(); 149 WebDataServiceTest::TearDown();
146 } 150 }
147 151
148 void AppendFormField(const string16& name, 152 void AppendFormField(const string16& name,
149 const string16& value, 153 const string16& value,
150 std::vector<FormFieldData>* form_fields) { 154 std::vector<FormFieldData>* form_fields) {
151 FormFieldData field; 155 FormFieldData field;
152 field.name = name; 156 field.name = name;
153 field.value = value; 157 field.value = value;
154 form_fields->push_back(field); 158 form_fields->push_back(field);
155 } 159 }
156 160
157 string16 name1_; 161 string16 name1_;
158 string16 name2_; 162 string16 name2_;
159 string16 value1_; 163 string16 value1_;
160 string16 value2_; 164 string16 value2_;
161 int unique_id1_, unique_id2_; 165 int unique_id1_, unique_id2_;
162 const TimeDelta test_timeout_; 166 const TimeDelta test_timeout_;
163 scoped_refptr<AutofillDBThreadObserverHelper> observer_helper_; 167 testing::NiceMock<MockAutofillWebDataServiceObserver> observer_;
164 WaitableEvent done_event_; 168 WaitableEvent done_event_;
165 }; 169 };
166 170
167 // Simple consumer for Keywords data. Stores the result data and quits UI 171 // Simple consumer for Keywords data. Stores the result data and quits UI
168 // message loop when callback is invoked. 172 // message loop when callback is invoked.
169 class KeywordsConsumer : public WebDataServiceConsumer { 173 class KeywordsConsumer : public WebDataServiceConsumer {
170 public: 174 public:
171 KeywordsConsumer() : load_succeeded(false) {} 175 KeywordsConsumer() : load_succeeded(false) {}
172 176
173 virtual void OnWebDataServiceRequestDone( 177 virtual void OnWebDataServiceRequestDone(
(...skipping 18 matching lines...) Expand all
192 }; 196 };
193 197
194 TEST_F(WebDataServiceAutofillTest, FormFillAdd) { 198 TEST_F(WebDataServiceAutofillTest, FormFillAdd) {
195 const AutofillChange expected_changes[] = { 199 const AutofillChange expected_changes[] = {
196 AutofillChange(AutofillChange::ADD, AutofillKey(name1_, value1_)), 200 AutofillChange(AutofillChange::ADD, AutofillKey(name1_, value1_)),
197 AutofillChange(AutofillChange::ADD, AutofillKey(name2_, value2_)) 201 AutofillChange(AutofillChange::ADD, AutofillKey(name2_, value2_))
198 }; 202 };
199 203
200 // This will verify that the correct notification is triggered, 204 // This will verify that the correct notification is triggered,
201 // passing the correct list of autofill keys in the details. 205 // passing the correct list of autofill keys in the details.
202 EXPECT_CALL( 206 EXPECT_CALL(observer_,
203 *observer_helper_->observer(), 207 AutofillEntriesChanged(ElementsAreArray(expected_changes)))
204 Observe(int(chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED), 208 .WillOnce(SignalEvent(&done_event_));
205 content::Source<AutofillWebDataService>(wds_.get()),
206 Property(&content::Details<const AutofillChangeList>::ptr,
207 Pointee(ElementsAreArray(expected_changes))))).
208 WillOnce(SignalEvent(&done_event_));
209 209
210 std::vector<FormFieldData> form_fields; 210 std::vector<FormFieldData> form_fields;
211 AppendFormField(name1_, value1_, &form_fields); 211 AppendFormField(name1_, value1_, &form_fields);
212 AppendFormField(name2_, value2_, &form_fields); 212 AppendFormField(name2_, value2_, &form_fields);
213 wds_->AddFormFields(form_fields); 213 wds_->AddFormFields(form_fields);
214 214
215 // The event will be signaled when the mock observer is notified. 215 // The event will be signaled when the mock observer is notified.
216 done_event_.TimedWait(test_timeout_); 216 done_event_.TimedWait(test_timeout_);
217 217
218 AutofillWebDataServiceConsumer<std::vector<string16> > consumer; 218 AutofillWebDataServiceConsumer<std::vector<string16> > consumer;
219 WebDataService::Handle handle; 219 WebDataService::Handle handle;
220 static const int limit = 10; 220 static const int limit = 10;
221 handle = wds_->GetFormValuesForElementName( 221 handle = wds_->GetFormValuesForElementName(
222 name1_, string16(), limit, &consumer); 222 name1_, string16(), limit, &consumer);
223 223
224 // The message loop will exit when the consumer is called. 224 // The message loop will exit when the consumer is called.
225 MessageLoop::current()->Run(); 225 MessageLoop::current()->Run();
226 226
227 EXPECT_EQ(handle, consumer.handle()); 227 EXPECT_EQ(handle, consumer.handle());
228 ASSERT_EQ(1U, consumer.result().size()); 228 ASSERT_EQ(1U, consumer.result().size());
229 EXPECT_EQ(value1_, consumer.result()[0]); 229 EXPECT_EQ(value1_, consumer.result()[0]);
230 } 230 }
231 231
232 TEST_F(WebDataServiceAutofillTest, FormFillRemoveOne) { 232 TEST_F(WebDataServiceAutofillTest, FormFillRemoveOne) {
233 // First add some values to autofill. 233 // First add some values to autofill.
234 EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). 234 EXPECT_CALL(observer_, AutofillEntriesChanged(_))
235 WillOnce(SignalEvent(&done_event_)); 235 .WillOnce(SignalEvent(&done_event_));
236 std::vector<FormFieldData> form_fields; 236 std::vector<FormFieldData> form_fields;
237 AppendFormField(name1_, value1_, &form_fields); 237 AppendFormField(name1_, value1_, &form_fields);
238 wds_->AddFormFields(form_fields); 238 wds_->AddFormFields(form_fields);
239 239
240 // The event will be signaled when the mock observer is notified. 240 // The event will be signaled when the mock observer is notified.
241 done_event_.TimedWait(test_timeout_); 241 done_event_.TimedWait(test_timeout_);
242 242
243 // This will verify that the correct notification is triggered, 243 // This will verify that the correct notification is triggered,
244 // passing the correct list of autofill keys in the details. 244 // passing the correct list of autofill keys in the details.
245 const AutofillChange expected_changes[] = { 245 const AutofillChange expected_changes[] = {
246 AutofillChange(AutofillChange::REMOVE, AutofillKey(name1_, value1_)) 246 AutofillChange(AutofillChange::REMOVE, AutofillKey(name1_, value1_))
247 }; 247 };
248 EXPECT_CALL( 248 EXPECT_CALL(observer_,
249 *observer_helper_->observer(), 249 AutofillEntriesChanged(ElementsAreArray(expected_changes)))
250 Observe(int(chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED), 250 .WillOnce(SignalEvent(&done_event_));
251 content::Source<AutofillWebDataService>(wds_.get()),
252 Property(&content::Details<const AutofillChangeList>::ptr,
253 Pointee(ElementsAreArray(expected_changes))))).
254 WillOnce(SignalEvent(&done_event_));
255 wds_->RemoveFormValueForElementName(name1_, value1_); 251 wds_->RemoveFormValueForElementName(name1_, value1_);
256 252
257 // The event will be signaled when the mock observer is notified. 253 // The event will be signaled when the mock observer is notified.
258 done_event_.TimedWait(test_timeout_); 254 done_event_.TimedWait(test_timeout_);
259 } 255 }
260 256
261 TEST_F(WebDataServiceAutofillTest, FormFillRemoveMany) { 257 TEST_F(WebDataServiceAutofillTest, FormFillRemoveMany) {
262 TimeDelta one_day(TimeDelta::FromDays(1)); 258 TimeDelta one_day(TimeDelta::FromDays(1));
263 Time t = Time::Now(); 259 Time t = Time::Now();
264 260
265 EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). 261 EXPECT_CALL(observer_, AutofillEntriesChanged(_))
266 WillOnce(SignalEvent(&done_event_)); 262 .WillOnce(SignalEvent(&done_event_));
263
267 std::vector<FormFieldData> form_fields; 264 std::vector<FormFieldData> form_fields;
268 AppendFormField(name1_, value1_, &form_fields); 265 AppendFormField(name1_, value1_, &form_fields);
269 AppendFormField(name2_, value2_, &form_fields); 266 AppendFormField(name2_, value2_, &form_fields);
270 wds_->AddFormFields(form_fields); 267 wds_->AddFormFields(form_fields);
271 268
272 // The event will be signaled when the mock observer is notified. 269 // The event will be signaled when the mock observer is notified.
273 done_event_.TimedWait(test_timeout_); 270 done_event_.TimedWait(test_timeout_);
274 271
275 // This will verify that the correct notification is triggered, 272 // This will verify that the correct notification is triggered,
276 // passing the correct list of autofill keys in the details. 273 // passing the correct list of autofill keys in the details.
277 const AutofillChange expected_changes[] = { 274 const AutofillChange expected_changes[] = {
278 AutofillChange(AutofillChange::REMOVE, AutofillKey(name1_, value1_)), 275 AutofillChange(AutofillChange::REMOVE, AutofillKey(name1_, value1_)),
279 AutofillChange(AutofillChange::REMOVE, AutofillKey(name2_, value2_)) 276 AutofillChange(AutofillChange::REMOVE, AutofillKey(name2_, value2_))
280 }; 277 };
281 EXPECT_CALL( 278 EXPECT_CALL(observer_,
282 *observer_helper_->observer(), 279 AutofillEntriesChanged(ElementsAreArray(expected_changes)))
283 Observe(int(chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED), 280 .WillOnce(SignalEvent(&done_event_));
284 content::Source<AutofillWebDataService>(wds_.get()),
285 Property(&content::Details<const AutofillChangeList>::ptr,
286 Pointee(ElementsAreArray(expected_changes))))).
287 WillOnce(SignalEvent(&done_event_));
288 wds_->RemoveFormElementsAddedBetween(t, t + one_day); 281 wds_->RemoveFormElementsAddedBetween(t, t + one_day);
289 282
290 // The event will be signaled when the mock observer is notified. 283 // The event will be signaled when the mock observer is notified.
291 done_event_.TimedWait(test_timeout_); 284 done_event_.TimedWait(test_timeout_);
292 } 285 }
293 286
294 TEST_F(WebDataServiceAutofillTest, ProfileAdd) { 287 TEST_F(WebDataServiceAutofillTest, ProfileAdd) {
295 AutofillProfile profile; 288 AutofillProfile profile;
296 289
297 // Check that GUID-based notification was sent. 290 // Check that GUID-based notification was sent.
298 const AutofillProfileChange expected_change( 291 const AutofillProfileChange expected_change(
299 AutofillProfileChange::ADD, profile.guid(), &profile); 292 AutofillProfileChange::ADD, profile.guid(), &profile);
300 EXPECT_CALL( 293 EXPECT_CALL(observer_, AutofillProfileChanged(expected_change))
301 *observer_helper_->observer(), 294 .WillOnce(SignalEvent(&done_event_));
302 Observe(int(chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED),
303 content::Source<AutofillWebDataService>(wds_.get()),
304 Property(&content::Details<const AutofillProfileChange>::ptr,
305 Pointee(expected_change)))).
306 WillOnce(SignalEvent(&done_event_));
307 295
308 wds_->AddAutofillProfile(profile); 296 wds_->AddAutofillProfile(profile);
309 done_event_.TimedWait(test_timeout_); 297 done_event_.TimedWait(test_timeout_);
310 298
311 // Check that it was added. 299 // Check that it was added.
312 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer; 300 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer;
313 WebDataService::Handle handle = wds_->GetAutofillProfiles(&consumer); 301 WebDataService::Handle handle = wds_->GetAutofillProfiles(&consumer);
314 MessageLoop::current()->Run(); 302 MessageLoop::current()->Run();
315 EXPECT_EQ(handle, consumer.handle()); 303 EXPECT_EQ(handle, consumer.handle());
316 ASSERT_EQ(1U, consumer.result().size()); 304 ASSERT_EQ(1U, consumer.result().size());
317 EXPECT_EQ(profile, *consumer.result()[0]); 305 EXPECT_EQ(profile, *consumer.result()[0]);
318 STLDeleteElements(&consumer.result()); 306 STLDeleteElements(&consumer.result());
319 } 307 }
320 308
321 TEST_F(WebDataServiceAutofillTest, ProfileRemove) { 309 TEST_F(WebDataServiceAutofillTest, ProfileRemove) {
322 AutofillProfile profile; 310 AutofillProfile profile;
323 311
324 // Add a profile. 312 // Add a profile.
325 EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). 313 EXPECT_CALL(observer_, AutofillProfileChanged(_))
326 Times(1). 314 .WillOnce(SignalEvent(&done_event_));
327 WillOnce(SignalEvent(&done_event_));
328 wds_->AddAutofillProfile(profile); 315 wds_->AddAutofillProfile(profile);
329 done_event_.TimedWait(test_timeout_); 316 done_event_.TimedWait(test_timeout_);
330 317
331 // Check that it was added. 318 // Check that it was added.
332 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer; 319 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer;
333 WebDataService::Handle handle = wds_->GetAutofillProfiles(&consumer); 320 WebDataService::Handle handle = wds_->GetAutofillProfiles(&consumer);
334 MessageLoop::current()->Run(); 321 MessageLoop::current()->Run();
335 EXPECT_EQ(handle, consumer.handle()); 322 EXPECT_EQ(handle, consumer.handle());
336 ASSERT_EQ(1U, consumer.result().size()); 323 ASSERT_EQ(1U, consumer.result().size());
337 EXPECT_EQ(profile, *consumer.result()[0]); 324 EXPECT_EQ(profile, *consumer.result()[0]);
338 STLDeleteElements(&consumer.result()); 325 STLDeleteElements(&consumer.result());
339 326
340 // Check that GUID-based notification was sent. 327 // Check that GUID-based notification was sent.
341 const AutofillProfileChange expected_change( 328 const AutofillProfileChange expected_change(
342 AutofillProfileChange::REMOVE, profile.guid(), NULL); 329 AutofillProfileChange::REMOVE, profile.guid(), NULL);
343 EXPECT_CALL( 330 EXPECT_CALL(observer_, AutofillProfileChanged(expected_change))
344 *observer_helper_->observer(), 331 .WillOnce(SignalEvent(&done_event_));
345 Observe(int(chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED),
346 content::Source<AutofillWebDataService>(wds_.get()),
347 Property(&content::Details<const AutofillProfileChange>::ptr,
348 Pointee(expected_change)))).
349 WillOnce(SignalEvent(&done_event_));
350 332
351 // Remove the profile. 333 // Remove the profile.
352 wds_->RemoveAutofillProfile(profile.guid()); 334 wds_->RemoveAutofillProfile(profile.guid());
353 done_event_.TimedWait(test_timeout_); 335 done_event_.TimedWait(test_timeout_);
354 336
355 // Check that it was removed. 337 // Check that it was removed.
356 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer2; 338 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer2;
357 WebDataService::Handle handle2 = wds_->GetAutofillProfiles(&consumer2); 339 WebDataService::Handle handle2 = wds_->GetAutofillProfiles(&consumer2);
358 MessageLoop::current()->Run(); 340 MessageLoop::current()->Run();
359 EXPECT_EQ(handle2, consumer2.handle()); 341 EXPECT_EQ(handle2, consumer2.handle());
360 ASSERT_EQ(0U, consumer2.result().size()); 342 ASSERT_EQ(0U, consumer2.result().size());
361 } 343 }
362 344
363 TEST_F(WebDataServiceAutofillTest, ProfileUpdate) { 345 TEST_F(WebDataServiceAutofillTest, ProfileUpdate) {
364 AutofillProfile profile1; 346 AutofillProfile profile1;
365 profile1.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Abe")); 347 profile1.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Abe"));
366 AutofillProfile profile2; 348 AutofillProfile profile2;
367 profile2.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Alice")); 349 profile2.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Alice"));
368 350
369 EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). 351 EXPECT_CALL(observer_, AutofillProfileChanged(_))
370 WillOnce(DoDefault()). 352 .WillOnce(DoDefault())
371 WillOnce(SignalEvent(&done_event_)); 353 .WillOnce(SignalEvent(&done_event_));
354
372 wds_->AddAutofillProfile(profile1); 355 wds_->AddAutofillProfile(profile1);
373 wds_->AddAutofillProfile(profile2); 356 wds_->AddAutofillProfile(profile2);
374 done_event_.TimedWait(test_timeout_); 357 done_event_.TimedWait(test_timeout_);
375 358
376 // Check that they were added. 359 // Check that they were added.
377 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer; 360 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer;
378 WebDataService::Handle handle = wds_->GetAutofillProfiles(&consumer); 361 WebDataService::Handle handle = wds_->GetAutofillProfiles(&consumer);
379 MessageLoop::current()->Run(); 362 MessageLoop::current()->Run();
380 EXPECT_EQ(handle, consumer.handle()); 363 EXPECT_EQ(handle, consumer.handle());
381 ASSERT_EQ(2U, consumer.result().size()); 364 ASSERT_EQ(2U, consumer.result().size());
382 EXPECT_EQ(profile1, *consumer.result()[0]); 365 EXPECT_EQ(profile1, *consumer.result()[0]);
383 EXPECT_EQ(profile2, *consumer.result()[1]); 366 EXPECT_EQ(profile2, *consumer.result()[1]);
384 STLDeleteElements(&consumer.result()); 367 STLDeleteElements(&consumer.result());
385 368
386 AutofillProfile profile1_changed(profile1); 369 AutofillProfile profile1_changed(profile1);
387 profile1_changed.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bill")); 370 profile1_changed.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Bill"));
388 const AutofillProfileChange expected_change( 371 const AutofillProfileChange expected_change(
389 AutofillProfileChange::UPDATE, profile1.guid(), &profile1_changed); 372 AutofillProfileChange::UPDATE, profile1.guid(), &profile1_changed);
390 373
391 EXPECT_CALL( 374 EXPECT_CALL(observer_, AutofillProfileChanged(expected_change))
392 *observer_helper_->observer(), 375 .WillOnce(SignalEvent(&done_event_));
393 Observe(int(chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED),
394 content::Source<AutofillWebDataService>(wds_.get()),
395 Property(&content::Details<const AutofillProfileChange>::ptr,
396 Pointee(expected_change)))).
397 WillOnce(SignalEvent(&done_event_));
398 376
399 // Update the profile. 377 // Update the profile.
400 wds_->UpdateAutofillProfile(profile1_changed); 378 wds_->UpdateAutofillProfile(profile1_changed);
401 done_event_.TimedWait(test_timeout_); 379 done_event_.TimedWait(test_timeout_);
402 380
403 // Check that the updates were made. 381 // Check that the updates were made.
404 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer2; 382 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > consumer2;
405 WebDataService::Handle handle2 = wds_->GetAutofillProfiles(&consumer2); 383 WebDataService::Handle handle2 = wds_->GetAutofillProfiles(&consumer2);
406 MessageLoop::current()->Run(); 384 MessageLoop::current()->Run();
407 EXPECT_EQ(handle2, consumer2.handle()); 385 EXPECT_EQ(handle2, consumer2.handle());
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 EXPECT_EQ(handle2, consumer2.handle()); 466 EXPECT_EQ(handle2, consumer2.handle());
489 ASSERT_EQ(2U, consumer2.result().size()); 467 ASSERT_EQ(2U, consumer2.result().size());
490 EXPECT_NE(card1, *consumer2.result()[0]); 468 EXPECT_NE(card1, *consumer2.result()[0]);
491 EXPECT_EQ(card1_changed, *consumer2.result()[0]); 469 EXPECT_EQ(card1_changed, *consumer2.result()[0]);
492 EXPECT_EQ(card2, *consumer2.result()[1]); 470 EXPECT_EQ(card2, *consumer2.result()[1]);
493 STLDeleteElements(&consumer2.result()); 471 STLDeleteElements(&consumer2.result());
494 } 472 }
495 473
496 TEST_F(WebDataServiceAutofillTest, AutofillRemoveModifiedBetween) { 474 TEST_F(WebDataServiceAutofillTest, AutofillRemoveModifiedBetween) {
497 // Add a profile. 475 // Add a profile.
498 EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). 476 EXPECT_CALL(observer_, AutofillProfileChanged(_))
499 Times(1). 477 .WillOnce(SignalEvent(&done_event_));
500 WillOnce(SignalEvent(&done_event_));
501 AutofillProfile profile; 478 AutofillProfile profile;
502 wds_->AddAutofillProfile(profile); 479 wds_->AddAutofillProfile(profile);
503 done_event_.TimedWait(test_timeout_); 480 done_event_.TimedWait(test_timeout_);
504 481
505 // Check that it was added. 482 // Check that it was added.
506 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > 483 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> >
507 profile_consumer; 484 profile_consumer;
508 WebDataService::Handle handle = wds_->GetAutofillProfiles(&profile_consumer); 485 WebDataService::Handle handle = wds_->GetAutofillProfiles(&profile_consumer);
509 MessageLoop::current()->Run(); 486 MessageLoop::current()->Run();
510 EXPECT_EQ(handle, profile_consumer.handle()); 487 EXPECT_EQ(handle, profile_consumer.handle());
(...skipping 11 matching lines...) Expand all
522 handle = wds_->GetCreditCards(&card_consumer); 499 handle = wds_->GetCreditCards(&card_consumer);
523 MessageLoop::current()->Run(); 500 MessageLoop::current()->Run();
524 EXPECT_EQ(handle, card_consumer.handle()); 501 EXPECT_EQ(handle, card_consumer.handle());
525 ASSERT_EQ(1U, card_consumer.result().size()); 502 ASSERT_EQ(1U, card_consumer.result().size());
526 EXPECT_EQ(credit_card, *card_consumer.result()[0]); 503 EXPECT_EQ(credit_card, *card_consumer.result()[0]);
527 STLDeleteElements(&card_consumer.result()); 504 STLDeleteElements(&card_consumer.result());
528 505
529 // Check that GUID-based notification was sent for the profile. 506 // Check that GUID-based notification was sent for the profile.
530 const AutofillProfileChange expected_profile_change( 507 const AutofillProfileChange expected_profile_change(
531 AutofillProfileChange::REMOVE, profile.guid(), NULL); 508 AutofillProfileChange::REMOVE, profile.guid(), NULL);
532 EXPECT_CALL( 509 EXPECT_CALL(observer_, AutofillProfileChanged(expected_profile_change))
533 *observer_helper_->observer(), 510 .WillOnce(SignalEvent(&done_event_));
534 Observe(int(chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED),
535 content::Source<AutofillWebDataService>(wds_.get()),
536 Property(&content::Details<const AutofillProfileChange>::ptr,
537 Pointee(expected_profile_change)))).
538 WillOnce(SignalEvent(&done_event_));
539 511
540 // Remove the profile using time range of "all time". 512 // Remove the profile using time range of "all time".
541 wds_->RemoveAutofillDataModifiedBetween(Time(), Time()); 513 wds_->RemoveAutofillDataModifiedBetween(Time(), Time());
542 done_event_.TimedWait(test_timeout_); 514 done_event_.TimedWait(test_timeout_);
543 WaitForDatabaseThread(); 515 WaitForDatabaseThread();
544 516
545 // Check that the profile was removed. 517 // Check that the profile was removed.
546 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> > 518 AutofillWebDataServiceConsumer<std::vector<AutofillProfile*> >
547 profile_consumer2; 519 profile_consumer2;
548 WebDataService::Handle handle2 = 520 WebDataService::Handle handle2 =
549 wds_->GetAutofillProfiles(&profile_consumer2); 521 wds_->GetAutofillProfiles(&profile_consumer2);
550 MessageLoop::current()->Run(); 522 MessageLoop::current()->Run();
551 EXPECT_EQ(handle2, profile_consumer2.handle()); 523 EXPECT_EQ(handle2, profile_consumer2.handle());
552 ASSERT_EQ(0U, profile_consumer2.result().size()); 524 ASSERT_EQ(0U, profile_consumer2.result().size());
553 525
554 // Check that the credit card was removed. 526 // Check that the credit card was removed.
555 AutofillWebDataServiceConsumer<std::vector<CreditCard*> > card_consumer2; 527 AutofillWebDataServiceConsumer<std::vector<CreditCard*> > card_consumer2;
556 handle2 = wds_->GetCreditCards(&card_consumer2); 528 handle2 = wds_->GetCreditCards(&card_consumer2);
557 MessageLoop::current()->Run(); 529 MessageLoop::current()->Run();
558 EXPECT_EQ(handle2, card_consumer2.handle()); 530 EXPECT_EQ(handle2, card_consumer2.handle());
559 ASSERT_EQ(0U, card_consumer2.result().size()); 531 ASSERT_EQ(0U, card_consumer2.result().size());
560 } 532 }
OLDNEW
« no previous file with comments | « chrome/browser/webdata/web_data_service_factory.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698