Remove SimpleCacheSetSize test.
[chromium-blink-merge.git] / base / prefs / pref_service_unittest.cc
blob262d7e9de9daef9c7d8f03fde148f9bab273dfe5
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.
5 #include <string>
7 #include "base/prefs/json_pref_store.h"
8 #include "base/prefs/mock_pref_change_callback.h"
9 #include "base/prefs/pref_change_registrar.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service_factory.h"
12 #include "base/prefs/pref_value_store.h"
13 #include "base/prefs/testing_pref_service.h"
14 #include "base/prefs/testing_pref_store.h"
15 #include "base/values.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using testing::_;
20 using testing::Mock;
22 const char kPrefName[] = "pref.name";
24 TEST(PrefServiceTest, NoObserverFire) {
25 TestingPrefServiceSimple prefs;
27 const char pref_name[] = "homepage";
28 prefs.registry()->RegisterStringPref(pref_name, std::string());
30 const char new_pref_value[] = "http://www.google.com/";
31 MockPrefChangeCallback obs(&prefs);
32 PrefChangeRegistrar registrar;
33 registrar.Init(&prefs);
34 registrar.Add(pref_name, obs.GetCallback());
36 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
37 const base::StringValue expected_value(new_pref_value);
38 obs.Expect(pref_name, &expected_value);
39 prefs.SetString(pref_name, new_pref_value);
40 Mock::VerifyAndClearExpectations(&obs);
42 // Setting the pref to the same value should not set the pref value a second
43 // time.
44 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
45 prefs.SetString(pref_name, new_pref_value);
46 Mock::VerifyAndClearExpectations(&obs);
48 // Clearing the pref should cause the pref to fire.
49 const base::StringValue expected_default_value((std::string()));
50 obs.Expect(pref_name, &expected_default_value);
51 prefs.ClearPref(pref_name);
52 Mock::VerifyAndClearExpectations(&obs);
54 // Clearing the pref again should not cause the pref to fire.
55 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
56 prefs.ClearPref(pref_name);
57 Mock::VerifyAndClearExpectations(&obs);
60 TEST(PrefServiceTest, HasPrefPath) {
61 TestingPrefServiceSimple prefs;
63 const char path[] = "fake.path";
65 // Shouldn't initially have a path.
66 EXPECT_FALSE(prefs.HasPrefPath(path));
68 // Register the path. This doesn't set a value, so the path still shouldn't
69 // exist.
70 prefs.registry()->RegisterStringPref(path, std::string());
71 EXPECT_FALSE(prefs.HasPrefPath(path));
73 // Set a value and make sure we have a path.
74 prefs.SetString(path, "blah");
75 EXPECT_TRUE(prefs.HasPrefPath(path));
78 TEST(PrefServiceTest, Observers) {
79 const char pref_name[] = "homepage";
81 TestingPrefServiceSimple prefs;
82 prefs.SetUserPref(pref_name,
83 new base::StringValue("http://www.cnn.com"));
84 prefs.registry()->RegisterStringPref(pref_name, std::string());
86 const char new_pref_value[] = "http://www.google.com/";
87 const base::StringValue expected_new_pref_value(new_pref_value);
88 MockPrefChangeCallback obs(&prefs);
89 PrefChangeRegistrar registrar;
90 registrar.Init(&prefs);
91 registrar.Add(pref_name, obs.GetCallback());
93 PrefChangeRegistrar registrar_two;
94 registrar_two.Init(&prefs);
96 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
97 obs.Expect(pref_name, &expected_new_pref_value);
98 prefs.SetString(pref_name, new_pref_value);
99 Mock::VerifyAndClearExpectations(&obs);
101 // Now try adding a second pref observer.
102 const char new_pref_value2[] = "http://www.youtube.com/";
103 const base::StringValue expected_new_pref_value2(new_pref_value2);
104 MockPrefChangeCallback obs2(&prefs);
105 obs.Expect(pref_name, &expected_new_pref_value2);
106 obs2.Expect(pref_name, &expected_new_pref_value2);
107 registrar_two.Add(pref_name, obs2.GetCallback());
108 // This should fire the checks in obs and obs2.
109 prefs.SetString(pref_name, new_pref_value2);
110 Mock::VerifyAndClearExpectations(&obs);
111 Mock::VerifyAndClearExpectations(&obs2);
113 // Set a recommended value.
114 const base::StringValue recommended_pref_value("http://www.gmail.com/");
115 obs.Expect(pref_name, &expected_new_pref_value2);
116 obs2.Expect(pref_name, &expected_new_pref_value2);
117 // This should fire the checks in obs and obs2 but with an unchanged value
118 // as the recommended value is being overridden by the user-set value.
119 prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy());
120 Mock::VerifyAndClearExpectations(&obs);
121 Mock::VerifyAndClearExpectations(&obs2);
123 // Make sure obs2 still works after removing obs.
124 registrar.Remove(pref_name);
125 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
126 obs2.Expect(pref_name, &expected_new_pref_value);
127 // This should only fire the observer in obs2.
128 prefs.SetString(pref_name, new_pref_value);
129 Mock::VerifyAndClearExpectations(&obs);
130 Mock::VerifyAndClearExpectations(&obs2);
133 // Make sure that if a preference changes type, so the wrong type is stored in
134 // the user pref file, it uses the correct fallback value instead.
135 TEST(PrefServiceTest, GetValueChangedType) {
136 const int kTestValue = 10;
137 TestingPrefServiceSimple prefs;
138 prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue);
140 // Check falling back to a recommended value.
141 prefs.SetUserPref(kPrefName,
142 new base::StringValue("not an integer"));
143 const PrefService::Preference* pref = prefs.FindPreference(kPrefName);
144 ASSERT_TRUE(pref);
145 const base::Value* value = pref->GetValue();
146 ASSERT_TRUE(value);
147 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
148 int actual_int_value = -1;
149 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
150 EXPECT_EQ(kTestValue, actual_int_value);
153 TEST(PrefServiceTest, GetValueAndGetRecommendedValue) {
154 const int kDefaultValue = 5;
155 const int kUserValue = 10;
156 const int kRecommendedValue = 15;
157 TestingPrefServiceSimple prefs;
158 prefs.registry()->RegisterIntegerPref(kPrefName, kDefaultValue);
160 // Create pref with a default value only.
161 const PrefService::Preference* pref = prefs.FindPreference(kPrefName);
162 ASSERT_TRUE(pref);
164 // Check that GetValue() returns the default value.
165 const base::Value* value = pref->GetValue();
166 ASSERT_TRUE(value);
167 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
168 int actual_int_value = -1;
169 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
170 EXPECT_EQ(kDefaultValue, actual_int_value);
172 // Check that GetRecommendedValue() returns no value.
173 value = pref->GetRecommendedValue();
174 ASSERT_FALSE(value);
176 // Set a user-set value.
177 prefs.SetUserPref(kPrefName, new base::FundamentalValue(kUserValue));
179 // Check that GetValue() returns the user-set value.
180 value = pref->GetValue();
181 ASSERT_TRUE(value);
182 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
183 actual_int_value = -1;
184 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
185 EXPECT_EQ(kUserValue, actual_int_value);
187 // Check that GetRecommendedValue() returns no value.
188 value = pref->GetRecommendedValue();
189 ASSERT_FALSE(value);
191 // Set a recommended value.
192 prefs.SetRecommendedPref(kPrefName,
193 new base::FundamentalValue(kRecommendedValue));
195 // Check that GetValue() returns the user-set value.
196 value = pref->GetValue();
197 ASSERT_TRUE(value);
198 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
199 actual_int_value = -1;
200 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
201 EXPECT_EQ(kUserValue, actual_int_value);
203 // Check that GetRecommendedValue() returns the recommended value.
204 value = pref->GetRecommendedValue();
205 ASSERT_TRUE(value);
206 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
207 actual_int_value = -1;
208 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
209 EXPECT_EQ(kRecommendedValue, actual_int_value);
211 // Remove the user-set value.
212 prefs.RemoveUserPref(kPrefName);
214 // Check that GetValue() returns the recommended value.
215 value = pref->GetValue();
216 ASSERT_TRUE(value);
217 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
218 actual_int_value = -1;
219 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
220 EXPECT_EQ(kRecommendedValue, actual_int_value);
222 // Check that GetRecommendedValue() returns the recommended value.
223 value = pref->GetRecommendedValue();
224 ASSERT_TRUE(value);
225 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType());
226 actual_int_value = -1;
227 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
228 EXPECT_EQ(kRecommendedValue, actual_int_value);
231 // A PrefStore which just stores the last write flags that were used to write
232 // values to it.
233 class WriteFlagChecker : public TestingPrefStore {
234 public:
235 WriteFlagChecker() {}
237 void ReportValueChanged(const std::string& key, uint32 flags) override {
238 SetLastWriteFlags(flags);
241 void SetValue(const std::string& key,
242 base::Value* value,
243 uint32 flags) override {
244 SetLastWriteFlags(flags);
245 delete value;
248 void SetValueSilently(const std::string& key,
249 base::Value* value,
250 uint32 flags) override {
251 SetLastWriteFlags(flags);
252 delete value;
255 void RemoveValue(const std::string& key, uint32 flags) override {
256 SetLastWriteFlags(flags);
259 uint32 GetLastFlagsAndClear() {
260 CHECK(last_write_flags_set_);
261 uint32 result = last_write_flags_;
262 last_write_flags_set_ = false;
263 last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS;
264 return result;
267 bool last_write_flags_set() { return last_write_flags_set_; }
269 private:
270 ~WriteFlagChecker() override {}
272 void SetLastWriteFlags(uint32 flags) {
273 CHECK(!last_write_flags_set_);
274 last_write_flags_set_ = true;
275 last_write_flags_ = flags;
278 bool last_write_flags_set_ = false;
279 uint32 last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS;
282 TEST(PrefServiceTest, WriteablePrefStoreFlags) {
283 scoped_refptr<WriteFlagChecker> flag_checker(new WriteFlagChecker);
284 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple);
285 base::PrefServiceFactory factory;
286 factory.set_user_prefs(flag_checker);
287 scoped_ptr<PrefService> prefs(factory.Create(registry.get()));
289 // The first 8 bits of write flags are reserved for subclasses. Create a
290 // custom flag in this range
291 uint32 kCustomRegistrationFlag = 1 << 2;
293 // A map of the registration flags that will be tested and the write flags
294 // they are expected to convert to.
295 struct RegistrationToWriteFlags {
296 const char* pref_name;
297 uint32 registration_flags;
298 uint32 write_flags;
300 const RegistrationToWriteFlags kRegistrationToWriteFlags[] = {
301 {"none",
302 PrefRegistry::NO_REGISTRATION_FLAGS,
303 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS},
304 {"lossy",
305 PrefRegistry::LOSSY_PREF,
306 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG},
307 {"custom",
308 kCustomRegistrationFlag,
309 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS},
310 {"lossyandcustom",
311 PrefRegistry::LOSSY_PREF | kCustomRegistrationFlag,
312 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG}};
314 for (size_t i = 0; i < arraysize(kRegistrationToWriteFlags); ++i) {
315 RegistrationToWriteFlags entry = kRegistrationToWriteFlags[i];
316 registry->RegisterDictionaryPref(
317 entry.pref_name, new base::DictionaryValue(), entry.registration_flags);
319 SCOPED_TRACE("Currently testing pref with name: " +
320 std::string(entry.pref_name));
322 prefs->GetMutableUserPref(entry.pref_name, base::Value::TYPE_DICTIONARY);
323 EXPECT_TRUE(flag_checker->last_write_flags_set());
324 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
326 prefs->ReportUserPrefChanged(entry.pref_name);
327 EXPECT_TRUE(flag_checker->last_write_flags_set());
328 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
330 prefs->ClearPref(entry.pref_name);
331 EXPECT_TRUE(flag_checker->last_write_flags_set());
332 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
334 prefs->SetUserPrefValue(entry.pref_name, new base::DictionaryValue());
335 EXPECT_TRUE(flag_checker->last_write_flags_set());
336 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
340 class PrefServiceSetValueTest : public testing::Test {
341 protected:
342 static const char kName[];
343 static const char kValue[];
345 PrefServiceSetValueTest() : observer_(&prefs_) {}
347 TestingPrefServiceSimple prefs_;
348 MockPrefChangeCallback observer_;
351 const char PrefServiceSetValueTest::kName[] = "name";
352 const char PrefServiceSetValueTest::kValue[] = "value";
354 TEST_F(PrefServiceSetValueTest, SetStringValue) {
355 const char default_string[] = "default";
356 const base::StringValue default_value(default_string);
357 prefs_.registry()->RegisterStringPref(kName, default_string);
359 PrefChangeRegistrar registrar;
360 registrar.Init(&prefs_);
361 registrar.Add(kName, observer_.GetCallback());
363 // Changing the controlling store from default to user triggers notification.
364 observer_.Expect(kName, &default_value);
365 prefs_.Set(kName, default_value);
366 Mock::VerifyAndClearExpectations(&observer_);
368 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
369 prefs_.Set(kName, default_value);
370 Mock::VerifyAndClearExpectations(&observer_);
372 base::StringValue new_value(kValue);
373 observer_.Expect(kName, &new_value);
374 prefs_.Set(kName, new_value);
375 Mock::VerifyAndClearExpectations(&observer_);
378 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
379 prefs_.registry()->RegisterDictionaryPref(kName);
380 PrefChangeRegistrar registrar;
381 registrar.Init(&prefs_);
382 registrar.Add(kName, observer_.GetCallback());
384 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
385 prefs_.RemoveUserPref(kName);
386 Mock::VerifyAndClearExpectations(&observer_);
388 base::DictionaryValue new_value;
389 new_value.SetString(kName, kValue);
390 observer_.Expect(kName, &new_value);
391 prefs_.Set(kName, new_value);
392 Mock::VerifyAndClearExpectations(&observer_);
394 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
395 prefs_.Set(kName, new_value);
396 Mock::VerifyAndClearExpectations(&observer_);
398 base::DictionaryValue empty;
399 observer_.Expect(kName, &empty);
400 prefs_.Set(kName, empty);
401 Mock::VerifyAndClearExpectations(&observer_);
404 TEST_F(PrefServiceSetValueTest, SetListValue) {
405 prefs_.registry()->RegisterListPref(kName);
406 PrefChangeRegistrar registrar;
407 registrar.Init(&prefs_);
408 registrar.Add(kName, observer_.GetCallback());
410 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
411 prefs_.RemoveUserPref(kName);
412 Mock::VerifyAndClearExpectations(&observer_);
414 base::ListValue new_value;
415 new_value.Append(new base::StringValue(kValue));
416 observer_.Expect(kName, &new_value);
417 prefs_.Set(kName, new_value);
418 Mock::VerifyAndClearExpectations(&observer_);
420 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
421 prefs_.Set(kName, new_value);
422 Mock::VerifyAndClearExpectations(&observer_);
424 base::ListValue empty;
425 observer_.Expect(kName, &empty);
426 prefs_.Set(kName, empty);
427 Mock::VerifyAndClearExpectations(&observer_);