Chromium Code Reviews

Side by Side Diff: chrome/browser/prefs/pref_service_unittest.cc

Issue 5646003: Sanitize PrefStore interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix PrefService mock construction in PrefServiceTest to include command line store. Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
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"
11 #include "chrome/browser/policy/configuration_policy_pref_store.h" 11 #include "chrome/browser/policy/configuration_policy_pref_store.h"
12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" 12 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
13 #include "chrome/browser/prefs/browser_prefs.h" 13 #include "chrome/browser/prefs/browser_prefs.h"
14 #include "chrome/browser/prefs/command_line_pref_store.h" 14 #include "chrome/browser/prefs/command_line_pref_store.h"
15 #include "chrome/browser/prefs/testing_pref_store.h"
16 #include "chrome/browser/prefs/pref_change_registrar.h" 15 #include "chrome/browser/prefs/pref_change_registrar.h"
17 #include "chrome/browser/prefs/pref_observer_mock.h" 16 #include "chrome/browser/prefs/pref_observer_mock.h"
17 #include "chrome/browser/prefs/pref_service_mock_builder.h"
18 #include "chrome/browser/prefs/pref_value_store.h" 18 #include "chrome/browser/prefs/pref_value_store.h"
19 #include "chrome/browser/prefs/testing_pref_store.h"
19 #include "chrome/common/chrome_paths.h" 20 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/pref_names.h" 22 #include "chrome/common/pref_names.h"
22 #include "chrome/test/testing_pref_service.h" 23 #include "chrome/test/testing_pref_service.h"
23 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 26
26 using testing::_; 27 using testing::_;
27 using testing::Mock; 28 using testing::Mock;
28 29
(...skipping 110 matching lines...)
139 // Make sure obs2 still works after removing obs. 140 // Make sure obs2 still works after removing obs.
140 registrar.Remove(pref_name, &obs); 141 registrar.Remove(pref_name, &obs);
141 EXPECT_CALL(obs, Observe(_, _, _)).Times(0); 142 EXPECT_CALL(obs, Observe(_, _, _)).Times(0);
142 obs2.Expect(&prefs, pref_name, &expected_new_pref_value); 143 obs2.Expect(&prefs, pref_name, &expected_new_pref_value);
143 // This should only fire the observer in obs2. 144 // This should only fire the observer in obs2.
144 prefs.SetString(pref_name, new_pref_value); 145 prefs.SetString(pref_name, new_pref_value);
145 Mock::VerifyAndClearExpectations(&obs); 146 Mock::VerifyAndClearExpectations(&obs);
146 Mock::VerifyAndClearExpectations(&obs2); 147 Mock::VerifyAndClearExpectations(&obs2);
147 } 148 }
148 149
149 TEST(PrefServiceTest, ProxyFromCommandLineNotPolicy) {
150 CommandLine command_line(CommandLine::NO_PROGRAM);
151 command_line.AppendSwitch(switches::kProxyAutoDetect);
152 TestingPrefService prefs(NULL, NULL, &command_line);
153 browser::RegisterUserPrefs(&prefs);
154 EXPECT_TRUE(prefs.GetBoolean(prefs::kProxyAutoDetect));
155 const PrefService::Preference* pref =
156 prefs.FindPreference(prefs::kProxyAutoDetect);
157 ASSERT_TRUE(pref);
158 EXPECT_FALSE(pref->IsManaged());
159 }
160
161 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineOptions) { 150 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineOptions) {
162 CommandLine command_line(CommandLine::NO_PROGRAM); 151 CommandLine command_line(CommandLine::NO_PROGRAM);
163 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123"); 152 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123");
164 command_line.AppendSwitchASCII(switches::kProxyPacUrl, "456"); 153 command_line.AppendSwitchASCII(switches::kProxyPacUrl, "456");
165 command_line.AppendSwitchASCII(switches::kProxyServer, "789"); 154 command_line.AppendSwitchASCII(switches::kProxyServer, "789");
166 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( 155 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
167 new policy::MockConfigurationPolicyProvider()); 156 new policy::MockConfigurationPolicyProvider());
168 Value* mode_value = Value::CreateIntegerValue( 157 Value* mode_value = Value::CreateIntegerValue(
169 policy::kPolicyManuallyConfiguredProxyMode); 158 policy::kPolicyManuallyConfiguredProxyMode);
170 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value); 159 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value);
171 provider->AddPolicy(policy::kPolicyProxyBypassList, 160 provider->AddPolicy(policy::kPolicyProxyBypassList,
172 Value::CreateStringValue("abc")); 161 Value::CreateStringValue("abc"));
173 provider->AddPolicy(policy::kPolicyProxyPacUrl, 162 provider->AddPolicy(policy::kPolicyProxyPacUrl,
174 Value::CreateStringValue("def")); 163 Value::CreateStringValue("def"));
175 provider->AddPolicy(policy::kPolicyProxyServer, 164 provider->AddPolicy(policy::kPolicyProxyServer,
176 Value::CreateStringValue("ghi")); 165 Value::CreateStringValue("ghi"));
177 166
178 // First verify that command-line options are set correctly when 167 // First verify that command-line options are set correctly when
179 // there is no policy in effect. 168 // there is no policy in effect.
180 TestingPrefService prefs(NULL, NULL, &command_line); 169 PrefServiceMockBuilder builder;
181 browser::RegisterUserPrefs(&prefs); 170 builder.WithCommandLine(&command_line);
182 EXPECT_FALSE(prefs.GetBoolean(prefs::kProxyAutoDetect)); 171 scoped_ptr<PrefService> prefs(builder.Create());
183 EXPECT_FALSE(prefs.GetBoolean(prefs::kNoProxyServer)); 172 browser::RegisterUserPrefs(prefs.get());
184 EXPECT_EQ("789", prefs.GetString(prefs::kProxyServer)); 173 EXPECT_FALSE(prefs->GetBoolean(prefs::kProxyAutoDetect));
185 EXPECT_EQ("456", prefs.GetString(prefs::kProxyPacUrl)); 174 EXPECT_FALSE(prefs->GetBoolean(prefs::kNoProxyServer));
186 EXPECT_EQ("123", prefs.GetString(prefs::kProxyBypassList)); 175 EXPECT_EQ("789", prefs->GetString(prefs::kProxyServer));
176 EXPECT_EQ("456", prefs->GetString(prefs::kProxyPacUrl));
177 EXPECT_EQ("123", prefs->GetString(prefs::kProxyBypassList));
187 178
188 // Try a second time time with the managed PrefStore in place, the 179 // Try a second time time with the managed PrefStore in place, the
189 // manual proxy policy should have removed all traces of the command 180 // manual proxy policy should have removed all traces of the command
190 // line and replaced them with the policy versions. 181 // line and replaced them with the policy versions.
191 TestingPrefService prefs2(provider.get(), NULL, &command_line); 182 builder.WithCommandLine(&command_line);
192 browser::RegisterUserPrefs(&prefs2); 183 builder.WithManagedPlatformProvider(provider.get());
193 EXPECT_FALSE(prefs2.GetBoolean(prefs::kProxyAutoDetect)); 184 scoped_ptr<PrefService> prefs2(builder.Create());
194 EXPECT_FALSE(prefs2.GetBoolean(prefs::kNoProxyServer)); 185 browser::RegisterUserPrefs(prefs2.get());
195 EXPECT_EQ("ghi", prefs2.GetString(prefs::kProxyServer)); 186 EXPECT_FALSE(prefs2->GetBoolean(prefs::kProxyAutoDetect));
196 EXPECT_EQ("def", prefs2.GetString(prefs::kProxyPacUrl)); 187 EXPECT_FALSE(prefs2->GetBoolean(prefs::kNoProxyServer));
197 EXPECT_EQ("abc", prefs2.GetString(prefs::kProxyBypassList)); 188 EXPECT_EQ("ghi", prefs2->GetString(prefs::kProxyServer));
189 EXPECT_EQ("def", prefs2->GetString(prefs::kProxyPacUrl));
190 EXPECT_EQ("abc", prefs2->GetString(prefs::kProxyBypassList));
198 } 191 }
199 192
200 TEST(PrefServiceTest, ProxyPolicyOverridesUnrelatedCommandLineOptions) { 193 TEST(PrefServiceTest, ProxyPolicyOverridesUnrelatedCommandLineOptions) {
201 CommandLine command_line(CommandLine::NO_PROGRAM); 194 CommandLine command_line(CommandLine::NO_PROGRAM);
202 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123"); 195 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123");
203 command_line.AppendSwitchASCII(switches::kProxyPacUrl, "456"); 196 command_line.AppendSwitchASCII(switches::kProxyPacUrl, "456");
204 command_line.AppendSwitchASCII(switches::kProxyServer, "789"); 197 command_line.AppendSwitchASCII(switches::kProxyServer, "789");
205 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( 198 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
206 new policy::MockConfigurationPolicyProvider()); 199 new policy::MockConfigurationPolicyProvider());
207 Value* mode_value = Value::CreateIntegerValue( 200 Value* mode_value = Value::CreateIntegerValue(
208 policy::kPolicyUseSystemProxyMode); 201 policy::kPolicyUseSystemProxyMode);
209 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value); 202 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value);
210 203
211 // First verify that command-line options are set correctly when 204 // First verify that command-line options are set correctly when
212 // there is no policy in effect. 205 // there is no policy in effect.
213 TestingPrefService prefs(NULL, NULL, &command_line); 206 PrefServiceMockBuilder builder;
214 browser::RegisterUserPrefs(&prefs); 207 builder.WithCommandLine(&command_line);
215 EXPECT_FALSE(prefs.GetBoolean(prefs::kProxyAutoDetect)); 208 scoped_ptr<PrefService> prefs(builder.Create());
216 EXPECT_FALSE(prefs.GetBoolean(prefs::kNoProxyServer)); 209 browser::RegisterUserPrefs(prefs.get());
217 EXPECT_EQ("789", prefs.GetString(prefs::kProxyServer)); 210 EXPECT_FALSE(prefs->GetBoolean(prefs::kProxyAutoDetect));
218 EXPECT_EQ("456", prefs.GetString(prefs::kProxyPacUrl)); 211 EXPECT_FALSE(prefs->GetBoolean(prefs::kNoProxyServer));
219 EXPECT_EQ("123", prefs.GetString(prefs::kProxyBypassList)); 212 EXPECT_EQ("789", prefs->GetString(prefs::kProxyServer));
213 EXPECT_EQ("456", prefs->GetString(prefs::kProxyPacUrl));
214 EXPECT_EQ("123", prefs->GetString(prefs::kProxyBypassList));
220 215
221 // Try a second time time with the managed PrefStore in place, the 216 // Try a second time time with the managed PrefStore in place, the
222 // no proxy policy should have removed all traces of the command 217 // no proxy policy should have removed all traces of the command
223 // line proxy settings, even though they were not the specific one 218 // line proxy settings, even though they were not the specific one
224 // set in policy. 219 // set in policy.
225 TestingPrefService prefs2(provider.get(), NULL, &command_line); 220 builder.WithCommandLine(&command_line);
226 browser::RegisterUserPrefs(&prefs2); 221 builder.WithManagedPlatformProvider(provider.get());
227 EXPECT_FALSE(prefs2.GetBoolean(prefs::kProxyAutoDetect)); 222 scoped_ptr<PrefService> prefs2(builder.Create());
228 EXPECT_FALSE(prefs2.GetBoolean(prefs::kNoProxyServer)); 223 browser::RegisterUserPrefs(prefs2.get());
229 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyServer)); 224 EXPECT_FALSE(prefs2->GetBoolean(prefs::kProxyAutoDetect));
230 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyPacUrl)); 225 EXPECT_FALSE(prefs2->GetBoolean(prefs::kNoProxyServer));
231 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyBypassList)); 226 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyServer));
227 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyPacUrl));
228 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyBypassList));
232 } 229 }
233 230
234 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineNoProxy) { 231 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineNoProxy) {
235 CommandLine command_line(CommandLine::NO_PROGRAM); 232 CommandLine command_line(CommandLine::NO_PROGRAM);
236 command_line.AppendSwitch(switches::kNoProxyServer); 233 command_line.AppendSwitch(switches::kNoProxyServer);
237 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( 234 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
238 new policy::MockConfigurationPolicyProvider()); 235 new policy::MockConfigurationPolicyProvider());
239 Value* mode_value = Value::CreateIntegerValue( 236 Value* mode_value = Value::CreateIntegerValue(
240 policy::kPolicyAutoDetectProxyMode); 237 policy::kPolicyAutoDetectProxyMode);
241 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value); 238 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value);
242 239
243 // First verify that command-line options are set correctly when 240 // First verify that command-line options are set correctly when
244 // there is no policy in effect. 241 // there is no policy in effect.
245 TestingPrefService prefs(NULL, NULL, &command_line); 242 PrefServiceMockBuilder builder;
246 browser::RegisterUserPrefs(&prefs); 243 builder.WithCommandLine(&command_line);
247 EXPECT_FALSE(prefs.GetBoolean(prefs::kProxyAutoDetect)); 244 scoped_ptr<PrefService> prefs(builder.Create());
248 EXPECT_TRUE(prefs.GetBoolean(prefs::kNoProxyServer)); 245 browser::RegisterUserPrefs(prefs.get());
249 EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyServer)); 246 EXPECT_FALSE(prefs->GetBoolean(prefs::kProxyAutoDetect));
250 EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyPacUrl)); 247 EXPECT_TRUE(prefs->GetBoolean(prefs::kNoProxyServer));
251 EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyBypassList)); 248 EXPECT_EQ(std::string(), prefs->GetString(prefs::kProxyServer));
249 EXPECT_EQ(std::string(), prefs->GetString(prefs::kProxyPacUrl));
250 EXPECT_EQ(std::string(), prefs->GetString(prefs::kProxyBypassList));
252 251
253 // Try a second time time with the managed PrefStore in place, the 252 // Try a second time time with the managed PrefStore in place, the
254 // auto-detect should be overridden. The default pref store must be 253 // auto-detect should be overridden. The default pref store must be
255 // in place with the appropriate default value for this to work. 254 // in place with the appropriate default value for this to work.
256 TestingPrefService prefs2(provider.get(), NULL, &command_line); 255 builder.WithCommandLine(&command_line);
257 browser::RegisterUserPrefs(&prefs2); 256 builder.WithManagedPlatformProvider(provider.get());
258 EXPECT_TRUE(prefs2.GetBoolean(prefs::kProxyAutoDetect)); 257 scoped_ptr<PrefService> prefs2(builder.Create());
259 EXPECT_FALSE(prefs2.GetBoolean(prefs::kNoProxyServer)); 258 browser::RegisterUserPrefs(prefs2.get());
260 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyServer)); 259 EXPECT_TRUE(prefs2->GetBoolean(prefs::kProxyAutoDetect));
261 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyPacUrl)); 260 EXPECT_FALSE(prefs2->GetBoolean(prefs::kNoProxyServer));
262 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyBypassList)); 261 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyServer));
262 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyPacUrl));
263 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyBypassList));
263 } 264 }
264 265
265 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineAutoDetect) { 266 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineAutoDetect) {
266 CommandLine command_line(CommandLine::NO_PROGRAM); 267 CommandLine command_line(CommandLine::NO_PROGRAM);
267 command_line.AppendSwitch(switches::kProxyAutoDetect); 268 command_line.AppendSwitch(switches::kProxyAutoDetect);
268 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( 269 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
269 new policy::MockConfigurationPolicyProvider()); 270 new policy::MockConfigurationPolicyProvider());
270 Value* mode_value = Value::CreateIntegerValue( 271 Value* mode_value = Value::CreateIntegerValue(
271 policy::kPolicyNoProxyServerMode); 272 policy::kPolicyNoProxyServerMode);
272 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value); 273 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value);
273 274
274 // First verify that the auto-detect is set if there is no managed 275 // First verify that the auto-detect is set if there is no managed
275 // PrefStore. 276 // PrefStore.
276 TestingPrefService prefs(NULL, NULL, &command_line); 277 PrefServiceMockBuilder builder;
277 browser::RegisterUserPrefs(&prefs); 278 builder.WithCommandLine(&command_line);
278 EXPECT_TRUE(prefs.GetBoolean(prefs::kProxyAutoDetect)); 279 scoped_ptr<PrefService> prefs(builder.Create());
279 EXPECT_FALSE(prefs.GetBoolean(prefs::kNoProxyServer)); 280 browser::RegisterUserPrefs(prefs.get());
280 EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyServer)); 281 EXPECT_TRUE(prefs->GetBoolean(prefs::kProxyAutoDetect));
281 EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyPacUrl)); 282 EXPECT_FALSE(prefs->GetBoolean(prefs::kNoProxyServer));
282 EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyBypassList)); 283 EXPECT_EQ(std::string(), prefs->GetString(prefs::kProxyServer));
284 EXPECT_EQ(std::string(), prefs->GetString(prefs::kProxyPacUrl));
285 EXPECT_EQ(std::string(), prefs->GetString(prefs::kProxyBypassList));
283 286
284 // Try a second time time with the managed PrefStore in place, the 287 // Try a second time time with the managed PrefStore in place, the
285 // auto-detect should be overridden. The default pref store must be 288 // auto-detect should be overridden. The default pref store must be
286 // in place with the appropriate default value for this to work. 289 // in place with the appropriate default value for this to work.
287 TestingPrefService prefs2(provider.get(), NULL, &command_line); 290 builder.WithCommandLine(&command_line);
288 browser::RegisterUserPrefs(&prefs2); 291 builder.WithManagedPlatformProvider(provider.get());
289 EXPECT_FALSE(prefs2.GetBoolean(prefs::kProxyAutoDetect)); 292 scoped_ptr<PrefService> prefs2(builder.Create());
290 EXPECT_TRUE(prefs2.GetBoolean(prefs::kNoProxyServer)); 293 browser::RegisterUserPrefs(prefs2.get());
291 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyServer)); 294 EXPECT_FALSE(prefs2->GetBoolean(prefs::kProxyAutoDetect));
292 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyPacUrl)); 295 EXPECT_TRUE(prefs2->GetBoolean(prefs::kNoProxyServer));
293 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyBypassList)); 296 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyServer));
297 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyPacUrl));
298 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyBypassList));
294 } 299 }
295 300
296 class PrefServiceSetValueTest : public testing::Test { 301 class PrefServiceSetValueTest : public testing::Test {
297 protected: 302 protected:
298 static const char kName[]; 303 static const char kName[];
299 static const char kValue[]; 304 static const char kValue[];
300 305
301 PrefServiceSetValueTest() 306 PrefServiceSetValueTest()
302 : null_value_(Value::CreateNullValue()) {} 307 : null_value_(Value::CreateNullValue()) {}
303 308
(...skipping 75 matching lines...)
379 Mock::VerifyAndClearExpectations(&observer_); 384 Mock::VerifyAndClearExpectations(&observer_);
380 385
381 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); 386 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
382 prefs_.Set(kName, new_value); 387 prefs_.Set(kName, new_value);
383 Mock::VerifyAndClearExpectations(&observer_); 388 Mock::VerifyAndClearExpectations(&observer_);
384 389
385 observer_.Expect(&prefs_, kName, null_value_.get()); 390 observer_.Expect(&prefs_, kName, null_value_.get());
386 prefs_.Set(kName, *null_value_); 391 prefs_.Set(kName, *null_value_);
387 Mock::VerifyAndClearExpectations(&observer_); 392 Mock::VerifyAndClearExpectations(&observer_);
388 } 393 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_service_mock_builder.cc ('k') | chrome/browser/prefs/pref_value_map.h » ('j') | no next file with comments »

Powered by Google App Engine