blob: c413e328ec81145e7799d99fc88e4e96087173c1 [file] [log] [blame]
[email protected]39f83892011-04-25 17:15:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]acd78969c2010-12-08 09:49:112// 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/prefs/pref_notifier_impl.h"
6#include "chrome/browser/prefs/pref_observer_mock.h"
7#include "chrome/browser/prefs/pref_service.h"
8#include "chrome/browser/prefs/pref_value_store.h"
[email protected]acd78969c2010-12-08 09:49:119#include "chrome/test/testing_pref_service.h"
[email protected]652e16d2011-03-07 03:53:0010#include "content/common/notification_observer_mock.h"
11#include "content/common/notification_registrar.h"
12#include "content/common/notification_service.h"
[email protected]acd78969c2010-12-08 09:49:1113#include "testing/gmock/include/gmock/gmock.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16using testing::_;
17using testing::Field;
18using testing::Invoke;
19using testing::Mock;
20using testing::Truly;
21
22namespace {
23
24const char kChangedPref[] = "changed_pref";
25const char kUnchangedPref[] = "unchanged_pref";
26
[email protected]acd78969c2010-12-08 09:49:1127// Test PrefNotifier that allows tracking of observers and notifications.
28class MockPrefNotifier : public PrefNotifierImpl {
29 public:
30 explicit MockPrefNotifier(PrefService* pref_service)
31 : PrefNotifierImpl(pref_service) {}
32 virtual ~MockPrefNotifier() {}
33
34 MOCK_METHOD1(FireObservers, void(const std::string& path));
35
36 size_t CountObserver(const char* path, NotificationObserver* obs) {
37 PrefObserverMap::const_iterator observer_iterator =
38 pref_observers()->find(path);
39 if (observer_iterator == pref_observers()->end())
40 return false;
41
42 NotificationObserverList* observer_list = observer_iterator->second;
43 NotificationObserverList::Iterator it(*observer_list);
44 NotificationObserver* existing_obs;
45 size_t count = 0;
46 while ((existing_obs = it.GetNext()) != NULL) {
47 if (existing_obs == obs)
48 count++;
49 }
50
51 return count;
52 }
53};
54
55// Test fixture class.
56class PrefNotifierTest : public testing::Test {
57 protected:
58 virtual void SetUp() {
[email protected]d36f941b2011-05-09 06:19:1659 pref_service_.RegisterBooleanPref(kChangedPref,
60 true,
61 PrefService::UNSYNCABLE_PREF);
62 pref_service_.RegisterBooleanPref(kUnchangedPref,
63 true,
64 PrefService::UNSYNCABLE_PREF);
[email protected]acd78969c2010-12-08 09:49:1165 }
66
67 TestingPrefService pref_service_;
68
69 PrefObserverMock obs1_;
70 PrefObserverMock obs2_;
71};
72
73TEST_F(PrefNotifierTest, OnPreferenceChanged) {
74 MockPrefNotifier notifier(&pref_service_);
75 EXPECT_CALL(notifier, FireObservers(kChangedPref)).Times(1);
76 notifier.OnPreferenceChanged(kChangedPref);
77}
78
79TEST_F(PrefNotifierTest, OnInitializationCompleted) {
80 MockPrefNotifier notifier(&pref_service_);
81 NotificationObserverMock observer;
82 NotificationRegistrar registrar;
83 registrar.Add(&observer, NotificationType::PREF_INITIALIZATION_COMPLETED,
84 Source<PrefService>(&pref_service_));
85 EXPECT_CALL(observer, Observe(
86 Field(&NotificationType::value,
87 NotificationType::PREF_INITIALIZATION_COMPLETED),
88 Source<PrefService>(&pref_service_),
[email protected]845b43a82011-05-11 10:14:4389 Property(&Details<bool>::ptr, testing::Pointee(true))));
90 notifier.OnInitializationCompleted(true);
[email protected]acd78969c2010-12-08 09:49:1191}
92
93TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) {
94 const char pref_name[] = "homepage";
95 const char pref_name2[] = "proxy";
96
97 MockPrefNotifier notifier(&pref_service_);
98 notifier.AddPrefObserver(pref_name, &obs1_);
99 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
100 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_));
101 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
102 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
103
104 // Re-adding the same observer for the same pref doesn't change anything.
105 // Skip this in debug mode, since it hits a DCHECK and death tests aren't
106 // thread-safe.
107#if defined(NDEBUG)
108 notifier.AddPrefObserver(pref_name, &obs1_);
109 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
110 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_));
111 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
112 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
113#endif // NDEBUG
114
115 // Ensure that we can add the same observer to a different pref.
116 notifier.AddPrefObserver(pref_name2, &obs1_);
117 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
118 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
119 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
120 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
121
122 // Ensure that we can add another observer to the same pref.
123 notifier.AddPrefObserver(pref_name, &obs2_);
124 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
125 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
126 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs2_));
127 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
128
129 // Ensure that we can remove all observers, and that removing a non-existent
130 // observer is harmless.
131 notifier.RemovePrefObserver(pref_name, &obs1_);
132 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
133 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
134 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs2_));
135 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
136
137 notifier.RemovePrefObserver(pref_name, &obs2_);
138 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
139 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
140 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
141 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
142
143 notifier.RemovePrefObserver(pref_name, &obs1_);
144 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
145 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
146 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
147 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
148
149 notifier.RemovePrefObserver(pref_name2, &obs1_);
150 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
151 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_));
152 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
153 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
154}
155
156TEST_F(PrefNotifierTest, FireObservers) {
157 FundamentalValue value_true(true);
158 PrefNotifierImpl notifier(&pref_service_);
159 notifier.AddPrefObserver(kChangedPref, &obs1_);
160 notifier.AddPrefObserver(kUnchangedPref, &obs1_);
161
162 obs1_.Expect(&pref_service_, kChangedPref, &value_true);
163 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0);
164 notifier.OnPreferenceChanged(kChangedPref);
165 Mock::VerifyAndClearExpectations(&obs1_);
166 Mock::VerifyAndClearExpectations(&obs2_);
167
168 notifier.AddPrefObserver(kChangedPref, &obs2_);
169 notifier.AddPrefObserver(kUnchangedPref, &obs2_);
170
171 obs1_.Expect(&pref_service_, kChangedPref, &value_true);
172 obs2_.Expect(&pref_service_, kChangedPref, &value_true);
173 notifier.OnPreferenceChanged(kChangedPref);
174 Mock::VerifyAndClearExpectations(&obs1_);
175 Mock::VerifyAndClearExpectations(&obs2_);
176
177 // Make sure removing an observer from one pref doesn't affect anything else.
178 notifier.RemovePrefObserver(kChangedPref, &obs1_);
179
180 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
181 obs2_.Expect(&pref_service_, kChangedPref, &value_true);
182 notifier.OnPreferenceChanged(kChangedPref);
183 Mock::VerifyAndClearExpectations(&obs1_);
184 Mock::VerifyAndClearExpectations(&obs2_);
185
186 // Make sure removing an observer entirely doesn't affect anything else.
187 notifier.RemovePrefObserver(kUnchangedPref, &obs1_);
188
189 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
190 obs2_.Expect(&pref_service_, kChangedPref, &value_true);
191 notifier.OnPreferenceChanged(kChangedPref);
192 Mock::VerifyAndClearExpectations(&obs1_);
193 Mock::VerifyAndClearExpectations(&obs2_);
194
195 notifier.RemovePrefObserver(kChangedPref, &obs2_);
196 notifier.RemovePrefObserver(kUnchangedPref, &obs2_);
197}
198
199} // namespace