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"
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
));
35 policy
.Set(key::kManagedBookmarks
,
36 POLICY_LEVEL_MANDATORY
,
39 base::JSONReader::Read("["
41 " \"name\": \"Google\","
42 " \"url\": \"google.com\""
45 " \"name\": \"Empty Folder\","
49 " \"name\": \"Big Folder\","
52 " \"name\": \"Youtube\","
53 " \"url\": \"youtube.com\""
56 " \"name\": \"Chromium\","
57 " \"url\": \"chromium.org\""
60 " \"name\": \"More Stuff\","
63 " \"name\": \"Bugs\","
64 " \"url\": \"crbug.com\""
73 UpdateProviderPolicy(policy
);
74 const base::Value
* pref_value
= NULL
;
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()
101 .Set("url", "http://crbug.com/")
108 EXPECT_TRUE(pref_value
->Equals(expected
.get()));
110 #endif // defined(ENABLE_EXTENSIONS)
112 TEST_F(ManagedBookmarksPolicyHandlerTest
, WrongPolicyType
) {
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
,
120 new base::StringValue(
123 " \"name\": \"Google\","
124 " \"url\": \"google.com\""
128 UpdateProviderPolicy(policy
);
129 EXPECT_FALSE(store_
->GetValue(bookmarks::prefs::kManagedBookmarks
, NULL
));
132 #if defined(ENABLE_EXTENSIONS)
133 TEST_F(ManagedBookmarksPolicyHandlerTest
, UnknownKeys
) {
135 policy
.Set(key::kManagedBookmarks
,
136 POLICY_LEVEL_MANDATORY
,
139 base::JSONReader::Read("["
141 " \"name\": \"Google\","
142 " \"unknown\": \"should be ignored\","
143 " \"url\": \"google.com\""
148 UpdateProviderPolicy(policy
);
149 const base::Value
* pref_value
= NULL
;
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/"))
160 EXPECT_TRUE(pref_value
->Equals(expected
.get()));
164 #if defined(ENABLE_EXTENSIONS)
165 TEST_F(ManagedBookmarksPolicyHandlerTest
, BadBookmark
) {
167 policy
.Set(key::kManagedBookmarks
,
168 POLICY_LEVEL_MANDATORY
,
171 base::JSONReader::Read("["
173 " \"name\": \"Empty\","
177 " \"name\": \"Invalid type\","
181 " \"name\": \"Invalid URL\","
185 " \"name\": \"Google\","
186 " \"url\": \"google.com\""
191 UpdateProviderPolicy(policy
);
192 const base::Value
* pref_value
= NULL
;
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/"))
203 EXPECT_TRUE(pref_value
->Equals(expected
.get()));
207 } // namespace policy