OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 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 "extensions/common/value_builder.h" |
| 13 #include "policy/policy_constants.h" |
| 14 |
| 15 namespace policy { |
| 16 |
| 17 class ManagedBookmarksPolicyHandlerTest |
| 18 : public ConfigurationPolicyPrefStoreTest { |
| 19 virtual void SetUp() OVERRIDE { |
| 20 Schema chrome_schema = Schema::Wrap(GetChromeSchemaData()); |
| 21 handler_list_.AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>( |
| 22 new ManagedBookmarksPolicyHandler(chrome_schema))); |
| 23 } |
| 24 }; |
| 25 |
| 26 TEST_F(ManagedBookmarksPolicyHandlerTest, ApplyPolicySettings) { |
| 27 EXPECT_FALSE(store_->GetValue(prefs::kManagedBookmarks, NULL)); |
| 28 |
| 29 PolicyMap policy; |
| 30 policy.Set(key::kManagedBookmarks, |
| 31 POLICY_LEVEL_MANDATORY, |
| 32 POLICY_SCOPE_USER, |
| 33 base::JSONReader::Read( |
| 34 "[" |
| 35 " {" |
| 36 " \"name\": \"Google\"," |
| 37 " \"url\": \"google.com\"" |
| 38 " }," |
| 39 " {" |
| 40 " \"name\": \"Empty Folder\"," |
| 41 " \"children\": []" |
| 42 " }," |
| 43 " {" |
| 44 " \"name\": \"Big Folder\"," |
| 45 " \"children\": [" |
| 46 " {" |
| 47 " \"name\": \"Youtube\"," |
| 48 " \"url\": \"youtube.com\"" |
| 49 " }," |
| 50 " {" |
| 51 " \"name\": \"Chromium\"," |
| 52 " \"url\": \"chromium.org\"" |
| 53 " }," |
| 54 " {" |
| 55 " \"name\": \"More Stuff\"," |
| 56 " \"children\": [" |
| 57 " {" |
| 58 " \"name\": \"Bugs\"," |
| 59 " \"url\": \"crbug.com\"" |
| 60 " }" |
| 61 " ]" |
| 62 " }" |
| 63 " ]" |
| 64 " }" |
| 65 "]"), |
| 66 NULL); |
| 67 UpdateProviderPolicy(policy); |
| 68 const base::Value* pref_value = NULL; |
| 69 EXPECT_TRUE(store_->GetValue(prefs::kManagedBookmarks, &pref_value)); |
| 70 ASSERT_TRUE(pref_value); |
| 71 |
| 72 scoped_ptr<base::Value> expected( |
| 73 extensions::ListBuilder() |
| 74 .Append(extensions::DictionaryBuilder() |
| 75 .Set("name", "Google") |
| 76 .Set("url", "http://google.com/")) |
| 77 .Append(extensions::DictionaryBuilder() |
| 78 .Set("name", "Empty Folder") |
| 79 .Set("children", extensions::ListBuilder().Pass())) |
| 80 .Append(extensions::DictionaryBuilder() |
| 81 .Set("name", "Big Folder") |
| 82 .Set("children", extensions::ListBuilder() |
| 83 .Append(extensions::DictionaryBuilder() |
| 84 .Set("name", "Youtube") |
| 85 .Set("url", "http://youtube.com/")) |
| 86 .Append(extensions::DictionaryBuilder() |
| 87 .Set("name", "Chromium") |
| 88 .Set("url", "http://chromium.org/")) |
| 89 .Append(extensions::DictionaryBuilder() |
| 90 .Set("name", "More Stuff") |
| 91 .Set("children", extensions::ListBuilder() |
| 92 .Append(extensions::DictionaryBuilder() |
| 93 .Set("name", "Bugs") |
| 94 .Set("url", "http://crbug.com/") |
| 95 .Pass()) |
| 96 .Pass()) |
| 97 .Pass()) |
| 98 .Pass()) |
| 99 .Pass()) |
| 100 .Build()); |
| 101 EXPECT_TRUE(pref_value->Equals(expected.get())); |
| 102 } |
| 103 |
| 104 TEST_F(ManagedBookmarksPolicyHandlerTest, WrongPolicyType) { |
| 105 PolicyMap policy; |
| 106 // The expected type is base::ListValue, but this policy sets it as an |
| 107 // unparsed base::StringValue. Any type other than ListValue should fail. |
| 108 policy.Set(key::kManagedBookmarks, |
| 109 POLICY_LEVEL_MANDATORY, |
| 110 POLICY_SCOPE_USER, |
| 111 new base::StringValue( |
| 112 "[" |
| 113 " {" |
| 114 " \"name\": \"Google\"," |
| 115 " \"url\": \"google.com\"" |
| 116 " }," |
| 117 "]"), |
| 118 NULL); |
| 119 UpdateProviderPolicy(policy); |
| 120 EXPECT_FALSE(store_->GetValue(prefs::kManagedBookmarks, NULL)); |
| 121 } |
| 122 |
| 123 TEST_F(ManagedBookmarksPolicyHandlerTest, UnknownKeys) { |
| 124 PolicyMap policy; |
| 125 policy.Set(key::kManagedBookmarks, |
| 126 POLICY_LEVEL_MANDATORY, |
| 127 POLICY_SCOPE_USER, |
| 128 base::JSONReader::Read( |
| 129 "[" |
| 130 " {" |
| 131 " \"name\": \"Google\"," |
| 132 " \"unknown\": \"should be ignored\"," |
| 133 " \"url\": \"google.com\"" |
| 134 " }" |
| 135 "]"), |
| 136 NULL); |
| 137 UpdateProviderPolicy(policy); |
| 138 const base::Value* pref_value = NULL; |
| 139 EXPECT_TRUE(store_->GetValue(prefs::kManagedBookmarks, &pref_value)); |
| 140 ASSERT_TRUE(pref_value); |
| 141 |
| 142 scoped_ptr<base::Value> expected( |
| 143 extensions::ListBuilder() |
| 144 .Append(extensions::DictionaryBuilder() |
| 145 .Set("name", "Google") |
| 146 .Set("url", "http://google.com/")) |
| 147 .Build()); |
| 148 EXPECT_TRUE(pref_value->Equals(expected.get())); |
| 149 } |
| 150 |
| 151 TEST_F(ManagedBookmarksPolicyHandlerTest, BadBookmark) { |
| 152 PolicyMap policy; |
| 153 policy.Set(key::kManagedBookmarks, |
| 154 POLICY_LEVEL_MANDATORY, |
| 155 POLICY_SCOPE_USER, |
| 156 base::JSONReader::Read( |
| 157 "[" |
| 158 " {" |
| 159 " \"name\": \"Empty\"," |
| 160 " \"url\": \"\"" |
| 161 " }," |
| 162 " {" |
| 163 " \"name\": \"Invalid type\"," |
| 164 " \"url\": 4" |
| 165 " }," |
| 166 " {" |
| 167 " \"name\": \"Invalid URL\"," |
| 168 " \"url\": \"?\"" |
| 169 " }," |
| 170 " {" |
| 171 " \"name\": \"Google\"," |
| 172 " \"url\": \"google.com\"" |
| 173 " }" |
| 174 "]"), |
| 175 NULL); |
| 176 UpdateProviderPolicy(policy); |
| 177 const base::Value* pref_value = NULL; |
| 178 EXPECT_TRUE(store_->GetValue(prefs::kManagedBookmarks, &pref_value)); |
| 179 ASSERT_TRUE(pref_value); |
| 180 |
| 181 scoped_ptr<base::Value> expected( |
| 182 extensions::ListBuilder() |
| 183 .Append(extensions::DictionaryBuilder() |
| 184 .Set("name", "Google") |
| 185 .Set("url", "http://google.com/")) |
| 186 .Build()); |
| 187 EXPECT_TRUE(pref_value->Equals(expected.get())); |
| 188 } |
| 189 |
| 190 } // namespace policy |
OLD | NEW |