1 // Copyright 2013 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 "chrome/browser/policy/configuration_policy_pref_store_test.h"
7 #include "chrome/browser/search_engines/default_search_policy_handler.h"
8 #include "chrome/common/pref_names.h"
9 #include "components/policy/core/browser/configuration_policy_pref_store.h"
10 #include "policy/policy_constants.h"
14 class DefaultSearchPolicyHandlerTest
15 : public ConfigurationPolicyPrefStoreTest
{
17 DefaultSearchPolicyHandlerTest() {
18 default_alternate_urls_
.AppendString(
19 "http://www.google.com/#q={searchTerms}");
20 default_alternate_urls_
.AppendString(
21 "http://www.google.com/search#q={searchTerms}");
24 virtual void SetUp() OVERRIDE
{
25 handler_list_
.AddHandler(make_scoped_ptr
<ConfigurationPolicyHandler
>(
26 new DefaultSearchPolicyHandler
));
30 static const char kSearchURL
[];
31 static const char kSuggestURL
[];
32 static const char kIconURL
[];
33 static const char kName
[];
34 static const char kKeyword
[];
35 static const char kReplacementKey
[];
36 static const char kImageURL
[];
37 static const char kImageParams
[];
38 static const char kNewTabURL
[];
40 // Build a default search policy by setting search-related keys in |policy| to
41 // reasonable values. You can update any of the keys after calling this
43 void BuildDefaultSearchPolicy(PolicyMap
* policy
);
45 base::ListValue default_alternate_urls_
;
48 const char DefaultSearchPolicyHandlerTest::kSearchURL
[] =
49 "http://test.com/search?t={searchTerms}";
50 const char DefaultSearchPolicyHandlerTest::kSuggestURL
[] =
51 "http://test.com/sugg?={searchTerms}";
52 const char DefaultSearchPolicyHandlerTest::kIconURL
[] =
53 "http://test.com/icon.jpg";
54 const char DefaultSearchPolicyHandlerTest::kName
[] =
56 const char DefaultSearchPolicyHandlerTest::kKeyword
[] =
58 const char DefaultSearchPolicyHandlerTest::kReplacementKey
[] =
60 const char DefaultSearchPolicyHandlerTest::kImageURL
[] =
61 "http://test.com/searchbyimage/upload";
62 const char DefaultSearchPolicyHandlerTest::kImageParams
[] =
63 "image_content=content,image_url=http://test.com/test.png";
64 const char DefaultSearchPolicyHandlerTest::kNewTabURL
[] =
65 "http://test.com/newtab";
67 void DefaultSearchPolicyHandlerTest::
68 BuildDefaultSearchPolicy(PolicyMap
* policy
) {
69 base::ListValue
* encodings
= new base::ListValue();
70 encodings
->AppendString("UTF-16");
71 encodings
->AppendString("UTF-8");
72 policy
->Set(key::kDefaultSearchProviderEnabled
, POLICY_LEVEL_MANDATORY
,
73 POLICY_SCOPE_USER
, base::Value::CreateBooleanValue(true), NULL
);
74 policy
->Set(key::kDefaultSearchProviderSearchURL
, POLICY_LEVEL_MANDATORY
,
75 POLICY_SCOPE_USER
, base::Value::CreateStringValue(kSearchURL
),
77 policy
->Set(key::kDefaultSearchProviderName
, POLICY_LEVEL_MANDATORY
,
78 POLICY_SCOPE_USER
, base::Value::CreateStringValue(kName
), NULL
);
79 policy
->Set(key::kDefaultSearchProviderKeyword
, POLICY_LEVEL_MANDATORY
,
80 POLICY_SCOPE_USER
, base::Value::CreateStringValue(kKeyword
),
82 policy
->Set(key::kDefaultSearchProviderSuggestURL
, POLICY_LEVEL_MANDATORY
,
83 POLICY_SCOPE_USER
, base::Value::CreateStringValue(kSuggestURL
),
85 policy
->Set(key::kDefaultSearchProviderIconURL
, POLICY_LEVEL_MANDATORY
,
86 POLICY_SCOPE_USER
, base::Value::CreateStringValue(kIconURL
),
88 policy
->Set(key::kDefaultSearchProviderEncodings
, POLICY_LEVEL_MANDATORY
,
89 POLICY_SCOPE_USER
, encodings
, NULL
);
90 policy
->Set(key::kDefaultSearchProviderAlternateURLs
, POLICY_LEVEL_MANDATORY
,
91 POLICY_SCOPE_USER
, default_alternate_urls_
.DeepCopy(), NULL
);
92 policy
->Set(key::kDefaultSearchProviderSearchTermsReplacementKey
,
93 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
94 base::Value::CreateStringValue(kReplacementKey
), NULL
);
95 policy
->Set(key::kDefaultSearchProviderImageURL
,
96 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
97 base::Value::CreateStringValue(kImageURL
), NULL
);
98 policy
->Set(key::kDefaultSearchProviderImageURLPostParams
,
99 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
100 base::Value::CreateStringValue(kImageParams
), NULL
);
101 policy
->Set(key::kDefaultSearchProviderNewTabURL
,
102 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
103 base::Value::CreateStringValue(kNewTabURL
), NULL
);
106 // Checks that if the policy for default search is valid, i.e. there's a
107 // search URL, that all the elements have been given proper defaults.
108 TEST_F(DefaultSearchPolicyHandlerTest
, MinimallyDefined
) {
110 policy
.Set(key::kDefaultSearchProviderEnabled
, POLICY_LEVEL_MANDATORY
,
111 POLICY_SCOPE_USER
, base::Value::CreateBooleanValue(true), NULL
);
112 policy
.Set(key::kDefaultSearchProviderSearchURL
, POLICY_LEVEL_MANDATORY
,
113 POLICY_SCOPE_USER
, base::Value::CreateStringValue(kSearchURL
),
115 UpdateProviderPolicy(policy
);
117 const base::Value
* value
= NULL
;
118 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderSearchURL
, &value
));
119 EXPECT_TRUE(base::StringValue(kSearchURL
).Equals(value
));
121 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderName
, &value
));
122 EXPECT_TRUE(base::StringValue("test.com").Equals(value
));
124 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderKeyword
, &value
));
125 EXPECT_TRUE(base::StringValue("test.com").Equals(value
));
127 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderSuggestURL
,
129 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
131 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderIconURL
, &value
));
132 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
134 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderEncodings
, &value
));
135 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
137 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderInstantURL
,
139 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
141 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderAlternateURLs
,
143 EXPECT_TRUE(base::ListValue().Equals(value
));
146 store_
->GetValue(prefs::kDefaultSearchProviderSearchTermsReplacementKey
,
148 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
150 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderImageURL
, &value
));
151 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
153 EXPECT_TRUE(store_
->GetValue(
154 prefs::kDefaultSearchProviderSearchURLPostParams
, &value
));
155 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
157 EXPECT_TRUE(store_
->GetValue(
158 prefs::kDefaultSearchProviderSuggestURLPostParams
, &value
));
159 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
161 EXPECT_TRUE(store_
->GetValue(
162 prefs::kDefaultSearchProviderInstantURLPostParams
, &value
));
163 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
165 EXPECT_TRUE(store_
->GetValue(
166 prefs::kDefaultSearchProviderImageURLPostParams
, &value
));
167 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
169 EXPECT_TRUE(store_
->GetValue(
170 prefs::kDefaultSearchProviderNewTabURL
, &value
));
171 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
174 // Checks that for a fully defined search policy, all elements have been
176 TEST_F(DefaultSearchPolicyHandlerTest
, FullyDefined
) {
178 BuildDefaultSearchPolicy(&policy
);
179 UpdateProviderPolicy(policy
);
181 const base::Value
* value
= NULL
;
182 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderSearchURL
, &value
));
183 EXPECT_TRUE(base::StringValue(kSearchURL
).Equals(value
));
185 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderName
, &value
));
186 EXPECT_TRUE(base::StringValue(kName
).Equals(value
));
188 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderKeyword
, &value
));
189 EXPECT_TRUE(base::StringValue(kKeyword
).Equals(value
));
191 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderSuggestURL
,
193 EXPECT_TRUE(base::StringValue(kSuggestURL
).Equals(value
));
195 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderIconURL
, &value
));
196 EXPECT_TRUE(base::StringValue(kIconURL
).Equals(value
));
198 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderEncodings
, &value
));
199 EXPECT_TRUE(base::StringValue("UTF-16;UTF-8").Equals(value
));
201 EXPECT_TRUE(store_
->GetValue(
202 prefs::kDefaultSearchProviderAlternateURLs
, &value
));
203 EXPECT_TRUE(default_alternate_urls_
.Equals(value
));
206 store_
->GetValue(prefs::kDefaultSearchProviderSearchTermsReplacementKey
,
208 EXPECT_TRUE(base::StringValue(kReplacementKey
).Equals(value
));
210 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderImageURL
, &value
));
211 EXPECT_TRUE(base::StringValue(std::string(kImageURL
)).Equals(value
));
213 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderImageURLPostParams
,
215 EXPECT_TRUE(base::StringValue(std::string(kImageParams
)).Equals(value
));
217 EXPECT_TRUE(store_
->GetValue(
218 prefs::kDefaultSearchProviderSearchURLPostParams
, &value
));
219 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
221 EXPECT_TRUE(store_
->GetValue(
222 prefs::kDefaultSearchProviderSuggestURLPostParams
, &value
));
223 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
225 EXPECT_TRUE(store_
->GetValue(
226 prefs::kDefaultSearchProviderInstantURLPostParams
, &value
));
227 EXPECT_TRUE(base::StringValue(std::string()).Equals(value
));
230 // Checks that if the default search policy is missing, that no elements of the
231 // default search policy will be present.
232 TEST_F(DefaultSearchPolicyHandlerTest
, MissingUrl
) {
234 BuildDefaultSearchPolicy(&policy
);
235 policy
.Erase(key::kDefaultSearchProviderSearchURL
);
236 UpdateProviderPolicy(policy
);
238 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderSearchURL
, NULL
));
239 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderName
, NULL
));
240 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderKeyword
, NULL
));
241 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderSuggestURL
, NULL
));
242 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderIconURL
, NULL
));
243 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderEncodings
, NULL
));
244 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderAlternateURLs
,
246 EXPECT_FALSE(store_
->GetValue(
247 prefs::kDefaultSearchProviderSearchTermsReplacementKey
, NULL
));
248 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderImageURL
, NULL
));
249 EXPECT_FALSE(store_
->GetValue(
250 prefs::kDefaultSearchProviderImageURLPostParams
, NULL
));
251 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderInstantURL
, NULL
));
252 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderNewTabURL
, NULL
));
255 // Checks that if the default search policy is invalid, that no elements of the
256 // default search policy will be present.
257 TEST_F(DefaultSearchPolicyHandlerTest
, Invalid
) {
259 BuildDefaultSearchPolicy(&policy
);
260 const char bad_search_url
[] = "http://test.com/noSearchTerms";
261 policy
.Set(key::kDefaultSearchProviderSearchURL
, POLICY_LEVEL_MANDATORY
,
263 base::Value::CreateStringValue(bad_search_url
), NULL
);
264 UpdateProviderPolicy(policy
);
266 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderSearchURL
, NULL
));
267 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderName
, NULL
));
268 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderKeyword
, NULL
));
269 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderSuggestURL
, NULL
));
270 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderIconURL
, NULL
));
271 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderEncodings
, NULL
));
272 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderAlternateURLs
,
274 EXPECT_FALSE(store_
->GetValue(
275 prefs::kDefaultSearchProviderSearchTermsReplacementKey
, NULL
));
276 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderImageURL
, NULL
));
277 EXPECT_FALSE(store_
->GetValue(
278 prefs::kDefaultSearchProviderImageURLPostParams
, NULL
));
279 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderInstantURL
, NULL
));
280 EXPECT_FALSE(store_
->GetValue(prefs::kDefaultSearchProviderNewTabURL
, NULL
));
283 // Checks that if the default search policy is invalid, that no elements of the
284 // default search policy will be present.
285 TEST_F(DefaultSearchPolicyHandlerTest
, Disabled
) {
287 policy
.Set(key::kDefaultSearchProviderEnabled
, POLICY_LEVEL_MANDATORY
,
288 POLICY_SCOPE_USER
, base::Value::CreateBooleanValue(false), NULL
);
289 UpdateProviderPolicy(policy
);
291 const base::Value
* value
= NULL
;
292 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderEnabled
, &value
));
293 base::FundamentalValue
expected_enabled(false);
294 EXPECT_TRUE(base::Value::Equals(&expected_enabled
, value
));
295 EXPECT_TRUE(store_
->GetValue(prefs::kDefaultSearchProviderSearchURL
, &value
));
296 base::StringValue
expected_search_url((std::string()));
297 EXPECT_TRUE(base::Value::Equals(&expected_search_url
, value
));
300 } // namespace policy