Chromium Code Reviews| 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/contact_database.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/file_path.h" | |
| 11 #include "base/file_util.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/scoped_vector.h" | |
| 14 #include "base/message_loop.h" | |
| 15 #include "base/scoped_temp_dir.h" | |
| 16 #include "chrome/browser/chromeos/contacts/contact.pb.h" | |
| 17 #include "chrome/browser/chromeos/contacts/contact_test_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 #include "ui/gfx/size.h" | |
| 22 | |
| 23 using content::BrowserThread; | |
| 24 | |
| 25 namespace contacts { | |
| 26 namespace test { | |
| 27 | |
| 28 // Name of the directory created within a temporary directory to store the | |
| 29 // contacts database. | |
| 30 const FilePath::CharType kDatabaseDirectoryName[] = | |
| 31 FILE_PATH_LITERAL("contacts"); | |
| 32 | |
| 33 class ContactDatabaseTest : public testing::Test { | |
| 34 public: | |
| 35 ContactDatabaseTest() | |
| 36 : ui_thread_(BrowserThread::UI, &message_loop_), | |
| 37 file_thread_(BrowserThread::FILE, &message_loop_) { | |
| 38 } | |
| 39 | |
| 40 virtual ~ContactDatabaseTest() { | |
| 41 } | |
| 42 | |
| 43 protected: | |
| 44 // testing::Test implementation. | |
| 45 virtual void SetUp() OVERRIDE { | |
| 46 testing::Test::SetUp(); | |
| 47 CHECK(temp_dir_.CreateUniqueTempDir()); | |
| 48 CreateDatabase(); | |
| 49 } | |
| 50 | |
| 51 protected: | |
| 52 FilePath database_path() const { | |
| 53 return temp_dir_.path().Append(kDatabaseDirectoryName); | |
| 54 } | |
| 55 | |
| 56 void clear_db() { db_.reset(NULL); } | |
| 57 | |
| 58 void CreateDatabase() { | |
| 59 db_.reset(new ContactDatabase); | |
| 60 db_->Init(database_path(), | |
| 61 base::Bind(&ContactDatabaseTest::OnDatabaseInitialized, | |
| 62 base::Unretained(this))); | |
| 63 | |
| 64 // The database will be initialized on the file thread; run the message loop | |
| 65 // until that happens. | |
| 66 message_loop_.Run(); | |
| 67 } | |
| 68 | |
| 69 // Calls ContactDatabase::SaveContacts() and blocks until the operation is | |
| 70 // complete. | |
| 71 void SaveContacts(scoped_ptr<ContactPointers> contacts, bool is_full_update) { | |
| 72 CHECK(db_.get()); | |
| 73 db_->SaveContacts(contacts.Pass(), is_full_update, | |
| 74 base::Bind(&ContactDatabaseTest::OnContactsSaved, | |
| 75 base::Unretained(this))); | |
| 76 message_loop_.Run(); | |
| 77 } | |
| 78 | |
| 79 // Calls ContactDatabase::LoadContacts() and blocks until the operation is | |
| 80 // complete. | |
| 81 scoped_ptr<ScopedVector<Contact> > LoadContacts() { | |
| 82 CHECK(db_.get()); | |
| 83 db_->LoadContacts(base::Bind(&ContactDatabaseTest::OnContactsLoaded, | |
| 84 base::Unretained(this))); | |
| 85 message_loop_.Run(); | |
| 86 return loaded_contacts_.Pass(); | |
| 87 } | |
| 88 | |
| 89 private: | |
| 90 void OnDatabaseInitialized(bool success) { | |
| 91 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 92 CHECK(success); | |
| 93 message_loop_.Quit(); | |
|
satorux1
2012/07/30 17:15:07
Just FYI. In gdata_cache_unittest.cc, gdata::test:
Daniel Erat
2012/07/30 22:12:09
Do you think it's worthwhile to move this to conte
satorux1
2012/07/31 07:26:30
That's a good question. Not sure if it's worthwhil
Daniel Erat
2012/07/31 16:44:12
Does something like chrome/browser/chromeos/test_u
| |
| 94 } | |
| 95 | |
| 96 void OnContactsSaved(bool success) { | |
| 97 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 98 CHECK(success); | |
| 99 message_loop_.Quit(); | |
| 100 } | |
| 101 | |
| 102 void OnContactsLoaded(bool success, | |
| 103 scoped_ptr<ScopedVector<Contact> > contacts) { | |
| 104 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 105 CHECK(success); | |
| 106 loaded_contacts_.swap(contacts); | |
| 107 message_loop_.Quit(); | |
| 108 } | |
| 109 | |
| 110 MessageLoopForUI message_loop_; | |
| 111 content::TestBrowserThread ui_thread_; | |
| 112 content::TestBrowserThread file_thread_; | |
| 113 | |
| 114 // Temporary directory where the database is saved. | |
| 115 ScopedTempDir temp_dir_; | |
| 116 | |
| 117 scoped_ptr<ContactDatabase> db_; | |
| 118 | |
| 119 // Contacts returned by the most-recent ContactDatabase::LoadContacts() call. | |
| 120 // Used to pass contacts from OnContactsLoaded() to LoadContacts(). | |
| 121 scoped_ptr<ScopedVector<Contact> > loaded_contacts_; | |
| 122 | |
| 123 DISALLOW_COPY_AND_ASSIGN(ContactDatabaseTest); | |
| 124 }; | |
| 125 | |
| 126 TEST_F(ContactDatabaseTest, SaveAndReload) { | |
| 127 // Save a contact to the database and check that we get the same data back | |
| 128 // when loading it. | |
| 129 const std::string kProviderId = "provider_id_1"; | |
| 130 scoped_ptr<Contact> contact(new Contact); | |
| 131 InitContact(kProviderId, "1", false, contact.get()); | |
| 132 AddEmailAddress("email_1", Contact_AddressType_Relation_HOME, | |
| 133 "email_label_1", true, contact.get()); | |
| 134 AddEmailAddress("email_2", Contact_AddressType_Relation_WORK, | |
| 135 "", false, contact.get()); | |
| 136 AddPhoneNumber("123-456-7890", Contact_AddressType_Relation_HOME, | |
| 137 "phone_label", true, contact.get()); | |
| 138 AddPostalAddress("postal_1", Contact_AddressType_Relation_HOME, | |
| 139 "postal_label_1", true, contact.get()); | |
| 140 AddPostalAddress("postal_2", Contact_AddressType_Relation_OTHER, | |
| 141 "postal_label_2", false, contact.get()); | |
| 142 AddInstantMessagingAddress("im_1", | |
| 143 Contact_InstantMessagingAddress_Protocol_AIM, | |
| 144 Contact_AddressType_Relation_HOME, | |
| 145 "im_label_1", true, contact.get()); | |
| 146 SetPhoto(gfx::Size(20, 20), contact.get()); | |
| 147 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); | |
| 148 contacts_to_save->push_back(contact.get()); | |
| 149 SaveContacts(contacts_to_save.Pass(), true); | |
| 150 | |
| 151 scoped_ptr<ScopedVector<Contact> > loaded_contacts = LoadContacts(); | |
| 152 EXPECT_EQ(VarContactsToString(1, contact.get()), | |
| 153 ContactsToString(*loaded_contacts)); | |
| 154 | |
| 155 // Modify the contact, save it, and check that the loaded contact is also | |
| 156 // updated. | |
| 157 InitContact(kProviderId, "2", false, contact.get()); | |
| 158 AddEmailAddress("email_3", Contact_AddressType_Relation_OTHER, | |
| 159 "email_label_2", true, contact.get()); | |
| 160 AddPhoneNumber("phone_2", Contact_AddressType_Relation_OTHER, | |
| 161 "phone_label_2", false, contact.get()); | |
| 162 AddPostalAddress("postal_3", Contact_AddressType_Relation_HOME, | |
| 163 "postal_label_3", true, contact.get()); | |
| 164 SetPhoto(gfx::Size(64, 64), contact.get()); | |
| 165 contacts_to_save.reset(new ContactPointers); | |
| 166 contacts_to_save->push_back(contact.get()); | |
| 167 SaveContacts(contacts_to_save.Pass(), true); | |
| 168 | |
| 169 loaded_contacts = LoadContacts(); | |
| 170 EXPECT_EQ(VarContactsToString(1, contact.get()), | |
| 171 ContactsToString(*loaded_contacts)); | |
| 172 } | |
| 173 | |
| 174 TEST_F(ContactDatabaseTest, FullAndPartialUpdates) { | |
| 175 // Do a full update that inserts two contacts into the database. | |
| 176 const std::string kProviderId1 = "provider_id_1"; | |
| 177 const std::string kSharedEmail = "foo@example.org"; | |
| 178 scoped_ptr<Contact> contact1(new Contact); | |
| 179 InitContact(kProviderId1, "1", false, contact1.get()); | |
| 180 AddEmailAddress(kSharedEmail, Contact_AddressType_Relation_HOME, | |
| 181 "", true, contact1.get()); | |
| 182 | |
| 183 const std::string kProviderId2 = "provider_id_2"; | |
| 184 scoped_ptr<Contact> contact2(new Contact); | |
| 185 InitContact(kProviderId2, "2", false, contact2.get()); | |
| 186 AddEmailAddress(kSharedEmail, Contact_AddressType_Relation_WORK, | |
| 187 "", true, contact2.get()); | |
| 188 | |
| 189 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); | |
| 190 contacts_to_save->push_back(contact1.get()); | |
| 191 contacts_to_save->push_back(contact2.get()); | |
| 192 SaveContacts(contacts_to_save.Pass(), true); | |
| 193 scoped_ptr<ScopedVector<Contact> > loaded_contacts = LoadContacts(); | |
| 194 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), | |
| 195 ContactsToString(*loaded_contacts)); | |
| 196 | |
| 197 // Do a partial update including just the second contact. | |
| 198 InitContact(kProviderId2, "2b", false, contact2.get()); | |
| 199 AddPostalAddress("postal_1", Contact_AddressType_Relation_HOME, | |
| 200 "", true, contact2.get()); | |
| 201 contacts_to_save.reset(new ContactPointers); | |
| 202 contacts_to_save->push_back(contact2.get()); | |
| 203 SaveContacts(contacts_to_save.Pass(), false); | |
| 204 loaded_contacts = LoadContacts(); | |
| 205 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), | |
| 206 ContactsToString(*loaded_contacts)); | |
| 207 | |
| 208 // Do a full update including just the first contact. The second contact | |
| 209 // should be removed from the database. | |
| 210 InitContact(kProviderId1, "1b", false, contact1.get()); | |
| 211 AddPostalAddress("postal_2", Contact_AddressType_Relation_WORK, | |
| 212 "", true, contact1.get()); | |
| 213 AddPhoneNumber("phone", Contact_AddressType_Relation_HOME, | |
| 214 "", true, contact1.get()); | |
| 215 contacts_to_save.reset(new ContactPointers); | |
| 216 contacts_to_save->push_back(contact1.get()); | |
| 217 SaveContacts(contacts_to_save.Pass(), true); | |
| 218 loaded_contacts = LoadContacts(); | |
| 219 EXPECT_EQ(VarContactsToString(1, contact1.get()), | |
| 220 ContactsToString(*loaded_contacts)); | |
| 221 | |
| 222 // Do a full update including no contacts. The database should be cleared. | |
| 223 contacts_to_save.reset(new ContactPointers); | |
| 224 SaveContacts(contacts_to_save.Pass(), true); | |
| 225 loaded_contacts = LoadContacts(); | |
| 226 EXPECT_TRUE(loaded_contacts->empty()); | |
| 227 } | |
| 228 | |
| 229 // Test that we create a new database when we encounter a corrupted one. | |
| 230 TEST_F(ContactDatabaseTest, DeleteWhenCorrupt) { | |
| 231 clear_db(); | |
| 232 // Overwrite all of the files in the database with a space character. | |
| 233 file_util::FileEnumerator enumerator( | |
| 234 database_path(), false, file_util::FileEnumerator::FILES); | |
| 235 for (FilePath path = enumerator.Next(); !path.empty(); | |
| 236 path = enumerator.Next()) { | |
| 237 file_util::WriteFile(path, " ", 1); | |
| 238 } | |
| 239 CreateDatabase(); | |
| 240 | |
| 241 // Make sure that the resulting database is usable. | |
| 242 scoped_ptr<Contact> contact(new Contact); | |
| 243 InitContact("1", "1", false, contact.get()); | |
| 244 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); | |
| 245 contacts_to_save->push_back(contact.get()); | |
| 246 SaveContacts(contacts_to_save.Pass(), true); | |
| 247 scoped_ptr<ScopedVector<Contact> > loaded_contacts = LoadContacts(); | |
| 248 EXPECT_EQ(VarContactsToString(1, contact.get()), | |
| 249 ContactsToString(*loaded_contacts)); | |
| 250 } | |
| 251 | |
| 252 } // namespace test | |
| 253 } // namespace contacts | |
| OLD | NEW |