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/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/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/common/web_preferences.h" 27 #include "content/public/test/web_contents_tester.h" 28 #include "ui/base/test/data/resource.h" 29 30 using content::WebContentsTester; 31 using content::WebPreferences; 32 33 TEST(ChromePrefServiceTest, UpdateCommandLinePrefStore) { 34 TestingPrefServiceSimple prefs; 35 prefs.registry()->RegisterBooleanPref(prefs::kCloudPrintProxyEnabled, false); 36 37 // Check to make sure the value is as expected. 38 const PrefService::Preference* pref = 39 prefs.FindPreference(prefs::kCloudPrintProxyEnabled); 40 ASSERT_TRUE(pref); 41 const base::Value* value = pref->GetValue(); 42 ASSERT_TRUE(value); 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); 47 48 // Change the command line. 49 CommandLine cmd_line(CommandLine::NO_PROGRAM); 50 cmd_line.AppendSwitch(switches::kEnableCloudPrintProxy); 51 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); 55 ASSERT_TRUE(pref); 56 value = pref->GetValue(); 57 ASSERT_TRUE(value); 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); 62 } 63 64 class ChromePrefServiceUserFilePrefsTest : public testing::Test { 65 protected: 66 virtual void SetUp() { 67 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 68 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_)); 72 } 73 74 void ClearListValue(PrefService* prefs, const char* key) { 75 ListPrefUpdate updater(prefs, key); 76 updater->Clear(); 77 } 78 79 void ClearDictionaryValue(PrefService* prefs, const char* key) { 80 DictionaryPrefUpdate updater(prefs, key); 81 updater->Clear(); 82 } 83 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_; 90 }; 91 92 class ChromePrefServiceWebKitPrefs : public ChromeRenderViewHostTestHarness { 93 protected: 94 virtual void SetUp() { 95 ChromeRenderViewHostTestHarness::SetUp(); 96 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. 99 100 // Set some (WebKit) user preferences. 101 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")); 113 } 114 }; 115 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 WebContentsTester::For(web_contents())->TestComputeWebkitPrefs(); 121 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); 127 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"; 133 #else 134 const char kDefaultFont[] = "Times New Roman"; 135 #endif 136 EXPECT_EQ(base::ASCIIToUTF16(kDefaultFont), 137 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]); 138 EXPECT_TRUE(webkit_prefs.javascript_enabled); 139 } 140