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.
6 #include "base/bind_helpers.h"
7 #include "base/prefs/pref_change_registrar.h"
8 #include "base/prefs/pref_observer.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
20 const char kHomePage
[] = "homepage";
21 const char kHomePageIsNewTabPage
[] = "homepage_is_newtabpage";
22 const char kApplicationLocale
[] = "intl.app_locale";
24 // A mock provider that allows us to capture pref observer changes.
25 class MockPrefService
: public TestingPrefServiceSimple
{
28 virtual ~MockPrefService() {}
30 MOCK_METHOD2(AddPrefObserver
,
31 void(const char*, PrefObserver
*));
32 MOCK_METHOD2(RemovePrefObserver
,
33 void(const char*, PrefObserver
*));
38 class PrefChangeRegistrarTest
: public testing::Test
{
40 PrefChangeRegistrarTest() {}
41 virtual ~PrefChangeRegistrarTest() {}
44 virtual void SetUp() override
;
46 base::Closure
observer() const {
47 return base::Bind(&base::DoNothing
);
50 MockPrefService
* service() const { return service_
.get(); }
53 scoped_ptr
<MockPrefService
> service_
;
56 void PrefChangeRegistrarTest::SetUp() {
57 service_
.reset(new MockPrefService());
60 TEST_F(PrefChangeRegistrarTest
, AddAndRemove
) {
61 PrefChangeRegistrar registrar
;
62 registrar
.Init(service());
65 EXPECT_CALL(*service(),
66 AddPrefObserver(Eq(std::string("test.pref.1")), ®istrar
));
67 EXPECT_CALL(*service(),
68 AddPrefObserver(Eq(std::string("test.pref.2")), ®istrar
));
69 registrar
.Add("test.pref.1", observer());
70 registrar
.Add("test.pref.2", observer());
71 EXPECT_FALSE(registrar
.IsEmpty());
74 Mock::VerifyAndClearExpectations(service());
75 EXPECT_CALL(*service(),
76 RemovePrefObserver(Eq(std::string("test.pref.1")), ®istrar
));
77 EXPECT_CALL(*service(),
78 RemovePrefObserver(Eq(std::string("test.pref.2")), ®istrar
));
79 registrar
.Remove("test.pref.1");
80 registrar
.Remove("test.pref.2");
81 EXPECT_TRUE(registrar
.IsEmpty());
83 // Explicitly check the expectations now to make sure that the Removes
84 // worked (rather than the registrar destructor doing the work).
85 Mock::VerifyAndClearExpectations(service());
88 TEST_F(PrefChangeRegistrarTest
, AutoRemove
) {
89 PrefChangeRegistrar registrar
;
90 registrar
.Init(service());
92 // Setup of auto-remove.
93 EXPECT_CALL(*service(),
94 AddPrefObserver(Eq(std::string("test.pref.1")), ®istrar
));
95 registrar
.Add("test.pref.1", observer());
96 Mock::VerifyAndClearExpectations(service());
97 EXPECT_FALSE(registrar
.IsEmpty());
99 // Test auto-removing.
100 EXPECT_CALL(*service(),
101 RemovePrefObserver(Eq(std::string("test.pref.1")), ®istrar
));
104 TEST_F(PrefChangeRegistrarTest
, RemoveAll
) {
105 PrefChangeRegistrar registrar
;
106 registrar
.Init(service());
108 EXPECT_CALL(*service(),
109 AddPrefObserver(Eq(std::string("test.pref.1")), ®istrar
));
110 EXPECT_CALL(*service(),
111 AddPrefObserver(Eq(std::string("test.pref.2")), ®istrar
));
112 registrar
.Add("test.pref.1", observer());
113 registrar
.Add("test.pref.2", observer());
114 Mock::VerifyAndClearExpectations(service());
116 EXPECT_CALL(*service(),
117 RemovePrefObserver(Eq(std::string("test.pref.1")), ®istrar
));
118 EXPECT_CALL(*service(),
119 RemovePrefObserver(Eq(std::string("test.pref.2")), ®istrar
));
120 registrar
.RemoveAll();
121 EXPECT_TRUE(registrar
.IsEmpty());
123 // Explicitly check the expectations now to make sure that the RemoveAll
124 // worked (rather than the registrar destructor doing the work).
125 Mock::VerifyAndClearExpectations(service());
128 class ObserveSetOfPreferencesTest
: public testing::Test
{
130 virtual void SetUp() {
131 pref_service_
.reset(new TestingPrefServiceSimple
);
132 PrefRegistrySimple
* registry
= pref_service_
->registry();
133 registry
->RegisterStringPref(kHomePage
, "http://google.com");
134 registry
->RegisterBooleanPref(kHomePageIsNewTabPage
, false);
135 registry
->RegisterStringPref(kApplicationLocale
, std::string());
138 PrefChangeRegistrar
* CreatePrefChangeRegistrar() {
139 PrefChangeRegistrar
* pref_set
= new PrefChangeRegistrar();
140 base::Closure callback
= base::Bind(&base::DoNothing
);
141 pref_set
->Init(pref_service_
.get());
142 pref_set
->Add(kHomePage
, callback
);
143 pref_set
->Add(kHomePageIsNewTabPage
, callback
);
147 MOCK_METHOD1(OnPreferenceChanged
, void(const std::string
&));
149 scoped_ptr
<TestingPrefServiceSimple
> pref_service_
;
152 TEST_F(ObserveSetOfPreferencesTest
, IsObserved
) {
153 scoped_ptr
<PrefChangeRegistrar
> pref_set(CreatePrefChangeRegistrar());
154 EXPECT_TRUE(pref_set
->IsObserved(kHomePage
));
155 EXPECT_TRUE(pref_set
->IsObserved(kHomePageIsNewTabPage
));
156 EXPECT_FALSE(pref_set
->IsObserved(kApplicationLocale
));
159 TEST_F(ObserveSetOfPreferencesTest
, IsManaged
) {
160 scoped_ptr
<PrefChangeRegistrar
> pref_set(CreatePrefChangeRegistrar());
161 EXPECT_FALSE(pref_set
->IsManaged());
162 pref_service_
->SetManagedPref(kHomePage
,
163 new StringValue("http://crbug.com"));
164 EXPECT_TRUE(pref_set
->IsManaged());
165 pref_service_
->SetManagedPref(kHomePageIsNewTabPage
,
166 new FundamentalValue(true));
167 EXPECT_TRUE(pref_set
->IsManaged());
168 pref_service_
->RemoveManagedPref(kHomePage
);
169 EXPECT_TRUE(pref_set
->IsManaged());
170 pref_service_
->RemoveManagedPref(kHomePageIsNewTabPage
);
171 EXPECT_FALSE(pref_set
->IsManaged());
174 TEST_F(ObserveSetOfPreferencesTest
, Observe
) {
178 PrefChangeRegistrar pref_set
;
179 PrefChangeRegistrar::NamedChangeCallback callback
= base::Bind(
180 &ObserveSetOfPreferencesTest::OnPreferenceChanged
,
181 base::Unretained(this));
182 pref_set
.Init(pref_service_
.get());
183 pref_set
.Add(kHomePage
, callback
);
184 pref_set
.Add(kHomePageIsNewTabPage
, callback
);
186 EXPECT_CALL(*this, OnPreferenceChanged(kHomePage
));
187 pref_service_
->SetUserPref(kHomePage
, new StringValue("http://crbug.com"));
188 Mock::VerifyAndClearExpectations(this);
190 EXPECT_CALL(*this, OnPreferenceChanged(kHomePageIsNewTabPage
));
191 pref_service_
->SetUserPref(kHomePageIsNewTabPage
,
192 new FundamentalValue(true));
193 Mock::VerifyAndClearExpectations(this);
195 EXPECT_CALL(*this, OnPreferenceChanged(_
)).Times(0);
196 pref_service_
->SetUserPref(kApplicationLocale
, new StringValue("en_US.utf8"));
197 Mock::VerifyAndClearExpectations(this);