Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / policy_handlers_unittest.cc
index a7c1191..09b2d19 100644 (file)
@@ -2,12 +2,13 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#include "base/json/json_reader.h"
 #include "base/prefs/pref_value_map.h"
 #include "chrome/browser/extensions/external_policy_loader.h"
 #include "chrome/browser/extensions/policy_handlers.h"
-#include "chrome/common/pref_names.h"
 #include "components/policy/core/browser/policy_error_map.h"
 #include "components/policy/core/common/policy_map.h"
+#include "components/policy/core/common/schema.h"
 #include "extensions/browser/pref_names.h"
 #include "policy/policy_constants.h"
 #include "testing/gtest/include/gtest/gtest.h"
 namespace extensions {
 
 const char kTestPref[] = "unit_test.test_pref";
+const char kTestManagementPolicy1[] =
+    "{"
+    "  \"abcdefghijklmnopabcdefghijklmnop\": {"
+    "    \"installation_mode\": \"force_installed\","
+    "  },"
+    "}";
+const char kTestManagementPolicy2[] =
+    "{"
+    "  \"abcdefghijklmnopabcdefghijklmnop\": {"
+    "    \"installation_mode\": \"force_installed\","
+    "    \"update_url\": \"http://example.com/app\","
+    "  },"
+    "  \"*\": {"
+    "    \"installation_mode\": \"blocked\","
+    "  },"
+    "}";
 
 TEST(ExtensionListPolicyHandlerTest, CheckPolicySettings) {
   base::ListValue list;
@@ -281,4 +298,57 @@ TEST(ExtensionURLPatternListPolicyHandlerTest, ApplyPolicySettings) {
   EXPECT_TRUE(base::Value::Equals(&list, value));
 }
 
+TEST(ExtensionSettingsPolicyHandlerTest, CheckPolicySettings) {
+  std::string error;
+  scoped_ptr<base::Value> policy_value(base::JSONReader::ReadAndReturnError(
+      kTestManagementPolicy1,
+      base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS,
+      NULL,
+      &error));
+  ASSERT_TRUE(policy_value.get()) << error;
+
+  policy::Schema chrome_schema =
+      policy::Schema::Wrap(policy::GetChromeSchemaData());
+  policy::PolicyMap policy_map;
+  policy::PolicyErrorMap errors;
+  ExtensionSettingsPolicyHandler handler(chrome_schema);
+
+  policy_map.Set(policy::key::kExtensionSettings,
+                 policy::POLICY_LEVEL_MANDATORY,
+                 policy::POLICY_SCOPE_USER,
+                 policy_value.release(),
+                 NULL);
+  // CheckPolicySettings() fails due to missing update URL.
+  EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
+  EXPECT_FALSE(errors.empty());
+}
+
+TEST(ExtensionSettingsPolicyHandlerTest, ApplyPolicySettings) {
+  std::string error;
+  scoped_ptr<base::Value> policy_value(base::JSONReader::ReadAndReturnError(
+      kTestManagementPolicy2,
+      base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS,
+      NULL,
+      &error));
+  ASSERT_TRUE(policy_value.get()) << error;
+
+  policy::Schema chrome_schema =
+      policy::Schema::Wrap(policy::GetChromeSchemaData());
+  policy::PolicyMap policy_map;
+  policy::PolicyErrorMap errors;
+  PrefValueMap prefs;
+  ExtensionSettingsPolicyHandler handler(chrome_schema);
+
+  policy_map.Set(policy::key::kExtensionSettings,
+                 policy::POLICY_LEVEL_MANDATORY,
+                 policy::POLICY_SCOPE_USER,
+                 policy_value->DeepCopy(),
+                 NULL);
+  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
+  handler.ApplyPolicySettings(policy_map, &prefs);
+  base::Value* value = NULL;
+  ASSERT_TRUE(prefs.GetValue(pref_names::kExtensionManagement, &value));
+  EXPECT_TRUE(base::Value::Equals(policy_value.get(), value));
+}
+
 }  // namespace extensions