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 <cstdarg> |
| 8 #include <map> |
| 9 #include <string> |
| 10 #include <utility> |
| 11 |
| 12 #include "base/bind.h" |
| 13 #include "base/file_path.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/scoped_vector.h" |
| 16 #include "base/message_loop.h" |
| 17 #include "base/scoped_temp_dir.h" |
| 18 #include "chrome/browser/chromeos/contacts/contact.h" |
| 19 #include "chrome/browser/chromeos/contacts/contact_test_lib.h" |
| 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/test/test_browser_thread.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "third_party/skia/include/core/SkBitmap.h" |
| 24 #include "ui/gfx/size.h" |
| 25 |
| 26 using content::BrowserThread; |
| 27 |
| 28 namespace contacts { |
| 29 namespace test { |
| 30 |
| 31 const FilePath::CharType kDatabaseFileName[] = FILE_PATH_LITERAL("contacts.db"); |
| 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 db_(new ContactDatabase), |
| 39 save_was_successful_(false) { |
| 40 } |
| 41 |
| 42 virtual ~ContactDatabaseTest() { |
| 43 } |
| 44 |
| 45 protected: |
| 46 // testing::Test implementation. |
| 47 virtual void SetUp() OVERRIDE { |
| 48 testing::Test::SetUp(); |
| 49 CHECK(temp_dir_.CreateUniqueTempDir()); |
| 50 db_->Init(temp_dir_.path().Append(kDatabaseFileName), |
| 51 base::Bind(&ContactDatabaseTest::OnDatabaseInitialized, |
| 52 base::Unretained(this))); |
| 53 // The database will be initialized on the file thread; run the message loop |
| 54 // until that happens. |
| 55 message_loop_.Run(); |
| 56 } |
| 57 |
| 58 protected: |
| 59 // Calls ContactDatabase::SaveContacts() and blocks until the operation is |
| 60 // complete. |
| 61 void SaveContacts(scoped_ptr<ContactPointers> contacts, bool is_full_update) { |
| 62 db_->SaveContacts(contacts.Pass(), is_full_update, |
| 63 base::Bind(&ContactDatabaseTest::OnContactsSaved, |
| 64 base::Unretained(this))); |
| 65 message_loop_.Run(); |
| 66 } |
| 67 |
| 68 // Calls ContactDatabase::LoadContacts() and blocks until the operation is |
| 69 // complete. |
| 70 scoped_ptr<ScopedVector<Contact> > LoadContacts() { |
| 71 db_->LoadContacts(base::Bind(&ContactDatabaseTest::OnContactsLoaded, |
| 72 base::Unretained(this))); |
| 73 message_loop_.Run(); |
| 74 return loaded_contacts_.Pass(); |
| 75 } |
| 76 |
| 77 private: |
| 78 void OnDatabaseInitialized(bool success) { |
| 79 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 80 CHECK(success); |
| 81 message_loop_.Quit(); |
| 82 } |
| 83 |
| 84 void OnContactsSaved(bool success) { |
| 85 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 86 CHECK(success); |
| 87 message_loop_.Quit(); |
| 88 } |
| 89 |
| 90 void OnContactsLoaded(bool success, |
| 91 scoped_ptr<ScopedVector<Contact> > contacts) { |
| 92 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 93 CHECK(success); |
| 94 loaded_contacts_.swap(contacts); |
| 95 message_loop_.Quit(); |
| 96 } |
| 97 |
| 98 MessageLoopForUI message_loop_; |
| 99 content::TestBrowserThread ui_thread_; |
| 100 content::TestBrowserThread file_thread_; |
| 101 |
| 102 ScopedTempDir temp_dir_; |
| 103 |
| 104 scoped_ptr<ContactDatabase> db_; |
| 105 |
| 106 // Did the last ContactDatabase::SaveContacts() call return success? |
| 107 bool save_was_successful_; |
| 108 |
| 109 // Contacts returned by the most-recent ContactDatabase::LoadContacts() call. |
| 110 // Used to pass contacts from OnContactsLoaded to LoadContacts(). |
| 111 scoped_ptr<ScopedVector<Contact> > loaded_contacts_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(ContactDatabaseTest); |
| 114 }; |
| 115 |
| 116 TEST_F(ContactDatabaseTest, SaveAndReload) { |
| 117 // Save a contact to the database and check that we get the same data back |
| 118 // when loading it. |
| 119 const std::string kProviderId = "provider_id_1"; |
| 120 scoped_ptr<Contact> contact(new Contact); |
| 121 InitContact(kProviderId, "1", false, contact.get()); |
| 122 AddEmailAddress("email_1", Contact::AddressType::RELATION_HOME, |
| 123 "email_label_1", true, contact.get()); |
| 124 AddEmailAddress("email_2", Contact::AddressType::RELATION_WORK, |
| 125 "", false, contact.get()); |
| 126 AddPhoneNumber("123-456-7890", Contact::AddressType::RELATION_HOME, |
| 127 "phone_label", true, contact.get()); |
| 128 AddPostalAddress("postal_1", Contact::AddressType::RELATION_HOME, |
| 129 "postal_label_1", true, contact.get()); |
| 130 AddPostalAddress("postal_2", Contact::AddressType::RELATION_OTHER, |
| 131 "postal_label_2", false, contact.get()); |
| 132 AddInstantMessagingAddress("im_1", |
| 133 Contact::InstantMessagingAddress::PROTOCOL_AIM, |
| 134 Contact::AddressType::RELATION_HOME, |
| 135 "im_label_1", true, contact.get()); |
| 136 SetPhoto(gfx::Size(20, 20), contact.get()); |
| 137 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); |
| 138 contacts_to_save->push_back(contact.get()); |
| 139 SaveContacts(contacts_to_save.Pass(), true); |
| 140 |
| 141 scoped_ptr<ScopedVector<contacts::Contact> > loaded_contacts = LoadContacts(); |
| 142 EXPECT_EQ(VarContactsToString(1, contact.get()), |
| 143 ContactsToString(*loaded_contacts)); |
| 144 |
| 145 // Modify the contact, save it, and check that the loaded contact is also |
| 146 // updated. |
| 147 InitContact(kProviderId, "2", false, contact.get()); |
| 148 AddEmailAddress("email_3", Contact::AddressType::RELATION_OTHER, |
| 149 "email_label_2", true, contact.get()); |
| 150 AddPhoneNumber("phone_2", Contact::AddressType::RELATION_OTHER, |
| 151 "phone_label_2", false, contact.get()); |
| 152 AddPostalAddress("postal_3", Contact::AddressType::RELATION_HOME, |
| 153 "postal_label_3", true, contact.get()); |
| 154 SetPhoto(gfx::Size(64, 64), contact.get()); |
| 155 contacts_to_save.reset(new ContactPointers); |
| 156 contacts_to_save->push_back(contact.get()); |
| 157 SaveContacts(contacts_to_save.Pass(), true); |
| 158 |
| 159 loaded_contacts = LoadContacts(); |
| 160 EXPECT_EQ(VarContactsToString(1, contact.get()), |
| 161 ContactsToString(*loaded_contacts)); |
| 162 } |
| 163 |
| 164 TEST_F(ContactDatabaseTest, FullAndPartialUpdates) { |
| 165 // Do a full update that inserts two contacts into the database. |
| 166 const std::string kProviderId1 = "provider_id_1"; |
| 167 const std::string kSharedEmail = "foo@example.org"; |
| 168 scoped_ptr<Contact> contact1(new Contact); |
| 169 InitContact(kProviderId1, "1", false, contact1.get()); |
| 170 AddEmailAddress(kSharedEmail, Contact::AddressType::RELATION_HOME, |
| 171 "", true, contact1.get()); |
| 172 |
| 173 const std::string kProviderId2 = "provider_id_2"; |
| 174 scoped_ptr<Contact> contact2(new Contact); |
| 175 InitContact(kProviderId2, "2", false, contact2.get()); |
| 176 AddEmailAddress(kSharedEmail, Contact::AddressType::RELATION_WORK, |
| 177 "", true, contact2.get()); |
| 178 |
| 179 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); |
| 180 contacts_to_save->push_back(contact1.get()); |
| 181 contacts_to_save->push_back(contact2.get()); |
| 182 SaveContacts(contacts_to_save.Pass(), true); |
| 183 scoped_ptr<ScopedVector<contacts::Contact> > loaded_contacts = LoadContacts(); |
| 184 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), |
| 185 ContactsToString(*loaded_contacts)); |
| 186 |
| 187 // Do a partial update including just the second contact. |
| 188 InitContact(kProviderId2, "2b", false, contact2.get()); |
| 189 AddPostalAddress("postal_1", Contact::AddressType::RELATION_HOME, |
| 190 "", true, contact2.get()); |
| 191 contacts_to_save.reset(new ContactPointers); |
| 192 contacts_to_save->push_back(contact2.get()); |
| 193 SaveContacts(contacts_to_save.Pass(), false); |
| 194 loaded_contacts = LoadContacts(); |
| 195 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), |
| 196 ContactsToString(*loaded_contacts)); |
| 197 |
| 198 // Do a full update including just the first contact. The second contact |
| 199 // should be removed from the database. |
| 200 InitContact(kProviderId1, "1b", false, contact1.get()); |
| 201 AddPostalAddress("postal_2", Contact::AddressType::RELATION_WORK, |
| 202 "", true, contact1.get()); |
| 203 AddPhoneNumber("phone", Contact::AddressType::RELATION_HOME, |
| 204 "", true, contact1.get()); |
| 205 contacts_to_save.reset(new ContactPointers); |
| 206 contacts_to_save->push_back(contact1.get()); |
| 207 SaveContacts(contacts_to_save.Pass(), true); |
| 208 loaded_contacts = LoadContacts(); |
| 209 EXPECT_EQ(VarContactsToString(1, contact1.get()), |
| 210 ContactsToString(*loaded_contacts)); |
| 211 |
| 212 // Do a full update including no contacts. The database should be cleared. |
| 213 contacts_to_save.reset(new ContactPointers); |
| 214 SaveContacts(contacts_to_save.Pass(), true); |
| 215 loaded_contacts = LoadContacts(); |
| 216 EXPECT_TRUE(loaded_contacts->empty()); |
| 217 } |
| 218 |
| 219 } // namespace test |
| 220 } // namespace contacts |
OLD | NEW |