OLD | NEW |
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 "chrome/browser/chromeos/contacts/google_contact_store.h" | 5 #include "chrome/browser/chromeos/contacts/google_contact_store.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 | 84 |
85 FakeContactDatabase* db_; // not owned | 85 FakeContactDatabase* db_; // not owned |
86 gdata::GDataContactsServiceStub* gdata_service_; // not owned | 86 gdata::GDataContactsServiceStub* gdata_service_; // not owned |
87 | 87 |
88 private: | 88 private: |
89 DISALLOW_COPY_AND_ASSIGN(GoogleContactStoreTest); | 89 DISALLOW_COPY_AND_ASSIGN(GoogleContactStoreTest); |
90 }; | 90 }; |
91 | 91 |
92 TEST_F(GoogleContactStoreTest, LoadFromDatabase) { | 92 TEST_F(GoogleContactStoreTest, LoadFromDatabase) { |
93 // Store two contacts in the database. | 93 // Store two contacts in the database. |
94 const std::string kProviderId1 = "provider1"; | 94 const std::string kContactId1 = "contact1"; |
95 const std::string kProviderId2 = "provider2"; | 95 const std::string kContactId2 = "contact2"; |
96 scoped_ptr<Contact> contact1(new Contact); | 96 scoped_ptr<Contact> contact1(new Contact); |
97 InitContact(kProviderId1, "1", false, contact1.get()); | 97 InitContact(kContactId1, "1", false, contact1.get()); |
98 scoped_ptr<Contact> contact2(new Contact); | 98 scoped_ptr<Contact> contact2(new Contact); |
99 InitContact(kProviderId2, "2", false, contact2.get()); | 99 InitContact(kContactId2, "2", false, contact2.get()); |
100 ContactPointers db_contacts; | 100 ContactPointers db_contacts; |
101 db_contacts.push_back(contact1.get()); | 101 db_contacts.push_back(contact1.get()); |
102 db_contacts.push_back(contact2.get()); | 102 db_contacts.push_back(contact2.get()); |
103 UpdateMetadata db_metadata; | 103 UpdateMetadata db_metadata; |
104 db_->SetContacts(db_contacts, db_metadata); | 104 db_->SetContacts(db_contacts, db_metadata); |
105 | 105 |
106 // Tell the GData service to report failure, initialize the store, and check | 106 // Tell the GData service to report failure, initialize the store, and check |
107 // that the contacts from the database are loaded. | 107 // that the contacts from the database are loaded. |
108 gdata_service_->set_download_should_succeed(false); | 108 gdata_service_->set_download_should_succeed(false); |
109 store_->Init(); | 109 store_->Init(); |
110 ContactPointers loaded_contacts; | 110 ContactPointers loaded_contacts; |
111 store_->AppendContacts(&loaded_contacts); | 111 store_->AppendContacts(&loaded_contacts); |
112 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), | 112 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), |
113 ContactsToString(loaded_contacts)); | 113 ContactsToString(loaded_contacts)); |
114 EXPECT_TRUE(test_api_->update_scheduled()); | 114 EXPECT_TRUE(test_api_->update_scheduled()); |
115 EXPECT_EQ(1, observer_.num_updates()); | 115 EXPECT_EQ(1, observer_.num_updates()); |
116 | 116 |
117 // Check that we can also grab the contact via its ID. | 117 // Check that we can also grab the contact via its ID. |
118 const Contact* loaded_contact1 = store_->GetContactByProviderId(kProviderId1); | 118 const Contact* loaded_contact1 = store_->GetContactById(kContactId1); |
119 ASSERT_TRUE(loaded_contact1); | 119 ASSERT_TRUE(loaded_contact1); |
120 EXPECT_EQ(ContactToString(*contact1), ContactToString(*loaded_contact1)); | 120 EXPECT_EQ(ContactToString(*contact1), ContactToString(*loaded_contact1)); |
121 | 121 |
122 // We should get NULL if we request a nonexistent contact. | 122 // We should get NULL if we request a nonexistent contact. |
123 EXPECT_FALSE(store_->GetContactByProviderId("bogus_id")); | 123 EXPECT_FALSE(store_->GetContactById("bogus_id")); |
124 } | 124 } |
125 | 125 |
126 TEST_F(GoogleContactStoreTest, LoadFromGData) { | 126 TEST_F(GoogleContactStoreTest, LoadFromGData) { |
127 // Store two contacts in the GData service. | 127 // Store two contacts in the GData service. |
128 scoped_ptr<Contact> contact1(new Contact); | 128 scoped_ptr<Contact> contact1(new Contact); |
129 InitContact("provider1", "1", false, contact1.get()); | 129 InitContact("contact1", "1", false, contact1.get()); |
130 scoped_ptr<Contact> contact2(new Contact); | 130 scoped_ptr<Contact> contact2(new Contact); |
131 InitContact("provider2", "2", false, contact2.get()); | 131 InitContact("contact2", "2", false, contact2.get()); |
132 ContactPointers gdata_contacts; | 132 ContactPointers gdata_contacts; |
133 gdata_contacts.push_back(contact1.get()); | 133 gdata_contacts.push_back(contact1.get()); |
134 gdata_contacts.push_back(contact2.get()); | 134 gdata_contacts.push_back(contact2.get()); |
135 gdata_service_->SetContacts(gdata_contacts, base::Time()); | 135 gdata_service_->SetContacts(gdata_contacts, base::Time()); |
136 | 136 |
137 // Initialize the store and check that the contacts are loaded from GData. | 137 // Initialize the store and check that the contacts are loaded from GData. |
138 store_->Init(); | 138 store_->Init(); |
139 ContactPointers loaded_contacts; | 139 ContactPointers loaded_contacts; |
140 store_->AppendContacts(&loaded_contacts); | 140 store_->AppendContacts(&loaded_contacts); |
141 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), | 141 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), |
142 ContactsToString(loaded_contacts)); | 142 ContactsToString(loaded_contacts)); |
143 EXPECT_TRUE(test_api_->update_scheduled()); | 143 EXPECT_TRUE(test_api_->update_scheduled()); |
144 EXPECT_EQ(1, observer_.num_updates()); | 144 EXPECT_EQ(1, observer_.num_updates()); |
145 | 145 |
146 // The contacts should've been saved to the database, too. | 146 // The contacts should've been saved to the database, too. |
147 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), | 147 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), |
148 ContactsToString(db_->contacts())); | 148 ContactsToString(db_->contacts())); |
149 } | 149 } |
150 | 150 |
151 TEST_F(GoogleContactStoreTest, UpdateFromGData) { | 151 TEST_F(GoogleContactStoreTest, UpdateFromGData) { |
152 scoped_ptr<Contact> contact1(new Contact); | 152 scoped_ptr<Contact> contact1(new Contact); |
153 InitContact("provider1", "1", false, contact1.get()); | 153 InitContact("contact1", "1", false, contact1.get()); |
154 scoped_ptr<Contact> contact2(new Contact); | 154 scoped_ptr<Contact> contact2(new Contact); |
155 InitContact("provider2", "2", false, contact2.get()); | 155 InitContact("contact2", "2", false, contact2.get()); |
156 scoped_ptr<Contact> contact3(new Contact); | 156 scoped_ptr<Contact> contact3(new Contact); |
157 InitContact("provider3", "3", false, contact3.get()); | 157 InitContact("contact3", "3", false, contact3.get()); |
158 | 158 |
159 // Store the first two contacts in the database. | 159 // Store the first two contacts in the database. |
160 ContactPointers db_contacts; | 160 ContactPointers db_contacts; |
161 db_contacts.push_back(contact1.get()); | 161 db_contacts.push_back(contact1.get()); |
162 db_contacts.push_back(contact2.get()); | 162 db_contacts.push_back(contact2.get()); |
163 UpdateMetadata db_metadata; | 163 UpdateMetadata db_metadata; |
164 db_->SetContacts(db_contacts, db_metadata); | 164 db_->SetContacts(db_contacts, db_metadata); |
165 | 165 |
166 // Store all three in the GData service. We expect the update request to ask | 166 // Store all three in the GData service. We expect the update request to ask |
167 // for all contacts updated one millisecond after the newest contact in the | 167 // for all contacts updated one millisecond after the newest contact in the |
(...skipping 19 matching lines...) Expand all Loading... |
187 // All three contacts should've been saved to the database. | 187 // All three contacts should've been saved to the database. |
188 EXPECT_EQ(VarContactsToString( | 188 EXPECT_EQ(VarContactsToString( |
189 3, contact1.get(), contact2.get(), contact3.get()), | 189 3, contact1.get(), contact2.get(), contact3.get()), |
190 ContactsToString(db_->contacts())); | 190 ContactsToString(db_->contacts())); |
191 EXPECT_EQ(3, db_->num_saved_contacts()); | 191 EXPECT_EQ(3, db_->num_saved_contacts()); |
192 EXPECT_TRUE(test_api_->update_scheduled()); | 192 EXPECT_TRUE(test_api_->update_scheduled()); |
193 } | 193 } |
194 | 194 |
195 TEST_F(GoogleContactStoreTest, FetchUpdatedContacts) { | 195 TEST_F(GoogleContactStoreTest, FetchUpdatedContacts) { |
196 scoped_ptr<Contact> contact1(new Contact); | 196 scoped_ptr<Contact> contact1(new Contact); |
197 InitContact("provider1", "1", false, contact1.get()); | 197 InitContact("contact1", "1", false, contact1.get()); |
198 scoped_ptr<Contact> contact2(new Contact); | 198 scoped_ptr<Contact> contact2(new Contact); |
199 InitContact("provider2", "2", false, contact2.get()); | 199 InitContact("contact2", "2", false, contact2.get()); |
200 scoped_ptr<Contact> contact3(new Contact); | 200 scoped_ptr<Contact> contact3(new Contact); |
201 InitContact("provider3", "3", false, contact3.get()); | 201 InitContact("contact3", "3", false, contact3.get()); |
202 | 202 |
203 ContactPointers kAllContacts; | 203 ContactPointers kAllContacts; |
204 kAllContacts.push_back(contact1.get()); | 204 kAllContacts.push_back(contact1.get()); |
205 kAllContacts.push_back(contact2.get()); | 205 kAllContacts.push_back(contact2.get()); |
206 kAllContacts.push_back(contact3.get()); | 206 kAllContacts.push_back(contact3.get()); |
207 | 207 |
208 // Tell the GData service to return all three contacts in response to a full | 208 // Tell the GData service to return all three contacts in response to a full |
209 // update. | 209 // update. |
210 ContactPointers gdata_contacts(kAllContacts); | 210 ContactPointers gdata_contacts(kAllContacts); |
211 gdata_service_->SetContacts(gdata_contacts, base::Time()); | 211 gdata_service_->SetContacts(gdata_contacts, base::Time()); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 store_->AppendContacts(&loaded_contacts); | 260 store_->AppendContacts(&loaded_contacts); |
261 EXPECT_EQ(ContactsToString(kAllContacts), ContactsToString(loaded_contacts)); | 261 EXPECT_EQ(ContactsToString(kAllContacts), ContactsToString(loaded_contacts)); |
262 EXPECT_EQ(ContactsToString(kAllContacts), ContactsToString(db_->contacts())); | 262 EXPECT_EQ(ContactsToString(kAllContacts), ContactsToString(db_->contacts())); |
263 EXPECT_EQ(1, db_->num_saved_contacts()); | 263 EXPECT_EQ(1, db_->num_saved_contacts()); |
264 EXPECT_TRUE(test_api_->update_scheduled()); | 264 EXPECT_TRUE(test_api_->update_scheduled()); |
265 EXPECT_EQ(1, observer_.num_updates()); | 265 EXPECT_EQ(1, observer_.num_updates()); |
266 } | 266 } |
267 | 267 |
268 TEST_F(GoogleContactStoreTest, DontReturnDeletedContacts) { | 268 TEST_F(GoogleContactStoreTest, DontReturnDeletedContacts) { |
269 // Tell GData to return a single deleted contact. | 269 // Tell GData to return a single deleted contact. |
270 const std::string kProviderId = "provider"; | 270 const std::string kContactId = "contact"; |
271 scoped_ptr<Contact> contact(new Contact); | 271 scoped_ptr<Contact> contact(new Contact); |
272 InitContact(kProviderId, "1", true, contact.get()); | 272 InitContact(kContactId, "1", true, contact.get()); |
273 ContactPointers gdata_contacts; | 273 ContactPointers gdata_contacts; |
274 gdata_contacts.push_back(contact.get()); | 274 gdata_contacts.push_back(contact.get()); |
275 gdata_service_->SetContacts(gdata_contacts, base::Time()); | 275 gdata_service_->SetContacts(gdata_contacts, base::Time()); |
276 | 276 |
277 // The contact shouldn't be returned by AppendContacts() or | 277 // The contact shouldn't be returned by AppendContacts() or |
278 // GetContactByProviderId(). | 278 // GetContactById(). |
279 store_->Init(); | 279 store_->Init(); |
280 ContactPointers loaded_contacts; | 280 ContactPointers loaded_contacts; |
281 store_->AppendContacts(&loaded_contacts); | 281 store_->AppendContacts(&loaded_contacts); |
282 EXPECT_TRUE(loaded_contacts.empty()); | 282 EXPECT_TRUE(loaded_contacts.empty()); |
283 EXPECT_FALSE(store_->GetContactByProviderId(kProviderId)); | 283 EXPECT_FALSE(store_->GetContactById(kContactId)); |
284 } | 284 } |
285 | 285 |
286 // Test that we do a full refresh from GData if enough time has passed since the | 286 // Test that we do a full refresh from GData if enough time has passed since the |
287 // last refresh that we might've missed some contact deletions (see | 287 // last refresh that we might've missed some contact deletions (see |
288 // |kForceFullUpdateDays| in google_contact_store.cc). | 288 // |kForceFullUpdateDays| in google_contact_store.cc). |
289 TEST_F(GoogleContactStoreTest, FullRefreshAfterThirtyDays) { | 289 TEST_F(GoogleContactStoreTest, FullRefreshAfterThirtyDays) { |
290 base::Time::Exploded kInitTimeExploded = { 2012, 3, 0, 1, 16, 34, 56, 123 }; | 290 base::Time::Exploded kInitTimeExploded = { 2012, 3, 0, 1, 16, 34, 56, 123 }; |
291 base::Time kInitTime = base::Time::FromUTCExploded(kInitTimeExploded); | 291 base::Time kInitTime = base::Time::FromUTCExploded(kInitTimeExploded); |
292 | 292 |
293 base::Time kOldUpdateTime = kInitTime - base::TimeDelta::FromDays(31); | 293 base::Time kOldUpdateTime = kInitTime - base::TimeDelta::FromDays(31); |
294 scoped_ptr<Contact> contact1(new Contact); | 294 scoped_ptr<Contact> contact1(new Contact); |
295 InitContact("provider1", "1", false, contact1.get()); | 295 InitContact("contact1", "1", false, contact1.get()); |
296 contact1->set_update_time(kOldUpdateTime.ToInternalValue()); | 296 contact1->set_update_time(kOldUpdateTime.ToInternalValue()); |
297 scoped_ptr<Contact> contact2(new Contact); | 297 scoped_ptr<Contact> contact2(new Contact); |
298 InitContact("provider2", "2", false, contact2.get()); | 298 InitContact("contact2", "2", false, contact2.get()); |
299 contact2->set_update_time(kOldUpdateTime.ToInternalValue()); | 299 contact2->set_update_time(kOldUpdateTime.ToInternalValue()); |
300 | 300 |
301 // Put both contacts in the database, along with metadata saying that the last | 301 // Put both contacts in the database, along with metadata saying that the last |
302 // successful update was 31 days in the past. | 302 // successful update was 31 days in the past. |
303 ContactPointers db_contacts; | 303 ContactPointers db_contacts; |
304 db_contacts.push_back(contact1.get()); | 304 db_contacts.push_back(contact1.get()); |
305 db_contacts.push_back(contact2.get()); | 305 db_contacts.push_back(contact2.get()); |
306 UpdateMetadata db_metadata; | 306 UpdateMetadata db_metadata; |
307 db_metadata.set_last_update_start_time(kOldUpdateTime.ToInternalValue()); | 307 db_metadata.set_last_update_start_time(kOldUpdateTime.ToInternalValue()); |
308 db_->SetContacts(db_contacts, db_metadata); | 308 db_->SetContacts(db_contacts, db_metadata); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 test_api_->set_current_time(kSecondUpdateTime); | 347 test_api_->set_current_time(kSecondUpdateTime); |
348 test_api_->DoUpdate(); | 348 test_api_->DoUpdate(); |
349 loaded_contacts.clear(); | 349 loaded_contacts.clear(); |
350 store_->AppendContacts(&loaded_contacts); | 350 store_->AppendContacts(&loaded_contacts); |
351 EXPECT_EQ(ContactsToString(gdata_contacts), | 351 EXPECT_EQ(ContactsToString(gdata_contacts), |
352 ContactsToString(loaded_contacts)); | 352 ContactsToString(loaded_contacts)); |
353 } | 353 } |
354 | 354 |
355 TEST_F(GoogleContactStoreTest, HandleDatabaseInitFailure) { | 355 TEST_F(GoogleContactStoreTest, HandleDatabaseInitFailure) { |
356 scoped_ptr<Contact> contact1(new Contact); | 356 scoped_ptr<Contact> contact1(new Contact); |
357 InitContact("provider1", "1", false, contact1.get()); | 357 InitContact("contact1", "1", false, contact1.get()); |
358 | 358 |
359 // Store a contact in the database but make initialization fail. | 359 // Store a contact in the database but make initialization fail. |
360 ContactPointers db_contacts; | 360 ContactPointers db_contacts; |
361 db_contacts.push_back(contact1.get()); | 361 db_contacts.push_back(contact1.get()); |
362 UpdateMetadata db_metadata; | 362 UpdateMetadata db_metadata; |
363 db_->SetContacts(db_contacts, db_metadata); | 363 db_->SetContacts(db_contacts, db_metadata); |
364 db_->set_init_success(false); | 364 db_->set_init_success(false); |
365 | 365 |
366 // Create a second contact and tell the GData service to return it. | 366 // Create a second contact and tell the GData service to return it. |
367 scoped_ptr<Contact> contact2(new Contact); | 367 scoped_ptr<Contact> contact2(new Contact); |
368 InitContact("provider2", "2", false, contact2.get()); | 368 InitContact("contact2", "2", false, contact2.get()); |
369 ContactPointers gdata_contacts; | 369 ContactPointers gdata_contacts; |
370 gdata_contacts.push_back(contact2.get()); | 370 gdata_contacts.push_back(contact2.get()); |
371 gdata_service_->SetContacts(gdata_contacts, base::Time()); | 371 gdata_service_->SetContacts(gdata_contacts, base::Time()); |
372 | 372 |
373 // Initialize the store. We shouldn't get the first contact (since DB | 373 // Initialize the store. We shouldn't get the first contact (since DB |
374 // initialization failed) but we should still fetch the second one from GData | 374 // initialization failed) but we should still fetch the second one from GData |
375 // and schedule an update. | 375 // and schedule an update. |
376 store_->Init(); | 376 store_->Init(); |
377 ContactPointers loaded_contacts; | 377 ContactPointers loaded_contacts; |
378 store_->AppendContacts(&loaded_contacts); | 378 store_->AppendContacts(&loaded_contacts); |
(...skipping 27 matching lines...) Expand all Loading... |
406 EXPECT_EQ(0, gdata_service_->num_download_requests()); | 406 EXPECT_EQ(0, gdata_service_->num_download_requests()); |
407 | 407 |
408 // When we're back online, the update should happen. | 408 // When we're back online, the update should happen. |
409 gdata_service_->reset_stats(); | 409 gdata_service_->reset_stats(); |
410 test_api_->NotifyAboutNetworkStateChange(true); | 410 test_api_->NotifyAboutNetworkStateChange(true); |
411 EXPECT_EQ(1, gdata_service_->num_download_requests()); | 411 EXPECT_EQ(1, gdata_service_->num_download_requests()); |
412 } | 412 } |
413 | 413 |
414 } // namespace test | 414 } // namespace test |
415 } // namespace contacts | 415 } // namespace contacts |
OLD | NEW |