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/contact_database.h" | 5 #include "chrome/browser/chromeos/contacts/contact_database.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 | 70 |
71 void DestroyDatabase() { | 71 void DestroyDatabase() { |
72 if (db_) { | 72 if (db_) { |
73 db_->DestroyOnUIThread(); | 73 db_->DestroyOnUIThread(); |
74 db_ = NULL; | 74 db_ = NULL; |
75 } | 75 } |
76 } | 76 } |
77 | 77 |
78 // Calls ContactDatabase::SaveContacts() and blocks until the operation is | 78 // Calls ContactDatabase::SaveContacts() and blocks until the operation is |
79 // complete. | 79 // complete. |
80 void SaveContacts(scoped_ptr<ContactPointers> contacts, bool is_full_update) { | 80 void SaveContacts(scoped_ptr<ContactPointers> contacts, |
| 81 scoped_ptr<UpdateMetadata> metadata, |
| 82 bool is_full_update) { |
81 CHECK(db_); | 83 CHECK(db_); |
82 db_->SaveContacts(contacts.Pass(), is_full_update, | 84 db_->SaveContacts(contacts.Pass(), metadata.Pass(), is_full_update, |
83 base::Bind(&ContactDatabaseTest::OnContactsSaved, | 85 base::Bind(&ContactDatabaseTest::OnContactsSaved, |
84 base::Unretained(this))); | 86 base::Unretained(this))); |
85 message_loop_.Run(); | 87 message_loop_.Run(); |
86 } | 88 } |
87 | 89 |
88 // Calls ContactDatabase::LoadContacts() and blocks until the operation is | 90 // Calls ContactDatabase::LoadContacts() and blocks until the operation is |
89 // complete. | 91 // complete. |
90 scoped_ptr<ScopedVector<Contact> > LoadContacts() { | 92 void LoadContacts(scoped_ptr<ScopedVector<Contact> >* contacts_out, |
| 93 scoped_ptr<UpdateMetadata>* metadata_out) { |
91 CHECK(db_); | 94 CHECK(db_); |
92 db_->LoadContacts(base::Bind(&ContactDatabaseTest::OnContactsLoaded, | 95 db_->LoadContacts(base::Bind(&ContactDatabaseTest::OnContactsLoaded, |
93 base::Unretained(this))); | 96 base::Unretained(this))); |
94 message_loop_.Run(); | 97 message_loop_.Run(); |
95 return loaded_contacts_.Pass(); | 98 contacts_out->swap(loaded_contacts_); |
| 99 metadata_out->swap(loaded_metadata_); |
96 } | 100 } |
97 | 101 |
98 private: | 102 private: |
99 void OnDatabaseInitialized(bool success) { | 103 void OnDatabaseInitialized(bool success) { |
100 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 104 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
101 CHECK(success); | 105 CHECK(success); |
102 // TODO(derat): Move gdata::test::RunBlockingPoolTask() to a shared location | 106 // TODO(derat): Move gdata::test::RunBlockingPoolTask() to a shared location |
103 // and use it for these tests. | 107 // and use it for these tests. |
104 message_loop_.Quit(); | 108 message_loop_.Quit(); |
105 } | 109 } |
106 | 110 |
107 void OnContactsSaved(bool success) { | 111 void OnContactsSaved(bool success) { |
108 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 112 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
109 CHECK(success); | 113 CHECK(success); |
110 message_loop_.Quit(); | 114 message_loop_.Quit(); |
111 } | 115 } |
112 | 116 |
113 void OnContactsLoaded(bool success, | 117 void OnContactsLoaded(bool success, |
114 scoped_ptr<ScopedVector<Contact> > contacts) { | 118 scoped_ptr<ScopedVector<Contact> > contacts, |
| 119 scoped_ptr<UpdateMetadata> metadata) { |
115 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 120 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
116 CHECK(success); | 121 CHECK(success); |
117 loaded_contacts_.swap(contacts); | 122 loaded_contacts_.swap(contacts); |
| 123 loaded_metadata_.swap(metadata); |
118 message_loop_.Quit(); | 124 message_loop_.Quit(); |
119 } | 125 } |
120 | 126 |
121 MessageLoopForUI message_loop_; | 127 MessageLoopForUI message_loop_; |
122 content::TestBrowserThread ui_thread_; | 128 content::TestBrowserThread ui_thread_; |
123 | 129 |
124 // Temporary directory where the database is saved. | 130 // Temporary directory where the database is saved. |
125 ScopedTempDir temp_dir_; | 131 ScopedTempDir temp_dir_; |
126 | 132 |
127 // This class retains ownership of this object. | 133 // This class retains ownership of this object. |
128 ContactDatabase* db_; | 134 ContactDatabase* db_; |
129 | 135 |
130 // Contacts returned by the most-recent ContactDatabase::LoadContacts() call. | 136 // Contacts and metadata returned by the most-recent |
131 // Used to pass contacts from OnContactsLoaded() to LoadContacts(). | 137 // ContactDatabase::LoadContacts() call. Used to pass returned values from |
| 138 // OnContactsLoaded() to LoadContacts(). |
132 scoped_ptr<ScopedVector<Contact> > loaded_contacts_; | 139 scoped_ptr<ScopedVector<Contact> > loaded_contacts_; |
| 140 scoped_ptr<UpdateMetadata> loaded_metadata_; |
133 | 141 |
134 DISALLOW_COPY_AND_ASSIGN(ContactDatabaseTest); | 142 DISALLOW_COPY_AND_ASSIGN(ContactDatabaseTest); |
135 }; | 143 }; |
136 | 144 |
137 TEST_F(ContactDatabaseTest, SaveAndReload) { | 145 TEST_F(ContactDatabaseTest, SaveAndReload) { |
138 // Save a contact to the database and check that we get the same data back | 146 // Save a contact to the database and check that we get the same data back |
139 // when loading it. | 147 // when loading it. |
140 const std::string kProviderId = "provider_id_1"; | 148 const std::string kProviderId = "provider_id_1"; |
141 scoped_ptr<Contact> contact(new Contact); | 149 scoped_ptr<Contact> contact(new Contact); |
142 InitContact(kProviderId, "1", false, contact.get()); | 150 InitContact(kProviderId, "1", false, contact.get()); |
143 AddEmailAddress("email_1", Contact_AddressType_Relation_HOME, | 151 AddEmailAddress("email_1", Contact_AddressType_Relation_HOME, |
144 "email_label_1", true, contact.get()); | 152 "email_label_1", true, contact.get()); |
145 AddEmailAddress("email_2", Contact_AddressType_Relation_WORK, | 153 AddEmailAddress("email_2", Contact_AddressType_Relation_WORK, |
146 "", false, contact.get()); | 154 "", false, contact.get()); |
147 AddPhoneNumber("123-456-7890", Contact_AddressType_Relation_HOME, | 155 AddPhoneNumber("123-456-7890", Contact_AddressType_Relation_HOME, |
148 "phone_label", true, contact.get()); | 156 "phone_label", true, contact.get()); |
149 AddPostalAddress("postal_1", Contact_AddressType_Relation_HOME, | 157 AddPostalAddress("postal_1", Contact_AddressType_Relation_HOME, |
150 "postal_label_1", true, contact.get()); | 158 "postal_label_1", true, contact.get()); |
151 AddPostalAddress("postal_2", Contact_AddressType_Relation_OTHER, | 159 AddPostalAddress("postal_2", Contact_AddressType_Relation_OTHER, |
152 "postal_label_2", false, contact.get()); | 160 "postal_label_2", false, contact.get()); |
153 AddInstantMessagingAddress("im_1", | 161 AddInstantMessagingAddress("im_1", |
154 Contact_InstantMessagingAddress_Protocol_AIM, | 162 Contact_InstantMessagingAddress_Protocol_AIM, |
155 Contact_AddressType_Relation_HOME, | 163 Contact_AddressType_Relation_HOME, |
156 "im_label_1", true, contact.get()); | 164 "im_label_1", true, contact.get()); |
157 SetPhoto(gfx::Size(20, 20), contact.get()); | 165 SetPhoto(gfx::Size(20, 20), contact.get()); |
158 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); | 166 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); |
159 contacts_to_save->push_back(contact.get()); | 167 contacts_to_save->push_back(contact.get()); |
160 SaveContacts(contacts_to_save.Pass(), true); | |
161 | 168 |
162 scoped_ptr<ScopedVector<Contact> > loaded_contacts = LoadContacts(); | 169 const int64 kLastUpdateTime = 1234; |
| 170 scoped_ptr<UpdateMetadata> metadata_to_save(new UpdateMetadata); |
| 171 metadata_to_save->set_last_update_start_time(kLastUpdateTime); |
| 172 |
| 173 SaveContacts(contacts_to_save.Pass(), metadata_to_save.Pass(), true); |
| 174 scoped_ptr<ScopedVector<Contact> > loaded_contacts; |
| 175 scoped_ptr<UpdateMetadata> loaded_metadata; |
| 176 LoadContacts(&loaded_contacts, &loaded_metadata); |
163 EXPECT_EQ(VarContactsToString(1, contact.get()), | 177 EXPECT_EQ(VarContactsToString(1, contact.get()), |
164 ContactsToString(*loaded_contacts)); | 178 ContactsToString(*loaded_contacts)); |
| 179 EXPECT_EQ(kLastUpdateTime, loaded_metadata->last_update_start_time()); |
165 | 180 |
166 // Modify the contact, save it, and check that the loaded contact is also | 181 // Modify the contact, save it, and check that the loaded contact is also |
167 // updated. | 182 // updated. |
168 InitContact(kProviderId, "2", false, contact.get()); | 183 InitContact(kProviderId, "2", false, contact.get()); |
169 AddEmailAddress("email_3", Contact_AddressType_Relation_OTHER, | 184 AddEmailAddress("email_3", Contact_AddressType_Relation_OTHER, |
170 "email_label_2", true, contact.get()); | 185 "email_label_2", true, contact.get()); |
171 AddPhoneNumber("phone_2", Contact_AddressType_Relation_OTHER, | 186 AddPhoneNumber("phone_2", Contact_AddressType_Relation_OTHER, |
172 "phone_label_2", false, contact.get()); | 187 "phone_label_2", false, contact.get()); |
173 AddPostalAddress("postal_3", Contact_AddressType_Relation_HOME, | 188 AddPostalAddress("postal_3", Contact_AddressType_Relation_HOME, |
174 "postal_label_3", true, contact.get()); | 189 "postal_label_3", true, contact.get()); |
175 SetPhoto(gfx::Size(64, 64), contact.get()); | 190 SetPhoto(gfx::Size(64, 64), contact.get()); |
176 contacts_to_save.reset(new ContactPointers); | 191 contacts_to_save.reset(new ContactPointers); |
177 contacts_to_save->push_back(contact.get()); | 192 contacts_to_save->push_back(contact.get()); |
178 SaveContacts(contacts_to_save.Pass(), true); | 193 metadata_to_save.reset(new UpdateMetadata); |
| 194 const int64 kNewLastUpdateTime = 5678; |
| 195 metadata_to_save->set_last_update_start_time(kNewLastUpdateTime); |
| 196 SaveContacts(contacts_to_save.Pass(), metadata_to_save.Pass(), true); |
179 | 197 |
180 loaded_contacts = LoadContacts(); | 198 LoadContacts(&loaded_contacts, &loaded_metadata); |
181 EXPECT_EQ(VarContactsToString(1, contact.get()), | 199 EXPECT_EQ(VarContactsToString(1, contact.get()), |
182 ContactsToString(*loaded_contacts)); | 200 ContactsToString(*loaded_contacts)); |
| 201 EXPECT_EQ(kNewLastUpdateTime, loaded_metadata->last_update_start_time()); |
183 } | 202 } |
184 | 203 |
185 TEST_F(ContactDatabaseTest, FullAndPartialUpdates) { | 204 TEST_F(ContactDatabaseTest, FullAndPartialUpdates) { |
186 // Do a full update that inserts two contacts into the database. | 205 // Do a full update that inserts two contacts into the database. |
187 const std::string kProviderId1 = "provider_id_1"; | 206 const std::string kProviderId1 = "provider_id_1"; |
188 const std::string kSharedEmail = "foo@example.org"; | 207 const std::string kSharedEmail = "foo@example.org"; |
189 scoped_ptr<Contact> contact1(new Contact); | 208 scoped_ptr<Contact> contact1(new Contact); |
190 InitContact(kProviderId1, "1", false, contact1.get()); | 209 InitContact(kProviderId1, "1", false, contact1.get()); |
191 AddEmailAddress(kSharedEmail, Contact_AddressType_Relation_HOME, | 210 AddEmailAddress(kSharedEmail, Contact_AddressType_Relation_HOME, |
192 "", true, contact1.get()); | 211 "", true, contact1.get()); |
193 | 212 |
194 const std::string kProviderId2 = "provider_id_2"; | 213 const std::string kProviderId2 = "provider_id_2"; |
195 scoped_ptr<Contact> contact2(new Contact); | 214 scoped_ptr<Contact> contact2(new Contact); |
196 InitContact(kProviderId2, "2", false, contact2.get()); | 215 InitContact(kProviderId2, "2", false, contact2.get()); |
197 AddEmailAddress(kSharedEmail, Contact_AddressType_Relation_WORK, | 216 AddEmailAddress(kSharedEmail, Contact_AddressType_Relation_WORK, |
198 "", true, contact2.get()); | 217 "", true, contact2.get()); |
199 | 218 |
200 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); | 219 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); |
201 contacts_to_save->push_back(contact1.get()); | 220 contacts_to_save->push_back(contact1.get()); |
202 contacts_to_save->push_back(contact2.get()); | 221 contacts_to_save->push_back(contact2.get()); |
203 SaveContacts(contacts_to_save.Pass(), true); | 222 scoped_ptr<UpdateMetadata> metadata_to_save(new UpdateMetadata); |
204 scoped_ptr<ScopedVector<Contact> > loaded_contacts = LoadContacts(); | 223 SaveContacts(contacts_to_save.Pass(), metadata_to_save.Pass(), true); |
| 224 |
| 225 scoped_ptr<ScopedVector<Contact> > loaded_contacts; |
| 226 scoped_ptr<UpdateMetadata> loaded_metadata; |
| 227 LoadContacts(&loaded_contacts, &loaded_metadata); |
205 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), | 228 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), |
206 ContactsToString(*loaded_contacts)); | 229 ContactsToString(*loaded_contacts)); |
207 | 230 |
208 // Do a partial update including just the second contact. | 231 // Do a partial update including just the second contact. |
209 InitContact(kProviderId2, "2b", false, contact2.get()); | 232 InitContact(kProviderId2, "2b", false, contact2.get()); |
210 AddPostalAddress("postal_1", Contact_AddressType_Relation_HOME, | 233 AddPostalAddress("postal_1", Contact_AddressType_Relation_HOME, |
211 "", true, contact2.get()); | 234 "", true, contact2.get()); |
212 contacts_to_save.reset(new ContactPointers); | 235 contacts_to_save.reset(new ContactPointers); |
213 contacts_to_save->push_back(contact2.get()); | 236 contacts_to_save->push_back(contact2.get()); |
214 SaveContacts(contacts_to_save.Pass(), false); | 237 metadata_to_save.reset(new UpdateMetadata); |
215 loaded_contacts = LoadContacts(); | 238 SaveContacts(contacts_to_save.Pass(), metadata_to_save.Pass(), false); |
| 239 LoadContacts(&loaded_contacts, &loaded_metadata); |
216 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), | 240 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), |
217 ContactsToString(*loaded_contacts)); | 241 ContactsToString(*loaded_contacts)); |
218 | 242 |
| 243 // Do an empty partial update and check that the metadata is still updated. |
| 244 contacts_to_save.reset(new ContactPointers); |
| 245 metadata_to_save.reset(new UpdateMetadata); |
| 246 const int64 kLastUpdateTime = 1234; |
| 247 metadata_to_save->set_last_update_start_time(kLastUpdateTime); |
| 248 SaveContacts(contacts_to_save.Pass(), metadata_to_save.Pass(), false); |
| 249 LoadContacts(&loaded_contacts, &loaded_metadata); |
| 250 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), |
| 251 ContactsToString(*loaded_contacts)); |
| 252 EXPECT_EQ(kLastUpdateTime, loaded_metadata->last_update_start_time()); |
| 253 |
219 // Do a full update including just the first contact. The second contact | 254 // Do a full update including just the first contact. The second contact |
220 // should be removed from the database. | 255 // should be removed from the database. |
221 InitContact(kProviderId1, "1b", false, contact1.get()); | 256 InitContact(kProviderId1, "1b", false, contact1.get()); |
222 AddPostalAddress("postal_2", Contact_AddressType_Relation_WORK, | 257 AddPostalAddress("postal_2", Contact_AddressType_Relation_WORK, |
223 "", true, contact1.get()); | 258 "", true, contact1.get()); |
224 AddPhoneNumber("phone", Contact_AddressType_Relation_HOME, | 259 AddPhoneNumber("phone", Contact_AddressType_Relation_HOME, |
225 "", true, contact1.get()); | 260 "", true, contact1.get()); |
226 contacts_to_save.reset(new ContactPointers); | 261 contacts_to_save.reset(new ContactPointers); |
227 contacts_to_save->push_back(contact1.get()); | 262 contacts_to_save->push_back(contact1.get()); |
228 SaveContacts(contacts_to_save.Pass(), true); | 263 metadata_to_save.reset(new UpdateMetadata); |
229 loaded_contacts = LoadContacts(); | 264 SaveContacts(contacts_to_save.Pass(), metadata_to_save.Pass(), true); |
| 265 LoadContacts(&loaded_contacts, &loaded_metadata); |
230 EXPECT_EQ(VarContactsToString(1, contact1.get()), | 266 EXPECT_EQ(VarContactsToString(1, contact1.get()), |
231 ContactsToString(*loaded_contacts)); | 267 ContactsToString(*loaded_contacts)); |
232 | 268 |
233 // Do a full update including no contacts. The database should be cleared. | 269 // Do a full update including no contacts. The database should be cleared. |
234 contacts_to_save.reset(new ContactPointers); | 270 contacts_to_save.reset(new ContactPointers); |
235 SaveContacts(contacts_to_save.Pass(), true); | 271 metadata_to_save.reset(new UpdateMetadata); |
236 loaded_contacts = LoadContacts(); | 272 SaveContacts(contacts_to_save.Pass(), metadata_to_save.Pass(), true); |
| 273 LoadContacts(&loaded_contacts, &loaded_metadata); |
237 EXPECT_TRUE(loaded_contacts->empty()); | 274 EXPECT_TRUE(loaded_contacts->empty()); |
238 } | 275 } |
239 | 276 |
240 // Test that we create a new database when we encounter a corrupted one. | 277 // Test that we create a new database when we encounter a corrupted one. |
241 TEST_F(ContactDatabaseTest, DeleteWhenCorrupt) { | 278 TEST_F(ContactDatabaseTest, DeleteWhenCorrupt) { |
242 DestroyDatabase(); | 279 DestroyDatabase(); |
243 // Overwrite all of the files in the database with a space character. | 280 // Overwrite all of the files in the database with a space character. |
244 file_util::FileEnumerator enumerator( | 281 file_util::FileEnumerator enumerator( |
245 database_path(), false, file_util::FileEnumerator::FILES); | 282 database_path(), false, file_util::FileEnumerator::FILES); |
246 for (FilePath path = enumerator.Next(); !path.empty(); | 283 for (FilePath path = enumerator.Next(); !path.empty(); |
247 path = enumerator.Next()) { | 284 path = enumerator.Next()) { |
248 file_util::WriteFile(path, " ", 1); | 285 file_util::WriteFile(path, " ", 1); |
249 } | 286 } |
250 CreateDatabase(); | 287 CreateDatabase(); |
251 | 288 |
252 // Make sure that the resulting database is usable. | 289 // Make sure that the resulting database is usable. |
253 scoped_ptr<Contact> contact(new Contact); | 290 scoped_ptr<Contact> contact(new Contact); |
254 InitContact("1", "1", false, contact.get()); | 291 InitContact("1", "1", false, contact.get()); |
255 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); | 292 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); |
256 contacts_to_save->push_back(contact.get()); | 293 contacts_to_save->push_back(contact.get()); |
257 SaveContacts(contacts_to_save.Pass(), true); | 294 scoped_ptr<UpdateMetadata> metadata_to_save(new UpdateMetadata); |
258 scoped_ptr<ScopedVector<Contact> > loaded_contacts = LoadContacts(); | 295 SaveContacts(contacts_to_save.Pass(), metadata_to_save.Pass(), true); |
| 296 |
| 297 scoped_ptr<ScopedVector<Contact> > loaded_contacts; |
| 298 scoped_ptr<UpdateMetadata> loaded_metadata; |
| 299 LoadContacts(&loaded_contacts, &loaded_metadata); |
259 EXPECT_EQ(VarContactsToString(1, contact.get()), | 300 EXPECT_EQ(VarContactsToString(1, contact.get()), |
260 ContactsToString(*loaded_contacts)); | 301 ContactsToString(*loaded_contacts)); |
261 } | 302 } |
262 | 303 |
263 } // namespace test | 304 } // namespace test |
264 } // namespace contacts | 305 } // namespace contacts |
OLD | NEW |