chrome_pref_service_unittest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
1// 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_service_mock_builder.h"
18#include "chrome/browser/prefs/scoped_user_pref_update.h"
19#include "chrome/common/chrome_paths.h"
20#include "chrome/common/chrome_switches.h"
21#include "chrome/common/pref_names.h"
22#include "chrome/test/base/chrome_render_view_host_test_harness.h"
23#include "chrome/test/base/testing_pref_service_syncable.h"
24#include "chrome/test/base/testing_profile.h"
25#include "components/user_prefs/pref_registry_syncable.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<user_prefs::PrefRegistrySyncable> registry(
105      new user_prefs::PrefRegistrySyncable);
106  scoped_ptr<PrefServiceSyncable> prefs(builder.CreateSyncable(registry));
107
108  // Register testing prefs.
109  registry->RegisterListPref("list",
110                             user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
111  registry->RegisterDictionaryPref(
112      "dict",
113      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
114
115  base::ListValue* non_empty_list = new base::ListValue;
116  non_empty_list->Append(base::Value::CreateStringValue("test"));
117  registry->RegisterListPref("list_needs_empty_value",
118                             non_empty_list,
119                             user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
120
121  base::DictionaryValue* non_empty_dict = new base::DictionaryValue;
122  non_empty_dict->SetString("dummy", "whatever");
123  registry->RegisterDictionaryPref(
124      "dict_needs_empty_value",
125      non_empty_dict,
126      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
127
128  // Set all testing prefs to empty.
129  ClearListValue(prefs.get(), "list");
130  ClearListValue(prefs.get(), "list_needs_empty_value");
131  ClearDictionaryValue(prefs.get(), "dict");
132  ClearDictionaryValue(prefs.get(), "dict_needs_empty_value");
133
134  // Write to file.
135  prefs->CommitPendingWrite();
136  message_loop_.RunUntilIdle();
137
138  // Compare to expected output.
139  base::FilePath golden_output_file =
140      data_dir_.AppendASCII("write.golden.need_empty_value.json");
141  ASSERT_TRUE(file_util::PathExists(golden_output_file));
142  EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file));
143}
144
145class ChromePrefServiceWebKitPrefs : public ChromeRenderViewHostTestHarness {
146 protected:
147  ChromePrefServiceWebKitPrefs()
148      : ui_thread_(BrowserThread::UI, &message_loop_) {
149  }
150
151  virtual void SetUp() {
152    ChromeRenderViewHostTestHarness::SetUp();
153
154    // Supply our own profile so we use the correct profile data. The test
155    // harness is not supposed to overwrite a profile if it's already created.
156
157    // Set some (WebKit) user preferences.
158    TestingPrefServiceSyncable* pref_services =
159        profile()->GetTestingPrefService();
160#if defined(TOOLKIT_GTK)
161    pref_services->SetUserPref(prefs::kUsesSystemTheme,
162                               Value::CreateBooleanValue(false));
163#endif
164    pref_services->SetUserPref(prefs::kDefaultCharset,
165                               Value::CreateStringValue("utf8"));
166    pref_services->SetUserPref(prefs::kWebKitDefaultFontSize,
167                               Value::CreateIntegerValue(20));
168    pref_services->SetUserPref(prefs::kWebKitTextAreasAreResizable,
169                               Value::CreateBooleanValue(false));
170    pref_services->SetUserPref(prefs::kWebKitUsesUniversalDetector,
171                               Value::CreateBooleanValue(true));
172    pref_services->SetUserPref("webkit.webprefs.foo",
173                               Value::CreateStringValue("bar"));
174  }
175
176 private:
177  content::TestBrowserThread ui_thread_;
178};
179
180// Tests to see that webkit preferences are properly loaded and copied over
181// to a WebPreferences object.
182TEST_F(ChromePrefServiceWebKitPrefs, PrefsCopied) {
183  WebPreferences webkit_prefs =
184      WebContentsTester::For(web_contents())->TestGetWebkitPrefs();
185
186  // These values have been overridden by the profile preferences.
187  EXPECT_EQ("UTF-8", webkit_prefs.default_encoding);
188  EXPECT_EQ(20, webkit_prefs.default_font_size);
189  EXPECT_FALSE(webkit_prefs.text_areas_are_resizable);
190  EXPECT_TRUE(webkit_prefs.uses_universal_detector);
191
192  // These should still be the default values.
193#if defined(OS_MACOSX)
194  const char kDefaultFont[] = "Times";
195#elif defined(OS_CHROMEOS)
196  const char kDefaultFont[] = "Tinos";
197#else
198  const char kDefaultFont[] = "Times New Roman";
199#endif
200  EXPECT_EQ(ASCIIToUTF16(kDefaultFont),
201            webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]);
202  EXPECT_TRUE(webkit_prefs.javascript_enabled);
203}
204