Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / components / search_engines / default_search_policy_handler_unittest.cc
blobc4fdea213f890930d25211d71637daac03a569c4
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/memory/scoped_ptr.h"
6 #include "components/policy/core/browser/configuration_policy_pref_store.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"
10 #include "components/search_engines/default_search_policy_handler.h"
11 #include "components/search_engines/search_engines_pref_names.h"
12 #include "policy/policy_constants.h"
14 namespace {
15 // TODO(caitkp): Should we find a way to route this through DefaultSearchManager
16 // to avoid hardcoding this here?
17 const char kDefaultSearchProviderData[] =
18 "default_search_provider_data.template_url_data";
19 } // namespace
21 namespace policy {
23 class DefaultSearchPolicyHandlerTest
24 : public ConfigurationPolicyPrefStoreTest {
25 public:
26 DefaultSearchPolicyHandlerTest() {
27 default_alternate_urls_.AppendString(
28 "http://www.google.com/#q={searchTerms}");
29 default_alternate_urls_.AppendString(
30 "http://www.google.com/search#q={searchTerms}");
33 void SetUp() override {
34 handler_list_.AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>(
35 new DefaultSearchPolicyHandler));
38 protected:
39 static const char kSearchURL[];
40 static const char kSuggestURL[];
41 static const char kIconURL[];
42 static const char kName[];
43 static const char kKeyword[];
44 static const char kReplacementKey[];
45 static const char kImageURL[];
46 static const char kImageParams[];
47 static const char kNewTabURL[];
48 static const char kFileSearchURL[];
49 static const char kHostName[];
51 // Build a default search policy by setting search-related keys in |policy| to
52 // reasonable values. You can update any of the keys after calling this
53 // method.
54 void BuildDefaultSearchPolicy(PolicyMap* policy);
56 base::ListValue default_alternate_urls_;
59 const char DefaultSearchPolicyHandlerTest::kSearchURL[] =
60 "http://test.com/search?t={searchTerms}";
61 const char DefaultSearchPolicyHandlerTest::kSuggestURL[] =
62 "http://test.com/sugg?={searchTerms}";
63 const char DefaultSearchPolicyHandlerTest::kIconURL[] =
64 "http://test.com/icon.jpg";
65 const char DefaultSearchPolicyHandlerTest::kName[] =
66 "MyName";
67 const char DefaultSearchPolicyHandlerTest::kKeyword[] =
68 "MyKeyword";
69 const char DefaultSearchPolicyHandlerTest::kReplacementKey[] =
70 "espv";
71 const char DefaultSearchPolicyHandlerTest::kImageURL[] =
72 "http://test.com/searchbyimage/upload";
73 const char DefaultSearchPolicyHandlerTest::kImageParams[] =
74 "image_content=content,image_url=http://test.com/test.png";
75 const char DefaultSearchPolicyHandlerTest::kNewTabURL[] =
76 "http://test.com/newtab";
77 const char DefaultSearchPolicyHandlerTest::kFileSearchURL[] =
78 "file:///c:/path/to/search?t={searchTerms}";
79 const char DefaultSearchPolicyHandlerTest::kHostName[] = "test.com";
81 void DefaultSearchPolicyHandlerTest::
82 BuildDefaultSearchPolicy(PolicyMap* policy) {
83 base::ListValue* encodings = new base::ListValue();
84 encodings->AppendString("UTF-16");
85 encodings->AppendString("UTF-8");
86 policy->Set(key::kDefaultSearchProviderEnabled,
87 POLICY_LEVEL_MANDATORY,
88 POLICY_SCOPE_USER,
89 POLICY_SOURCE_CLOUD,
90 new base::FundamentalValue(true),
91 NULL);
92 policy->Set(key::kDefaultSearchProviderSearchURL,
93 POLICY_LEVEL_MANDATORY,
94 POLICY_SCOPE_USER,
95 POLICY_SOURCE_CLOUD,
96 new base::StringValue(kSearchURL),
97 NULL);
98 policy->Set(key::kDefaultSearchProviderName,
99 POLICY_LEVEL_MANDATORY,
100 POLICY_SCOPE_USER,
101 POLICY_SOURCE_CLOUD,
102 new base::StringValue(kName),
103 NULL);
104 policy->Set(key::kDefaultSearchProviderKeyword,
105 POLICY_LEVEL_MANDATORY,
106 POLICY_SCOPE_USER,
107 POLICY_SOURCE_CLOUD,
108 new base::StringValue(kKeyword),
109 NULL);
110 policy->Set(key::kDefaultSearchProviderSuggestURL,
111 POLICY_LEVEL_MANDATORY,
112 POLICY_SCOPE_USER,
113 POLICY_SOURCE_CLOUD,
114 new base::StringValue(kSuggestURL),
115 NULL);
116 policy->Set(key::kDefaultSearchProviderIconURL,
117 POLICY_LEVEL_MANDATORY,
118 POLICY_SCOPE_USER,
119 POLICY_SOURCE_CLOUD,
120 new base::StringValue(kIconURL),
121 NULL);
122 policy->Set(key::kDefaultSearchProviderEncodings, POLICY_LEVEL_MANDATORY,
123 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, encodings, nullptr);
124 policy->Set(key::kDefaultSearchProviderAlternateURLs, POLICY_LEVEL_MANDATORY,
125 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
126 default_alternate_urls_.DeepCopy(), nullptr);
127 policy->Set(key::kDefaultSearchProviderSearchTermsReplacementKey,
128 POLICY_LEVEL_MANDATORY,
129 POLICY_SCOPE_USER,
130 POLICY_SOURCE_CLOUD,
131 new base::StringValue(kReplacementKey),
132 NULL);
133 policy->Set(key::kDefaultSearchProviderImageURL,
134 POLICY_LEVEL_MANDATORY,
135 POLICY_SCOPE_USER,
136 POLICY_SOURCE_CLOUD,
137 new base::StringValue(kImageURL),
138 NULL);
139 policy->Set(key::kDefaultSearchProviderImageURLPostParams,
140 POLICY_LEVEL_MANDATORY,
141 POLICY_SCOPE_USER,
142 POLICY_SOURCE_CLOUD,
143 new base::StringValue(kImageParams),
144 NULL);
145 policy->Set(key::kDefaultSearchProviderNewTabURL,
146 POLICY_LEVEL_MANDATORY,
147 POLICY_SCOPE_USER,
148 POLICY_SOURCE_CLOUD,
149 new base::StringValue(kNewTabURL),
150 NULL);
153 // Checks that if the default search policy is missing, that no elements of the
154 // default search policy will be present.
155 TEST_F(DefaultSearchPolicyHandlerTest, MissingUrl) {
156 PolicyMap policy;
157 BuildDefaultSearchPolicy(&policy);
158 policy.Erase(key::kDefaultSearchProviderSearchURL);
159 UpdateProviderPolicy(policy);
161 const base::Value* temp = nullptr;
162 EXPECT_FALSE(store_->GetValue(kDefaultSearchProviderData, &temp));
165 // Checks that if the default search policy is invalid, that no elements of the
166 // default search policy will be present.
167 TEST_F(DefaultSearchPolicyHandlerTest, Invalid) {
168 PolicyMap policy;
169 BuildDefaultSearchPolicy(&policy);
170 const char bad_search_url[] = "http://test.com/noSearchTerms";
171 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
172 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
173 new base::StringValue(bad_search_url), nullptr);
174 UpdateProviderPolicy(policy);
176 const base::Value* temp = nullptr;
177 EXPECT_FALSE(store_->GetValue(kDefaultSearchProviderData, &temp));
180 // Checks that for a fully defined search policy, all elements have been
181 // read properly into the dictionary pref.
182 TEST_F(DefaultSearchPolicyHandlerTest, FullyDefined) {
183 PolicyMap policy;
184 BuildDefaultSearchPolicy(&policy);
185 UpdateProviderPolicy(policy);
187 const base::Value* temp = NULL;
188 const base::DictionaryValue* dictionary;
189 std::string value;
190 const base::ListValue* list_value;
191 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp));
192 temp->GetAsDictionary(&dictionary);
194 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kURL, &value));
195 EXPECT_EQ(kSearchURL, value);
196 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kShortName, &value));
197 EXPECT_EQ(kName, value);
198 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kKeyword, &value));
199 EXPECT_EQ(kKeyword, value);
201 EXPECT_TRUE(
202 dictionary->GetString(DefaultSearchManager::kSuggestionsURL, &value));
203 EXPECT_EQ(kSuggestURL, value);
204 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kFaviconURL, &value));
205 EXPECT_EQ(kIconURL, value);
207 base::ListValue encodings;
208 encodings.AppendString("UTF-16");
209 encodings.AppendString("UTF-8");
211 EXPECT_TRUE(
212 dictionary->GetList(DefaultSearchManager::kInputEncodings, &list_value));
213 EXPECT_TRUE(encodings.Equals(list_value));
215 EXPECT_TRUE(
216 dictionary->GetList(DefaultSearchManager::kAlternateURLs, &list_value));
217 EXPECT_TRUE(default_alternate_urls_.Equals(list_value));
219 EXPECT_TRUE(dictionary->GetString(
220 DefaultSearchManager::kSearchTermsReplacementKey, &value));
221 EXPECT_EQ(kReplacementKey, value);
223 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kImageURL, &value));
224 EXPECT_EQ(kImageURL, value);
226 EXPECT_TRUE(
227 dictionary->GetString(DefaultSearchManager::kImageURLPostParams, &value));
228 EXPECT_EQ(kImageParams, value);
230 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kSearchURLPostParams,
231 &value));
232 EXPECT_EQ(std::string(), value);
234 EXPECT_TRUE(dictionary->GetString(
235 DefaultSearchManager::kSuggestionsURLPostParams, &value));
236 EXPECT_EQ(std::string(), value);
238 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kInstantURLPostParams,
239 &value));
240 EXPECT_EQ(std::string(), value);
243 // Checks that disabling default search is properly reflected the dictionary
244 // pref.
245 TEST_F(DefaultSearchPolicyHandlerTest, Disabled) {
246 PolicyMap policy;
247 policy.Set(key::kDefaultSearchProviderEnabled,
248 POLICY_LEVEL_MANDATORY,
249 POLICY_SCOPE_USER,
250 POLICY_SOURCE_CLOUD,
251 new base::FundamentalValue(false),
252 NULL);
253 UpdateProviderPolicy(policy);
254 const base::Value* temp = NULL;
255 const base::DictionaryValue* dictionary;
256 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp));
257 temp->GetAsDictionary(&dictionary);
258 bool disabled = false;
259 EXPECT_TRUE(dictionary->GetBoolean(DefaultSearchManager::kDisabledByPolicy,
260 &disabled));
261 EXPECT_TRUE(disabled);
264 // 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.
266 TEST_F(DefaultSearchPolicyHandlerTest, MinimallyDefined) {
267 PolicyMap policy;
268 policy.Set(key::kDefaultSearchProviderEnabled,
269 POLICY_LEVEL_MANDATORY,
270 POLICY_SCOPE_USER,
271 POLICY_SOURCE_CLOUD,
272 new base::FundamentalValue(true),
273 NULL);
274 policy.Set(key::kDefaultSearchProviderSearchURL,
275 POLICY_LEVEL_MANDATORY,
276 POLICY_SCOPE_USER,
277 POLICY_SOURCE_CLOUD,
278 new base::StringValue(kSearchURL),
279 NULL);
280 UpdateProviderPolicy(policy);
282 const base::Value* temp = NULL;
283 const base::DictionaryValue* dictionary;
284 std::string value;
285 const base::ListValue* list_value;
286 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp));
287 temp->GetAsDictionary(&dictionary);
289 // Name and keyword should be derived from host.
290 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kURL, &value));
291 EXPECT_EQ(kSearchURL, value);
292 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kShortName, &value));
293 EXPECT_EQ(kHostName, value);
294 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kKeyword, &value));
295 EXPECT_EQ(kHostName, value);
297 // Everything else should be set to the default value.
298 EXPECT_TRUE(
299 dictionary->GetString(DefaultSearchManager::kSuggestionsURL, &value));
300 EXPECT_EQ(std::string(), value);
301 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kFaviconURL, &value));
302 EXPECT_EQ(std::string(), value);
303 EXPECT_TRUE(
304 dictionary->GetList(DefaultSearchManager::kInputEncodings, &list_value));
305 EXPECT_TRUE(base::ListValue().Equals(list_value));
306 EXPECT_TRUE(
307 dictionary->GetList(DefaultSearchManager::kAlternateURLs, &list_value));
308 EXPECT_TRUE(base::ListValue().Equals(list_value));
309 EXPECT_TRUE(dictionary->GetString(
310 DefaultSearchManager::kSearchTermsReplacementKey, &value));
311 EXPECT_EQ(std::string(), value);
312 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kImageURL, &value));
313 EXPECT_EQ(std::string(), value);
314 EXPECT_TRUE(
315 dictionary->GetString(DefaultSearchManager::kImageURLPostParams, &value));
316 EXPECT_EQ(std::string(), value);
317 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kSearchURLPostParams,
318 &value));
319 EXPECT_EQ(std::string(), value);
320 EXPECT_TRUE(dictionary->GetString(
321 DefaultSearchManager::kSuggestionsURLPostParams, &value));
322 EXPECT_EQ(std::string(), value);
323 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kInstantURLPostParams,
324 &value));
325 EXPECT_EQ(std::string(), value);
328 // Checks that setting a file URL as the default search is reflected properly in
329 // the dictionary pref.
330 TEST_F(DefaultSearchPolicyHandlerTest, FileURL) {
331 PolicyMap policy;
332 policy.Set(key::kDefaultSearchProviderEnabled,
333 POLICY_LEVEL_MANDATORY,
334 POLICY_SCOPE_USER,
335 POLICY_SOURCE_CLOUD,
336 new base::FundamentalValue(true),
337 NULL);
338 policy.Set(key::kDefaultSearchProviderSearchURL,
339 POLICY_LEVEL_MANDATORY,
340 POLICY_SCOPE_USER,
341 POLICY_SOURCE_CLOUD,
342 new base::StringValue(kFileSearchURL),
343 NULL);
344 UpdateProviderPolicy(policy);
346 const base::Value* temp = NULL;
347 const base::DictionaryValue* dictionary;
348 std::string value;
350 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp));
351 temp->GetAsDictionary(&dictionary);
353 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kURL, &value));
354 EXPECT_EQ(kFileSearchURL, value);
355 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kShortName, &value));
356 EXPECT_EQ("_", value);
357 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kKeyword, &value));
358 EXPECT_EQ("_", value);
360 } // namespace policy