| 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" |
| 11 #include "components/bookmarks/common/bookmark_pref_names.h" | 11 #include "components/bookmarks/common/bookmark_pref_names.h" |
| 12 #include "components/policy/core/browser/configuration_policy_pref_store.h" | 12 #include "components/policy/core/browser/configuration_policy_pref_store.h" |
| 13 #include "components/policy/core/browser/configuration_policy_pref_store_test.h" | 13 #include "components/policy/core/browser/configuration_policy_pref_store_test.h" |
| 14 #include "components/policy/core/common/policy_map.h" | 14 #include "components/policy/core/common/policy_map.h" |
| 15 #include "components/policy/core/common/policy_types.h" | 15 #include "components/policy/core/common/policy_types.h" |
| 16 #include "components/policy/core/common/schema.h" | 16 #include "components/policy/core/common/schema.h" |
| 17 #include "components/policy/policy_constants.h" | 17 #include "components/policy/policy_constants.h" |
| 18 #include "extensions/features/features.h" |
| 18 | 19 |
| 19 #if defined(ENABLE_EXTENSIONS) | 20 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 20 #include "extensions/common/value_builder.h" | 21 #include "extensions/common/value_builder.h" |
| 21 #endif | 22 #endif |
| 22 | 23 |
| 23 namespace policy { | 24 namespace policy { |
| 24 | 25 |
| 25 class ManagedBookmarksPolicyHandlerTest | 26 class ManagedBookmarksPolicyHandlerTest |
| 26 : public ConfigurationPolicyPrefStoreTest { | 27 : public ConfigurationPolicyPrefStoreTest { |
| 27 void SetUp() override { | 28 void SetUp() override { |
| 28 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData()); | 29 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData()); |
| 29 handler_list_.AddHandler(base::WrapUnique<ConfigurationPolicyHandler>( | 30 handler_list_.AddHandler(base::WrapUnique<ConfigurationPolicyHandler>( |
| 30 new ManagedBookmarksPolicyHandler(chrome_schema))); | 31 new ManagedBookmarksPolicyHandler(chrome_schema))); |
| 31 } | 32 } |
| 32 }; | 33 }; |
| 33 | 34 |
| 34 #if defined(ENABLE_EXTENSIONS) | 35 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 35 TEST_F(ManagedBookmarksPolicyHandlerTest, ApplyPolicySettings) { | 36 TEST_F(ManagedBookmarksPolicyHandlerTest, ApplyPolicySettings) { |
| 36 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); | 37 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); |
| 37 | 38 |
| 38 PolicyMap policy; | 39 PolicyMap policy; |
| 39 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 40 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 40 POLICY_SOURCE_CLOUD, | 41 POLICY_SOURCE_CLOUD, |
| 41 base::JSONReader::Read("[" | 42 base::JSONReader::Read("[" |
| 42 // The following gets filtered out from the | 43 // The following gets filtered out from the |
| 43 // JSON string when parsed. | 44 // JSON string when parsed. |
| 44 " {" | 45 " {" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 "com" | 129 "com" |
| 129 "/") | 130 "/") |
| 130 .Build()) | 131 .Build()) |
| 131 .Build()) | 132 .Build()) |
| 132 .Build()) | 133 .Build()) |
| 133 .Build()) | 134 .Build()) |
| 134 .Build()) | 135 .Build()) |
| 135 .Build()); | 136 .Build()); |
| 136 EXPECT_TRUE(pref_value->Equals(expected.get())); | 137 EXPECT_TRUE(pref_value->Equals(expected.get())); |
| 137 } | 138 } |
| 138 #endif // defined(ENABLE_EXTENSIONS) | 139 #endif // BUILDFLAG(ENABLE_EXTENSIONS) |
| 139 | 140 |
| 140 #if defined(ENABLE_EXTENSIONS) | 141 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 141 TEST_F(ManagedBookmarksPolicyHandlerTest, ApplyPolicySettingsNoTitle) { | 142 TEST_F(ManagedBookmarksPolicyHandlerTest, ApplyPolicySettingsNoTitle) { |
| 142 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); | 143 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); |
| 143 | 144 |
| 144 PolicyMap policy; | 145 PolicyMap policy; |
| 145 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 146 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 146 POLICY_SOURCE_CLOUD, | 147 POLICY_SOURCE_CLOUD, |
| 147 base::JSONReader::Read("[" | 148 base::JSONReader::Read("[" |
| 148 " {" | 149 " {" |
| 149 " \"name\": \"Google\"," | 150 " \"name\": \"Google\"," |
| 150 " \"url\": \"google.com\"" | 151 " \"url\": \"google.com\"" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 168 | 169 |
| 169 std::unique_ptr<base::Value> expected( | 170 std::unique_ptr<base::Value> expected( |
| 170 extensions::ListBuilder() | 171 extensions::ListBuilder() |
| 171 .Append(extensions::DictionaryBuilder() | 172 .Append(extensions::DictionaryBuilder() |
| 172 .Set("name", "Google") | 173 .Set("name", "Google") |
| 173 .Set("url", "http://google.com/") | 174 .Set("url", "http://google.com/") |
| 174 .Build()) | 175 .Build()) |
| 175 .Build()); | 176 .Build()); |
| 176 EXPECT_TRUE(pref_value->Equals(expected.get())); | 177 EXPECT_TRUE(pref_value->Equals(expected.get())); |
| 177 } | 178 } |
| 178 #endif // defined(ENABLE_EXTENSIONS) | 179 #endif // BUILDFLAG(ENABLE_EXTENSIONS) |
| 179 | 180 |
| 180 TEST_F(ManagedBookmarksPolicyHandlerTest, WrongPolicyType) { | 181 TEST_F(ManagedBookmarksPolicyHandlerTest, WrongPolicyType) { |
| 181 PolicyMap policy; | 182 PolicyMap policy; |
| 182 // The expected type is base::ListValue, but this policy sets it as an | 183 // The expected type is base::ListValue, but this policy sets it as an |
| 183 // unparsed base::StringValue. Any type other than ListValue should fail. | 184 // unparsed base::StringValue. Any type other than ListValue should fail. |
| 184 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 185 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 185 POLICY_SOURCE_CLOUD, | 186 POLICY_SOURCE_CLOUD, |
| 186 base::MakeUnique<base::StringValue>("[" | 187 base::MakeUnique<base::StringValue>("[" |
| 187 " {" | 188 " {" |
| 188 " \"name\": \"Google\"," | 189 " \"name\": \"Google\"," |
| 189 " \"url\": \"google.com\"" | 190 " \"url\": \"google.com\"" |
| 190 " }," | 191 " }," |
| 191 "]"), | 192 "]"), |
| 192 nullptr); | 193 nullptr); |
| 193 UpdateProviderPolicy(policy); | 194 UpdateProviderPolicy(policy); |
| 194 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); | 195 EXPECT_FALSE(store_->GetValue(bookmarks::prefs::kManagedBookmarks, NULL)); |
| 195 } | 196 } |
| 196 | 197 |
| 197 #if defined(ENABLE_EXTENSIONS) | 198 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 198 TEST_F(ManagedBookmarksPolicyHandlerTest, UnknownKeys) { | 199 TEST_F(ManagedBookmarksPolicyHandlerTest, UnknownKeys) { |
| 199 PolicyMap policy; | 200 PolicyMap policy; |
| 200 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 201 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 201 POLICY_SOURCE_CLOUD, | 202 POLICY_SOURCE_CLOUD, |
| 202 base::JSONReader::Read("[" | 203 base::JSONReader::Read("[" |
| 203 " {" | 204 " {" |
| 204 " \"name\": \"Google\"," | 205 " \"name\": \"Google\"," |
| 205 " \"unknown\": \"should be ignored\"," | 206 " \"unknown\": \"should be ignored\"," |
| 206 " \"url\": \"google.com\"" | 207 " \"url\": \"google.com\"" |
| 207 " }" | 208 " }" |
| 208 "]"), | 209 "]"), |
| 209 nullptr); | 210 nullptr); |
| 210 UpdateProviderPolicy(policy); | 211 UpdateProviderPolicy(policy); |
| 211 const base::Value* pref_value = NULL; | 212 const base::Value* pref_value = NULL; |
| 212 EXPECT_TRUE( | 213 EXPECT_TRUE( |
| 213 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value)); | 214 store_->GetValue(bookmarks::prefs::kManagedBookmarks, &pref_value)); |
| 214 ASSERT_TRUE(pref_value); | 215 ASSERT_TRUE(pref_value); |
| 215 | 216 |
| 216 std::unique_ptr<base::Value> expected( | 217 std::unique_ptr<base::Value> expected( |
| 217 extensions::ListBuilder() | 218 extensions::ListBuilder() |
| 218 .Append(extensions::DictionaryBuilder() | 219 .Append(extensions::DictionaryBuilder() |
| 219 .Set("name", "Google") | 220 .Set("name", "Google") |
| 220 .Set("url", "http://google.com/") | 221 .Set("url", "http://google.com/") |
| 221 .Build()) | 222 .Build()) |
| 222 .Build()); | 223 .Build()); |
| 223 EXPECT_TRUE(pref_value->Equals(expected.get())); | 224 EXPECT_TRUE(pref_value->Equals(expected.get())); |
| 224 } | 225 } |
| 225 #endif | 226 #endif |
| 226 | 227 |
| 227 #if defined(ENABLE_EXTENSIONS) | 228 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 228 TEST_F(ManagedBookmarksPolicyHandlerTest, BadBookmark) { | 229 TEST_F(ManagedBookmarksPolicyHandlerTest, BadBookmark) { |
| 229 PolicyMap policy; | 230 PolicyMap policy; |
| 230 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 231 policy.Set(key::kManagedBookmarks, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 231 POLICY_SOURCE_CLOUD, | 232 POLICY_SOURCE_CLOUD, |
| 232 base::JSONReader::Read("[" | 233 base::JSONReader::Read("[" |
| 233 " {" | 234 " {" |
| 234 " \"name\": \"Empty\"," | 235 " \"name\": \"Empty\"," |
| 235 " \"url\": \"\"" | 236 " \"url\": \"\"" |
| 236 " }," | 237 " }," |
| 237 " {" | 238 " {" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 259 .Append(extensions::DictionaryBuilder() | 260 .Append(extensions::DictionaryBuilder() |
| 260 .Set("name", "Google") | 261 .Set("name", "Google") |
| 261 .Set("url", "http://google.com/") | 262 .Set("url", "http://google.com/") |
| 262 .Build()) | 263 .Build()) |
| 263 .Build()); | 264 .Build()); |
| 264 EXPECT_TRUE(pref_value->Equals(expected.get())); | 265 EXPECT_TRUE(pref_value->Equals(expected.get())); |
| 265 } | 266 } |
| 266 #endif | 267 #endif |
| 267 | 268 |
| 268 } // namespace policy | 269 } // namespace policy |
| OLD | NEW |