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