blob: 3c04dd24e602074ec854023fcd5bb334e317da0a [file] [log] [blame]
[email protected]d3b05ea2012-01-24 22:57:051// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]ecde2742010-04-02 17:36:185#include <string>
6
[email protected]ce1850e92010-10-15 08:40:587#include "base/command_line.h"
[email protected]3b63f8f42011-03-28 01:54:158#include "base/memory/scoped_ptr.h"
[email protected]1bec2372011-12-07 09:19:069#include "base/utf_string_conversions.h"
[email protected]ecde2742010-04-02 17:36:1810#include "base/values.h"
[email protected]ce1850e92010-10-15 08:40:5811#include "chrome/browser/policy/configuration_policy_pref_store.h"
12#include "chrome/browser/policy/mock_configuration_policy_provider.h"
13#include "chrome/browser/prefs/browser_prefs.h"
14#include "chrome/browser/prefs/command_line_pref_store.h"
[email protected]2fb7dc982010-09-29 12:24:2815#include "chrome/browser/prefs/pref_change_registrar.h"
[email protected]acd78969c2010-12-08 09:49:1116#include "chrome/browser/prefs/pref_observer_mock.h"
[email protected]f2d1f612010-12-09 15:10:1717#include "chrome/browser/prefs/pref_service_mock_builder.h"
[email protected]37858e52010-08-26 00:22:0218#include "chrome/browser/prefs/pref_value_store.h"
[email protected]f2d1f612010-12-09 15:10:1719#include "chrome/browser/prefs/testing_pref_store.h"
initial.commit09911bf2008-07-26 23:55:2920#include "chrome/common/chrome_paths.h"
[email protected]ce1850e92010-10-15 08:40:5821#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2922#include "chrome/common/pref_names.h"
[email protected]1bec2372011-12-07 09:19:0623#include "chrome/test/base/chrome_render_view_host_test_harness.h"
[email protected]8ad3636e2011-08-01 22:31:4024#include "chrome/test/base/testing_pref_service.h"
[email protected]1bec2372011-12-07 09:19:0625#include "chrome/test/base/testing_profile.h"
26#include "content/browser/tab_contents/test_tab_contents.h"
27#include "content/test/test_browser_thread.h"
[email protected]ecde2742010-04-02 17:36:1828#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2929#include "testing/gtest/include/gtest/gtest.h"
[email protected]41110ca2011-06-27 16:45:4930#include "ui/base/test/data/resource.h"
initial.commit09911bf2008-07-26 23:55:2931
[email protected]1bec2372011-12-07 09:19:0632using content::BrowserThread;
[email protected]ecde2742010-04-02 17:36:1833using testing::_;
34using testing::Mock;
[email protected]12a3c022010-11-03 10:24:1135
[email protected]277404c22010-04-22 13:09:4536TEST(PrefServiceTest, NoObserverFire) {
[email protected]74379bc52010-07-21 13:54:0837 TestingPrefService prefs;
[email protected]7aa0a962010-04-21 17:24:4238
[email protected]57ecc4b2010-08-11 03:02:5139 const char pref_name[] = "homepage";
[email protected]12a3c022010-11-03 10:24:1140 prefs.RegisterStringPref(pref_name, std::string());
[email protected]7aa0a962010-04-21 17:24:4241
[email protected]acd78969c2010-12-08 09:49:1142 const char new_pref_value[] = "http://www.google.com/";
43 PrefObserverMock obs;
[email protected]2fb7dc982010-09-29 12:24:2844 PrefChangeRegistrar registrar;
45 registrar.Init(&prefs);
46 registrar.Add(pref_name, &obs);
[email protected]7aa0a962010-04-21 17:24:4247
[email protected]acd78969c2010-12-08 09:49:1148 // This should fire the checks in PrefObserverMock::Observe.
49 const StringValue expected_value(new_pref_value);
50 obs.Expect(&prefs, pref_name, &expected_value);
51 prefs.SetString(pref_name, new_pref_value);
52 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4253
54 // Setting the pref to the same value should not set the pref value a second
55 // time.
[email protected]acd78969c2010-12-08 09:49:1156 EXPECT_CALL(obs, Observe(_, _, _)).Times(0);
[email protected]7aa0a962010-04-21 17:24:4257 prefs.SetString(pref_name, new_pref_value);
[email protected]acd78969c2010-12-08 09:49:1158 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4259
60 // Clearing the pref should cause the pref to fire.
[email protected]acd78969c2010-12-08 09:49:1161 const StringValue expected_default_value("");
62 obs.Expect(&prefs, pref_name, &expected_default_value);
[email protected]7aa0a962010-04-21 17:24:4263 prefs.ClearPref(pref_name);
[email protected]acd78969c2010-12-08 09:49:1164 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4265
66 // Clearing the pref again should not cause the pref to fire.
[email protected]acd78969c2010-12-08 09:49:1167 EXPECT_CALL(obs, Observe(_, _, _)).Times(0);
[email protected]7aa0a962010-04-21 17:24:4268 prefs.ClearPref(pref_name);
[email protected]acd78969c2010-12-08 09:49:1169 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4270}
71
[email protected]277404c22010-04-22 13:09:4572TEST(PrefServiceTest, HasPrefPath) {
[email protected]74379bc52010-07-21 13:54:0873 TestingPrefService prefs;
[email protected]7aa0a962010-04-21 17:24:4274
[email protected]57ecc4b2010-08-11 03:02:5175 const char path[] = "fake.path";
[email protected]7aa0a962010-04-21 17:24:4276
77 // Shouldn't initially have a path.
78 EXPECT_FALSE(prefs.HasPrefPath(path));
79
80 // Register the path. This doesn't set a value, so the path still shouldn't
81 // exist.
[email protected]20ce516d2010-06-18 02:20:0482 prefs.RegisterStringPref(path, std::string());
[email protected]7aa0a962010-04-21 17:24:4283 EXPECT_FALSE(prefs.HasPrefPath(path));
84
85 // Set a value and make sure we have a path.
[email protected]ddd231e2010-06-29 20:35:1986 prefs.SetString(path, "blah");
[email protected]7aa0a962010-04-21 17:24:4287 EXPECT_TRUE(prefs.HasPrefPath(path));
88}
89
[email protected]277404c22010-04-22 13:09:4590TEST(PrefServiceTest, Observers) {
[email protected]57ecc4b2010-08-11 03:02:5191 const char pref_name[] = "homepage";
[email protected]277404c22010-04-22 13:09:4592
[email protected]74379bc52010-07-21 13:54:0893 TestingPrefService prefs;
[email protected]57ecc4b2010-08-11 03:02:5194 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com"));
[email protected]12a3c022010-11-03 10:24:1195 prefs.RegisterStringPref(pref_name, std::string());
[email protected]277404c22010-04-22 13:09:4596
[email protected]acd78969c2010-12-08 09:49:1197 const char new_pref_value[] = "http://www.google.com/";
98 const StringValue expected_new_pref_value(new_pref_value);
99 PrefObserverMock obs;
[email protected]2fb7dc982010-09-29 12:24:28100 PrefChangeRegistrar registrar;
101 registrar.Init(&prefs);
102 registrar.Add(pref_name, &obs);
[email protected]277404c22010-04-22 13:09:45103
[email protected]acd78969c2010-12-08 09:49:11104 // This should fire the checks in PrefObserverMock::Observe.
105 obs.Expect(&prefs, pref_name, &expected_new_pref_value);
106 prefs.SetString(pref_name, new_pref_value);
107 Mock::VerifyAndClearExpectations(&obs);
[email protected]277404c22010-04-22 13:09:45108
109 // Now try adding a second pref observer.
[email protected]acd78969c2010-12-08 09:49:11110 const char new_pref_value2[] = "http://www.youtube.com/";
111 const StringValue expected_new_pref_value2(new_pref_value2);
112 PrefObserverMock obs2;
113 obs.Expect(&prefs, pref_name, &expected_new_pref_value2);
114 obs2.Expect(&prefs, pref_name, &expected_new_pref_value2);
[email protected]2fb7dc982010-09-29 12:24:28115 registrar.Add(pref_name, &obs2);
[email protected]277404c22010-04-22 13:09:45116 // This should fire the checks in obs and obs2.
117 prefs.SetString(pref_name, new_pref_value2);
[email protected]acd78969c2010-12-08 09:49:11118 Mock::VerifyAndClearExpectations(&obs);
119 Mock::VerifyAndClearExpectations(&obs2);
[email protected]277404c22010-04-22 13:09:45120
121 // Make sure obs2 still works after removing obs.
[email protected]2fb7dc982010-09-29 12:24:28122 registrar.Remove(pref_name, &obs);
[email protected]acd78969c2010-12-08 09:49:11123 EXPECT_CALL(obs, Observe(_, _, _)).Times(0);
124 obs2.Expect(&prefs, pref_name, &expected_new_pref_value);
[email protected]277404c22010-04-22 13:09:45125 // This should only fire the observer in obs2.
126 prefs.SetString(pref_name, new_pref_value);
[email protected]acd78969c2010-12-08 09:49:11127 Mock::VerifyAndClearExpectations(&obs);
128 Mock::VerifyAndClearExpectations(&obs2);
[email protected]277404c22010-04-22 13:09:45129}
130
[email protected]9a8c4022011-01-25 14:25:33131// Make sure that if a preference changes type, so the wrong type is stored in
132// the user pref file, it uses the correct fallback value instead.
133TEST(PrefServiceTest, GetValueChangedType) {
134 const int kTestValue = 10;
135 TestingPrefService prefs;
136 prefs.RegisterIntegerPref(prefs::kStabilityLaunchCount, kTestValue);
137
138 // Check falling back to a recommended value.
139 prefs.SetUserPref(prefs::kStabilityLaunchCount,
140 Value::CreateStringValue("not an integer"));
141 const PrefService::Preference* pref =
142 prefs.FindPreference(prefs::kStabilityLaunchCount);
143 ASSERT_TRUE(pref);
144 const Value* value = pref->GetValue();
145 ASSERT_TRUE(value);
146 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
147 int actual_int_value = -1;
148 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
149 EXPECT_EQ(kTestValue, actual_int_value);
150}
151
[email protected]d3b05ea2012-01-24 22:57:05152TEST(PrefServiceTest, UpdateCommandLinePrefStore) {
153 TestingPrefService prefs;
154 prefs.RegisterBooleanPref(prefs::kCloudPrintProxyEnabled, false);
155
156 // Check to make sure the value is as expected.
157 const PrefService::Preference* pref =
158 prefs.FindPreference(prefs::kCloudPrintProxyEnabled);
159 ASSERT_TRUE(pref);
160 const Value* value = pref->GetValue();
161 ASSERT_TRUE(value);
162 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType());
163 bool actual_bool_value = true;
164 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value));
165 EXPECT_EQ(false, actual_bool_value);
166
167 // Change the command line.
168 CommandLine cmd_line(CommandLine::NO_PROGRAM);
169 cmd_line.AppendSwitch(switches::kEnableCloudPrintProxy);
170
171 // Call UpdateCommandLinePrefStore and check to see if the value has changed.
172 prefs.UpdateCommandLinePrefStore(&cmd_line);
173 pref = prefs.FindPreference(prefs::kCloudPrintProxyEnabled);
174 ASSERT_TRUE(pref);
175 value = pref->GetValue();
176 ASSERT_TRUE(value);
177 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType());
178 actual_bool_value = false;
179 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value));
180 EXPECT_EQ(true, actual_bool_value);
181}
182
[email protected]ecde2742010-04-02 17:36:18183class PrefServiceSetValueTest : public testing::Test {
184 protected:
[email protected]12a3c022010-11-03 10:24:11185 static const char kName[];
186 static const char kValue[];
[email protected]ecde2742010-04-02 17:36:18187
[email protected]74379bc52010-07-21 13:54:08188 TestingPrefService prefs_;
[email protected]acd78969c2010-12-08 09:49:11189 PrefObserverMock observer_;
[email protected]ecde2742010-04-02 17:36:18190};
[email protected]ddd231e2010-06-29 20:35:19191
[email protected]12a3c022010-11-03 10:24:11192const char PrefServiceSetValueTest::kName[] = "name";
193const char PrefServiceSetValueTest::kValue[] = "value";
[email protected]ecde2742010-04-02 17:36:18194
195TEST_F(PrefServiceSetValueTest, SetStringValue) {
[email protected]20ce516d2010-06-18 02:20:04196 const char default_string[] = "default";
[email protected]acd78969c2010-12-08 09:49:11197 const StringValue default_value(default_string);
[email protected]12a3c022010-11-03 10:24:11198 prefs_.RegisterStringPref(kName, default_string);
[email protected]2fb7dc982010-09-29 12:24:28199
200 PrefChangeRegistrar registrar;
201 registrar.Init(&prefs_);
[email protected]12a3c022010-11-03 10:24:11202 registrar.Add(kName, &observer_);
[email protected]2fb7dc982010-09-29 12:24:28203
[email protected]c3b54f372010-09-14 08:25:07204 // Changing the controlling store from default to user triggers notification.
[email protected]acd78969c2010-12-08 09:49:11205 observer_.Expect(&prefs_, kName, &default_value);
206 prefs_.Set(kName, default_value);
[email protected]c3b54f372010-09-14 08:25:07207 Mock::VerifyAndClearExpectations(&observer_);
208
[email protected]acd78969c2010-12-08 09:49:11209 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
210 prefs_.Set(kName, default_value);
[email protected]ecde2742010-04-02 17:36:18211 Mock::VerifyAndClearExpectations(&observer_);
212
[email protected]acd78969c2010-12-08 09:49:11213 StringValue new_value(kValue);
214 observer_.Expect(&prefs_, kName, &new_value);
215 prefs_.Set(kName, new_value);
216 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18217}
218
219TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
[email protected]12a3c022010-11-03 10:24:11220 prefs_.RegisterDictionaryPref(kName);
[email protected]2fb7dc982010-09-29 12:24:28221 PrefChangeRegistrar registrar;
222 registrar.Init(&prefs_);
[email protected]12a3c022010-11-03 10:24:11223 registrar.Add(kName, &observer_);
[email protected]ecde2742010-04-02 17:36:18224
[email protected]acd78969c2010-12-08 09:49:11225 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
[email protected]9a8c4022011-01-25 14:25:33226 prefs_.RemoveUserPref(kName);
[email protected]ecde2742010-04-02 17:36:18227 Mock::VerifyAndClearExpectations(&observer_);
228
229 DictionaryValue new_value;
[email protected]12a3c022010-11-03 10:24:11230 new_value.SetString(kName, kValue);
[email protected]acd78969c2010-12-08 09:49:11231 observer_.Expect(&prefs_, kName, &new_value);
[email protected]12a3c022010-11-03 10:24:11232 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18233 Mock::VerifyAndClearExpectations(&observer_);
234
[email protected]acd78969c2010-12-08 09:49:11235 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
236 prefs_.Set(kName, new_value);
237 Mock::VerifyAndClearExpectations(&observer_);
238
[email protected]9a8c4022011-01-25 14:25:33239 DictionaryValue empty;
240 observer_.Expect(&prefs_, kName, &empty);
241 prefs_.Set(kName, empty);
[email protected]ecde2742010-04-02 17:36:18242 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18243}
244
245TEST_F(PrefServiceSetValueTest, SetListValue) {
[email protected]12a3c022010-11-03 10:24:11246 prefs_.RegisterListPref(kName);
[email protected]2fb7dc982010-09-29 12:24:28247 PrefChangeRegistrar registrar;
248 registrar.Init(&prefs_);
[email protected]12a3c022010-11-03 10:24:11249 registrar.Add(kName, &observer_);
[email protected]ecde2742010-04-02 17:36:18250
[email protected]acd78969c2010-12-08 09:49:11251 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
[email protected]9a8c4022011-01-25 14:25:33252 prefs_.RemoveUserPref(kName);
[email protected]ecde2742010-04-02 17:36:18253 Mock::VerifyAndClearExpectations(&observer_);
254
255 ListValue new_value;
[email protected]12a3c022010-11-03 10:24:11256 new_value.Append(Value::CreateStringValue(kValue));
[email protected]acd78969c2010-12-08 09:49:11257 observer_.Expect(&prefs_, kName, &new_value);
[email protected]12a3c022010-11-03 10:24:11258 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18259 Mock::VerifyAndClearExpectations(&observer_);
260
[email protected]acd78969c2010-12-08 09:49:11261 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
262 prefs_.Set(kName, new_value);
263 Mock::VerifyAndClearExpectations(&observer_);
264
[email protected]9a8c4022011-01-25 14:25:33265 ListValue empty;
266 observer_.Expect(&prefs_, kName, &empty);
267 prefs_.Set(kName, empty);
[email protected]ecde2742010-04-02 17:36:18268 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18269}
[email protected]1bec2372011-12-07 09:19:06270
271class PrefServiceWebKitPrefs : public ChromeRenderViewHostTestHarness {
272 protected:
273 PrefServiceWebKitPrefs() : ui_thread_(BrowserThread::UI, &message_loop_) {
274 }
275
276 virtual void SetUp() {
277 ChromeRenderViewHostTestHarness::SetUp();
278
279 // Supply our own profile so we use the correct profile data. The test
280 // harness is not supposed to overwrite a profile if it's already created.
281
282 // Set some (WebKit) user preferences.
283 TestingPrefService* pref_services = profile()->GetTestingPrefService();
284#if defined(TOOLKIT_USES_GTK)
285 pref_services->SetUserPref(prefs::kUsesSystemTheme,
286 Value::CreateBooleanValue(false));
287#endif
[email protected]7a5f5932011-12-29 10:35:49288 pref_services->SetUserPref(prefs::kGlobalDefaultCharset,
[email protected]1bec2372011-12-07 09:19:06289 Value::CreateStringValue("utf8"));
[email protected]7a5f5932011-12-29 10:35:49290 pref_services->SetUserPref(prefs::kWebKitGlobalDefaultFontSize,
[email protected]1bec2372011-12-07 09:19:06291 Value::CreateIntegerValue(20));
292 pref_services->SetUserPref(prefs::kWebKitTextAreasAreResizable,
293 Value::CreateBooleanValue(false));
294 pref_services->SetUserPref(prefs::kWebKitUsesUniversalDetector,
295 Value::CreateBooleanValue(true));
296 pref_services->SetUserPref("webkit.webprefs.foo",
297 Value::CreateStringValue("bar"));
298 }
299
300 private:
301 content::TestBrowserThread ui_thread_;
302};
303
304// Tests to see that webkit preferences are properly loaded and copied over
305// to a WebPreferences object.
306TEST_F(PrefServiceWebKitPrefs, PrefsCopied) {
307 WebPreferences webkit_prefs = contents()->TestGetWebkitPrefs();
308
309 // These values have been overridden by the profile preferences.
310 EXPECT_EQ("UTF-8", webkit_prefs.default_encoding);
311 EXPECT_EQ(20, webkit_prefs.default_font_size);
312 EXPECT_FALSE(webkit_prefs.text_areas_are_resizable);
313 EXPECT_TRUE(webkit_prefs.uses_universal_detector);
314
315 // These should still be the default values.
316#if defined(OS_MACOSX)
317 const char kDefaultFont[] = "Times";
318#elif defined(OS_CHROMEOS)
319 const char kDefaultFont[] = "Tinos";
320#else
321 const char kDefaultFont[] = "Times New Roman";
322#endif
323 EXPECT_EQ(ASCIIToUTF16(kDefaultFont), webkit_prefs.standard_font_family);
324 EXPECT_TRUE(webkit_prefs.javascript_enabled);
325}