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