OLD | NEW |
| (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 "chrome/browser/chromeos/contacts/google_contact_store.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/time/time.h" | |
11 #include "chrome/browser/chromeos/contacts/contact.pb.h" | |
12 #include "chrome/browser/chromeos/contacts/contact_store_observer.h" | |
13 #include "chrome/browser/chromeos/contacts/contact_test_util.h" | |
14 #include "chrome/browser/chromeos/contacts/fake_contact_database.h" | |
15 #include "chrome/browser/chromeos/contacts/gdata_contacts_service_stub.h" | |
16 #include "chrome/test/base/testing_profile.h" | |
17 #include "content/public/browser/browser_thread.h" | |
18 #include "content/public/test/test_browser_thread.h" | |
19 #include "google_apis/drive/time_util.h" | |
20 #include "net/base/network_change_notifier.h" | |
21 #include "net/url_request/url_request_test_util.h" | |
22 #include "testing/gtest/include/gtest/gtest.h" | |
23 | |
24 using content::BrowserThread; | |
25 | |
26 namespace contacts { | |
27 namespace test { | |
28 | |
29 // ContactStoreObserver implementation that just counts the number of times | |
30 // that it's been told that a store has been updated. | |
31 class TestContactStoreObserver : public ContactStoreObserver { | |
32 public: | |
33 TestContactStoreObserver() : num_updates_(0) {} | |
34 virtual ~TestContactStoreObserver() {} | |
35 | |
36 int num_updates() const { return num_updates_; } | |
37 void reset_stats() { num_updates_ = 0; } | |
38 | |
39 // ContactStoreObserver overrides: | |
40 virtual void OnContactsUpdated(ContactStore* store) OVERRIDE { | |
41 DCHECK(store); | |
42 num_updates_++; | |
43 } | |
44 | |
45 private: | |
46 // Number of times that OnContactsUpdated() has been called. | |
47 int num_updates_; | |
48 | |
49 DISALLOW_COPY_AND_ASSIGN(TestContactStoreObserver); | |
50 }; | |
51 | |
52 class GoogleContactStoreTest : public testing::Test { | |
53 public: | |
54 GoogleContactStoreTest() : ui_thread_(BrowserThread::UI, &message_loop_) {} | |
55 virtual ~GoogleContactStoreTest() {} | |
56 | |
57 protected: | |
58 // testing::Test implementation. | |
59 virtual void SetUp() OVERRIDE { | |
60 // Create a mock NetworkChangeNotifier so the store won't be notified about | |
61 // changes to the system's actual network state. | |
62 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); | |
63 | |
64 profile_.reset(new TestingProfile); | |
65 | |
66 store_.reset(new GoogleContactStore(NULL, // request_context_getter | |
67 profile_.get())); | |
68 store_->AddObserver(&observer_); | |
69 | |
70 test_api_.reset(new GoogleContactStore::TestAPI(store_.get())); | |
71 | |
72 db_ = new FakeContactDatabase; | |
73 test_api_->SetDatabase(db_); | |
74 | |
75 gdata_service_ = new GDataContactsServiceStub; | |
76 test_api_->SetGDataService(gdata_service_); | |
77 } | |
78 | |
79 base::MessageLoopForUI message_loop_; | |
80 content::TestBrowserThread ui_thread_; | |
81 | |
82 TestContactStoreObserver observer_; | |
83 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | |
84 scoped_ptr<TestingProfile> profile_; | |
85 scoped_ptr<GoogleContactStore> store_; | |
86 scoped_ptr<GoogleContactStore::TestAPI> test_api_; | |
87 | |
88 FakeContactDatabase* db_; // not owned | |
89 GDataContactsServiceStub* gdata_service_; // not owned | |
90 | |
91 private: | |
92 DISALLOW_COPY_AND_ASSIGN(GoogleContactStoreTest); | |
93 }; | |
94 | |
95 TEST_F(GoogleContactStoreTest, LoadFromDatabase) { | |
96 // Store two contacts in the database. | |
97 const std::string kContactId1 = "contact1"; | |
98 const std::string kContactId2 = "contact2"; | |
99 scoped_ptr<Contact> contact1(new Contact); | |
100 InitContact(kContactId1, "1", false, contact1.get()); | |
101 scoped_ptr<Contact> contact2(new Contact); | |
102 InitContact(kContactId2, "2", false, contact2.get()); | |
103 ContactPointers db_contacts; | |
104 db_contacts.push_back(contact1.get()); | |
105 db_contacts.push_back(contact2.get()); | |
106 UpdateMetadata db_metadata; | |
107 db_->SetContacts(db_contacts, db_metadata); | |
108 | |
109 // Tell the GData service to report failure, initialize the store, and check | |
110 // that the contacts from the database are loaded. | |
111 gdata_service_->set_download_should_succeed(false); | |
112 store_->Init(); | |
113 ContactPointers loaded_contacts; | |
114 store_->AppendContacts(&loaded_contacts); | |
115 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), | |
116 ContactsToString(loaded_contacts)); | |
117 EXPECT_TRUE(test_api_->update_scheduled()); | |
118 EXPECT_EQ(1, observer_.num_updates()); | |
119 | |
120 // Check that we can also grab the contact via its ID. | |
121 const Contact* loaded_contact1 = store_->GetContactById(kContactId1); | |
122 ASSERT_TRUE(loaded_contact1); | |
123 EXPECT_EQ(ContactToString(*contact1), ContactToString(*loaded_contact1)); | |
124 | |
125 // We should get NULL if we request a nonexistent contact. | |
126 EXPECT_FALSE(store_->GetContactById("bogus_id")); | |
127 } | |
128 | |
129 TEST_F(GoogleContactStoreTest, LoadFromGData) { | |
130 // Store two contacts in the GData service. | |
131 scoped_ptr<Contact> contact1(new Contact); | |
132 InitContact("contact1", "1", false, contact1.get()); | |
133 scoped_ptr<Contact> contact2(new Contact); | |
134 InitContact("contact2", "2", false, contact2.get()); | |
135 ContactPointers gdata_contacts; | |
136 gdata_contacts.push_back(contact1.get()); | |
137 gdata_contacts.push_back(contact2.get()); | |
138 gdata_service_->SetContacts(gdata_contacts, base::Time()); | |
139 | |
140 // Initialize the store and check that the contacts are loaded from GData. | |
141 store_->Init(); | |
142 ContactPointers loaded_contacts; | |
143 store_->AppendContacts(&loaded_contacts); | |
144 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), | |
145 ContactsToString(loaded_contacts)); | |
146 EXPECT_TRUE(test_api_->update_scheduled()); | |
147 EXPECT_EQ(1, observer_.num_updates()); | |
148 | |
149 // The contacts should've been saved to the database, too. | |
150 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), | |
151 ContactMapToString(db_->contacts())); | |
152 } | |
153 | |
154 TEST_F(GoogleContactStoreTest, UpdateFromGData) { | |
155 scoped_ptr<Contact> contact1(new Contact); | |
156 InitContact("contact1", "1", false, contact1.get()); | |
157 scoped_ptr<Contact> contact2(new Contact); | |
158 InitContact("contact2", "2", false, contact2.get()); | |
159 scoped_ptr<Contact> contact3(new Contact); | |
160 InitContact("contact3", "3", false, contact3.get()); | |
161 | |
162 // Store the first two contacts in the database. | |
163 ContactPointers db_contacts; | |
164 db_contacts.push_back(contact1.get()); | |
165 db_contacts.push_back(contact2.get()); | |
166 UpdateMetadata db_metadata; | |
167 db_->SetContacts(db_contacts, db_metadata); | |
168 | |
169 // Store all three in the GData service. We expect the update request to ask | |
170 // for all contacts updated one millisecond after the newest contact in the | |
171 // database. | |
172 ContactPointers gdata_contacts; | |
173 gdata_contacts.push_back(contact1.get()); | |
174 gdata_contacts.push_back(contact2.get()); | |
175 gdata_contacts.push_back(contact3.get()); | |
176 gdata_service_->SetContacts( | |
177 gdata_contacts, | |
178 base::Time::FromInternalValue(contact2->update_time()) + | |
179 base::TimeDelta::FromMilliseconds(1)); | |
180 | |
181 // Check that the store ends up with all three contacts. | |
182 store_->Init(); | |
183 ContactPointers loaded_contacts; | |
184 store_->AppendContacts(&loaded_contacts); | |
185 EXPECT_EQ(VarContactsToString( | |
186 3, contact1.get(), contact2.get(), contact3.get()), | |
187 ContactsToString(loaded_contacts)); | |
188 EXPECT_EQ(2, observer_.num_updates()); | |
189 | |
190 // All three contacts should've been saved to the database. | |
191 EXPECT_EQ(VarContactsToString( | |
192 3, contact1.get(), contact2.get(), contact3.get()), | |
193 ContactMapToString(db_->contacts())); | |
194 EXPECT_EQ(3, db_->num_saved_contacts()); | |
195 EXPECT_TRUE(test_api_->update_scheduled()); | |
196 } | |
197 | |
198 TEST_F(GoogleContactStoreTest, FetchUpdatedContacts) { | |
199 scoped_ptr<Contact> contact1(new Contact); | |
200 InitContact("contact1", "1", false, contact1.get()); | |
201 scoped_ptr<Contact> contact2(new Contact); | |
202 InitContact("contact2", "2", false, contact2.get()); | |
203 scoped_ptr<Contact> contact3(new Contact); | |
204 InitContact("contact3", "3", false, contact3.get()); | |
205 | |
206 ContactPointers kAllContacts; | |
207 kAllContacts.push_back(contact1.get()); | |
208 kAllContacts.push_back(contact2.get()); | |
209 kAllContacts.push_back(contact3.get()); | |
210 | |
211 // Tell the GData service to return all three contacts in response to a full | |
212 // update. | |
213 ContactPointers gdata_contacts(kAllContacts); | |
214 gdata_service_->SetContacts(gdata_contacts, base::Time()); | |
215 | |
216 // All the contacts should be loaded and saved to the database. | |
217 store_->Init(); | |
218 ContactPointers loaded_contacts; | |
219 store_->AppendContacts(&loaded_contacts); | |
220 EXPECT_EQ(ContactsToString(kAllContacts), ContactsToString(loaded_contacts)); | |
221 EXPECT_EQ(ContactsToString(kAllContacts), | |
222 ContactMapToString(db_->contacts())); | |
223 EXPECT_EQ(static_cast<int>(kAllContacts.size()), db_->num_saved_contacts()); | |
224 EXPECT_TRUE(test_api_->update_scheduled()); | |
225 EXPECT_EQ(1, observer_.num_updates()); | |
226 observer_.reset_stats(); | |
227 | |
228 // Update the third contact. | |
229 contact3->set_full_name("new full name"); | |
230 base::Time old_contact3_update_time = | |
231 base::Time::FromInternalValue(contact3->update_time()); | |
232 contact3->set_update_time( | |
233 (old_contact3_update_time + | |
234 base::TimeDelta::FromSeconds(10)).ToInternalValue()); | |
235 gdata_contacts.clear(); | |
236 gdata_contacts.push_back(contact3.get()); | |
237 gdata_service_->SetContacts( | |
238 gdata_contacts, | |
239 old_contact3_update_time + base::TimeDelta::FromMilliseconds(1)); | |
240 | |
241 // Check that the updated contact is loaded (i.e. the store passed the | |
242 // correct minimum update time to the service) and saved back to the database. | |
243 db_->reset_stats(); | |
244 test_api_->DoUpdate(); | |
245 loaded_contacts.clear(); | |
246 store_->AppendContacts(&loaded_contacts); | |
247 EXPECT_EQ(ContactsToString(kAllContacts), ContactsToString(loaded_contacts)); | |
248 EXPECT_EQ(ContactsToString(kAllContacts), | |
249 ContactMapToString(db_->contacts())); | |
250 EXPECT_EQ(1, db_->num_saved_contacts()); | |
251 EXPECT_TRUE(test_api_->update_scheduled()); | |
252 EXPECT_EQ(1, observer_.num_updates()); | |
253 observer_.reset_stats(); | |
254 | |
255 // The next update should be based on the third contact's new update time. | |
256 contact3->set_full_name("yet another full name"); | |
257 gdata_service_->SetContacts( | |
258 gdata_contacts, | |
259 base::Time::FromInternalValue(contact3->update_time()) + | |
260 base::TimeDelta::FromMilliseconds(1)); | |
261 | |
262 db_->reset_stats(); | |
263 test_api_->DoUpdate(); | |
264 loaded_contacts.clear(); | |
265 store_->AppendContacts(&loaded_contacts); | |
266 EXPECT_EQ(ContactsToString(kAllContacts), ContactsToString(loaded_contacts)); | |
267 EXPECT_EQ(ContactsToString(kAllContacts), | |
268 ContactMapToString(db_->contacts())); | |
269 EXPECT_EQ(1, db_->num_saved_contacts()); | |
270 EXPECT_TRUE(test_api_->update_scheduled()); | |
271 EXPECT_EQ(1, observer_.num_updates()); | |
272 } | |
273 | |
274 TEST_F(GoogleContactStoreTest, DontReturnDeletedContacts) { | |
275 // Tell GData to return a single deleted contact. | |
276 const std::string kContactId = "contact"; | |
277 scoped_ptr<Contact> contact(new Contact); | |
278 InitContact(kContactId, "1", true, contact.get()); | |
279 ContactPointers gdata_contacts; | |
280 gdata_contacts.push_back(contact.get()); | |
281 gdata_service_->SetContacts(gdata_contacts, base::Time()); | |
282 | |
283 // The contact shouldn't be returned by AppendContacts() or | |
284 // GetContactById(). | |
285 store_->Init(); | |
286 ContactPointers loaded_contacts; | |
287 store_->AppendContacts(&loaded_contacts); | |
288 EXPECT_TRUE(loaded_contacts.empty()); | |
289 EXPECT_FALSE(store_->GetContactById(kContactId)); | |
290 } | |
291 | |
292 // Test that we do a full refresh from GData if enough time has passed since the | |
293 // last refresh that we might've missed some contact deletions (see | |
294 // |kForceFullUpdateDays| in google_contact_store.cc). | |
295 TEST_F(GoogleContactStoreTest, FullRefreshAfterThirtyDays) { | |
296 base::Time::Exploded kInitTimeExploded = { 2012, 3, 0, 1, 16, 34, 56, 123 }; | |
297 base::Time kInitTime = base::Time::FromUTCExploded(kInitTimeExploded); | |
298 | |
299 base::Time kOldUpdateTime = kInitTime - base::TimeDelta::FromDays(31); | |
300 scoped_ptr<Contact> contact1(new Contact); | |
301 InitContact("contact1", "1", false, contact1.get()); | |
302 contact1->set_update_time(kOldUpdateTime.ToInternalValue()); | |
303 scoped_ptr<Contact> contact2(new Contact); | |
304 InitContact("contact2", "2", false, contact2.get()); | |
305 contact2->set_update_time(kOldUpdateTime.ToInternalValue()); | |
306 | |
307 // Put both contacts in the database, along with metadata saying that the last | |
308 // successful update was 31 days in the past. | |
309 ContactPointers db_contacts; | |
310 db_contacts.push_back(contact1.get()); | |
311 db_contacts.push_back(contact2.get()); | |
312 UpdateMetadata db_metadata; | |
313 db_metadata.set_last_update_start_time(kOldUpdateTime.ToInternalValue()); | |
314 db_->SetContacts(db_contacts, db_metadata); | |
315 | |
316 // Tell the GData service to return only the first contact and to expect a | |
317 // full refresh (since it's been a long time since the last update). | |
318 ContactPointers gdata_contacts; | |
319 gdata_contacts.push_back(contact1.get()); | |
320 gdata_service_->SetContacts(gdata_contacts, base::Time()); | |
321 | |
322 test_api_->set_current_time(kInitTime); | |
323 store_->Init(); | |
324 ContactPointers loaded_contacts; | |
325 store_->AppendContacts(&loaded_contacts); | |
326 EXPECT_EQ(ContactsToString(gdata_contacts), | |
327 ContactsToString(loaded_contacts)); | |
328 EXPECT_TRUE(test_api_->update_scheduled()); | |
329 | |
330 // Make GData return both contacts now in response to an incremental update. | |
331 gdata_contacts.clear(); | |
332 contact2->set_update_time(kInitTime.ToInternalValue()); | |
333 gdata_contacts.push_back(contact1.get()); | |
334 gdata_contacts.push_back(contact2.get()); | |
335 gdata_service_->SetContacts( | |
336 gdata_contacts, kOldUpdateTime + base::TimeDelta::FromMilliseconds(1)); | |
337 | |
338 // Advance the time by twenty days and check that the update is successful. | |
339 base::Time kFirstUpdateTime = kInitTime + base::TimeDelta::FromDays(20); | |
340 test_api_->set_current_time(kFirstUpdateTime); | |
341 test_api_->DoUpdate(); | |
342 loaded_contacts.clear(); | |
343 store_->AppendContacts(&loaded_contacts); | |
344 EXPECT_EQ(ContactsToString(gdata_contacts), | |
345 ContactsToString(loaded_contacts)); | |
346 | |
347 // After we advance the time by 31 days, we should do a full refresh again. | |
348 gdata_contacts.clear(); | |
349 gdata_contacts.push_back(contact1.get()); | |
350 gdata_service_->SetContacts(gdata_contacts, base::Time()); | |
351 base::Time kSecondUpdateTime = | |
352 kFirstUpdateTime + base::TimeDelta::FromDays(31); | |
353 test_api_->set_current_time(kSecondUpdateTime); | |
354 test_api_->DoUpdate(); | |
355 loaded_contacts.clear(); | |
356 store_->AppendContacts(&loaded_contacts); | |
357 EXPECT_EQ(ContactsToString(gdata_contacts), | |
358 ContactsToString(loaded_contacts)); | |
359 } | |
360 | |
361 TEST_F(GoogleContactStoreTest, HandleDatabaseInitFailure) { | |
362 scoped_ptr<Contact> contact1(new Contact); | |
363 InitContact("contact1", "1", false, contact1.get()); | |
364 | |
365 // Store a contact in the database but make initialization fail. | |
366 ContactPointers db_contacts; | |
367 db_contacts.push_back(contact1.get()); | |
368 UpdateMetadata db_metadata; | |
369 db_->SetContacts(db_contacts, db_metadata); | |
370 db_->set_init_success(false); | |
371 | |
372 // Create a second contact and tell the GData service to return it. | |
373 scoped_ptr<Contact> contact2(new Contact); | |
374 InitContact("contact2", "2", false, contact2.get()); | |
375 ContactPointers gdata_contacts; | |
376 gdata_contacts.push_back(contact2.get()); | |
377 gdata_service_->SetContacts(gdata_contacts, base::Time()); | |
378 | |
379 // Initialize the store. We shouldn't get the first contact (since DB | |
380 // initialization failed) but we should still fetch the second one from GData | |
381 // and schedule an update. | |
382 store_->Init(); | |
383 ContactPointers loaded_contacts; | |
384 store_->AppendContacts(&loaded_contacts); | |
385 EXPECT_EQ(ContactsToString(gdata_contacts), | |
386 ContactsToString(loaded_contacts)); | |
387 EXPECT_TRUE(test_api_->update_scheduled()); | |
388 } | |
389 | |
390 TEST_F(GoogleContactStoreTest, AvoidUpdatesWhenOffline) { | |
391 EXPECT_EQ(0, gdata_service_->num_download_requests()); | |
392 | |
393 // Notify the store that we're offline. Init() shouldn't attempt an update | |
394 // and the update timer shouldn't be running. | |
395 test_api_->NotifyAboutNetworkStateChange(false); | |
396 store_->Init(); | |
397 EXPECT_EQ(0, gdata_service_->num_download_requests()); | |
398 EXPECT_FALSE(test_api_->update_scheduled()); | |
399 | |
400 // We should do an update and schedule further updates as soon as we go | |
401 // online. | |
402 gdata_service_->reset_stats(); | |
403 test_api_->NotifyAboutNetworkStateChange(true); | |
404 EXPECT_EQ(1, gdata_service_->num_download_requests()); | |
405 EXPECT_TRUE(test_api_->update_scheduled()); | |
406 | |
407 // If we call DoUpdate() to mimic the code path that's used for a timer-driven | |
408 // update while we're offline, we should again defer the update. | |
409 gdata_service_->reset_stats(); | |
410 test_api_->NotifyAboutNetworkStateChange(false); | |
411 test_api_->DoUpdate(); | |
412 EXPECT_EQ(0, gdata_service_->num_download_requests()); | |
413 | |
414 // When we're back online, the update should happen. | |
415 gdata_service_->reset_stats(); | |
416 test_api_->NotifyAboutNetworkStateChange(true); | |
417 EXPECT_EQ(1, gdata_service_->num_download_requests()); | |
418 } | |
419 | |
420 TEST_F(GoogleContactStoreTest, DropDeletedContacts) { | |
421 // Tell the GData service to return a single contact. | |
422 scoped_ptr<Contact> contact1(new Contact); | |
423 InitContact("contact1", "1", false, contact1.get()); | |
424 ContactPointers gdata_contacts; | |
425 gdata_contacts.push_back(contact1.get()); | |
426 gdata_service_->SetContacts(gdata_contacts, base::Time()); | |
427 | |
428 // Check that the contact store loads it into memory and saves it to the | |
429 // database. | |
430 store_->Init(); | |
431 EXPECT_EQ(0, gdata_service_->num_download_requests_with_wrong_timestamps()); | |
432 EXPECT_EQ(base::Time::FromInternalValue(contact1->update_time()), | |
433 test_api_->last_contact_update_time()); | |
434 EXPECT_EQ(VarContactsToString(1, contact1.get()), | |
435 ContactsToString(*test_api_->GetLoadedContacts())); | |
436 EXPECT_EQ(VarContactsToString(1, contact1.get()), | |
437 ContactMapToString(db_->contacts())); | |
438 EXPECT_EQ(contact1->update_time(), | |
439 db_->metadata().last_contact_update_time()); | |
440 EXPECT_TRUE(test_api_->update_scheduled()); | |
441 | |
442 // Now tell the GData service to return a more-newly-updated, already deleted | |
443 // contact. | |
444 scoped_ptr<Contact> contact2(new Contact); | |
445 InitContact("contact2", "2", true, contact2.get()); | |
446 contact2->set_update_time( | |
447 (base::Time::FromInternalValue(contact1->update_time()) + | |
448 base::TimeDelta::FromSeconds(5)).ToInternalValue()); | |
449 gdata_contacts.clear(); | |
450 gdata_contacts.push_back(contact2.get()); | |
451 gdata_service_->SetContacts( | |
452 gdata_contacts, | |
453 base::Time::FromInternalValue(contact1->update_time()) + | |
454 base::TimeDelta::FromMilliseconds(1)); | |
455 | |
456 // The contact store should save the last update time from the deleted | |
457 // contact, but the contact itself shouldn't be loaded into memory or written | |
458 // to the database. | |
459 test_api_->DoUpdate(); | |
460 EXPECT_EQ(0, gdata_service_->num_download_requests_with_wrong_timestamps()); | |
461 EXPECT_EQ(base::Time::FromInternalValue(contact2->update_time()), | |
462 test_api_->last_contact_update_time()); | |
463 EXPECT_EQ(VarContactsToString(1, contact1.get()), | |
464 ContactsToString(*test_api_->GetLoadedContacts())); | |
465 EXPECT_EQ(VarContactsToString(1, contact1.get()), | |
466 ContactMapToString(db_->contacts())); | |
467 EXPECT_EQ(contact2->update_time(), | |
468 db_->metadata().last_contact_update_time()); | |
469 | |
470 // Tell the GData service to report the first contact as having been deleted. | |
471 contact1->set_update_time( | |
472 (base::Time::FromInternalValue(contact2->update_time()) + | |
473 base::TimeDelta::FromSeconds(10)).ToInternalValue()); | |
474 contact1->set_deleted(true); | |
475 gdata_contacts.clear(); | |
476 gdata_contacts.push_back(contact1.get()); | |
477 gdata_service_->SetContacts( | |
478 gdata_contacts, | |
479 base::Time::FromInternalValue(contact2->update_time()) + | |
480 base::TimeDelta::FromMilliseconds(1)); | |
481 | |
482 // The contact store should drop the first contact after another update. | |
483 test_api_->DoUpdate(); | |
484 EXPECT_EQ(0, gdata_service_->num_download_requests_with_wrong_timestamps()); | |
485 EXPECT_EQ(base::Time::FromInternalValue(contact1->update_time()), | |
486 test_api_->last_contact_update_time()); | |
487 EXPECT_TRUE(test_api_->GetLoadedContacts()->empty()); | |
488 EXPECT_TRUE(db_->contacts().empty()); | |
489 EXPECT_EQ(contact1->update_time(), | |
490 db_->metadata().last_contact_update_time()); | |
491 } | |
492 | |
493 TEST_F(GoogleContactStoreTest, UseLastContactUpdateTimeFromMetadata) { | |
494 base::Time::Exploded kInitTimeExploded = { 2012, 3, 0, 1, 16, 34, 56, 123 }; | |
495 base::Time kInitTime = base::Time::FromUTCExploded(kInitTimeExploded); | |
496 | |
497 // Configure the metadata to say that a contact was updated one day before the | |
498 // current time. We won't create a contact that actually contains this time, | |
499 // though; this mimics the situation where the most-recently-updated contact | |
500 // has been deleted and wasn't saved to the database. | |
501 base::Time kDeletedContactUpdateTime = | |
502 kInitTime - base::TimeDelta::FromDays(1); | |
503 UpdateMetadata db_metadata; | |
504 db_metadata.set_last_contact_update_time( | |
505 kDeletedContactUpdateTime.ToInternalValue()); | |
506 | |
507 // Create a non-deleted contact with an update time one day prior to the | |
508 // update time in the metadata. | |
509 base::Time kNonDeletedContactUpdateTime = | |
510 kDeletedContactUpdateTime - base::TimeDelta::FromDays(1); | |
511 scoped_ptr<Contact> non_deleted_contact(new Contact); | |
512 InitContact("contact", "1", false, non_deleted_contact.get()); | |
513 non_deleted_contact->set_update_time( | |
514 kNonDeletedContactUpdateTime.ToInternalValue()); | |
515 | |
516 // Save the contact to the database. | |
517 ContactPointers db_contacts; | |
518 db_contacts.push_back(non_deleted_contact.get()); | |
519 db_->SetContacts(db_contacts, db_metadata); | |
520 | |
521 // Tell the GData service to expect the deleted contact's update time. | |
522 ContactPointers gdata_contacts; | |
523 gdata_contacts.push_back(non_deleted_contact.get()); | |
524 gdata_service_->SetContacts( | |
525 gdata_contacts, | |
526 kDeletedContactUpdateTime + base::TimeDelta::FromMilliseconds(1)); | |
527 | |
528 test_api_->set_current_time(kInitTime); | |
529 store_->Init(); | |
530 EXPECT_EQ(0, gdata_service_->num_download_requests_with_wrong_timestamps()); | |
531 ContactPointers loaded_contacts; | |
532 store_->AppendContacts(&loaded_contacts); | |
533 EXPECT_EQ(ContactsToString(gdata_contacts), | |
534 ContactsToString(loaded_contacts)); | |
535 EXPECT_TRUE(test_api_->update_scheduled()); | |
536 | |
537 } | |
538 | |
539 } // namespace test | |
540 } // namespace contacts | |
OLD | NEW |