Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / chrome / browser / policy / managed_bookmarks_policy_handler_unittest.cc
blob50097313ef7629fa011f1c2da6c8780ea4ee08e5
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/policy_types.h"
12 #include "components/policy/core/common/schema.h"
13 #include "policy/policy_constants.h"
15 #if defined(ENABLE_EXTENSIONS)
16 #include "extensions/common/value_builder.h"
17 #endif
19 namespace policy {
21 class ManagedBookmarksPolicyHandlerTest
22 : public ConfigurationPolicyPrefStoreTest {
23 void SetUp() override {
24 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData());
25 handler_list_.AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>(
26 new ManagedBookmarksPolicyHandler(chrome_schema)));
30 #if defined(ENABLE_EXTENSIONS)
31 TEST_F(ManagedBookmarksPolicyHandlerTest, ApplyPolicySettings) {
32 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL));
34 PolicyMap policy;
35 policy.Set(key::kManagedBookmarks,
36 POLICY_LEVEL_MANDATORY,
37 POLICY_SCOPE_USER,
38 POLICY_SOURCE_CLOUD,
39 base::JSONReader::Read("["
40 " {"
41 " \"name\": \"Google\","
42 " \"url\": \"google.com\""
43 " },"
44 " {"
45 " \"name\": \"Empty Folder\","
46 " \"children\": []"
47 " },"
48 " {"
49 " \"name\": \"Big Folder\","
50 " \"children\": ["
51 " {"
52 " \"name\": \"Youtube\","
53 " \"url\": \"youtube.com\""
54 " },"
55 " {"
56 " \"name\": \"Chromium\","
57 " \"url\": \"chromium.org\""
58 " },"
59 " {"
60 " \"name\": \"More Stuff\","
61 " \"children\": ["
62 " {"
63 " \"name\": \"Bugs\","
64 " \"url\": \"crbug.com\""
65 " }"
66 " ]"
67 " }"
68 " ]"
69 " }"
70 "]")
71 .release(),
72 NULL);
73 UpdateProviderPolicy(policy);
74 const base::Value* pref_value = NULL;
75 EXPECT_TRUE(
76 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value));
77 ASSERT_TRUE(pref_value);
79 scoped_ptr<base::Value> expected(
80 extensions::ListBuilder()
81 .Append(extensions::DictionaryBuilder()
82 .Set("name", "Google")
83 .Set("url", "http://google.com/"))
84 .Append(extensions::DictionaryBuilder()
85 .Set("name", "Empty Folder")
86 .Set("children", extensions::ListBuilder().Pass()))
87 .Append(extensions::DictionaryBuilder()
88 .Set("name", "Big Folder")
89 .Set("children", extensions::ListBuilder()
90 .Append(extensions::DictionaryBuilder()
91 .Set("name", "Youtube")
92 .Set("url", "http://youtube.com/"))
93 .Append(extensions::DictionaryBuilder()
94 .Set("name", "Chromium")
95 .Set("url", "http://chromium.org/"))
96 .Append(extensions::DictionaryBuilder()
97 .Set("name", "More Stuff")
98 .Set("children", extensions::ListBuilder()
99 .Append(extensions::DictionaryBuilder()
100 .Set("name", "Bugs")
101 .Set("url", "http://crbug.com/")
102 .Pass())
103 .Pass())
104 .Pass())
105 .Pass())
106 .Pass())
107 .Build());
108 EXPECT_TRUE(pref_value->Equals(expected.get()));
110 #endif // defined(ENABLE_EXTENSIONS)
112 TEST_F(ManagedBookmarksPolicyHandlerTest, WrongPolicyType) {
113 PolicyMap policy;
114 // The expected type is base::ListValue, but this policy sets it as an
115 // unparsed base::StringValue. Any type other than ListValue should fail.
116 policy.Set(key::kManagedBookmarks,
117 POLICY_LEVEL_MANDATORY,
118 POLICY_SCOPE_USER,
119 POLICY_SOURCE_CLOUD,
120 new base::StringValue(
122 " {"
123 " \"name\": \"Google\","
124 " \"url\": \"google.com\""
125 " },"
126 "]"),
127 NULL);
128 UpdateProviderPolicy(policy);
129 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL));
132 #if defined(ENABLE_EXTENSIONS)
133 TEST_F(ManagedBookmarksPolicyHandlerTest, UnknownKeys) {
134 PolicyMap policy;
135 policy.Set(key::kManagedBookmarks,
136 POLICY_LEVEL_MANDATORY,
137 POLICY_SCOPE_USER,
138 POLICY_SOURCE_CLOUD,
139 base::JSONReader::Read("["
140 " {"
141 " \"name\": \"Google\","
142 " \"unknown\": \"should be ignored\","
143 " \"url\": \"google.com\""
144 " }"
145 "]")
146 .release(),
147 NULL);
148 UpdateProviderPolicy(policy);
149 const base::Value* pref_value = NULL;
150 EXPECT_TRUE(
151 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value));
152 ASSERT_TRUE(pref_value);
154 scoped_ptr<base::Value> expected(
155 extensions::ListBuilder()
156 .Append(extensions::DictionaryBuilder()
157 .Set("name", "Google")
158 .Set("url", "http://google.com/"))
159 .Build());
160 EXPECT_TRUE(pref_value->Equals(expected.get()));
162 #endif
164 #if defined(ENABLE_EXTENSIONS)
165 TEST_F(ManagedBookmarksPolicyHandlerTest, BadBookmark) {
166 PolicyMap policy;
167 policy.Set(key::kManagedBookmarks,
168 POLICY_LEVEL_MANDATORY,
169 POLICY_SCOPE_USER,
170 POLICY_SOURCE_CLOUD,
171 base::JSONReader::Read("["
172 " {"
173 " \"name\": \"Empty\","
174 " \"url\": \"\""
175 " },"
176 " {"
177 " \"name\": \"Invalid type\","
178 " \"url\": 4"
179 " },"
180 " {"
181 " \"name\": \"Invalid URL\","
182 " \"url\": \"?\""
183 " },"
184 " {"
185 " \"name\": \"Google\","
186 " \"url\": \"google.com\""
187 " }"
188 "]")
189 .release(),
190 NULL);
191 UpdateProviderPolicy(policy);
192 const base::Value* pref_value = NULL;
193 EXPECT_TRUE(
194 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value));
195 ASSERT_TRUE(pref_value);
197 scoped_ptr<base::Value> expected(
198 extensions::ListBuilder()
199 .Append(extensions::DictionaryBuilder()
200 .Set("name", "Google")
201 .Set("url", "http://google.com/"))
202 .Build());
203 EXPECT_TRUE(pref_value->Equals(expected.get()));
205 #endif
207 } // namespace policy