| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/test/data/resource.h" | 7 #include "app/test/data/resource.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/extension_pref_store.h" |
| 11 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 12 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 13 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 13 #include "chrome/browser/prefs/browser_prefs.h" | 14 #include "chrome/browser/prefs/browser_prefs.h" |
| 14 #include "chrome/browser/prefs/command_line_pref_store.h" | 15 #include "chrome/browser/prefs/command_line_pref_store.h" |
| 15 #include "chrome/browser/prefs/pref_change_registrar.h" | 16 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 16 #include "chrome/browser/prefs/pref_observer_mock.h" | 17 #include "chrome/browser/prefs/pref_observer_mock.h" |
| 17 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 18 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
| 18 #include "chrome/browser/prefs/pref_value_store.h" | 19 #include "chrome/browser/prefs/pref_value_store.h" |
| 19 #include "chrome/browser/prefs/testing_pref_store.h" | 20 #include "chrome/browser/prefs/testing_pref_store.h" |
| 20 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 // Make sure obs2 still works after removing obs. | 141 // Make sure obs2 still works after removing obs. |
| 141 registrar.Remove(pref_name, &obs); | 142 registrar.Remove(pref_name, &obs); |
| 142 EXPECT_CALL(obs, Observe(_, _, _)).Times(0); | 143 EXPECT_CALL(obs, Observe(_, _, _)).Times(0); |
| 143 obs2.Expect(&prefs, pref_name, &expected_new_pref_value); | 144 obs2.Expect(&prefs, pref_name, &expected_new_pref_value); |
| 144 // This should only fire the observer in obs2. | 145 // This should only fire the observer in obs2. |
| 145 prefs.SetString(pref_name, new_pref_value); | 146 prefs.SetString(pref_name, new_pref_value); |
| 146 Mock::VerifyAndClearExpectations(&obs); | 147 Mock::VerifyAndClearExpectations(&obs); |
| 147 Mock::VerifyAndClearExpectations(&obs2); | 148 Mock::VerifyAndClearExpectations(&obs2); |
| 148 } | 149 } |
| 149 | 150 |
| 151 // 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 TEST(PrefServiceTest, GetValueChangedType) { |
| 154 const int kTestValue = 10; |
| 155 TestingPrefService prefs; |
| 156 prefs.RegisterIntegerPref(prefs::kStabilityLaunchCount, kTestValue); |
| 157 |
| 158 // Check falling back to a recommended value. |
| 159 prefs.SetUserPref(prefs::kStabilityLaunchCount, |
| 160 Value::CreateStringValue("not an integer")); |
| 161 const PrefService::Preference* pref = |
| 162 prefs.FindPreference(prefs::kStabilityLaunchCount); |
| 163 ASSERT_TRUE(pref); |
| 164 const Value* value = pref->GetValue(); |
| 165 ASSERT_TRUE(value); |
| 166 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 167 int actual_int_value = -1; |
| 168 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 169 EXPECT_EQ(kTestValue, actual_int_value); |
| 170 } |
| 171 |
| 150 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineOptions) { | 172 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineOptions) { |
| 151 CommandLine command_line(CommandLine::NO_PROGRAM); | 173 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 152 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123"); | 174 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123"); |
| 153 command_line.AppendSwitchASCII(switches::kProxyPacUrl, "456"); | 175 command_line.AppendSwitchASCII(switches::kProxyPacUrl, "456"); |
| 154 command_line.AppendSwitchASCII(switches::kProxyServer, "789"); | 176 command_line.AppendSwitchASCII(switches::kProxyServer, "789"); |
| 155 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( | 177 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( |
| 156 new policy::MockConfigurationPolicyProvider()); | 178 new policy::MockConfigurationPolicyProvider()); |
| 157 Value* mode_value = Value::CreateIntegerValue( | 179 Value* mode_value = Value::CreateIntegerValue( |
| 158 policy::kPolicyManuallyConfiguredProxyMode); | 180 policy::kPolicyManuallyConfiguredProxyMode); |
| 159 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value); | 181 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyServer)); | 318 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyServer)); |
| 297 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyPacUrl)); | 319 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyPacUrl)); |
| 298 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyBypassList)); | 320 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyBypassList)); |
| 299 } | 321 } |
| 300 | 322 |
| 301 class PrefServiceSetValueTest : public testing::Test { | 323 class PrefServiceSetValueTest : public testing::Test { |
| 302 protected: | 324 protected: |
| 303 static const char kName[]; | 325 static const char kName[]; |
| 304 static const char kValue[]; | 326 static const char kValue[]; |
| 305 | 327 |
| 306 PrefServiceSetValueTest() | |
| 307 : null_value_(Value::CreateNullValue()) {} | |
| 308 | |
| 309 TestingPrefService prefs_; | 328 TestingPrefService prefs_; |
| 310 scoped_ptr<Value> null_value_; | |
| 311 PrefObserverMock observer_; | 329 PrefObserverMock observer_; |
| 312 }; | 330 }; |
| 313 | 331 |
| 314 const char PrefServiceSetValueTest::kName[] = "name"; | 332 const char PrefServiceSetValueTest::kName[] = "name"; |
| 315 const char PrefServiceSetValueTest::kValue[] = "value"; | 333 const char PrefServiceSetValueTest::kValue[] = "value"; |
| 316 | 334 |
| 317 TEST_F(PrefServiceSetValueTest, SetStringValue) { | 335 TEST_F(PrefServiceSetValueTest, SetStringValue) { |
| 318 const char default_string[] = "default"; | 336 const char default_string[] = "default"; |
| 319 const StringValue default_value(default_string); | 337 const StringValue default_value(default_string); |
| 320 prefs_.RegisterStringPref(kName, default_string); | 338 prefs_.RegisterStringPref(kName, default_string); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 337 prefs_.Set(kName, new_value); | 355 prefs_.Set(kName, new_value); |
| 338 Mock::VerifyAndClearExpectations(&observer_); | 356 Mock::VerifyAndClearExpectations(&observer_); |
| 339 } | 357 } |
| 340 | 358 |
| 341 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { | 359 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { |
| 342 prefs_.RegisterDictionaryPref(kName); | 360 prefs_.RegisterDictionaryPref(kName); |
| 343 PrefChangeRegistrar registrar; | 361 PrefChangeRegistrar registrar; |
| 344 registrar.Init(&prefs_); | 362 registrar.Init(&prefs_); |
| 345 registrar.Add(kName, &observer_); | 363 registrar.Add(kName, &observer_); |
| 346 | 364 |
| 347 // Dictionary values are special: setting one to NULL is the same as clearing | |
| 348 // the user value, allowing the NULL default to take (or keep) control. | |
| 349 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); | 365 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
| 350 prefs_.Set(kName, *null_value_); | 366 prefs_.RemoveUserPref(kName); |
| 351 Mock::VerifyAndClearExpectations(&observer_); | 367 Mock::VerifyAndClearExpectations(&observer_); |
| 352 | 368 |
| 353 DictionaryValue new_value; | 369 DictionaryValue new_value; |
| 354 new_value.SetString(kName, kValue); | 370 new_value.SetString(kName, kValue); |
| 355 observer_.Expect(&prefs_, kName, &new_value); | 371 observer_.Expect(&prefs_, kName, &new_value); |
| 356 prefs_.Set(kName, new_value); | 372 prefs_.Set(kName, new_value); |
| 357 Mock::VerifyAndClearExpectations(&observer_); | 373 Mock::VerifyAndClearExpectations(&observer_); |
| 358 | 374 |
| 359 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); | 375 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
| 360 prefs_.Set(kName, new_value); | 376 prefs_.Set(kName, new_value); |
| 361 Mock::VerifyAndClearExpectations(&observer_); | 377 Mock::VerifyAndClearExpectations(&observer_); |
| 362 | 378 |
| 363 observer_.Expect(&prefs_, kName, null_value_.get()); | 379 DictionaryValue empty; |
| 364 prefs_.Set(kName, *null_value_); | 380 observer_.Expect(&prefs_, kName, &empty); |
| 381 prefs_.Set(kName, empty); |
| 365 Mock::VerifyAndClearExpectations(&observer_); | 382 Mock::VerifyAndClearExpectations(&observer_); |
| 366 } | 383 } |
| 367 | 384 |
| 368 TEST_F(PrefServiceSetValueTest, SetListValue) { | 385 TEST_F(PrefServiceSetValueTest, SetListValue) { |
| 369 prefs_.RegisterListPref(kName); | 386 prefs_.RegisterListPref(kName); |
| 370 PrefChangeRegistrar registrar; | 387 PrefChangeRegistrar registrar; |
| 371 registrar.Init(&prefs_); | 388 registrar.Init(&prefs_); |
| 372 registrar.Add(kName, &observer_); | 389 registrar.Add(kName, &observer_); |
| 373 | 390 |
| 374 // List values are special: setting one to NULL is the same as clearing the | |
| 375 // user value, allowing the NULL default to take (or keep) control. | |
| 376 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); | 391 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
| 377 prefs_.Set(kName, *null_value_); | 392 prefs_.RemoveUserPref(kName); |
| 378 Mock::VerifyAndClearExpectations(&observer_); | 393 Mock::VerifyAndClearExpectations(&observer_); |
| 379 | 394 |
| 380 ListValue new_value; | 395 ListValue new_value; |
| 381 new_value.Append(Value::CreateStringValue(kValue)); | 396 new_value.Append(Value::CreateStringValue(kValue)); |
| 382 observer_.Expect(&prefs_, kName, &new_value); | 397 observer_.Expect(&prefs_, kName, &new_value); |
| 383 prefs_.Set(kName, new_value); | 398 prefs_.Set(kName, new_value); |
| 384 Mock::VerifyAndClearExpectations(&observer_); | 399 Mock::VerifyAndClearExpectations(&observer_); |
| 385 | 400 |
| 386 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); | 401 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
| 387 prefs_.Set(kName, new_value); | 402 prefs_.Set(kName, new_value); |
| 388 Mock::VerifyAndClearExpectations(&observer_); | 403 Mock::VerifyAndClearExpectations(&observer_); |
| 389 | 404 |
| 390 observer_.Expect(&prefs_, kName, null_value_.get()); | 405 ListValue empty; |
| 391 prefs_.Set(kName, *null_value_); | 406 observer_.Expect(&prefs_, kName, &empty); |
| 407 prefs_.Set(kName, empty); |
| 392 Mock::VerifyAndClearExpectations(&observer_); | 408 Mock::VerifyAndClearExpectations(&observer_); |
| 393 } | 409 } |
| 410 |
| 411 class PrefServiceIncognitoTest : public PrefServiceSetValueTest {}; |
| 412 |
| 413 TEST_F(PrefServiceIncognitoTest, SetValue) { |
| 414 const char default_string[] = "default"; |
| 415 const StringValue default_value(default_string); |
| 416 prefs_.RegisterStringPref(kName, default_string); |
| 417 |
| 418 scoped_refptr<PrefStore> incognito_extension_prefs(new ExtensionPrefStore); |
| 419 scoped_ptr<PrefService> overlay_pref_service( |
| 420 prefs_.CreateIncognitoPrefService(incognito_extension_prefs.get())); |
| 421 |
| 422 PrefChangeRegistrar registrar; |
| 423 registrar.Init(&prefs_); |
| 424 registrar.Add(kName, &observer_); |
| 425 |
| 426 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
| 427 StringValue new_value(kValue); |
| 428 overlay_pref_service->Set(kName, new_value); |
| 429 Mock::VerifyAndClearExpectations(&observer_); |
| 430 |
| 431 // Check that the value is only set in the overlay pref service. |
| 432 std::string value; |
| 433 value = prefs_.GetString(kName); |
| 434 EXPECT_EQ(std::string(default_string), value); |
| 435 value = overlay_pref_service->GetString(kName); |
| 436 EXPECT_EQ(std::string(kValue), value); |
| 437 } |
| OLD | NEW |