blob: d6de51b097aa634bedb21da0b07e054ab9e8f6e4 [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>
Jan Keitel6288ad6b2024-07-26 09:00:148#include <string_view>
Jinho Bang84b58bd2018-01-01 21:44:489
Avi Drissman12be0312023-01-11 09:16:0910#include "base/functional/bind.h"
11#include "base/functional/callback_helpers.h"
brettwf00b9b42016-02-01 22:11:3812#include "components/prefs/pref_observer.h"
13#include "components/prefs/pref_registry_simple.h"
14#include "components/prefs/testing_pref_service.h"
[email protected]492d2142010-09-10 13:55:1815#include "testing/gmock/include/gmock/gmock.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
[email protected]7e3ec42c2012-12-16 05:13:2118namespace base {
[email protected]f2d1f612010-12-09 15:10:1719namespace {
20
Jan Keitel6288ad6b2024-07-26 09:00:1421using testing::Mock;
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:
Jan Keitel0a58e902024-07-10 15:54:5930 MockPrefService() = default;
31 ~MockPrefService() override = default;
[email protected]492d2142010-09-10 13:55:1832
Jan Keitel0a58e902024-07-10 15:54:5933 MOCK_METHOD(void,
34 AddPrefObserver,
Jan Keitel6288ad6b2024-07-26 09:00:1435 (std::string_view, PrefObserver*),
Jan Keitel0a58e902024-07-10 15:54:5936 (override));
37 MOCK_METHOD(void,
38 RemovePrefObserver,
Jan Keitel6288ad6b2024-07-26 09:00:1439 (std::string_view, PrefObserver*),
Jan Keitel0a58e902024-07-10 15:54:5940 (override));
[email protected]492d2142010-09-10 13:55:1841};
42
Sylvain Defresne073318e22019-10-14 11:06:0243// Due to overloads, base::DoNothing() cannot be passed directly to
44// PrefChangeRegistrar::Add() as it is convertible to all callbacks.
45base::RepeatingClosure DoNothingClosure() {
46 return base::DoNothing();
47}
48
[email protected]f2d1f612010-12-09 15:10:1749} // namespace
50
[email protected]492d2142010-09-10 13:55:1851class PrefChangeRegistrarTest : public testing::Test {
52 public:
Jan Keitel0a58e902024-07-10 15:54:5953 PrefChangeRegistrarTest() = default;
54 ~PrefChangeRegistrarTest() override = default;
[email protected]492d2142010-09-10 13:55:1855
56 protected:
dcheng8aef37612014-12-23 02:56:4757 void SetUp() override;
[email protected]492d2142010-09-10 13:55:1858
[email protected]492d2142010-09-10 13:55:1859 MockPrefService* service() const { return service_.get(); }
60
61 private:
dcheng5f043bc2016-04-22 19:09:0662 std::unique_ptr<MockPrefService> service_;
[email protected]492d2142010-09-10 13:55:1863};
64
65void PrefChangeRegistrarTest::SetUp() {
Peter Boström665b49d2021-04-05 19:29:5866 service_ = std::make_unique<MockPrefService>();
[email protected]492d2142010-09-10 13:55:1867}
68
69TEST_F(PrefChangeRegistrarTest, AddAndRemove) {
70 PrefChangeRegistrar registrar;
71 registrar.Init(service());
72
73 // Test adding.
Jan Keitel6288ad6b2024-07-26 09:00:1474 EXPECT_CALL(*service(), AddPrefObserver("test.pref.1", &registrar));
75 EXPECT_CALL(*service(), AddPrefObserver("test.pref.2", &registrar));
Sylvain Defresne073318e22019-10-14 11:06:0276 registrar.Add("test.pref.1", DoNothingClosure());
77 registrar.Add("test.pref.2", DoNothingClosure());
[email protected]2fb7dc982010-09-29 12:24:2878 EXPECT_FALSE(registrar.IsEmpty());
[email protected]492d2142010-09-10 13:55:1879
80 // Test removing.
81 Mock::VerifyAndClearExpectations(service());
Jan Keitel6288ad6b2024-07-26 09:00:1482 EXPECT_CALL(*service(), RemovePrefObserver("test.pref.1", &registrar));
83 EXPECT_CALL(*service(), RemovePrefObserver("test.pref.2", &registrar));
[email protected]54ffd94a2012-11-12 15:29:2084 registrar.Remove("test.pref.1");
85 registrar.Remove("test.pref.2");
[email protected]2fb7dc982010-09-29 12:24:2886 EXPECT_TRUE(registrar.IsEmpty());
87
88 // Explicitly check the expectations now to make sure that the Removes
89 // worked (rather than the registrar destructor doing the work).
90 Mock::VerifyAndClearExpectations(service());
[email protected]492d2142010-09-10 13:55:1891}
92
93TEST_F(PrefChangeRegistrarTest, AutoRemove) {
94 PrefChangeRegistrar registrar;
95 registrar.Init(service());
96
97 // Setup of auto-remove.
Jan Keitel6288ad6b2024-07-26 09:00:1498 EXPECT_CALL(*service(), AddPrefObserver("test.pref.1", &registrar));
Sylvain Defresne073318e22019-10-14 11:06:0299 registrar.Add("test.pref.1", DoNothingClosure());
[email protected]492d2142010-09-10 13:55:18100 Mock::VerifyAndClearExpectations(service());
[email protected]2fb7dc982010-09-29 12:24:28101 EXPECT_FALSE(registrar.IsEmpty());
[email protected]492d2142010-09-10 13:55:18102
103 // Test auto-removing.
Jan Keitel6288ad6b2024-07-26 09:00:14104 EXPECT_CALL(*service(), RemovePrefObserver("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
Jan Keitel6288ad6b2024-07-26 09:00:14111 EXPECT_CALL(*service(), AddPrefObserver("test.pref.1", &registrar));
112 EXPECT_CALL(*service(), AddPrefObserver("test.pref.2", &registrar));
Sylvain Defresne073318e22019-10-14 11:06:02113 registrar.Add("test.pref.1", DoNothingClosure());
114 registrar.Add("test.pref.2", DoNothingClosure());
[email protected]2fb7dc982010-09-29 12:24:28115 Mock::VerifyAndClearExpectations(service());
116
Jan Keitel6288ad6b2024-07-26 09:00:14117 EXPECT_CALL(*service(), RemovePrefObserver("test.pref.1", &registrar));
118 EXPECT_CALL(*service(), RemovePrefObserver("test.pref.2", &registrar));
[email protected]2fb7dc982010-09-29 12:24:28119 registrar.RemoveAll();
120 EXPECT_TRUE(registrar.IsEmpty());
121
122 // Explicitly check the expectations now to make sure that the RemoveAll
123 // worked (rather than the registrar destructor doing the work).
124 Mock::VerifyAndClearExpectations(service());
125}
[email protected]cb7cec12012-10-15 23:54:59126
[email protected]96a5c342012-12-04 18:14:02127class ObserveSetOfPreferencesTest : public testing::Test {
[email protected]cb7cec12012-10-15 23:54:59128 public:
Daniel Cheng67848522018-04-27 22:04:41129 void SetUp() override {
Peter Boström665b49d2021-04-05 19:29:58130 pref_service_ = std::make_unique<TestingPrefServiceSimple>();
[email protected]b1de2c72013-02-06 02:45:47131 PrefRegistrySimple* registry = pref_service_->registry();
132 registry->RegisterStringPref(kHomePage, "http://google.com");
133 registry->RegisterBooleanPref(kHomePageIsNewTabPage, false);
[email protected]007b3f82013-04-09 08:46:45134 registry->RegisterStringPref(kApplicationLocale, std::string());
[email protected]cb7cec12012-10-15 23:54:59135 }
136
[email protected]96a5c342012-12-04 18:14:02137 PrefChangeRegistrar* CreatePrefChangeRegistrar() {
[email protected]cb7cec12012-10-15 23:54:59138 PrefChangeRegistrar* pref_set = new PrefChangeRegistrar();
139 pref_set->Init(pref_service_.get());
Sylvain Defresne073318e22019-10-14 11:06:02140 pref_set->Add(kHomePage, DoNothingClosure());
141 pref_set->Add(kHomePageIsNewTabPage, DoNothingClosure());
[email protected]cb7cec12012-10-15 23:54:59142 return pref_set;
143 }
144
[email protected]96a5c342012-12-04 18:14:02145 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&));
[email protected]54ffd94a2012-11-12 15:29:20146
dcheng5f043bc2016-04-22 19:09:06147 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
[email protected]cb7cec12012-10-15 23:54:59148};
149
150TEST_F(ObserveSetOfPreferencesTest, IsObserved) {
dcheng5f043bc2016-04-22 19:09:06151 std::unique_ptr<PrefChangeRegistrar> pref_set(CreatePrefChangeRegistrar());
[email protected]5bfdcfd2012-11-22 22:08:24152 EXPECT_TRUE(pref_set->IsObserved(kHomePage));
153 EXPECT_TRUE(pref_set->IsObserved(kHomePageIsNewTabPage));
154 EXPECT_FALSE(pref_set->IsObserved(kApplicationLocale));
[email protected]cb7cec12012-10-15 23:54:59155}
156
[email protected]cb7cec12012-10-15 23:54:59157TEST_F(ObserveSetOfPreferencesTest, Observe) {
158 using testing::_;
159 using testing::Mock;
160
[email protected]96a5c342012-12-04 18:14:02161 PrefChangeRegistrar pref_set;
Sylvain Defresne073318e22019-10-14 11:06:02162 PrefChangeRegistrar::NamedChangeCallback callback =
163 base::BindRepeating(&ObserveSetOfPreferencesTest::OnPreferenceChanged,
164 base::Unretained(this));
[email protected]96a5c342012-12-04 18:14:02165 pref_set.Init(pref_service_.get());
166 pref_set.Add(kHomePage, callback);
167 pref_set.Add(kHomePageIsNewTabPage, callback);
[email protected]cb7cec12012-10-15 23:54:59168
[email protected]96a5c342012-12-04 18:14:02169 EXPECT_CALL(*this, OnPreferenceChanged(kHomePage));
vabr8684c9a2017-03-29 13:14:57170 pref_service_->SetUserPref(kHomePage,
Jinho Bang84b58bd2018-01-01 21:44:48171 std::make_unique<Value>("http://crbug.com"));
[email protected]96a5c342012-12-04 18:14:02172 Mock::VerifyAndClearExpectations(this);
[email protected]cb7cec12012-10-15 23:54:59173
[email protected]96a5c342012-12-04 18:14:02174 EXPECT_CALL(*this, OnPreferenceChanged(kHomePageIsNewTabPage));
vabr8684c9a2017-03-29 13:14:57175 pref_service_->SetUserPref(kHomePageIsNewTabPage,
Jinho Bang84b58bd2018-01-01 21:44:48176 std::make_unique<Value>(true));
[email protected]96a5c342012-12-04 18:14:02177 Mock::VerifyAndClearExpectations(this);
[email protected]cb7cec12012-10-15 23:54:59178
[email protected]96a5c342012-12-04 18:14:02179 EXPECT_CALL(*this, OnPreferenceChanged(_)).Times(0);
vabr8684c9a2017-03-29 13:14:57180 pref_service_->SetUserPref(kApplicationLocale,
Jinho Bang84b58bd2018-01-01 21:44:48181 std::make_unique<Value>("en_US.utf8"));
[email protected]96a5c342012-12-04 18:14:02182 Mock::VerifyAndClearExpectations(this);
[email protected]cb7cec12012-10-15 23:54:59183}
[email protected]7e3ec42c2012-12-16 05:13:21184
185} // namespace base