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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "components/policy/core/browser/configuration_policy_pref_store.h" | 6 #include "components/policy/core/browser/configuration_policy_pref_store.h" |
7 #include "components/policy/core/browser/configuration_policy_pref_store_test.h" | 7 #include "components/policy/core/browser/configuration_policy_pref_store_test.h" |
8 #include "components/policy/core/common/policy_types.h" | |
9 #include "components/search_engines/default_search_manager.h" | 8 #include "components/search_engines/default_search_manager.h" |
10 #include "components/search_engines/default_search_policy_handler.h" | 9 #include "components/search_engines/default_search_policy_handler.h" |
11 #include "components/search_engines/search_engines_pref_names.h" | 10 #include "components/search_engines/search_engines_pref_names.h" |
12 #include "policy/policy_constants.h" | 11 #include "policy/policy_constants.h" |
13 | 12 |
14 namespace { | 13 namespace { |
15 // TODO(caitkp): Should we find a way to route this through DefaultSearchManager | 14 // TODO(caitkp): Should we find a way to route this through DefaultSearchManager |
16 // to avoid hardcoding this here? | 15 // to avoid hardcoding this here? |
17 const char kDefaultSearchProviderData[] = | 16 const char kDefaultSearchProviderData[] = |
18 "default_search_provider_data.template_url_data"; | 17 "default_search_provider_data.template_url_data"; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 const char DefaultSearchPolicyHandlerTest::kHostName[] = "test.com"; | 78 const char DefaultSearchPolicyHandlerTest::kHostName[] = "test.com"; |
80 | 79 |
81 void DefaultSearchPolicyHandlerTest:: | 80 void DefaultSearchPolicyHandlerTest:: |
82 BuildDefaultSearchPolicy(PolicyMap* policy) { | 81 BuildDefaultSearchPolicy(PolicyMap* policy) { |
83 base::ListValue* encodings = new base::ListValue(); | 82 base::ListValue* encodings = new base::ListValue(); |
84 encodings->AppendString("UTF-16"); | 83 encodings->AppendString("UTF-16"); |
85 encodings->AppendString("UTF-8"); | 84 encodings->AppendString("UTF-8"); |
86 policy->Set(key::kDefaultSearchProviderEnabled, | 85 policy->Set(key::kDefaultSearchProviderEnabled, |
87 POLICY_LEVEL_MANDATORY, | 86 POLICY_LEVEL_MANDATORY, |
88 POLICY_SCOPE_USER, | 87 POLICY_SCOPE_USER, |
89 POLICY_SOURCE_CLOUD, | |
90 new base::FundamentalValue(true), | 88 new base::FundamentalValue(true), |
91 NULL); | 89 NULL); |
92 policy->Set(key::kDefaultSearchProviderSearchURL, | 90 policy->Set(key::kDefaultSearchProviderSearchURL, |
93 POLICY_LEVEL_MANDATORY, | 91 POLICY_LEVEL_MANDATORY, |
94 POLICY_SCOPE_USER, | 92 POLICY_SCOPE_USER, |
95 POLICY_SOURCE_CLOUD, | |
96 new base::StringValue(kSearchURL), | 93 new base::StringValue(kSearchURL), |
97 NULL); | 94 NULL); |
98 policy->Set(key::kDefaultSearchProviderName, | 95 policy->Set(key::kDefaultSearchProviderName, |
99 POLICY_LEVEL_MANDATORY, | 96 POLICY_LEVEL_MANDATORY, |
100 POLICY_SCOPE_USER, | 97 POLICY_SCOPE_USER, |
101 POLICY_SOURCE_CLOUD, | |
102 new base::StringValue(kName), | 98 new base::StringValue(kName), |
103 NULL); | 99 NULL); |
104 policy->Set(key::kDefaultSearchProviderKeyword, | 100 policy->Set(key::kDefaultSearchProviderKeyword, |
105 POLICY_LEVEL_MANDATORY, | 101 POLICY_LEVEL_MANDATORY, |
106 POLICY_SCOPE_USER, | 102 POLICY_SCOPE_USER, |
107 POLICY_SOURCE_CLOUD, | |
108 new base::StringValue(kKeyword), | 103 new base::StringValue(kKeyword), |
109 NULL); | 104 NULL); |
110 policy->Set(key::kDefaultSearchProviderSuggestURL, | 105 policy->Set(key::kDefaultSearchProviderSuggestURL, |
111 POLICY_LEVEL_MANDATORY, | 106 POLICY_LEVEL_MANDATORY, |
112 POLICY_SCOPE_USER, | 107 POLICY_SCOPE_USER, |
113 POLICY_SOURCE_CLOUD, | |
114 new base::StringValue(kSuggestURL), | 108 new base::StringValue(kSuggestURL), |
115 NULL); | 109 NULL); |
116 policy->Set(key::kDefaultSearchProviderIconURL, | 110 policy->Set(key::kDefaultSearchProviderIconURL, |
117 POLICY_LEVEL_MANDATORY, | 111 POLICY_LEVEL_MANDATORY, |
118 POLICY_SCOPE_USER, | 112 POLICY_SCOPE_USER, |
119 POLICY_SOURCE_CLOUD, | |
120 new base::StringValue(kIconURL), | 113 new base::StringValue(kIconURL), |
121 NULL); | 114 NULL); |
122 policy->Set(key::kDefaultSearchProviderEncodings, POLICY_LEVEL_MANDATORY, | 115 policy->Set(key::kDefaultSearchProviderEncodings, POLICY_LEVEL_MANDATORY, |
123 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, encodings, nullptr); | 116 POLICY_SCOPE_USER, encodings, NULL); |
124 policy->Set(key::kDefaultSearchProviderAlternateURLs, POLICY_LEVEL_MANDATORY, | 117 policy->Set(key::kDefaultSearchProviderAlternateURLs, POLICY_LEVEL_MANDATORY, |
125 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 118 POLICY_SCOPE_USER, default_alternate_urls_.DeepCopy(), NULL); |
126 default_alternate_urls_.DeepCopy(), nullptr); | |
127 policy->Set(key::kDefaultSearchProviderSearchTermsReplacementKey, | 119 policy->Set(key::kDefaultSearchProviderSearchTermsReplacementKey, |
128 POLICY_LEVEL_MANDATORY, | 120 POLICY_LEVEL_MANDATORY, |
129 POLICY_SCOPE_USER, | 121 POLICY_SCOPE_USER, |
130 POLICY_SOURCE_CLOUD, | |
131 new base::StringValue(kReplacementKey), | 122 new base::StringValue(kReplacementKey), |
132 NULL); | 123 NULL); |
133 policy->Set(key::kDefaultSearchProviderImageURL, | 124 policy->Set(key::kDefaultSearchProviderImageURL, |
134 POLICY_LEVEL_MANDATORY, | 125 POLICY_LEVEL_MANDATORY, |
135 POLICY_SCOPE_USER, | 126 POLICY_SCOPE_USER, |
136 POLICY_SOURCE_CLOUD, | |
137 new base::StringValue(kImageURL), | 127 new base::StringValue(kImageURL), |
138 NULL); | 128 NULL); |
139 policy->Set(key::kDefaultSearchProviderImageURLPostParams, | 129 policy->Set(key::kDefaultSearchProviderImageURLPostParams, |
140 POLICY_LEVEL_MANDATORY, | 130 POLICY_LEVEL_MANDATORY, |
141 POLICY_SCOPE_USER, | 131 POLICY_SCOPE_USER, |
142 POLICY_SOURCE_CLOUD, | |
143 new base::StringValue(kImageParams), | 132 new base::StringValue(kImageParams), |
144 NULL); | 133 NULL); |
145 policy->Set(key::kDefaultSearchProviderNewTabURL, | 134 policy->Set(key::kDefaultSearchProviderNewTabURL, |
146 POLICY_LEVEL_MANDATORY, | 135 POLICY_LEVEL_MANDATORY, |
147 POLICY_SCOPE_USER, | 136 POLICY_SCOPE_USER, |
148 POLICY_SOURCE_CLOUD, | |
149 new base::StringValue(kNewTabURL), | 137 new base::StringValue(kNewTabURL), |
150 NULL); | 138 NULL); |
151 } | 139 } |
152 | 140 |
153 // Checks that if the default search policy is missing, that no elements of the | 141 // Checks that if the default search policy is missing, that no elements of the |
154 // default search policy will be present. | 142 // default search policy will be present. |
155 TEST_F(DefaultSearchPolicyHandlerTest, MissingUrl) { | 143 TEST_F(DefaultSearchPolicyHandlerTest, MissingUrl) { |
156 PolicyMap policy; | 144 PolicyMap policy; |
157 BuildDefaultSearchPolicy(&policy); | 145 BuildDefaultSearchPolicy(&policy); |
158 policy.Erase(key::kDefaultSearchProviderSearchURL); | 146 policy.Erase(key::kDefaultSearchProviderSearchURL); |
159 UpdateProviderPolicy(policy); | 147 UpdateProviderPolicy(policy); |
160 | 148 |
161 const base::Value* temp = nullptr; | 149 const base::Value* temp = nullptr; |
162 EXPECT_FALSE(store_->GetValue(kDefaultSearchProviderData, &temp)); | 150 EXPECT_FALSE(store_->GetValue(kDefaultSearchProviderData, &temp)); |
163 } | 151 } |
164 | 152 |
165 // Checks that if the default search policy is invalid, that no elements of the | 153 // Checks that if the default search policy is invalid, that no elements of the |
166 // default search policy will be present. | 154 // default search policy will be present. |
167 TEST_F(DefaultSearchPolicyHandlerTest, Invalid) { | 155 TEST_F(DefaultSearchPolicyHandlerTest, Invalid) { |
168 PolicyMap policy; | 156 PolicyMap policy; |
169 BuildDefaultSearchPolicy(&policy); | 157 BuildDefaultSearchPolicy(&policy); |
170 const char bad_search_url[] = "http://test.com/noSearchTerms"; | 158 const char bad_search_url[] = "http://test.com/noSearchTerms"; |
171 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, | 159 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, |
172 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 160 POLICY_SCOPE_USER, new base::StringValue(bad_search_url), NULL); |
173 new base::StringValue(bad_search_url), nullptr); | |
174 UpdateProviderPolicy(policy); | 161 UpdateProviderPolicy(policy); |
175 | 162 |
176 const base::Value* temp = nullptr; | 163 const base::Value* temp = nullptr; |
177 EXPECT_FALSE(store_->GetValue(kDefaultSearchProviderData, &temp)); | 164 EXPECT_FALSE(store_->GetValue(kDefaultSearchProviderData, &temp)); |
178 } | 165 } |
179 | 166 |
180 // Checks that for a fully defined search policy, all elements have been | 167 // Checks that for a fully defined search policy, all elements have been |
181 // read properly into the dictionary pref. | 168 // read properly into the dictionary pref. |
182 TEST_F(DefaultSearchPolicyHandlerTest, FullyDefined) { | 169 TEST_F(DefaultSearchPolicyHandlerTest, FullyDefined) { |
183 PolicyMap policy; | 170 PolicyMap policy; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 EXPECT_EQ(std::string(), value); | 227 EXPECT_EQ(std::string(), value); |
241 } | 228 } |
242 | 229 |
243 // Checks that disabling default search is properly reflected the dictionary | 230 // Checks that disabling default search is properly reflected the dictionary |
244 // pref. | 231 // pref. |
245 TEST_F(DefaultSearchPolicyHandlerTest, Disabled) { | 232 TEST_F(DefaultSearchPolicyHandlerTest, Disabled) { |
246 PolicyMap policy; | 233 PolicyMap policy; |
247 policy.Set(key::kDefaultSearchProviderEnabled, | 234 policy.Set(key::kDefaultSearchProviderEnabled, |
248 POLICY_LEVEL_MANDATORY, | 235 POLICY_LEVEL_MANDATORY, |
249 POLICY_SCOPE_USER, | 236 POLICY_SCOPE_USER, |
250 POLICY_SOURCE_CLOUD, | |
251 new base::FundamentalValue(false), | 237 new base::FundamentalValue(false), |
252 NULL); | 238 NULL); |
253 UpdateProviderPolicy(policy); | 239 UpdateProviderPolicy(policy); |
254 const base::Value* temp = NULL; | 240 const base::Value* temp = NULL; |
255 const base::DictionaryValue* dictionary; | 241 const base::DictionaryValue* dictionary; |
256 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp)); | 242 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp)); |
257 temp->GetAsDictionary(&dictionary); | 243 temp->GetAsDictionary(&dictionary); |
258 bool disabled = false; | 244 bool disabled = false; |
259 EXPECT_TRUE(dictionary->GetBoolean(DefaultSearchManager::kDisabledByPolicy, | 245 EXPECT_TRUE(dictionary->GetBoolean(DefaultSearchManager::kDisabledByPolicy, |
260 &disabled)); | 246 &disabled)); |
261 EXPECT_TRUE(disabled); | 247 EXPECT_TRUE(disabled); |
262 } | 248 } |
263 | 249 |
264 // Checks that if the policy for default search is valid, i.e. there's a | 250 // Checks that if the policy for default search is valid, i.e. there's a |
265 // search URL, that all the elements have been given proper defaults. | 251 // search URL, that all the elements have been given proper defaults. |
266 TEST_F(DefaultSearchPolicyHandlerTest, MinimallyDefined) { | 252 TEST_F(DefaultSearchPolicyHandlerTest, MinimallyDefined) { |
267 PolicyMap policy; | 253 PolicyMap policy; |
268 policy.Set(key::kDefaultSearchProviderEnabled, | 254 policy.Set(key::kDefaultSearchProviderEnabled, |
269 POLICY_LEVEL_MANDATORY, | 255 POLICY_LEVEL_MANDATORY, |
270 POLICY_SCOPE_USER, | 256 POLICY_SCOPE_USER, |
271 POLICY_SOURCE_CLOUD, | |
272 new base::FundamentalValue(true), | 257 new base::FundamentalValue(true), |
273 NULL); | 258 NULL); |
274 policy.Set(key::kDefaultSearchProviderSearchURL, | 259 policy.Set(key::kDefaultSearchProviderSearchURL, |
275 POLICY_LEVEL_MANDATORY, | 260 POLICY_LEVEL_MANDATORY, |
276 POLICY_SCOPE_USER, | 261 POLICY_SCOPE_USER, |
277 POLICY_SOURCE_CLOUD, | |
278 new base::StringValue(kSearchURL), | 262 new base::StringValue(kSearchURL), |
279 NULL); | 263 NULL); |
280 UpdateProviderPolicy(policy); | 264 UpdateProviderPolicy(policy); |
281 | 265 |
282 const base::Value* temp = NULL; | 266 const base::Value* temp = NULL; |
283 const base::DictionaryValue* dictionary; | 267 const base::DictionaryValue* dictionary; |
284 std::string value; | 268 std::string value; |
285 const base::ListValue* list_value; | 269 const base::ListValue* list_value; |
286 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp)); | 270 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp)); |
287 temp->GetAsDictionary(&dictionary); | 271 temp->GetAsDictionary(&dictionary); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 EXPECT_EQ(std::string(), value); | 309 EXPECT_EQ(std::string(), value); |
326 } | 310 } |
327 | 311 |
328 // Checks that setting a file URL as the default search is reflected properly in | 312 // Checks that setting a file URL as the default search is reflected properly in |
329 // the dictionary pref. | 313 // the dictionary pref. |
330 TEST_F(DefaultSearchPolicyHandlerTest, FileURL) { | 314 TEST_F(DefaultSearchPolicyHandlerTest, FileURL) { |
331 PolicyMap policy; | 315 PolicyMap policy; |
332 policy.Set(key::kDefaultSearchProviderEnabled, | 316 policy.Set(key::kDefaultSearchProviderEnabled, |
333 POLICY_LEVEL_MANDATORY, | 317 POLICY_LEVEL_MANDATORY, |
334 POLICY_SCOPE_USER, | 318 POLICY_SCOPE_USER, |
335 POLICY_SOURCE_CLOUD, | |
336 new base::FundamentalValue(true), | 319 new base::FundamentalValue(true), |
337 NULL); | 320 NULL); |
338 policy.Set(key::kDefaultSearchProviderSearchURL, | 321 policy.Set(key::kDefaultSearchProviderSearchURL, |
339 POLICY_LEVEL_MANDATORY, | 322 POLICY_LEVEL_MANDATORY, |
340 POLICY_SCOPE_USER, | 323 POLICY_SCOPE_USER, |
341 POLICY_SOURCE_CLOUD, | |
342 new base::StringValue(kFileSearchURL), | 324 new base::StringValue(kFileSearchURL), |
343 NULL); | 325 NULL); |
344 UpdateProviderPolicy(policy); | 326 UpdateProviderPolicy(policy); |
345 | 327 |
346 const base::Value* temp = NULL; | 328 const base::Value* temp = NULL; |
347 const base::DictionaryValue* dictionary; | 329 const base::DictionaryValue* dictionary; |
348 std::string value; | 330 std::string value; |
349 | 331 |
350 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp)); | 332 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp)); |
351 temp->GetAsDictionary(&dictionary); | 333 temp->GetAsDictionary(&dictionary); |
352 | 334 |
353 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kURL, &value)); | 335 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kURL, &value)); |
354 EXPECT_EQ(kFileSearchURL, value); | 336 EXPECT_EQ(kFileSearchURL, value); |
355 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kShortName, &value)); | 337 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kShortName, &value)); |
356 EXPECT_EQ("_", value); | 338 EXPECT_EQ("_", value); |
357 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kKeyword, &value)); | 339 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kKeyword, &value)); |
358 EXPECT_EQ("_", value); | 340 EXPECT_EQ("_", value); |
359 } | 341 } |
360 } // namespace policy | 342 } // namespace policy |
OLD | NEW |