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_manager.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "chrome/test/base/testing_profile.h" |
| 10 #include "chrome/browser/chromeos/contacts/contact.pb.h" |
| 11 #include "chrome/browser/chromeos/contacts/contact_manager_observer.h" |
| 12 #include "chrome/browser/chromeos/contacts/contact_test_util.h" |
| 13 #include "chrome/browser/chromeos/contacts/fake_contact_store.h" |
| 14 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/notification_details.h" |
| 17 #include "content/public/browser/notification_service.h" |
| 18 #include "content/public/browser/notification_source.h" |
| 19 #include "content/public/test/test_browser_thread.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 using content::BrowserThread; |
| 23 |
| 24 namespace contacts { |
| 25 namespace test { |
| 26 |
| 27 // ContactManagerObserver implementation that registers itself with a |
| 28 // ContactManager and counts the number of times that it's been told that |
| 29 // contacts have been updated. |
| 30 class TestContactManagerObserver : public ContactManagerObserver { |
| 31 public: |
| 32 TestContactManagerObserver(ContactManager* contact_manager, |
| 33 Profile* profile) |
| 34 : contact_manager_(contact_manager), |
| 35 profile_(profile), |
| 36 num_updates_(0) { |
| 37 contact_manager_->AddObserver(this, profile_); |
| 38 } |
| 39 ~TestContactManagerObserver() { |
| 40 contact_manager_->RemoveObserver(this, profile_); |
| 41 } |
| 42 |
| 43 int num_updates() const { return num_updates_; } |
| 44 void reset_stats() { num_updates_ = 0; } |
| 45 |
| 46 // ContactManagerObserver overrides: |
| 47 void OnContactsUpdated(ContactManager* manager, Profile* profile) OVERRIDE { |
| 48 CHECK(manager == contact_manager_); |
| 49 CHECK(profile == profile_); |
| 50 num_updates_++; |
| 51 } |
| 52 |
| 53 private: |
| 54 ContactManager* contact_manager_; // not owned |
| 55 Profile* profile_; // not owned |
| 56 |
| 57 // Number of times that OnContactsUpdated() has been called. |
| 58 int num_updates_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(TestContactManagerObserver); |
| 61 }; |
| 62 |
| 63 class ContactManagerTest : public testing::Test { |
| 64 public: |
| 65 ContactManagerTest() : ui_thread_(BrowserThread::UI, &message_loop_) {} |
| 66 virtual ~ContactManagerTest() {} |
| 67 |
| 68 protected: |
| 69 // testing::Test implementation. |
| 70 virtual void SetUp() OVERRIDE { |
| 71 profile_.reset(new TestingProfile); |
| 72 manager_.reset(new ContactManager); |
| 73 |
| 74 store_factory_ = new FakeContactStoreFactory; |
| 75 manager_->set_contact_store_factory_for_testing( |
| 76 scoped_ptr<ContactStoreFactory>(store_factory_).Pass()); |
| 77 |
| 78 observer_.reset( |
| 79 new TestContactManagerObserver(manager_.get(), profile_.get())); |
| 80 } |
| 81 |
| 82 // Notify the ContactManager that |profile| has been created. |
| 83 void NotifyAboutProfileCreation(Profile* profile) { |
| 84 CHECK(profile); |
| 85 manager_->Observe( |
| 86 chrome::NOTIFICATION_PROFILE_CREATED, |
| 87 content::Source<Profile>(profile_.get()), |
| 88 content::NotificationService::NoDetails()); |
| 89 } |
| 90 |
| 91 // Notify the ContactManager that |profile| has been destroyed. |
| 92 void NotifyAboutProfileDestruction(Profile* profile) { |
| 93 CHECK(profile); |
| 94 manager_->Observe( |
| 95 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 96 content::NotificationService::AllSources(), |
| 97 content::Details<Profile>(profile_.get())); |
| 98 } |
| 99 |
| 100 MessageLoopForUI message_loop_; |
| 101 content::TestBrowserThread ui_thread_; |
| 102 |
| 103 scoped_ptr<TestingProfile> profile_; |
| 104 scoped_ptr<ContactManager> manager_; |
| 105 FakeContactStoreFactory* store_factory_; // not owned |
| 106 scoped_ptr<TestContactManagerObserver> observer_; |
| 107 FakeContactStore* store_; // not owned |
| 108 |
| 109 private: |
| 110 DISALLOW_COPY_AND_ASSIGN(ContactManagerTest); |
| 111 }; |
| 112 |
| 113 TEST_F(ContactManagerTest, NotifyOnUpdate) { |
| 114 // ContactManager should notify its observers when it receives notification |
| 115 // that a ContactStore has been updated. |
| 116 observer_->reset_stats(); |
| 117 NotifyAboutProfileCreation(profile_.get()); |
| 118 EXPECT_EQ(0, observer_->num_updates()); |
| 119 |
| 120 FakeContactStore* store = |
| 121 store_factory_->GetContactStoreForProfile(profile_.get()); |
| 122 ASSERT_TRUE(store); |
| 123 store->NotifyObserversAboutContactsUpdate(); |
| 124 EXPECT_EQ(1, observer_->num_updates()); |
| 125 |
| 126 store->NotifyObserversAboutContactsUpdate(); |
| 127 EXPECT_EQ(2, observer_->num_updates()); |
| 128 } |
| 129 |
| 130 TEST_F(ContactManagerTest, GetContacts) { |
| 131 // Create two contacts and tell the store to return them. |
| 132 const std::string kProviderId1 = "1"; |
| 133 scoped_ptr<Contact> contact1(new Contact); |
| 134 InitContact(kProviderId1, "1", false, contact1.get()); |
| 135 |
| 136 const std::string kProviderId2 = "2"; |
| 137 scoped_ptr<Contact> contact2(new Contact); |
| 138 InitContact(kProviderId2, "2", false, contact2.get()); |
| 139 |
| 140 NotifyAboutProfileCreation(profile_.get()); |
| 141 FakeContactStore* store = |
| 142 store_factory_->GetContactStoreForProfile(profile_.get()); |
| 143 ASSERT_TRUE(store); |
| 144 |
| 145 ContactPointers store_contacts; |
| 146 store_contacts.push_back(contact1.get()); |
| 147 store_contacts.push_back(contact2.get()); |
| 148 store->SetContacts(store_contacts); |
| 149 store->NotifyObserversAboutContactsUpdate(); |
| 150 |
| 151 // Check that GetAllContacts() returns both contacts. |
| 152 scoped_ptr<ContactPointers> loaded_contacts = |
| 153 manager_->GetAllContacts(profile_.get()); |
| 154 EXPECT_EQ(ContactsToString(store_contacts), |
| 155 ContactsToString(*loaded_contacts)); |
| 156 |
| 157 // Check that we can get individual contacts using GetContactByProviderId(). |
| 158 const Contact* loaded_contact = |
| 159 manager_->GetContactByProviderId(profile_.get(), kProviderId1); |
| 160 ASSERT_TRUE(loaded_contact); |
| 161 EXPECT_EQ(ContactToString(*contact1), ContactToString(*loaded_contact)); |
| 162 |
| 163 loaded_contact = |
| 164 manager_->GetContactByProviderId(profile_.get(), kProviderId2); |
| 165 ASSERT_TRUE(loaded_contact); |
| 166 EXPECT_EQ(ContactToString(*contact2), ContactToString(*loaded_contact)); |
| 167 |
| 168 // We should get NULL if we pass a nonexistent provider ID or an unregistered |
| 169 // profile. |
| 170 EXPECT_FALSE(manager_->GetContactByProviderId(profile_.get(), "foo")); |
| 171 TestingProfile other_profile; |
| 172 EXPECT_FALSE(manager_->GetContactByProviderId(&other_profile, kProviderId1)); |
| 173 |
| 174 // Destroy the profile. |
| 175 NotifyAboutProfileDestruction(profile_.get()); |
| 176 EXPECT_FALSE(manager_->GetContactByProviderId(profile_.get(), kProviderId1)); |
| 177 } |
| 178 |
| 179 TEST_F(ContactManagerTest, ProfileUnsupportedByContactStore) { |
| 180 // ContactManager shouldn't try to create a ContactStore for an unsuppported |
| 181 // Profile. |
| 182 store_factory_->set_permit_store_creation(false); |
| 183 NotifyAboutProfileCreation(profile_.get()); |
| 184 EXPECT_FALSE(store_factory_->GetContactStoreForProfile(profile_.get())); |
| 185 NotifyAboutProfileDestruction(profile_.get()); |
| 186 } |
| 187 |
| 188 } // namespace test |
| 189 } // namespace contacts |
OLD | NEW |