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

Side by Side Diff: chrome/browser/chromeos/contacts/contact_manager_unittest.cc

Issue 10831162: contacts: Add ContactManager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: apply review feedback and merge Created 8 years, 4 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_browser_process.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "chrome/test/base/testing_profile_manager.h"
12 #include "chrome/browser/chromeos/contacts/contact.pb.h"
13 #include "chrome/browser/chromeos/contacts/contact_manager_observer.h"
14 #include "chrome/browser/chromeos/contacts/contact_test_util.h"
15 #include "chrome/browser/chromeos/contacts/fake_contact_store.h"
16 #include "chrome/common/chrome_notification_types.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 ~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 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(
74 static_cast<TestingBrowserProcess*>(g_browser_process)));
75 ASSERT_TRUE(profile_manager_->SetUp());
76
77 contact_manager_.reset(new ContactManager);
78 store_factory_ = new FakeContactStoreFactory;
79 contact_manager_->SetContactStoreForTesting(
80 scoped_ptr<ContactStoreFactory>(store_factory_).Pass());
81 contact_manager_->Init();
82 }
83
84 MessageLoopForUI message_loop_;
85 content::TestBrowserThread ui_thread_;
86
87 scoped_ptr<TestingProfileManager> profile_manager_;
88 scoped_ptr<ContactManager> contact_manager_;
89 FakeContactStoreFactory* store_factory_; // not owned
90
91 private:
92 DISALLOW_COPY_AND_ASSIGN(ContactManagerTest);
93 };
94
95 TEST_F(ContactManagerTest, NotifyOnUpdate) {
96 const std::string kProfileName = "test_profile";
97 TestingProfile* profile =
98 profile_manager_->CreateTestingProfile(kProfileName);
99 TestContactManagerObserver observer(contact_manager_.get(), profile);
100 EXPECT_EQ(0, observer.num_updates());
101
102 // ContactManager should notify its observers when it receives notification
103 // that a ContactStore has been updated.
104 FakeContactStore* store = store_factory_->GetContactStoreForProfile(profile);
105 ASSERT_TRUE(store);
106 store->NotifyObserversAboutContactsUpdate();
107 EXPECT_EQ(1, observer.num_updates());
108
109 store->NotifyObserversAboutContactsUpdate();
110 EXPECT_EQ(2, observer.num_updates());
111
112 profile_manager_->DeleteTestingProfile(kProfileName);
113 EXPECT_EQ(2, observer.num_updates());
114 }
115
116 TEST_F(ContactManagerTest, GetContacts) {
117 // Create two contacts and tell the store to return them.
118 const std::string kProviderId1 = "1";
119 scoped_ptr<Contact> contact1(new Contact);
120 InitContact(kProviderId1, "1", false, contact1.get());
121
122 const std::string kProviderId2 = "2";
123 scoped_ptr<Contact> contact2(new Contact);
124 InitContact(kProviderId2, "2", false, contact2.get());
125
126 const std::string kProfileName = "test_profile";
127 TestingProfile* profile =
128 profile_manager_->CreateTestingProfile(kProfileName);
129 FakeContactStore* store = store_factory_->GetContactStoreForProfile(profile);
130 ASSERT_TRUE(store);
131
132 ContactPointers store_contacts;
133 store_contacts.push_back(contact1.get());
134 store_contacts.push_back(contact2.get());
135 store->SetContacts(store_contacts);
136 store->NotifyObserversAboutContactsUpdate();
137
138 // Check that GetAllContacts() returns both contacts.
139 scoped_ptr<ContactPointers> loaded_contacts =
140 contact_manager_->GetAllContacts(profile);
141 EXPECT_EQ(ContactsToString(store_contacts),
142 ContactsToString(*loaded_contacts));
143
144 // Check that we can get individual contacts using GetContactByProviderId().
145 const Contact* loaded_contact =
146 contact_manager_->GetContactByProviderId(profile, kProviderId1);
147 ASSERT_TRUE(loaded_contact);
148 EXPECT_EQ(ContactToString(*contact1), ContactToString(*loaded_contact));
149
150 loaded_contact =
151 contact_manager_->GetContactByProviderId(profile, kProviderId2);
152 ASSERT_TRUE(loaded_contact);
153 EXPECT_EQ(ContactToString(*contact2), ContactToString(*loaded_contact));
154
155 // We should get NULL if we pass a nonexistent provider ID.
156 EXPECT_FALSE(contact_manager_->GetContactByProviderId(profile, "foo"));
157
158 profile_manager_->DeleteTestingProfile(kProfileName);
159 }
160
161 TEST_F(ContactManagerTest, ProfileUnsupportedByContactStore) {
162 // ContactManager shouldn't try to create a ContactStore for an unsuppported
163 // Profile.
164 store_factory_->set_permit_store_creation(false);
165 const std::string kProfileName = "test_profile";
166 TestingProfile* profile =
167 profile_manager_->CreateTestingProfile(kProfileName);
168 EXPECT_FALSE(store_factory_->GetContactStoreForProfile(profile));
169 profile_manager_->DeleteTestingProfile(kProfileName);
170 }
171
172 } // namespace test
173 } // namespace contacts
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698