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

Side by Side Diff: chrome/browser/prefs/pref_notifier_impl_unittest.cc

Issue 5441002: Clean up pref change notification handling. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix extension prefs breakage Created 10 years 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) 2010 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/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"
9 #include "chrome/common/notification_observer_mock.h"
10 #include "chrome/common/notification_service.h"
11 #include "chrome/test/testing_pref_service.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using testing::_;
16 using testing::Field;
17 using testing::Invoke;
18 using testing::Mock;
19 using testing::Truly;
20
21 namespace {
22
23 const char kChangedPref[] = "changed_pref";
24 const char kUnchangedPref[] = "unchanged_pref";
25
26 bool DetailsAreChangedPref(const Details<std::string>& details) {
27 std::string* string_in = Details<std::string>(details).ptr();
28 return strcmp(string_in->c_str(), kChangedPref) == 0;
29 }
30
31 // Test PrefNotifier that allows tracking of observers and notifications.
32 class MockPrefNotifier : public PrefNotifierImpl {
33 public:
34 explicit MockPrefNotifier(PrefService* pref_service)
35 : PrefNotifierImpl(pref_service) {}
36 virtual ~MockPrefNotifier() {}
37
38 MOCK_METHOD1(FireObservers, void(const std::string& path));
39
40 size_t CountObserver(const char* path, NotificationObserver* obs) {
41 PrefObserverMap::const_iterator observer_iterator =
42 pref_observers()->find(path);
43 if (observer_iterator == pref_observers()->end())
44 return false;
45
46 NotificationObserverList* observer_list = observer_iterator->second;
47 NotificationObserverList::Iterator it(*observer_list);
48 NotificationObserver* existing_obs;
49 size_t count = 0;
50 while ((existing_obs = it.GetNext()) != NULL) {
51 if (existing_obs == obs)
52 count++;
53 }
54
55 return count;
56 }
57 };
58
59 // Test fixture class.
60 class PrefNotifierTest : public testing::Test {
61 protected:
62 virtual void SetUp() {
63 pref_service_.RegisterBooleanPref(kChangedPref, true);
64 pref_service_.RegisterBooleanPref(kUnchangedPref, true);
65 }
66
67 TestingPrefService pref_service_;
68
69 PrefObserverMock obs1_;
70 PrefObserverMock obs2_;
71 };
72
73 TEST_F(PrefNotifierTest, OnPreferenceChanged) {
74 MockPrefNotifier notifier(&pref_service_);
75 EXPECT_CALL(notifier, FireObservers(kChangedPref)).Times(1);
76 notifier.OnPreferenceChanged(kChangedPref);
77 }
78
79 TEST_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_),
89 NotificationService::NoDetails()));
90 notifier.OnInitializationCompleted();
91 }
92
93 TEST_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
156 TEST_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
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_notifier_impl.cc ('k') | chrome/browser/prefs/pref_notifier_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698