OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/command_line.h" | |
8 #include "base/file_util.h" | |
9 #include "base/files/scoped_temp_dir.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/path_service.h" | |
12 #include "base/prefs/json_pref_store.h" | 7 #include "base/prefs/json_pref_store.h" |
13 #include "base/prefs/mock_pref_change_callback.h" | 8 #include "base/prefs/mock_pref_change_callback.h" |
14 #include "base/prefs/pref_registry_simple.h" | 9 #include "base/prefs/pref_registry_simple.h" |
15 #include "base/prefs/pref_value_store.h" | 10 #include "base/prefs/pref_value_store.h" |
16 #include "base/prefs/public/pref_change_registrar.h" | 11 #include "base/prefs/public/pref_change_registrar.h" |
17 #include "base/prefs/testing_pref_store.h" | 12 #include "base/prefs/testing_pref_store.h" |
18 #include "base/utf_string_conversions.h" | |
19 #include "base/values.h" | 13 #include "base/values.h" |
20 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 14 #include "chrome/test/base/testing_pref_service.h" |
21 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | |
22 #include "chrome/browser/prefs/browser_prefs.h" | |
23 #include "chrome/browser/prefs/command_line_pref_store.h" | |
24 #include "chrome/browser/prefs/pref_registry_syncable.h" | |
25 #include "chrome/browser/prefs/pref_service_mock_builder.h" | |
26 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
27 #include "chrome/common/chrome_paths.h" | |
28 #include "chrome/common/chrome_switches.h" | |
29 #include "chrome/common/pref_names.h" | |
30 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
31 #include "chrome/test/base/testing_pref_service_syncable.h" | |
32 #include "chrome/test/base/testing_profile.h" | |
33 #include "content/public/test/test_browser_thread.h" | |
34 #include "content/public/test/web_contents_tester.h" | |
35 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
37 #include "ui/base/test/data/resource.h" | |
38 #include "webkit/glue/webpreferences.h" | |
39 | 17 |
40 using content::BrowserThread; | |
41 using content::WebContentsTester; | |
42 using testing::_; | 18 using testing::_; |
43 using testing::Mock; | 19 using testing::Mock; |
44 | 20 |
| 21 const char kPrefName[] = "pref.name"; |
| 22 |
45 TEST(PrefServiceTest, NoObserverFire) { | 23 TEST(PrefServiceTest, NoObserverFire) { |
46 TestingPrefServiceSimple prefs; | 24 TestingPrefServiceSimple prefs; |
47 | 25 |
48 const char pref_name[] = "homepage"; | 26 const char pref_name[] = "homepage"; |
49 prefs.registry()->RegisterStringPref(pref_name, std::string()); | 27 prefs.registry()->RegisterStringPref(pref_name, std::string()); |
50 | 28 |
51 const char new_pref_value[] = "http://www.google.com/"; | 29 const char new_pref_value[] = "http://www.google.com/"; |
52 MockPrefChangeCallback obs(&prefs); | 30 MockPrefChangeCallback obs(&prefs); |
53 PrefChangeRegistrar registrar; | 31 PrefChangeRegistrar registrar; |
54 registrar.Init(&prefs); | 32 registrar.Init(&prefs); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 prefs.SetString(pref_name, new_pref_value); | 126 prefs.SetString(pref_name, new_pref_value); |
149 Mock::VerifyAndClearExpectations(&obs); | 127 Mock::VerifyAndClearExpectations(&obs); |
150 Mock::VerifyAndClearExpectations(&obs2); | 128 Mock::VerifyAndClearExpectations(&obs2); |
151 } | 129 } |
152 | 130 |
153 // Make sure that if a preference changes type, so the wrong type is stored in | 131 // Make sure that if a preference changes type, so the wrong type is stored in |
154 // the user pref file, it uses the correct fallback value instead. | 132 // the user pref file, it uses the correct fallback value instead. |
155 TEST(PrefServiceTest, GetValueChangedType) { | 133 TEST(PrefServiceTest, GetValueChangedType) { |
156 const int kTestValue = 10; | 134 const int kTestValue = 10; |
157 TestingPrefServiceSimple prefs; | 135 TestingPrefServiceSimple prefs; |
158 prefs.registry()->RegisterIntegerPref( | 136 prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue); |
159 prefs::kStabilityLaunchCount, kTestValue); | |
160 | 137 |
161 // Check falling back to a recommended value. | 138 // Check falling back to a recommended value. |
162 prefs.SetUserPref(prefs::kStabilityLaunchCount, | 139 prefs.SetUserPref(kPrefName, Value::CreateStringValue("not an integer")); |
163 Value::CreateStringValue("not an integer")); | 140 const PrefService::Preference* pref = prefs.FindPreference(kPrefName); |
164 const PrefService::Preference* pref = | |
165 prefs.FindPreference(prefs::kStabilityLaunchCount); | |
166 ASSERT_TRUE(pref); | 141 ASSERT_TRUE(pref); |
167 const Value* value = pref->GetValue(); | 142 const Value* value = pref->GetValue(); |
168 ASSERT_TRUE(value); | 143 ASSERT_TRUE(value); |
169 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); | 144 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
170 int actual_int_value = -1; | 145 int actual_int_value = -1; |
171 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 146 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
172 EXPECT_EQ(kTestValue, actual_int_value); | 147 EXPECT_EQ(kTestValue, actual_int_value); |
173 } | 148 } |
174 | 149 |
175 TEST(PrefServiceTest, UpdateCommandLinePrefStore) { | |
176 TestingPrefServiceSimple prefs; | |
177 prefs.registry()->RegisterBooleanPref(prefs::kCloudPrintProxyEnabled, false); | |
178 | |
179 // Check to make sure the value is as expected. | |
180 const PrefService::Preference* pref = | |
181 prefs.FindPreference(prefs::kCloudPrintProxyEnabled); | |
182 ASSERT_TRUE(pref); | |
183 const Value* value = pref->GetValue(); | |
184 ASSERT_TRUE(value); | |
185 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); | |
186 bool actual_bool_value = true; | |
187 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); | |
188 EXPECT_FALSE(actual_bool_value); | |
189 | |
190 // Change the command line. | |
191 CommandLine cmd_line(CommandLine::NO_PROGRAM); | |
192 cmd_line.AppendSwitch(switches::kEnableCloudPrintProxy); | |
193 | |
194 // Call UpdateCommandLinePrefStore and check to see if the value has changed. | |
195 prefs.UpdateCommandLinePrefStore(new CommandLinePrefStore(&cmd_line)); | |
196 pref = prefs.FindPreference(prefs::kCloudPrintProxyEnabled); | |
197 ASSERT_TRUE(pref); | |
198 value = pref->GetValue(); | |
199 ASSERT_TRUE(value); | |
200 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); | |
201 actual_bool_value = false; | |
202 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); | |
203 EXPECT_TRUE(actual_bool_value); | |
204 } | |
205 | |
206 TEST(PrefServiceTest, GetValueAndGetRecommendedValue) { | 150 TEST(PrefServiceTest, GetValueAndGetRecommendedValue) { |
207 const int kDefaultValue = 5; | 151 const int kDefaultValue = 5; |
208 const int kUserValue = 10; | 152 const int kUserValue = 10; |
209 const int kRecommendedValue = 15; | 153 const int kRecommendedValue = 15; |
210 TestingPrefServiceSimple prefs; | 154 TestingPrefServiceSimple prefs; |
211 prefs.registry()->RegisterIntegerPref( | 155 prefs.registry()->RegisterIntegerPref(kPrefName, kDefaultValue); |
212 prefs::kStabilityLaunchCount, kDefaultValue); | |
213 | 156 |
214 // Create pref with a default value only. | 157 // Create pref with a default value only. |
215 const PrefService::Preference* pref = | 158 const PrefService::Preference* pref = prefs.FindPreference(kPrefName); |
216 prefs.FindPreference(prefs::kStabilityLaunchCount); | |
217 ASSERT_TRUE(pref); | 159 ASSERT_TRUE(pref); |
218 | 160 |
219 // Check that GetValue() returns the default value. | 161 // Check that GetValue() returns the default value. |
220 const Value* value = pref->GetValue(); | 162 const Value* value = pref->GetValue(); |
221 ASSERT_TRUE(value); | 163 ASSERT_TRUE(value); |
222 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); | 164 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
223 int actual_int_value = -1; | 165 int actual_int_value = -1; |
224 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 166 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
225 EXPECT_EQ(kDefaultValue, actual_int_value); | 167 EXPECT_EQ(kDefaultValue, actual_int_value); |
226 | 168 |
227 // Check that GetRecommendedValue() returns no value. | 169 // Check that GetRecommendedValue() returns no value. |
228 value = pref->GetRecommendedValue(); | 170 value = pref->GetRecommendedValue(); |
229 ASSERT_FALSE(value); | 171 ASSERT_FALSE(value); |
230 | 172 |
231 // Set a user-set value. | 173 // Set a user-set value. |
232 prefs.SetUserPref(prefs::kStabilityLaunchCount, | 174 prefs.SetUserPref(kPrefName, Value::CreateIntegerValue(kUserValue)); |
233 Value::CreateIntegerValue(kUserValue)); | |
234 | 175 |
235 // Check that GetValue() returns the user-set value. | 176 // Check that GetValue() returns the user-set value. |
236 value = pref->GetValue(); | 177 value = pref->GetValue(); |
237 ASSERT_TRUE(value); | 178 ASSERT_TRUE(value); |
238 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); | 179 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
239 actual_int_value = -1; | 180 actual_int_value = -1; |
240 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 181 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
241 EXPECT_EQ(kUserValue, actual_int_value); | 182 EXPECT_EQ(kUserValue, actual_int_value); |
242 | 183 |
243 // Check that GetRecommendedValue() returns no value. | 184 // Check that GetRecommendedValue() returns no value. |
244 value = pref->GetRecommendedValue(); | 185 value = pref->GetRecommendedValue(); |
245 ASSERT_FALSE(value); | 186 ASSERT_FALSE(value); |
246 | 187 |
247 // Set a recommended value. | 188 // Set a recommended value. |
248 prefs.SetRecommendedPref(prefs::kStabilityLaunchCount, | 189 prefs.SetRecommendedPref(kPrefName, |
249 Value::CreateIntegerValue(kRecommendedValue)); | 190 Value::CreateIntegerValue(kRecommendedValue)); |
250 | 191 |
251 // Check that GetValue() returns the user-set value. | 192 // Check that GetValue() returns the user-set value. |
252 value = pref->GetValue(); | 193 value = pref->GetValue(); |
253 ASSERT_TRUE(value); | 194 ASSERT_TRUE(value); |
254 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); | 195 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
255 actual_int_value = -1; | 196 actual_int_value = -1; |
256 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 197 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
257 EXPECT_EQ(kUserValue, actual_int_value); | 198 EXPECT_EQ(kUserValue, actual_int_value); |
258 | 199 |
259 // Check that GetRecommendedValue() returns the recommended value. | 200 // Check that GetRecommendedValue() returns the recommended value. |
260 value = pref->GetRecommendedValue(); | 201 value = pref->GetRecommendedValue(); |
261 ASSERT_TRUE(value); | 202 ASSERT_TRUE(value); |
262 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); | 203 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
263 actual_int_value = -1; | 204 actual_int_value = -1; |
264 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 205 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
265 EXPECT_EQ(kRecommendedValue, actual_int_value); | 206 EXPECT_EQ(kRecommendedValue, actual_int_value); |
266 | 207 |
267 // Remove the user-set value. | 208 // Remove the user-set value. |
268 prefs.RemoveUserPref(prefs::kStabilityLaunchCount); | 209 prefs.RemoveUserPref(kPrefName); |
269 | 210 |
270 // Check that GetValue() returns the recommended value. | 211 // Check that GetValue() returns the recommended value. |
271 value = pref->GetValue(); | 212 value = pref->GetValue(); |
272 ASSERT_TRUE(value); | 213 ASSERT_TRUE(value); |
273 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); | 214 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
274 actual_int_value = -1; | 215 actual_int_value = -1; |
275 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 216 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
276 EXPECT_EQ(kRecommendedValue, actual_int_value); | 217 EXPECT_EQ(kRecommendedValue, actual_int_value); |
277 | 218 |
278 // Check that GetRecommendedValue() returns the recommended value. | 219 // Check that GetRecommendedValue() returns the recommended value. |
279 value = pref->GetRecommendedValue(); | 220 value = pref->GetRecommendedValue(); |
280 ASSERT_TRUE(value); | 221 ASSERT_TRUE(value); |
281 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); | 222 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
282 actual_int_value = -1; | 223 actual_int_value = -1; |
283 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 224 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
284 EXPECT_EQ(kRecommendedValue, actual_int_value); | 225 EXPECT_EQ(kRecommendedValue, actual_int_value); |
285 } | 226 } |
286 | 227 |
287 class PrefServiceUserFilePrefsTest : public testing::Test { | |
288 protected: | |
289 virtual void SetUp() { | |
290 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
291 | |
292 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); | |
293 data_dir_ = data_dir_.AppendASCII("pref_service"); | |
294 ASSERT_TRUE(file_util::PathExists(data_dir_)); | |
295 } | |
296 | |
297 void ClearListValue(PrefService* prefs, const char* key) { | |
298 ListPrefUpdate updater(prefs, key); | |
299 updater->Clear(); | |
300 } | |
301 | |
302 void ClearDictionaryValue(PrefService* prefs, const char* key) { | |
303 DictionaryPrefUpdate updater(prefs, key); | |
304 updater->Clear(); | |
305 } | |
306 | |
307 // The path to temporary directory used to contain the test operations. | |
308 base::ScopedTempDir temp_dir_; | |
309 // The path to the directory where the test data is stored. | |
310 base::FilePath data_dir_; | |
311 // A message loop that we can use as the file thread message loop. | |
312 MessageLoop message_loop_; | |
313 }; | |
314 | |
315 // Verifies that ListValue and DictionaryValue pref with non emtpy default | |
316 // preserves its empty value. | |
317 TEST_F(PrefServiceUserFilePrefsTest, PreserveEmptyValue) { | |
318 base::FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); | |
319 | |
320 ASSERT_TRUE(file_util::CopyFile( | |
321 data_dir_.AppendASCII("read.need_empty_value.json"), | |
322 pref_file)); | |
323 | |
324 PrefServiceMockBuilder builder; | |
325 builder.WithUserFilePrefs(pref_file, message_loop_.message_loop_proxy()); | |
326 scoped_refptr<PrefRegistrySyncable> registry(new PrefRegistrySyncable); | |
327 scoped_ptr<PrefServiceSyncable> prefs(builder.CreateSyncable(registry)); | |
328 | |
329 // Register testing prefs. | |
330 registry->RegisterListPref("list", | |
331 PrefRegistrySyncable::UNSYNCABLE_PREF); | |
332 registry->RegisterDictionaryPref("dict", | |
333 PrefRegistrySyncable::UNSYNCABLE_PREF); | |
334 | |
335 base::ListValue* non_empty_list = new base::ListValue; | |
336 non_empty_list->Append(base::Value::CreateStringValue("test")); | |
337 registry->RegisterListPref("list_needs_empty_value", | |
338 non_empty_list, | |
339 PrefRegistrySyncable::UNSYNCABLE_PREF); | |
340 | |
341 base::DictionaryValue* non_empty_dict = new base::DictionaryValue; | |
342 non_empty_dict->SetString("dummy", "whatever"); | |
343 registry->RegisterDictionaryPref("dict_needs_empty_value", | |
344 non_empty_dict, | |
345 PrefRegistrySyncable::UNSYNCABLE_PREF); | |
346 | |
347 // Set all testing prefs to empty. | |
348 ClearListValue(prefs.get(), "list"); | |
349 ClearListValue(prefs.get(), "list_needs_empty_value"); | |
350 ClearDictionaryValue(prefs.get(), "dict"); | |
351 ClearDictionaryValue(prefs.get(), "dict_needs_empty_value"); | |
352 | |
353 // Write to file. | |
354 prefs->CommitPendingWrite(); | |
355 message_loop_.RunUntilIdle(); | |
356 | |
357 // Compare to expected output. | |
358 base::FilePath golden_output_file = | |
359 data_dir_.AppendASCII("write.golden.need_empty_value.json"); | |
360 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | |
361 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); | |
362 } | |
363 | |
364 class PrefServiceSetValueTest : public testing::Test { | 228 class PrefServiceSetValueTest : public testing::Test { |
365 protected: | 229 protected: |
366 static const char kName[]; | 230 static const char kName[]; |
367 static const char kValue[]; | 231 static const char kValue[]; |
368 | 232 |
369 PrefServiceSetValueTest() : observer_(&prefs_) {} | 233 PrefServiceSetValueTest() : observer_(&prefs_) {} |
370 | 234 |
371 TestingPrefServiceSimple prefs_; | 235 TestingPrefServiceSimple prefs_; |
372 MockPrefChangeCallback observer_; | 236 MockPrefChangeCallback observer_; |
373 }; | 237 }; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 | 307 |
444 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); | 308 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
445 prefs_.Set(kName, new_value); | 309 prefs_.Set(kName, new_value); |
446 Mock::VerifyAndClearExpectations(&observer_); | 310 Mock::VerifyAndClearExpectations(&observer_); |
447 | 311 |
448 ListValue empty; | 312 ListValue empty; |
449 observer_.Expect(kName, &empty); | 313 observer_.Expect(kName, &empty); |
450 prefs_.Set(kName, empty); | 314 prefs_.Set(kName, empty); |
451 Mock::VerifyAndClearExpectations(&observer_); | 315 Mock::VerifyAndClearExpectations(&observer_); |
452 } | 316 } |
453 | |
454 class PrefServiceWebKitPrefs : public ChromeRenderViewHostTestHarness { | |
455 protected: | |
456 PrefServiceWebKitPrefs() : ui_thread_(BrowserThread::UI, &message_loop_) { | |
457 } | |
458 | |
459 virtual void SetUp() { | |
460 ChromeRenderViewHostTestHarness::SetUp(); | |
461 | |
462 // Supply our own profile so we use the correct profile data. The test | |
463 // harness is not supposed to overwrite a profile if it's already created. | |
464 | |
465 // Set some (WebKit) user preferences. | |
466 TestingPrefServiceSyncable* pref_services = | |
467 profile()->GetTestingPrefService(); | |
468 #if defined(TOOLKIT_GTK) | |
469 pref_services->SetUserPref(prefs::kUsesSystemTheme, | |
470 Value::CreateBooleanValue(false)); | |
471 #endif | |
472 pref_services->SetUserPref(prefs::kDefaultCharset, | |
473 Value::CreateStringValue("utf8")); | |
474 pref_services->SetUserPref(prefs::kWebKitDefaultFontSize, | |
475 Value::CreateIntegerValue(20)); | |
476 pref_services->SetUserPref(prefs::kWebKitTextAreasAreResizable, | |
477 Value::CreateBooleanValue(false)); | |
478 pref_services->SetUserPref(prefs::kWebKitUsesUniversalDetector, | |
479 Value::CreateBooleanValue(true)); | |
480 pref_services->SetUserPref("webkit.webprefs.foo", | |
481 Value::CreateStringValue("bar")); | |
482 } | |
483 | |
484 private: | |
485 content::TestBrowserThread ui_thread_; | |
486 }; | |
487 | |
488 // Tests to see that webkit preferences are properly loaded and copied over | |
489 // to a WebPreferences object. | |
490 TEST_F(PrefServiceWebKitPrefs, PrefsCopied) { | |
491 webkit_glue::WebPreferences webkit_prefs = | |
492 WebContentsTester::For(web_contents())->TestGetWebkitPrefs(); | |
493 | |
494 // These values have been overridden by the profile preferences. | |
495 EXPECT_EQ("UTF-8", webkit_prefs.default_encoding); | |
496 EXPECT_EQ(20, webkit_prefs.default_font_size); | |
497 EXPECT_FALSE(webkit_prefs.text_areas_are_resizable); | |
498 EXPECT_TRUE(webkit_prefs.uses_universal_detector); | |
499 | |
500 // These should still be the default values. | |
501 #if defined(OS_MACOSX) | |
502 const char kDefaultFont[] = "Times"; | |
503 #elif defined(OS_CHROMEOS) | |
504 const char kDefaultFont[] = "Tinos"; | |
505 #else | |
506 const char kDefaultFont[] = "Times New Roman"; | |
507 #endif | |
508 EXPECT_EQ(ASCIIToUTF16(kDefaultFont), | |
509 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]); | |
510 EXPECT_TRUE(webkit_prefs.javascript_enabled); | |
511 } | |
OLD | NEW |