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.
5 #include "base/command_line.h"
6 #include "base/files/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/prefs/scoped_user_pref_update.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/prefs/browser_prefs.h"
15 #include "chrome/browser/prefs/command_line_pref_store.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "components/policy/core/browser/configuration_policy_pref_store.h"
22 #include "components/policy/core/common/mock_configuration_policy_provider.h"
23 #include "components/pref_registry/pref_registry_syncable.h"
24 #include "components/syncable_prefs/pref_service_mock_factory.h"
25 #include "components/syncable_prefs/testing_pref_service_syncable.h"
26 #include "content/public/common/web_preferences.h"
27 #include "content/public/test/test_renderer_host.h"
28 #include "ui/base/test/data/resource.h"
30 using content::RenderViewHostTester
;
31 using content::WebPreferences
;
33 TEST(ChromePrefServiceTest
, UpdateCommandLinePrefStore
) {
34 TestingPrefServiceSimple prefs
;
35 prefs
.registry()->RegisterBooleanPref(prefs::kCloudPrintProxyEnabled
, false);
37 // Check to make sure the value is as expected.
38 const PrefService::Preference
* pref
=
39 prefs
.FindPreference(prefs::kCloudPrintProxyEnabled
);
41 const base::Value
* value
= pref
->GetValue();
43 EXPECT_EQ(base::Value::TYPE_BOOLEAN
, value
->GetType());
44 bool actual_bool_value
= true;
45 EXPECT_TRUE(value
->GetAsBoolean(&actual_bool_value
));
46 EXPECT_FALSE(actual_bool_value
);
48 // Change the command line.
49 base::CommandLine
cmd_line(base::CommandLine::NO_PROGRAM
);
50 cmd_line
.AppendSwitch(switches::kEnableCloudPrintProxy
);
52 // Call UpdateCommandLinePrefStore and check to see if the value has changed.
53 prefs
.UpdateCommandLinePrefStore(new CommandLinePrefStore(&cmd_line
));
54 pref
= prefs
.FindPreference(prefs::kCloudPrintProxyEnabled
);
56 value
= pref
->GetValue();
58 EXPECT_EQ(base::Value::TYPE_BOOLEAN
, value
->GetType());
59 actual_bool_value
= false;
60 EXPECT_TRUE(value
->GetAsBoolean(&actual_bool_value
));
61 EXPECT_TRUE(actual_bool_value
);
64 class ChromePrefServiceUserFilePrefsTest
: public testing::Test
{
66 void SetUp() override
{
67 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
69 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &data_dir_
));
70 data_dir_
= data_dir_
.AppendASCII("pref_service");
71 ASSERT_TRUE(base::PathExists(data_dir_
));
74 void ClearListValue(PrefService
* prefs
, const char* key
) {
75 ListPrefUpdate
updater(prefs
, key
);
79 void ClearDictionaryValue(PrefService
* prefs
, const char* key
) {
80 DictionaryPrefUpdate
updater(prefs
, key
);
84 // The path to temporary directory used to contain the test operations.
85 base::ScopedTempDir temp_dir_
;
86 // The path to the directory where the test data is stored.
87 base::FilePath data_dir_
;
88 // A message loop that we can use as the file thread message loop.
89 base::MessageLoop message_loop_
;
92 class ChromePrefServiceWebKitPrefs
: public ChromeRenderViewHostTestHarness
{
94 void SetUp() override
{
95 ChromeRenderViewHostTestHarness::SetUp();
97 // Supply our own profile so we use the correct profile data. The test
98 // harness is not supposed to overwrite a profile if it's already created.
100 // Set some (WebKit) user preferences.
101 syncable_prefs::TestingPrefServiceSyncable
* pref_services
=
102 profile()->GetTestingPrefService();
103 pref_services
->SetUserPref(prefs::kDefaultCharset
,
104 new base::StringValue("utf8"));
105 pref_services
->SetUserPref(prefs::kWebKitDefaultFontSize
,
106 new base::FundamentalValue(20));
107 pref_services
->SetUserPref(prefs::kWebKitTextAreasAreResizable
,
108 new base::FundamentalValue(false));
109 pref_services
->SetUserPref(prefs::kWebKitUsesUniversalDetector
,
110 new base::FundamentalValue(true));
111 pref_services
->SetUserPref("webkit.webprefs.foo",
112 new base::StringValue("bar"));
116 // Tests to see that webkit preferences are properly loaded and copied over
117 // to a WebPreferences object.
118 TEST_F(ChromePrefServiceWebKitPrefs
, PrefsCopied
) {
119 WebPreferences webkit_prefs
=
120 RenderViewHostTester::For(rvh())->TestComputeWebkitPrefs();
122 // These values have been overridden by the profile preferences.
123 EXPECT_EQ("UTF-8", webkit_prefs
.default_encoding
);
124 EXPECT_EQ(20, webkit_prefs
.default_font_size
);
125 EXPECT_FALSE(webkit_prefs
.text_areas_are_resizable
);
126 EXPECT_TRUE(webkit_prefs
.uses_universal_detector
);
128 // These should still be the default values.
129 #if defined(OS_MACOSX)
130 const char kDefaultFont
[] = "Times";
131 #elif defined(OS_CHROMEOS)
132 const char kDefaultFont
[] = "Tinos";
134 const char kDefaultFont
[] = "Times New Roman";
136 EXPECT_EQ(base::ASCIIToUTF16(kDefaultFont
),
137 webkit_prefs
.standard_font_family_map
[prefs::kWebKitCommonScript
]);
138 EXPECT_TRUE(webkit_prefs
.javascript_enabled
);