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.h" | |
10 #include "base/time.h" | |
11 #include "chrome/test/base/testing_profile.h" | |
12 #include "chrome/browser/chromeos/contacts/contact.pb.h" | |
13 #include "chrome/browser/chromeos/contacts/contact_store_observer.h" | |
14 #include "chrome/browser/chromeos/contacts/contact_test_util.h" | |
15 #include "chrome/browser/chromeos/contacts/fake_contact_database.h" | |
16 #include "chrome/browser/chromeos/gdata/gdata_contacts_service_stub.h" | |
17 #include "chrome/browser/chromeos/gdata/gdata_util.h" | |
18 #include "content/public/browser/browser_thread.h" | |
19 #include "content/public/test/test_browser_thread.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 using content::BrowserThread; | |
23 | |
24 namespace contacts { | |
25 namespace test { | |
26 | |
27 // ContactStoreObserver implementation that just counts the number of times | |
28 // that it's been told that a store has been updated. | |
29 class TestContactStoreObserver : public ContactStoreObserver { | |
30 public: | |
31 TestContactStoreObserver() : num_updates_(0) {} | |
32 ~TestContactStoreObserver() {} | |
33 | |
34 int num_updates() const { return num_updates_; } | |
35 void reset_stats() { num_updates_ = 0; } | |
36 | |
37 // ContactStoreObserver overrides: | |
38 void OnContactsUpdated(ContactStore* store) OVERRIDE { | |
39 DCHECK(store); | |
40 num_updates_++; | |
41 } | |
42 | |
43 private: | |
44 // Number of times that OnContactsUpdated() has been called. | |
45 int num_updates_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(TestContactStoreObserver); | |
48 }; | |
49 | |
50 class GoogleContactStoreTest : public testing::Test { | |
51 public: | |
52 GoogleContactStoreTest() : ui_thread_(BrowserThread::UI, &message_loop_) {} | |
53 virtual ~GoogleContactStoreTest() {} | |
54 | |
55 protected: | |
56 // testing::Test implementation. | |
57 virtual void SetUp() OVERRIDE { | |
58 testing::Test::SetUp(); | |
59 profile_.reset(new TestingProfile); | |
60 | |
61 store_.reset(new GoogleContactStore(profile_.get())); | |
62 store_->AddObserver(&observer_); | |
63 | |
64 test_api_.reset(new GoogleContactStore::TestAPI(store_.get())); | |
65 | |
66 db_ = new FakeContactDatabase; | |
67 test_api_->SetDatabase(db_); | |
68 | |
69 gdata_service_ = new gdata::GDataContactsServiceStub; | |
70 test_api_->SetGDataService(gdata_service_); | |
71 } | |
72 | |
73 private: | |
74 MessageLoopForUI message_loop_; | |
Daniel Erat
2012/08/02 23:34:22
There's no real reason to make these private. I'l
| |
75 content::TestBrowserThread ui_thread_; | |
76 | |
77 protected: | |
78 TestContactStoreObserver observer_; | |
79 scoped_ptr<TestingProfile> profile_; | |
80 scoped_ptr<GoogleContactStore> store_; | |
81 scoped_ptr<GoogleContactStore::TestAPI> test_api_; | |
82 | |
83 FakeContactDatabase* db_; // not owned | |
84 gdata::GDataContactsServiceStub* gdata_service_; // not owned | |
85 | |
86 private: | |
87 DISALLOW_COPY_AND_ASSIGN(GoogleContactStoreTest); | |
88 }; | |
89 | |
90 TEST_F(GoogleContactStoreTest, LoadFromDatabase) { | |
91 // Store two contacts in the database. | |
92 const std::string kProviderId1 = "provider1"; | |
93 const std::string kProviderId2 = "provider2"; | |
94 scoped_ptr<Contact> contact1(new Contact); | |
95 InitContact(kProviderId1, "1", false, contact1.get()); | |
96 scoped_ptr<Contact> contact2(new Contact); | |
97 InitContact(kProviderId2, "2", false, contact2.get()); | |
98 ContactPointers db_contacts; | |
99 db_contacts.push_back(contact1.get()); | |
100 db_contacts.push_back(contact2.get()); | |
101 UpdateMetadata db_metadata; | |
102 db_->SetContacts(db_contacts, db_metadata); | |
103 | |
104 // Tell the GData service to report failure, initialize the store, and check | |
105 // that the contacts from the database are loaded. | |
106 gdata_service_->set_download_should_succeed(false); | |
107 store_->Init(); | |
108 ContactPointers loaded_contacts; | |
109 store_->AppendContacts(&loaded_contacts); | |
110 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), | |
111 ContactsToString(loaded_contacts)); | |
112 EXPECT_TRUE(test_api_->update_scheduled()); | |
113 EXPECT_EQ(1, observer_.num_updates()); | |
114 | |
115 // Check that we can also grab the contact via its ID. | |
116 const Contact* loaded_contact1 = store_->GetContactByProviderId(kProviderId1); | |
117 ASSERT_TRUE(loaded_contact1); | |
118 EXPECT_EQ(ContactToString(*contact1), ContactToString(*loaded_contact1)); | |
119 | |
120 // We should get NULL if we request a nonexistent contact. | |
121 EXPECT_FALSE(store_->GetContactByProviderId("bogus_id")); | |
122 } | |
123 | |
124 TEST_F(GoogleContactStoreTest, LoadFromGData) { | |
125 // Store two contacts in the GData service. | |
126 scoped_ptr<Contact> contact1(new Contact); | |
127 InitContact("provider1", "1", false, contact1.get()); | |
128 scoped_ptr<Contact> contact2(new Contact); | |
129 InitContact("provider2", "2", false, contact2.get()); | |
130 ContactPointers gdata_contacts; | |
131 gdata_contacts.push_back(contact1.get()); | |
132 gdata_contacts.push_back(contact2.get()); | |
133 gdata_service_->SetContacts(gdata_contacts, base::Time()); | |
134 | |
135 // Initialize the store and check that the contacts are loaded from GData. | |
136 store_->Init(); | |
137 ContactPointers loaded_contacts; | |
138 store_->AppendContacts(&loaded_contacts); | |
139 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), | |
140 ContactsToString(loaded_contacts)); | |
141 EXPECT_TRUE(test_api_->update_scheduled()); | |
142 EXPECT_EQ(1, observer_.num_updates()); | |
143 | |
144 // The contacts should've been saved to the database, too. | |
145 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), | |
146 ContactsToString(db_->contacts())); | |
147 } | |
148 | |
149 TEST_F(GoogleContactStoreTest, UpdateFromGData) { | |
150 scoped_ptr<Contact> contact1(new Contact); | |
151 InitContact("provider1", "1", false, contact1.get()); | |
152 scoped_ptr<Contact> contact2(new Contact); | |
153 InitContact("provider2", "2", false, contact2.get()); | |
154 scoped_ptr<Contact> contact3(new Contact); | |
155 InitContact("provider3", "3", false, contact3.get()); | |
156 | |
157 // Store the first two contacts in the database. | |
158 ContactPointers db_contacts; | |
159 db_contacts.push_back(contact1.get()); | |
160 db_contacts.push_back(contact2.get()); | |
161 UpdateMetadata db_metadata; | |
162 db_->SetContacts(db_contacts, db_metadata); | |
163 | |
164 // Store all three in the GData service. We expect the update request to ask | |
165 // for all contacts updated one millisecond after the newest contact in the | |
166 // database. | |
167 ContactPointers gdata_contacts; | |
168 gdata_contacts.push_back(contact1.get()); | |
169 gdata_contacts.push_back(contact2.get()); | |
170 gdata_contacts.push_back(contact3.get()); | |
171 gdata_service_->SetContacts( | |
172 gdata_contacts, | |
173 base::Time::FromInternalValue(contact2->update_time()) + | |
174 base::TimeDelta::FromMilliseconds(1)); | |
175 | |
176 // Check that the store ends up with all three contacts. | |
177 store_->Init(); | |
178 ContactPointers loaded_contacts; | |
179 store_->AppendContacts(&loaded_contacts); | |
180 EXPECT_EQ(VarContactsToString( | |
181 3, contact1.get(), contact2.get(), contact3.get()), | |
182 ContactsToString(loaded_contacts)); | |
183 EXPECT_EQ(2, observer_.num_updates()); | |
184 | |
185 // All three contacts should've been saved to the database. | |
186 EXPECT_EQ(VarContactsToString( | |
187 3, contact1.get(), contact2.get(), contact3.get()), | |
188 ContactsToString(db_->contacts())); | |
189 EXPECT_TRUE(test_api_->update_scheduled()); | |
190 } | |
191 | |
192 TEST_F(GoogleContactStoreTest, FetchUpdatedContacts) { | |
193 scoped_ptr<Contact> contact1(new Contact); | |
194 InitContact("provider1", "1", false, contact1.get()); | |
195 scoped_ptr<Contact> contact2(new Contact); | |
196 InitContact("provider2", "2", false, contact2.get()); | |
197 scoped_ptr<Contact> contact3(new Contact); | |
198 InitContact("provider3", "3", false, contact3.get()); | |
199 | |
200 // Tell the GData service to return all three contacts in response to a full | |
201 // update. | |
202 ContactPointers gdata_contacts; | |
203 gdata_contacts.push_back(contact1.get()); | |
204 gdata_contacts.push_back(contact2.get()); | |
205 gdata_contacts.push_back(contact3.get()); | |
206 gdata_service_->SetContacts(gdata_contacts, base::Time()); | |
207 | |
208 store_->Init(); | |
209 ContactPointers loaded_contacts; | |
210 store_->AppendContacts(&loaded_contacts); | |
211 EXPECT_EQ(ContactsToString(gdata_contacts), | |
212 ContactsToString(loaded_contacts)); | |
213 EXPECT_EQ(ContactsToString(gdata_contacts), | |
214 ContactsToString(db_->contacts())); | |
215 EXPECT_TRUE(test_api_->update_scheduled()); | |
216 EXPECT_EQ(1, observer_.num_updates()); | |
217 observer_.reset_stats(); | |
218 | |
219 // Update the third contact. | |
220 contact3->set_full_name("new full name"); | |
221 base::Time old_contact3_update_time = | |
222 base::Time::FromInternalValue(contact3->update_time()); | |
223 contact3->set_update_time( | |
224 (old_contact3_update_time + | |
225 base::TimeDelta::FromSeconds(10)).ToInternalValue()); | |
226 gdata_service_->SetContacts( | |
227 gdata_contacts, | |
228 old_contact3_update_time + base::TimeDelta::FromMilliseconds(1)); | |
229 | |
230 // Check that the updated contact is loaded (i.e. the store passed the | |
231 // correct minimum update time to the service). | |
232 test_api_->DoUpdate(); | |
233 loaded_contacts.clear(); | |
234 store_->AppendContacts(&loaded_contacts); | |
235 EXPECT_EQ(ContactsToString(gdata_contacts), | |
236 ContactsToString(loaded_contacts)); | |
237 EXPECT_EQ(ContactsToString(gdata_contacts), | |
238 ContactsToString(db_->contacts())); | |
239 EXPECT_TRUE(test_api_->update_scheduled()); | |
240 EXPECT_EQ(1, observer_.num_updates()); | |
241 observer_.reset_stats(); | |
242 | |
243 // The next update should be based on the third contact's new update time. | |
244 contact3->set_full_name("yet another full name"); | |
245 gdata_service_->SetContacts( | |
246 gdata_contacts, | |
247 base::Time::FromInternalValue(contact3->update_time()) + | |
248 base::TimeDelta::FromMilliseconds(1)); | |
249 test_api_->DoUpdate(); | |
250 loaded_contacts.clear(); | |
251 store_->AppendContacts(&loaded_contacts); | |
252 EXPECT_EQ(ContactsToString(gdata_contacts), | |
253 ContactsToString(loaded_contacts)); | |
254 EXPECT_EQ(ContactsToString(gdata_contacts), | |
255 ContactsToString(db_->contacts())); | |
256 EXPECT_TRUE(test_api_->update_scheduled()); | |
257 EXPECT_EQ(1, observer_.num_updates()); | |
258 } | |
259 | |
260 TEST_F(GoogleContactStoreTest, DontReturnDeletedContacts) { | |
261 // Tell GData to return a single deleted contact. | |
262 const std::string kProviderId = "provider"; | |
263 scoped_ptr<Contact> contact(new Contact); | |
264 InitContact(kProviderId, "1", true, contact.get()); | |
265 ContactPointers gdata_contacts; | |
266 gdata_contacts.push_back(contact.get()); | |
267 gdata_service_->SetContacts(gdata_contacts, base::Time()); | |
268 | |
269 // The contact shouldn't be returned by AppendContacts() or | |
270 // GetContactByProviderId(). | |
271 store_->Init(); | |
272 ContactPointers loaded_contacts; | |
273 store_->AppendContacts(&loaded_contacts); | |
274 EXPECT_TRUE(loaded_contacts.empty()); | |
275 EXPECT_TRUE(store_->GetContactByProviderId(kProviderId) == NULL); | |
276 } | |
277 | |
278 // Test that we do a full refresh from GData if enough time has passed since the | |
279 // last refresh that we might've missed some contact deletions (see | |
280 // |kForceFullUpdateDays| in google_contact_store.cc). | |
281 TEST_F(GoogleContactStoreTest, FullRefreshAfterThirtyDays) { | |
282 base::Time::Exploded kInitTimeExploded = { 2012, 3, 0, 1, 16, 34, 56, 123 }; | |
283 base::Time kInitTime = base::Time::FromUTCExploded(kInitTimeExploded); | |
284 | |
285 base::Time kOldUpdateTime = kInitTime - base::TimeDelta::FromDays(31); | |
286 scoped_ptr<Contact> contact1(new Contact); | |
287 InitContact("provider1", "1", false, contact1.get()); | |
288 contact1->set_update_time(kOldUpdateTime.ToInternalValue()); | |
289 scoped_ptr<Contact> contact2(new Contact); | |
290 InitContact("provider2", "2", false, contact2.get()); | |
291 contact2->set_update_time(kOldUpdateTime.ToInternalValue()); | |
292 | |
293 // Put both contacts in the database, along with metadata saying that the last | |
294 // successful update was 31 days in the past. | |
295 ContactPointers db_contacts; | |
296 db_contacts.push_back(contact1.get()); | |
297 db_contacts.push_back(contact2.get()); | |
298 UpdateMetadata db_metadata; | |
299 db_metadata.set_last_update_start_time(kOldUpdateTime.ToInternalValue()); | |
300 db_->SetContacts(db_contacts, db_metadata); | |
301 | |
302 // Tell the GData service to return only the first contact and to expect a | |
303 // full refresh (since it's been a long time since the last update). | |
304 ContactPointers gdata_contacts; | |
305 gdata_contacts.push_back(contact1.get()); | |
306 gdata_service_->SetContacts(gdata_contacts, base::Time()); | |
307 | |
308 test_api_->set_current_time(kInitTime); | |
309 store_->Init(); | |
310 ContactPointers loaded_contacts; | |
311 store_->AppendContacts(&loaded_contacts); | |
312 EXPECT_EQ(ContactsToString(gdata_contacts), | |
313 ContactsToString(loaded_contacts)); | |
314 EXPECT_TRUE(test_api_->update_scheduled()); | |
315 | |
316 // Make GData return both contacts now in response to an incremental update. | |
317 gdata_contacts.clear(); | |
318 contact2->set_update_time(kInitTime.ToInternalValue()); | |
319 gdata_contacts.push_back(contact1.get()); | |
320 gdata_contacts.push_back(contact2.get()); | |
321 gdata_service_->SetContacts( | |
322 gdata_contacts, kOldUpdateTime + base::TimeDelta::FromMilliseconds(1)); | |
323 | |
324 // Advance the time by twenty days and check that the update is successful. | |
325 base::Time kFirstUpdateTime = kInitTime + base::TimeDelta::FromDays(20); | |
326 test_api_->set_current_time(kFirstUpdateTime); | |
327 test_api_->DoUpdate(); | |
328 loaded_contacts.clear(); | |
329 store_->AppendContacts(&loaded_contacts); | |
330 EXPECT_EQ(ContactsToString(gdata_contacts), | |
331 ContactsToString(loaded_contacts)); | |
332 | |
333 // After we advance the time by 31 days, we should do a full refresh again. | |
334 gdata_contacts.clear(); | |
335 gdata_contacts.push_back(contact1.get()); | |
336 gdata_service_->SetContacts(gdata_contacts, base::Time()); | |
337 base::Time kSecondUpdateTime = | |
338 kFirstUpdateTime + base::TimeDelta::FromDays(31); | |
339 test_api_->set_current_time(kSecondUpdateTime); | |
340 test_api_->DoUpdate(); | |
341 loaded_contacts.clear(); | |
342 store_->AppendContacts(&loaded_contacts); | |
343 EXPECT_EQ(ContactsToString(gdata_contacts), | |
344 ContactsToString(loaded_contacts)); | |
345 } | |
346 | |
347 TEST_F(GoogleContactStoreTest, HandleDatabaseInitFailure) { | |
348 scoped_ptr<Contact> contact1(new Contact); | |
349 InitContact("provider1", "1", false, contact1.get()); | |
350 | |
351 // Store a contact in the database but make initialization fail. | |
352 ContactPointers db_contacts; | |
353 db_contacts.push_back(contact1.get()); | |
354 UpdateMetadata db_metadata; | |
355 db_->SetContacts(db_contacts, db_metadata); | |
356 db_->set_init_success(false); | |
357 | |
358 // Create a second contact and tell the GData service to return it. | |
359 scoped_ptr<Contact> contact2(new Contact); | |
360 InitContact("provider2", "2", false, contact2.get()); | |
361 ContactPointers gdata_contacts; | |
362 gdata_contacts.push_back(contact2.get()); | |
363 gdata_service_->SetContacts(gdata_contacts, base::Time()); | |
364 | |
365 // Initialize the store. We shouldn't get the first contact (since DB | |
366 // initialization failed) but we should still fetch the second one from GData | |
367 // and schedule an update. | |
368 store_->Init(); | |
369 ContactPointers loaded_contacts; | |
370 store_->AppendContacts(&loaded_contacts); | |
371 EXPECT_EQ(ContactsToString(gdata_contacts), | |
372 ContactsToString(loaded_contacts)); | |
373 EXPECT_TRUE(test_api_->update_scheduled()); | |
374 } | |
375 | |
376 } // namespace test | |
377 } // namespace contacts | |
OLD | NEW |