OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/policy/managed_bookmarks_policy_handler.h" | 5 #include "chrome/browser/policy/managed_bookmarks_policy_handler.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 handler_list_.AddHandler(base::WrapUnique<ConfigurationPolicyHandler>( | 29 handler_list_.AddHandler(base::WrapUnique<ConfigurationPolicyHandler>( |
30 new ManagedBookmarksPolicyHandler(chrome_schema))); | 30 new ManagedBookmarksPolicyHandler(chrome_schema))); |
31 } | 31 } |
32 }; | 32 }; |
33 | 33 |
34 #if defined(ENABLE_EXTENSIONS) | 34 #if defined(ENABLE_EXTENSIONS) |
35 TEST_F(ManagedBookmarksPolicyHandlerTest, ApplyPolicySettings) { | 35 TEST_F(ManagedBookmarksPolicyHandlerTest, ApplyPolicySettings) { |
36 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); | 36 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); |
37 | 37 |
38 PolicyMap policy; | 38 PolicyMap policy; |
39 policy.Set(key::kManagedBookmarks, | 39 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
40 POLICY_LEVEL_MANDATORY, | |
41 POLICY_SCOPE_USER, | |
42 POLICY_SOURCE_CLOUD, | 40 POLICY_SOURCE_CLOUD, |
43 base::JSONReader::Read("[" | 41 base::JSONReader::Read("[" |
44 // The following gets filtered out from the | 42 // The following gets filtered out from the |
45 // JSON string when parsed. | 43 // JSON string when parsed. |
46 " {" | 44 " {" |
47 " \"toplevel_name\": \"abc 123\"" | 45 " \"toplevel_name\": \"abc 123\"" |
48 " }," | 46 " }," |
49 " {" | 47 " {" |
50 " \"name\": \"Google\"," | 48 " \"name\": \"Google\"," |
51 " \"url\": \"google.com\"" | 49 " \"url\": \"google.com\"" |
(...skipping 17 matching lines...) Expand all Loading... |
69 " \"name\": \"More Stuff\"," | 67 " \"name\": \"More Stuff\"," |
70 " \"children\": [" | 68 " \"children\": [" |
71 " {" | 69 " {" |
72 " \"name\": \"Bugs\"," | 70 " \"name\": \"Bugs\"," |
73 " \"url\": \"crbug.com\"" | 71 " \"url\": \"crbug.com\"" |
74 " }" | 72 " }" |
75 " ]" | 73 " ]" |
76 " }" | 74 " }" |
77 " ]" | 75 " ]" |
78 " }" | 76 " }" |
79 "]") | 77 "]"), |
80 .release(), | 78 nullptr); |
81 NULL); | |
82 UpdateProviderPolicy(policy); | 79 UpdateProviderPolicy(policy); |
83 const base::Value* pref_value = NULL; | 80 const base::Value* pref_value = NULL; |
84 EXPECT_TRUE( | 81 EXPECT_TRUE( |
85 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value)); | 82 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value)); |
86 ASSERT_TRUE(pref_value); | 83 ASSERT_TRUE(pref_value); |
87 | 84 |
88 // Make sure the kManagedBookmarksFolderName pref is set correctly. | 85 // Make sure the kManagedBookmarksFolderName pref is set correctly. |
89 const base::Value* folder_value = NULL; | 86 const base::Value* folder_value = NULL; |
90 std::string folder_name; | 87 std::string folder_name; |
91 EXPECT_TRUE(store_->GetValue(bookmarks::prefs::kManagedBookmarksFolderName, | 88 EXPECT_TRUE(store_->GetValue(bookmarks::prefs::kManagedBookmarksFolderName, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 .Build()); | 135 .Build()); |
139 EXPECT_TRUE(pref_value->Equals(expected.get())); | 136 EXPECT_TRUE(pref_value->Equals(expected.get())); |
140 } | 137 } |
141 #endif // defined(ENABLE_EXTENSIONS) | 138 #endif // defined(ENABLE_EXTENSIONS) |
142 | 139 |
143 #if defined(ENABLE_EXTENSIONS) | 140 #if defined(ENABLE_EXTENSIONS) |
144 TEST_F(ManagedBookmarksPolicyHandlerTest, ApplyPolicySettingsNoTitle) { | 141 TEST_F(ManagedBookmarksPolicyHandlerTest, ApplyPolicySettingsNoTitle) { |
145 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); | 142 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); |
146 | 143 |
147 PolicyMap policy; | 144 PolicyMap policy; |
148 policy.Set(key::kManagedBookmarks, | 145 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
149 POLICY_LEVEL_MANDATORY, | |
150 POLICY_SCOPE_USER, | |
151 POLICY_SOURCE_CLOUD, | 146 POLICY_SOURCE_CLOUD, |
152 base::JSONReader::Read("[" | 147 base::JSONReader::Read("[" |
153 " {" | 148 " {" |
154 " \"name\": \"Google\"," | 149 " \"name\": \"Google\"," |
155 " \"url\": \"google.com\"" | 150 " \"url\": \"google.com\"" |
156 " }" | 151 " }" |
157 "]") | 152 "]"), |
158 .release(), | 153 nullptr); |
159 NULL); | |
160 UpdateProviderPolicy(policy); | 154 UpdateProviderPolicy(policy); |
161 const base::Value* pref_value = NULL; | 155 const base::Value* pref_value = NULL; |
162 EXPECT_TRUE( | 156 EXPECT_TRUE( |
163 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value)); | 157 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value)); |
164 ASSERT_TRUE(pref_value); | 158 ASSERT_TRUE(pref_value); |
165 | 159 |
166 // Make sure the kManagedBookmarksFolderName pref is set correctly. | 160 // Make sure the kManagedBookmarksFolderName pref is set correctly. |
167 const base::Value* folder_value = NULL; | 161 const base::Value* folder_value = NULL; |
168 std::string folder_name; | 162 std::string folder_name; |
169 EXPECT_TRUE(store_->GetValue(bookmarks::prefs::kManagedBookmarksFolderName, | 163 EXPECT_TRUE(store_->GetValue(bookmarks::prefs::kManagedBookmarksFolderName, |
(...skipping 10 matching lines...) Expand all Loading... |
180 .Build()) | 174 .Build()) |
181 .Build()); | 175 .Build()); |
182 EXPECT_TRUE(pref_value->Equals(expected.get())); | 176 EXPECT_TRUE(pref_value->Equals(expected.get())); |
183 } | 177 } |
184 #endif // defined(ENABLE_EXTENSIONS) | 178 #endif // defined(ENABLE_EXTENSIONS) |
185 | 179 |
186 TEST_F(ManagedBookmarksPolicyHandlerTest, WrongPolicyType) { | 180 TEST_F(ManagedBookmarksPolicyHandlerTest, WrongPolicyType) { |
187 PolicyMap policy; | 181 PolicyMap policy; |
188 // The expected type is base::ListValue, but this policy sets it as an | 182 // The expected type is base::ListValue, but this policy sets it as an |
189 // unparsed base::StringValue. Any type other than ListValue should fail. | 183 // unparsed base::StringValue. Any type other than ListValue should fail. |
190 policy.Set(key::kManagedBookmarks, | 184 policy.Set( |
191 POLICY_LEVEL_MANDATORY, | 185 key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
192 POLICY_SCOPE_USER, | 186 POLICY_SOURCE_CLOUD, |
193 POLICY_SOURCE_CLOUD, | 187 base::WrapUnique(new base::StringValue("[" |
194 new base::StringValue( | 188 " {" |
195 "[" | 189 " \"name\": \"Google\"," |
196 " {" | 190 " \"url\": \"google.com\"" |
197 " \"name\": \"Google\"," | 191 " }," |
198 " \"url\": \"google.com\"" | 192 "]")), |
199 " }," | 193 nullptr); |
200 "]"), | |
201 NULL); | |
202 UpdateProviderPolicy(policy); | 194 UpdateProviderPolicy(policy); |
203 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); | 195 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); |
204 } | 196 } |
205 | 197 |
206 #if defined(ENABLE_EXTENSIONS) | 198 #if defined(ENABLE_EXTENSIONS) |
207 TEST_F(ManagedBookmarksPolicyHandlerTest, UnknownKeys) { | 199 TEST_F(ManagedBookmarksPolicyHandlerTest, UnknownKeys) { |
208 PolicyMap policy; | 200 PolicyMap policy; |
209 policy.Set(key::kManagedBookmarks, | 201 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
210 POLICY_LEVEL_MANDATORY, | |
211 POLICY_SCOPE_USER, | |
212 POLICY_SOURCE_CLOUD, | 202 POLICY_SOURCE_CLOUD, |
213 base::JSONReader::Read("[" | 203 base::JSONReader::Read("[" |
214 " {" | 204 " {" |
215 " \"name\": \"Google\"," | 205 " \"name\": \"Google\"," |
216 " \"unknown\": \"should be ignored\"," | 206 " \"unknown\": \"should be ignored\"," |
217 " \"url\": \"google.com\"" | 207 " \"url\": \"google.com\"" |
218 " }" | 208 " }" |
219 "]") | 209 "]"), |
220 .release(), | 210 nullptr); |
221 NULL); | |
222 UpdateProviderPolicy(policy); | 211 UpdateProviderPolicy(policy); |
223 const base::Value* pref_value = NULL; | 212 const base::Value* pref_value = NULL; |
224 EXPECT_TRUE( | 213 EXPECT_TRUE( |
225 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value)); | 214 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value)); |
226 ASSERT_TRUE(pref_value); | 215 ASSERT_TRUE(pref_value); |
227 | 216 |
228 std::unique_ptr<base::Value> expected( | 217 std::unique_ptr<base::Value> expected( |
229 extensions::ListBuilder() | 218 extensions::ListBuilder() |
230 .Append(extensions::DictionaryBuilder() | 219 .Append(extensions::DictionaryBuilder() |
231 .Set("name", "Google") | 220 .Set("name", "Google") |
232 .Set("url", "http://google.com/") | 221 .Set("url", "http://google.com/") |
233 .Build()) | 222 .Build()) |
234 .Build()); | 223 .Build()); |
235 EXPECT_TRUE(pref_value->Equals(expected.get())); | 224 EXPECT_TRUE(pref_value->Equals(expected.get())); |
236 } | 225 } |
237 #endif | 226 #endif |
238 | 227 |
239 #if defined(ENABLE_EXTENSIONS) | 228 #if defined(ENABLE_EXTENSIONS) |
240 TEST_F(ManagedBookmarksPolicyHandlerTest, BadBookmark) { | 229 TEST_F(ManagedBookmarksPolicyHandlerTest, BadBookmark) { |
241 PolicyMap policy; | 230 PolicyMap policy; |
242 policy.Set(key::kManagedBookmarks, | 231 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
243 POLICY_LEVEL_MANDATORY, | |
244 POLICY_SCOPE_USER, | |
245 POLICY_SOURCE_CLOUD, | 232 POLICY_SOURCE_CLOUD, |
246 base::JSONReader::Read("[" | 233 base::JSONReader::Read("[" |
247 " {" | 234 " {" |
248 " \"name\": \"Empty\"," | 235 " \"name\": \"Empty\"," |
249 " \"url\": \"\"" | 236 " \"url\": \"\"" |
250 " }," | 237 " }," |
251 " {" | 238 " {" |
252 " \"name\": \"Invalid type\"," | 239 " \"name\": \"Invalid type\"," |
253 " \"url\": 4" | 240 " \"url\": 4" |
254 " }," | 241 " }," |
255 " {" | 242 " {" |
256 " \"name\": \"Invalid URL\"," | 243 " \"name\": \"Invalid URL\"," |
257 " \"url\": \"?\"" | 244 " \"url\": \"?\"" |
258 " }," | 245 " }," |
259 " {" | 246 " {" |
260 " \"name\": \"Google\"," | 247 " \"name\": \"Google\"," |
261 " \"url\": \"google.com\"" | 248 " \"url\": \"google.com\"" |
262 " }" | 249 " }" |
263 "]") | 250 "]"), |
264 .release(), | 251 nullptr); |
265 NULL); | |
266 UpdateProviderPolicy(policy); | 252 UpdateProviderPolicy(policy); |
267 const base::Value* pref_value = NULL; | 253 const base::Value* pref_value = NULL; |
268 EXPECT_TRUE( | 254 EXPECT_TRUE( |
269 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value)); | 255 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value)); |
270 ASSERT_TRUE(pref_value); | 256 ASSERT_TRUE(pref_value); |
271 | 257 |
272 std::unique_ptr<base::Value> expected( | 258 std::unique_ptr<base::Value> expected( |
273 extensions::ListBuilder() | 259 extensions::ListBuilder() |
274 .Append(extensions::DictionaryBuilder() | 260 .Append(extensions::DictionaryBuilder() |
275 .Set("name", "Google") | 261 .Set("name", "Google") |
276 .Set("url", "http://google.com/") | 262 .Set("url", "http://google.com/") |
277 .Build()) | 263 .Build()) |
278 .Build()); | 264 .Build()); |
279 EXPECT_TRUE(pref_value->Equals(expected.get())); | 265 EXPECT_TRUE(pref_value->Equals(expected.get())); |
280 } | 266 } |
281 #endif | 267 #endif |
282 | 268 |
283 } // namespace policy | 269 } // namespace policy |
OLD | NEW |