Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(666)

Side by Side Diff: chrome/browser/chromeos/contacts/contact_database_unittest.cc

Issue 10832064: contacts: Add contacts::ContactDatabase. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: one parameter per line Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 db_(NULL) {
38 }
39
40 virtual ~ContactDatabaseTest() {
41 }
42
43 protected:
44 // testing::Test implementation.
45 virtual void SetUp() OVERRIDE {
46 CHECK(temp_dir_.CreateUniqueTempDir());
47 CreateDatabase();
48 }
49
50 virtual void TearDown() OVERRIDE {
51 DestroyDatabase();
52 }
53
54 protected:
55 FilePath database_path() const {
56 return temp_dir_.path().Append(kDatabaseDirectoryName);
57 }
58
59 void CreateDatabase() {
60 DestroyDatabase();
61 db_ = new ContactDatabase;
62 db_->Init(database_path(),
63 base::Bind(&ContactDatabaseTest::OnDatabaseInitialized,
64 base::Unretained(this)));
65
66 // The database will be initialized on the file thread; run the message loop
67 // until that happens.
68 message_loop_.Run();
69 }
70
71 void DestroyDatabase() {
72 if (db_) {
73 db_->DestroyOnUIThread();
74 db_ = NULL;
75 }
76 }
77
78 // Calls ContactDatabase::SaveContacts() and blocks until the operation is
79 // complete.
80 void SaveContacts(scoped_ptr<ContactPointers> contacts, bool is_full_update) {
81 CHECK(db_);
82 db_->SaveContacts(contacts.Pass(), is_full_update,
83 base::Bind(&ContactDatabaseTest::OnContactsSaved,
84 base::Unretained(this)));
85 message_loop_.Run();
86 }
87
88 // Calls ContactDatabase::LoadContacts() and blocks until the operation is
89 // complete.
90 scoped_ptr<ScopedVector<Contact> > LoadContacts() {
91 CHECK(db_);
92 db_->LoadContacts(base::Bind(&ContactDatabaseTest::OnContactsLoaded,
93 base::Unretained(this)));
94 message_loop_.Run();
95 return loaded_contacts_.Pass();
96 }
97
98 private:
99 void OnDatabaseInitialized(bool success) {
100 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
101 CHECK(success);
102 // TODO(derat): Move gdata::test::RunBlockingPoolTask() to a shared location
103 // and use it for these tests.
104 message_loop_.Quit();
105 }
106
107 void OnContactsSaved(bool success) {
108 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
109 CHECK(success);
110 message_loop_.Quit();
111 }
112
113 void OnContactsLoaded(bool success,
114 scoped_ptr<ScopedVector<Contact> > contacts) {
115 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
116 CHECK(success);
117 loaded_contacts_.swap(contacts);
118 message_loop_.Quit();
119 }
120
121 MessageLoopForUI message_loop_;
122 content::TestBrowserThread ui_thread_;
123
124 // Temporary directory where the database is saved.
125 ScopedTempDir temp_dir_;
126
127 // This class retains ownership of this object.
128 ContactDatabase* db_;
129
130 // Contacts returned by the most-recent ContactDatabase::LoadContacts() call.
131 // Used to pass contacts from OnContactsLoaded() to LoadContacts().
132 scoped_ptr<ScopedVector<Contact> > loaded_contacts_;
133
134 DISALLOW_COPY_AND_ASSIGN(ContactDatabaseTest);
135 };
136
137 TEST_F(ContactDatabaseTest, SaveAndReload) {
138 // Save a contact to the database and check that we get the same data back
139 // when loading it.
140 const std::string kProviderId = "provider_id_1";
141 scoped_ptr<Contact> contact(new Contact);
142 InitContact(kProviderId, "1", false, contact.get());
143 AddEmailAddress("email_1", Contact_AddressType_Relation_HOME,
144 "email_label_1", true, contact.get());
145 AddEmailAddress("email_2", Contact_AddressType_Relation_WORK,
146 "", false, contact.get());
147 AddPhoneNumber("123-456-7890", Contact_AddressType_Relation_HOME,
148 "phone_label", true, contact.get());
149 AddPostalAddress("postal_1", Contact_AddressType_Relation_HOME,
150 "postal_label_1", true, contact.get());
151 AddPostalAddress("postal_2", Contact_AddressType_Relation_OTHER,
152 "postal_label_2", false, contact.get());
153 AddInstantMessagingAddress("im_1",
154 Contact_InstantMessagingAddress_Protocol_AIM,
155 Contact_AddressType_Relation_HOME,
156 "im_label_1", true, contact.get());
157 SetPhoto(gfx::Size(20, 20), contact.get());
158 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers);
159 contacts_to_save->push_back(contact.get());
160 SaveContacts(contacts_to_save.Pass(), true);
161
162 scoped_ptr<ScopedVector<Contact> > loaded_contacts = LoadContacts();
163 EXPECT_EQ(VarContactsToString(1, contact.get()),
164 ContactsToString(*loaded_contacts));
165
166 // Modify the contact, save it, and check that the loaded contact is also
167 // updated.
168 InitContact(kProviderId, "2", false, contact.get());
169 AddEmailAddress("email_3", Contact_AddressType_Relation_OTHER,
170 "email_label_2", true, contact.get());
171 AddPhoneNumber("phone_2", Contact_AddressType_Relation_OTHER,
172 "phone_label_2", false, contact.get());
173 AddPostalAddress("postal_3", Contact_AddressType_Relation_HOME,
174 "postal_label_3", true, contact.get());
175 SetPhoto(gfx::Size(64, 64), contact.get());
176 contacts_to_save.reset(new ContactPointers);
177 contacts_to_save->push_back(contact.get());
178 SaveContacts(contacts_to_save.Pass(), true);
179
180 loaded_contacts = LoadContacts();
181 EXPECT_EQ(VarContactsToString(1, contact.get()),
182 ContactsToString(*loaded_contacts));
183 }
184
185 TEST_F(ContactDatabaseTest, FullAndPartialUpdates) {
186 // Do a full update that inserts two contacts into the database.
187 const std::string kProviderId1 = "provider_id_1";
188 const std::string kSharedEmail = "foo@example.org";
189 scoped_ptr<Contact> contact1(new Contact);
190 InitContact(kProviderId1, "1", false, contact1.get());
191 AddEmailAddress(kSharedEmail, Contact_AddressType_Relation_HOME,
192 "", true, contact1.get());
193
194 const std::string kProviderId2 = "provider_id_2";
195 scoped_ptr<Contact> contact2(new Contact);
196 InitContact(kProviderId2, "2", false, contact2.get());
197 AddEmailAddress(kSharedEmail, Contact_AddressType_Relation_WORK,
198 "", true, contact2.get());
199
200 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers);
201 contacts_to_save->push_back(contact1.get());
202 contacts_to_save->push_back(contact2.get());
203 SaveContacts(contacts_to_save.Pass(), true);
204 scoped_ptr<ScopedVector<Contact> > loaded_contacts = LoadContacts();
205 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()),
206 ContactsToString(*loaded_contacts));
207
208 // Do a partial update including just the second contact.
209 InitContact(kProviderId2, "2b", false, contact2.get());
210 AddPostalAddress("postal_1", Contact_AddressType_Relation_HOME,
211 "", true, contact2.get());
212 contacts_to_save.reset(new ContactPointers);
213 contacts_to_save->push_back(contact2.get());
214 SaveContacts(contacts_to_save.Pass(), false);
215 loaded_contacts = LoadContacts();
216 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()),
217 ContactsToString(*loaded_contacts));
218
219 // Do a full update including just the first contact. The second contact
220 // should be removed from the database.
221 InitContact(kProviderId1, "1b", false, contact1.get());
222 AddPostalAddress("postal_2", Contact_AddressType_Relation_WORK,
223 "", true, contact1.get());
224 AddPhoneNumber("phone", Contact_AddressType_Relation_HOME,
225 "", true, contact1.get());
226 contacts_to_save.reset(new ContactPointers);
227 contacts_to_save->push_back(contact1.get());
228 SaveContacts(contacts_to_save.Pass(), true);
229 loaded_contacts = LoadContacts();
230 EXPECT_EQ(VarContactsToString(1, contact1.get()),
231 ContactsToString(*loaded_contacts));
232
233 // Do a full update including no contacts. The database should be cleared.
234 contacts_to_save.reset(new ContactPointers);
235 SaveContacts(contacts_to_save.Pass(), true);
236 loaded_contacts = LoadContacts();
237 EXPECT_TRUE(loaded_contacts->empty());
238 }
239
240 // Test that we create a new database when we encounter a corrupted one.
241 TEST_F(ContactDatabaseTest, DeleteWhenCorrupt) {
242 DestroyDatabase();
243 // Overwrite all of the files in the database with a space character.
244 file_util::FileEnumerator enumerator(
245 database_path(), false, file_util::FileEnumerator::FILES);
246 for (FilePath path = enumerator.Next(); !path.empty();
247 path = enumerator.Next()) {
248 file_util::WriteFile(path, " ", 1);
249 }
250 CreateDatabase();
251
252 // Make sure that the resulting database is usable.
253 scoped_ptr<Contact> contact(new Contact);
254 InitContact("1", "1", false, contact.get());
255 scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers);
256 contacts_to_save->push_back(contact.get());
257 SaveContacts(contacts_to_save.Pass(), true);
258 scoped_ptr<ScopedVector<Contact> > loaded_contacts = LoadContacts();
259 EXPECT_EQ(VarContactsToString(1, contact.get()),
260 ContactsToString(*loaded_contacts));
261 }
262
263 } // namespace test
264 } // namespace contacts
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/contacts/contact_database.cc ('k') | chrome/browser/chromeos/contacts/contact_test_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698