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/pref_registry/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 base::Value* value = pref->GetValue(); 41 ASSERT_TRUE(value); 42 EXPECT_EQ(base::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(base::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 pref_services->SetUserPref(prefs::kDefaultCharset, 103 base::Value::CreateStringValue("utf8")); 104 pref_services->SetUserPref(prefs::kWebKitDefaultFontSize, 105 base::Value::CreateIntegerValue(20)); 106 pref_services->SetUserPref(prefs::kWebKitTextAreasAreResizable, 107 base::Value::CreateBooleanValue(false)); 108 pref_services->SetUserPref(prefs::kWebKitUsesUniversalDetector, 109 base::Value::CreateBooleanValue(true)); 110 pref_services->SetUserPref("webkit.webprefs.foo", 111 base::Value::CreateStringValue("bar")); 112 } 113 }; 114 115 // Tests to see that webkit preferences are properly loaded and copied over 116 // to a WebPreferences object. 117 TEST_F(ChromePrefServiceWebKitPrefs, PrefsCopied) { 118 WebPreferences webkit_prefs = 119 WebContentsTester::For(web_contents())->TestGetWebkitPrefs(); 120 121 // These values have been overridden by the profile preferences. 122 EXPECT_EQ("UTF-8", webkit_prefs.default_encoding); 123 EXPECT_EQ(20, webkit_prefs.default_font_size); 124 EXPECT_FALSE(webkit_prefs.text_areas_are_resizable); 125 EXPECT_TRUE(webkit_prefs.uses_universal_detector); 126 127 // These should still be the default values. 128 #if defined(OS_MACOSX) 129 const char kDefaultFont[] = "Times"; 130 #elif defined(OS_CHROMEOS) 131 const char kDefaultFont[] = "Tinos"; 132 #else 133 const char kDefaultFont[] = "Times New Roman"; 134 #endif 135 EXPECT_EQ(base::ASCIIToUTF16(kDefaultFont), 136 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]); 137 EXPECT_TRUE(webkit_prefs.javascript_enabled); 138 } 139