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/search_engines/default_search_manager.h"
9 #include "components/search_engines/default_search_policy_handler.h"
10 #include "components/search_engines/search_engines_pref_names.h"
11 #include "policy/policy_constants.h"
14 // TODO(caitkp): Should we find a way to route this through DefaultSearchManager
15 // to avoid hardcoding this here?
16 const char kDefaultSearchProviderData
[] =
17 "default_search_provider_data.template_url_data";
22 class DefaultSearchPolicyHandlerTest
23 : public ConfigurationPolicyPrefStoreTest
{
25 DefaultSearchPolicyHandlerTest() {
26 default_alternate_urls_
.AppendString(
27 "http://www.google.com/#q={searchTerms}");
28 default_alternate_urls_
.AppendString(
29 "http://www.google.com/search#q={searchTerms}");
32 void SetUp() override
{
33 handler_list_
.AddHandler(make_scoped_ptr
<ConfigurationPolicyHandler
>(
34 new DefaultSearchPolicyHandler
));
38 static const char kSearchURL
[];
39 static const char kSuggestURL
[];
40 static const char kIconURL
[];
41 static const char kName
[];
42 static const char kKeyword
[];
43 static const char kReplacementKey
[];
44 static const char kImageURL
[];
45 static const char kImageParams
[];
46 static const char kNewTabURL
[];
47 static const char kFileSearchURL
[];
48 static const char kHostName
[];
50 // Build a default search policy by setting search-related keys in |policy| to
51 // reasonable values. You can update any of the keys after calling this
53 void BuildDefaultSearchPolicy(PolicyMap
* policy
);
55 base::ListValue default_alternate_urls_
;
58 const char DefaultSearchPolicyHandlerTest::kSearchURL
[] =
59 "http://test.com/search?t={searchTerms}";
60 const char DefaultSearchPolicyHandlerTest::kSuggestURL
[] =
61 "http://test.com/sugg?={searchTerms}";
62 const char DefaultSearchPolicyHandlerTest::kIconURL
[] =
63 "http://test.com/icon.jpg";
64 const char DefaultSearchPolicyHandlerTest::kName
[] =
66 const char DefaultSearchPolicyHandlerTest::kKeyword
[] =
68 const char DefaultSearchPolicyHandlerTest::kReplacementKey
[] =
70 const char DefaultSearchPolicyHandlerTest::kImageURL
[] =
71 "http://test.com/searchbyimage/upload";
72 const char DefaultSearchPolicyHandlerTest::kImageParams
[] =
73 "image_content=content,image_url=http://test.com/test.png";
74 const char DefaultSearchPolicyHandlerTest::kNewTabURL
[] =
75 "http://test.com/newtab";
76 const char DefaultSearchPolicyHandlerTest::kFileSearchURL
[] =
77 "file:///c:/path/to/search?t={searchTerms}";
78 const char DefaultSearchPolicyHandlerTest::kHostName
[] = "test.com";
80 void DefaultSearchPolicyHandlerTest::
81 BuildDefaultSearchPolicy(PolicyMap
* policy
) {
82 base::ListValue
* encodings
= new base::ListValue();
83 encodings
->AppendString("UTF-16");
84 encodings
->AppendString("UTF-8");
85 policy
->Set(key::kDefaultSearchProviderEnabled
,
86 POLICY_LEVEL_MANDATORY
,
88 new base::FundamentalValue(true),
90 policy
->Set(key::kDefaultSearchProviderSearchURL
,
91 POLICY_LEVEL_MANDATORY
,
93 new base::StringValue(kSearchURL
),
95 policy
->Set(key::kDefaultSearchProviderName
,
96 POLICY_LEVEL_MANDATORY
,
98 new base::StringValue(kName
),
100 policy
->Set(key::kDefaultSearchProviderKeyword
,
101 POLICY_LEVEL_MANDATORY
,
103 new base::StringValue(kKeyword
),
105 policy
->Set(key::kDefaultSearchProviderSuggestURL
,
106 POLICY_LEVEL_MANDATORY
,
108 new base::StringValue(kSuggestURL
),
110 policy
->Set(key::kDefaultSearchProviderIconURL
,
111 POLICY_LEVEL_MANDATORY
,
113 new base::StringValue(kIconURL
),
115 policy
->Set(key::kDefaultSearchProviderEncodings
, POLICY_LEVEL_MANDATORY
,
116 POLICY_SCOPE_USER
, encodings
, NULL
);
117 policy
->Set(key::kDefaultSearchProviderAlternateURLs
, POLICY_LEVEL_MANDATORY
,
118 POLICY_SCOPE_USER
, default_alternate_urls_
.DeepCopy(), NULL
);
119 policy
->Set(key::kDefaultSearchProviderSearchTermsReplacementKey
,
120 POLICY_LEVEL_MANDATORY
,
122 new base::StringValue(kReplacementKey
),
124 policy
->Set(key::kDefaultSearchProviderImageURL
,
125 POLICY_LEVEL_MANDATORY
,
127 new base::StringValue(kImageURL
),
129 policy
->Set(key::kDefaultSearchProviderImageURLPostParams
,
130 POLICY_LEVEL_MANDATORY
,
132 new base::StringValue(kImageParams
),
134 policy
->Set(key::kDefaultSearchProviderNewTabURL
,
135 POLICY_LEVEL_MANDATORY
,
137 new base::StringValue(kNewTabURL
),
141 // Checks that if the default search policy is missing, that no elements of the
142 // default search policy will be present.
143 TEST_F(DefaultSearchPolicyHandlerTest
, MissingUrl
) {
145 BuildDefaultSearchPolicy(&policy
);
146 policy
.Erase(key::kDefaultSearchProviderSearchURL
);
147 UpdateProviderPolicy(policy
);
149 const base::Value
* temp
= nullptr;
150 EXPECT_FALSE(store_
->GetValue(kDefaultSearchProviderData
, &temp
));
153 // Checks that if the default search policy is invalid, that no elements of the
154 // default search policy will be present.
155 TEST_F(DefaultSearchPolicyHandlerTest
, Invalid
) {
157 BuildDefaultSearchPolicy(&policy
);
158 const char bad_search_url
[] = "http://test.com/noSearchTerms";
159 policy
.Set(key::kDefaultSearchProviderSearchURL
, POLICY_LEVEL_MANDATORY
,
160 POLICY_SCOPE_USER
, new base::StringValue(bad_search_url
), NULL
);
161 UpdateProviderPolicy(policy
);
163 const base::Value
* temp
= nullptr;
164 EXPECT_FALSE(store_
->GetValue(kDefaultSearchProviderData
, &temp
));
167 // Checks that for a fully defined search policy, all elements have been
168 // read properly into the dictionary pref.
169 TEST_F(DefaultSearchPolicyHandlerTest
, FullyDefined
) {
171 BuildDefaultSearchPolicy(&policy
);
172 UpdateProviderPolicy(policy
);
174 const base::Value
* temp
= NULL
;
175 const base::DictionaryValue
* dictionary
;
177 const base::ListValue
* list_value
;
178 EXPECT_TRUE(store_
->GetValue(kDefaultSearchProviderData
, &temp
));
179 temp
->GetAsDictionary(&dictionary
);
181 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kURL
, &value
));
182 EXPECT_EQ(kSearchURL
, value
);
183 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kShortName
, &value
));
184 EXPECT_EQ(kName
, value
);
185 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kKeyword
, &value
));
186 EXPECT_EQ(kKeyword
, value
);
189 dictionary
->GetString(DefaultSearchManager::kSuggestionsURL
, &value
));
190 EXPECT_EQ(kSuggestURL
, value
);
191 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kFaviconURL
, &value
));
192 EXPECT_EQ(kIconURL
, value
);
194 base::ListValue encodings
;
195 encodings
.AppendString("UTF-16");
196 encodings
.AppendString("UTF-8");
199 dictionary
->GetList(DefaultSearchManager::kInputEncodings
, &list_value
));
200 EXPECT_TRUE(encodings
.Equals(list_value
));
203 dictionary
->GetList(DefaultSearchManager::kAlternateURLs
, &list_value
));
204 EXPECT_TRUE(default_alternate_urls_
.Equals(list_value
));
206 EXPECT_TRUE(dictionary
->GetString(
207 DefaultSearchManager::kSearchTermsReplacementKey
, &value
));
208 EXPECT_EQ(kReplacementKey
, value
);
210 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kImageURL
, &value
));
211 EXPECT_EQ(kImageURL
, value
);
214 dictionary
->GetString(DefaultSearchManager::kImageURLPostParams
, &value
));
215 EXPECT_EQ(kImageParams
, value
);
217 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kSearchURLPostParams
,
219 EXPECT_EQ(std::string(), value
);
221 EXPECT_TRUE(dictionary
->GetString(
222 DefaultSearchManager::kSuggestionsURLPostParams
, &value
));
223 EXPECT_EQ(std::string(), value
);
225 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kInstantURLPostParams
,
227 EXPECT_EQ(std::string(), value
);
230 // Checks that disabling default search is properly reflected the dictionary
232 TEST_F(DefaultSearchPolicyHandlerTest
, Disabled
) {
234 policy
.Set(key::kDefaultSearchProviderEnabled
,
235 POLICY_LEVEL_MANDATORY
,
237 new base::FundamentalValue(false),
239 UpdateProviderPolicy(policy
);
240 const base::Value
* temp
= NULL
;
241 const base::DictionaryValue
* dictionary
;
242 EXPECT_TRUE(store_
->GetValue(kDefaultSearchProviderData
, &temp
));
243 temp
->GetAsDictionary(&dictionary
);
244 bool disabled
= false;
245 EXPECT_TRUE(dictionary
->GetBoolean(DefaultSearchManager::kDisabledByPolicy
,
247 EXPECT_TRUE(disabled
);
250 // Checks that if the policy for default search is valid, i.e. there's a
251 // search URL, that all the elements have been given proper defaults.
252 TEST_F(DefaultSearchPolicyHandlerTest
, MinimallyDefined
) {
254 policy
.Set(key::kDefaultSearchProviderEnabled
,
255 POLICY_LEVEL_MANDATORY
,
257 new base::FundamentalValue(true),
259 policy
.Set(key::kDefaultSearchProviderSearchURL
,
260 POLICY_LEVEL_MANDATORY
,
262 new base::StringValue(kSearchURL
),
264 UpdateProviderPolicy(policy
);
266 const base::Value
* temp
= NULL
;
267 const base::DictionaryValue
* dictionary
;
269 const base::ListValue
* list_value
;
270 EXPECT_TRUE(store_
->GetValue(kDefaultSearchProviderData
, &temp
));
271 temp
->GetAsDictionary(&dictionary
);
273 // Name and keyword should be derived from host.
274 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kURL
, &value
));
275 EXPECT_EQ(kSearchURL
, value
);
276 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kShortName
, &value
));
277 EXPECT_EQ(kHostName
, value
);
278 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kKeyword
, &value
));
279 EXPECT_EQ(kHostName
, value
);
281 // Everything else should be set to the default value.
283 dictionary
->GetString(DefaultSearchManager::kSuggestionsURL
, &value
));
284 EXPECT_EQ(std::string(), value
);
285 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kFaviconURL
, &value
));
286 EXPECT_EQ(std::string(), value
);
288 dictionary
->GetList(DefaultSearchManager::kInputEncodings
, &list_value
));
289 EXPECT_TRUE(base::ListValue().Equals(list_value
));
291 dictionary
->GetList(DefaultSearchManager::kAlternateURLs
, &list_value
));
292 EXPECT_TRUE(base::ListValue().Equals(list_value
));
293 EXPECT_TRUE(dictionary
->GetString(
294 DefaultSearchManager::kSearchTermsReplacementKey
, &value
));
295 EXPECT_EQ(std::string(), value
);
296 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kImageURL
, &value
));
297 EXPECT_EQ(std::string(), value
);
299 dictionary
->GetString(DefaultSearchManager::kImageURLPostParams
, &value
));
300 EXPECT_EQ(std::string(), value
);
301 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kSearchURLPostParams
,
303 EXPECT_EQ(std::string(), value
);
304 EXPECT_TRUE(dictionary
->GetString(
305 DefaultSearchManager::kSuggestionsURLPostParams
, &value
));
306 EXPECT_EQ(std::string(), value
);
307 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kInstantURLPostParams
,
309 EXPECT_EQ(std::string(), value
);
312 // Checks that setting a file URL as the default search is reflected properly in
313 // the dictionary pref.
314 TEST_F(DefaultSearchPolicyHandlerTest
, FileURL
) {
316 policy
.Set(key::kDefaultSearchProviderEnabled
,
317 POLICY_LEVEL_MANDATORY
,
319 new base::FundamentalValue(true),
321 policy
.Set(key::kDefaultSearchProviderSearchURL
,
322 POLICY_LEVEL_MANDATORY
,
324 new base::StringValue(kFileSearchURL
),
326 UpdateProviderPolicy(policy
);
328 const base::Value
* temp
= NULL
;
329 const base::DictionaryValue
* dictionary
;
332 EXPECT_TRUE(store_
->GetValue(kDefaultSearchProviderData
, &temp
));
333 temp
->GetAsDictionary(&dictionary
);
335 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kURL
, &value
));
336 EXPECT_EQ(kFileSearchURL
, value
);
337 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kShortName
, &value
));
338 EXPECT_EQ("_", value
);
339 EXPECT_TRUE(dictionary
->GetString(DefaultSearchManager::kKeyword
, &value
));
340 EXPECT_EQ("_", value
);
342 } // namespace policy