Add GCMChannelStatusSyncer to schedule requests and enable/disable GCM
[chromium-blink-merge.git] / chrome / browser / prefs / proxy_policy_unittest.cc
blob17bb21bcd92a44f282253c2d8245235b71aa4b90
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/callback.h"
7 #include "base/command_line.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/prefs/browser_prefs.h"
11 #include "chrome/browser/prefs/pref_service_mock_factory.h"
12 #include "chrome/browser/prefs/pref_service_syncable.h"
13 #include "chrome/browser/prefs/proxy_config_dictionary.h"
14 #include "chrome/browser/prefs/proxy_prefs.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/policy/core/common/external_data_fetcher.h"
18 #include "components/policy/core/common/mock_configuration_policy_provider.h"
19 #include "components/policy/core/common/policy_map.h"
20 #include "components/policy/core/common/policy_service_impl.h"
21 #include "components/pref_registry/pref_registry_syncable.h"
22 #include "policy/policy_constants.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 using ::testing::Return;
26 using ::testing::_;
28 namespace policy {
30 namespace {
32 void assertProxyMode(const ProxyConfigDictionary& dict,
33 ProxyPrefs::ProxyMode expected_mode) {
34 ProxyPrefs::ProxyMode actual_mode;
35 ASSERT_TRUE(dict.GetMode(&actual_mode));
36 EXPECT_EQ(expected_mode, actual_mode);
39 void assertProxyServer(const ProxyConfigDictionary& dict,
40 const std::string& expected) {
41 std::string actual;
42 if (!expected.empty()) {
43 ASSERT_TRUE(dict.GetProxyServer(&actual));
44 EXPECT_EQ(expected, actual);
45 } else {
46 EXPECT_FALSE(dict.GetProxyServer(&actual));
50 void assertPacUrl(const ProxyConfigDictionary& dict,
51 const std::string& expected) {
52 std::string actual;
53 if (!expected.empty()) {
54 ASSERT_TRUE(dict.GetPacUrl(&actual));
55 EXPECT_EQ(expected, actual);
56 } else {
57 EXPECT_FALSE(dict.GetPacUrl(&actual));
61 void assertBypassList(const ProxyConfigDictionary& dict,
62 const std::string& expected) {
63 std::string actual;
64 if (!expected.empty()) {
65 ASSERT_TRUE(dict.GetBypassList(&actual));
66 EXPECT_EQ(expected, actual);
67 } else {
68 EXPECT_FALSE(dict.GetBypassList(&actual));
72 void assertProxyModeWithoutParams(const ProxyConfigDictionary& dict,
73 ProxyPrefs::ProxyMode proxy_mode) {
74 assertProxyMode(dict, proxy_mode);
75 assertProxyServer(dict, std::string());
76 assertPacUrl(dict, std::string());
77 assertBypassList(dict, std::string());
80 } // namespace
82 class ProxyPolicyTest : public testing::Test {
83 protected:
84 ProxyPolicyTest()
85 : command_line_(CommandLine::NO_PROGRAM) {}
87 virtual void SetUp() OVERRIDE {
88 EXPECT_CALL(provider_, IsInitializationComplete(_))
89 .WillRepeatedly(Return(true));
91 PolicyServiceImpl::Providers providers;
92 providers.push_back(&provider_);
93 policy_service_.reset(new PolicyServiceImpl(providers));
94 provider_.Init();
97 virtual void TearDown() OVERRIDE {
98 provider_.Shutdown();
101 scoped_ptr<PrefService> CreatePrefService(bool with_managed_policies) {
102 PrefServiceMockFactory factory;
103 factory.SetCommandLine(&command_line_);
104 if (with_managed_policies)
105 factory.SetManagedPolicies(policy_service_.get());
106 scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
107 new user_prefs::PrefRegistrySyncable);
108 scoped_ptr<PrefServiceSyncable> prefs =
109 factory.CreateSyncable(registry.get());
110 chrome::RegisterUserProfilePrefs(registry.get());
111 return prefs.PassAs<PrefService>();
114 base::MessageLoop loop_;
115 CommandLine command_line_;
116 MockConfigurationPolicyProvider provider_;
117 scoped_ptr<PolicyServiceImpl> policy_service_;
120 TEST_F(ProxyPolicyTest, OverridesCommandLineOptions) {
121 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123");
122 command_line_.AppendSwitchASCII(switches::kProxyServer, "789");
123 base::Value* mode_name =
124 new base::StringValue(ProxyPrefs::kFixedServersProxyModeName);
125 PolicyMap policy;
126 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
127 mode_name, NULL);
128 policy.Set(key::kProxyBypassList,
129 POLICY_LEVEL_MANDATORY,
130 POLICY_SCOPE_USER,
131 new base::StringValue("abc"),
132 NULL);
133 policy.Set(key::kProxyServer,
134 POLICY_LEVEL_MANDATORY,
135 POLICY_SCOPE_USER,
136 new base::StringValue("ghi"),
137 NULL);
138 provider_.UpdateChromePolicy(policy);
140 // First verify that command-line options are set correctly when
141 // there is no policy in effect.
142 scoped_ptr<PrefService> prefs(CreatePrefService(false));
143 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
144 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS);
145 assertProxyServer(dict, "789");
146 assertPacUrl(dict, std::string());
147 assertBypassList(dict, "123");
149 // Try a second time time with the managed PrefStore in place, the
150 // manual proxy policy should have removed all traces of the command
151 // line and replaced them with the policy versions.
152 prefs = CreatePrefService(true);
153 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy));
154 assertProxyMode(dict2, ProxyPrefs::MODE_FIXED_SERVERS);
155 assertProxyServer(dict2, "ghi");
156 assertPacUrl(dict2, std::string());
157 assertBypassList(dict2, "abc");
160 TEST_F(ProxyPolicyTest, OverridesUnrelatedCommandLineOptions) {
161 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123");
162 command_line_.AppendSwitchASCII(switches::kProxyServer, "789");
163 base::Value* mode_name =
164 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName);
165 PolicyMap policy;
166 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
167 mode_name, NULL);
168 provider_.UpdateChromePolicy(policy);
170 // First verify that command-line options are set correctly when
171 // there is no policy in effect.
172 scoped_ptr<PrefService> prefs = CreatePrefService(false);
173 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
174 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS);
175 assertProxyServer(dict, "789");
176 assertPacUrl(dict, std::string());
177 assertBypassList(dict, "123");
179 // Try a second time time with the managed PrefStore in place, the
180 // no proxy policy should have removed all traces of the command
181 // line proxy settings, even though they were not the specific one
182 // set in policy.
183 prefs = CreatePrefService(true);
184 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy));
185 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
188 TEST_F(ProxyPolicyTest, OverridesCommandLineNoProxy) {
189 command_line_.AppendSwitch(switches::kNoProxyServer);
190 base::Value* mode_name =
191 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName);
192 PolicyMap policy;
193 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
194 mode_name, NULL);
195 provider_.UpdateChromePolicy(policy);
197 // First verify that command-line options are set correctly when
198 // there is no policy in effect.
199 scoped_ptr<PrefService> prefs = CreatePrefService(false);
200 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
201 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_DIRECT);
203 // Try a second time time with the managed PrefStore in place, the
204 // auto-detect should be overridden. The default pref store must be
205 // in place with the appropriate default value for this to work.
206 prefs = CreatePrefService(true);
207 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy));
208 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
211 TEST_F(ProxyPolicyTest, OverridesCommandLineAutoDetect) {
212 command_line_.AppendSwitch(switches::kProxyAutoDetect);
213 base::Value* mode_name =
214 new base::StringValue(ProxyPrefs::kDirectProxyModeName);
215 PolicyMap policy;
216 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
217 mode_name, NULL);
218 provider_.UpdateChromePolicy(policy);
220 // First verify that the auto-detect is set if there is no managed
221 // PrefStore.
222 scoped_ptr<PrefService> prefs = CreatePrefService(false);
223 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
224 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_AUTO_DETECT);
226 // Try a second time time with the managed PrefStore in place, the
227 // auto-detect should be overridden. The default pref store must be
228 // in place with the appropriate default value for this to work.
229 prefs = CreatePrefService(true);
230 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy));
231 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_DIRECT);
234 } // namespace policy