Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / prefs / proxy_policy_unittest.cc
blobb7a5213f4b17c28ddb4529553eb3b073f76f4a3b
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() : command_line_(base::CommandLine::NO_PROGRAM) {}
86 void SetUp() override {
87 EXPECT_CALL(provider_, IsInitializationComplete(_))
88 .WillRepeatedly(Return(true));
90 PolicyServiceImpl::Providers providers;
91 providers.push_back(&provider_);
92 policy_service_.reset(new PolicyServiceImpl(providers));
93 provider_.Init();
96 void TearDown() override { provider_.Shutdown(); }
98 scoped_ptr<PrefService> CreatePrefService(bool with_managed_policies) {
99 PrefServiceMockFactory factory;
100 factory.SetCommandLine(&command_line_);
101 if (with_managed_policies)
102 factory.SetManagedPolicies(policy_service_.get());
103 scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
104 new user_prefs::PrefRegistrySyncable);
105 scoped_ptr<PrefServiceSyncable> prefs =
106 factory.CreateSyncable(registry.get());
107 chrome::RegisterUserProfilePrefs(registry.get());
108 return prefs.Pass();
111 base::MessageLoop loop_;
112 base::CommandLine command_line_;
113 MockConfigurationPolicyProvider provider_;
114 scoped_ptr<PolicyServiceImpl> policy_service_;
117 TEST_F(ProxyPolicyTest, OverridesCommandLineOptions) {
118 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123");
119 command_line_.AppendSwitchASCII(switches::kProxyServer, "789");
120 base::Value* mode_name =
121 new base::StringValue(ProxyPrefs::kFixedServersProxyModeName);
122 PolicyMap policy;
123 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
124 mode_name, NULL);
125 policy.Set(key::kProxyBypassList,
126 POLICY_LEVEL_MANDATORY,
127 POLICY_SCOPE_USER,
128 new base::StringValue("abc"),
129 NULL);
130 policy.Set(key::kProxyServer,
131 POLICY_LEVEL_MANDATORY,
132 POLICY_SCOPE_USER,
133 new base::StringValue("ghi"),
134 NULL);
135 provider_.UpdateChromePolicy(policy);
137 // First verify that command-line options are set correctly when
138 // there is no policy in effect.
139 scoped_ptr<PrefService> prefs(CreatePrefService(false));
140 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
141 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS);
142 assertProxyServer(dict, "789");
143 assertPacUrl(dict, std::string());
144 assertBypassList(dict, "123");
146 // Try a second time time with the managed PrefStore in place, the
147 // manual proxy policy should have removed all traces of the command
148 // line and replaced them with the policy versions.
149 prefs = CreatePrefService(true);
150 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy));
151 assertProxyMode(dict2, ProxyPrefs::MODE_FIXED_SERVERS);
152 assertProxyServer(dict2, "ghi");
153 assertPacUrl(dict2, std::string());
154 assertBypassList(dict2, "abc");
157 TEST_F(ProxyPolicyTest, OverridesUnrelatedCommandLineOptions) {
158 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123");
159 command_line_.AppendSwitchASCII(switches::kProxyServer, "789");
160 base::Value* mode_name =
161 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName);
162 PolicyMap policy;
163 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
164 mode_name, NULL);
165 provider_.UpdateChromePolicy(policy);
167 // First verify that command-line options are set correctly when
168 // there is no policy in effect.
169 scoped_ptr<PrefService> prefs = CreatePrefService(false);
170 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
171 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS);
172 assertProxyServer(dict, "789");
173 assertPacUrl(dict, std::string());
174 assertBypassList(dict, "123");
176 // Try a second time time with the managed PrefStore in place, the
177 // no proxy policy should have removed all traces of the command
178 // line proxy settings, even though they were not the specific one
179 // set in policy.
180 prefs = CreatePrefService(true);
181 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy));
182 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
185 TEST_F(ProxyPolicyTest, OverridesCommandLineNoProxy) {
186 command_line_.AppendSwitch(switches::kNoProxyServer);
187 base::Value* mode_name =
188 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName);
189 PolicyMap policy;
190 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
191 mode_name, NULL);
192 provider_.UpdateChromePolicy(policy);
194 // First verify that command-line options are set correctly when
195 // there is no policy in effect.
196 scoped_ptr<PrefService> prefs = CreatePrefService(false);
197 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
198 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_DIRECT);
200 // Try a second time time with the managed PrefStore in place, the
201 // auto-detect should be overridden. The default pref store must be
202 // in place with the appropriate default value for this to work.
203 prefs = CreatePrefService(true);
204 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy));
205 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
208 TEST_F(ProxyPolicyTest, OverridesCommandLineAutoDetect) {
209 command_line_.AppendSwitch(switches::kProxyAutoDetect);
210 base::Value* mode_name =
211 new base::StringValue(ProxyPrefs::kDirectProxyModeName);
212 PolicyMap policy;
213 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
214 mode_name, NULL);
215 provider_.UpdateChromePolicy(policy);
217 // First verify that the auto-detect is set if there is no managed
218 // PrefStore.
219 scoped_ptr<PrefService> prefs = CreatePrefService(false);
220 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
221 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_AUTO_DETECT);
223 // Try a second time time with the managed PrefStore in place, the
224 // auto-detect should be overridden. The default pref store must be
225 // in place with the appropriate default value for this to work.
226 prefs = CreatePrefService(true);
227 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy));
228 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_DIRECT);
231 } // namespace policy