Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

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

Issue 6240013: Make proxy settings one atomic dictionary in the PrefStores such that modifications are atomic. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/out/Debug
Patch Set: Fixed Lint comment Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/prefs/pref_service.cc ('k') | chrome/browser/prefs/pref_set_observer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/pref_change_registrar.h" 15 #include "chrome/browser/prefs/pref_change_registrar.h"
16 #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" 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/proxy_prefs.h" 19 #include "chrome/browser/prefs/proxy_config_dictionary.h"
20 #include "chrome/browser/prefs/testing_pref_store.h" 20 #include "chrome/browser/prefs/testing_pref_store.h"
21 #include "chrome/common/chrome_paths.h" 21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/chrome_switches.h" 22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
24 #include "chrome/test/testing_pref_service.h" 24 #include "chrome/test/testing_pref_service.h"
25 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
27 27
28 using testing::_; 28 using testing::_;
29 using testing::Mock; 29 using testing::Mock;
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 prefs.FindPreference(prefs::kStabilityLaunchCount); 162 prefs.FindPreference(prefs::kStabilityLaunchCount);
163 ASSERT_TRUE(pref); 163 ASSERT_TRUE(pref);
164 const Value* value = pref->GetValue(); 164 const Value* value = pref->GetValue();
165 ASSERT_TRUE(value); 165 ASSERT_TRUE(value);
166 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); 166 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
167 int actual_int_value = -1; 167 int actual_int_value = -1;
168 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); 168 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
169 EXPECT_EQ(kTestValue, actual_int_value); 169 EXPECT_EQ(kTestValue, actual_int_value);
170 } 170 }
171 171
172 void assertProxyMode(const ProxyConfigDictionary& dict,
173 ProxyPrefs::ProxyMode expected_mode) {
174 ProxyPrefs::ProxyMode actual_mode;
175 ASSERT_TRUE(dict.GetMode(&actual_mode));
176 EXPECT_EQ(expected_mode, actual_mode);
177 }
178
179 void assertProxyServer(const ProxyConfigDictionary& dict,
180 const std::string& expected) {
181 std::string actual;
182 if (!expected.empty()) {
183 ASSERT_TRUE(dict.GetProxyServer(&actual));
184 EXPECT_EQ(expected, actual);
185 } else {
186 EXPECT_FALSE(dict.GetProxyServer(&actual));
187 }
188 }
189
190 void assertPacUrl(const ProxyConfigDictionary& dict,
191 const std::string& expected) {
192 std::string actual;
193 if (!expected.empty()) {
194 ASSERT_TRUE(dict.GetPacUrl(&actual));
195 EXPECT_EQ(expected, actual);
196 } else {
197 EXPECT_FALSE(dict.GetPacUrl(&actual));
198 }
199 }
200
201 void assertBypassList(const ProxyConfigDictionary& dict,
202 const std::string& expected) {
203 std::string actual;
204 if (!expected.empty()) {
205 ASSERT_TRUE(dict.GetBypassList(&actual));
206 EXPECT_EQ(expected, actual);
207 } else {
208 EXPECT_FALSE(dict.GetBypassList(&actual));
209 }
210 }
211
212 void assertProxyModeWithoutParams(const ProxyConfigDictionary& dict,
213 ProxyPrefs::ProxyMode proxy_mode) {
214 assertProxyMode(dict, proxy_mode);
215 assertProxyServer(dict, "");
216 assertPacUrl(dict, "");
217 assertBypassList(dict, "");
218 }
219
172 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineOptions) { 220 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineOptions) {
173 CommandLine command_line(CommandLine::NO_PROGRAM); 221 CommandLine command_line(CommandLine::NO_PROGRAM);
174 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123"); 222 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123");
175 command_line.AppendSwitchASCII(switches::kProxyServer, "789"); 223 command_line.AppendSwitchASCII(switches::kProxyServer, "789");
176 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( 224 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
177 new policy::MockConfigurationPolicyProvider()); 225 new policy::MockConfigurationPolicyProvider());
178 Value* mode_name = Value::CreateStringValue( 226 Value* mode_name = Value::CreateStringValue(
179 ProxyPrefs::kFixedServersProxyModeName); 227 ProxyPrefs::kFixedServersProxyModeName);
180 provider->AddPolicy(policy::kPolicyProxyMode, mode_name); 228 provider->AddPolicy(policy::kPolicyProxyMode, mode_name);
181 provider->AddPolicy(policy::kPolicyProxyBypassList, 229 provider->AddPolicy(policy::kPolicyProxyBypassList,
182 Value::CreateStringValue("abc")); 230 Value::CreateStringValue("abc"));
183 provider->AddPolicy(policy::kPolicyProxyServer, 231 provider->AddPolicy(policy::kPolicyProxyServer,
184 Value::CreateStringValue("ghi")); 232 Value::CreateStringValue("ghi"));
185 233
186 // First verify that command-line options are set correctly when 234 // First verify that command-line options are set correctly when
187 // there is no policy in effect. 235 // there is no policy in effect.
188 PrefServiceMockBuilder builder; 236 PrefServiceMockBuilder builder;
189 builder.WithCommandLine(&command_line); 237 builder.WithCommandLine(&command_line);
190 scoped_ptr<PrefService> prefs(builder.Create()); 238 scoped_ptr<PrefService> prefs(builder.Create());
191 browser::RegisterUserPrefs(prefs.get()); 239 browser::RegisterUserPrefs(prefs.get());
192 EXPECT_EQ(ProxyPrefs::MODE_FIXED_SERVERS, 240 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
193 prefs->GetInteger(prefs::kProxyMode)); 241 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS);
194 EXPECT_EQ("789", prefs->GetString(prefs::kProxyServer)); 242 assertProxyServer(dict, "789");
195 EXPECT_EQ(std::string(), prefs->GetString(prefs::kProxyPacUrl)); 243 assertPacUrl(dict, "");
196 EXPECT_EQ("123", prefs->GetString(prefs::kProxyBypassList)); 244 assertBypassList(dict, "123");
197 245
198 // Try a second time time with the managed PrefStore in place, the 246 // Try a second time time with the managed PrefStore in place, the
199 // manual proxy policy should have removed all traces of the command 247 // manual proxy policy should have removed all traces of the command
200 // line and replaced them with the policy versions. 248 // line and replaced them with the policy versions.
201 builder.WithCommandLine(&command_line); 249 builder.WithCommandLine(&command_line);
202 builder.WithManagedPlatformProvider(provider.get()); 250 builder.WithManagedPlatformProvider(provider.get());
203 scoped_ptr<PrefService> prefs2(builder.Create()); 251 scoped_ptr<PrefService> prefs2(builder.Create());
204 browser::RegisterUserPrefs(prefs2.get()); 252 browser::RegisterUserPrefs(prefs2.get());
205 EXPECT_EQ(ProxyPrefs::MODE_FIXED_SERVERS, 253 ProxyConfigDictionary dict2(prefs2->GetDictionary(prefs::kProxy));
206 prefs2->GetInteger(prefs::kProxyMode)); 254 assertProxyMode(dict2, ProxyPrefs::MODE_FIXED_SERVERS);
207 EXPECT_EQ("ghi", prefs2->GetString(prefs::kProxyServer)); 255 assertProxyServer(dict2, "ghi");
208 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyPacUrl)); 256 assertPacUrl(dict2, "");
209 EXPECT_EQ("abc", prefs2->GetString(prefs::kProxyBypassList)); 257 assertBypassList(dict2, "abc");
210 } 258 }
211 259
212 TEST(PrefServiceTest, ProxyPolicyOverridesUnrelatedCommandLineOptions) { 260 TEST(PrefServiceTest, ProxyPolicyOverridesUnrelatedCommandLineOptions) {
213 CommandLine command_line(CommandLine::NO_PROGRAM); 261 CommandLine command_line(CommandLine::NO_PROGRAM);
214 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123"); 262 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123");
215 command_line.AppendSwitchASCII(switches::kProxyServer, "789"); 263 command_line.AppendSwitchASCII(switches::kProxyServer, "789");
216 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( 264 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
217 new policy::MockConfigurationPolicyProvider()); 265 new policy::MockConfigurationPolicyProvider());
218 Value* mode_name = Value::CreateStringValue( 266 Value* mode_name = Value::CreateStringValue(
219 ProxyPrefs::kAutoDetectProxyModeName); 267 ProxyPrefs::kAutoDetectProxyModeName);
220 provider->AddPolicy(policy::kPolicyProxyMode, mode_name); 268 provider->AddPolicy(policy::kPolicyProxyMode, mode_name);
221 269
222 // First verify that command-line options are set correctly when 270 // First verify that command-line options are set correctly when
223 // there is no policy in effect. 271 // there is no policy in effect.
224 PrefServiceMockBuilder builder; 272 PrefServiceMockBuilder builder;
225 builder.WithCommandLine(&command_line); 273 builder.WithCommandLine(&command_line);
226 scoped_ptr<PrefService> prefs(builder.Create()); 274 scoped_ptr<PrefService> prefs(builder.Create());
227 browser::RegisterUserPrefs(prefs.get()); 275 browser::RegisterUserPrefs(prefs.get());
228 EXPECT_EQ(ProxyPrefs::MODE_FIXED_SERVERS, 276 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
229 prefs->GetInteger(prefs::kProxyMode)); 277 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS);
230 EXPECT_EQ("789", prefs->GetString(prefs::kProxyServer)); 278 assertProxyServer(dict, "789");
231 EXPECT_EQ("123", prefs->GetString(prefs::kProxyBypassList)); 279 assertPacUrl(dict, "");
280 assertBypassList(dict, "123");
232 281
233 // Try a second time time with the managed PrefStore in place, the 282 // Try a second time time with the managed PrefStore in place, the
234 // no proxy policy should have removed all traces of the command 283 // no proxy policy should have removed all traces of the command
235 // line proxy settings, even though they were not the specific one 284 // line proxy settings, even though they were not the specific one
236 // set in policy. 285 // set in policy.
237 builder.WithCommandLine(&command_line); 286 builder.WithCommandLine(&command_line);
238 builder.WithManagedPlatformProvider(provider.get()); 287 builder.WithManagedPlatformProvider(provider.get());
239 scoped_ptr<PrefService> prefs2(builder.Create()); 288 scoped_ptr<PrefService> prefs2(builder.Create());
240 browser::RegisterUserPrefs(prefs2.get()); 289 browser::RegisterUserPrefs(prefs2.get());
241 EXPECT_EQ(ProxyPrefs::MODE_AUTO_DETECT, 290 ProxyConfigDictionary dict2(prefs2->GetDictionary(prefs::kProxy));
242 prefs2->GetInteger(prefs::kProxyMode)); 291 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
243 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyServer));
244 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyPacUrl));
245 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyBypassList));
246 } 292 }
247 293
248 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineNoProxy) { 294 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineNoProxy) {
249 CommandLine command_line(CommandLine::NO_PROGRAM); 295 CommandLine command_line(CommandLine::NO_PROGRAM);
250 command_line.AppendSwitch(switches::kNoProxyServer); 296 command_line.AppendSwitch(switches::kNoProxyServer);
251 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( 297 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
252 new policy::MockConfigurationPolicyProvider()); 298 new policy::MockConfigurationPolicyProvider());
253 Value* mode_name = Value::CreateStringValue( 299 Value* mode_name = Value::CreateStringValue(
254 ProxyPrefs::kAutoDetectProxyModeName); 300 ProxyPrefs::kAutoDetectProxyModeName);
255 provider->AddPolicy(policy::kPolicyProxyMode, mode_name); 301 provider->AddPolicy(policy::kPolicyProxyMode, mode_name);
256 302
257 // First verify that command-line options are set correctly when 303 // First verify that command-line options are set correctly when
258 // there is no policy in effect. 304 // there is no policy in effect.
259 PrefServiceMockBuilder builder; 305 PrefServiceMockBuilder builder;
260 builder.WithCommandLine(&command_line); 306 builder.WithCommandLine(&command_line);
261 scoped_ptr<PrefService> prefs(builder.Create()); 307 scoped_ptr<PrefService> prefs(builder.Create());
262 browser::RegisterUserPrefs(prefs.get()); 308 browser::RegisterUserPrefs(prefs.get());
263 EXPECT_EQ(ProxyPrefs::MODE_DIRECT, prefs->GetInteger(prefs::kProxyMode)); 309 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
264 EXPECT_EQ(std::string(), prefs->GetString(prefs::kProxyServer)); 310 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_DIRECT);
265 EXPECT_EQ(std::string(), prefs->GetString(prefs::kProxyPacUrl));
266 EXPECT_EQ(std::string(), prefs->GetString(prefs::kProxyBypassList));
267 311
268 // Try a second time time with the managed PrefStore in place, the 312 // Try a second time time with the managed PrefStore in place, the
269 // auto-detect should be overridden. The default pref store must be 313 // auto-detect should be overridden. The default pref store must be
270 // in place with the appropriate default value for this to work. 314 // in place with the appropriate default value for this to work.
271 builder.WithCommandLine(&command_line); 315 builder.WithCommandLine(&command_line);
272 builder.WithManagedPlatformProvider(provider.get()); 316 builder.WithManagedPlatformProvider(provider.get());
273 scoped_ptr<PrefService> prefs2(builder.Create()); 317 scoped_ptr<PrefService> prefs2(builder.Create());
274 browser::RegisterUserPrefs(prefs2.get()); 318 browser::RegisterUserPrefs(prefs2.get());
275 EXPECT_EQ(ProxyPrefs::MODE_AUTO_DETECT, 319 ProxyConfigDictionary dict2(prefs2->GetDictionary(prefs::kProxy));
276 prefs2->GetInteger(prefs::kProxyMode)); 320 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
277 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyServer));
278 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyPacUrl));
279 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyBypassList));
280 } 321 }
281 322
282 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineAutoDetect) { 323 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineAutoDetect) {
283 CommandLine command_line(CommandLine::NO_PROGRAM); 324 CommandLine command_line(CommandLine::NO_PROGRAM);
284 command_line.AppendSwitch(switches::kProxyAutoDetect); 325 command_line.AppendSwitch(switches::kProxyAutoDetect);
285 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( 326 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
286 new policy::MockConfigurationPolicyProvider()); 327 new policy::MockConfigurationPolicyProvider());
287 Value* mode_name = Value::CreateStringValue( 328 Value* mode_name = Value::CreateStringValue(
288 ProxyPrefs::kDirectProxyModeName); 329 ProxyPrefs::kDirectProxyModeName);
289 provider->AddPolicy(policy::kPolicyProxyMode, mode_name); 330 provider->AddPolicy(policy::kPolicyProxyMode, mode_name);
290 331
291 // First verify that the auto-detect is set if there is no managed 332 // First verify that the auto-detect is set if there is no managed
292 // PrefStore. 333 // PrefStore.
293 PrefServiceMockBuilder builder; 334 PrefServiceMockBuilder builder;
294 builder.WithCommandLine(&command_line); 335 builder.WithCommandLine(&command_line);
295 scoped_ptr<PrefService> prefs(builder.Create()); 336 scoped_ptr<PrefService> prefs(builder.Create());
296 browser::RegisterUserPrefs(prefs.get()); 337 browser::RegisterUserPrefs(prefs.get());
297 EXPECT_EQ(ProxyPrefs::MODE_AUTO_DETECT, prefs->GetInteger(prefs::kProxyMode)); 338 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
298 EXPECT_EQ(std::string(), prefs->GetString(prefs::kProxyServer)); 339 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_AUTO_DETECT);
299 EXPECT_EQ(std::string(), prefs->GetString(prefs::kProxyPacUrl));
300 EXPECT_EQ(std::string(), prefs->GetString(prefs::kProxyBypassList));
301 340
302 // Try a second time time with the managed PrefStore in place, the 341 // Try a second time time with the managed PrefStore in place, the
303 // auto-detect should be overridden. The default pref store must be 342 // auto-detect should be overridden. The default pref store must be
304 // in place with the appropriate default value for this to work. 343 // in place with the appropriate default value for this to work.
305 builder.WithCommandLine(&command_line); 344 builder.WithCommandLine(&command_line);
306 builder.WithManagedPlatformProvider(provider.get()); 345 builder.WithManagedPlatformProvider(provider.get());
307 scoped_ptr<PrefService> prefs2(builder.Create()); 346 scoped_ptr<PrefService> prefs2(builder.Create());
308 browser::RegisterUserPrefs(prefs2.get()); 347 browser::RegisterUserPrefs(prefs2.get());
309 EXPECT_EQ(ProxyPrefs::MODE_DIRECT, prefs2->GetInteger(prefs::kProxyMode)); 348 ProxyConfigDictionary dict2(prefs2->GetDictionary(prefs::kProxy));
310 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyServer)); 349 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_DIRECT);
311 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyPacUrl));
312 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyBypassList));
313 } 350 }
314 351
315 class PrefServiceSetValueTest : public testing::Test { 352 class PrefServiceSetValueTest : public testing::Test {
316 protected: 353 protected:
317 static const char kName[]; 354 static const char kName[];
318 static const char kValue[]; 355 static const char kValue[];
319 356
320 TestingPrefService prefs_; 357 TestingPrefService prefs_;
321 PrefObserverMock observer_; 358 PrefObserverMock observer_;
322 }; 359 };
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 429
393 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); 430 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
394 prefs_.Set(kName, new_value); 431 prefs_.Set(kName, new_value);
395 Mock::VerifyAndClearExpectations(&observer_); 432 Mock::VerifyAndClearExpectations(&observer_);
396 433
397 ListValue empty; 434 ListValue empty;
398 observer_.Expect(&prefs_, kName, &empty); 435 observer_.Expect(&prefs_, kName, &empty);
399 prefs_.Set(kName, empty); 436 prefs_.Set(kName, empty);
400 Mock::VerifyAndClearExpectations(&observer_); 437 Mock::VerifyAndClearExpectations(&observer_);
401 } 438 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_service.cc ('k') | chrome/browser/prefs/pref_set_observer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698