Index: chrome/browser/prefs/pref_service_unittest.cc |
diff --git a/chrome/browser/prefs/pref_service_unittest.cc b/chrome/browser/prefs/pref_service_unittest.cc |
index 1a797083b147e6a9e4a105ce8702f96ef78b8ba6..7f60bdec08bd81c515822a2d66fd0deecc5a1e8a 100644 |
--- a/chrome/browser/prefs/pref_service_unittest.cc |
+++ b/chrome/browser/prefs/pref_service_unittest.cc |
@@ -1,4 +1,4 @@ |
-// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
@@ -8,6 +8,7 @@ |
#include "base/command_line.h" |
#include "base/scoped_ptr.h" |
#include "base/values.h" |
+#include "chrome/browser/extensions/extension_pref_store.h" |
#include "chrome/browser/policy/configuration_policy_pref_store.h" |
#include "chrome/browser/policy/mock_configuration_policy_provider.h" |
#include "chrome/browser/prefs/browser_prefs.h" |
@@ -148,6 +149,27 @@ TEST(PrefServiceTest, Observers) { |
Mock::VerifyAndClearExpectations(&obs2); |
} |
+// Make sure that if a preference changes type, so the wrong type is stored in |
+// the user pref file, it uses the correct fallback value instead. |
+TEST(PrefServiceTest, GetValueChangedType) { |
+ const int kTestValue = 10; |
+ TestingPrefService prefs; |
+ prefs.RegisterIntegerPref(prefs::kStabilityLaunchCount, kTestValue); |
+ |
+ // Check falling back to a recommended value. |
+ prefs.SetUserPref(prefs::kStabilityLaunchCount, |
+ Value::CreateStringValue("not an integer")); |
+ const PrefService::Preference* pref = |
+ prefs.FindPreference(prefs::kStabilityLaunchCount); |
+ ASSERT_TRUE(pref); |
+ const Value* value = pref->GetValue(); |
+ ASSERT_TRUE(value); |
+ EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
+ int actual_int_value = -1; |
+ EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
+ EXPECT_EQ(kTestValue, actual_int_value); |
+} |
+ |
TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineOptions) { |
CommandLine command_line(CommandLine::NO_PROGRAM); |
command_line.AppendSwitchASCII(switches::kProxyBypassList, "123"); |
@@ -296,11 +318,7 @@ class PrefServiceSetValueTest : public testing::Test { |
static const char kName[]; |
static const char kValue[]; |
- PrefServiceSetValueTest() |
- : null_value_(Value::CreateNullValue()) {} |
- |
TestingPrefService prefs_; |
- scoped_ptr<Value> null_value_; |
PrefObserverMock observer_; |
}; |
@@ -337,10 +355,8 @@ TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { |
registrar.Init(&prefs_); |
registrar.Add(kName, &observer_); |
- // Dictionary values are special: setting one to NULL is the same as clearing |
- // the user value, allowing the NULL default to take (or keep) control. |
EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
- prefs_.Set(kName, *null_value_); |
+ prefs_.RemoveUserPref(kName); |
Mock::VerifyAndClearExpectations(&observer_); |
DictionaryValue new_value; |
@@ -353,8 +369,9 @@ TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { |
prefs_.Set(kName, new_value); |
Mock::VerifyAndClearExpectations(&observer_); |
- observer_.Expect(&prefs_, kName, null_value_.get()); |
- prefs_.Set(kName, *null_value_); |
+ DictionaryValue empty; |
+ observer_.Expect(&prefs_, kName, &empty); |
+ prefs_.Set(kName, empty); |
Mock::VerifyAndClearExpectations(&observer_); |
} |
@@ -364,10 +381,8 @@ TEST_F(PrefServiceSetValueTest, SetListValue) { |
registrar.Init(&prefs_); |
registrar.Add(kName, &observer_); |
- // List values are special: setting one to NULL is the same as clearing the |
- // user value, allowing the NULL default to take (or keep) control. |
EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
- prefs_.Set(kName, *null_value_); |
+ prefs_.RemoveUserPref(kName); |
Mock::VerifyAndClearExpectations(&observer_); |
ListValue new_value; |
@@ -380,7 +395,37 @@ TEST_F(PrefServiceSetValueTest, SetListValue) { |
prefs_.Set(kName, new_value); |
Mock::VerifyAndClearExpectations(&observer_); |
- observer_.Expect(&prefs_, kName, null_value_.get()); |
- prefs_.Set(kName, *null_value_); |
+ ListValue empty; |
+ observer_.Expect(&prefs_, kName, &empty); |
+ prefs_.Set(kName, empty); |
+ Mock::VerifyAndClearExpectations(&observer_); |
+} |
+ |
+class PrefServiceIncognitoTest : public PrefServiceSetValueTest {}; |
+ |
+TEST_F(PrefServiceIncognitoTest, SetValue) { |
+ const char default_string[] = "default"; |
+ const StringValue default_value(default_string); |
+ prefs_.RegisterStringPref(kName, default_string); |
+ |
+ scoped_refptr<PrefStore> incognito_extension_prefs( |
+ new ExtensionPrefStore(true)); |
+ scoped_ptr<PrefService> overlay_pref_service( |
+ prefs_.CreateIncognitoPrefService(incognito_extension_prefs.get())); |
+ |
+ PrefChangeRegistrar registrar; |
+ registrar.Init(&prefs_); |
+ registrar.Add(kName, &observer_); |
+ |
+ EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
+ StringValue new_value(kValue); |
+ overlay_pref_service->Set(kName, new_value); |
Mock::VerifyAndClearExpectations(&observer_); |
+ |
+ // Check that the value is only set in the overlay pref service. |
+ std::string value; |
+ value = prefs_.GetString(kName); |
+ EXPECT_EQ(std::string(default_string), value); |
+ value = overlay_pref_service->GetString(kName); |
+ EXPECT_EQ(std::string(kValue), value); |
} |