Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / policy / managed_bookmarks_policy_handler_unittest.cc
blobb232f6e8216f1c0e54ee405579046b3a6cd3f62a
1 // Copyright 2014 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/json/json_reader.h"
6 #include "chrome/browser/policy/managed_bookmarks_policy_handler.h"
7 #include "components/bookmarks/common/bookmark_pref_names.h"
8 #include "components/policy/core/browser/configuration_policy_pref_store.h"
9 #include "components/policy/core/browser/configuration_policy_pref_store_test.h"
10 #include "components/policy/core/common/policy_map.h"
11 #include "components/policy/core/common/schema.h"
12 #include "policy/policy_constants.h"
14 #if defined(ENABLE_EXTENSIONS)
15 #include "extensions/common/value_builder.h"
16 #endif
18 namespace policy {
20 class ManagedBookmarksPolicyHandlerTest
21 : public ConfigurationPolicyPrefStoreTest {
22 void SetUp() override {
23 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData());
24 handler_list_.AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>(
25 new ManagedBookmarksPolicyHandler(chrome_schema)));
29 #if defined(ENABLE_EXTENSIONS)
30 TEST_F(ManagedBookmarksPolicyHandlerTest, ApplyPolicySettings) {
31 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL));
33 PolicyMap policy;
34 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
35 base::JSONReader::DeprecatedRead(
36 "["
37 " {"
38 " \"name\": \"Google\","
39 " \"url\": \"google.com\""
40 " },"
41 " {"
42 " \"name\": \"Empty Folder\","
43 " \"children\": []"
44 " },"
45 " {"
46 " \"name\": \"Big Folder\","
47 " \"children\": ["
48 " {"
49 " \"name\": \"Youtube\","
50 " \"url\": \"youtube.com\""
51 " },"
52 " {"
53 " \"name\": \"Chromium\","
54 " \"url\": \"chromium.org\""
55 " },"
56 " {"
57 " \"name\": \"More Stuff\","
58 " \"children\": ["
59 " {"
60 " \"name\": \"Bugs\","
61 " \"url\": \"crbug.com\""
62 " }"
63 " ]"
64 " }"
65 " ]"
66 " }"
67 "]"),
68 NULL);
69 UpdateProviderPolicy(policy);
70 const base::Value* pref_value = NULL;
71 EXPECT_TRUE(
72 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value));
73 ASSERT_TRUE(pref_value);
75 scoped_ptr<base::Value> expected(
76 extensions::ListBuilder()
77 .Append(extensions::DictionaryBuilder()
78 .Set("name", "Google")
79 .Set("url", "http://google.com/"))
80 .Append(extensions::DictionaryBuilder()
81 .Set("name", "Empty Folder")
82 .Set("children", extensions::ListBuilder().Pass()))
83 .Append(extensions::DictionaryBuilder()
84 .Set("name", "Big Folder")
85 .Set("children", extensions::ListBuilder()
86 .Append(extensions::DictionaryBuilder()
87 .Set("name", "Youtube")
88 .Set("url", "http://youtube.com/"))
89 .Append(extensions::DictionaryBuilder()
90 .Set("name", "Chromium")
91 .Set("url", "http://chromium.org/"))
92 .Append(extensions::DictionaryBuilder()
93 .Set("name", "More Stuff")
94 .Set("children", extensions::ListBuilder()
95 .Append(extensions::DictionaryBuilder()
96 .Set("name", "Bugs")
97 .Set("url", "http://crbug.com/")
98 .Pass())
99 .Pass())
100 .Pass())
101 .Pass())
102 .Pass())
103 .Build());
104 EXPECT_TRUE(pref_value->Equals(expected.get()));
106 #endif // defined(ENABLE_EXTENSIONS)
108 TEST_F(ManagedBookmarksPolicyHandlerTest, WrongPolicyType) {
109 PolicyMap policy;
110 // The expected type is base::ListValue, but this policy sets it as an
111 // unparsed base::StringValue. Any type other than ListValue should fail.
112 policy.Set(key::kManagedBookmarks,
113 POLICY_LEVEL_MANDATORY,
114 POLICY_SCOPE_USER,
115 new base::StringValue(
117 " {"
118 " \"name\": \"Google\","
119 " \"url\": \"google.com\""
120 " },"
121 "]"),
122 NULL);
123 UpdateProviderPolicy(policy);
124 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL));
127 #if defined(ENABLE_EXTENSIONS)
128 TEST_F(ManagedBookmarksPolicyHandlerTest, UnknownKeys) {
129 PolicyMap policy;
130 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
131 base::JSONReader::DeprecatedRead(
133 " {"
134 " \"name\": \"Google\","
135 " \"unknown\": \"should be ignored\","
136 " \"url\": \"google.com\""
137 " }"
138 "]"),
139 NULL);
140 UpdateProviderPolicy(policy);
141 const base::Value* pref_value = NULL;
142 EXPECT_TRUE(
143 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value));
144 ASSERT_TRUE(pref_value);
146 scoped_ptr<base::Value> expected(
147 extensions::ListBuilder()
148 .Append(extensions::DictionaryBuilder()
149 .Set("name", "Google")
150 .Set("url", "http://google.com/"))
151 .Build());
152 EXPECT_TRUE(pref_value->Equals(expected.get()));
154 #endif
156 #if defined(ENABLE_EXTENSIONS)
157 TEST_F(ManagedBookmarksPolicyHandlerTest, BadBookmark) {
158 PolicyMap policy;
159 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
160 base::JSONReader::DeprecatedRead(
162 " {"
163 " \"name\": \"Empty\","
164 " \"url\": \"\""
165 " },"
166 " {"
167 " \"name\": \"Invalid type\","
168 " \"url\": 4"
169 " },"
170 " {"
171 " \"name\": \"Invalid URL\","
172 " \"url\": \"?\""
173 " },"
174 " {"
175 " \"name\": \"Google\","
176 " \"url\": \"google.com\""
177 " }"
178 "]"),
179 NULL);
180 UpdateProviderPolicy(policy);
181 const base::Value* pref_value = NULL;
182 EXPECT_TRUE(
183 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value));
184 ASSERT_TRUE(pref_value);
186 scoped_ptr<base::Value> expected(
187 extensions::ListBuilder()
188 .Append(extensions::DictionaryBuilder()
189 .Set("name", "Google")
190 .Set("url", "http://google.com/"))
191 .Build());
192 EXPECT_TRUE(pref_value->Equals(expected.get()));
194 #endif
196 } // namespace policy