Add missing pnacl libraries and headers and tools.
[chromium-blink-merge.git] / base / prefs / pref_service_unittest.cc
blob927f2f7779f8cf7869301a2ee51731e6b6c9b816
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_value_store.h"
12 #include "base/prefs/testing_pref_service.h"
13 #include "base/prefs/testing_pref_store.h"
14 #include "base/values.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using testing::_;
19 using testing::Mock;
21 const char kPrefName[] = "pref.name";
23 TEST(PrefServiceTest, NoObserverFire) {
24 TestingPrefServiceSimple prefs;
26 const char pref_name[] = "homepage";
27 prefs.registry()->RegisterStringPref(pref_name, std::string());
29 const char new_pref_value[] = "http://www.google.com/";
30 MockPrefChangeCallback obs(&prefs);
31 PrefChangeRegistrar registrar;
32 registrar.Init(&prefs);
33 registrar.Add(pref_name, obs.GetCallback());
35 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
36 const StringValue expected_value(new_pref_value);
37 obs.Expect(pref_name, &expected_value);
38 prefs.SetString(pref_name, new_pref_value);
39 Mock::VerifyAndClearExpectations(&obs);
41 // Setting the pref to the same value should not set the pref value a second
42 // time.
43 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
44 prefs.SetString(pref_name, new_pref_value);
45 Mock::VerifyAndClearExpectations(&obs);
47 // Clearing the pref should cause the pref to fire.
48 const StringValue expected_default_value((std::string()));
49 obs.Expect(pref_name, &expected_default_value);
50 prefs.ClearPref(pref_name);
51 Mock::VerifyAndClearExpectations(&obs);
53 // Clearing the pref again should not cause the pref to fire.
54 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
55 prefs.ClearPref(pref_name);
56 Mock::VerifyAndClearExpectations(&obs);
59 TEST(PrefServiceTest, HasPrefPath) {
60 TestingPrefServiceSimple prefs;
62 const char path[] = "fake.path";
64 // Shouldn't initially have a path.
65 EXPECT_FALSE(prefs.HasPrefPath(path));
67 // Register the path. This doesn't set a value, so the path still shouldn't
68 // exist.
69 prefs.registry()->RegisterStringPref(path, std::string());
70 EXPECT_FALSE(prefs.HasPrefPath(path));
72 // Set a value and make sure we have a path.
73 prefs.SetString(path, "blah");
74 EXPECT_TRUE(prefs.HasPrefPath(path));
77 TEST(PrefServiceTest, Observers) {
78 const char pref_name[] = "homepage";
80 TestingPrefServiceSimple prefs;
81 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com"));
82 prefs.registry()->RegisterStringPref(pref_name, std::string());
84 const char new_pref_value[] = "http://www.google.com/";
85 const StringValue expected_new_pref_value(new_pref_value);
86 MockPrefChangeCallback obs(&prefs);
87 PrefChangeRegistrar registrar;
88 registrar.Init(&prefs);
89 registrar.Add(pref_name, obs.GetCallback());
91 PrefChangeRegistrar registrar_two;
92 registrar_two.Init(&prefs);
94 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
95 obs.Expect(pref_name, &expected_new_pref_value);
96 prefs.SetString(pref_name, new_pref_value);
97 Mock::VerifyAndClearExpectations(&obs);
99 // Now try adding a second pref observer.
100 const char new_pref_value2[] = "http://www.youtube.com/";
101 const StringValue expected_new_pref_value2(new_pref_value2);
102 MockPrefChangeCallback obs2(&prefs);
103 obs.Expect(pref_name, &expected_new_pref_value2);
104 obs2.Expect(pref_name, &expected_new_pref_value2);
105 registrar_two.Add(pref_name, obs2.GetCallback());
106 // This should fire the checks in obs and obs2.
107 prefs.SetString(pref_name, new_pref_value2);
108 Mock::VerifyAndClearExpectations(&obs);
109 Mock::VerifyAndClearExpectations(&obs2);
111 // Set a recommended value.
112 const StringValue recommended_pref_value("http://www.gmail.com/");
113 obs.Expect(pref_name, &expected_new_pref_value2);
114 obs2.Expect(pref_name, &expected_new_pref_value2);
115 // This should fire the checks in obs and obs2 but with an unchanged value
116 // as the recommended value is being overridden by the user-set value.
117 prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy());
118 Mock::VerifyAndClearExpectations(&obs);
119 Mock::VerifyAndClearExpectations(&obs2);
121 // Make sure obs2 still works after removing obs.
122 registrar.Remove(pref_name);
123 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
124 obs2.Expect(pref_name, &expected_new_pref_value);
125 // This should only fire the observer in obs2.
126 prefs.SetString(pref_name, new_pref_value);
127 Mock::VerifyAndClearExpectations(&obs);
128 Mock::VerifyAndClearExpectations(&obs2);
131 // Make sure that if a preference changes type, so the wrong type is stored in
132 // the user pref file, it uses the correct fallback value instead.
133 TEST(PrefServiceTest, GetValueChangedType) {
134 const int kTestValue = 10;
135 TestingPrefServiceSimple prefs;
136 prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue);
138 // Check falling back to a recommended value.
139 prefs.SetUserPref(kPrefName, Value::CreateStringValue("not an integer"));
140 const PrefService::Preference* pref = prefs.FindPreference(kPrefName);
141 ASSERT_TRUE(pref);
142 const Value* value = pref->GetValue();
143 ASSERT_TRUE(value);
144 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
145 int actual_int_value = -1;
146 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
147 EXPECT_EQ(kTestValue, actual_int_value);
150 TEST(PrefServiceTest, GetValueAndGetRecommendedValue) {
151 const int kDefaultValue = 5;
152 const int kUserValue = 10;
153 const int kRecommendedValue = 15;
154 TestingPrefServiceSimple prefs;
155 prefs.registry()->RegisterIntegerPref(kPrefName, kDefaultValue);
157 // Create pref with a default value only.
158 const PrefService::Preference* pref = prefs.FindPreference(kPrefName);
159 ASSERT_TRUE(pref);
161 // Check that GetValue() returns the default value.
162 const Value* value = pref->GetValue();
163 ASSERT_TRUE(value);
164 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
165 int actual_int_value = -1;
166 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
167 EXPECT_EQ(kDefaultValue, actual_int_value);
169 // Check that GetRecommendedValue() returns no value.
170 value = pref->GetRecommendedValue();
171 ASSERT_FALSE(value);
173 // Set a user-set value.
174 prefs.SetUserPref(kPrefName, Value::CreateIntegerValue(kUserValue));
176 // Check that GetValue() returns the user-set value.
177 value = pref->GetValue();
178 ASSERT_TRUE(value);
179 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
180 actual_int_value = -1;
181 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
182 EXPECT_EQ(kUserValue, actual_int_value);
184 // Check that GetRecommendedValue() returns no value.
185 value = pref->GetRecommendedValue();
186 ASSERT_FALSE(value);
188 // Set a recommended value.
189 prefs.SetRecommendedPref(kPrefName,
190 Value::CreateIntegerValue(kRecommendedValue));
192 // Check that GetValue() returns the user-set value.
193 value = pref->GetValue();
194 ASSERT_TRUE(value);
195 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
196 actual_int_value = -1;
197 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
198 EXPECT_EQ(kUserValue, actual_int_value);
200 // Check that GetRecommendedValue() returns the recommended value.
201 value = pref->GetRecommendedValue();
202 ASSERT_TRUE(value);
203 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
204 actual_int_value = -1;
205 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
206 EXPECT_EQ(kRecommendedValue, actual_int_value);
208 // Remove the user-set value.
209 prefs.RemoveUserPref(kPrefName);
211 // Check that GetValue() returns the recommended value.
212 value = pref->GetValue();
213 ASSERT_TRUE(value);
214 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
215 actual_int_value = -1;
216 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
217 EXPECT_EQ(kRecommendedValue, actual_int_value);
219 // Check that GetRecommendedValue() returns the recommended value.
220 value = pref->GetRecommendedValue();
221 ASSERT_TRUE(value);
222 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
223 actual_int_value = -1;
224 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
225 EXPECT_EQ(kRecommendedValue, actual_int_value);
228 class PrefServiceSetValueTest : public testing::Test {
229 protected:
230 static const char kName[];
231 static const char kValue[];
233 PrefServiceSetValueTest() : observer_(&prefs_) {}
235 TestingPrefServiceSimple prefs_;
236 MockPrefChangeCallback observer_;
239 const char PrefServiceSetValueTest::kName[] = "name";
240 const char PrefServiceSetValueTest::kValue[] = "value";
242 TEST_F(PrefServiceSetValueTest, SetStringValue) {
243 const char default_string[] = "default";
244 const StringValue default_value(default_string);
245 prefs_.registry()->RegisterStringPref(kName, default_string);
247 PrefChangeRegistrar registrar;
248 registrar.Init(&prefs_);
249 registrar.Add(kName, observer_.GetCallback());
251 // Changing the controlling store from default to user triggers notification.
252 observer_.Expect(kName, &default_value);
253 prefs_.Set(kName, default_value);
254 Mock::VerifyAndClearExpectations(&observer_);
256 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
257 prefs_.Set(kName, default_value);
258 Mock::VerifyAndClearExpectations(&observer_);
260 StringValue new_value(kValue);
261 observer_.Expect(kName, &new_value);
262 prefs_.Set(kName, new_value);
263 Mock::VerifyAndClearExpectations(&observer_);
266 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
267 prefs_.registry()->RegisterDictionaryPref(kName);
268 PrefChangeRegistrar registrar;
269 registrar.Init(&prefs_);
270 registrar.Add(kName, observer_.GetCallback());
272 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
273 prefs_.RemoveUserPref(kName);
274 Mock::VerifyAndClearExpectations(&observer_);
276 DictionaryValue new_value;
277 new_value.SetString(kName, kValue);
278 observer_.Expect(kName, &new_value);
279 prefs_.Set(kName, new_value);
280 Mock::VerifyAndClearExpectations(&observer_);
282 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
283 prefs_.Set(kName, new_value);
284 Mock::VerifyAndClearExpectations(&observer_);
286 DictionaryValue empty;
287 observer_.Expect(kName, &empty);
288 prefs_.Set(kName, empty);
289 Mock::VerifyAndClearExpectations(&observer_);
292 TEST_F(PrefServiceSetValueTest, SetListValue) {
293 prefs_.registry()->RegisterListPref(kName);
294 PrefChangeRegistrar registrar;
295 registrar.Init(&prefs_);
296 registrar.Add(kName, observer_.GetCallback());
298 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
299 prefs_.RemoveUserPref(kName);
300 Mock::VerifyAndClearExpectations(&observer_);
302 ListValue new_value;
303 new_value.Append(Value::CreateStringValue(kValue));
304 observer_.Expect(kName, &new_value);
305 prefs_.Set(kName, new_value);
306 Mock::VerifyAndClearExpectations(&observer_);
308 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
309 prefs_.Set(kName, new_value);
310 Mock::VerifyAndClearExpectations(&observer_);
312 ListValue empty;
313 observer_.Expect(kName, &empty);
314 prefs_.Set(kName, empty);
315 Mock::VerifyAndClearExpectations(&observer_);