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 db_(NULL) { | |
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 virtual void TearDown() OVERRIDE { | |
52 DestroyDatabase(); | |
53 testing::Test::TearDown(); | |
satorux1
2012/07/31 07:26:30
what does it do? maybe shutdown the blocking pool?
Daniel Erat
2012/07/31 16:44:12
I'll remove these; testing::Test appears to have e
| |
54 } | |
55 | |
56 protected: | |
57 FilePath database_path() const { | |
58 return temp_dir_.path().Append(kDatabaseDirectoryName); | |
59 } | |
60 | |
61 void CreateDatabase() { | |
62 DestroyDatabase(); | |
63 db_ = new ContactDatabase; | |
64 db_->Init(database_path(), | |
65 base::Bind(&ContactDatabaseTest::OnDatabaseInitialized, | |
66 base::Unretained(this))); | |
67 | |
68 // The database will be initialized on the file thread; run the message loop | |
69 // until that happens. | |
70 message_loop_.Run(); | |
71 } | |
72 | |
73 void DestroyDatabase() { | |
74 if (db_) { | |
75 db_->DestroyOnUIThread(); | |
76 db_ = NULL; | |
77 } | |
78 } | |
79 | |
80 // Calls ContactDatabase::SaveContacts() and blocks until the operation is | |
81 // complete. | |
82 void SaveContacts(scoped_ptr<ContactPointers> contacts, bool is_full_update) { | |
83 CHECK(db_); | |
84 db_->SaveContacts(contacts.Pass(), is_full_update, | |
85 base::Bind(&ContactDatabaseTest::OnContactsSaved, | |
86 base::Unretained(this))); | |
87 message_loop_.Run(); | |
88 } | |
89 | |
90 // Calls ContactDatabase::LoadContacts() and blocks until the operation is | |
91 // complete. | |
92 scoped_ptr<ScopedVector<Contact> > LoadContacts() { | |
93 CHECK(db_); | |
94 db_->LoadContacts(base::Bind(&ContactDatabaseTest::OnContactsLoaded, | |
95 base::Unretained(this))); | |
96 message_loop_.Run(); | |
97 return loaded_contacts_.Pass(); | |
98 } | |
99 | |
100 private: | |
101 void OnDatabaseInitialized(bool success) { | |
102 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
103 CHECK(success); | |
104 message_loop_.Quit(); | |
Daniel Erat
2012/07/31 19:34:42
Asked in an earlier patch set re moving RunBlockin
satorux1
2012/07/31 19:56:57
Forgot to reply. TODO() sounds good for now.
| |
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 | |
OLD | NEW |