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