1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/prefs/pref_member.h"
10 #include "base/location.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/task/sequenced_task_runner.h"
14 #include "base/task/thread_pool.h"
15 #include "base/test/task_environment.h"
16 #include "components/prefs/pref_registry_simple.h"
17 #include "components/prefs/testing_pref_service.h"
18 #include "testing/gtest/include/gtest/gtest.h"
22 const char kBoolPref[] = "bool";
23 const char kIntPref[] = "int";
24 const char kDoublePref[] = "double";
25 const char kStringPref[] = "string";
26 const char kStringListPref[] = "string_list";
28 void RegisterTestPrefs(PrefRegistrySimple* registry) {
29 registry->RegisterBooleanPref(kBoolPref, false);
30 registry->RegisterIntegerPref(kIntPref, 0);
31 registry->RegisterDoublePref(kDoublePref, 0.0);
32 registry->RegisterStringPref(kStringPref, "default");
33 registry->RegisterListPref(kStringListPref);
36 class GetPrefValueHelper
37 : public base::RefCountedThreadSafe<GetPrefValueHelper> {
41 task_runner_(base::ThreadPool::CreateSequencedTaskRunner({})) {}
43 void Init(const std::string& pref_name, PrefService* prefs) {
44 pref_.Init(pref_name, prefs);
45 pref_.MoveToSequence(task_runner_);
53 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::MANUAL,
54 base::WaitableEvent::InitialState::NOT_SIGNALED);
55 ASSERT_TRUE(task_runner_->PostTask(
57 base::BindOnce(&GetPrefValueHelper::GetPrefValue, this, &event)));
61 bool value() { return value_; }
64 friend class base::RefCountedThreadSafe<GetPrefValueHelper>;
65 ~GetPrefValueHelper() {}
67 void GetPrefValue(base::WaitableEvent* event) {
68 value_ = pref_.GetValue();
72 BooleanPrefMember pref_;
75 // The sequence |pref_| runs on.
76 scoped_refptr<base::SequencedTaskRunner> task_runner_;
79 class PrefMemberTestClass {
81 explicit PrefMemberTestClass(PrefService* prefs)
82 : observe_cnt_(0), prefs_(prefs) {
83 str_.Init(kStringPref, prefs,
84 base::BindRepeating(&PrefMemberTestClass::OnPreferenceChanged,
85 base::Unretained(this)));
88 void OnPreferenceChanged(const std::string& pref_name) {
89 EXPECT_EQ(pref_name, kStringPref);
90 EXPECT_EQ(str_.GetValue(), prefs_->GetString(kStringPref));
91 EXPECT_EQ(str_.IsDefaultValue(),
92 prefs_->FindPreference(kStringPref)->IsDefaultValue());
96 StringPrefMember str_;
100 raw_ptr<PrefService> prefs_;
103 } // anonymous namespace
105 class PrefMemberTest : public testing::Test {
106 base::test::TaskEnvironment task_environment_;
109 TEST_F(PrefMemberTest, BasicGetAndSet) {
110 TestingPrefServiceSimple prefs;
111 RegisterTestPrefs(prefs.registry());
114 BooleanPrefMember boolean;
115 boolean.Init(kBoolPref, &prefs);
117 // Check the defaults
118 EXPECT_FALSE(prefs.GetBoolean(kBoolPref));
119 EXPECT_FALSE(boolean.GetValue());
120 EXPECT_FALSE(*boolean);
121 EXPECT_TRUE(boolean.IsDefaultValue());
123 // Try changing through the member variable.
124 boolean.SetValue(true);
125 EXPECT_TRUE(boolean.GetValue());
126 EXPECT_TRUE(prefs.GetBoolean(kBoolPref));
127 EXPECT_TRUE(*boolean);
128 EXPECT_FALSE(boolean.IsDefaultValue());
130 // Try changing back through the pref.
131 prefs.SetBoolean(kBoolPref, false);
132 EXPECT_FALSE(prefs.GetBoolean(kBoolPref));
133 EXPECT_FALSE(boolean.GetValue());
134 EXPECT_FALSE(*boolean);
135 EXPECT_FALSE(boolean.IsDefaultValue());
138 IntegerPrefMember integer;
139 integer.Init(kIntPref, &prefs);
141 // Check the defaults
142 EXPECT_EQ(0, prefs.GetInteger(kIntPref));
143 EXPECT_EQ(0, integer.GetValue());
144 EXPECT_EQ(0, *integer);
145 EXPECT_TRUE(integer.IsDefaultValue());
147 // Try changing through the member variable.
149 EXPECT_EQ(5, integer.GetValue());
150 EXPECT_EQ(5, prefs.GetInteger(kIntPref));
151 EXPECT_EQ(5, *integer);
152 EXPECT_FALSE(integer.IsDefaultValue());
154 // Try changing back through the pref.
155 prefs.SetInteger(kIntPref, 2);
156 EXPECT_EQ(2, prefs.GetInteger(kIntPref));
157 EXPECT_EQ(2, integer.GetValue());
158 EXPECT_EQ(2, *integer);
159 EXPECT_FALSE(integer.IsDefaultValue());
162 DoublePrefMember double_member;
163 double_member.Init(kDoublePref, &prefs);
165 // Check the defaults
166 EXPECT_EQ(0.0, prefs.GetDouble(kDoublePref));
167 EXPECT_EQ(0.0, double_member.GetValue());
168 EXPECT_EQ(0.0, *double_member);
169 EXPECT_TRUE(double_member.IsDefaultValue());
171 // Try changing through the member variable.
172 double_member.SetValue(1.0);
173 EXPECT_EQ(1.0, double_member.GetValue());
174 EXPECT_EQ(1.0, prefs.GetDouble(kDoublePref));
175 EXPECT_EQ(1.0, *double_member);
176 EXPECT_FALSE(double_member.IsDefaultValue());
178 // Try changing back through the pref.
179 prefs.SetDouble(kDoublePref, 3.0);
180 EXPECT_EQ(3.0, prefs.GetDouble(kDoublePref));
181 EXPECT_EQ(3.0, double_member.GetValue());
182 EXPECT_EQ(3.0, *double_member);
183 EXPECT_FALSE(double_member.IsDefaultValue());
186 StringPrefMember string;
187 string.Init(kStringPref, &prefs);
189 // Check the defaults
190 EXPECT_EQ("default", prefs.GetString(kStringPref));
191 EXPECT_EQ("default", string.GetValue());
192 EXPECT_EQ("default", *string);
193 EXPECT_TRUE(string.IsDefaultValue());
195 // Try changing through the member variable.
196 string.SetValue("foo");
197 EXPECT_EQ("foo", string.GetValue());
198 EXPECT_EQ("foo", prefs.GetString(kStringPref));
199 EXPECT_EQ("foo", *string);
200 EXPECT_FALSE(string.IsDefaultValue());
202 // Try changing back through the pref.
203 prefs.SetString(kStringPref, "bar");
204 EXPECT_EQ("bar", prefs.GetString(kStringPref));
205 EXPECT_EQ("bar", string.GetValue());
206 EXPECT_EQ("bar", *string);
207 EXPECT_FALSE(string.IsDefaultValue());
210 base::Value::List expected_list;
211 std::vector<std::string> expected_vector;
212 StringListPrefMember string_list;
213 string_list.Init(kStringListPref, &prefs);
215 // Check the defaults
216 EXPECT_EQ(expected_list, prefs.GetList(kStringListPref));
217 EXPECT_EQ(expected_vector, string_list.GetValue());
218 EXPECT_EQ(expected_vector, *string_list);
219 EXPECT_TRUE(string_list.IsDefaultValue());
221 // Try changing through the pref member.
222 expected_list.Append("foo");
223 expected_vector.push_back("foo");
224 string_list.SetValue(expected_vector);
226 EXPECT_EQ(expected_list, prefs.GetList(kStringListPref));
227 EXPECT_EQ(expected_vector, string_list.GetValue());
228 EXPECT_EQ(expected_vector, *string_list);
229 EXPECT_FALSE(string_list.IsDefaultValue());
231 // Try adding through the pref.
232 expected_list.Append("bar");
233 expected_vector.push_back("bar");
234 prefs.SetList(kStringListPref, expected_list.Clone());
236 EXPECT_EQ(expected_list, prefs.GetList(kStringListPref));
237 EXPECT_EQ(expected_vector, string_list.GetValue());
238 EXPECT_EQ(expected_vector, *string_list);
239 EXPECT_FALSE(string_list.IsDefaultValue());
241 // Try removing through the pref.
242 expected_list.erase(expected_list.begin());
243 expected_vector.erase(expected_vector.begin());
244 prefs.SetList(kStringListPref, expected_list.Clone());
246 EXPECT_EQ(expected_list, prefs.GetList(kStringListPref));
247 EXPECT_EQ(expected_vector, string_list.GetValue());
248 EXPECT_EQ(expected_vector, *string_list);
249 EXPECT_FALSE(string_list.IsDefaultValue());
252 TEST_F(PrefMemberTest, InvalidList) {
253 // Set the vector to an initial good value.
254 std::vector<std::string> expected_vector;
255 expected_vector.push_back("foo");
257 // Try to add a valid list first.
258 base::Value list(base::Value::Type::LIST);
260 std::vector<std::string> vector;
261 EXPECT_TRUE(subtle::PrefMemberVectorStringUpdate(list, &vector));
262 EXPECT_EQ(expected_vector, vector);
264 // Now try to add an invalid list. |vector| should not be changed.
266 EXPECT_FALSE(subtle::PrefMemberVectorStringUpdate(list, &vector));
267 EXPECT_EQ(expected_vector, vector);
270 TEST_F(PrefMemberTest, TwoPrefs) {
271 // Make sure two DoublePrefMembers stay in sync.
272 TestingPrefServiceSimple prefs;
273 RegisterTestPrefs(prefs.registry());
275 DoublePrefMember pref1;
276 pref1.Init(kDoublePref, &prefs);
277 DoublePrefMember pref2;
278 pref2.Init(kDoublePref, &prefs);
281 EXPECT_EQ(2.3, *pref2);
284 EXPECT_EQ(3.5, *pref1);
286 prefs.SetDouble(kDoublePref, 4.2);
287 EXPECT_EQ(4.2, *pref1);
288 EXPECT_EQ(4.2, *pref2);
291 TEST_F(PrefMemberTest, Observer) {
292 TestingPrefServiceSimple prefs;
293 RegisterTestPrefs(prefs.registry());
295 PrefMemberTestClass test_obj(&prefs);
296 EXPECT_EQ("default", *test_obj.str_);
297 EXPECT_TRUE(test_obj.str_.IsDefaultValue());
299 // Changing the pref from the default value to an explicitly-set version of
300 // the same value fires the observer. The caller may be sensitive to
302 prefs.SetString(kStringPref, "default");
303 EXPECT_EQ("default", *test_obj.str_);
304 EXPECT_EQ(1, test_obj.observe_cnt_);
305 EXPECT_FALSE(test_obj.str_.IsDefaultValue());
307 // Calling SetValue should not fire the observer.
308 test_obj.str_.SetValue("hello");
309 EXPECT_EQ(1, test_obj.observe_cnt_);
310 EXPECT_EQ("hello", prefs.GetString(kStringPref));
312 // Changing the pref does fire the observer.
313 prefs.SetString(kStringPref, "world");
314 EXPECT_EQ(2, test_obj.observe_cnt_);
315 EXPECT_EQ("world", *(test_obj.str_));
317 // Not changing the value should not fire the observer.
318 prefs.SetString(kStringPref, "world");
319 EXPECT_EQ(2, test_obj.observe_cnt_);
320 EXPECT_EQ("world", *(test_obj.str_));
322 prefs.SetString(kStringPref, "hello");
323 EXPECT_EQ(3, test_obj.observe_cnt_);
324 EXPECT_EQ("hello", prefs.GetString(kStringPref));
327 TEST_F(PrefMemberTest, NoInit) {
328 // Make sure not calling Init on a PrefMember doesn't cause problems.
329 IntegerPrefMember pref;
332 TEST_F(PrefMemberTest, MoveToSequence) {
333 TestingPrefServiceSimple prefs;
334 scoped_refptr<GetPrefValueHelper> helper(new GetPrefValueHelper());
335 RegisterTestPrefs(prefs.registry());
336 helper->Init(kBoolPref, &prefs);
338 helper->FetchValue();
339 EXPECT_FALSE(helper->value());
341 prefs.SetBoolean(kBoolPref, true);
343 helper->FetchValue();
344 EXPECT_TRUE(helper->value());
348 helper->FetchValue();
349 EXPECT_TRUE(helper->value());