Fix the duplicatedly commited composition text when switching IME.
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / configuration_policy_handler_chromeos_unittest.cc
bloba5691b8bbadad0d519d5d91316f5049e088b856c
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 "chrome/browser/chromeos/policy/configuration_policy_handler_chromeos.h"
7 #include "base/callback.h"
8 #include "base/json/json_reader.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/pref_value_map.h"
11 #include "base/values.h"
12 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/policy/core/browser/policy_error_map.h"
15 #include "components/policy/core/common/external_data_fetcher.h"
16 #include "components/policy/core/common/policy_map.h"
17 #include "components/policy/core/common/policy_types.h"
18 #include "components/policy/core/common/schema.h"
19 #include "policy/policy_constants.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace policy {
24 namespace {
26 // Test cases for the screen magnifier type policy setting.
27 class ScreenMagnifierPolicyHandlerTest : public testing::Test {
28 protected:
29 PolicyMap policy_;
30 PrefValueMap prefs_;
31 ScreenMagnifierPolicyHandler handler_;
34 class LoginScreenPowerManagementPolicyHandlerTest : public testing::Test {
35 protected:
36 LoginScreenPowerManagementPolicyHandlerTest();
38 void SetUp() override;
40 Schema chrome_schema_;
42 private:
43 DISALLOW_COPY_AND_ASSIGN(LoginScreenPowerManagementPolicyHandlerTest);
46 LoginScreenPowerManagementPolicyHandlerTest::
47 LoginScreenPowerManagementPolicyHandlerTest() {
50 void LoginScreenPowerManagementPolicyHandlerTest::SetUp() {
51 chrome_schema_ = Schema::Wrap(GetChromeSchemaData());
54 TEST_F(ScreenMagnifierPolicyHandlerTest, Default) {
55 handler_.ApplyPolicySettings(policy_, &prefs_);
56 EXPECT_FALSE(
57 prefs_.GetValue(prefs::kAccessibilityScreenMagnifierEnabled, NULL));
58 EXPECT_FALSE(prefs_.GetValue(prefs::kAccessibilityScreenMagnifierType, NULL));
61 TEST_F(ScreenMagnifierPolicyHandlerTest, Disabled) {
62 policy_.Set(key::kScreenMagnifierType,
63 POLICY_LEVEL_MANDATORY,
64 POLICY_SCOPE_USER,
65 POLICY_SOURCE_CLOUD,
66 new base::FundamentalValue(0),
67 NULL);
68 handler_.ApplyPolicySettings(policy_, &prefs_);
70 const base::Value* enabled = NULL;
71 EXPECT_TRUE(
72 prefs_.GetValue(prefs::kAccessibilityScreenMagnifierEnabled, &enabled));
73 ASSERT_TRUE(enabled);
74 EXPECT_TRUE(base::FundamentalValue(false).Equals(enabled));
76 const base::Value* type = NULL;
77 EXPECT_TRUE(prefs_.GetValue(prefs::kAccessibilityScreenMagnifierType, &type));
78 ASSERT_TRUE(type);
79 EXPECT_TRUE(base::FundamentalValue(0).Equals(type));
82 TEST_F(ScreenMagnifierPolicyHandlerTest, Enabled) {
83 policy_.Set(key::kScreenMagnifierType,
84 POLICY_LEVEL_MANDATORY,
85 POLICY_SCOPE_USER,
86 POLICY_SOURCE_CLOUD,
87 new base::FundamentalValue(1),
88 NULL);
89 handler_.ApplyPolicySettings(policy_, &prefs_);
91 const base::Value* enabled = NULL;
92 EXPECT_TRUE(
93 prefs_.GetValue(prefs::kAccessibilityScreenMagnifierEnabled, &enabled));
94 ASSERT_TRUE(enabled);
95 EXPECT_TRUE(base::FundamentalValue(true).Equals(enabled));
97 const base::Value* type = NULL;
98 EXPECT_TRUE(prefs_.GetValue(prefs::kAccessibilityScreenMagnifierType, &type));
99 ASSERT_TRUE(type);
100 EXPECT_TRUE(base::FundamentalValue(1).Equals(type));
103 TEST(ExternalDataPolicyHandlerTest, Empty) {
104 PolicyErrorMap errors;
105 EXPECT_TRUE(ExternalDataPolicyHandler(key::kUserAvatarImage)
106 .CheckPolicySettings(PolicyMap(), &errors));
107 EXPECT_TRUE(errors.GetErrors(key::kUserAvatarImage).empty());
110 TEST(ExternalDataPolicyHandlerTest, WrongType) {
111 PolicyMap policy_map;
112 policy_map.Set(key::kUserAvatarImage,
113 POLICY_LEVEL_MANDATORY,
114 POLICY_SCOPE_USER,
115 POLICY_SOURCE_CLOUD,
116 new base::FundamentalValue(false),
117 NULL);
118 PolicyErrorMap errors;
119 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
120 .CheckPolicySettings(policy_map, &errors));
121 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
124 TEST(ExternalDataPolicyHandlerTest, MissingURL) {
125 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
126 dict->SetString("hash", "1234567890123456789012345678901234567890");
127 PolicyMap policy_map;
128 policy_map.Set(key::kUserAvatarImage,
129 POLICY_LEVEL_MANDATORY,
130 POLICY_SCOPE_USER,
131 POLICY_SOURCE_CLOUD,
132 dict.release(),
133 NULL);
134 PolicyErrorMap errors;
135 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
136 .CheckPolicySettings(policy_map, &errors));
137 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
140 TEST(ExternalDataPolicyHandlerTest, InvalidURL) {
141 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
142 dict->SetString("url", "http://");
143 dict->SetString("hash", "1234567890123456789012345678901234567890");
144 PolicyMap policy_map;
145 policy_map.Set(key::kUserAvatarImage,
146 POLICY_LEVEL_MANDATORY,
147 POLICY_SCOPE_USER,
148 POLICY_SOURCE_CLOUD,
149 dict.release(),
150 NULL);
151 PolicyErrorMap errors;
152 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
153 .CheckPolicySettings(policy_map, &errors));
154 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
157 TEST(ExternalDataPolicyHandlerTest, MissingHash) {
158 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
159 dict->SetString("url", "http://localhost/");
160 PolicyMap policy_map;
161 policy_map.Set(key::kUserAvatarImage,
162 POLICY_LEVEL_MANDATORY,
163 POLICY_SCOPE_USER,
164 POLICY_SOURCE_CLOUD,
165 dict.release(),
166 NULL);
167 PolicyErrorMap errors;
168 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
169 .CheckPolicySettings(policy_map, &errors));
170 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
173 TEST(ExternalDataPolicyHandlerTest, InvalidHash) {
174 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
175 dict->SetString("url", "http://localhost/");
176 dict->SetString("hash", "1234");
177 PolicyMap policy_map;
178 policy_map.Set(key::kUserAvatarImage,
179 POLICY_LEVEL_MANDATORY,
180 POLICY_SCOPE_USER,
181 POLICY_SOURCE_CLOUD,
182 dict.release(),
183 NULL);
184 PolicyErrorMap errors;
185 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
186 .CheckPolicySettings(policy_map, &errors));
187 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
190 TEST(ExternalDataPolicyHandlerTest, Valid) {
191 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
192 dict->SetString("url", "http://localhost/");
193 dict->SetString(
194 "hash",
195 "1234567890123456789012345678901234567890123456789012345678901234");
196 PolicyMap policy_map;
197 policy_map.Set(key::kUserAvatarImage,
198 POLICY_LEVEL_MANDATORY,
199 POLICY_SCOPE_USER,
200 POLICY_SOURCE_CLOUD,
201 dict.release(),
202 NULL);
203 PolicyErrorMap errors;
204 EXPECT_TRUE(ExternalDataPolicyHandler(key::kUserAvatarImage)
205 .CheckPolicySettings(policy_map, &errors));
206 EXPECT_TRUE(errors.GetErrors(key::kUserAvatarImage).empty());
209 const char kLoginScreenPowerManagementPolicy[] =
211 " \"AC\": {"
212 " \"Delays\": {"
213 " \"ScreenDim\": 5000,"
214 " \"ScreenOff\": 7000,"
215 " \"Idle\": 9000"
216 " },"
217 " \"IdleAction\": \"DoNothing\""
218 " },"
219 " \"Battery\": {"
220 " \"Delays\": {"
221 " \"ScreenDim\": 1000,"
222 " \"ScreenOff\": 3000,"
223 " \"Idle\": 4000"
224 " },"
225 " \"IdleAction\": \"DoNothing\""
226 " },"
227 " \"LidCloseAction\": \"DoNothing\","
228 " \"UserActivityScreenDimDelayScale\": 300"
229 "}";
231 } // namespace
233 TEST(NetworkConfigurationPolicyHandlerTest, Empty) {
234 PolicyMap policy_map;
235 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
236 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
237 PolicyErrorMap errors;
238 EXPECT_TRUE(handler->CheckPolicySettings(policy_map, &errors));
239 EXPECT_TRUE(errors.GetErrors(key::kOpenNetworkConfiguration).empty());
242 TEST(NetworkConfigurationPolicyHandlerTest, ValidONC) {
243 const std::string kTestONC(
245 " \"NetworkConfigurations\": [{"
246 " \"GUID\": \"{485d6076-dd44-6b6d-69787465725f5045}\","
247 " \"Type\": \"WiFi\","
248 " \"Name\": \"some name\","
249 " \"WiFi\": {"
250 " \"Security\": \"WEP-PSK\","
251 " \"SSID\": \"ssid\","
252 " \"Passphrase\": \"pass\","
253 " }"
254 " }]"
255 "}");
257 PolicyMap policy_map;
258 policy_map.Set(key::kOpenNetworkConfiguration,
259 POLICY_LEVEL_MANDATORY,
260 POLICY_SCOPE_USER,
261 POLICY_SOURCE_CLOUD,
262 new base::StringValue(kTestONC),
263 NULL);
264 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
265 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
266 PolicyErrorMap errors;
267 EXPECT_TRUE(handler->CheckPolicySettings(policy_map, &errors));
268 EXPECT_TRUE(errors.GetErrors(key::kOpenNetworkConfiguration).empty());
271 TEST(NetworkConfigurationPolicyHandlerTest, WrongType) {
272 PolicyMap policy_map;
273 policy_map.Set(key::kOpenNetworkConfiguration,
274 POLICY_LEVEL_MANDATORY,
275 POLICY_SCOPE_USER,
276 POLICY_SOURCE_CLOUD,
277 new base::FundamentalValue(false),
278 NULL);
279 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
280 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
281 PolicyErrorMap errors;
282 EXPECT_FALSE(handler->CheckPolicySettings(policy_map, &errors));
283 EXPECT_FALSE(errors.GetErrors(key::kOpenNetworkConfiguration).empty());
286 TEST(NetworkConfigurationPolicyHandlerTest, JSONParseError) {
287 const std::string kTestONC("I'm not proper JSON!");
288 PolicyMap policy_map;
289 policy_map.Set(key::kOpenNetworkConfiguration,
290 POLICY_LEVEL_MANDATORY,
291 POLICY_SCOPE_USER,
292 POLICY_SOURCE_CLOUD,
293 new base::StringValue(kTestONC),
294 NULL);
295 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
296 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
297 PolicyErrorMap errors;
298 EXPECT_FALSE(handler->CheckPolicySettings(policy_map, &errors));
299 EXPECT_FALSE(errors.GetErrors(key::kOpenNetworkConfiguration).empty());
302 TEST(NetworkConfigurationPolicyHandlerTest, Sanitization) {
303 const std::string kTestONC(
305 " \"NetworkConfigurations\": [{"
306 " \"GUID\": \"{485d6076-dd44-6b6d-69787465725f5045}\","
307 " \"Type\": \"WiFi\","
308 " \"Name\": \"some name\","
309 " \"WiFi\": {"
310 " \"Security\": \"WEP-PSK\","
311 " \"SSID\": \"ssid\","
312 " \"Passphrase\": \"pass\","
313 " }"
314 " }]"
315 "}");
317 PolicyMap policy_map;
318 policy_map.Set(key::kOpenNetworkConfiguration,
319 POLICY_LEVEL_MANDATORY,
320 POLICY_SCOPE_USER,
321 POLICY_SOURCE_CLOUD,
322 new base::StringValue(kTestONC),
323 NULL);
324 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
325 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
326 PolicyErrorMap errors;
327 handler->PrepareForDisplaying(&policy_map);
328 const base::Value* sanitized =
329 policy_map.GetValue(key::kOpenNetworkConfiguration);
330 ASSERT_TRUE(sanitized);
331 std::string sanitized_onc;
332 EXPECT_TRUE(sanitized->GetAsString(&sanitized_onc));
333 EXPECT_FALSE(sanitized_onc.empty());
334 EXPECT_EQ(std::string::npos, sanitized_onc.find("pass"));
337 TEST(PinnedLauncherAppsPolicyHandler, PrefTranslation) {
338 base::ListValue list;
339 PolicyMap policy_map;
340 PrefValueMap prefs;
341 base::ListValue expected_pinned_apps;
342 base::Value* value = NULL;
343 PinnedLauncherAppsPolicyHandler handler;
345 policy_map.Set(key::kPinnedLauncherApps, POLICY_LEVEL_MANDATORY,
346 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, list.DeepCopy(),
347 nullptr);
348 handler.ApplyPolicySettings(policy_map, &prefs);
349 EXPECT_TRUE(prefs.GetValue(prefs::kPinnedLauncherApps, &value));
350 EXPECT_TRUE(base::Value::Equals(&expected_pinned_apps, value));
352 base::StringValue entry1("abcdefghijklmnopabcdefghijklmnop");
353 base::DictionaryValue* entry1_dict = new base::DictionaryValue();
354 entry1_dict->Set(ash::kPinnedAppsPrefAppIDPath, entry1.DeepCopy());
355 expected_pinned_apps.Append(entry1_dict);
356 list.Append(entry1.DeepCopy());
357 policy_map.Set(key::kPinnedLauncherApps, POLICY_LEVEL_MANDATORY,
358 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, list.DeepCopy(),
359 nullptr);
360 prefs.Clear();
361 handler.ApplyPolicySettings(policy_map, &prefs);
362 EXPECT_TRUE(prefs.GetValue(prefs::kPinnedLauncherApps, &value));
363 EXPECT_TRUE(base::Value::Equals(&expected_pinned_apps, value));
366 TEST_F(LoginScreenPowerManagementPolicyHandlerTest, Empty) {
367 PolicyMap policy_map;
368 LoginScreenPowerManagementPolicyHandler handler(chrome_schema_);
369 PolicyErrorMap errors;
370 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
371 EXPECT_TRUE(errors.GetErrors(key::kDeviceLoginScreenPowerManagement).empty());
374 TEST_F(LoginScreenPowerManagementPolicyHandlerTest, ValidPolicy) {
375 PolicyMap policy_map;
376 policy_map.Set(
377 key::kDeviceLoginScreenPowerManagement, POLICY_LEVEL_MANDATORY,
378 POLICY_SCOPE_USER,
379 POLICY_SOURCE_CLOUD,
380 base::JSONReader::Read(kLoginScreenPowerManagementPolicy).release(),
381 NULL);
382 LoginScreenPowerManagementPolicyHandler handler(chrome_schema_);
383 PolicyErrorMap errors;
384 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
385 EXPECT_TRUE(errors.GetErrors(key::kDeviceLoginScreenPowerManagement).empty());
388 TEST_F(LoginScreenPowerManagementPolicyHandlerTest, WrongType) {
389 PolicyMap policy_map;
390 policy_map.Set(key::kDeviceLoginScreenPowerManagement,
391 POLICY_LEVEL_MANDATORY,
392 POLICY_SCOPE_USER,
393 POLICY_SOURCE_CLOUD,
394 new base::FundamentalValue(false),
395 NULL);
396 LoginScreenPowerManagementPolicyHandler handler(chrome_schema_);
397 PolicyErrorMap errors;
398 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
399 EXPECT_FALSE(
400 errors.GetErrors(key::kDeviceLoginScreenPowerManagement).empty());
403 } // namespace policy