Index: chrome/browser/chromeos/contacts/contact_manager_unittest.cc |
diff --git a/chrome/browser/chromeos/contacts/contact_manager_unittest.cc b/chrome/browser/chromeos/contacts/contact_manager_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..83cd1eefda5f32ceae9f5de3bcd53725bc30e337 |
--- /dev/null |
+++ b/chrome/browser/chromeos/contacts/contact_manager_unittest.cc |
@@ -0,0 +1,189 @@ |
+// 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_manager.h" |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "base/message_loop.h" |
+#include "chrome/test/base/testing_profile.h" |
+#include "chrome/browser/chromeos/contacts/contact.pb.h" |
+#include "chrome/browser/chromeos/contacts/contact_manager_observer.h" |
+#include "chrome/browser/chromeos/contacts/contact_test_util.h" |
+#include "chrome/browser/chromeos/contacts/fake_contact_store.h" |
+#include "chrome/common/chrome_notification_types.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/notification_details.h" |
+#include "content/public/browser/notification_service.h" |
+#include "content/public/browser/notification_source.h" |
+#include "content/public/test/test_browser_thread.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace contacts { |
+namespace test { |
+ |
+// ContactManagerObserver implementation that registers itself with a |
+// ContactManager and counts the number of times that it's been told that |
+// contacts have been updated. |
+class TestContactManagerObserver : public ContactManagerObserver { |
+ public: |
+ TestContactManagerObserver(ContactManager* contact_manager, |
+ Profile* profile) |
+ : contact_manager_(contact_manager), |
+ profile_(profile), |
+ num_updates_(0) { |
+ contact_manager_->AddObserver(this, profile_); |
+ } |
+ ~TestContactManagerObserver() { |
+ contact_manager_->RemoveObserver(this, profile_); |
+ } |
+ |
+ int num_updates() const { return num_updates_; } |
+ void reset_stats() { num_updates_ = 0; } |
+ |
+ // ContactManagerObserver overrides: |
+ void OnContactsUpdated(ContactManager* manager, Profile* profile) OVERRIDE { |
+ CHECK(manager == contact_manager_); |
+ CHECK(profile == profile_); |
+ num_updates_++; |
+ } |
+ |
+ private: |
+ ContactManager* contact_manager_; // not owned |
+ Profile* profile_; // not owned |
+ |
+ // Number of times that OnContactsUpdated() has been called. |
+ int num_updates_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestContactManagerObserver); |
+}; |
+ |
+class ContactManagerTest : public testing::Test { |
+ public: |
+ ContactManagerTest() : ui_thread_(BrowserThread::UI, &message_loop_) {} |
+ virtual ~ContactManagerTest() {} |
+ |
+ protected: |
+ // testing::Test implementation. |
+ virtual void SetUp() OVERRIDE { |
+ profile_.reset(new TestingProfile); |
+ manager_.reset(new ContactManager); |
+ |
+ store_factory_ = new FakeContactStoreFactory; |
+ manager_->set_contact_store_factory_for_testing( |
+ scoped_ptr<ContactStoreFactory>(store_factory_).Pass()); |
+ |
+ observer_.reset( |
+ new TestContactManagerObserver(manager_.get(), profile_.get())); |
+ } |
+ |
+ // Notify the ContactManager that |profile| has been created. |
+ void NotifyAboutProfileCreation(Profile* profile) { |
+ CHECK(profile); |
+ manager_->Observe( |
+ chrome::NOTIFICATION_PROFILE_CREATED, |
+ content::Source<Profile>(profile_.get()), |
+ content::NotificationService::NoDetails()); |
+ } |
+ |
+ // Notify the ContactManager that |profile| has been destroyed. |
+ void NotifyAboutProfileDestruction(Profile* profile) { |
+ CHECK(profile); |
+ manager_->Observe( |
+ chrome::NOTIFICATION_PROFILE_DESTROYED, |
+ content::NotificationService::AllSources(), |
+ content::Details<Profile>(profile_.get())); |
+ } |
+ |
+ MessageLoopForUI message_loop_; |
+ content::TestBrowserThread ui_thread_; |
+ |
+ scoped_ptr<TestingProfile> profile_; |
+ scoped_ptr<ContactManager> manager_; |
+ FakeContactStoreFactory* store_factory_; // not owned |
+ scoped_ptr<TestContactManagerObserver> observer_; |
+ FakeContactStore* store_; // not owned |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(ContactManagerTest); |
+}; |
+ |
+TEST_F(ContactManagerTest, NotifyOnUpdate) { |
+ // ContactManager should notify its observers when it receives notification |
+ // that a ContactStore has been updated. |
+ observer_->reset_stats(); |
+ NotifyAboutProfileCreation(profile_.get()); |
+ EXPECT_EQ(0, observer_->num_updates()); |
+ |
+ FakeContactStore* store = |
+ store_factory_->GetContactStoreForProfile(profile_.get()); |
+ ASSERT_TRUE(store); |
+ store->NotifyObserversAboutContactsUpdate(); |
+ EXPECT_EQ(1, observer_->num_updates()); |
+ |
+ store->NotifyObserversAboutContactsUpdate(); |
+ EXPECT_EQ(2, observer_->num_updates()); |
+} |
+ |
+TEST_F(ContactManagerTest, GetContacts) { |
+ // Create two contacts and tell the store to return them. |
+ const std::string kProviderId1 = "1"; |
+ scoped_ptr<Contact> contact1(new Contact); |
+ InitContact(kProviderId1, "1", false, contact1.get()); |
+ |
+ const std::string kProviderId2 = "2"; |
+ scoped_ptr<Contact> contact2(new Contact); |
+ InitContact(kProviderId2, "2", false, contact2.get()); |
+ |
+ NotifyAboutProfileCreation(profile_.get()); |
+ FakeContactStore* store = |
+ store_factory_->GetContactStoreForProfile(profile_.get()); |
+ ASSERT_TRUE(store); |
+ |
+ ContactPointers store_contacts; |
+ store_contacts.push_back(contact1.get()); |
+ store_contacts.push_back(contact2.get()); |
+ store->SetContacts(store_contacts); |
+ store->NotifyObserversAboutContactsUpdate(); |
+ |
+ // Check that GetAllContacts() returns both contacts. |
+ scoped_ptr<ContactPointers> loaded_contacts = |
+ manager_->GetAllContacts(profile_.get()); |
+ EXPECT_EQ(ContactsToString(store_contacts), |
+ ContactsToString(*loaded_contacts)); |
+ |
+ // Check that we can get individual contacts using GetContactByProviderId(). |
+ const Contact* loaded_contact = |
+ manager_->GetContactByProviderId(profile_.get(), kProviderId1); |
+ ASSERT_TRUE(loaded_contact); |
+ EXPECT_EQ(ContactToString(*contact1), ContactToString(*loaded_contact)); |
+ |
+ loaded_contact = |
+ manager_->GetContactByProviderId(profile_.get(), kProviderId2); |
+ ASSERT_TRUE(loaded_contact); |
+ EXPECT_EQ(ContactToString(*contact2), ContactToString(*loaded_contact)); |
+ |
+ // We should get NULL if we pass a nonexistent provider ID or an unregistered |
+ // profile. |
+ EXPECT_FALSE(manager_->GetContactByProviderId(profile_.get(), "foo")); |
+ TestingProfile other_profile; |
+ EXPECT_FALSE(manager_->GetContactByProviderId(&other_profile, kProviderId1)); |
+ |
+ // Destroy the profile. |
+ NotifyAboutProfileDestruction(profile_.get()); |
+ EXPECT_FALSE(manager_->GetContactByProviderId(profile_.get(), kProviderId1)); |
+} |
+ |
+TEST_F(ContactManagerTest, ProfileUnsupportedByContactStore) { |
+ // ContactManager shouldn't try to create a ContactStore for an unsuppported |
+ // Profile. |
+ store_factory_->set_permit_store_creation(false); |
+ NotifyAboutProfileCreation(profile_.get()); |
+ EXPECT_FALSE(store_factory_->GetContactStoreForProfile(profile_.get())); |
+ NotifyAboutProfileDestruction(profile_.get()); |
+} |
+ |
+} // namespace test |
+} // namespace contacts |