Chromium Code Reviews| Index: chrome/browser/chromeos/contacts/contact_database_unittest.cc |
| diff --git a/chrome/browser/chromeos/contacts/contact_database_unittest.cc b/chrome/browser/chromeos/contacts/contact_database_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4baef79d13637f5323f78dba233088afe7c0a967 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/contacts/contact_database_unittest.cc |
| @@ -0,0 +1,253 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/contacts/contact_database.h" |
| + |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/file_path.h" |
| +#include "base/file_util.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/message_loop.h" |
| +#include "base/scoped_temp_dir.h" |
| +#include "chrome/browser/chromeos/contacts/contact.pb.h" |
| +#include "chrome/browser/chromeos/contacts/contact_test_util.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/gfx/size.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace contacts { |
| +namespace test { |
| + |
| +// Name of the directory created within a temporary directory to store the |
| +// contacts database. |
| +const FilePath::CharType kDatabaseDirectoryName[] = |
| + FILE_PATH_LITERAL("contacts"); |
| + |
| +class ContactDatabaseTest : public testing::Test { |
| + public: |
| + ContactDatabaseTest() |
| + : ui_thread_(BrowserThread::UI, &message_loop_), |
| + file_thread_(BrowserThread::FILE, &message_loop_) { |
| + } |
| + |
| + virtual ~ContactDatabaseTest() { |
| + } |
| + |
| + protected: |
| + // testing::Test implementation. |
| + virtual void SetUp() OVERRIDE { |
| + testing::Test::SetUp(); |
| + CHECK(temp_dir_.CreateUniqueTempDir()); |
| + CreateDatabase(); |
| + } |
| + |
| + protected: |
| + FilePath database_path() const { |
| + return temp_dir_.path().Append(kDatabaseDirectoryName); |
| + } |
| + |
| + void clear_db() { db_.reset(NULL); } |
| + |
| + void CreateDatabase() { |
| + db_.reset(new ContactDatabase); |
| + db_->Init(database_path(), |
| + base::Bind(&ContactDatabaseTest::OnDatabaseInitialized, |
| + base::Unretained(this))); |
| + |
| + // The database will be initialized on the file thread; run the message loop |
| + // until that happens. |
| + message_loop_.Run(); |
| + } |
| + |
| + // Calls ContactDatabase::SaveContacts() and blocks until the operation is |
| + // complete. |
| + void SaveContacts(scoped_ptr<ContactPointers> contacts, bool is_full_update) { |
| + CHECK(db_.get()); |
| + db_->SaveContacts(contacts.Pass(), is_full_update, |
| + base::Bind(&ContactDatabaseTest::OnContactsSaved, |
| + base::Unretained(this))); |
| + message_loop_.Run(); |
| + } |
| + |
| + // Calls ContactDatabase::LoadContacts() and blocks until the operation is |
| + // complete. |
| + scoped_ptr<ScopedVector<Contact> > LoadContacts() { |
| + CHECK(db_.get()); |
| + db_->LoadContacts(base::Bind(&ContactDatabaseTest::OnContactsLoaded, |
| + base::Unretained(this))); |
| + message_loop_.Run(); |
| + return loaded_contacts_.Pass(); |
| + } |
| + |
| + private: |
| + void OnDatabaseInitialized(bool success) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + CHECK(success); |
| + 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
|
| + } |
| + |
| + void OnContactsSaved(bool success) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + CHECK(success); |
| + message_loop_.Quit(); |
| + } |
| + |
| + void OnContactsLoaded(bool success, |
| + scoped_ptr<ScopedVector<Contact> > contacts) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + CHECK(success); |
| + loaded_contacts_.swap(contacts); |
| + message_loop_.Quit(); |
| + } |
| + |
| + MessageLoopForUI message_loop_; |
| + content::TestBrowserThread ui_thread_; |
| + content::TestBrowserThread file_thread_; |
| + |
| + // Temporary directory where the database is saved. |
| + ScopedTempDir temp_dir_; |
| + |
| + scoped_ptr<ContactDatabase> db_; |
| + |
| + // Contacts returned by the most-recent ContactDatabase::LoadContacts() call. |
| + // Used to pass contacts from OnContactsLoaded() to LoadContacts(). |
| + scoped_ptr<ScopedVector<Contact> > loaded_contacts_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ContactDatabaseTest); |
| +}; |
| + |
| +TEST_F(ContactDatabaseTest, SaveAndReload) { |
| + // Save a contact to the database and check that we get the same data back |
| + // when loading it. |
| + const std::string kProviderId = "provider_id_1"; |
| + scoped_ptr<Contact> contact(new Contact); |
| + InitContact(kProviderId, "1", false, contact.get()); |
| + AddEmailAddress("email_1", Contact_AddressType_Relation_HOME, |
| + "email_label_1", true, contact.get()); |
| + AddEmailAddress("email_2", Contact_AddressType_Relation_WORK, |
| + "", false, contact.get()); |
| + AddPhoneNumber("123-456-7890", Contact_AddressType_Relation_HOME, |
| + "phone_label", true, contact.get()); |
| + AddPostalAddress("postal_1", Contact_AddressType_Relation_HOME, |
| + "postal_label_1", true, contact.get()); |
| + AddPostalAddress("postal_2", Contact_AddressType_Relation_OTHER, |
| + "postal_label_2", false, contact.get()); |
| + AddInstantMessagingAddress("im_1", |
| + Contact_InstantMessagingAddress_Protocol_AIM, |
| + Contact_AddressType_Relation_HOME, |
| + "im_label_1", true, contact.get()); |
| + SetPhoto(gfx::Size(20, 20), contact.get()); |
| + scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); |
| + contacts_to_save->push_back(contact.get()); |
| + SaveContacts(contacts_to_save.Pass(), true); |
| + |
| + scoped_ptr<ScopedVector<Contact> > loaded_contacts = LoadContacts(); |
| + EXPECT_EQ(VarContactsToString(1, contact.get()), |
| + ContactsToString(*loaded_contacts)); |
| + |
| + // Modify the contact, save it, and check that the loaded contact is also |
| + // updated. |
| + InitContact(kProviderId, "2", false, contact.get()); |
| + AddEmailAddress("email_3", Contact_AddressType_Relation_OTHER, |
| + "email_label_2", true, contact.get()); |
| + AddPhoneNumber("phone_2", Contact_AddressType_Relation_OTHER, |
| + "phone_label_2", false, contact.get()); |
| + AddPostalAddress("postal_3", Contact_AddressType_Relation_HOME, |
| + "postal_label_3", true, contact.get()); |
| + SetPhoto(gfx::Size(64, 64), contact.get()); |
| + contacts_to_save.reset(new ContactPointers); |
| + contacts_to_save->push_back(contact.get()); |
| + SaveContacts(contacts_to_save.Pass(), true); |
| + |
| + loaded_contacts = LoadContacts(); |
| + EXPECT_EQ(VarContactsToString(1, contact.get()), |
| + ContactsToString(*loaded_contacts)); |
| +} |
| + |
| +TEST_F(ContactDatabaseTest, FullAndPartialUpdates) { |
| + // Do a full update that inserts two contacts into the database. |
| + const std::string kProviderId1 = "provider_id_1"; |
| + const std::string kSharedEmail = "foo@example.org"; |
| + scoped_ptr<Contact> contact1(new Contact); |
| + InitContact(kProviderId1, "1", false, contact1.get()); |
| + AddEmailAddress(kSharedEmail, Contact_AddressType_Relation_HOME, |
| + "", true, contact1.get()); |
| + |
| + const std::string kProviderId2 = "provider_id_2"; |
| + scoped_ptr<Contact> contact2(new Contact); |
| + InitContact(kProviderId2, "2", false, contact2.get()); |
| + AddEmailAddress(kSharedEmail, Contact_AddressType_Relation_WORK, |
| + "", true, contact2.get()); |
| + |
| + scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); |
| + contacts_to_save->push_back(contact1.get()); |
| + contacts_to_save->push_back(contact2.get()); |
| + SaveContacts(contacts_to_save.Pass(), true); |
| + scoped_ptr<ScopedVector<Contact> > loaded_contacts = LoadContacts(); |
| + EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), |
| + ContactsToString(*loaded_contacts)); |
| + |
| + // Do a partial update including just the second contact. |
| + InitContact(kProviderId2, "2b", false, contact2.get()); |
| + AddPostalAddress("postal_1", Contact_AddressType_Relation_HOME, |
| + "", true, contact2.get()); |
| + contacts_to_save.reset(new ContactPointers); |
| + contacts_to_save->push_back(contact2.get()); |
| + SaveContacts(contacts_to_save.Pass(), false); |
| + loaded_contacts = LoadContacts(); |
| + EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), |
| + ContactsToString(*loaded_contacts)); |
| + |
| + // Do a full update including just the first contact. The second contact |
| + // should be removed from the database. |
| + InitContact(kProviderId1, "1b", false, contact1.get()); |
| + AddPostalAddress("postal_2", Contact_AddressType_Relation_WORK, |
| + "", true, contact1.get()); |
| + AddPhoneNumber("phone", Contact_AddressType_Relation_HOME, |
| + "", true, contact1.get()); |
| + contacts_to_save.reset(new ContactPointers); |
| + contacts_to_save->push_back(contact1.get()); |
| + SaveContacts(contacts_to_save.Pass(), true); |
| + loaded_contacts = LoadContacts(); |
| + EXPECT_EQ(VarContactsToString(1, contact1.get()), |
| + ContactsToString(*loaded_contacts)); |
| + |
| + // Do a full update including no contacts. The database should be cleared. |
| + contacts_to_save.reset(new ContactPointers); |
| + SaveContacts(contacts_to_save.Pass(), true); |
| + loaded_contacts = LoadContacts(); |
| + EXPECT_TRUE(loaded_contacts->empty()); |
| +} |
| + |
| +// Test that we create a new database when we encounter a corrupted one. |
| +TEST_F(ContactDatabaseTest, DeleteWhenCorrupt) { |
| + clear_db(); |
| + // Overwrite all of the files in the database with a space character. |
| + file_util::FileEnumerator enumerator( |
| + database_path(), false, file_util::FileEnumerator::FILES); |
| + for (FilePath path = enumerator.Next(); !path.empty(); |
| + path = enumerator.Next()) { |
| + file_util::WriteFile(path, " ", 1); |
| + } |
| + CreateDatabase(); |
| + |
| + // Make sure that the resulting database is usable. |
| + scoped_ptr<Contact> contact(new Contact); |
| + InitContact("1", "1", false, contact.get()); |
| + scoped_ptr<ContactPointers> contacts_to_save(new ContactPointers); |
| + contacts_to_save->push_back(contact.get()); |
| + SaveContacts(contacts_to_save.Pass(), true); |
| + scoped_ptr<ScopedVector<Contact> > loaded_contacts = LoadContacts(); |
| + EXPECT_EQ(VarContactsToString(1, contact.get()), |
| + ContactsToString(*loaded_contacts)); |
| +} |
| + |
| +} // namespace test |
| +} // namespace contacts |