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..ae580900bdfd5443e59042e4b8f25b4268408f28 |
--- /dev/null |
+++ b/chrome/browser/chromeos/contacts/contact_database_unittest.cc |
@@ -0,0 +1,220 @@ |
+// 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 <cstdarg> |
+#include <map> |
+#include <string> |
+#include <utility> |
+ |
+#include "base/bind.h" |
+#include "base/file_path.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.h" |
+#include "chrome/browser/chromeos/contacts/contact_test_lib.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/test/test_browser_thread.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
+#include "ui/gfx/size.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace contacts { |
+namespace test { |
+ |
+const FilePath::CharType kDatabaseFileName[] = FILE_PATH_LITERAL("contacts.db"); |
+ |
+class ContactDatabaseTest : public testing::Test { |
+ public: |
+ ContactDatabaseTest() |
+ : ui_thread_(BrowserThread::UI, &message_loop_), |
+ file_thread_(BrowserThread::FILE, &message_loop_), |
+ db_(new ContactDatabase), |
+ save_was_successful_(false) { |
+ } |
+ |
+ virtual ~ContactDatabaseTest() { |
+ } |
+ |
+ protected: |
+ // testing::Test implementation. |
+ virtual void SetUp() OVERRIDE { |
+ testing::Test::SetUp(); |
+ CHECK(temp_dir_.CreateUniqueTempDir()); |
+ db_->Init(temp_dir_.path().Append(kDatabaseFileName), |
+ 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(); |
+ } |
+ |
+ protected: |
+ // Calls ContactDatabase::SaveContacts() and blocks until the operation is |
+ // complete. |
+ void SaveContacts(scoped_ptr<ContactPointers> contacts, bool is_full_update) { |
+ 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() { |
+ 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(); |
+ } |
+ |
+ 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_; |
+ |
+ ScopedTempDir temp_dir_; |
+ |
+ scoped_ptr<ContactDatabase> db_; |
+ |
+ // Did the last ContactDatabase::SaveContacts() call return success? |
+ bool save_was_successful_; |
+ |
+ // 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<contacts::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<contacts::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()); |
+} |
+ |
+} // namespace test |
+} // namespace contacts |