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"
20 class ManagedBookmarksPolicyHandlerTest
21 : public ConfigurationPolicyPrefStoreTest
{
22 virtual 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
));
34 policy
.Set(key::kManagedBookmarks
,
35 POLICY_LEVEL_MANDATORY
,
37 base::JSONReader::Read(
40 " \"name\": \"Google\","
41 " \"url\": \"google.com\""
44 " \"name\": \"Empty Folder\","
48 " \"name\": \"Big Folder\","
51 " \"name\": \"Youtube\","
52 " \"url\": \"youtube.com\""
55 " \"name\": \"Chromium\","
56 " \"url\": \"chromium.org\""
59 " \"name\": \"More Stuff\","
62 " \"name\": \"Bugs\","
63 " \"url\": \"crbug.com\""
71 UpdateProviderPolicy(policy
);
72 const base::Value
* pref_value
= NULL
;
74 store_
->GetValue(bookmarks::prefs::kManagedBookmarks
, &pref_value
));
75 ASSERT_TRUE(pref_value
);
77 scoped_ptr
<base::Value
> expected(
78 extensions::ListBuilder()
79 .Append(extensions::DictionaryBuilder()
80 .Set("name", "Google")
81 .Set("url", "http://google.com/"))
82 .Append(extensions::DictionaryBuilder()
83 .Set("name", "Empty Folder")
84 .Set("children", extensions::ListBuilder().Pass()))
85 .Append(extensions::DictionaryBuilder()
86 .Set("name", "Big Folder")
87 .Set("children", extensions::ListBuilder()
88 .Append(extensions::DictionaryBuilder()
89 .Set("name", "Youtube")
90 .Set("url", "http://youtube.com/"))
91 .Append(extensions::DictionaryBuilder()
92 .Set("name", "Chromium")
93 .Set("url", "http://chromium.org/"))
94 .Append(extensions::DictionaryBuilder()
95 .Set("name", "More Stuff")
96 .Set("children", extensions::ListBuilder()
97 .Append(extensions::DictionaryBuilder()
99 .Set("url", "http://crbug.com/")
106 EXPECT_TRUE(pref_value
->Equals(expected
.get()));
108 #endif // defined(ENABLE_EXTENSIONS)
110 TEST_F(ManagedBookmarksPolicyHandlerTest
, WrongPolicyType
) {
112 // The expected type is base::ListValue, but this policy sets it as an
113 // unparsed base::StringValue. Any type other than ListValue should fail.
114 policy
.Set(key::kManagedBookmarks
,
115 POLICY_LEVEL_MANDATORY
,
117 new base::StringValue(
120 " \"name\": \"Google\","
121 " \"url\": \"google.com\""
125 UpdateProviderPolicy(policy
);
126 EXPECT_FALSE(store_
->GetValue(bookmarks::prefs::kManagedBookmarks
, NULL
));
129 #if defined(ENABLE_EXTENSIONS)
130 TEST_F(ManagedBookmarksPolicyHandlerTest
, UnknownKeys
) {
132 policy
.Set(key::kManagedBookmarks
,
133 POLICY_LEVEL_MANDATORY
,
135 base::JSONReader::Read(
138 " \"name\": \"Google\","
139 " \"unknown\": \"should be ignored\","
140 " \"url\": \"google.com\""
144 UpdateProviderPolicy(policy
);
145 const base::Value
* pref_value
= NULL
;
147 store_
->GetValue(bookmarks::prefs::kManagedBookmarks
, &pref_value
));
148 ASSERT_TRUE(pref_value
);
150 scoped_ptr
<base::Value
> expected(
151 extensions::ListBuilder()
152 .Append(extensions::DictionaryBuilder()
153 .Set("name", "Google")
154 .Set("url", "http://google.com/"))
156 EXPECT_TRUE(pref_value
->Equals(expected
.get()));
160 #if defined(ENABLE_EXTENSIONS)
161 TEST_F(ManagedBookmarksPolicyHandlerTest
, BadBookmark
) {
163 policy
.Set(key::kManagedBookmarks
,
164 POLICY_LEVEL_MANDATORY
,
166 base::JSONReader::Read(
169 " \"name\": \"Empty\","
173 " \"name\": \"Invalid type\","
177 " \"name\": \"Invalid URL\","
181 " \"name\": \"Google\","
182 " \"url\": \"google.com\""
186 UpdateProviderPolicy(policy
);
187 const base::Value
* pref_value
= NULL
;
189 store_
->GetValue(bookmarks::prefs::kManagedBookmarks
, &pref_value
));
190 ASSERT_TRUE(pref_value
);
192 scoped_ptr
<base::Value
> expected(
193 extensions::ListBuilder()
194 .Append(extensions::DictionaryBuilder()
195 .Set("name", "Google")
196 .Set("url", "http://google.com/"))
198 EXPECT_TRUE(pref_value
->Equals(expected
.get()));
202 } // namespace policy