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

Unified 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, 5 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 side-by-side diff with in-line comments
Download patch
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..d72651e0b1d8fefc2afb885d871de6caa9215a92
--- /dev/null
+++ b/chrome/browser/chromeos/contacts/contact_database_unittest.cc
@@ -0,0 +1,264 @@
+// 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_),
+ db_(NULL) {
+ }
+
+ virtual ~ContactDatabaseTest() {
+ }
+
+ protected:
+ // testing::Test implementation.
+ virtual void SetUp() OVERRIDE {
+ CHECK(temp_dir_.CreateUniqueTempDir());
+ CreateDatabase();
+ }
+
+ virtual void TearDown() OVERRIDE {
+ DestroyDatabase();
+ }
+
+ protected:
+ FilePath database_path() const {
+ return temp_dir_.path().Append(kDatabaseDirectoryName);
+ }
+
+ void CreateDatabase() {
+ DestroyDatabase();
+ db_ = 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();
+ }
+
+ void DestroyDatabase() {
+ if (db_) {
+ db_->DestroyOnUIThread();
+ db_ = NULL;
+ }
+ }
+
+ // Calls ContactDatabase::SaveContacts() and blocks until the operation is
+ // complete.
+ void SaveContacts(scoped_ptr<ContactPointers> contacts, bool is_full_update) {
+ CHECK(db_);
+ 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_);
+ 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);
+ // TODO(derat): Move gdata::test::RunBlockingPoolTask() to a shared location
+ // and use it for these tests.
+ message_loop_.Quit();
+ }
+
+ 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_;
+
+ // Temporary directory where the database is saved.
+ ScopedTempDir temp_dir_;
+
+ // This class retains ownership of this object.
+ 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) {
+ DestroyDatabase();
+ // 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
« 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