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

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

Issue 7096013: Allow device policy code to be optionally included. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Make changes requested by torne. Created 9 years, 6 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
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/memory/scoped_ptr.h" 9 #include "base/memory/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_config_dictionary.h"
20 #include "chrome/browser/prefs/testing_pref_store.h" 19 #include "chrome/browser/prefs/testing_pref_store.h"
21 #include "chrome/common/chrome_paths.h" 20 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/pref_names.h" 22 #include "chrome/common/pref_names.h"
24 #include "chrome/test/testing_pref_service.h" 23 #include "chrome/test/testing_pref_service.h"
25 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
27 26
28 using testing::_; 27 using testing::_;
29 using testing::Mock; 28 using testing::Mock;
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 prefs.FindPreference(prefs::kStabilityLaunchCount); 161 prefs.FindPreference(prefs::kStabilityLaunchCount);
163 ASSERT_TRUE(pref); 162 ASSERT_TRUE(pref);
164 const Value* value = pref->GetValue(); 163 const Value* value = pref->GetValue();
165 ASSERT_TRUE(value); 164 ASSERT_TRUE(value);
166 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); 165 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
167 int actual_int_value = -1; 166 int actual_int_value = -1;
168 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); 167 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
169 EXPECT_EQ(kTestValue, actual_int_value); 168 EXPECT_EQ(kTestValue, actual_int_value);
170 } 169 }
171 170
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
220 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineOptions) {
221 CommandLine command_line(CommandLine::NO_PROGRAM);
222 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123");
223 command_line.AppendSwitchASCII(switches::kProxyServer, "789");
224 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
225 new policy::MockConfigurationPolicyProvider());
226 Value* mode_name = Value::CreateStringValue(
227 ProxyPrefs::kFixedServersProxyModeName);
228 provider->AddPolicy(policy::kPolicyProxyMode, mode_name);
229 provider->AddPolicy(policy::kPolicyProxyBypassList,
230 Value::CreateStringValue("abc"));
231 provider->AddPolicy(policy::kPolicyProxyServer,
232 Value::CreateStringValue("ghi"));
233
234 // First verify that command-line options are set correctly when
235 // there is no policy in effect.
236 PrefServiceMockBuilder builder;
237 builder.WithCommandLine(&command_line);
238 scoped_ptr<PrefService> prefs(builder.Create());
239 browser::RegisterUserPrefs(prefs.get());
240 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
241 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS);
242 assertProxyServer(dict, "789");
243 assertPacUrl(dict, "");
244 assertBypassList(dict, "123");
245
246 // Try a second time time with the managed PrefStore in place, the
247 // manual proxy policy should have removed all traces of the command
248 // line and replaced them with the policy versions.
249 builder.WithCommandLine(&command_line);
250 builder.WithManagedPlatformProvider(provider.get());
251 scoped_ptr<PrefService> prefs2(builder.Create());
252 browser::RegisterUserPrefs(prefs2.get());
253 ProxyConfigDictionary dict2(prefs2->GetDictionary(prefs::kProxy));
254 assertProxyMode(dict2, ProxyPrefs::MODE_FIXED_SERVERS);
255 assertProxyServer(dict2, "ghi");
256 assertPacUrl(dict2, "");
257 assertBypassList(dict2, "abc");
258 }
259
260 TEST(PrefServiceTest, ProxyPolicyOverridesUnrelatedCommandLineOptions) {
261 CommandLine command_line(CommandLine::NO_PROGRAM);
262 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123");
263 command_line.AppendSwitchASCII(switches::kProxyServer, "789");
264 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
265 new policy::MockConfigurationPolicyProvider());
266 Value* mode_name = Value::CreateStringValue(
267 ProxyPrefs::kAutoDetectProxyModeName);
268 provider->AddPolicy(policy::kPolicyProxyMode, mode_name);
269
270 // First verify that command-line options are set correctly when
271 // there is no policy in effect.
272 PrefServiceMockBuilder builder;
273 builder.WithCommandLine(&command_line);
274 scoped_ptr<PrefService> prefs(builder.Create());
275 browser::RegisterUserPrefs(prefs.get());
276 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
277 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS);
278 assertProxyServer(dict, "789");
279 assertPacUrl(dict, "");
280 assertBypassList(dict, "123");
281
282 // Try a second time time with the managed PrefStore in place, the
283 // no proxy policy should have removed all traces of the command
284 // line proxy settings, even though they were not the specific one
285 // set in policy.
286 builder.WithCommandLine(&command_line);
287 builder.WithManagedPlatformProvider(provider.get());
288 scoped_ptr<PrefService> prefs2(builder.Create());
289 browser::RegisterUserPrefs(prefs2.get());
290 ProxyConfigDictionary dict2(prefs2->GetDictionary(prefs::kProxy));
291 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
292 }
293
294 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineNoProxy) {
295 CommandLine command_line(CommandLine::NO_PROGRAM);
296 command_line.AppendSwitch(switches::kNoProxyServer);
297 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
298 new policy::MockConfigurationPolicyProvider());
299 Value* mode_name = Value::CreateStringValue(
300 ProxyPrefs::kAutoDetectProxyModeName);
301 provider->AddPolicy(policy::kPolicyProxyMode, mode_name);
302
303 // First verify that command-line options are set correctly when
304 // there is no policy in effect.
305 PrefServiceMockBuilder builder;
306 builder.WithCommandLine(&command_line);
307 scoped_ptr<PrefService> prefs(builder.Create());
308 browser::RegisterUserPrefs(prefs.get());
309 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
310 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_DIRECT);
311
312 // Try a second time time with the managed PrefStore in place, the
313 // auto-detect should be overridden. The default pref store must be
314 // in place with the appropriate default value for this to work.
315 builder.WithCommandLine(&command_line);
316 builder.WithManagedPlatformProvider(provider.get());
317 scoped_ptr<PrefService> prefs2(builder.Create());
318 browser::RegisterUserPrefs(prefs2.get());
319 ProxyConfigDictionary dict2(prefs2->GetDictionary(prefs::kProxy));
320 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
321 }
322
323 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineAutoDetect) {
324 CommandLine command_line(CommandLine::NO_PROGRAM);
325 command_line.AppendSwitch(switches::kProxyAutoDetect);
326 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
327 new policy::MockConfigurationPolicyProvider());
328 Value* mode_name = Value::CreateStringValue(
329 ProxyPrefs::kDirectProxyModeName);
330 provider->AddPolicy(policy::kPolicyProxyMode, mode_name);
331
332 // First verify that the auto-detect is set if there is no managed
333 // PrefStore.
334 PrefServiceMockBuilder builder;
335 builder.WithCommandLine(&command_line);
336 scoped_ptr<PrefService> prefs(builder.Create());
337 browser::RegisterUserPrefs(prefs.get());
338 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
339 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_AUTO_DETECT);
340
341 // Try a second time time with the managed PrefStore in place, the
342 // auto-detect should be overridden. The default pref store must be
343 // in place with the appropriate default value for this to work.
344 builder.WithCommandLine(&command_line);
345 builder.WithManagedPlatformProvider(provider.get());
346 scoped_ptr<PrefService> prefs2(builder.Create());
347 browser::RegisterUserPrefs(prefs2.get());
348 ProxyConfigDictionary dict2(prefs2->GetDictionary(prefs::kProxy));
349 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_DIRECT);
350 }
351
352 class PrefServiceSetValueTest : public testing::Test { 171 class PrefServiceSetValueTest : public testing::Test {
353 protected: 172 protected:
354 static const char kName[]; 173 static const char kName[];
355 static const char kValue[]; 174 static const char kValue[];
356 175
357 TestingPrefService prefs_; 176 TestingPrefService prefs_;
358 PrefObserverMock observer_; 177 PrefObserverMock observer_;
359 }; 178 };
360 179
361 const char PrefServiceSetValueTest::kName[] = "name"; 180 const char PrefServiceSetValueTest::kName[] = "name";
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 248
430 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); 249 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
431 prefs_.Set(kName, new_value); 250 prefs_.Set(kName, new_value);
432 Mock::VerifyAndClearExpectations(&observer_); 251 Mock::VerifyAndClearExpectations(&observer_);
433 252
434 ListValue empty; 253 ListValue empty;
435 observer_.Expect(&prefs_, kName, &empty); 254 observer_.Expect(&prefs_, kName, &empty);
436 prefs_.Set(kName, empty); 255 prefs_.Set(kName, empty);
437 Mock::VerifyAndClearExpectations(&observer_); 256 Mock::VerifyAndClearExpectations(&observer_);
438 } 257 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_service_mock_builder.cc ('k') | chrome/browser/prefs/proxy_policy_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698