| 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" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/prefs/json_pref_store.h" | 12 #include "base/prefs/json_pref_store.h" |
| 13 #include "base/prefs/public/pref_change_registrar.h" | 13 #include "base/prefs/public/pref_change_registrar.h" |
| 14 #include "base/prefs/testing_pref_store.h" | 14 #include "base/prefs/testing_pref_store.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 17 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 18 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 18 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 19 #include "chrome/browser/prefs/browser_prefs.h" | 19 #include "chrome/browser/prefs/browser_prefs.h" |
| 20 #include "chrome/browser/prefs/command_line_pref_store.h" | 20 #include "chrome/browser/prefs/command_line_pref_store.h" |
| 21 #include "chrome/browser/prefs/mock_pref_change_callback.h" | 21 #include "chrome/browser/prefs/mock_pref_change_callback.h" |
| 22 #include "chrome/browser/prefs/pref_registry_simple.h" |
| 22 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 23 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
| 23 #include "chrome/browser/prefs/pref_value_store.h" | 24 #include "chrome/browser/prefs/pref_value_store.h" |
| 24 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 25 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 25 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
| 26 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
| 27 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
| 28 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 29 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 29 #include "chrome/test/base/testing_pref_service.h" | 30 #include "chrome/test/base/testing_pref_service.h" |
| 30 #include "chrome/test/base/testing_profile.h" | 31 #include "chrome/test/base/testing_profile.h" |
| 31 #include "content/public/test/test_browser_thread.h" | 32 #include "content/public/test/test_browser_thread.h" |
| 32 #include "content/public/test/web_contents_tester.h" | 33 #include "content/public/test/web_contents_tester.h" |
| 33 #include "testing/gmock/include/gmock/gmock.h" | 34 #include "testing/gmock/include/gmock/gmock.h" |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 35 #include "ui/base/test/data/resource.h" | 36 #include "ui/base/test/data/resource.h" |
| 36 #include "webkit/glue/webpreferences.h" | 37 #include "webkit/glue/webpreferences.h" |
| 37 | 38 |
| 38 using content::BrowserThread; | 39 using content::BrowserThread; |
| 39 using content::WebContentsTester; | 40 using content::WebContentsTester; |
| 40 using testing::_; | 41 using testing::_; |
| 41 using testing::Mock; | 42 using testing::Mock; |
| 42 | 43 |
| 43 TEST(PrefServiceTest, NoObserverFire) { | 44 TEST(PrefServiceTest, NoObserverFire) { |
| 44 TestingPrefServiceSimple prefs; | 45 TestingPrefServiceSimple prefs; |
| 45 | 46 |
| 46 const char pref_name[] = "homepage"; | 47 const char pref_name[] = "homepage"; |
| 47 prefs.RegisterStringPref(pref_name, std::string()); | 48 prefs.registry()->RegisterStringPref(pref_name, std::string()); |
| 48 | 49 |
| 49 const char new_pref_value[] = "http://www.google.com/"; | 50 const char new_pref_value[] = "http://www.google.com/"; |
| 50 MockPrefChangeCallback obs(&prefs); | 51 MockPrefChangeCallback obs(&prefs); |
| 51 PrefChangeRegistrar registrar; | 52 PrefChangeRegistrar registrar; |
| 52 registrar.Init(&prefs); | 53 registrar.Init(&prefs); |
| 53 registrar.Add(pref_name, obs.GetCallback()); | 54 registrar.Add(pref_name, obs.GetCallback()); |
| 54 | 55 |
| 55 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged. | 56 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged. |
| 56 const StringValue expected_value(new_pref_value); | 57 const StringValue expected_value(new_pref_value); |
| 57 obs.Expect(pref_name, &expected_value); | 58 obs.Expect(pref_name, &expected_value); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 79 TEST(PrefServiceTest, HasPrefPath) { | 80 TEST(PrefServiceTest, HasPrefPath) { |
| 80 TestingPrefServiceSimple prefs; | 81 TestingPrefServiceSimple prefs; |
| 81 | 82 |
| 82 const char path[] = "fake.path"; | 83 const char path[] = "fake.path"; |
| 83 | 84 |
| 84 // Shouldn't initially have a path. | 85 // Shouldn't initially have a path. |
| 85 EXPECT_FALSE(prefs.HasPrefPath(path)); | 86 EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 86 | 87 |
| 87 // Register the path. This doesn't set a value, so the path still shouldn't | 88 // Register the path. This doesn't set a value, so the path still shouldn't |
| 88 // exist. | 89 // exist. |
| 89 prefs.RegisterStringPref(path, std::string()); | 90 prefs.registry()->RegisterStringPref(path, std::string()); |
| 90 EXPECT_FALSE(prefs.HasPrefPath(path)); | 91 EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 91 | 92 |
| 92 // Set a value and make sure we have a path. | 93 // Set a value and make sure we have a path. |
| 93 prefs.SetString(path, "blah"); | 94 prefs.SetString(path, "blah"); |
| 94 EXPECT_TRUE(prefs.HasPrefPath(path)); | 95 EXPECT_TRUE(prefs.HasPrefPath(path)); |
| 95 } | 96 } |
| 96 | 97 |
| 97 TEST(PrefServiceTest, Observers) { | 98 TEST(PrefServiceTest, Observers) { |
| 98 const char pref_name[] = "homepage"; | 99 const char pref_name[] = "homepage"; |
| 99 | 100 |
| 100 TestingPrefServiceSimple prefs; | 101 TestingPrefServiceSimple prefs; |
| 101 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com")); | 102 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com")); |
| 102 prefs.RegisterStringPref(pref_name, std::string()); | 103 prefs.registry()->RegisterStringPref(pref_name, std::string()); |
| 103 | 104 |
| 104 const char new_pref_value[] = "http://www.google.com/"; | 105 const char new_pref_value[] = "http://www.google.com/"; |
| 105 const StringValue expected_new_pref_value(new_pref_value); | 106 const StringValue expected_new_pref_value(new_pref_value); |
| 106 MockPrefChangeCallback obs(&prefs); | 107 MockPrefChangeCallback obs(&prefs); |
| 107 PrefChangeRegistrar registrar; | 108 PrefChangeRegistrar registrar; |
| 108 registrar.Init(&prefs); | 109 registrar.Init(&prefs); |
| 109 registrar.Add(pref_name, obs.GetCallback()); | 110 registrar.Add(pref_name, obs.GetCallback()); |
| 110 | 111 |
| 111 PrefChangeRegistrar registrar_two; | 112 PrefChangeRegistrar registrar_two; |
| 112 registrar_two.Init(&prefs); | 113 registrar_two.Init(&prefs); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 prefs.SetString(pref_name, new_pref_value); | 147 prefs.SetString(pref_name, new_pref_value); |
| 147 Mock::VerifyAndClearExpectations(&obs); | 148 Mock::VerifyAndClearExpectations(&obs); |
| 148 Mock::VerifyAndClearExpectations(&obs2); | 149 Mock::VerifyAndClearExpectations(&obs2); |
| 149 } | 150 } |
| 150 | 151 |
| 151 // Make sure that if a preference changes type, so the wrong type is stored in | 152 // Make sure that if a preference changes type, so the wrong type is stored in |
| 152 // the user pref file, it uses the correct fallback value instead. | 153 // the user pref file, it uses the correct fallback value instead. |
| 153 TEST(PrefServiceTest, GetValueChangedType) { | 154 TEST(PrefServiceTest, GetValueChangedType) { |
| 154 const int kTestValue = 10; | 155 const int kTestValue = 10; |
| 155 TestingPrefServiceSimple prefs; | 156 TestingPrefServiceSimple prefs; |
| 156 prefs.RegisterIntegerPref(prefs::kStabilityLaunchCount, kTestValue); | 157 prefs.registry()->RegisterIntegerPref( |
| 158 prefs::kStabilityLaunchCount, kTestValue); |
| 157 | 159 |
| 158 // Check falling back to a recommended value. | 160 // Check falling back to a recommended value. |
| 159 prefs.SetUserPref(prefs::kStabilityLaunchCount, | 161 prefs.SetUserPref(prefs::kStabilityLaunchCount, |
| 160 Value::CreateStringValue("not an integer")); | 162 Value::CreateStringValue("not an integer")); |
| 161 const PrefService::Preference* pref = | 163 const PrefService::Preference* pref = |
| 162 prefs.FindPreference(prefs::kStabilityLaunchCount); | 164 prefs.FindPreference(prefs::kStabilityLaunchCount); |
| 163 ASSERT_TRUE(pref); | 165 ASSERT_TRUE(pref); |
| 164 const Value* value = pref->GetValue(); | 166 const Value* value = pref->GetValue(); |
| 165 ASSERT_TRUE(value); | 167 ASSERT_TRUE(value); |
| 166 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); | 168 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 167 int actual_int_value = -1; | 169 int actual_int_value = -1; |
| 168 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 170 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 169 EXPECT_EQ(kTestValue, actual_int_value); | 171 EXPECT_EQ(kTestValue, actual_int_value); |
| 170 } | 172 } |
| 171 | 173 |
| 172 TEST(PrefServiceTest, UpdateCommandLinePrefStore) { | 174 TEST(PrefServiceTest, UpdateCommandLinePrefStore) { |
| 173 TestingPrefServiceSimple prefs; | 175 TestingPrefServiceSimple prefs; |
| 174 prefs.RegisterBooleanPref(prefs::kCloudPrintProxyEnabled, false); | 176 prefs.registry()->RegisterBooleanPref(prefs::kCloudPrintProxyEnabled, false); |
| 175 | 177 |
| 176 // Check to make sure the value is as expected. | 178 // Check to make sure the value is as expected. |
| 177 const PrefService::Preference* pref = | 179 const PrefService::Preference* pref = |
| 178 prefs.FindPreference(prefs::kCloudPrintProxyEnabled); | 180 prefs.FindPreference(prefs::kCloudPrintProxyEnabled); |
| 179 ASSERT_TRUE(pref); | 181 ASSERT_TRUE(pref); |
| 180 const Value* value = pref->GetValue(); | 182 const Value* value = pref->GetValue(); |
| 181 ASSERT_TRUE(value); | 183 ASSERT_TRUE(value); |
| 182 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); | 184 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); |
| 183 bool actual_bool_value = true; | 185 bool actual_bool_value = true; |
| 184 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); | 186 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 198 actual_bool_value = false; | 200 actual_bool_value = false; |
| 199 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); | 201 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); |
| 200 EXPECT_TRUE(actual_bool_value); | 202 EXPECT_TRUE(actual_bool_value); |
| 201 } | 203 } |
| 202 | 204 |
| 203 TEST(PrefServiceTest, GetValueAndGetRecommendedValue) { | 205 TEST(PrefServiceTest, GetValueAndGetRecommendedValue) { |
| 204 const int kDefaultValue = 5; | 206 const int kDefaultValue = 5; |
| 205 const int kUserValue = 10; | 207 const int kUserValue = 10; |
| 206 const int kRecommendedValue = 15; | 208 const int kRecommendedValue = 15; |
| 207 TestingPrefServiceSimple prefs; | 209 TestingPrefServiceSimple prefs; |
| 208 prefs.RegisterIntegerPref(prefs::kStabilityLaunchCount, kDefaultValue); | 210 prefs.registry()->RegisterIntegerPref( |
| 211 prefs::kStabilityLaunchCount, kDefaultValue); |
| 209 | 212 |
| 210 // Create pref with a default value only. | 213 // Create pref with a default value only. |
| 211 const PrefService::Preference* pref = | 214 const PrefService::Preference* pref = |
| 212 prefs.FindPreference(prefs::kStabilityLaunchCount); | 215 prefs.FindPreference(prefs::kStabilityLaunchCount); |
| 213 ASSERT_TRUE(pref); | 216 ASSERT_TRUE(pref); |
| 214 | 217 |
| 215 // Check that GetValue() returns the default value. | 218 // Check that GetValue() returns the default value. |
| 216 const Value* value = pref->GetValue(); | 219 const Value* value = pref->GetValue(); |
| 217 ASSERT_TRUE(value); | 220 ASSERT_TRUE(value); |
| 218 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); | 221 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 TestingPrefServiceSimple prefs_; | 369 TestingPrefServiceSimple prefs_; |
| 367 MockPrefChangeCallback observer_; | 370 MockPrefChangeCallback observer_; |
| 368 }; | 371 }; |
| 369 | 372 |
| 370 const char PrefServiceSetValueTest::kName[] = "name"; | 373 const char PrefServiceSetValueTest::kName[] = "name"; |
| 371 const char PrefServiceSetValueTest::kValue[] = "value"; | 374 const char PrefServiceSetValueTest::kValue[] = "value"; |
| 372 | 375 |
| 373 TEST_F(PrefServiceSetValueTest, SetStringValue) { | 376 TEST_F(PrefServiceSetValueTest, SetStringValue) { |
| 374 const char default_string[] = "default"; | 377 const char default_string[] = "default"; |
| 375 const StringValue default_value(default_string); | 378 const StringValue default_value(default_string); |
| 376 prefs_.RegisterStringPref(kName, default_string); | 379 prefs_.registry()->RegisterStringPref(kName, default_string); |
| 377 | 380 |
| 378 PrefChangeRegistrar registrar; | 381 PrefChangeRegistrar registrar; |
| 379 registrar.Init(&prefs_); | 382 registrar.Init(&prefs_); |
| 380 registrar.Add(kName, observer_.GetCallback()); | 383 registrar.Add(kName, observer_.GetCallback()); |
| 381 | 384 |
| 382 // Changing the controlling store from default to user triggers notification. | 385 // Changing the controlling store from default to user triggers notification. |
| 383 observer_.Expect(kName, &default_value); | 386 observer_.Expect(kName, &default_value); |
| 384 prefs_.Set(kName, default_value); | 387 prefs_.Set(kName, default_value); |
| 385 Mock::VerifyAndClearExpectations(&observer_); | 388 Mock::VerifyAndClearExpectations(&observer_); |
| 386 | 389 |
| 387 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); | 390 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
| 388 prefs_.Set(kName, default_value); | 391 prefs_.Set(kName, default_value); |
| 389 Mock::VerifyAndClearExpectations(&observer_); | 392 Mock::VerifyAndClearExpectations(&observer_); |
| 390 | 393 |
| 391 StringValue new_value(kValue); | 394 StringValue new_value(kValue); |
| 392 observer_.Expect(kName, &new_value); | 395 observer_.Expect(kName, &new_value); |
| 393 prefs_.Set(kName, new_value); | 396 prefs_.Set(kName, new_value); |
| 394 Mock::VerifyAndClearExpectations(&observer_); | 397 Mock::VerifyAndClearExpectations(&observer_); |
| 395 } | 398 } |
| 396 | 399 |
| 397 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { | 400 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { |
| 398 prefs_.RegisterDictionaryPref(kName); | 401 prefs_.registry()->RegisterDictionaryPref(kName); |
| 399 PrefChangeRegistrar registrar; | 402 PrefChangeRegistrar registrar; |
| 400 registrar.Init(&prefs_); | 403 registrar.Init(&prefs_); |
| 401 registrar.Add(kName, observer_.GetCallback()); | 404 registrar.Add(kName, observer_.GetCallback()); |
| 402 | 405 |
| 403 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); | 406 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
| 404 prefs_.RemoveUserPref(kName); | 407 prefs_.RemoveUserPref(kName); |
| 405 Mock::VerifyAndClearExpectations(&observer_); | 408 Mock::VerifyAndClearExpectations(&observer_); |
| 406 | 409 |
| 407 DictionaryValue new_value; | 410 DictionaryValue new_value; |
| 408 new_value.SetString(kName, kValue); | 411 new_value.SetString(kName, kValue); |
| 409 observer_.Expect(kName, &new_value); | 412 observer_.Expect(kName, &new_value); |
| 410 prefs_.Set(kName, new_value); | 413 prefs_.Set(kName, new_value); |
| 411 Mock::VerifyAndClearExpectations(&observer_); | 414 Mock::VerifyAndClearExpectations(&observer_); |
| 412 | 415 |
| 413 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); | 416 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
| 414 prefs_.Set(kName, new_value); | 417 prefs_.Set(kName, new_value); |
| 415 Mock::VerifyAndClearExpectations(&observer_); | 418 Mock::VerifyAndClearExpectations(&observer_); |
| 416 | 419 |
| 417 DictionaryValue empty; | 420 DictionaryValue empty; |
| 418 observer_.Expect(kName, &empty); | 421 observer_.Expect(kName, &empty); |
| 419 prefs_.Set(kName, empty); | 422 prefs_.Set(kName, empty); |
| 420 Mock::VerifyAndClearExpectations(&observer_); | 423 Mock::VerifyAndClearExpectations(&observer_); |
| 421 } | 424 } |
| 422 | 425 |
| 423 TEST_F(PrefServiceSetValueTest, SetListValue) { | 426 TEST_F(PrefServiceSetValueTest, SetListValue) { |
| 424 prefs_.RegisterListPref(kName); | 427 prefs_.registry()->RegisterListPref(kName); |
| 425 PrefChangeRegistrar registrar; | 428 PrefChangeRegistrar registrar; |
| 426 registrar.Init(&prefs_); | 429 registrar.Init(&prefs_); |
| 427 registrar.Add(kName, observer_.GetCallback()); | 430 registrar.Add(kName, observer_.GetCallback()); |
| 428 | 431 |
| 429 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); | 432 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
| 430 prefs_.RemoveUserPref(kName); | 433 prefs_.RemoveUserPref(kName); |
| 431 Mock::VerifyAndClearExpectations(&observer_); | 434 Mock::VerifyAndClearExpectations(&observer_); |
| 432 | 435 |
| 433 ListValue new_value; | 436 ListValue new_value; |
| 434 new_value.Append(Value::CreateStringValue(kValue)); | 437 new_value.Append(Value::CreateStringValue(kValue)); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 const char kDefaultFont[] = "Times"; | 500 const char kDefaultFont[] = "Times"; |
| 498 #elif defined(OS_CHROMEOS) | 501 #elif defined(OS_CHROMEOS) |
| 499 const char kDefaultFont[] = "Tinos"; | 502 const char kDefaultFont[] = "Tinos"; |
| 500 #else | 503 #else |
| 501 const char kDefaultFont[] = "Times New Roman"; | 504 const char kDefaultFont[] = "Times New Roman"; |
| 502 #endif | 505 #endif |
| 503 EXPECT_EQ(ASCIIToUTF16(kDefaultFont), | 506 EXPECT_EQ(ASCIIToUTF16(kDefaultFont), |
| 504 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]); | 507 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]); |
| 505 EXPECT_TRUE(webkit_prefs.javascript_enabled); | 508 EXPECT_TRUE(webkit_prefs.javascript_enabled); |
| 506 } | 509 } |
| OLD | NEW |