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 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
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
35 base::JSONReader::DeprecatedRead(
38 " \"name\": \"Google\","
39 " \"url\": \"google.com\""
42 " \"name\": \"Empty Folder\","
46 " \"name\": \"Big Folder\","
49 " \"name\": \"Youtube\","
50 " \"url\": \"youtube.com\""
53 " \"name\": \"Chromium\","
54 " \"url\": \"chromium.org\""
57 " \"name\": \"More Stuff\","
60 " \"name\": \"Bugs\","
61 " \"url\": \"crbug.com\""
69 UpdateProviderPolicy(policy
);
70 const base::Value
* pref_value
= NULL
;
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()
97 .Set("url", "http://crbug.com/")
104 EXPECT_TRUE(pref_value
->Equals(expected
.get()));
106 #endif // defined(ENABLE_EXTENSIONS)
108 TEST_F(ManagedBookmarksPolicyHandlerTest
, WrongPolicyType
) {
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
,
115 new base::StringValue(
118 " \"name\": \"Google\","
119 " \"url\": \"google.com\""
123 UpdateProviderPolicy(policy
);
124 EXPECT_FALSE(store_
->GetValue(bookmarks::prefs::kManagedBookmarks
, NULL
));
127 #if defined(ENABLE_EXTENSIONS)
128 TEST_F(ManagedBookmarksPolicyHandlerTest
, UnknownKeys
) {
130 policy
.Set(key::kManagedBookmarks
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
131 base::JSONReader::DeprecatedRead(
134 " \"name\": \"Google\","
135 " \"unknown\": \"should be ignored\","
136 " \"url\": \"google.com\""
140 UpdateProviderPolicy(policy
);
141 const base::Value
* pref_value
= NULL
;
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/"))
152 EXPECT_TRUE(pref_value
->Equals(expected
.get()));
156 #if defined(ENABLE_EXTENSIONS)
157 TEST_F(ManagedBookmarksPolicyHandlerTest
, BadBookmark
) {
159 policy
.Set(key::kManagedBookmarks
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
160 base::JSONReader::DeprecatedRead(
163 " \"name\": \"Empty\","
167 " \"name\": \"Invalid type\","
171 " \"name\": \"Invalid URL\","
175 " \"name\": \"Google\","
176 " \"url\": \"google.com\""
180 UpdateProviderPolicy(policy
);
181 const base::Value
* pref_value
= NULL
;
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/"))
192 EXPECT_TRUE(pref_value
->Equals(expected
.get()));
196 } // namespace policy