blob: 5db0c6783b6196217632ffedf12722e3c8d1445b [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2010 The Chromium Authors
[email protected]492d2142010-09-10 13:55:182// 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
Avi Drissman12be0312023-01-11 09:16:099#include "base/functional/bind.h"
10#include "base/functional/callback_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() {}
Daniel Cheng67848522018-04-27 22:04:4131 ~MockPrefService() override {}
[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
Sylvain Defresne073318e22019-10-14 11:06:0237// Due to overloads, base::DoNothing() cannot be passed directly to
38// PrefChangeRegistrar::Add() as it is convertible to all callbacks.
39base::RepeatingClosure DoNothingClosure() {
40 return base::DoNothing();
41}
42
[email protected]f2d1f612010-12-09 15:10:1743} // namespace
44
[email protected]492d2142010-09-10 13:55:1845class PrefChangeRegistrarTest : public testing::Test {
46 public:
47 PrefChangeRegistrarTest() {}
dcheng8aef37612014-12-23 02:56:4748 ~PrefChangeRegistrarTest() override {}
[email protected]492d2142010-09-10 13:55:1849
50 protected:
dcheng8aef37612014-12-23 02:56:4751 void SetUp() override;
[email protected]492d2142010-09-10 13:55:1852
[email protected]492d2142010-09-10 13:55:1853 MockPrefService* service() const { return service_.get(); }
54
55 private:
dcheng5f043bc2016-04-22 19:09:0656 std::unique_ptr<MockPrefService> service_;
[email protected]492d2142010-09-10 13:55:1857};
58
59void PrefChangeRegistrarTest::SetUp() {
Peter Boström665b49d2021-04-05 19:29:5860 service_ = std::make_unique<MockPrefService>();
[email protected]492d2142010-09-10 13:55:1861}
62
63TEST_F(PrefChangeRegistrarTest, AddAndRemove) {
64 PrefChangeRegistrar registrar;
65 registrar.Init(service());
66
67 // Test adding.
68 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:2069 AddPrefObserver(Eq(std::string("test.pref.1")), &registrar));
[email protected]492d2142010-09-10 13:55:1870 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:2071 AddPrefObserver(Eq(std::string("test.pref.2")), &registrar));
Sylvain Defresne073318e22019-10-14 11:06:0272 registrar.Add("test.pref.1", DoNothingClosure());
73 registrar.Add("test.pref.2", DoNothingClosure());
[email protected]2fb7dc982010-09-29 12:24:2874 EXPECT_FALSE(registrar.IsEmpty());
[email protected]492d2142010-09-10 13:55:1875
76 // Test removing.
77 Mock::VerifyAndClearExpectations(service());
78 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:2079 RemovePrefObserver(Eq(std::string("test.pref.1")), &registrar));
[email protected]492d2142010-09-10 13:55:1880 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:2081 RemovePrefObserver(Eq(std::string("test.pref.2")), &registrar));
82 registrar.Remove("test.pref.1");
83 registrar.Remove("test.pref.2");
[email protected]2fb7dc982010-09-29 12:24:2884 EXPECT_TRUE(registrar.IsEmpty());
85
86 // Explicitly check the expectations now to make sure that the Removes
87 // worked (rather than the registrar destructor doing the work).
88 Mock::VerifyAndClearExpectations(service());
[email protected]492d2142010-09-10 13:55:1889}
90
91TEST_F(PrefChangeRegistrarTest, AutoRemove) {
92 PrefChangeRegistrar registrar;
93 registrar.Init(service());
94
95 // Setup of auto-remove.
96 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:2097 AddPrefObserver(Eq(std::string("test.pref.1")), &registrar));
Sylvain Defresne073318e22019-10-14 11:06:0298 registrar.Add("test.pref.1", DoNothingClosure());
[email protected]492d2142010-09-10 13:55:1899 Mock::VerifyAndClearExpectations(service());
[email protected]2fb7dc982010-09-29 12:24:28100 EXPECT_FALSE(registrar.IsEmpty());
[email protected]492d2142010-09-10 13:55:18101
102 // Test auto-removing.
103 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:20104 RemovePrefObserver(Eq(std::string("test.pref.1")), &registrar));
[email protected]492d2142010-09-10 13:55:18105}
[email protected]2fb7dc982010-09-29 12:24:28106
107TEST_F(PrefChangeRegistrarTest, RemoveAll) {
108 PrefChangeRegistrar registrar;
109 registrar.Init(service());
110
111 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:20112 AddPrefObserver(Eq(std::string("test.pref.1")), &registrar));
[email protected]2fb7dc982010-09-29 12:24:28113 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:20114 AddPrefObserver(Eq(std::string("test.pref.2")), &registrar));
Sylvain Defresne073318e22019-10-14 11:06:02115 registrar.Add("test.pref.1", DoNothingClosure());
116 registrar.Add("test.pref.2", DoNothingClosure());
[email protected]2fb7dc982010-09-29 12:24:28117 Mock::VerifyAndClearExpectations(service());
118
119 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:20120 RemovePrefObserver(Eq(std::string("test.pref.1")), &registrar));
[email protected]2fb7dc982010-09-29 12:24:28121 EXPECT_CALL(*service(),
[email protected]54ffd94a2012-11-12 15:29:20122 RemovePrefObserver(Eq(std::string("test.pref.2")), &registrar));
[email protected]2fb7dc982010-09-29 12:24:28123 registrar.RemoveAll();
124 EXPECT_TRUE(registrar.IsEmpty());
125
126 // Explicitly check the expectations now to make sure that the RemoveAll
127 // worked (rather than the registrar destructor doing the work).
128 Mock::VerifyAndClearExpectations(service());
129}
[email protected]cb7cec12012-10-15 23:54:59130
[email protected]96a5c342012-12-04 18:14:02131class ObserveSetOfPreferencesTest : public testing::Test {
[email protected]cb7cec12012-10-15 23:54:59132 public:
Daniel Cheng67848522018-04-27 22:04:41133 void SetUp() override {
Peter Boström665b49d2021-04-05 19:29:58134 pref_service_ = std::make_unique<TestingPrefServiceSimple>();
[email protected]b1de2c72013-02-06 02:45:47135 PrefRegistrySimple* registry = pref_service_->registry();
136 registry->RegisterStringPref(kHomePage, "http://google.com");
137 registry->RegisterBooleanPref(kHomePageIsNewTabPage, false);
[email protected]007b3f82013-04-09 08:46:45138 registry->RegisterStringPref(kApplicationLocale, std::string());
[email protected]cb7cec12012-10-15 23:54:59139 }
140
[email protected]96a5c342012-12-04 18:14:02141 PrefChangeRegistrar* CreatePrefChangeRegistrar() {
[email protected]cb7cec12012-10-15 23:54:59142 PrefChangeRegistrar* pref_set = new PrefChangeRegistrar();
143 pref_set->Init(pref_service_.get());
Sylvain Defresne073318e22019-10-14 11:06:02144 pref_set->Add(kHomePage, DoNothingClosure());
145 pref_set->Add(kHomePageIsNewTabPage, DoNothingClosure());
[email protected]cb7cec12012-10-15 23:54:59146 return pref_set;
147 }
148
[email protected]96a5c342012-12-04 18:14:02149 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&));
[email protected]54ffd94a2012-11-12 15:29:20150
dcheng5f043bc2016-04-22 19:09:06151 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
[email protected]cb7cec12012-10-15 23:54:59152};
153
154TEST_F(ObserveSetOfPreferencesTest, IsObserved) {
dcheng5f043bc2016-04-22 19:09:06155 std::unique_ptr<PrefChangeRegistrar> pref_set(CreatePrefChangeRegistrar());
[email protected]5bfdcfd2012-11-22 22:08:24156 EXPECT_TRUE(pref_set->IsObserved(kHomePage));
157 EXPECT_TRUE(pref_set->IsObserved(kHomePageIsNewTabPage));
158 EXPECT_FALSE(pref_set->IsObserved(kApplicationLocale));
[email protected]cb7cec12012-10-15 23:54:59159}
160
[email protected]cb7cec12012-10-15 23:54:59161TEST_F(ObserveSetOfPreferencesTest, Observe) {
162 using testing::_;
163 using testing::Mock;
164
[email protected]96a5c342012-12-04 18:14:02165 PrefChangeRegistrar pref_set;
Sylvain Defresne073318e22019-10-14 11:06:02166 PrefChangeRegistrar::NamedChangeCallback callback =
167 base::BindRepeating(&ObserveSetOfPreferencesTest::OnPreferenceChanged,
168 base::Unretained(this));
[email protected]96a5c342012-12-04 18:14:02169 pref_set.Init(pref_service_.get());
170 pref_set.Add(kHomePage, callback);
171 pref_set.Add(kHomePageIsNewTabPage, callback);
[email protected]cb7cec12012-10-15 23:54:59172
[email protected]96a5c342012-12-04 18:14:02173 EXPECT_CALL(*this, OnPreferenceChanged(kHomePage));
vabr8684c9a2017-03-29 13:14:57174 pref_service_->SetUserPref(kHomePage,
Jinho Bang84b58bd2018-01-01 21:44:48175 std::make_unique<Value>("http://crbug.com"));
[email protected]96a5c342012-12-04 18:14:02176 Mock::VerifyAndClearExpectations(this);
[email protected]cb7cec12012-10-15 23:54:59177
[email protected]96a5c342012-12-04 18:14:02178 EXPECT_CALL(*this, OnPreferenceChanged(kHomePageIsNewTabPage));
vabr8684c9a2017-03-29 13:14:57179 pref_service_->SetUserPref(kHomePageIsNewTabPage,
Jinho Bang84b58bd2018-01-01 21:44:48180 std::make_unique<Value>(true));
[email protected]96a5c342012-12-04 18:14:02181 Mock::VerifyAndClearExpectations(this);
[email protected]cb7cec12012-10-15 23:54:59182
[email protected]96a5c342012-12-04 18:14:02183 EXPECT_CALL(*this, OnPreferenceChanged(_)).Times(0);
vabr8684c9a2017-03-29 13:14:57184 pref_service_->SetUserPref(kApplicationLocale,
Jinho Bang84b58bd2018-01-01 21:44:48185 std::make_unique<Value>("en_US.utf8"));
[email protected]96a5c342012-12-04 18:14:02186 Mock::VerifyAndClearExpectations(this);
[email protected]cb7cec12012-10-15 23:54:59187}
[email protected]7e3ec42c2012-12-16 05:13:21188
189} // namespace base