blob: a76c93384420acbacad3733bbb923d1f0d7ed28c [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
avi9ef8bb02015-12-24 05:29:365#include <stddef.h>
6
[email protected]a6a7ced2012-11-01 17:24:187#include "base/bind.h"
8#include "base/callback.h"
brettwf00b9b42016-02-01 22:11:389#include "components/prefs/mock_pref_change_callback.h"
10#include "components/prefs/pref_notifier_impl.h"
11#include "components/prefs/pref_observer.h"
12#include "components/prefs/pref_registry_simple.h"
13#include "components/prefs/pref_service.h"
14#include "components/prefs/pref_value_store.h"
15#include "components/prefs/testing_pref_service.h"
[email protected]acd78969c2010-12-08 09:49:1116#include "testing/gmock/include/gmock/gmock.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19using testing::_;
20using testing::Field;
21using testing::Invoke;
22using testing::Mock;
23using testing::Truly;
24
25namespace {
26
27const char kChangedPref[] = "changed_pref";
28const char kUnchangedPref[] = "unchanged_pref";
29
[email protected]a6a7ced2012-11-01 17:24:1830class MockPrefInitObserver {
31 public:
32 MOCK_METHOD1(OnInitializationCompleted, void(bool));
33};
34
35// This is an unmodified PrefNotifierImpl, except we make
36// OnPreferenceChanged public for tests.
37class TestingPrefNotifierImpl : public PrefNotifierImpl {
38 public:
[email protected]96a5c342012-12-04 18:14:0239 explicit TestingPrefNotifierImpl(PrefService* service)
40 : PrefNotifierImpl(service) {
41 }
[email protected]a6a7ced2012-11-01 17:24:1842
43 // Make public for tests.
44 using PrefNotifierImpl::OnPreferenceChanged;
45};
46
47// Mock PrefNotifier that allows tracking of observers and notifications.
[email protected]acd78969c2010-12-08 09:49:1148class MockPrefNotifier : public PrefNotifierImpl {
49 public:
50 explicit MockPrefNotifier(PrefService* pref_service)
51 : PrefNotifierImpl(pref_service) {}
Daniel Cheng67848522018-04-27 22:04:4152 ~MockPrefNotifier() override {}
[email protected]acd78969c2010-12-08 09:49:1153
54 MOCK_METHOD1(FireObservers, void(const std::string& path));
55
georgesak7da6e9d2014-12-03 01:10:2956 size_t CountObserver(const std::string& path, PrefObserver* obs) {
[email protected]acd78969c2010-12-08 09:49:1157 PrefObserverMap::const_iterator observer_iterator =
58 pref_observers()->find(path);
59 if (observer_iterator == pref_observers()->end())
60 return false;
61
[email protected]acd78969c2010-12-08 09:49:1162 size_t count = 0;
dcheng018341c2016-10-14 21:53:5663 for (auto& existing_obs : *observer_iterator->second) {
64 if (&existing_obs == obs)
[email protected]acd78969c2010-12-08 09:49:1165 count++;
66 }
67
68 return count;
69 }
[email protected]a6a7ced2012-11-01 17:24:1870
71 // Make public for tests below.
72 using PrefNotifierImpl::OnPreferenceChanged;
73 using PrefNotifierImpl::OnInitializationCompleted;
[email protected]acd78969c2010-12-08 09:49:1174};
75
[email protected]96a5c342012-12-04 18:14:0276class PrefObserverMock : public PrefObserver {
77 public:
78 PrefObserverMock() {}
79 virtual ~PrefObserverMock() {}
80
[email protected]d6ab8452013-02-16 04:20:5981 MOCK_METHOD2(OnPreferenceChanged, void(PrefService*, const std::string&));
[email protected]96a5c342012-12-04 18:14:0282};
83
[email protected]acd78969c2010-12-08 09:49:1184// Test fixture class.
[email protected]583844c2011-08-27 00:38:3585class PrefNotifierTest : public testing::Test {
[email protected]acd78969c2010-12-08 09:49:1186 protected:
dcheng8aef37612014-12-23 02:56:4787 void SetUp() override {
[email protected]b1de2c72013-02-06 02:45:4788 pref_service_.registry()->RegisterBooleanPref(kChangedPref, true);
89 pref_service_.registry()->RegisterBooleanPref(kUnchangedPref, true);
[email protected]acd78969c2010-12-08 09:49:1190 }
91
[email protected]5b199522012-12-22 17:24:4492 TestingPrefServiceSimple pref_service_;
[email protected]acd78969c2010-12-08 09:49:1193
94 PrefObserverMock obs1_;
95 PrefObserverMock obs2_;
96};
97
98TEST_F(PrefNotifierTest, OnPreferenceChanged) {
99 MockPrefNotifier notifier(&pref_service_);
100 EXPECT_CALL(notifier, FireObservers(kChangedPref)).Times(1);
101 notifier.OnPreferenceChanged(kChangedPref);
102}
103
104TEST_F(PrefNotifierTest, OnInitializationCompleted) {
105 MockPrefNotifier notifier(&pref_service_);
[email protected]a6a7ced2012-11-01 17:24:18106 MockPrefInitObserver observer;
107 notifier.AddInitObserver(
108 base::Bind(&MockPrefInitObserver::OnInitializationCompleted,
109 base::Unretained(&observer)));
110 EXPECT_CALL(observer, OnInitializationCompleted(true));
[email protected]845b43a82011-05-11 10:14:43111 notifier.OnInitializationCompleted(true);
[email protected]acd78969c2010-12-08 09:49:11112}
113
114TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) {
115 const char pref_name[] = "homepage";
116 const char pref_name2[] = "proxy";
117
118 MockPrefNotifier notifier(&pref_service_);
119 notifier.AddPrefObserver(pref_name, &obs1_);
120 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
121 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_));
122 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
123 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
124
125 // Re-adding the same observer for the same pref doesn't change anything.
126 // Skip this in debug mode, since it hits a DCHECK and death tests aren't
127 // thread-safe.
[email protected]20960e072011-09-20 20:59:01128#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
[email protected]acd78969c2010-12-08 09:49:11129 notifier.AddPrefObserver(pref_name, &obs1_);
130 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
131 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_));
132 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
133 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
[email protected]20960e072011-09-20 20:59:01134#endif
[email protected]acd78969c2010-12-08 09:49:11135
136 // Ensure that we can add the same observer to a different pref.
137 notifier.AddPrefObserver(pref_name2, &obs1_);
138 ASSERT_EQ(1u, 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 // Ensure that we can add another observer to the same pref.
144 notifier.AddPrefObserver(pref_name, &obs2_);
145 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
146 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
147 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs2_));
148 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
149
150 // Ensure that we can remove all observers, and that removing a non-existent
151 // observer is harmless.
152 notifier.RemovePrefObserver(pref_name, &obs1_);
153 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
154 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
155 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs2_));
156 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
157
158 notifier.RemovePrefObserver(pref_name, &obs2_);
159 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
160 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
161 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
162 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
163
164 notifier.RemovePrefObserver(pref_name, &obs1_);
165 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
166 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
167 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
168 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
169
170 notifier.RemovePrefObserver(pref_name2, &obs1_);
171 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
172 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_));
173 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
174 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
175}
176
177TEST_F(PrefNotifierTest, FireObservers) {
[email protected]a6a7ced2012-11-01 17:24:18178 TestingPrefNotifierImpl notifier(&pref_service_);
[email protected]acd78969c2010-12-08 09:49:11179 notifier.AddPrefObserver(kChangedPref, &obs1_);
180 notifier.AddPrefObserver(kUnchangedPref, &obs1_);
181
[email protected]a6a7ced2012-11-01 17:24:18182 EXPECT_CALL(obs1_, OnPreferenceChanged(&pref_service_, kChangedPref));
183 EXPECT_CALL(obs2_, OnPreferenceChanged(_, _)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11184 notifier.OnPreferenceChanged(kChangedPref);
185 Mock::VerifyAndClearExpectations(&obs1_);
186 Mock::VerifyAndClearExpectations(&obs2_);
187
188 notifier.AddPrefObserver(kChangedPref, &obs2_);
189 notifier.AddPrefObserver(kUnchangedPref, &obs2_);
190
[email protected]a6a7ced2012-11-01 17:24:18191 EXPECT_CALL(obs1_, OnPreferenceChanged(&pref_service_, kChangedPref));
192 EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref));
[email protected]acd78969c2010-12-08 09:49:11193 notifier.OnPreferenceChanged(kChangedPref);
194 Mock::VerifyAndClearExpectations(&obs1_);
195 Mock::VerifyAndClearExpectations(&obs2_);
196
197 // Make sure removing an observer from one pref doesn't affect anything else.
198 notifier.RemovePrefObserver(kChangedPref, &obs1_);
199
[email protected]a6a7ced2012-11-01 17:24:18200 EXPECT_CALL(obs1_, OnPreferenceChanged(_, _)).Times(0);
201 EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref));
[email protected]acd78969c2010-12-08 09:49:11202 notifier.OnPreferenceChanged(kChangedPref);
203 Mock::VerifyAndClearExpectations(&obs1_);
204 Mock::VerifyAndClearExpectations(&obs2_);
205
206 // Make sure removing an observer entirely doesn't affect anything else.
207 notifier.RemovePrefObserver(kUnchangedPref, &obs1_);
208
[email protected]a6a7ced2012-11-01 17:24:18209 EXPECT_CALL(obs1_, OnPreferenceChanged(_, _)).Times(0);
210 EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref));
[email protected]acd78969c2010-12-08 09:49:11211 notifier.OnPreferenceChanged(kChangedPref);
212 Mock::VerifyAndClearExpectations(&obs1_);
213 Mock::VerifyAndClearExpectations(&obs2_);
214
215 notifier.RemovePrefObserver(kChangedPref, &obs2_);
216 notifier.RemovePrefObserver(kUnchangedPref, &obs2_);
217}
218
219} // namespace