OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/at_exit.h" |
6 #include "base/logging.h" | 7 #include "base/logging.h" |
7 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
8 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
9 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
10 #include "base/win/registry.h" | 11 #include "base/win/registry.h" |
11 #include "chrome/common/policy_constants.h" | 12 #include "chrome/common/policy_constants.h" |
12 #include "chrome_frame/policy_settings.h" | 13 #include "chrome_frame/policy_settings.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 using base::win::RegKey; | 16 using base::win::RegKey; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 return false; | 126 return false; |
126 | 127 |
127 std::wstring application_locale_value( | 128 std::wstring application_locale_value( |
128 ASCIIToWide(policy::key::kApplicationLocaleValue)); | 129 ASCIIToWide(policy::key::kApplicationLocaleValue)); |
129 EXPECT_TRUE(policy_key.WriteValue(application_locale_value.c_str(), locale)); | 130 EXPECT_TRUE(policy_key.WriteValue(application_locale_value.c_str(), locale)); |
130 return true; | 131 return true; |
131 } | 132 } |
132 | 133 |
133 } // end namespace | 134 } // end namespace |
134 | 135 |
135 TEST(PolicySettings, RendererForUrl) { | 136 class PolicySettingsTest : public testing::Test { |
136 TempRegKeyOverride::DeleteAllTempKeys(); | 137 protected: |
| 138 void SetUp() { |
| 139 TempRegKeyOverride::DeleteAllTempKeys(); |
137 | 140 |
138 scoped_ptr<TempRegKeyOverride> hklm_pol( | 141 hklm_pol_.reset(new TempRegKeyOverride(HKEY_LOCAL_MACHINE, L"hklm_pol")); |
139 new TempRegKeyOverride(HKEY_LOCAL_MACHINE, L"hklm_pol")); | 142 hkcu_pol_.reset(new TempRegKeyOverride(HKEY_CURRENT_USER, L"hkcu_pol")); |
140 scoped_ptr<TempRegKeyOverride> hkcu_pol( | |
141 new TempRegKeyOverride(HKEY_CURRENT_USER, L"hkcu_pol")); | |
142 | 143 |
| 144 ResetPolicySettings(); |
| 145 } |
| 146 |
| 147 void TearDown() { |
| 148 hkcu_pol_.reset(NULL); |
| 149 hklm_pol_.reset(NULL); |
| 150 TempRegKeyOverride::DeleteAllTempKeys(); |
| 151 } |
| 152 |
| 153 void ResetPolicySettings() { |
| 154 at_exit_manager_.ProcessCallbacksNow(); |
| 155 } |
| 156 |
| 157 // This is used to manage life cycle of PolicySettings singleton. |
| 158 base::ShadowingAtExitManager at_exit_manager_; |
| 159 scoped_ptr<TempRegKeyOverride> hklm_pol_; |
| 160 scoped_ptr<TempRegKeyOverride> hkcu_pol_; |
| 161 }; |
| 162 |
| 163 TEST_F(PolicySettingsTest, RendererForUrl) { |
143 const wchar_t* kTestUrls[] = { | 164 const wchar_t* kTestUrls[] = { |
144 L"http://www.example.com", | 165 L"http://www.example.com", |
145 L"http://www.pattern.com", | 166 L"http://www.pattern.com", |
146 L"http://www.test.com" | 167 L"http://www.test.com" |
147 }; | 168 }; |
148 const wchar_t* kTestFilters[] = { | 169 const wchar_t* kTestFilters[] = { |
149 L"*.example.com", | 170 L"*.example.com", |
150 L"*.pattern.com", | 171 L"*.pattern.com", |
151 L"*.test.com" | 172 L"*.test.com" |
152 }; | 173 }; |
153 const wchar_t kNoMatchUrl[] = L"http://www.chromium.org"; | 174 const wchar_t kNoMatchUrl[] = L"http://www.chromium.org"; |
154 | 175 |
155 scoped_ptr<PolicySettings> settings(new PolicySettings()); | |
156 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED, | 176 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED, |
157 settings->default_renderer()); | 177 PolicySettings::GetInstance()->default_renderer()); |
158 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED, | 178 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED, |
159 settings->GetRendererForUrl(kNoMatchUrl)); | 179 PolicySettings::GetInstance()->GetRendererForUrl(kNoMatchUrl)); |
160 | 180 |
161 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER }; | 181 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER }; |
162 for (int i = 0; i < arraysize(root); ++i) { | 182 for (int i = 0; i < arraysize(root); ++i) { |
163 EXPECT_TRUE(SetRendererSettings(root[i], | 183 EXPECT_TRUE(SetRendererSettings(root[i], |
164 PolicySettings::RENDER_IN_CHROME_FRAME, kTestFilters, | 184 PolicySettings::RENDER_IN_CHROME_FRAME, kTestFilters, |
165 arraysize(kTestFilters))); | 185 arraysize(kTestFilters))); |
166 | 186 |
167 settings.reset(new PolicySettings()); | 187 ResetPolicySettings(); |
168 EXPECT_EQ(PolicySettings::RENDER_IN_CHROME_FRAME, | 188 EXPECT_EQ(PolicySettings::RENDER_IN_CHROME_FRAME, |
169 settings->GetRendererForUrl(kNoMatchUrl)); | 189 PolicySettings::GetInstance()->GetRendererForUrl(kNoMatchUrl)); |
170 for (int j = 0; j < arraysize(kTestUrls); ++j) { | 190 for (int j = 0; j < arraysize(kTestUrls); ++j) { |
171 EXPECT_EQ(PolicySettings::RENDER_IN_HOST, | 191 EXPECT_EQ(PolicySettings::RENDER_IN_HOST, |
172 settings->GetRendererForUrl(kTestUrls[j])); | 192 PolicySettings::GetInstance()->GetRendererForUrl(kTestUrls[j])); |
173 } | 193 } |
174 | 194 |
175 EXPECT_TRUE(SetRendererSettings(root[i], | 195 EXPECT_TRUE(SetRendererSettings(root[i], |
176 PolicySettings::RENDER_IN_HOST, NULL, 0)); | 196 PolicySettings::RENDER_IN_HOST, NULL, 0)); |
177 | 197 |
178 settings.reset(new PolicySettings()); | 198 ResetPolicySettings(); |
179 EXPECT_EQ(PolicySettings::RENDER_IN_HOST, | 199 EXPECT_EQ(PolicySettings::RENDER_IN_HOST, |
180 settings->GetRendererForUrl(kNoMatchUrl)); | 200 PolicySettings::GetInstance()->GetRendererForUrl(kNoMatchUrl)); |
181 for (int j = 0; j < arraysize(kTestUrls); ++j) { | 201 for (int j = 0; j < arraysize(kTestUrls); ++j) { |
182 EXPECT_EQ(PolicySettings::RENDER_IN_HOST, | 202 EXPECT_EQ(PolicySettings::RENDER_IN_HOST, |
183 settings->GetRendererForUrl(kTestUrls[j])); | 203 PolicySettings::GetInstance()->GetRendererForUrl(kTestUrls[j])); |
184 } | 204 } |
185 | 205 |
186 DeleteChromeFramePolicyEntries(root[i]); | 206 DeleteChromeFramePolicyEntries(root[i]); |
187 } | 207 } |
188 | |
189 hkcu_pol.reset(NULL); | |
190 hklm_pol.reset(NULL); | |
191 TempRegKeyOverride::DeleteAllTempKeys(); | |
192 } | 208 } |
193 | 209 |
194 TEST(PolicySettings, RendererForContentType) { | 210 TEST_F(PolicySettingsTest, RendererForContentType) { |
195 TempRegKeyOverride::DeleteAllTempKeys(); | |
196 | |
197 scoped_ptr<TempRegKeyOverride> hklm_pol( | |
198 new TempRegKeyOverride(HKEY_LOCAL_MACHINE, L"hklm_pol")); | |
199 scoped_ptr<TempRegKeyOverride> hkcu_pol( | |
200 new TempRegKeyOverride(HKEY_CURRENT_USER, L"hkcu_pol")); | |
201 | |
202 scoped_ptr<PolicySettings> settings(new PolicySettings()); | |
203 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED, | 211 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED, |
204 settings->GetRendererForContentType(L"text/xml")); | 212 PolicySettings::GetInstance()->GetRendererForContentType( |
| 213 L"text/xml")); |
205 | 214 |
206 const wchar_t* kTestPolicyContentTypes[] = { | 215 const wchar_t* kTestPolicyContentTypes[] = { |
207 L"application/xml", | 216 L"application/xml", |
208 L"text/xml", | 217 L"text/xml", |
209 L"application/pdf", | 218 L"application/pdf", |
210 }; | 219 }; |
211 | 220 |
212 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER }; | 221 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER }; |
213 for (int i = 0; i < arraysize(root); ++i) { | 222 for (int i = 0; i < arraysize(root); ++i) { |
214 SetCFContentTypes(root[i], kTestPolicyContentTypes, | 223 SetCFContentTypes(root[i], kTestPolicyContentTypes, |
215 arraysize(kTestPolicyContentTypes)); | 224 arraysize(kTestPolicyContentTypes)); |
216 settings.reset(new PolicySettings()); | 225 ResetPolicySettings(); |
217 for (int type = 0; type < arraysize(kTestPolicyContentTypes); ++type) { | 226 for (int type = 0; type < arraysize(kTestPolicyContentTypes); ++type) { |
218 EXPECT_EQ(PolicySettings::RENDER_IN_CHROME_FRAME, | 227 EXPECT_EQ(PolicySettings::RENDER_IN_CHROME_FRAME, |
219 settings->GetRendererForContentType( | 228 PolicySettings::GetInstance()->GetRendererForContentType( |
220 kTestPolicyContentTypes[type])); | 229 kTestPolicyContentTypes[type])); |
221 } | 230 } |
222 | 231 |
223 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED, | 232 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED, |
224 settings->GetRendererForContentType(L"text/html")); | 233 PolicySettings::GetInstance()->GetRendererForContentType( |
| 234 L"text/html")); |
225 | 235 |
226 DeleteChromeFramePolicyEntries(root[i]); | 236 DeleteChromeFramePolicyEntries(root[i]); |
227 } | 237 } |
228 } | 238 } |
229 | 239 |
230 TEST(PolicySettings, ApplicationLocale) { | 240 TEST_F(PolicySettingsTest, ApplicationLocale) { |
231 TempRegKeyOverride::DeleteAllTempKeys(); | 241 EXPECT_TRUE(PolicySettings::GetInstance()->ApplicationLocale().empty()); |
232 | |
233 scoped_ptr<TempRegKeyOverride> hklm_pol( | |
234 new TempRegKeyOverride(HKEY_LOCAL_MACHINE, L"hklm_pol")); | |
235 scoped_ptr<TempRegKeyOverride> hkcu_pol( | |
236 new TempRegKeyOverride(HKEY_CURRENT_USER, L"hkcu_pol")); | |
237 | |
238 scoped_ptr<PolicySettings> settings(new PolicySettings()); | |
239 EXPECT_TRUE(settings->ApplicationLocale().empty()); | |
240 | 242 |
241 static const wchar_t kTestApplicationLocale[] = L"fr-CA"; | 243 static const wchar_t kTestApplicationLocale[] = L"fr-CA"; |
242 | 244 |
243 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER }; | 245 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER }; |
244 for (int i = 0; i < arraysize(root); ++i) { | 246 for (int i = 0; i < arraysize(root); ++i) { |
245 SetChromeApplicationLocale(root[i], kTestApplicationLocale); | 247 SetChromeApplicationLocale(root[i], kTestApplicationLocale); |
246 settings.reset(new PolicySettings()); | 248 ResetPolicySettings(); |
247 EXPECT_EQ(std::wstring(kTestApplicationLocale), | 249 EXPECT_EQ(std::wstring(kTestApplicationLocale), |
248 settings->ApplicationLocale()); | 250 PolicySettings::GetInstance()->ApplicationLocale()); |
249 | 251 |
250 DeleteChromeFramePolicyEntries(root[i]); | 252 DeleteChromeFramePolicyEntries(root[i]); |
251 } | 253 } |
252 } | 254 } |
OLD | NEW |