blob: 4027618a460c00027e2c84575e895ff7909c1587 [file] [log] [blame]
[email protected]492d2142010-09-10 13:55:181// 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
vabr8684c9a2017-03-29 13:14:575#include "components/prefs/pref_change_registrar.h"
6
Jinho Bang84b58bd2018-01-01 21:44:487#include <memory>
8
[email protected]96a5c342012-12-04 18:14:029#include "base/bind.h"
10#include "base/bind_helpers.h"
brettwf00b9b42016-02-01 22:11:3811#include "components/prefs/pref_observer.h"
12#include "components/prefs/pref_registry_simple.h"
13#include "components/prefs/testing_pref_service.h"
[email protected]492d2142010-09-10 13:55:1814#include "testing/gmock/include/gmock/gmock.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17using testing::Mock;
18using testing::Eq;
19
[email protected]7e3ec42c2012-12-16 05:13:2120namespace base {
[email protected]f2d1f612010-12-09 15:10:1721namespace {
22
[email protected]5bfdcfd2012-11-22 22:08:2423const char kHomePage[] = "homepage";
24const char kHomePageIsNewTabPage[] = "homepage_is_newtabpage";
25const char kApplicationLocale[] = "intl.app_locale";
26
[email protected]492d2142010-09-10 13:55:1827// A mock provider that allows us to capture pref observer changes.
[email protected]5b199522012-12-22 17:24:4428class MockPrefService : public TestingPrefServiceSimple {
[email protected]492d2142010-09-10 13:55:1829 public:
30 MockPrefService() {}
[email protected]ceac9972011-01-13 01:51:1431 virtual ~MockPrefService() {}
[email protected]492d2142010-09-10 13:55:1832
georgesak7da6e9d2014-12-03 01:10:2933 MOCK_METHOD2(AddPrefObserver, void(const std::string&, PrefObserver*));
34 MOCK_METHOD2(RemovePrefObserver, void(const std::string&, PrefObserver*));
[email protected]492d2142010-09-10 13:55:1835};
36
[email protected]f2d1f612010-12-09 15:10:1737} // namespace
38
[email protected]492d2142010-09-10 13:55:1839class PrefChangeRegistrarTest : public testing::Test {
40 public:
41 PrefChangeRegistrarTest() {}
dcheng8aef37612014-12-23 02:56:4742 ~PrefChangeRegistrarTest() override {}
[email protected]492d2142010-09-10 13:55:1843
44 protected:
dcheng8aef37612014-12-23 02:56:4745 void SetUp() override;
[email protected]492d2142010-09-10 13:55:1846
[email protected]96a5c342012-12-04 18:14:0247 base::Closure observer() const {
48 return base::Bind(&base::DoNothing);
49 }
50
[email protected]492d2142010-09-10 13:55:1851 MockPrefService* service() const { return service_.get(); }
52
53 private:
dcheng5f043bc2016-04-22 19:09:0654 std::unique_ptr<MockPrefService> service_;
[email protected]492d2142010-09-10 13:55:1855};
56
57void PrefChangeRegistrarTest::SetUp() {
58 service_.reset(new MockPrefService());
[email protected]492d2142010-09-10 13:55:1859}
60
61TEST_F(PrefChangeRegistrarTest, AddAndRemove) {
62 PrefChangeRegistrar registrar;
63 registrar.Init(service());
64
65 // Test adding.
66 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:2067 AddPrefObserver(Eq(std::string("test.pref.1")), &registrar));
[email protected]492d2142010-09-10 13:55:1868 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:2069 AddPrefObserver(Eq(std::string("test.pref.2")), &registrar));
[email protected]492d2142010-09-10 13:55:1870 registrar.Add("test.pref.1", observer());
71 registrar.Add("test.pref.2", observer());
[email protected]2fb7dc982010-09-29 12:24:2872 EXPECT_FALSE(registrar.IsEmpty());
[email protected]492d2142010-09-10 13:55:1873
74 // Test removing.
75 Mock::VerifyAndClearExpectations(service());
76 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:2077 RemovePrefObserver(Eq(std::string("test.pref.1")), &registrar));
[email protected]492d2142010-09-10 13:55:1878 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:2079 RemovePrefObserver(Eq(std::string("test.pref.2")), &registrar));
80 registrar.Remove("test.pref.1");
81 registrar.Remove("test.pref.2");
[email protected]2fb7dc982010-09-29 12:24:2882 EXPECT_TRUE(registrar.IsEmpty());
83
84 // Explicitly check the expectations now to make sure that the Removes
85 // worked (rather than the registrar destructor doing the work).
86 Mock::VerifyAndClearExpectations(service());
[email protected]492d2142010-09-10 13:55:1887}
88
89TEST_F(PrefChangeRegistrarTest, AutoRemove) {
90 PrefChangeRegistrar registrar;
91 registrar.Init(service());
92
93 // Setup of auto-remove.
94 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:2095 AddPrefObserver(Eq(std::string("test.pref.1")), &registrar));
[email protected]492d2142010-09-10 13:55:1896 registrar.Add("test.pref.1", observer());
97 Mock::VerifyAndClearExpectations(service());
[email protected]2fb7dc982010-09-29 12:24:2898 EXPECT_FALSE(registrar.IsEmpty());
[email protected]492d2142010-09-10 13:55:1899
100 // Test auto-removing.
101 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:20102 RemovePrefObserver(Eq(std::string("test.pref.1")), &registrar));
[email protected]492d2142010-09-10 13:55:18103}
[email protected]2fb7dc982010-09-29 12:24:28104
105TEST_F(PrefChangeRegistrarTest, RemoveAll) {
106 PrefChangeRegistrar registrar;
107 registrar.Init(service());
108
109 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:20110 AddPrefObserver(Eq(std::string("test.pref.1")), &registrar));
[email protected]2fb7dc982010-09-29 12:24:28111 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:20112 AddPrefObserver(Eq(std::string("test.pref.2")), &registrar));
[email protected]2fb7dc982010-09-29 12:24:28113 registrar.Add("test.pref.1", observer());
114 registrar.Add("test.pref.2", observer());
115 Mock::VerifyAndClearExpectations(service());
116
117 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:20118 RemovePrefObserver(Eq(std::string("test.pref.1")), &registrar));
[email protected]2fb7dc982010-09-29 12:24:28119 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:20120 RemovePrefObserver(Eq(std::string("test.pref.2")), &registrar));
[email protected]2fb7dc982010-09-29 12:24:28121 registrar.RemoveAll();
122 EXPECT_TRUE(registrar.IsEmpty());
123
124 // Explicitly check the expectations now to make sure that the RemoveAll
125 // worked (rather than the registrar destructor doing the work).
126 Mock::VerifyAndClearExpectations(service());
127}
[email protected]cb7cec12012-10-15 23:54:59128
[email protected]96a5c342012-12-04 18:14:02129class ObserveSetOfPreferencesTest : public testing::Test {
[email protected]cb7cec12012-10-15 23:54:59130 public:
131 virtual void SetUp() {
[email protected]5b199522012-12-22 17:24:44132 pref_service_.reset(new TestingPrefServiceSimple);
[email protected]b1de2c72013-02-06 02:45:47133 PrefRegistrySimple* registry = pref_service_->registry();
134 registry->RegisterStringPref(kHomePage, "http://google.com");
135 registry->RegisterBooleanPref(kHomePageIsNewTabPage, false);
[email protected]007b3f82013-04-09 08:46:45136 registry->RegisterStringPref(kApplicationLocale, std::string());
[email protected]cb7cec12012-10-15 23:54:59137 }
138
[email protected]96a5c342012-12-04 18:14:02139 PrefChangeRegistrar* CreatePrefChangeRegistrar() {
[email protected]cb7cec12012-10-15 23:54:59140 PrefChangeRegistrar* pref_set = new PrefChangeRegistrar();
[email protected]96a5c342012-12-04 18:14:02141 base::Closure callback = base::Bind(&base::DoNothing);
[email protected]cb7cec12012-10-15 23:54:59142 pref_set->Init(pref_service_.get());
[email protected]96a5c342012-12-04 18:14:02143 pref_set->Add(kHomePage, callback);
144 pref_set->Add(kHomePageIsNewTabPage, callback);
[email protected]cb7cec12012-10-15 23:54:59145 return pref_set;
146 }
147
[email protected]96a5c342012-12-04 18:14:02148 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&));
[email protected]54ffd94a2012-11-12 15:29:20149
dcheng5f043bc2016-04-22 19:09:06150 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
[email protected]cb7cec12012-10-15 23:54:59151};
152
153TEST_F(ObserveSetOfPreferencesTest, IsObserved) {
dcheng5f043bc2016-04-22 19:09:06154 std::unique_ptr<PrefChangeRegistrar> pref_set(CreatePrefChangeRegistrar());
[email protected]5bfdcfd2012-11-22 22:08:24155 EXPECT_TRUE(pref_set->IsObserved(kHomePage));
156 EXPECT_TRUE(pref_set->IsObserved(kHomePageIsNewTabPage));
157 EXPECT_FALSE(pref_set->IsObserved(kApplicationLocale));
[email protected]cb7cec12012-10-15 23:54:59158}
159
160TEST_F(ObserveSetOfPreferencesTest, IsManaged) {
dcheng5f043bc2016-04-22 19:09:06161 std::unique_ptr<PrefChangeRegistrar> pref_set(CreatePrefChangeRegistrar());
[email protected]cb7cec12012-10-15 23:54:59162 EXPECT_FALSE(pref_set->IsManaged());
vabr8684c9a2017-03-29 13:14:57163 pref_service_->SetManagedPref(kHomePage,
Jinho Bang84b58bd2018-01-01 21:44:48164 std::make_unique<Value>("http://crbug.com"));
[email protected]cb7cec12012-10-15 23:54:59165 EXPECT_TRUE(pref_set->IsManaged());
vabr8684c9a2017-03-29 13:14:57166 pref_service_->SetManagedPref(kHomePageIsNewTabPage,
Jinho Bang84b58bd2018-01-01 21:44:48167 std::make_unique<Value>(true));
[email protected]cb7cec12012-10-15 23:54:59168 EXPECT_TRUE(pref_set->IsManaged());
[email protected]5bfdcfd2012-11-22 22:08:24169 pref_service_->RemoveManagedPref(kHomePage);
[email protected]cb7cec12012-10-15 23:54:59170 EXPECT_TRUE(pref_set->IsManaged());
[email protected]5bfdcfd2012-11-22 22:08:24171 pref_service_->RemoveManagedPref(kHomePageIsNewTabPage);
[email protected]cb7cec12012-10-15 23:54:59172 EXPECT_FALSE(pref_set->IsManaged());
173}
174
[email protected]cb7cec12012-10-15 23:54:59175TEST_F(ObserveSetOfPreferencesTest, Observe) {
176 using testing::_;
177 using testing::Mock;
178
[email protected]96a5c342012-12-04 18:14:02179 PrefChangeRegistrar pref_set;
180 PrefChangeRegistrar::NamedChangeCallback callback = base::Bind(
181 &ObserveSetOfPreferencesTest::OnPreferenceChanged,
182 base::Unretained(this));
183 pref_set.Init(pref_service_.get());
184 pref_set.Add(kHomePage, callback);
185 pref_set.Add(kHomePageIsNewTabPage, callback);
[email protected]cb7cec12012-10-15 23:54:59186
[email protected]96a5c342012-12-04 18:14:02187 EXPECT_CALL(*this, OnPreferenceChanged(kHomePage));
vabr8684c9a2017-03-29 13:14:57188 pref_service_->SetUserPref(kHomePage,
Jinho Bang84b58bd2018-01-01 21:44:48189 std::make_unique<Value>("http://crbug.com"));
[email protected]96a5c342012-12-04 18:14:02190 Mock::VerifyAndClearExpectations(this);
[email protected]cb7cec12012-10-15 23:54:59191
[email protected]96a5c342012-12-04 18:14:02192 EXPECT_CALL(*this, OnPreferenceChanged(kHomePageIsNewTabPage));
vabr8684c9a2017-03-29 13:14:57193 pref_service_->SetUserPref(kHomePageIsNewTabPage,
Jinho Bang84b58bd2018-01-01 21:44:48194 std::make_unique<Value>(true));
[email protected]96a5c342012-12-04 18:14:02195 Mock::VerifyAndClearExpectations(this);
[email protected]cb7cec12012-10-15 23:54:59196
[email protected]96a5c342012-12-04 18:14:02197 EXPECT_CALL(*this, OnPreferenceChanged(_)).Times(0);
vabr8684c9a2017-03-29 13:14:57198 pref_service_->SetUserPref(kApplicationLocale,
Jinho Bang84b58bd2018-01-01 21:44:48199 std::make_unique<Value>("en_US.utf8"));
[email protected]96a5c342012-12-04 18:14:02200 Mock::VerifyAndClearExpectations(this);
[email protected]cb7cec12012-10-15 23:54:59201}
[email protected]7e3ec42c2012-12-16 05:13:21202
203} // namespace base