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"
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";
23 class DefaultSearchPolicyHandlerTest
24 : public ConfigurationPolicyPrefStoreTest
{
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
));
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
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
[] =
67 const char DefaultSearchPolicyHandlerTest::kKeyword
[] =
69 const char DefaultSearchPolicyHandlerTest::kReplacementKey
[] =
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
,
90 new base::FundamentalValue(true),
92 policy
->Set(key::kDefaultSearchProviderSearchURL
,
93 POLICY_LEVEL_MANDATORY
,
96 new base::StringValue(kSearchURL
),
98 policy
->Set(key::kDefaultSearchProviderName
,
99 POLICY_LEVEL_MANDATORY
,
102 new base::StringValue(kName
),
104 policy
->Set(key::kDefaultSearchProviderKeyword
,
105 POLICY_LEVEL_MANDATORY
,
108 new base::StringValue(kKeyword
),
110 policy
->Set(key::kDefaultSearchProviderSuggestURL
,
111 POLICY_LEVEL_MANDATORY
,
114 new base::StringValue(kSuggestURL
),
116 policy
->Set(key::kDefaultSearchProviderIconURL
,
117 POLICY_LEVEL_MANDATORY
,
120 new base::StringValue(kIconURL
),
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
,
131 new base::StringValue(kReplacementKey
),
133 policy
->Set(key::kDefaultSearchProviderImageURL
,
134 POLICY_LEVEL_MANDATORY
,
137 new base::StringValue(kImageURL
),
139 policy
->Set(key::kDefaultSearchProviderImageURLPostParams
,
140 POLICY_LEVEL_MANDATORY
,
143 new base::StringValue(kImageParams
),
145 policy
->Set(key::kDefaultSearchProviderNewTabURL
,
146 POLICY_LEVEL_MANDATORY
,
149 new base::StringValue(kNewTabURL
),
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
) {
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
) {
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
) {
184 BuildDefaultSearchPolicy(&policy
);
185 UpdateProviderPolicy(policy
);
187 const base::Value
* temp
= NULL
;
188 const base::DictionaryValue
* dictionary
;
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
);
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");
212 dictionary
->GetList(DefaultSearchManager::kInputEncodings
, &list_value
));
213 EXPECT_TRUE(encodings
.Equals(list_value
));
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
);
227 dictionary
->GetString(DefaultSearchManager::kImageURLPostParams
, &value
));
228 EXPECT_EQ(kImageParams
, value
);
230 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kSearchURLPostParams
,
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
,
240 EXPECT_EQ(std::string(), value
);
243 // Checks that disabling default search is properly reflected the dictionary
245 TEST_F(DefaultSearchPolicyHandlerTest
, Disabled
) {
247 policy
.Set(key::kDefaultSearchProviderEnabled
,
248 POLICY_LEVEL_MANDATORY
,
251 new base::FundamentalValue(false),
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
,
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
) {
268 policy
.Set(key::kDefaultSearchProviderEnabled
,
269 POLICY_LEVEL_MANDATORY
,
272 new base::FundamentalValue(true),
274 policy
.Set(key::kDefaultSearchProviderSearchURL
,
275 POLICY_LEVEL_MANDATORY
,
278 new base::StringValue(kSearchURL
),
280 UpdateProviderPolicy(policy
);
282 const base::Value
* temp
= NULL
;
283 const base::DictionaryValue
* dictionary
;
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.
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
);
304 dictionary
->GetList(DefaultSearchManager::kInputEncodings
, &list_value
));
305 EXPECT_TRUE(base::ListValue().Equals(list_value
));
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
);
315 dictionary
->GetString(DefaultSearchManager::kImageURLPostParams
, &value
));
316 EXPECT_EQ(std::string(), value
);
317 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kSearchURLPostParams
,
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
,
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
) {
332 policy
.Set(key::kDefaultSearchProviderEnabled
,
333 POLICY_LEVEL_MANDATORY
,
336 new base::FundamentalValue(true),
338 policy
.Set(key::kDefaultSearchProviderSearchURL
,
339 POLICY_LEVEL_MANDATORY
,
342 new base::StringValue(kFileSearchURL
),
344 UpdateProviderPolicy(policy
);
346 const base::Value
* temp
= NULL
;
347 const base::DictionaryValue
* dictionary
;
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