blob: 883f50a536d19e542aecb53eed8432f3bf4000de [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]ea3e4972012-04-12 03:41:378#include "base/file_util.h"
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/scoped_ptr.h"
[email protected]ea3e4972012-04-12 03:41:3710#include "base/path_service.h"
[email protected]03b9b4e2012-10-22 20:01:5211#include "base/prefs/json_pref_store.h"
12#include "base/prefs/public/pref_change_registrar.h"
13#include "base/prefs/testing_pref_store.h"
[email protected]ea3e4972012-04-12 03:41:3714#include "base/scoped_temp_dir.h"
[email protected]1bec2372011-12-07 09:19:0615#include "base/utf_string_conversions.h"
[email protected]ecde2742010-04-02 17:36:1816#include "base/values.h"
[email protected]ce1850e92010-10-15 08:40:5817#include "chrome/browser/policy/configuration_policy_pref_store.h"
18#include "chrome/browser/policy/mock_configuration_policy_provider.h"
19#include "chrome/browser/prefs/browser_prefs.h"
20#include "chrome/browser/prefs/command_line_pref_store.h"
[email protected]acd78969c2010-12-08 09:49:1121#include "chrome/browser/prefs/pref_observer_mock.h"
[email protected]f2d1f612010-12-09 15:10:1722#include "chrome/browser/prefs/pref_service_mock_builder.h"
[email protected]37858e52010-08-26 00:22:0223#include "chrome/browser/prefs/pref_value_store.h"
[email protected]ea3e4972012-04-12 03:41:3724#include "chrome/browser/prefs/scoped_user_pref_update.h"
initial.commit09911bf2008-07-26 23:55:2925#include "chrome/common/chrome_paths.h"
[email protected]ce1850e92010-10-15 08:40:5826#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2927#include "chrome/common/pref_names.h"
[email protected]1bec2372011-12-07 09:19:0628#include "chrome/test/base/chrome_render_view_host_test_harness.h"
[email protected]8ad3636e2011-08-01 22:31:4029#include "chrome/test/base/testing_pref_service.h"
[email protected]1bec2372011-12-07 09:19:0630#include "chrome/test/base/testing_profile.h"
[email protected]e97882f2012-06-04 02:23:1731#include "content/public/test/test_browser_thread.h"
[email protected]b1e3f202012-06-04 14:45:5032#include "content/public/test/web_contents_tester.h"
[email protected]ecde2742010-04-02 17:36:1833#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2934#include "testing/gtest/include/gtest/gtest.h"
[email protected]41110ca2011-06-27 16:45:4935#include "ui/base/test/data/resource.h"
[email protected]46a32b92012-03-22 13:04:4836#include "webkit/glue/webpreferences.h"
initial.commit09911bf2008-07-26 23:55:2937
[email protected]1bec2372011-12-07 09:19:0638using content::BrowserThread;
[email protected]46a32b92012-03-22 13:04:4839using content::WebContentsTester;
[email protected]ecde2742010-04-02 17:36:1840using testing::_;
41using testing::Mock;
[email protected]12a3c022010-11-03 10:24:1142
[email protected]277404c22010-04-22 13:09:4543TEST(PrefServiceTest, NoObserverFire) {
[email protected]74379bc52010-07-21 13:54:0844 TestingPrefService prefs;
[email protected]7aa0a962010-04-21 17:24:4245
[email protected]57ecc4b2010-08-11 03:02:5146 const char pref_name[] = "homepage";
[email protected]12a3c022010-11-03 10:24:1147 prefs.RegisterStringPref(pref_name, std::string());
[email protected]7aa0a962010-04-21 17:24:4248
[email protected]acd78969c2010-12-08 09:49:1149 const char new_pref_value[] = "http://www.google.com/";
50 PrefObserverMock obs;
[email protected]2fb7dc982010-09-29 12:24:2851 PrefChangeRegistrar registrar;
52 registrar.Init(&prefs);
53 registrar.Add(pref_name, &obs);
[email protected]7aa0a962010-04-21 17:24:4254
[email protected]acd78969c2010-12-08 09:49:1155 // This should fire the checks in PrefObserverMock::Observe.
56 const StringValue expected_value(new_pref_value);
57 obs.Expect(&prefs, pref_name, &expected_value);
58 prefs.SetString(pref_name, new_pref_value);
59 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4260
61 // Setting the pref to the same value should not set the pref value a second
62 // time.
[email protected]a6a7ced2012-11-01 17:24:1863 EXPECT_CALL(obs, OnPreferenceChanged(_, _)).Times(0);
[email protected]7aa0a962010-04-21 17:24:4264 prefs.SetString(pref_name, new_pref_value);
[email protected]acd78969c2010-12-08 09:49:1165 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4266
67 // Clearing the pref should cause the pref to fire.
[email protected]acd78969c2010-12-08 09:49:1168 const StringValue expected_default_value("");
69 obs.Expect(&prefs, pref_name, &expected_default_value);
[email protected]7aa0a962010-04-21 17:24:4270 prefs.ClearPref(pref_name);
[email protected]acd78969c2010-12-08 09:49:1171 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4272
73 // Clearing the pref again should not cause the pref to fire.
[email protected]a6a7ced2012-11-01 17:24:1874 EXPECT_CALL(obs, OnPreferenceChanged(_, _)).Times(0);
[email protected]7aa0a962010-04-21 17:24:4275 prefs.ClearPref(pref_name);
[email protected]acd78969c2010-12-08 09:49:1176 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4277}
78
[email protected]277404c22010-04-22 13:09:4579TEST(PrefServiceTest, HasPrefPath) {
[email protected]74379bc52010-07-21 13:54:0880 TestingPrefService prefs;
[email protected]7aa0a962010-04-21 17:24:4281
[email protected]57ecc4b2010-08-11 03:02:5182 const char path[] = "fake.path";
[email protected]7aa0a962010-04-21 17:24:4283
84 // Shouldn't initially have a path.
85 EXPECT_FALSE(prefs.HasPrefPath(path));
86
87 // Register the path. This doesn't set a value, so the path still shouldn't
88 // exist.
[email protected]20ce516d2010-06-18 02:20:0489 prefs.RegisterStringPref(path, std::string());
[email protected]7aa0a962010-04-21 17:24:4290 EXPECT_FALSE(prefs.HasPrefPath(path));
91
92 // Set a value and make sure we have a path.
[email protected]ddd231e2010-06-29 20:35:1993 prefs.SetString(path, "blah");
[email protected]7aa0a962010-04-21 17:24:4294 EXPECT_TRUE(prefs.HasPrefPath(path));
95}
96
[email protected]277404c22010-04-22 13:09:4597TEST(PrefServiceTest, Observers) {
[email protected]57ecc4b2010-08-11 03:02:5198 const char pref_name[] = "homepage";
[email protected]277404c22010-04-22 13:09:4599
[email protected]74379bc52010-07-21 13:54:08100 TestingPrefService prefs;
[email protected]57ecc4b2010-08-11 03:02:51101 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com"));
[email protected]12a3c022010-11-03 10:24:11102 prefs.RegisterStringPref(pref_name, std::string());
[email protected]277404c22010-04-22 13:09:45103
[email protected]acd78969c2010-12-08 09:49:11104 const char new_pref_value[] = "http://www.google.com/";
105 const StringValue expected_new_pref_value(new_pref_value);
106 PrefObserverMock obs;
[email protected]2fb7dc982010-09-29 12:24:28107 PrefChangeRegistrar registrar;
108 registrar.Init(&prefs);
109 registrar.Add(pref_name, &obs);
[email protected]277404c22010-04-22 13:09:45110
[email protected]54ffd94a2012-11-12 15:29:20111 PrefChangeRegistrar registrar_two;
112 registrar_two.Init(&prefs);
113
[email protected]acd78969c2010-12-08 09:49:11114 // This should fire the checks in PrefObserverMock::Observe.
115 obs.Expect(&prefs, pref_name, &expected_new_pref_value);
116 prefs.SetString(pref_name, new_pref_value);
117 Mock::VerifyAndClearExpectations(&obs);
[email protected]277404c22010-04-22 13:09:45118
119 // Now try adding a second pref observer.
[email protected]acd78969c2010-12-08 09:49:11120 const char new_pref_value2[] = "http://www.youtube.com/";
121 const StringValue expected_new_pref_value2(new_pref_value2);
122 PrefObserverMock obs2;
123 obs.Expect(&prefs, pref_name, &expected_new_pref_value2);
124 obs2.Expect(&prefs, pref_name, &expected_new_pref_value2);
[email protected]54ffd94a2012-11-12 15:29:20125 registrar_two.Add(pref_name, &obs2);
[email protected]277404c22010-04-22 13:09:45126 // This should fire the checks in obs and obs2.
127 prefs.SetString(pref_name, new_pref_value2);
[email protected]acd78969c2010-12-08 09:49:11128 Mock::VerifyAndClearExpectations(&obs);
129 Mock::VerifyAndClearExpectations(&obs2);
[email protected]277404c22010-04-22 13:09:45130
[email protected]7ca0f362012-07-30 10:14:03131 // Set a recommended value.
132 const StringValue recommended_pref_value("http://www.gmail.com/");
133 obs.Expect(&prefs, pref_name, &expected_new_pref_value2);
134 obs2.Expect(&prefs, pref_name, &expected_new_pref_value2);
135 // This should fire the checks in obs and obs2 but with an unchanged value
136 // as the recommended value is being overridden by the user-set value.
137 prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy());
138 Mock::VerifyAndClearExpectations(&obs);
139 Mock::VerifyAndClearExpectations(&obs2);
140
[email protected]277404c22010-04-22 13:09:45141 // Make sure obs2 still works after removing obs.
[email protected]54ffd94a2012-11-12 15:29:20142 registrar.Remove(pref_name);
[email protected]a6a7ced2012-11-01 17:24:18143 EXPECT_CALL(obs, OnPreferenceChanged(_, _)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11144 obs2.Expect(&prefs, pref_name, &expected_new_pref_value);
[email protected]277404c22010-04-22 13:09:45145 // This should only fire the observer in obs2.
146 prefs.SetString(pref_name, new_pref_value);
[email protected]acd78969c2010-12-08 09:49:11147 Mock::VerifyAndClearExpectations(&obs);
148 Mock::VerifyAndClearExpectations(&obs2);
[email protected]277404c22010-04-22 13:09:45149}
150
[email protected]9a8c4022011-01-25 14:25:33151// 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.
153TEST(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
[email protected]d3b05ea2012-01-24 22:57:05172TEST(PrefServiceTest, UpdateCommandLinePrefStore) {
173 TestingPrefService prefs;
174 prefs.RegisterBooleanPref(prefs::kCloudPrintProxyEnabled, false);
175
176 // Check to make sure the value is as expected.
177 const PrefService::Preference* pref =
178 prefs.FindPreference(prefs::kCloudPrintProxyEnabled);
179 ASSERT_TRUE(pref);
180 const Value* value = pref->GetValue();
181 ASSERT_TRUE(value);
182 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType());
183 bool actual_bool_value = true;
184 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value));
[email protected]4eccb81a2012-01-26 12:01:22185 EXPECT_FALSE(actual_bool_value);
[email protected]d3b05ea2012-01-24 22:57:05186
187 // Change the command line.
188 CommandLine cmd_line(CommandLine::NO_PROGRAM);
189 cmd_line.AppendSwitch(switches::kEnableCloudPrintProxy);
190
191 // Call UpdateCommandLinePrefStore and check to see if the value has changed.
192 prefs.UpdateCommandLinePrefStore(&cmd_line);
193 pref = prefs.FindPreference(prefs::kCloudPrintProxyEnabled);
194 ASSERT_TRUE(pref);
195 value = pref->GetValue();
196 ASSERT_TRUE(value);
197 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType());
198 actual_bool_value = false;
199 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value));
[email protected]4eccb81a2012-01-26 12:01:22200 EXPECT_TRUE(actual_bool_value);
[email protected]d3b05ea2012-01-24 22:57:05201}
202
[email protected]7ca0f362012-07-30 10:14:03203TEST(PrefServiceTest, GetValueAndGetRecommendedValue) {
204 const int kDefaultValue = 5;
205 const int kUserValue = 10;
206 const int kRecommendedValue = 15;
207 TestingPrefService prefs;
208 prefs.RegisterIntegerPref(prefs::kStabilityLaunchCount, kDefaultValue);
209
210 // Create pref with a default value only.
211 const PrefService::Preference* pref =
212 prefs.FindPreference(prefs::kStabilityLaunchCount);
213 ASSERT_TRUE(pref);
214
215 // Check that GetValue() returns the default value.
216 const Value* value = pref->GetValue();
217 ASSERT_TRUE(value);
218 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
219 int actual_int_value = -1;
220 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
221 EXPECT_EQ(kDefaultValue, actual_int_value);
222
223 // Check that GetRecommendedValue() returns no value.
224 value = pref->GetRecommendedValue();
225 ASSERT_FALSE(value);
226
227 // Set a user-set value.
228 prefs.SetUserPref(prefs::kStabilityLaunchCount,
229 Value::CreateIntegerValue(kUserValue));
230
231 // Check that GetValue() returns the user-set value.
232 value = pref->GetValue();
233 ASSERT_TRUE(value);
234 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
235 actual_int_value = -1;
236 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
237 EXPECT_EQ(kUserValue, actual_int_value);
238
239 // Check that GetRecommendedValue() returns no value.
240 value = pref->GetRecommendedValue();
241 ASSERT_FALSE(value);
242
243 // Set a recommended value.
244 prefs.SetRecommendedPref(prefs::kStabilityLaunchCount,
245 Value::CreateIntegerValue(kRecommendedValue));
246
247 // Check that GetValue() returns the user-set value.
248 value = pref->GetValue();
249 ASSERT_TRUE(value);
250 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
251 actual_int_value = -1;
252 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
253 EXPECT_EQ(kUserValue, actual_int_value);
254
255 // Check that GetRecommendedValue() returns the recommended value.
256 value = pref->GetRecommendedValue();
257 ASSERT_TRUE(value);
258 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
259 actual_int_value = -1;
260 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
261 EXPECT_EQ(kRecommendedValue, actual_int_value);
262
263 // Remove the user-set value.
264 prefs.RemoveUserPref(prefs::kStabilityLaunchCount);
265
266 // Check that GetValue() returns the recommended value.
267 value = pref->GetValue();
268 ASSERT_TRUE(value);
269 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
270 actual_int_value = -1;
271 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
272 EXPECT_EQ(kRecommendedValue, actual_int_value);
273
274 // Check that GetRecommendedValue() returns the recommended value.
275 value = pref->GetRecommendedValue();
276 ASSERT_TRUE(value);
277 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
278 actual_int_value = -1;
279 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
280 EXPECT_EQ(kRecommendedValue, actual_int_value);
281}
282
[email protected]ea3e4972012-04-12 03:41:37283class PrefServiceUserFilePrefsTest : public testing::Test {
284 protected:
285 virtual void SetUp() {
286 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
287
288 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_));
289 data_dir_ = data_dir_.AppendASCII("pref_service");
290 ASSERT_TRUE(file_util::PathExists(data_dir_));
291 }
292
293 void ClearListValue(PrefService* prefs, const char* key) {
294 ListPrefUpdate updater(prefs, key);
295 updater->Clear();
296 }
297
298 void ClearDictionaryValue(PrefService* prefs, const char* key) {
299 DictionaryPrefUpdate updater(prefs, key);
300 updater->Clear();
301 }
302
303 // The path to temporary directory used to contain the test operations.
304 ScopedTempDir temp_dir_;
305 // The path to the directory where the test data is stored.
306 FilePath data_dir_;
307 // A message loop that we can use as the file thread message loop.
308 MessageLoop message_loop_;
309};
310
311// Verifies that ListValue and DictionaryValue pref with non emtpy default
312// preserves its empty value.
313TEST_F(PrefServiceUserFilePrefsTest, PreserveEmptyValue) {
314 FilePath pref_file = temp_dir_.path().AppendASCII("write.json");
315
316 ASSERT_TRUE(file_util::CopyFile(
317 data_dir_.AppendASCII("read.need_empty_value.json"),
318 pref_file));
319
320 PrefServiceMockBuilder builder;
[email protected]0de615a2012-11-08 04:40:59321 builder.WithUserFilePrefs(pref_file, message_loop_.message_loop_proxy());
[email protected]ea3e4972012-04-12 03:41:37322 scoped_ptr<PrefService> prefs(builder.Create());
323
324 // Register testing prefs.
325 prefs->RegisterListPref("list",
326 PrefService::UNSYNCABLE_PREF);
327 prefs->RegisterDictionaryPref("dict",
328 PrefService::UNSYNCABLE_PREF);
329
330 base::ListValue* non_empty_list = new base::ListValue;
331 non_empty_list->Append(base::Value::CreateStringValue("test"));
332 prefs->RegisterListPref("list_needs_empty_value",
333 non_empty_list,
334 PrefService::UNSYNCABLE_PREF);
335
336 base::DictionaryValue* non_empty_dict = new base::DictionaryValue;
337 non_empty_dict->SetString("dummy", "whatever");
338 prefs->RegisterDictionaryPref("dict_needs_empty_value",
339 non_empty_dict,
340 PrefService::UNSYNCABLE_PREF);
341
342 // Set all testing prefs to empty.
343 ClearListValue(prefs.get(), "list");
344 ClearListValue(prefs.get(), "list_needs_empty_value");
345 ClearDictionaryValue(prefs.get(), "dict");
346 ClearDictionaryValue(prefs.get(), "dict_needs_empty_value");
347
348 // Write to file.
349 prefs->CommitPendingWrite();
[email protected]0de615a2012-11-08 04:40:59350 message_loop_.RunUntilIdle();
[email protected]ea3e4972012-04-12 03:41:37351
352 // Compare to expected output.
353 FilePath golden_output_file =
354 data_dir_.AppendASCII("write.golden.need_empty_value.json");
355 ASSERT_TRUE(file_util::PathExists(golden_output_file));
356 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file));
357}
358
[email protected]ecde2742010-04-02 17:36:18359class PrefServiceSetValueTest : public testing::Test {
360 protected:
[email protected]12a3c022010-11-03 10:24:11361 static const char kName[];
362 static const char kValue[];
[email protected]ecde2742010-04-02 17:36:18363
[email protected]74379bc52010-07-21 13:54:08364 TestingPrefService prefs_;
[email protected]acd78969c2010-12-08 09:49:11365 PrefObserverMock observer_;
[email protected]ecde2742010-04-02 17:36:18366};
[email protected]ddd231e2010-06-29 20:35:19367
[email protected]12a3c022010-11-03 10:24:11368const char PrefServiceSetValueTest::kName[] = "name";
369const char PrefServiceSetValueTest::kValue[] = "value";
[email protected]ecde2742010-04-02 17:36:18370
371TEST_F(PrefServiceSetValueTest, SetStringValue) {
[email protected]20ce516d2010-06-18 02:20:04372 const char default_string[] = "default";
[email protected]acd78969c2010-12-08 09:49:11373 const StringValue default_value(default_string);
[email protected]12a3c022010-11-03 10:24:11374 prefs_.RegisterStringPref(kName, default_string);
[email protected]2fb7dc982010-09-29 12:24:28375
376 PrefChangeRegistrar registrar;
377 registrar.Init(&prefs_);
[email protected]12a3c022010-11-03 10:24:11378 registrar.Add(kName, &observer_);
[email protected]2fb7dc982010-09-29 12:24:28379
[email protected]c3b54f372010-09-14 08:25:07380 // Changing the controlling store from default to user triggers notification.
[email protected]acd78969c2010-12-08 09:49:11381 observer_.Expect(&prefs_, kName, &default_value);
382 prefs_.Set(kName, default_value);
[email protected]c3b54f372010-09-14 08:25:07383 Mock::VerifyAndClearExpectations(&observer_);
384
[email protected]a6a7ced2012-11-01 17:24:18385 EXPECT_CALL(observer_, OnPreferenceChanged(_, _)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11386 prefs_.Set(kName, default_value);
[email protected]ecde2742010-04-02 17:36:18387 Mock::VerifyAndClearExpectations(&observer_);
388
[email protected]acd78969c2010-12-08 09:49:11389 StringValue new_value(kValue);
390 observer_.Expect(&prefs_, kName, &new_value);
391 prefs_.Set(kName, new_value);
392 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18393}
394
395TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
[email protected]12a3c022010-11-03 10:24:11396 prefs_.RegisterDictionaryPref(kName);
[email protected]2fb7dc982010-09-29 12:24:28397 PrefChangeRegistrar registrar;
398 registrar.Init(&prefs_);
[email protected]12a3c022010-11-03 10:24:11399 registrar.Add(kName, &observer_);
[email protected]ecde2742010-04-02 17:36:18400
[email protected]a6a7ced2012-11-01 17:24:18401 EXPECT_CALL(observer_, OnPreferenceChanged(_, _)).Times(0);
[email protected]9a8c4022011-01-25 14:25:33402 prefs_.RemoveUserPref(kName);
[email protected]ecde2742010-04-02 17:36:18403 Mock::VerifyAndClearExpectations(&observer_);
404
405 DictionaryValue new_value;
[email protected]12a3c022010-11-03 10:24:11406 new_value.SetString(kName, kValue);
[email protected]acd78969c2010-12-08 09:49:11407 observer_.Expect(&prefs_, kName, &new_value);
[email protected]12a3c022010-11-03 10:24:11408 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18409 Mock::VerifyAndClearExpectations(&observer_);
410
[email protected]a6a7ced2012-11-01 17:24:18411 EXPECT_CALL(observer_, OnPreferenceChanged(_, _)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11412 prefs_.Set(kName, new_value);
413 Mock::VerifyAndClearExpectations(&observer_);
414
[email protected]9a8c4022011-01-25 14:25:33415 DictionaryValue empty;
416 observer_.Expect(&prefs_, kName, &empty);
417 prefs_.Set(kName, empty);
[email protected]ecde2742010-04-02 17:36:18418 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18419}
420
421TEST_F(PrefServiceSetValueTest, SetListValue) {
[email protected]12a3c022010-11-03 10:24:11422 prefs_.RegisterListPref(kName);
[email protected]2fb7dc982010-09-29 12:24:28423 PrefChangeRegistrar registrar;
424 registrar.Init(&prefs_);
[email protected]12a3c022010-11-03 10:24:11425 registrar.Add(kName, &observer_);
[email protected]ecde2742010-04-02 17:36:18426
[email protected]a6a7ced2012-11-01 17:24:18427 EXPECT_CALL(observer_, OnPreferenceChanged(_, _)).Times(0);
[email protected]9a8c4022011-01-25 14:25:33428 prefs_.RemoveUserPref(kName);
[email protected]ecde2742010-04-02 17:36:18429 Mock::VerifyAndClearExpectations(&observer_);
430
431 ListValue new_value;
[email protected]12a3c022010-11-03 10:24:11432 new_value.Append(Value::CreateStringValue(kValue));
[email protected]acd78969c2010-12-08 09:49:11433 observer_.Expect(&prefs_, kName, &new_value);
[email protected]12a3c022010-11-03 10:24:11434 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18435 Mock::VerifyAndClearExpectations(&observer_);
436
[email protected]a6a7ced2012-11-01 17:24:18437 EXPECT_CALL(observer_, OnPreferenceChanged(_, _)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11438 prefs_.Set(kName, new_value);
439 Mock::VerifyAndClearExpectations(&observer_);
440
[email protected]9a8c4022011-01-25 14:25:33441 ListValue empty;
442 observer_.Expect(&prefs_, kName, &empty);
443 prefs_.Set(kName, empty);
[email protected]ecde2742010-04-02 17:36:18444 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18445}
[email protected]1bec2372011-12-07 09:19:06446
447class PrefServiceWebKitPrefs : public ChromeRenderViewHostTestHarness {
448 protected:
449 PrefServiceWebKitPrefs() : ui_thread_(BrowserThread::UI, &message_loop_) {
450 }
451
452 virtual void SetUp() {
453 ChromeRenderViewHostTestHarness::SetUp();
454
455 // Supply our own profile so we use the correct profile data. The test
456 // harness is not supposed to overwrite a profile if it's already created.
457
458 // Set some (WebKit) user preferences.
459 TestingPrefService* pref_services = profile()->GetTestingPrefService();
[email protected]a13283cc2012-04-05 00:21:22460#if defined(TOOLKIT_GTK)
[email protected]1bec2372011-12-07 09:19:06461 pref_services->SetUserPref(prefs::kUsesSystemTheme,
462 Value::CreateBooleanValue(false));
463#endif
[email protected]ddf72142012-05-22 04:52:40464 pref_services->SetUserPref(prefs::kDefaultCharset,
[email protected]1bec2372011-12-07 09:19:06465 Value::CreateStringValue("utf8"));
[email protected]ddf72142012-05-22 04:52:40466 pref_services->SetUserPref(prefs::kWebKitDefaultFontSize,
[email protected]1bec2372011-12-07 09:19:06467 Value::CreateIntegerValue(20));
468 pref_services->SetUserPref(prefs::kWebKitTextAreasAreResizable,
469 Value::CreateBooleanValue(false));
470 pref_services->SetUserPref(prefs::kWebKitUsesUniversalDetector,
471 Value::CreateBooleanValue(true));
472 pref_services->SetUserPref("webkit.webprefs.foo",
473 Value::CreateStringValue("bar"));
474 }
475
476 private:
477 content::TestBrowserThread ui_thread_;
478};
479
480// Tests to see that webkit preferences are properly loaded and copied over
481// to a WebPreferences object.
482TEST_F(PrefServiceWebKitPrefs, PrefsCopied) {
[email protected]6717bf272012-05-11 23:31:25483 webkit_glue::WebPreferences webkit_prefs =
[email protected]921441c2012-10-19 22:16:01484 WebContentsTester::For(web_contents())->TestGetWebkitPrefs();
[email protected]1bec2372011-12-07 09:19:06485
486 // These values have been overridden by the profile preferences.
487 EXPECT_EQ("UTF-8", webkit_prefs.default_encoding);
488 EXPECT_EQ(20, webkit_prefs.default_font_size);
489 EXPECT_FALSE(webkit_prefs.text_areas_are_resizable);
490 EXPECT_TRUE(webkit_prefs.uses_universal_detector);
491
492 // These should still be the default values.
493#if defined(OS_MACOSX)
494 const char kDefaultFont[] = "Times";
495#elif defined(OS_CHROMEOS)
496 const char kDefaultFont[] = "Tinos";
497#else
498 const char kDefaultFont[] = "Times New Roman";
499#endif
[email protected]966d1e12012-05-18 07:20:32500 EXPECT_EQ(ASCIIToUTF16(kDefaultFont),
501 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]);
[email protected]1bec2372011-12-07 09:19:06502 EXPECT_TRUE(webkit_prefs.javascript_enabled);
503}