blob: b65ee3e4de343325ba9f7736479f57f7f332636f [file] [log] [blame]
[email protected]aaa552312013-02-13 16:25:401// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/command_line.h"
6#include "base/file_util.h"
7#include "base/files/scoped_temp_dir.h"
8#include "base/memory/scoped_ptr.h"
9#include "base/path_service.h"
10#include "base/prefs/pref_registry_simple.h"
11#include "base/utf_string_conversions.h"
12#include "base/values.h"
13#include "chrome/browser/policy/configuration_policy_pref_store.h"
14#include "chrome/browser/policy/mock_configuration_policy_provider.h"
15#include "chrome/browser/prefs/browser_prefs.h"
16#include "chrome/browser/prefs/command_line_pref_store.h"
17#include "chrome/browser/prefs/pref_registry_syncable.h"
18#include "chrome/browser/prefs/pref_service_mock_builder.h"
19#include "chrome/browser/prefs/scoped_user_pref_update.h"
20#include "chrome/common/chrome_paths.h"
21#include "chrome/common/chrome_switches.h"
22#include "chrome/common/pref_names.h"
23#include "chrome/test/base/chrome_render_view_host_test_harness.h"
24#include "chrome/test/base/testing_pref_service_syncable.h"
25#include "chrome/test/base/testing_profile.h"
26#include "content/public/test/test_browser_thread.h"
27#include "content/public/test/web_contents_tester.h"
28#include "ui/base/test/data/resource.h"
29#include "webkit/glue/webpreferences.h"
30
31using content::BrowserThread;
32using content::WebContentsTester;
33
34TEST(ChromePrefServiceTest, UpdateCommandLinePrefStore) {
35 TestingPrefServiceSimple prefs;
36 prefs.registry()->RegisterBooleanPref(prefs::kCloudPrintProxyEnabled, false);
37
38 // Check to make sure the value is as expected.
39 const PrefService::Preference* pref =
40 prefs.FindPreference(prefs::kCloudPrintProxyEnabled);
41 ASSERT_TRUE(pref);
42 const Value* value = pref->GetValue();
43 ASSERT_TRUE(value);
44 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType());
45 bool actual_bool_value = true;
46 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value));
47 EXPECT_FALSE(actual_bool_value);
48
49 // Change the command line.
50 CommandLine cmd_line(CommandLine::NO_PROGRAM);
51 cmd_line.AppendSwitch(switches::kEnableCloudPrintProxy);
52
53 // Call UpdateCommandLinePrefStore and check to see if the value has changed.
54 prefs.UpdateCommandLinePrefStore(new CommandLinePrefStore(&cmd_line));
55 pref = prefs.FindPreference(prefs::kCloudPrintProxyEnabled);
56 ASSERT_TRUE(pref);
57 value = pref->GetValue();
58 ASSERT_TRUE(value);
59 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType());
60 actual_bool_value = false;
61 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value));
62 EXPECT_TRUE(actual_bool_value);
63}
64
65class ChromePrefServiceUserFilePrefsTest : public testing::Test {
66 protected:
67 virtual void SetUp() {
68 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
69
70 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_));
71 data_dir_ = data_dir_.AppendASCII("pref_service");
72 ASSERT_TRUE(file_util::PathExists(data_dir_));
73 }
74
75 void ClearListValue(PrefService* prefs, const char* key) {
76 ListPrefUpdate updater(prefs, key);
77 updater->Clear();
78 }
79
80 void ClearDictionaryValue(PrefService* prefs, const char* key) {
81 DictionaryPrefUpdate updater(prefs, key);
82 updater->Clear();
83 }
84
85 // The path to temporary directory used to contain the test operations.
86 base::ScopedTempDir temp_dir_;
87 // The path to the directory where the test data is stored.
88 base::FilePath data_dir_;
89 // A message loop that we can use as the file thread message loop.
90 MessageLoop message_loop_;
91};
92
93// Verifies that ListValue and DictionaryValue pref with non emtpy default
94// preserves its empty value.
95TEST_F(ChromePrefServiceUserFilePrefsTest, PreserveEmptyValue) {
96 base::FilePath pref_file = temp_dir_.path().AppendASCII("write.json");
97
98 ASSERT_TRUE(file_util::CopyFile(
99 data_dir_.AppendASCII("read.need_empty_value.json"),
100 pref_file));
101
102 PrefServiceMockBuilder builder;
103 builder.WithUserFilePrefs(pref_file, message_loop_.message_loop_proxy());
104 scoped_refptr<PrefRegistrySyncable> registry(new PrefRegistrySyncable);
105 scoped_ptr<PrefServiceSyncable> prefs(builder.CreateSyncable(registry));
106
107 // Register testing prefs.
108 registry->RegisterListPref("list",
109 PrefRegistrySyncable::UNSYNCABLE_PREF);
110 registry->RegisterDictionaryPref("dict",
111 PrefRegistrySyncable::UNSYNCABLE_PREF);
112
113 base::ListValue* non_empty_list = new base::ListValue;
114 non_empty_list->Append(base::Value::CreateStringValue("test"));
115 registry->RegisterListPref("list_needs_empty_value",
116 non_empty_list,
117 PrefRegistrySyncable::UNSYNCABLE_PREF);
118
119 base::DictionaryValue* non_empty_dict = new base::DictionaryValue;
120 non_empty_dict->SetString("dummy", "whatever");
121 registry->RegisterDictionaryPref("dict_needs_empty_value",
122 non_empty_dict,
123 PrefRegistrySyncable::UNSYNCABLE_PREF);
124
125 // Set all testing prefs to empty.
126 ClearListValue(prefs.get(), "list");
127 ClearListValue(prefs.get(), "list_needs_empty_value");
128 ClearDictionaryValue(prefs.get(), "dict");
129 ClearDictionaryValue(prefs.get(), "dict_needs_empty_value");
130
131 // Write to file.
132 prefs->CommitPendingWrite();
133 message_loop_.RunUntilIdle();
134
135 // Compare to expected output.
136 base::FilePath golden_output_file =
137 data_dir_.AppendASCII("write.golden.need_empty_value.json");
138 ASSERT_TRUE(file_util::PathExists(golden_output_file));
139 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file));
140}
141
142class ChromePrefServiceWebKitPrefs : public ChromeRenderViewHostTestHarness {
143 protected:
144 ChromePrefServiceWebKitPrefs()
145 : ui_thread_(BrowserThread::UI, &message_loop_) {
146 }
147
148 virtual void SetUp() {
149 ChromeRenderViewHostTestHarness::SetUp();
150
151 // Supply our own profile so we use the correct profile data. The test
152 // harness is not supposed to overwrite a profile if it's already created.
153
154 // Set some (WebKit) user preferences.
155 TestingPrefServiceSyncable* pref_services =
156 profile()->GetTestingPrefService();
157#if defined(TOOLKIT_GTK)
158 pref_services->SetUserPref(prefs::kUsesSystemTheme,
159 Value::CreateBooleanValue(false));
160#endif
161 pref_services->SetUserPref(prefs::kDefaultCharset,
162 Value::CreateStringValue("utf8"));
163 pref_services->SetUserPref(prefs::kWebKitDefaultFontSize,
164 Value::CreateIntegerValue(20));
165 pref_services->SetUserPref(prefs::kWebKitTextAreasAreResizable,
166 Value::CreateBooleanValue(false));
167 pref_services->SetUserPref(prefs::kWebKitUsesUniversalDetector,
168 Value::CreateBooleanValue(true));
169 pref_services->SetUserPref("webkit.webprefs.foo",
170 Value::CreateStringValue("bar"));
171 }
172
173 private:
174 content::TestBrowserThread ui_thread_;
175};
176
177// Tests to see that webkit preferences are properly loaded and copied over
178// to a WebPreferences object.
179TEST_F(ChromePrefServiceWebKitPrefs, PrefsCopied) {
180 webkit_glue::WebPreferences webkit_prefs =
181 WebContentsTester::For(web_contents())->TestGetWebkitPrefs();
182
183 // These values have been overridden by the profile preferences.
184 EXPECT_EQ("UTF-8", webkit_prefs.default_encoding);
185 EXPECT_EQ(20, webkit_prefs.default_font_size);
186 EXPECT_FALSE(webkit_prefs.text_areas_are_resizable);
187 EXPECT_TRUE(webkit_prefs.uses_universal_detector);
188
189 // These should still be the default values.
190#if defined(OS_MACOSX)
191 const char kDefaultFont[] = "Times";
192#elif defined(OS_CHROMEOS)
193 const char kDefaultFont[] = "Tinos";
194#else
195 const char kDefaultFont[] = "Times New Roman";
196#endif
197 EXPECT_EQ(ASCIIToUTF16(kDefaultFont),
198 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]);
199 EXPECT_TRUE(webkit_prefs.javascript_enabled);
200}