blob: ee42891bab9752bb3c787cb94c3a09a0ae2327ea [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) {
jdoerrie3feb1852018-10-05 12:16:4457 auto observer_iterator = pref_observers()->find(path);
[email protected]acd78969c2010-12-08 09:49:1158 if (observer_iterator == pref_observers()->end())
59 return false;
60
[email protected]acd78969c2010-12-08 09:49:1161 size_t count = 0;
dcheng018341c2016-10-14 21:53:5662 for (auto& existing_obs : *observer_iterator->second) {
63 if (&existing_obs == obs)
[email protected]acd78969c2010-12-08 09:49:1164 count++;
65 }
66
67 return count;
68 }
[email protected]a6a7ced2012-11-01 17:24:1869
70 // Make public for tests below.
71 using PrefNotifierImpl::OnPreferenceChanged;
72 using PrefNotifierImpl::OnInitializationCompleted;
[email protected]acd78969c2010-12-08 09:49:1173};
74
[email protected]96a5c342012-12-04 18:14:0275class PrefObserverMock : public PrefObserver {
76 public:
77 PrefObserverMock() {}
78 virtual ~PrefObserverMock() {}
79
[email protected]d6ab8452013-02-16 04:20:5980 MOCK_METHOD2(OnPreferenceChanged, void(PrefService*, const std::string&));
[email protected]96a5c342012-12-04 18:14:0281};
82
[email protected]acd78969c2010-12-08 09:49:1183// Test fixture class.
[email protected]583844c2011-08-27 00:38:3584class PrefNotifierTest : public testing::Test {
[email protected]acd78969c2010-12-08 09:49:1185 protected:
dcheng8aef37612014-12-23 02:56:4786 void SetUp() override {
[email protected]b1de2c72013-02-06 02:45:4787 pref_service_.registry()->RegisterBooleanPref(kChangedPref, true);
88 pref_service_.registry()->RegisterBooleanPref(kUnchangedPref, true);
[email protected]acd78969c2010-12-08 09:49:1189 }
90
[email protected]5b199522012-12-22 17:24:4491 TestingPrefServiceSimple pref_service_;
[email protected]acd78969c2010-12-08 09:49:1192
93 PrefObserverMock obs1_;
94 PrefObserverMock obs2_;
95};
96
97TEST_F(PrefNotifierTest, OnPreferenceChanged) {
98 MockPrefNotifier notifier(&pref_service_);
99 EXPECT_CALL(notifier, FireObservers(kChangedPref)).Times(1);
100 notifier.OnPreferenceChanged(kChangedPref);
101}
102
103TEST_F(PrefNotifierTest, OnInitializationCompleted) {
104 MockPrefNotifier notifier(&pref_service_);
[email protected]a6a7ced2012-11-01 17:24:18105 MockPrefInitObserver observer;
106 notifier.AddInitObserver(
tzik2bcf8e42018-07-31 11:22:15107 base::BindOnce(&MockPrefInitObserver::OnInitializationCompleted,
108 base::Unretained(&observer)));
[email protected]a6a7ced2012-11-01 17:24:18109 EXPECT_CALL(observer, OnInitializationCompleted(true));
[email protected]845b43a82011-05-11 10:14:43110 notifier.OnInitializationCompleted(true);
[email protected]acd78969c2010-12-08 09:49:11111}
112
113TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) {
114 const char pref_name[] = "homepage";
115 const char pref_name2[] = "proxy";
116
117 MockPrefNotifier notifier(&pref_service_);
118 notifier.AddPrefObserver(pref_name, &obs1_);
119 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
120 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_));
121 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
122 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
123
124 // Re-adding the same observer for the same pref doesn't change anything.
125 // Skip this in debug mode, since it hits a DCHECK and death tests aren't
126 // thread-safe.
[email protected]20960e072011-09-20 20:59:01127#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
[email protected]acd78969c2010-12-08 09:49:11128 notifier.AddPrefObserver(pref_name, &obs1_);
129 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
130 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_));
131 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
132 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
[email protected]20960e072011-09-20 20:59:01133#endif
[email protected]acd78969c2010-12-08 09:49:11134
135 // Ensure that we can add the same observer to a different pref.
136 notifier.AddPrefObserver(pref_name2, &obs1_);
137 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
138 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
139 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
140 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
141
142 // Ensure that we can add another observer to the same pref.
143 notifier.AddPrefObserver(pref_name, &obs2_);
144 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
145 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
146 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs2_));
147 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
148
149 // Ensure that we can remove all observers, and that removing a non-existent
150 // observer is harmless.
151 notifier.RemovePrefObserver(pref_name, &obs1_);
152 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
153 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
154 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs2_));
155 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
156
157 notifier.RemovePrefObserver(pref_name, &obs2_);
158 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
159 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
160 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
161 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
162
163 notifier.RemovePrefObserver(pref_name, &obs1_);
164 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
165 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
166 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
167 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
168
169 notifier.RemovePrefObserver(pref_name2, &obs1_);
170 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
171 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_));
172 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
173 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
174}
175
176TEST_F(PrefNotifierTest, FireObservers) {
[email protected]a6a7ced2012-11-01 17:24:18177 TestingPrefNotifierImpl notifier(&pref_service_);
[email protected]acd78969c2010-12-08 09:49:11178 notifier.AddPrefObserver(kChangedPref, &obs1_);
179 notifier.AddPrefObserver(kUnchangedPref, &obs1_);
180
[email protected]a6a7ced2012-11-01 17:24:18181 EXPECT_CALL(obs1_, OnPreferenceChanged(&pref_service_, kChangedPref));
182 EXPECT_CALL(obs2_, OnPreferenceChanged(_, _)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11183 notifier.OnPreferenceChanged(kChangedPref);
184 Mock::VerifyAndClearExpectations(&obs1_);
185 Mock::VerifyAndClearExpectations(&obs2_);
186
187 notifier.AddPrefObserver(kChangedPref, &obs2_);
188 notifier.AddPrefObserver(kUnchangedPref, &obs2_);
189
[email protected]a6a7ced2012-11-01 17:24:18190 EXPECT_CALL(obs1_, OnPreferenceChanged(&pref_service_, kChangedPref));
191 EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref));
[email protected]acd78969c2010-12-08 09:49:11192 notifier.OnPreferenceChanged(kChangedPref);
193 Mock::VerifyAndClearExpectations(&obs1_);
194 Mock::VerifyAndClearExpectations(&obs2_);
195
196 // Make sure removing an observer from one pref doesn't affect anything else.
197 notifier.RemovePrefObserver(kChangedPref, &obs1_);
198
[email protected]a6a7ced2012-11-01 17:24:18199 EXPECT_CALL(obs1_, OnPreferenceChanged(_, _)).Times(0);
200 EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref));
[email protected]acd78969c2010-12-08 09:49:11201 notifier.OnPreferenceChanged(kChangedPref);
202 Mock::VerifyAndClearExpectations(&obs1_);
203 Mock::VerifyAndClearExpectations(&obs2_);
204
205 // Make sure removing an observer entirely doesn't affect anything else.
206 notifier.RemovePrefObserver(kUnchangedPref, &obs1_);
207
[email protected]a6a7ced2012-11-01 17:24:18208 EXPECT_CALL(obs1_, OnPreferenceChanged(_, _)).Times(0);
209 EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref));
[email protected]acd78969c2010-12-08 09:49:11210 notifier.OnPreferenceChanged(kChangedPref);
211 Mock::VerifyAndClearExpectations(&obs1_);
212 Mock::VerifyAndClearExpectations(&obs2_);
213
214 notifier.RemovePrefObserver(kChangedPref, &obs2_);
215 notifier.RemovePrefObserver(kUnchangedPref, &obs2_);
216}
217
218} // namespace