Audio indicator in each tab
[chromium-blink-merge.git] / chrome_frame / test / policy_settings_unittest.cc
blobe13af500faed4ef2963ddf4376fbc1a68911bd16
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/basictypes.h"
6 #include "base/at_exit.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/stringprintf.h"
10 #include "base/utf_string_conversions.h"
11 #include "base/win/registry.h"
12 #include "chrome_frame/policy_settings.h"
13 #include "chrome_frame/test/chrome_frame_test_utils.h"
14 #include "content/public/common/content_switches.h"
15 #include "policy/policy_constants.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using base::win::RegKey;
19 using chrome_frame_test::ScopedVirtualizeHklmAndHkcu;
21 namespace {
23 // A best effort way to zap CF policy entries that may be in the registry.
24 void DeleteChromeFramePolicyEntries(HKEY root) {
25 RegKey key;
26 if (key.Open(root, policy::kRegistryMandatorySubKey,
27 KEY_ALL_ACCESS) == ERROR_SUCCESS) {
28 key.DeleteValue(
29 ASCIIToWide(policy::key::kChromeFrameRendererSettings).c_str());
30 key.DeleteKey(ASCIIToWide(policy::key::kRenderInChromeFrameList).c_str());
31 key.DeleteKey(ASCIIToWide(policy::key::kRenderInHostList).c_str());
32 key.DeleteKey(ASCIIToWide(policy::key::kChromeFrameContentTypes).c_str());
33 key.DeleteKey(ASCIIToWide(policy::key::kApplicationLocaleValue).c_str());
37 bool InitializePolicyKey(HKEY policy_root, RegKey* policy_key) {
38 EXPECT_EQ(ERROR_SUCCESS, policy_key->Create(policy_root,
39 policy::kRegistryMandatorySubKey, KEY_ALL_ACCESS));
40 return policy_key->Valid();
43 void WritePolicyList(RegKey* policy_key,
44 const wchar_t* list_name,
45 const wchar_t* values[], int count) {
46 DCHECK(policy_key);
47 // Remove any previous settings
48 policy_key->DeleteKey(list_name);
50 RegKey list_key;
51 EXPECT_EQ(ERROR_SUCCESS, list_key.Create(policy_key->Handle(), list_name,
52 KEY_ALL_ACCESS));
53 for (int i = 0; i < count; ++i) {
54 EXPECT_EQ(ERROR_SUCCESS,
55 list_key.WriteValue(base::StringPrintf(L"%i", i).c_str(), values[i]));
59 bool SetRendererSettings(HKEY policy_root,
60 PolicySettings::RendererForUrl renderer,
61 const wchar_t* exclusions[],
62 int exclusion_count) {
63 RegKey policy_key;
64 if (!InitializePolicyKey(policy_root, &policy_key))
65 return false;
67 policy_key.WriteValue(
68 ASCIIToWide(policy::key::kChromeFrameRendererSettings).c_str(),
69 static_cast<DWORD>(renderer));
71 std::wstring in_cf(ASCIIToWide(policy::key::kRenderInChromeFrameList));
72 std::wstring in_host(ASCIIToWide(policy::key::kRenderInHostList));
73 std::wstring exclusion_list(
74 renderer == PolicySettings::RENDER_IN_CHROME_FRAME ? in_host : in_cf);
75 WritePolicyList(&policy_key, exclusion_list.c_str(), exclusions,
76 exclusion_count);
78 return true;
81 bool SetCFContentTypes(HKEY policy_root,
82 const wchar_t* content_types[],
83 int count) {
84 RegKey policy_key;
85 if (!InitializePolicyKey(policy_root, &policy_key))
86 return false;
88 std::wstring type_list(ASCIIToWide(policy::key::kChromeFrameContentTypes));
89 WritePolicyList(&policy_key, type_list.c_str(), content_types, count);
91 return true;
94 bool SetCFPolicyString(HKEY policy_root,
95 const char* policy_name,
96 const wchar_t* value) {
97 RegKey policy_key;
98 if (!InitializePolicyKey(policy_root, &policy_key))
99 return false;
101 std::wstring policy_name_str(ASCIIToWide(policy_name));
102 EXPECT_EQ(ERROR_SUCCESS,
103 policy_key.WriteValue(policy_name_str.c_str(), value));
104 return true;
107 } // end namespace
109 class PolicySettingsTest : public testing::Test {
110 protected:
111 void SetUp() {
112 ResetPolicySettings();
115 void TearDown() {
118 void ResetPolicySettings() {
119 //at_exit_manager_.ProcessCallbacksNow();
120 DeleteAllSingletons();
123 // This is used to manage life cycle of PolicySettings singleton.
124 // base::ShadowingAtExitManager at_exit_manager_;
126 ScopedVirtualizeHklmAndHkcu registry_virtualization_;
129 TEST_F(PolicySettingsTest, RendererForUrl) {
130 const wchar_t* kTestUrls[] = {
131 L"http://www.example.com",
132 L"http://www.pattern.com",
133 L"http://www.test.com"
135 const wchar_t* kTestFilters[] = {
136 L"*.example.com",
137 L"*.pattern.com",
138 L"*.test.com"
140 const wchar_t kNoMatchUrl[] = L"http://www.chromium.org";
142 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED,
143 PolicySettings::GetInstance()->default_renderer());
144 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED,
145 PolicySettings::GetInstance()->GetRendererForUrl(kNoMatchUrl));
147 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER };
148 for (int i = 0; i < arraysize(root); ++i) {
149 EXPECT_TRUE(SetRendererSettings(root[i],
150 PolicySettings::RENDER_IN_CHROME_FRAME, kTestFilters,
151 arraysize(kTestFilters)));
153 ResetPolicySettings();
154 EXPECT_EQ(PolicySettings::RENDER_IN_CHROME_FRAME,
155 PolicySettings::GetInstance()->GetRendererForUrl(kNoMatchUrl));
156 for (int j = 0; j < arraysize(kTestUrls); ++j) {
157 EXPECT_EQ(PolicySettings::RENDER_IN_HOST,
158 PolicySettings::GetInstance()->GetRendererForUrl(kTestUrls[j]));
161 EXPECT_TRUE(SetRendererSettings(root[i],
162 PolicySettings::RENDER_IN_HOST, NULL, 0));
164 ResetPolicySettings();
165 EXPECT_EQ(PolicySettings::RENDER_IN_HOST,
166 PolicySettings::GetInstance()->GetRendererForUrl(kNoMatchUrl));
167 for (int j = 0; j < arraysize(kTestUrls); ++j) {
168 EXPECT_EQ(PolicySettings::RENDER_IN_HOST,
169 PolicySettings::GetInstance()->GetRendererForUrl(kTestUrls[j]));
172 DeleteChromeFramePolicyEntries(root[i]);
176 TEST_F(PolicySettingsTest, RendererForContentType) {
177 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED,
178 PolicySettings::GetInstance()->GetRendererForContentType(
179 L"text/xml"));
181 const wchar_t* kTestPolicyContentTypes[] = {
182 L"application/xml",
183 L"text/xml",
184 L"application/pdf",
187 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER };
188 for (int i = 0; i < arraysize(root); ++i) {
189 SetCFContentTypes(root[i], kTestPolicyContentTypes,
190 arraysize(kTestPolicyContentTypes));
191 ResetPolicySettings();
192 for (int type = 0; type < arraysize(kTestPolicyContentTypes); ++type) {
193 EXPECT_EQ(PolicySettings::RENDER_IN_CHROME_FRAME,
194 PolicySettings::GetInstance()->GetRendererForContentType(
195 kTestPolicyContentTypes[type]));
198 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED,
199 PolicySettings::GetInstance()->GetRendererForContentType(
200 L"text/html"));
202 DeleteChromeFramePolicyEntries(root[i]);
206 TEST_F(PolicySettingsTest, ApplicationLocale) {
207 EXPECT_TRUE(PolicySettings::GetInstance()->ApplicationLocale().empty());
209 static const wchar_t kTestApplicationLocale[] = L"fr-CA";
211 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER };
212 for (int i = 0; i < arraysize(root); ++i) {
213 SetCFPolicyString(root[i], policy::key::kApplicationLocaleValue,
214 kTestApplicationLocale);
215 ResetPolicySettings();
216 EXPECT_EQ(std::wstring(kTestApplicationLocale),
217 PolicySettings::GetInstance()->ApplicationLocale());
219 DeleteChromeFramePolicyEntries(root[i]);
223 TEST_F(PolicySettingsTest, AdditionalLaunchParameters) {
224 EXPECT_TRUE(PolicySettings::GetInstance()->
225 AdditionalLaunchParameters().GetProgram().empty());
227 std::string test_switches("--");
228 test_switches += switches::kDisableMediaSource;
230 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER };
231 for (int i = 0; i < arraysize(root); ++i) {
232 SetCFPolicyString(root[i], policy::key::kAdditionalLaunchParameters,
233 ASCIIToWide(test_switches).c_str());
234 ResetPolicySettings();
235 const CommandLine& additional_params =
236 PolicySettings::GetInstance()->AdditionalLaunchParameters();
237 EXPECT_TRUE(additional_params.HasSwitch(switches::kDisableMediaSource));
239 base::FilePath program_path(FILE_PATH_LITERAL("my_chrome.exe"));
240 CommandLine new_cmd_line(program_path);
241 new_cmd_line.AppendArguments(additional_params, false);
242 EXPECT_NE(new_cmd_line.GetProgram(), additional_params.GetProgram());
243 EXPECT_TRUE(new_cmd_line.HasSwitch(switches::kDisableMediaSource));
245 DeleteChromeFramePolicyEntries(root[i]);