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/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/browser/prefs/pref_service_mock_factory.h" 17 #include "chrome/common/chrome_paths.h" 18 #include "chrome/common/chrome_switches.h" 19 #include "chrome/common/pref_names.h" 20 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 21 #include "chrome/test/base/testing_pref_service_syncable.h" 22 #include "chrome/test/base/testing_profile.h" 23 #include "components/policy/core/browser/configuration_policy_pref_store.h" 24 #include "components/policy/core/common/mock_configuration_policy_provider.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 30 using content::WebContentsTester; 31 32 TEST(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 63 class 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 class ChromePrefServiceWebKitPrefs : public ChromeRenderViewHostTestHarness { 92 protected: 93 virtual void SetUp() { 94 ChromeRenderViewHostTestHarness::SetUp(); 95 96 // Supply our own profile so we use the correct profile data. The test 97 // harness is not supposed to overwrite a profile if it's already created. 98 99 // Set some (WebKit) user preferences. 100 TestingPrefServiceSyncable* pref_services = 101 profile()->GetTestingPrefService(); 102 #if defined(TOOLKIT_GTK) 103 pref_services->SetUserPref(prefs::kUsesSystemTheme, 104 Value::CreateBooleanValue(false)); 105 #endif 106 pref_services->SetUserPref(prefs::kDefaultCharset, 107 Value::CreateStringValue("utf8")); 108 pref_services->SetUserPref(prefs::kWebKitDefaultFontSize, 109 Value::CreateIntegerValue(20)); 110 pref_services->SetUserPref(prefs::kWebKitTextAreasAreResizable, 111 Value::CreateBooleanValue(false)); 112 pref_services->SetUserPref(prefs::kWebKitUsesUniversalDetector, 113 Value::CreateBooleanValue(true)); 114 pref_services->SetUserPref("webkit.webprefs.foo", 115 Value::CreateStringValue("bar")); 116 } 117 }; 118 119 // Tests to see that webkit preferences are properly loaded and copied over 120 // to a WebPreferences object. 121 TEST_F(ChromePrefServiceWebKitPrefs, PrefsCopied) { 122 WebPreferences webkit_prefs = 123 WebContentsTester::For(web_contents())->TestGetWebkitPrefs(); 124 125 // These values have been overridden by the profile preferences. 126 EXPECT_EQ("UTF-8", webkit_prefs.default_encoding); 127 EXPECT_EQ(20, webkit_prefs.default_font_size); 128 EXPECT_FALSE(webkit_prefs.text_areas_are_resizable); 129 EXPECT_TRUE(webkit_prefs.uses_universal_detector); 130 131 // These should still be the default values. 132 #if defined(OS_MACOSX) 133 const char kDefaultFont[] = "Times"; 134 #elif defined(OS_CHROMEOS) 135 const char kDefaultFont[] = "Tinos"; 136 #else 137 const char kDefaultFont[] = "Times New Roman"; 138 #endif 139 EXPECT_EQ(ASCIIToUTF16(kDefaultFont), 140 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]); 141 EXPECT_TRUE(webkit_prefs.javascript_enabled); 142 } 143