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