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