Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: base/prefs/pref_service_unittest.cc

Issue 1648403002: Move base/prefs to components/prefs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/prefs/pref_service_factory.cc ('k') | base/prefs/pref_store.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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
5 #include <stddef.h>
6 #include <stdint.h>
7
8 #include <string>
9
10 #include "base/macros.h"
11 #include "base/prefs/json_pref_store.h"
12 #include "base/prefs/mock_pref_change_callback.h"
13 #include "base/prefs/pref_change_registrar.h"
14 #include "base/prefs/pref_registry_simple.h"
15 #include "base/prefs/pref_service_factory.h"
16 #include "base/prefs/pref_value_store.h"
17 #include "base/prefs/testing_pref_service.h"
18 #include "base/prefs/testing_pref_store.h"
19 #include "base/values.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 using testing::_;
24 using testing::Mock;
25
26 const char kPrefName[] = "pref.name";
27
28 TEST(PrefServiceTest, NoObserverFire) {
29 TestingPrefServiceSimple prefs;
30
31 const char pref_name[] = "homepage";
32 prefs.registry()->RegisterStringPref(pref_name, std::string());
33
34 const char new_pref_value[] = "http://www.google.com/";
35 MockPrefChangeCallback obs(&prefs);
36 PrefChangeRegistrar registrar;
37 registrar.Init(&prefs);
38 registrar.Add(pref_name, obs.GetCallback());
39
40 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
41 const base::StringValue expected_value(new_pref_value);
42 obs.Expect(pref_name, &expected_value);
43 prefs.SetString(pref_name, new_pref_value);
44 Mock::VerifyAndClearExpectations(&obs);
45
46 // Setting the pref to the same value should not set the pref value a second
47 // time.
48 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
49 prefs.SetString(pref_name, new_pref_value);
50 Mock::VerifyAndClearExpectations(&obs);
51
52 // Clearing the pref should cause the pref to fire.
53 const base::StringValue expected_default_value((std::string()));
54 obs.Expect(pref_name, &expected_default_value);
55 prefs.ClearPref(pref_name);
56 Mock::VerifyAndClearExpectations(&obs);
57
58 // Clearing the pref again should not cause the pref to fire.
59 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
60 prefs.ClearPref(pref_name);
61 Mock::VerifyAndClearExpectations(&obs);
62 }
63
64 TEST(PrefServiceTest, HasPrefPath) {
65 TestingPrefServiceSimple prefs;
66
67 const char path[] = "fake.path";
68
69 // Shouldn't initially have a path.
70 EXPECT_FALSE(prefs.HasPrefPath(path));
71
72 // Register the path. This doesn't set a value, so the path still shouldn't
73 // exist.
74 prefs.registry()->RegisterStringPref(path, std::string());
75 EXPECT_FALSE(prefs.HasPrefPath(path));
76
77 // Set a value and make sure we have a path.
78 prefs.SetString(path, "blah");
79 EXPECT_TRUE(prefs.HasPrefPath(path));
80 }
81
82 TEST(PrefServiceTest, Observers) {
83 const char pref_name[] = "homepage";
84
85 TestingPrefServiceSimple prefs;
86 prefs.SetUserPref(pref_name,
87 new base::StringValue("http://www.cnn.com"));
88 prefs.registry()->RegisterStringPref(pref_name, std::string());
89
90 const char new_pref_value[] = "http://www.google.com/";
91 const base::StringValue expected_new_pref_value(new_pref_value);
92 MockPrefChangeCallback obs(&prefs);
93 PrefChangeRegistrar registrar;
94 registrar.Init(&prefs);
95 registrar.Add(pref_name, obs.GetCallback());
96
97 PrefChangeRegistrar registrar_two;
98 registrar_two.Init(&prefs);
99
100 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
101 obs.Expect(pref_name, &expected_new_pref_value);
102 prefs.SetString(pref_name, new_pref_value);
103 Mock::VerifyAndClearExpectations(&obs);
104
105 // Now try adding a second pref observer.
106 const char new_pref_value2[] = "http://www.youtube.com/";
107 const base::StringValue expected_new_pref_value2(new_pref_value2);
108 MockPrefChangeCallback obs2(&prefs);
109 obs.Expect(pref_name, &expected_new_pref_value2);
110 obs2.Expect(pref_name, &expected_new_pref_value2);
111 registrar_two.Add(pref_name, obs2.GetCallback());
112 // This should fire the checks in obs and obs2.
113 prefs.SetString(pref_name, new_pref_value2);
114 Mock::VerifyAndClearExpectations(&obs);
115 Mock::VerifyAndClearExpectations(&obs2);
116
117 // Set a recommended value.
118 const base::StringValue recommended_pref_value("http://www.gmail.com/");
119 obs.Expect(pref_name, &expected_new_pref_value2);
120 obs2.Expect(pref_name, &expected_new_pref_value2);
121 // This should fire the checks in obs and obs2 but with an unchanged value
122 // as the recommended value is being overridden by the user-set value.
123 prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy());
124 Mock::VerifyAndClearExpectations(&obs);
125 Mock::VerifyAndClearExpectations(&obs2);
126
127 // Make sure obs2 still works after removing obs.
128 registrar.Remove(pref_name);
129 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
130 obs2.Expect(pref_name, &expected_new_pref_value);
131 // This should only fire the observer in obs2.
132 prefs.SetString(pref_name, new_pref_value);
133 Mock::VerifyAndClearExpectations(&obs);
134 Mock::VerifyAndClearExpectations(&obs2);
135 }
136
137 // Make sure that if a preference changes type, so the wrong type is stored in
138 // the user pref file, it uses the correct fallback value instead.
139 TEST(PrefServiceTest, GetValueChangedType) {
140 const int kTestValue = 10;
141 TestingPrefServiceSimple prefs;
142 prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue);
143
144 // Check falling back to a recommended value.
145 prefs.SetUserPref(kPrefName,
146 new base::StringValue("not an integer"));
147 const PrefService::Preference* pref = prefs.FindPreference(kPrefName);
148 ASSERT_TRUE(pref);
149 const base::Value* value = pref->GetValue();
150 ASSERT_TRUE(value);
151 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
152 int actual_int_value = -1;
153 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
154 EXPECT_EQ(kTestValue, actual_int_value);
155 }
156
157 TEST(PrefServiceTest, GetValueAndGetRecommendedValue) {
158 const int kDefaultValue = 5;
159 const int kUserValue = 10;
160 const int kRecommendedValue = 15;
161 TestingPrefServiceSimple prefs;
162 prefs.registry()->RegisterIntegerPref(kPrefName, kDefaultValue);
163
164 // Create pref with a default value only.
165 const PrefService::Preference* pref = prefs.FindPreference(kPrefName);
166 ASSERT_TRUE(pref);
167
168 // Check that GetValue() returns the default value.
169 const base::Value* value = pref->GetValue();
170 ASSERT_TRUE(value);
171 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
172 int actual_int_value = -1;
173 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
174 EXPECT_EQ(kDefaultValue, actual_int_value);
175
176 // Check that GetRecommendedValue() returns no value.
177 value = pref->GetRecommendedValue();
178 ASSERT_FALSE(value);
179
180 // Set a user-set value.
181 prefs.SetUserPref(kPrefName, new base::FundamentalValue(kUserValue));
182
183 // Check that GetValue() returns the user-set value.
184 value = pref->GetValue();
185 ASSERT_TRUE(value);
186 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
187 actual_int_value = -1;
188 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
189 EXPECT_EQ(kUserValue, actual_int_value);
190
191 // Check that GetRecommendedValue() returns no value.
192 value = pref->GetRecommendedValue();
193 ASSERT_FALSE(value);
194
195 // Set a recommended value.
196 prefs.SetRecommendedPref(kPrefName,
197 new base::FundamentalValue(kRecommendedValue));
198
199 // Check that GetValue() returns the user-set value.
200 value = pref->GetValue();
201 ASSERT_TRUE(value);
202 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
203 actual_int_value = -1;
204 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
205 EXPECT_EQ(kUserValue, actual_int_value);
206
207 // Check that GetRecommendedValue() returns the recommended value.
208 value = pref->GetRecommendedValue();
209 ASSERT_TRUE(value);
210 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
211 actual_int_value = -1;
212 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
213 EXPECT_EQ(kRecommendedValue, actual_int_value);
214
215 // Remove the user-set value.
216 prefs.RemoveUserPref(kPrefName);
217
218 // Check that GetValue() returns the recommended value.
219 value = pref->GetValue();
220 ASSERT_TRUE(value);
221 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
222 actual_int_value = -1;
223 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
224 EXPECT_EQ(kRecommendedValue, actual_int_value);
225
226 // Check that GetRecommendedValue() returns the recommended value.
227 value = pref->GetRecommendedValue();
228 ASSERT_TRUE(value);
229 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
230 actual_int_value = -1;
231 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
232 EXPECT_EQ(kRecommendedValue, actual_int_value);
233 }
234
235 // A PrefStore which just stores the last write flags that were used to write
236 // values to it.
237 class WriteFlagChecker : public TestingPrefStore {
238 public:
239 WriteFlagChecker() {}
240
241 void ReportValueChanged(const std::string& key, uint32_t flags) override {
242 SetLastWriteFlags(flags);
243 }
244
245 void SetValue(const std::string& key,
246 scoped_ptr<base::Value> value,
247 uint32_t flags) override {
248 SetLastWriteFlags(flags);
249 }
250
251 void SetValueSilently(const std::string& key,
252 scoped_ptr<base::Value> value,
253 uint32_t flags) override {
254 SetLastWriteFlags(flags);
255 }
256
257 void RemoveValue(const std::string& key, uint32_t flags) override {
258 SetLastWriteFlags(flags);
259 }
260
261 uint32_t GetLastFlagsAndClear() {
262 CHECK(last_write_flags_set_);
263 uint32_t result = last_write_flags_;
264 last_write_flags_set_ = false;
265 last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS;
266 return result;
267 }
268
269 bool last_write_flags_set() { return last_write_flags_set_; }
270
271 private:
272 ~WriteFlagChecker() override {}
273
274 void SetLastWriteFlags(uint32_t flags) {
275 CHECK(!last_write_flags_set_);
276 last_write_flags_set_ = true;
277 last_write_flags_ = flags;
278 }
279
280 bool last_write_flags_set_ = false;
281 uint32_t last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS;
282 };
283
284 TEST(PrefServiceTest, WriteablePrefStoreFlags) {
285 scoped_refptr<WriteFlagChecker> flag_checker(new WriteFlagChecker);
286 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple);
287 base::PrefServiceFactory factory;
288 factory.set_user_prefs(flag_checker);
289 scoped_ptr<PrefService> prefs(factory.Create(registry.get()));
290
291 // The first 8 bits of write flags are reserved for subclasses. Create a
292 // custom flag in this range
293 uint32_t kCustomRegistrationFlag = 1 << 2;
294
295 // A map of the registration flags that will be tested and the write flags
296 // they are expected to convert to.
297 struct RegistrationToWriteFlags {
298 const char* pref_name;
299 uint32_t registration_flags;
300 uint32_t write_flags;
301 };
302 const RegistrationToWriteFlags kRegistrationToWriteFlags[] = {
303 {"none",
304 PrefRegistry::NO_REGISTRATION_FLAGS,
305 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS},
306 {"lossy",
307 PrefRegistry::LOSSY_PREF,
308 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG},
309 {"custom",
310 kCustomRegistrationFlag,
311 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS},
312 {"lossyandcustom",
313 PrefRegistry::LOSSY_PREF | kCustomRegistrationFlag,
314 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG}};
315
316 for (size_t i = 0; i < arraysize(kRegistrationToWriteFlags); ++i) {
317 RegistrationToWriteFlags entry = kRegistrationToWriteFlags[i];
318 registry->RegisterDictionaryPref(
319 entry.pref_name, new base::DictionaryValue(), entry.registration_flags);
320
321 SCOPED_TRACE("Currently testing pref with name: " +
322 std::string(entry.pref_name));
323
324 prefs->GetMutableUserPref(entry.pref_name, base::Value::TYPE_DICTIONARY);
325 EXPECT_TRUE(flag_checker->last_write_flags_set());
326 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
327
328 prefs->ReportUserPrefChanged(entry.pref_name);
329 EXPECT_TRUE(flag_checker->last_write_flags_set());
330 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
331
332 prefs->ClearPref(entry.pref_name);
333 EXPECT_TRUE(flag_checker->last_write_flags_set());
334 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
335
336 prefs->SetUserPrefValue(entry.pref_name, new base::DictionaryValue());
337 EXPECT_TRUE(flag_checker->last_write_flags_set());
338 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
339 }
340 }
341
342 class PrefServiceSetValueTest : public testing::Test {
343 protected:
344 static const char kName[];
345 static const char kValue[];
346
347 PrefServiceSetValueTest() : observer_(&prefs_) {}
348
349 TestingPrefServiceSimple prefs_;
350 MockPrefChangeCallback observer_;
351 };
352
353 const char PrefServiceSetValueTest::kName[] = "name";
354 const char PrefServiceSetValueTest::kValue[] = "value";
355
356 TEST_F(PrefServiceSetValueTest, SetStringValue) {
357 const char default_string[] = "default";
358 const base::StringValue default_value(default_string);
359 prefs_.registry()->RegisterStringPref(kName, default_string);
360
361 PrefChangeRegistrar registrar;
362 registrar.Init(&prefs_);
363 registrar.Add(kName, observer_.GetCallback());
364
365 // Changing the controlling store from default to user triggers notification.
366 observer_.Expect(kName, &default_value);
367 prefs_.Set(kName, default_value);
368 Mock::VerifyAndClearExpectations(&observer_);
369
370 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
371 prefs_.Set(kName, default_value);
372 Mock::VerifyAndClearExpectations(&observer_);
373
374 base::StringValue new_value(kValue);
375 observer_.Expect(kName, &new_value);
376 prefs_.Set(kName, new_value);
377 Mock::VerifyAndClearExpectations(&observer_);
378 }
379
380 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
381 prefs_.registry()->RegisterDictionaryPref(kName);
382 PrefChangeRegistrar registrar;
383 registrar.Init(&prefs_);
384 registrar.Add(kName, observer_.GetCallback());
385
386 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
387 prefs_.RemoveUserPref(kName);
388 Mock::VerifyAndClearExpectations(&observer_);
389
390 base::DictionaryValue new_value;
391 new_value.SetString(kName, kValue);
392 observer_.Expect(kName, &new_value);
393 prefs_.Set(kName, new_value);
394 Mock::VerifyAndClearExpectations(&observer_);
395
396 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
397 prefs_.Set(kName, new_value);
398 Mock::VerifyAndClearExpectations(&observer_);
399
400 base::DictionaryValue empty;
401 observer_.Expect(kName, &empty);
402 prefs_.Set(kName, empty);
403 Mock::VerifyAndClearExpectations(&observer_);
404 }
405
406 TEST_F(PrefServiceSetValueTest, SetListValue) {
407 prefs_.registry()->RegisterListPref(kName);
408 PrefChangeRegistrar registrar;
409 registrar.Init(&prefs_);
410 registrar.Add(kName, observer_.GetCallback());
411
412 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
413 prefs_.RemoveUserPref(kName);
414 Mock::VerifyAndClearExpectations(&observer_);
415
416 base::ListValue new_value;
417 new_value.Append(new base::StringValue(kValue));
418 observer_.Expect(kName, &new_value);
419 prefs_.Set(kName, new_value);
420 Mock::VerifyAndClearExpectations(&observer_);
421
422 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
423 prefs_.Set(kName, new_value);
424 Mock::VerifyAndClearExpectations(&observer_);
425
426 base::ListValue empty;
427 observer_.Expect(kName, &empty);
428 prefs_.Set(kName, empty);
429 Mock::VerifyAndClearExpectations(&observer_);
430 }
OLDNEW
« no previous file with comments | « base/prefs/pref_service_factory.cc ('k') | base/prefs/pref_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698