[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 5 | #include <string> |
| 6 | |
[email protected] | 3184cba4 | 2009-05-15 01:25:29 | [diff] [blame] | 7 | #include "app/test/data/resource.h" |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 8 | #include "base/command_line.h" |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 9 | #include "base/scoped_ptr.h" |
| 10 | #include "base/values.h" |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 11 | #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 12 | #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 13 | #include "chrome/browser/prefs/browser_prefs.h" |
| 14 | #include "chrome/browser/prefs/command_line_pref_store.h" |
| 15 | #include "chrome/browser/prefs/default_pref_store.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 16 | #include "chrome/browser/prefs/dummy_pref_store.h" |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 17 | #include "chrome/browser/prefs/pref_change_registrar.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 18 | #include "chrome/browser/prefs/pref_value_store.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "chrome/common/chrome_paths.h" |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 20 | #include "chrome/common/chrome_switches.h" |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 21 | #include "chrome/common/notification_observer_mock.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | #include "chrome/common/notification_service.h" |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 23 | #include "chrome/common/notification_type.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | #include "chrome/common/pref_names.h" |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 25 | #include "chrome/test/testing_pref_service.h" |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 26 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | #include "testing/gtest/include/gtest/gtest.h" |
| 28 | |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 29 | using testing::_; |
| 30 | using testing::Mock; |
| 31 | using testing::Pointee; |
| 32 | using testing::Property; |
| 33 | |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 34 | namespace { |
| 35 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | class TestPrefObserver : public NotificationObserver { |
| 37 | public: |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 38 | TestPrefObserver(const PrefService* prefs, |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 39 | const std::string& pref_name, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 40 | const std::string& new_pref_value) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | : observer_fired_(false), |
| 42 | prefs_(prefs), |
| 43 | pref_name_(pref_name), |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 44 | new_pref_value_(new_pref_value) {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | virtual ~TestPrefObserver() {} |
| 46 | |
| 47 | virtual void Observe(NotificationType type, |
| 48 | const NotificationSource& source, |
| 49 | const NotificationDetails& details) { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 50 | EXPECT_EQ(type.value, NotificationType::PREF_CHANGED); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 51 | const PrefService* prefs_in = Source<PrefService>(source).ptr(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | EXPECT_EQ(prefs_in, prefs_); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 53 | const std::string* pref_name_in = Details<std::string>(details).ptr(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | EXPECT_EQ(*pref_name_in, pref_name_); |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 55 | EXPECT_EQ(new_pref_value_, prefs_in->GetString("homepage")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | observer_fired_ = true; |
| 57 | } |
| 58 | |
| 59 | bool observer_fired() { return observer_fired_; } |
| 60 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 61 | void Reset(const std::string& new_pref_value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | observer_fired_ = false; |
| 63 | new_pref_value_ = new_pref_value; |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | bool observer_fired_; |
| 68 | const PrefService* prefs_; |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 69 | const std::string pref_name_; |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 70 | std::string new_pref_value_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | }; |
| 72 | |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 73 | } // namespace |
| 74 | |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 75 | // TODO(port): port this test to POSIX. |
| 76 | #if defined(OS_WIN) |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 77 | TEST(PrefServiceTest, LocalizedPrefs) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 78 | TestingPrefService prefs; |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 79 | const char kBoolean[] = "boolean"; |
| 80 | const char kInteger[] = "integer"; |
| 81 | const char kString[] = "string"; |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 82 | prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL); |
| 83 | prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT); |
| 84 | prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING); |
| 85 | |
| 86 | // The locale default should take preference over the user default. |
| 87 | EXPECT_FALSE(prefs.GetBoolean(kBoolean)); |
| 88 | EXPECT_EQ(1, prefs.GetInteger(kInteger)); |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 89 | EXPECT_EQ("hello", prefs.GetString(kString)); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 90 | |
| 91 | prefs.SetBoolean(kBoolean, true); |
| 92 | EXPECT_TRUE(prefs.GetBoolean(kBoolean)); |
| 93 | prefs.SetInteger(kInteger, 5); |
| 94 | EXPECT_EQ(5, prefs.GetInteger(kInteger)); |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 95 | prefs.SetString(kString, "foo"); |
| 96 | EXPECT_EQ("foo", prefs.GetString(kString)); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 97 | } |
| 98 | #endif |
| 99 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 100 | TEST(PrefServiceTest, NoObserverFire) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 101 | TestingPrefService prefs; |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 102 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 103 | const char pref_name[] = "homepage"; |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 104 | prefs.RegisterStringPref(pref_name, std::string()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 105 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 106 | const std::string new_pref_value("http://www.google.com/"); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 107 | TestPrefObserver obs(&prefs, pref_name, new_pref_value); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 108 | |
| 109 | PrefChangeRegistrar registrar; |
| 110 | registrar.Init(&prefs); |
| 111 | registrar.Add(pref_name, &obs); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 112 | // This should fire the checks in TestPrefObserver::Observe. |
| 113 | prefs.SetString(pref_name, new_pref_value); |
| 114 | |
| 115 | // Make sure the observer was actually fired. |
| 116 | EXPECT_TRUE(obs.observer_fired()); |
| 117 | |
| 118 | // Setting the pref to the same value should not set the pref value a second |
| 119 | // time. |
| 120 | obs.Reset(new_pref_value); |
| 121 | prefs.SetString(pref_name, new_pref_value); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 122 | EXPECT_FALSE(obs.observer_fired()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 123 | |
| 124 | // Clearing the pref should cause the pref to fire. |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 125 | obs.Reset(std::string()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 126 | prefs.ClearPref(pref_name); |
| 127 | EXPECT_TRUE(obs.observer_fired()); |
| 128 | |
| 129 | // Clearing the pref again should not cause the pref to fire. |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 130 | obs.Reset(std::string()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 131 | prefs.ClearPref(pref_name); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 132 | EXPECT_FALSE(obs.observer_fired()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 135 | TEST(PrefServiceTest, HasPrefPath) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 136 | TestingPrefService prefs; |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 137 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 138 | const char path[] = "fake.path"; |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 139 | |
| 140 | // Shouldn't initially have a path. |
| 141 | EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 142 | |
| 143 | // Register the path. This doesn't set a value, so the path still shouldn't |
| 144 | // exist. |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 145 | prefs.RegisterStringPref(path, std::string()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 146 | EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 147 | |
| 148 | // Set a value and make sure we have a path. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 149 | prefs.SetString(path, "blah"); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 150 | EXPECT_TRUE(prefs.HasPrefPath(path)); |
| 151 | } |
| 152 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 153 | TEST(PrefServiceTest, Observers) { |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 154 | const char pref_name[] = "homepage"; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 155 | |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 156 | TestingPrefService prefs; |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 157 | prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com")); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 158 | prefs.RegisterStringPref(pref_name, std::string()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 159 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 160 | const std::string new_pref_value("http://www.google.com/"); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 161 | TestPrefObserver obs(&prefs, pref_name, new_pref_value); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 162 | PrefChangeRegistrar registrar; |
| 163 | registrar.Init(&prefs); |
| 164 | registrar.Add(pref_name, &obs); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 165 | // This should fire the checks in TestPrefObserver::Observe. |
| 166 | prefs.SetString(pref_name, new_pref_value); |
| 167 | |
| 168 | // Make sure the tests were actually run. |
| 169 | EXPECT_TRUE(obs.observer_fired()); |
| 170 | |
| 171 | // Now try adding a second pref observer. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 172 | const std::string new_pref_value2("http://www.youtube.com/"); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 173 | obs.Reset(new_pref_value2); |
| 174 | TestPrefObserver obs2(&prefs, pref_name, new_pref_value2); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 175 | registrar.Add(pref_name, &obs2); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 176 | // This should fire the checks in obs and obs2. |
| 177 | prefs.SetString(pref_name, new_pref_value2); |
| 178 | EXPECT_TRUE(obs.observer_fired()); |
| 179 | EXPECT_TRUE(obs2.observer_fired()); |
| 180 | |
| 181 | // Make sure obs2 still works after removing obs. |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 182 | registrar.Remove(pref_name, &obs); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 183 | obs.Reset(std::string()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 184 | obs2.Reset(new_pref_value); |
| 185 | // This should only fire the observer in obs2. |
| 186 | prefs.SetString(pref_name, new_pref_value); |
| 187 | EXPECT_FALSE(obs.observer_fired()); |
| 188 | EXPECT_TRUE(obs2.observer_fired()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 191 | TEST(PrefServiceTest, ProxyFromCommandLineNotPolicy) { |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 192 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 193 | command_line.AppendSwitch(switches::kProxyAutoDetect); |
| 194 | TestingPrefService prefs(NULL, &command_line); |
| 195 | browser::RegisterUserPrefs(&prefs); |
| 196 | EXPECT_TRUE(prefs.GetBoolean(prefs::kProxyAutoDetect)); |
| 197 | const PrefService::Preference* pref = |
| 198 | prefs.FindPreference(prefs::kProxyAutoDetect); |
| 199 | ASSERT_TRUE(pref); |
| 200 | EXPECT_FALSE(pref->IsManaged()); |
| 201 | } |
| 202 | |
| 203 | TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineOptions) { |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 204 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 205 | command_line.AppendSwitchASCII(switches::kProxyBypassList, "123"); |
| 206 | command_line.AppendSwitchASCII(switches::kProxyPacUrl, "456"); |
| 207 | command_line.AppendSwitchASCII(switches::kProxyServer, "789"); |
| 208 | scoped_ptr<policy::MockConfigurationPolicyProvider> provider( |
| 209 | new policy::MockConfigurationPolicyProvider()); |
| 210 | Value* mode_value = Value::CreateIntegerValue( |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 211 | policy::kPolicyManuallyConfiguredProxyMode); |
| 212 | provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value); |
| 213 | provider->AddPolicy(policy::kPolicyProxyBypassList, |
| 214 | Value::CreateStringValue("abc")); |
| 215 | provider->AddPolicy(policy::kPolicyProxyPacUrl, |
| 216 | Value::CreateStringValue("def")); |
| 217 | provider->AddPolicy(policy::kPolicyProxyServer, |
| 218 | Value::CreateStringValue("ghi")); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 219 | |
| 220 | // First verify that command-line options are set correctly when |
| 221 | // there is no policy in effect. |
| 222 | TestingPrefService prefs(NULL, &command_line); |
| 223 | browser::RegisterUserPrefs(&prefs); |
| 224 | EXPECT_FALSE(prefs.GetBoolean(prefs::kProxyAutoDetect)); |
| 225 | EXPECT_FALSE(prefs.GetBoolean(prefs::kNoProxyServer)); |
| 226 | EXPECT_EQ("789", prefs.GetString(prefs::kProxyServer)); |
| 227 | EXPECT_EQ("456", prefs.GetString(prefs::kProxyPacUrl)); |
| 228 | EXPECT_EQ("123", prefs.GetString(prefs::kProxyBypassList)); |
| 229 | |
| 230 | // Try a second time time with the managed PrefStore in place, the |
| 231 | // manual proxy policy should have removed all traces of the command |
| 232 | // line and replaced them with the policy versions. |
| 233 | TestingPrefService prefs2(provider.get(), &command_line); |
| 234 | browser::RegisterUserPrefs(&prefs2); |
| 235 | EXPECT_FALSE(prefs2.GetBoolean(prefs::kProxyAutoDetect)); |
| 236 | EXPECT_FALSE(prefs2.GetBoolean(prefs::kNoProxyServer)); |
| 237 | EXPECT_EQ("ghi", prefs2.GetString(prefs::kProxyServer)); |
| 238 | EXPECT_EQ("def", prefs2.GetString(prefs::kProxyPacUrl)); |
| 239 | EXPECT_EQ("abc", prefs2.GetString(prefs::kProxyBypassList)); |
| 240 | } |
| 241 | |
| 242 | TEST(PrefServiceTest, ProxyPolicyOverridesUnrelatedCommandLineOptions) { |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 243 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 244 | command_line.AppendSwitchASCII(switches::kProxyBypassList, "123"); |
| 245 | command_line.AppendSwitchASCII(switches::kProxyPacUrl, "456"); |
| 246 | command_line.AppendSwitchASCII(switches::kProxyServer, "789"); |
| 247 | scoped_ptr<policy::MockConfigurationPolicyProvider> provider( |
| 248 | new policy::MockConfigurationPolicyProvider()); |
| 249 | Value* mode_value = Value::CreateIntegerValue( |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 250 | policy::kPolicyUseSystemProxyMode); |
| 251 | provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 252 | |
| 253 | // First verify that command-line options are set correctly when |
| 254 | // there is no policy in effect. |
| 255 | TestingPrefService prefs(NULL, &command_line); |
| 256 | browser::RegisterUserPrefs(&prefs); |
| 257 | EXPECT_FALSE(prefs.GetBoolean(prefs::kProxyAutoDetect)); |
| 258 | EXPECT_FALSE(prefs.GetBoolean(prefs::kNoProxyServer)); |
| 259 | EXPECT_EQ("789", prefs.GetString(prefs::kProxyServer)); |
| 260 | EXPECT_EQ("456", prefs.GetString(prefs::kProxyPacUrl)); |
| 261 | EXPECT_EQ("123", prefs.GetString(prefs::kProxyBypassList)); |
| 262 | |
| 263 | // Try a second time time with the managed PrefStore in place, the |
| 264 | // no proxy policy should have removed all traces of the command |
| 265 | // line proxy settings, even though they were not the specific one |
| 266 | // set in policy. |
| 267 | TestingPrefService prefs2(provider.get(), &command_line); |
| 268 | browser::RegisterUserPrefs(&prefs2); |
| 269 | EXPECT_FALSE(prefs2.GetBoolean(prefs::kProxyAutoDetect)); |
| 270 | EXPECT_FALSE(prefs2.GetBoolean(prefs::kNoProxyServer)); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 271 | EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyServer)); |
| 272 | EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyPacUrl)); |
| 273 | EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyBypassList)); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineNoProxy) { |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 277 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 278 | command_line.AppendSwitch(switches::kNoProxyServer); |
| 279 | scoped_ptr<policy::MockConfigurationPolicyProvider> provider( |
| 280 | new policy::MockConfigurationPolicyProvider()); |
| 281 | Value* mode_value = Value::CreateIntegerValue( |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 282 | policy::kPolicyAutoDetectProxyMode); |
| 283 | provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 284 | |
| 285 | // First verify that command-line options are set correctly when |
| 286 | // there is no policy in effect. |
| 287 | TestingPrefService prefs(NULL, &command_line); |
| 288 | browser::RegisterUserPrefs(&prefs); |
| 289 | EXPECT_FALSE(prefs.GetBoolean(prefs::kProxyAutoDetect)); |
| 290 | EXPECT_TRUE(prefs.GetBoolean(prefs::kNoProxyServer)); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 291 | EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyServer)); |
| 292 | EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyPacUrl)); |
| 293 | EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyBypassList)); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 294 | |
| 295 | // Try a second time time with the managed PrefStore in place, the |
| 296 | // auto-detect should be overridden. The default pref store must be |
| 297 | // in place with the appropriate default value for this to work. |
| 298 | TestingPrefService prefs2(provider.get(), &command_line); |
| 299 | browser::RegisterUserPrefs(&prefs2); |
| 300 | EXPECT_TRUE(prefs2.GetBoolean(prefs::kProxyAutoDetect)); |
| 301 | EXPECT_FALSE(prefs2.GetBoolean(prefs::kNoProxyServer)); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 302 | EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyServer)); |
| 303 | EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyPacUrl)); |
| 304 | EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyBypassList)); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineAutoDetect) { |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 308 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 309 | command_line.AppendSwitch(switches::kProxyAutoDetect); |
| 310 | scoped_ptr<policy::MockConfigurationPolicyProvider> provider( |
| 311 | new policy::MockConfigurationPolicyProvider()); |
| 312 | Value* mode_value = Value::CreateIntegerValue( |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 313 | policy::kPolicyNoProxyServerMode); |
| 314 | provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 315 | |
| 316 | // First verify that the auto-detect is set if there is no managed |
| 317 | // PrefStore. |
| 318 | TestingPrefService prefs(NULL, &command_line); |
| 319 | browser::RegisterUserPrefs(&prefs); |
| 320 | EXPECT_TRUE(prefs.GetBoolean(prefs::kProxyAutoDetect)); |
| 321 | EXPECT_FALSE(prefs.GetBoolean(prefs::kNoProxyServer)); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 322 | EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyServer)); |
| 323 | EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyPacUrl)); |
| 324 | EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyBypassList)); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 325 | |
| 326 | // Try a second time time with the managed PrefStore in place, the |
| 327 | // auto-detect should be overridden. The default pref store must be |
| 328 | // in place with the appropriate default value for this to work. |
| 329 | TestingPrefService prefs2(provider.get(), &command_line); |
| 330 | browser::RegisterUserPrefs(&prefs2); |
| 331 | EXPECT_FALSE(prefs2.GetBoolean(prefs::kProxyAutoDetect)); |
| 332 | EXPECT_TRUE(prefs2.GetBoolean(prefs::kNoProxyServer)); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 333 | EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyServer)); |
| 334 | EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyPacUrl)); |
| 335 | EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyBypassList)); |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 336 | } |
| 337 | |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 338 | class PrefServiceSetValueTest : public testing::Test { |
| 339 | protected: |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 340 | static const char kName[]; |
| 341 | static const char kValue[]; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 342 | |
| 343 | PrefServiceSetValueTest() |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 344 | : name_string_(kName), |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 345 | null_value_(Value::CreateNullValue()) {} |
| 346 | |
| 347 | void SetExpectNoNotification() { |
| 348 | EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
| 349 | } |
| 350 | |
| 351 | void SetExpectPrefChanged() { |
| 352 | EXPECT_CALL(observer_, |
| 353 | Observe(NotificationType(NotificationType::PREF_CHANGED), _, |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 354 | Property(&Details<std::string>::ptr, |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 355 | Pointee(name_string_)))); |
| 356 | } |
| 357 | |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 358 | TestingPrefService prefs_; |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 359 | std::string name_string_; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 360 | scoped_ptr<Value> null_value_; |
| 361 | NotificationObserverMock observer_; |
| 362 | }; |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 363 | |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 364 | const char PrefServiceSetValueTest::kName[] = "name"; |
| 365 | const char PrefServiceSetValueTest::kValue[] = "value"; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 366 | |
| 367 | TEST_F(PrefServiceSetValueTest, SetStringValue) { |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 368 | const char default_string[] = "default"; |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 369 | const scoped_ptr<Value> default_value( |
| 370 | Value::CreateStringValue(default_string)); |
| 371 | prefs_.RegisterStringPref(kName, default_string); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 372 | |
| 373 | PrefChangeRegistrar registrar; |
| 374 | registrar.Init(&prefs_); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 375 | registrar.Add(kName, &observer_); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 376 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 377 | // Changing the controlling store from default to user triggers notification. |
| 378 | SetExpectPrefChanged(); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 379 | prefs_.Set(kName, *default_value); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 380 | Mock::VerifyAndClearExpectations(&observer_); |
| 381 | |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 382 | SetExpectNoNotification(); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 383 | prefs_.Set(kName, *default_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 384 | Mock::VerifyAndClearExpectations(&observer_); |
| 385 | |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 386 | const scoped_ptr<Value> new_value(Value::CreateStringValue(kValue)); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 387 | SetExpectPrefChanged(); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 388 | prefs_.Set(kName, *new_value); |
| 389 | EXPECT_EQ(kValue, prefs_.GetString(kName)); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 393 | prefs_.RegisterDictionaryPref(kName); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 394 | PrefChangeRegistrar registrar; |
| 395 | registrar.Init(&prefs_); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 396 | registrar.Add(kName, &observer_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 397 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 398 | // Dictionary values are special: setting one to NULL is the same as clearing |
| 399 | // the user value, allowing the NULL default to take (or keep) control. |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 400 | SetExpectNoNotification(); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 401 | prefs_.Set(kName, *null_value_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 402 | Mock::VerifyAndClearExpectations(&observer_); |
| 403 | |
| 404 | DictionaryValue new_value; |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 405 | new_value.SetString(kName, kValue); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 406 | SetExpectPrefChanged(); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 407 | prefs_.Set(kName, new_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 408 | Mock::VerifyAndClearExpectations(&observer_); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 409 | DictionaryValue* dict = prefs_.GetMutableDictionary(kName); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 410 | EXPECT_EQ(1U, dict->size()); |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 411 | std::string out_value; |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 412 | dict->GetString(kName, &out_value); |
| 413 | EXPECT_EQ(kValue, out_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 414 | |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 415 | SetExpectNoNotification(); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 416 | prefs_.Set(kName, new_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 417 | Mock::VerifyAndClearExpectations(&observer_); |
| 418 | |
| 419 | SetExpectPrefChanged(); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 420 | prefs_.Set(kName, *null_value_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 421 | Mock::VerifyAndClearExpectations(&observer_); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 422 | dict = prefs_.GetMutableDictionary(kName); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 423 | EXPECT_EQ(0U, dict->size()); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | TEST_F(PrefServiceSetValueTest, SetListValue) { |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 427 | prefs_.RegisterListPref(kName); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 428 | PrefChangeRegistrar registrar; |
| 429 | registrar.Init(&prefs_); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 430 | registrar.Add(kName, &observer_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 431 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 432 | // List values are special: setting one to NULL is the same as clearing the |
| 433 | // user value, allowing the NULL default to take (or keep) control. |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 434 | SetExpectNoNotification(); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 435 | prefs_.Set(kName, *null_value_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 436 | Mock::VerifyAndClearExpectations(&observer_); |
| 437 | |
| 438 | ListValue new_value; |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 439 | new_value.Append(Value::CreateStringValue(kValue)); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 440 | SetExpectPrefChanged(); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 441 | prefs_.Set(kName, new_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 442 | Mock::VerifyAndClearExpectations(&observer_); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 443 | const ListValue* list = prefs_.GetMutableList(kName); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 444 | ASSERT_EQ(1U, list->GetSize()); |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 445 | std::string out_value; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 446 | list->GetString(0, &out_value); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 447 | EXPECT_EQ(kValue, out_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 448 | |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 449 | SetExpectNoNotification(); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 450 | prefs_.Set(kName, new_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 451 | Mock::VerifyAndClearExpectations(&observer_); |
| 452 | |
| 453 | SetExpectPrefChanged(); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 454 | prefs_.Set(kName, *null_value_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 455 | Mock::VerifyAndClearExpectations(&observer_); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame^] | 456 | list = prefs_.GetMutableList(kName); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 457 | EXPECT_EQ(0U, list->GetSize()); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 458 | } |