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/base_paths.h"
6 #include "base/command_line.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/metrics/proto/omnibox_event.pb.h"
11 #include "components/metrics/proto/omnibox_input_type.pb.h"
12 #include "components/search_engines/search_engines_switches.h"
13 #include "components/search_engines/search_terms_data.h"
14 #include "components/search_engines/template_url.h"
15 #include "components/search_engines/testing_search_terms_data.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using base::ASCIIToUTF16
;
20 class TemplateURLTest
: public testing::Test
{
22 TemplateURLTest() : search_terms_data_("http://www.google.com/") {}
23 void CheckSuggestBaseURL(const std::string
& base_url
,
24 const std::string
& base_suggest_url
) const;
26 static void ExpectPostParamIs(
27 const TemplateURLRef::PostParam
& param
,
28 const std::string
& name
,
29 const std::string
& value
,
30 const std::string
& content_type
= std::string());
32 TestingSearchTermsData search_terms_data_
;
35 void TemplateURLTest::CheckSuggestBaseURL(
36 const std::string
& base_url
,
37 const std::string
& base_suggest_url
) const {
38 TestingSearchTermsData
search_terms_data(base_url
);
39 EXPECT_EQ(base_suggest_url
, search_terms_data
.GoogleBaseSuggestURLValue());
43 void TemplateURLTest::ExpectPostParamIs(const TemplateURLRef::PostParam
& param
,
44 const std::string
& name
,
45 const std::string
& value
,
46 const std::string
& content_type
) {
47 EXPECT_EQ(name
, param
.name
);
48 EXPECT_EQ(value
, param
.value
);
49 EXPECT_EQ(content_type
, param
.content_type
);
52 TEST_F(TemplateURLTest
, Defaults
) {
54 EXPECT_FALSE(data
.show_in_default_list
);
55 EXPECT_FALSE(data
.safe_for_autoreplace
);
56 EXPECT_EQ(0, data
.prepopulate_id
);
59 TEST_F(TemplateURLTest
, TestValidWithComplete
) {
61 data
.SetURL("{searchTerms}");
62 TemplateURL
url(data
);
63 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
66 TEST_F(TemplateURLTest
, URLRefTestSearchTerms
) {
67 struct SearchTermsCase
{
69 const base::string16 terms
;
70 const std::string output
;
71 } search_term_cases
[] = {
72 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
73 "http://foosea%20rch/bar" },
74 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
75 "http://foosea%20rch/bar?boo=abc" },
76 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
77 "http://foo/?boo=sea+rch%2Fbar" },
78 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
79 "http://en.wikipedia.org/wiki/%3F" }
81 for (size_t i
= 0; i
< arraysize(search_term_cases
); ++i
) {
82 const SearchTermsCase
& value
= search_term_cases
[i
];
84 data
.SetURL(value
.url
);
85 TemplateURL
url(data
);
86 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
87 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
88 GURL
result(url
.url_ref().ReplaceSearchTerms(
89 TemplateURLRef::SearchTermsArgs(value
.terms
), search_terms_data_
));
90 ASSERT_TRUE(result
.is_valid());
91 EXPECT_EQ(value
.output
, result
.spec());
95 TEST_F(TemplateURLTest
, URLRefTestCount
) {
97 data
.SetURL("http://foo{searchTerms}{count?}");
98 TemplateURL
url(data
);
99 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
100 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
101 GURL
result(url
.url_ref().ReplaceSearchTerms(
102 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
103 ASSERT_TRUE(result
.is_valid());
104 EXPECT_EQ("http://foox/", result
.spec());
107 TEST_F(TemplateURLTest
, URLRefTestCount2
) {
108 TemplateURLData data
;
109 data
.SetURL("http://foo{searchTerms}{count}");
110 TemplateURL
url(data
);
111 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
112 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
113 GURL
result(url
.url_ref().ReplaceSearchTerms(
114 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
115 ASSERT_TRUE(result
.is_valid());
116 EXPECT_EQ("http://foox10/", result
.spec());
119 TEST_F(TemplateURLTest
, URLRefTestIndices
) {
120 TemplateURLData data
;
121 data
.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
122 TemplateURL
url(data
);
123 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
124 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
125 GURL
result(url
.url_ref().ReplaceSearchTerms(
126 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
127 ASSERT_TRUE(result
.is_valid());
128 EXPECT_EQ("http://fooxxy/", result
.spec());
131 TEST_F(TemplateURLTest
, URLRefTestIndices2
) {
132 TemplateURLData data
;
133 data
.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
134 TemplateURL
url(data
);
135 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
136 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
137 GURL
result(url
.url_ref().ReplaceSearchTerms(
138 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
139 ASSERT_TRUE(result
.is_valid());
140 EXPECT_EQ("http://fooxx1y1/", result
.spec());
143 TEST_F(TemplateURLTest
, URLRefTestEncoding
) {
144 TemplateURLData data
;
145 data
.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
146 TemplateURL
url(data
);
147 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
148 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
149 GURL
result(url
.url_ref().ReplaceSearchTerms(
150 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
151 ASSERT_TRUE(result
.is_valid());
152 EXPECT_EQ("http://fooxxutf-8ya/", result
.spec());
155 TEST_F(TemplateURLTest
, URLRefTestImageURLWithPOST
) {
156 const char kInvalidPostParamsString
[] =
157 "unknown_template={UnknownTemplate},bad_value=bad{value},"
158 "{google:sbiSource}";
159 // List all accpectable parameter format in valid_post_params_string. it is
160 // expected like: "name0=,name1=value1,name2={template1}"
161 const char kValidPostParamsString
[] =
162 "image_content={google:imageThumbnail},image_url={google:imageURL},"
163 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
164 "constant_param=constant,width={google:imageOriginalWidth}";
165 const char KImageSearchURL
[] = "http://foo.com/sbi";
167 TemplateURLData data
;
168 data
.image_url
= KImageSearchURL
;
170 // Try to parse invalid post parameters.
171 data
.image_url_post_params
= kInvalidPostParamsString
;
172 TemplateURL
url_bad(data
);
173 ASSERT_FALSE(url_bad
.image_url_ref().IsValid(search_terms_data_
));
174 const TemplateURLRef::PostParams
& bad_post_params
=
175 url_bad
.image_url_ref().post_params_
;
176 ASSERT_EQ(2U, bad_post_params
.size());
177 ExpectPostParamIs(bad_post_params
[0], "unknown_template",
178 "{UnknownTemplate}");
179 ExpectPostParamIs(bad_post_params
[1], "bad_value", "bad{value}");
181 // Try to parse valid post parameters.
182 data
.image_url_post_params
= kValidPostParamsString
;
183 TemplateURL
url(data
);
184 ASSERT_TRUE(url
.image_url_ref().IsValid(search_terms_data_
));
185 ASSERT_FALSE(url
.image_url_ref().SupportsReplacement(search_terms_data_
));
187 // Check term replacement.
188 TemplateURLRef::SearchTermsArgs
search_args(ASCIIToUTF16("X"));
189 search_args
.image_thumbnail_content
= "dummy-image-thumbnail";
190 search_args
.image_url
= GURL("http://dummyimage.com/dummy.jpg");
191 search_args
.image_original_size
= gfx::Size(10, 10);
192 // Replacement operation with no post_data buffer should still return
194 TestingSearchTermsData
search_terms_data("http://X");
195 GURL
result(url
.image_url_ref().ReplaceSearchTerms(
196 search_args
, search_terms_data
));
197 ASSERT_TRUE(result
.is_valid());
198 EXPECT_EQ(KImageSearchURL
, result
.spec());
199 TemplateURLRef::PostContent post_content
;
200 result
= GURL(url
.image_url_ref().ReplaceSearchTerms(
201 search_args
, search_terms_data
, &post_content
));
202 ASSERT_TRUE(result
.is_valid());
203 EXPECT_EQ(KImageSearchURL
, result
.spec());
204 ASSERT_FALSE(post_content
.first
.empty());
205 ASSERT_FALSE(post_content
.second
.empty());
207 // Check parsed result of post parameters.
208 const TemplateURLRef::Replacements
& replacements
=
209 url
.image_url_ref().replacements_
;
210 const TemplateURLRef::PostParams
& post_params
=
211 url
.image_url_ref().post_params_
;
212 EXPECT_EQ(7U, post_params
.size());
213 for (TemplateURLRef::PostParams::const_iterator i
= post_params
.begin();
214 i
!= post_params
.end(); ++i
) {
215 TemplateURLRef::Replacements::const_iterator j
= replacements
.begin();
216 for (; j
!= replacements
.end(); ++j
) {
217 if (j
->is_post_param
&& j
->index
==
218 static_cast<size_t>(i
- post_params
.begin())) {
220 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH
:
221 ExpectPostParamIs(*i
, "width",
223 search_args
.image_original_size
.width()));
225 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE
:
226 ExpectPostParamIs(*i
, "sbisrc",
227 search_terms_data
.GoogleImageSearchSource());
229 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL
:
230 ExpectPostParamIs(*i
, "image_content",
231 search_args
.image_thumbnail_content
,
234 case TemplateURLRef::GOOGLE_IMAGE_URL
:
235 ExpectPostParamIs(*i
, "image_url", search_args
.image_url
.spec());
237 case TemplateURLRef::LANGUAGE
:
238 ExpectPostParamIs(*i
, "language", "en");
241 ADD_FAILURE(); // Should never go here.
246 if (j
!= replacements
.end())
248 if (i
->name
== "empty_param")
249 ExpectPostParamIs(*i
, "empty_param", std::string());
251 ExpectPostParamIs(*i
, "constant_param", "constant");
255 // Test that setting the prepopulate ID from TemplateURL causes the stored
256 // TemplateURLRef to handle parsing the URL parameters differently.
257 TEST_F(TemplateURLTest
, SetPrepopulatedAndParse
) {
258 TemplateURLData data
;
259 data
.SetURL("http://foo{fhqwhgads}bar");
260 TemplateURL
url(data
);
261 TemplateURLRef::Replacements replacements
;
263 EXPECT_EQ("http://foo{fhqwhgads}bar", url
.url_ref().ParseURL(
264 "http://foo{fhqwhgads}bar", &replacements
, NULL
, &valid
));
265 EXPECT_TRUE(replacements
.empty());
268 data
.prepopulate_id
= 123;
269 TemplateURL
url2(data
);
270 EXPECT_EQ("http://foobar", url2
.url_ref().ParseURL("http://foo{fhqwhgads}bar",
273 EXPECT_TRUE(replacements
.empty());
277 TEST_F(TemplateURLTest
, InputEncodingBeforeSearchTerm
) {
278 TemplateURLData data
;
279 data
.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
280 TemplateURL
url(data
);
281 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
282 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
283 GURL
result(url
.url_ref().ReplaceSearchTerms(
284 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
285 ASSERT_TRUE(result
.is_valid());
286 EXPECT_EQ("http://fooxutf-8axyb/", result
.spec());
289 TEST_F(TemplateURLTest
, URLRefTestEncoding2
) {
290 TemplateURLData data
;
291 data
.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
292 TemplateURL
url(data
);
293 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
294 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
295 GURL
result(url
.url_ref().ReplaceSearchTerms(
296 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
297 ASSERT_TRUE(result
.is_valid());
298 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result
.spec());
301 TEST_F(TemplateURLTest
, URLRefTestSearchTermsUsingTermsData
) {
302 struct SearchTermsCase
{
304 const base::string16 terms
;
306 } search_term_cases
[] = {
307 { "{google:baseURL}{language}{searchTerms}", base::string16(),
308 "http://example.com/e/en" },
309 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
310 "http://example.com/complete/" }
313 TestingSearchTermsData
search_terms_data("http://example.com/e/");
314 TemplateURLData data
;
315 for (size_t i
= 0; i
< arraysize(search_term_cases
); ++i
) {
316 const SearchTermsCase
& value
= search_term_cases
[i
];
317 data
.SetURL(value
.url
);
318 TemplateURL
url(data
);
319 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data
));
320 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data
));
321 GURL
result(url
.url_ref().ReplaceSearchTerms(
322 TemplateURLRef::SearchTermsArgs(value
.terms
), search_terms_data
, NULL
));
323 ASSERT_TRUE(result
.is_valid());
324 EXPECT_EQ(value
.output
, result
.spec());
328 TEST_F(TemplateURLTest
, URLRefTermToWide
) {
330 const char* encoded_search_term
;
331 const base::string16 expected_decoded_term
;
332 } to_wide_cases
[] = {
333 {"hello+world", ASCIIToUTF16("hello world")},
334 // Test some big-5 input.
335 {"%a7A%A6%6e+to+you", base::WideToUTF16(L
"\x4f60\x597d to you")},
336 // Test some UTF-8 input. We should fall back to this when the encoding
337 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
338 // Big-5 trailing byte.
339 {"%e4%bd%a05%e5%a5%bd+to+you",
340 base::WideToUTF16(L
"\x4f60\x35\x597d to you")},
341 // Undecodable input should stay escaped.
342 {"%91%01+abcd", base::WideToUTF16(L
"%91%01 abcd")},
343 // Make sure we convert %2B to +.
344 {"C%2B%2B", ASCIIToUTF16("C++")},
345 // C%2B is escaped as C%252B, make sure we unescape it properly.
346 {"C%252B", ASCIIToUTF16("C%2B")},
349 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
350 TemplateURLData data
;
351 data
.SetURL("http://foo?q={searchTerms}");
352 data
.input_encodings
.push_back("big-5");
353 TemplateURL
url(data
);
354 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
355 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
356 for (size_t i
= 0; i
< arraysize(to_wide_cases
); i
++) {
357 EXPECT_EQ(to_wide_cases
[i
].expected_decoded_term
,
358 url
.url_ref().SearchTermToString16(
359 to_wide_cases
[i
].encoded_search_term
));
363 TEST_F(TemplateURLTest
, DisplayURLToURLRef
) {
365 const std::string url
;
366 const base::string16 expected_result
;
368 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
369 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
371 ASCIIToUTF16("http://X") },
372 { "http://foo{searchTerms",
373 ASCIIToUTF16("http://foo{searchTerms") },
374 { "http://foo{searchTerms}{language}",
375 ASCIIToUTF16("http://foo%s{language}") },
377 TemplateURLData data
;
378 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
379 data
.SetURL(test_data
[i
].url
);
380 TemplateURL
url(data
);
381 EXPECT_EQ(test_data
[i
].expected_result
,
382 url
.url_ref().DisplayURL(search_terms_data_
));
383 EXPECT_EQ(test_data
[i
].url
,
384 TemplateURLRef::DisplayURLToURLRef(
385 url
.url_ref().DisplayURL(search_terms_data_
)));
389 TEST_F(TemplateURLTest
, ReplaceSearchTerms
) {
391 const std::string url
;
392 const std::string expected_result
;
394 { "http://foo/{language}{searchTerms}{inputEncoding}",
395 "http://foo/{language}XUTF-8" },
396 { "http://foo/{language}{inputEncoding}{searchTerms}",
397 "http://foo/{language}UTF-8X" },
398 { "http://foo/{searchTerms}{language}{inputEncoding}",
399 "http://foo/X{language}UTF-8" },
400 { "http://foo/{searchTerms}{inputEncoding}{language}",
401 "http://foo/XUTF-8{language}" },
402 { "http://foo/{inputEncoding}{searchTerms}{language}",
403 "http://foo/UTF-8X{language}" },
404 { "http://foo/{inputEncoding}{language}{searchTerms}",
405 "http://foo/UTF-8{language}X" },
406 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
407 "http://foo/{language}aXaUTF-8a" },
408 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
409 "http://foo/{language}aUTF-8aXa" },
410 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
411 "http://foo/Xa{language}aUTF-8a" },
412 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
413 "http://foo/XaUTF-8a{language}a" },
414 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
415 "http://foo/UTF-8aXa{language}a" },
416 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
417 "http://foo/UTF-8a{language}aXa" },
419 TemplateURLData data
;
420 data
.input_encodings
.push_back("UTF-8");
421 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
422 data
.SetURL(test_data
[i
].url
);
423 TemplateURL
url(data
);
424 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
425 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
426 std::string expected_result
= test_data
[i
].expected_result
;
427 ReplaceSubstringsAfterOffset(&expected_result
, 0, "{language}",
428 search_terms_data_
.GetApplicationLocale());
429 GURL
result(url
.url_ref().ReplaceSearchTerms(
430 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
431 search_terms_data_
));
432 ASSERT_TRUE(result
.is_valid());
433 EXPECT_EQ(expected_result
, result
.spec());
438 // Tests replacing search terms in various encodings and making sure the
439 // generated URL matches the expected value.
440 TEST_F(TemplateURLTest
, ReplaceArbitrarySearchTerms
) {
442 const std::string encoding
;
443 const base::string16 search_term
;
444 const std::string url
;
445 const std::string expected_result
;
447 { "BIG5", base::WideToUTF16(L
"\x60BD"),
448 "http://foo/?{searchTerms}{inputEncoding}",
449 "http://foo/?%B1~BIG5" },
450 { "UTF-8", ASCIIToUTF16("blah"),
451 "http://foo/?{searchTerms}{inputEncoding}",
452 "http://foo/?blahUTF-8" },
453 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
454 "http://foo/{searchTerms}/bar",
455 "http://foo/%82%A0/bar"},
456 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
457 "http://foo/{searchTerms}/bar",
458 "http://foo/%82%A0%20%82%A2/bar"},
460 TemplateURLData data
;
461 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
462 data
.SetURL(test_data
[i
].url
);
463 data
.input_encodings
.clear();
464 data
.input_encodings
.push_back(test_data
[i
].encoding
);
465 TemplateURL
url(data
);
466 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
467 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
468 GURL
result(url
.url_ref().ReplaceSearchTerms(
469 TemplateURLRef::SearchTermsArgs(test_data
[i
].search_term
),
470 search_terms_data_
));
471 ASSERT_TRUE(result
.is_valid());
472 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
476 // Tests replacing assisted query stats (AQS) in various scenarios.
477 TEST_F(TemplateURLTest
, ReplaceAssistedQueryStats
) {
479 const base::string16 search_term
;
480 const std::string aqs
;
481 const std::string base_url
;
482 const std::string url
;
483 const std::string expected_result
;
486 { ASCIIToUTF16("foo"),
489 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
491 // HTTPS available, AQS should be replaced.
492 { ASCIIToUTF16("foo"),
495 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
496 "https://foo/?fooaqs=chrome.0.0l6&" },
497 // HTTPS available, however AQS is empty.
498 { ASCIIToUTF16("foo"),
501 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
502 "https://foo/?foo" },
503 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
504 { ASCIIToUTF16("foo"),
506 "http://www.google.com",
507 "http://foo?{searchTerms}{google:assistedQueryStats}",
509 // A non-Google search provider with HTTPS should allow AQS.
510 { ASCIIToUTF16("foo"),
512 "https://www.google.com",
513 "https://foo?{searchTerms}{google:assistedQueryStats}",
514 "https://foo/?fooaqs=chrome.0.0l6&" },
516 TemplateURLData data
;
517 data
.input_encodings
.push_back("UTF-8");
518 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
519 data
.SetURL(test_data
[i
].url
);
520 TemplateURL
url(data
);
521 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
522 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
523 TemplateURLRef::SearchTermsArgs
search_terms_args(test_data
[i
].search_term
);
524 search_terms_args
.assisted_query_stats
= test_data
[i
].aqs
;
525 search_terms_data_
.set_google_base_url(test_data
[i
].base_url
);
526 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
527 search_terms_data_
));
528 ASSERT_TRUE(result
.is_valid());
529 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
533 // Tests replacing cursor position.
534 TEST_F(TemplateURLTest
, ReplaceCursorPosition
) {
536 const base::string16 search_term
;
537 size_t cursor_position
;
538 const std::string url
;
539 const std::string expected_result
;
541 { ASCIIToUTF16("foo"),
542 base::string16::npos
,
543 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
544 "http://www.google.com/?foo&" },
545 { ASCIIToUTF16("foo"),
547 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
548 "http://www.google.com/?foo&cp=2&" },
549 { ASCIIToUTF16("foo"),
551 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
552 "http://www.google.com/?foo&cp=15&" },
554 TemplateURLData data
;
555 data
.input_encodings
.push_back("UTF-8");
556 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
557 data
.SetURL(test_data
[i
].url
);
558 TemplateURL
url(data
);
559 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
560 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
561 TemplateURLRef::SearchTermsArgs
search_terms_args(test_data
[i
].search_term
);
562 search_terms_args
.cursor_position
= test_data
[i
].cursor_position
;
563 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
564 search_terms_data_
));
565 ASSERT_TRUE(result
.is_valid());
566 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
570 // Tests replacing input type (&oit=).
571 TEST_F(TemplateURLTest
, ReplaceInputType
) {
573 const base::string16 search_term
;
574 metrics::OmniboxInputType::Type input_type
;
575 const std::string url
;
576 const std::string expected_result
;
578 { ASCIIToUTF16("foo"),
579 metrics::OmniboxInputType::UNKNOWN
,
580 "{google:baseURL}?{searchTerms}&{google:inputType}",
581 "http://www.google.com/?foo&oit=1&" },
582 { ASCIIToUTF16("foo"),
583 metrics::OmniboxInputType::URL
,
584 "{google:baseURL}?{searchTerms}&{google:inputType}",
585 "http://www.google.com/?foo&oit=3&" },
586 { ASCIIToUTF16("foo"),
587 metrics::OmniboxInputType::FORCED_QUERY
,
588 "{google:baseURL}?{searchTerms}&{google:inputType}",
589 "http://www.google.com/?foo&oit=5&" },
591 TemplateURLData data
;
592 data
.input_encodings
.push_back("UTF-8");
593 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
594 data
.SetURL(test_data
[i
].url
);
595 TemplateURL
url(data
);
596 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
597 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
598 TemplateURLRef::SearchTermsArgs
search_terms_args(test_data
[i
].search_term
);
599 search_terms_args
.input_type
= test_data
[i
].input_type
;
600 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
601 search_terms_data_
));
602 ASSERT_TRUE(result
.is_valid());
603 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
607 // Tests replacing currentPageUrl.
608 TEST_F(TemplateURLTest
, ReplaceCurrentPageUrl
) {
610 const base::string16 search_term
;
611 const std::string current_page_url
;
612 const std::string url
;
613 const std::string expected_result
;
615 { ASCIIToUTF16("foo"),
616 "http://www.google.com/",
617 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
618 "http://www.google.com/?foo&url=http%3A%2F%2Fwww.google.com%2F&" },
619 { ASCIIToUTF16("foo"),
621 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
622 "http://www.google.com/?foo&" },
623 { ASCIIToUTF16("foo"),
624 "http://g.com/+-/*&=",
625 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
626 "http://www.google.com/?foo&url=http%3A%2F%2Fg.com%2F%2B-%2F*%26%3D&" },
628 TemplateURLData data
;
629 data
.input_encodings
.push_back("UTF-8");
630 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
631 data
.SetURL(test_data
[i
].url
);
632 TemplateURL
url(data
);
633 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
634 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
635 TemplateURLRef::SearchTermsArgs
search_terms_args(test_data
[i
].search_term
);
636 search_terms_args
.current_page_url
= test_data
[i
].current_page_url
;
637 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
638 search_terms_data_
));
639 ASSERT_TRUE(result
.is_valid());
640 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
644 TEST_F(TemplateURLTest
, OmniboxStartmargin
) {
646 const bool enable_omnibox_start_margin
;
647 const int omnibox_start_margin
;
648 const std::string expected_result
;
652 "http://bar/foo?q=foobar" },
655 "http://bar/foo?es_sm=0&q=foobar" },
658 "http://bar/foo?es_sm=42&q=foobar" },
660 TemplateURLData data
;
661 data
.SetURL("http://bar/foo?{google:omniboxStartMarginParameter}"
663 data
.input_encodings
.push_back("UTF-8");
664 TemplateURL
url(data
);
665 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
666 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
667 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
668 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foobar"));
669 search_terms_args
.enable_omnibox_start_margin
=
670 test_data
[i
].enable_omnibox_start_margin
;
671 search_terms_data_
.set_omnibox_start_margin(
672 test_data
[i
].omnibox_start_margin
);
673 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
674 search_terms_data_
));
675 ASSERT_TRUE(result
.is_valid());
676 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
680 TEST_F(TemplateURLTest
, Suggestions
) {
682 const int accepted_suggestion
;
683 const base::string16 original_query_for_suggestion
;
684 const std::string expected_result
;
686 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE
, base::string16(),
687 "http://bar/foo?q=foobar" },
688 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE
, ASCIIToUTF16("foo"),
689 "http://bar/foo?q=foobar" },
690 { TemplateURLRef::NO_SUGGESTION_CHOSEN
, base::string16(),
691 "http://bar/foo?q=foobar" },
692 { TemplateURLRef::NO_SUGGESTION_CHOSEN
, ASCIIToUTF16("foo"),
693 "http://bar/foo?q=foobar" },
694 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
695 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
697 TemplateURLData data
;
698 data
.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
700 data
.input_encodings
.push_back("UTF-8");
701 TemplateURL
url(data
);
702 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
703 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
704 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
705 TemplateURLRef::SearchTermsArgs
search_terms_args(
706 ASCIIToUTF16("foobar"));
707 search_terms_args
.accepted_suggestion
= test_data
[i
].accepted_suggestion
;
708 search_terms_args
.original_query
=
709 test_data
[i
].original_query_for_suggestion
;
710 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
711 search_terms_data_
));
712 ASSERT_TRUE(result
.is_valid());
713 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
717 TEST_F(TemplateURLTest
, RLZ
) {
718 base::string16 rlz_string
= search_terms_data_
.GetRlzParameterValue(false);
720 TemplateURLData data
;
721 data
.SetURL("http://bar/?{google:RLZ}{searchTerms}");
722 TemplateURL
url(data
);
723 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
724 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
725 GURL
result(url
.url_ref().ReplaceSearchTerms(
726 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_
));
727 ASSERT_TRUE(result
.is_valid());
728 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string
) + "&x",
732 TEST_F(TemplateURLTest
, RLZFromAppList
) {
733 base::string16 rlz_string
= search_terms_data_
.GetRlzParameterValue(true);
735 TemplateURLData data
;
736 data
.SetURL("http://bar/?{google:RLZ}{searchTerms}");
737 TemplateURL
url(data
);
738 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
739 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
740 TemplateURLRef::SearchTermsArgs
args(ASCIIToUTF16("x"));
741 args
.from_app_list
= true;
742 GURL
result(url
.url_ref().ReplaceSearchTerms(args
, search_terms_data_
));
743 ASSERT_TRUE(result
.is_valid());
744 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string
) + "&x",
748 TEST_F(TemplateURLTest
, HostAndSearchTermKey
) {
750 const std::string url
;
751 const std::string host
;
752 const std::string path
;
753 const std::string search_term_key
;
755 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
756 { "http://blah/{searchTerms}", "blah", "/", ""},
758 // No term should result in empty values.
759 { "http://blah/", "", "", ""},
761 // Multiple terms should result in empty values.
762 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
764 // Term in the host shouldn't match.
765 { "http://{searchTerms}", "", "", ""},
767 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
768 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
770 // Single term with extra chars in value should match.
771 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
774 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
775 TemplateURLData data
;
776 data
.SetURL(test_data
[i
].url
);
777 TemplateURL
url(data
);
778 EXPECT_EQ(test_data
[i
].host
, url
.url_ref().GetHost(search_terms_data_
));
779 EXPECT_EQ(test_data
[i
].path
, url
.url_ref().GetPath(search_terms_data_
));
780 EXPECT_EQ(test_data
[i
].search_term_key
,
781 url
.url_ref().GetSearchTermKey(search_terms_data_
));
785 TEST_F(TemplateURLTest
, SearchTermKeyLocation
) {
787 const std::string url
;
788 const url::Parsed::ComponentType location
;
789 const std::string path
;
790 size_t position_in_path
;
792 { "http://blah/{searchTerms}/", url::Parsed::PATH
, "//", 1 },
793 { "http://blah/{searchTerms}", url::Parsed::PATH
, "/", 1 },
794 { "http://blah/begin/{searchTerms}/end", url::Parsed::PATH
, "/begin//end", 7 },
796 { "http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY
,
797 "/", std::string::npos
},
798 { "http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF
,
799 "/", std::string::npos
},
801 // Multiple search terms should result in empty values.
802 { "http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY
,
803 "", std::string::npos
},
804 { "http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY
,
805 "", std::string::npos
},
806 { "http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY
,
807 "", std::string::npos
},
810 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
811 TemplateURLData data
;
812 data
.SetURL(test_data
[i
].url
);
813 TemplateURL
url(data
);
814 EXPECT_EQ(test_data
[i
].location
,
815 url
.url_ref().GetSearchTermKeyLocation(search_terms_data_
));
816 EXPECT_EQ(test_data
[i
].path
,
817 url
.url_ref().GetPath(search_terms_data_
));
818 EXPECT_EQ(test_data
[i
].position_in_path
,
819 url
.url_ref().GetSearchTermPositionInPath(search_terms_data_
));
823 TEST_F(TemplateURLTest
, GoogleBaseSuggestURL
) {
824 static const struct {
825 const char* const base_url
;
826 const char* const base_suggest_url
;
828 { "http://google.com/", "http://google.com/complete/", },
829 { "http://www.google.com/", "http://www.google.com/complete/", },
830 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
831 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
832 { "http://google.com/intl/xx/", "http://google.com/complete/", },
835 for (size_t i
= 0; i
< arraysize(data
); ++i
)
836 CheckSuggestBaseURL(data
[i
].base_url
, data
[i
].base_suggest_url
);
839 TEST_F(TemplateURLTest
, ParseParameterKnown
) {
840 std::string
parsed_url("{searchTerms}");
841 TemplateURLData data
;
842 data
.SetURL(parsed_url
);
843 TemplateURL
url(data
);
844 TemplateURLRef::Replacements replacements
;
845 EXPECT_TRUE(url
.url_ref().ParseParameter(0, 12, &parsed_url
, &replacements
));
846 EXPECT_EQ(std::string(), parsed_url
);
847 ASSERT_EQ(1U, replacements
.size());
848 EXPECT_EQ(0U, replacements
[0].index
);
849 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS
, replacements
[0].type
);
852 TEST_F(TemplateURLTest
, ParseParameterUnknown
) {
853 std::string
parsed_url("{fhqwhgads}abc");
854 TemplateURLData data
;
855 data
.SetURL(parsed_url
);
856 TemplateURL
url(data
);
857 TemplateURLRef::Replacements replacements
;
859 // By default, TemplateURLRef should not consider itself prepopulated.
860 // Therefore we should not replace the unknown parameter.
861 EXPECT_FALSE(url
.url_ref().ParseParameter(0, 10, &parsed_url
, &replacements
));
862 EXPECT_EQ("{fhqwhgads}abc", parsed_url
);
863 EXPECT_TRUE(replacements
.empty());
865 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
866 parsed_url
= "{fhqwhgads}abc";
867 data
.prepopulate_id
= 1;
868 TemplateURL
url2(data
);
869 EXPECT_TRUE(url2
.url_ref().ParseParameter(0, 10, &parsed_url
, &replacements
));
870 EXPECT_EQ("abc", parsed_url
);
871 EXPECT_TRUE(replacements
.empty());
874 TEST_F(TemplateURLTest
, ParseURLEmpty
) {
875 TemplateURL
url((TemplateURLData()));
876 TemplateURLRef::Replacements replacements
;
878 EXPECT_EQ(std::string(),
879 url
.url_ref().ParseURL(std::string(), &replacements
, NULL
, &valid
));
880 EXPECT_TRUE(replacements
.empty());
884 TEST_F(TemplateURLTest
, ParseURLNoTemplateEnd
) {
885 TemplateURLData data
;
887 TemplateURL
url(data
);
888 TemplateURLRef::Replacements replacements
;
890 EXPECT_EQ(std::string(), url
.url_ref().ParseURL("{", &replacements
, NULL
,
892 EXPECT_TRUE(replacements
.empty());
896 TEST_F(TemplateURLTest
, ParseURLNoKnownParameters
) {
897 TemplateURLData data
;
899 TemplateURL
url(data
);
900 TemplateURLRef::Replacements replacements
;
902 EXPECT_EQ("{}", url
.url_ref().ParseURL("{}", &replacements
, NULL
, &valid
));
903 EXPECT_TRUE(replacements
.empty());
907 TEST_F(TemplateURLTest
, ParseURLTwoParameters
) {
908 TemplateURLData data
;
909 data
.SetURL("{}{{%s}}");
910 TemplateURL
url(data
);
911 TemplateURLRef::Replacements replacements
;
914 url
.url_ref().ParseURL("{}{{searchTerms}}", &replacements
, NULL
,
916 ASSERT_EQ(1U, replacements
.size());
917 EXPECT_EQ(3U, replacements
[0].index
);
918 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS
, replacements
[0].type
);
922 TEST_F(TemplateURLTest
, ParseURLNestedParameter
) {
923 TemplateURLData data
;
925 TemplateURL
url(data
);
926 TemplateURLRef::Replacements replacements
;
929 url
.url_ref().ParseURL("{{searchTerms}", &replacements
, NULL
,
931 ASSERT_EQ(1U, replacements
.size());
932 EXPECT_EQ(1U, replacements
[0].index
);
933 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS
, replacements
[0].type
);
937 TEST_F(TemplateURLTest
, SearchClient
) {
938 const std::string
base_url_str("http://google.com/?");
939 const std::string
terms_str("{searchTerms}&{google:searchClient}");
940 const std::string full_url_str
= base_url_str
+ terms_str
;
941 const base::string16
terms(ASCIIToUTF16(terms_str
));
942 search_terms_data_
.set_google_base_url(base_url_str
);
944 TemplateURLData data
;
945 data
.SetURL(full_url_str
);
946 TemplateURL
url(data
);
947 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
948 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
949 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foobar"));
951 // Check that the URL is correct when a client is not present.
952 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
953 search_terms_data_
));
954 ASSERT_TRUE(result
.is_valid());
955 EXPECT_EQ("http://google.com/?foobar&", result
.spec());
957 // Check that the URL is correct when a client is present.
958 search_terms_data_
.set_search_client("search_client");
959 GURL
result_2(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
960 search_terms_data_
));
961 ASSERT_TRUE(result_2
.is_valid());
962 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2
.spec());
965 TEST_F(TemplateURLTest
, GetURLNoInstantURL
) {
966 TemplateURLData data
;
967 data
.SetURL("http://google.com/?q={searchTerms}");
968 data
.suggestions_url
= "http://google.com/suggest?q={searchTerms}";
969 data
.alternate_urls
.push_back("http://google.com/alt?q={searchTerms}");
970 data
.alternate_urls
.push_back("{google:baseURL}/alt/#q={searchTerms}");
971 TemplateURL
url(data
);
972 ASSERT_EQ(3U, url
.URLCount());
973 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url
.GetURL(0));
974 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url
.GetURL(1));
975 EXPECT_EQ("http://google.com/?q={searchTerms}", url
.GetURL(2));
978 TEST_F(TemplateURLTest
, GetURLNoSuggestionsURL
) {
979 TemplateURLData data
;
980 data
.SetURL("http://google.com/?q={searchTerms}");
981 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
982 data
.alternate_urls
.push_back("http://google.com/alt?q={searchTerms}");
983 data
.alternate_urls
.push_back("{google:baseURL}/alt/#q={searchTerms}");
984 TemplateURL
url(data
);
985 ASSERT_EQ(3U, url
.URLCount());
986 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url
.GetURL(0));
987 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url
.GetURL(1));
988 EXPECT_EQ("http://google.com/?q={searchTerms}", url
.GetURL(2));
991 TEST_F(TemplateURLTest
, GetURLOnlyOneURL
) {
992 TemplateURLData data
;
993 data
.SetURL("http://www.google.co.uk/");
994 TemplateURL
url(data
);
995 ASSERT_EQ(1U, url
.URLCount());
996 EXPECT_EQ("http://www.google.co.uk/", url
.GetURL(0));
999 TEST_F(TemplateURLTest
, ExtractSearchTermsFromURL
) {
1000 TemplateURLData data
;
1001 data
.SetURL("http://google.com/?q={searchTerms}");
1002 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
1003 data
.alternate_urls
.push_back("http://google.com/alt/#q={searchTerms}");
1004 data
.alternate_urls
.push_back(
1005 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1006 TemplateURL
url(data
);
1007 base::string16 result
;
1009 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1010 GURL("http://google.com/?q=something"), search_terms_data_
, &result
));
1011 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1013 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1014 GURL("http://google.com/?espv&q=something"),
1015 search_terms_data_
, &result
));
1016 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1018 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1019 GURL("http://google.com/?espv=1&q=something"),
1020 search_terms_data_
, &result
));
1021 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1023 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1024 GURL("http://google.com/?espv=0&q=something"),
1025 search_terms_data_
, &result
));
1026 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1028 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1029 GURL("http://google.com/alt/#q=something"),
1030 search_terms_data_
, &result
));
1031 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1033 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1034 GURL("http://google.com/alt/#espv&q=something"),
1035 search_terms_data_
, &result
));
1036 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1038 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1039 GURL("http://google.com/alt/#espv=1&q=something"),
1040 search_terms_data_
, &result
));
1041 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1043 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1044 GURL("http://google.com/alt/#espv=0&q=something"),
1045 search_terms_data_
, &result
));
1046 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1048 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1049 GURL("http://google.ca/?q=something"), search_terms_data_
, &result
));
1050 EXPECT_EQ(base::string16(), result
);
1052 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1053 GURL("http://google.ca/?q=something&q=anything"),
1054 search_terms_data_
, &result
));
1055 EXPECT_EQ(base::string16(), result
);
1057 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1058 GURL("http://google.com/foo/?q=foo"), search_terms_data_
, &result
));
1059 EXPECT_EQ(base::string16(), result
);
1061 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1062 GURL("https://google.com/?q=foo"), search_terms_data_
, &result
));
1063 EXPECT_EQ(ASCIIToUTF16("foo"), result
);
1065 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1066 GURL("http://google.com:8080/?q=foo"), search_terms_data_
, &result
));
1067 EXPECT_EQ(base::string16(), result
);
1069 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1070 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_
, &result
));
1071 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result
);
1073 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1074 GURL("http://google.com/alt/?q=123#q=456"),
1075 search_terms_data_
, &result
));
1076 EXPECT_EQ(ASCIIToUTF16("456"), result
);
1078 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1079 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1080 search_terms_data_
, &result
));
1081 EXPECT_EQ(ASCIIToUTF16("123"), result
);
1083 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(GURL(
1084 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1085 search_terms_data_
, &result
));
1086 EXPECT_EQ(ASCIIToUTF16("789"), result
);
1088 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1089 GURL("http://google.com/alt/?q="), search_terms_data_
, &result
));
1090 EXPECT_EQ(base::string16(), result
);
1092 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1093 GURL("http://google.com/alt/?#q="), search_terms_data_
, &result
));
1094 EXPECT_EQ(base::string16(), result
);
1096 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1097 GURL("http://google.com/alt/?q=#q="), search_terms_data_
, &result
));
1098 EXPECT_EQ(base::string16(), result
);
1100 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1101 GURL("http://google.com/alt/?q=123#q="), search_terms_data_
, &result
));
1102 EXPECT_EQ(base::string16(), result
);
1104 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1105 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_
, &result
));
1106 EXPECT_EQ(ASCIIToUTF16("123"), result
);
1109 TEST_F(TemplateURLTest
, ExtractSearchTermsFromURLPath
) {
1110 TemplateURLData data
;
1111 data
.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1112 TemplateURL
url(data
);
1113 base::string16 result
;
1115 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1116 GURL("http://term-in-path.com/begin/something/end"),
1117 search_terms_data_
, &result
));
1118 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1120 // "%20" must be converted to space.
1121 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1122 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1123 search_terms_data_
, &result
));
1124 EXPECT_EQ(ASCIIToUTF16("a b c"), result
);
1126 // Plus must not be converted to space.
1127 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1128 GURL("http://term-in-path.com/begin/1+2+3/end"),
1129 search_terms_data_
, &result
));
1130 EXPECT_EQ(ASCIIToUTF16("1+2+3"), result
);
1132 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1133 GURL("http://term-in-path.com/about"), search_terms_data_
, &result
));
1134 EXPECT_EQ(base::string16(), result
);
1136 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1137 GURL("http://term-in-path.com/begin"), search_terms_data_
, &result
));
1138 EXPECT_EQ(base::string16(), result
);
1140 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1141 GURL("http://term-in-path.com/end"), search_terms_data_
, &result
));
1142 EXPECT_EQ(base::string16(), result
);
1145 // Checks that the ExtractSearchTermsFromURL function works correctly
1146 // for urls containing non-latin characters in UTF8 encoding.
1147 TEST_F(TemplateURLTest
, ExtractSearchTermsFromUTF8URL
) {
1148 TemplateURLData data
;
1149 data
.SetURL("http://utf-8.ru/?q={searchTerms}");
1150 data
.alternate_urls
.push_back("http://utf-8.ru/#q={searchTerms}");
1151 data
.alternate_urls
.push_back("http://utf-8.ru/path/{searchTerms}");
1152 TemplateURL
url(data
);
1153 base::string16 result
;
1155 // Russian text encoded with UTF-8.
1156 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1157 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1158 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
1159 search_terms_data_
, &result
));
1162 L
"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1163 L
"\x043C\x0438\x0440!"),
1166 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1167 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1169 search_terms_data_
, &result
));
1171 base::WideToUTF16(L
"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1174 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1175 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1177 search_terms_data_
, &result
));
1179 base::WideToUTF16(L
"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1183 // Checks that the ExtractSearchTermsFromURL function works correctly
1184 // for urls containing non-latin characters in non-UTF8 encoding.
1185 TEST_F(TemplateURLTest
, ExtractSearchTermsFromNonUTF8URL
) {
1186 TemplateURLData data
;
1187 data
.SetURL("http://windows-1251.ru/?q={searchTerms}");
1188 data
.alternate_urls
.push_back("http://windows-1251.ru/#q={searchTerms}");
1189 data
.alternate_urls
.push_back("http://windows-1251.ru/path/{searchTerms}");
1190 data
.input_encodings
.push_back("windows-1251");
1191 TemplateURL
url(data
);
1192 base::string16 result
;
1194 // Russian text encoded with Windows-1251.
1195 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1196 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1198 search_terms_data_
, &result
));
1201 L
"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1202 L
"\x043C\x0438\x0440!"),
1205 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1206 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1207 search_terms_data_
, &result
));
1209 base::WideToUTF16(L
"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1212 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1213 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1214 search_terms_data_
, &result
));
1216 base::WideToUTF16(L
"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1220 TEST_F(TemplateURLTest
, HasSearchTermsReplacementKey
) {
1221 TemplateURLData data
;
1222 data
.SetURL("http://google.com/?q={searchTerms}");
1223 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
1224 data
.alternate_urls
.push_back("http://google.com/alt/#q={searchTerms}");
1225 data
.alternate_urls
.push_back(
1226 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1227 data
.search_terms_replacement_key
= "espv";
1228 TemplateURL
url(data
);
1230 // Test with instant enabled required.
1231 EXPECT_FALSE(url
.HasSearchTermsReplacementKey(
1232 GURL("http://google.com/")));
1234 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1235 GURL("http://google.com/?espv")));
1237 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1238 GURL("http://google.com/#espv")));
1240 EXPECT_FALSE(url
.HasSearchTermsReplacementKey(
1241 GURL("http://google.com/?q=something")));
1243 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1244 GURL("http://google.com/?q=something&espv")));
1246 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1247 GURL("http://google.com/?q=something&espv=1")));
1249 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1250 GURL("http://google.com/?q=something&espv=0")));
1252 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1253 GURL("http://google.com/?espv&q=something")));
1255 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1256 GURL("http://google.com/?espv=1&q=something")));
1258 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1259 GURL("http://google.com/?espv=0&q=something")));
1261 EXPECT_FALSE(url
.HasSearchTermsReplacementKey(
1262 GURL("http://google.com/alt/#q=something")));
1264 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1265 GURL("http://google.com/alt/#q=something&espv")));
1267 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1268 GURL("http://google.com/alt/#q=something&espv=1")));
1270 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1271 GURL("http://google.com/alt/#q=something&espv=0")));
1273 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1274 GURL("http://google.com/alt/#espv&q=something")));
1276 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1277 GURL("http://google.com/alt/#espv=1&q=something")));
1279 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1280 GURL("http://google.com/alt/#espv=0&q=something")));
1282 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1283 GURL("http://google.com/?espv#q=something")));
1285 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1286 GURL("http://google.com/?espv=1#q=something")));
1288 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1289 GURL("http://google.com/?q=something#espv")));
1291 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1292 GURL("http://google.com/?q=something#espv=1")));
1294 // This does not ensure the domain matches.
1295 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1296 GURL("http://bing.com/?espv")));
1298 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1299 GURL("http://bing.com/#espv")));
1302 TEST_F(TemplateURLTest
, ReplaceSearchTermsInURL
) {
1303 TemplateURLData data
;
1304 data
.SetURL("http://google.com/?q={searchTerms}");
1305 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
1306 data
.alternate_urls
.push_back("http://google.com/alt/#q={searchTerms}");
1307 data
.alternate_urls
.push_back(
1308 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1309 TemplateURL
url(data
);
1310 TemplateURLRef::SearchTermsArgs
search_terms(ASCIIToUTF16("Bob Morane"));
1313 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1314 GURL("http://google.com/?q=something"), search_terms
,
1315 search_terms_data_
, &result
));
1316 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result
);
1318 result
= GURL("http://should.not.change.com");
1319 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1320 GURL("http://google.ca/?q=something"), search_terms
,
1321 search_terms_data_
, &result
));
1322 EXPECT_EQ(GURL("http://should.not.change.com"), result
);
1324 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1325 GURL("http://google.com/foo/?q=foo"), search_terms
,
1326 search_terms_data_
, &result
));
1328 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1329 GURL("https://google.com/?q=foo"), search_terms
,
1330 search_terms_data_
, &result
));
1331 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result
);
1333 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1334 GURL("http://google.com:8080/?q=foo"), search_terms
,
1335 search_terms_data_
, &result
));
1337 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1338 GURL("http://google.com/?q=1+2+3&b=456"), search_terms
,
1339 search_terms_data_
, &result
));
1340 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result
);
1342 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1343 // template_url.cc for details.
1344 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1345 GURL("http://google.com/alt/?q=123#q=456"), search_terms
,
1346 search_terms_data_
, &result
));
1347 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result
);
1349 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1350 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms
,
1351 search_terms_data_
, &result
));
1352 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
1355 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1356 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1357 search_terms
, search_terms_data_
, &result
));
1358 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1359 "#j=abc&q=Bob+Morane&h=def9"), result
);
1361 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1362 GURL("http://google.com/alt/?q="), search_terms
,
1363 search_terms_data_
, &result
));
1365 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1366 GURL("http://google.com/alt/?#q="), search_terms
,
1367 search_terms_data_
, &result
));
1369 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1370 GURL("http://google.com/alt/?q=#q="), search_terms
,
1371 search_terms_data_
, &result
));
1373 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1374 GURL("http://google.com/alt/?q=123#q="), search_terms
,
1375 search_terms_data_
, &result
));
1377 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1378 GURL("http://google.com/alt/?q=#q=123"), search_terms
,
1379 search_terms_data_
, &result
));
1380 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result
);
1383 TEST_F(TemplateURLTest
, ReplaceSearchTermsInURLPath
) {
1384 TemplateURLData data
;
1385 data
.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1386 TemplateURL
url(data
);
1387 TemplateURLRef::SearchTermsArgs
search_terms(ASCIIToUTF16("Bob Morane"));
1390 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1391 GURL("http://term-in-path.com/begin/something/end"), search_terms
,
1392 search_terms_data_
, &result
));
1393 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result
);
1395 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1396 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms
,
1397 search_terms_data_
, &result
));
1398 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result
);
1400 result
= GURL("http://should.not.change.com");
1401 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1402 GURL("http://term-in-path.com/about"), search_terms
,
1403 search_terms_data_
, &result
));
1404 EXPECT_EQ(GURL("http://should.not.change.com"), result
);
1407 // Checks that the ReplaceSearchTermsInURL function works correctly
1408 // for search terms containing non-latin characters for a search engine
1409 // using UTF-8 input encoding.
1410 TEST_F(TemplateURLTest
, ReplaceSearchTermsInUTF8URL
) {
1411 TemplateURLData data
;
1412 data
.SetURL("http://utf-8.ru/?q={searchTerms}");
1413 data
.alternate_urls
.push_back("http://utf-8.ru/#q={searchTerms}");
1414 data
.alternate_urls
.push_back("http://utf-8.ru/path/{searchTerms}");
1415 TemplateURL
url(data
);
1417 // Russian text which will be encoded with UTF-8.
1418 TemplateURLRef::SearchTermsArgs
search_terms(base::WideToUTF16(
1419 L
"\x0442\x0435\x043A\x0441\x0442"));
1422 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1423 GURL("http://utf-8.ru/?q=a+b"), search_terms
, search_terms_data_
,
1425 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1428 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1429 GURL("http://utf-8.ru/#q=a+b"), search_terms
, search_terms_data_
,
1431 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1434 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1435 GURL("http://utf-8.ru/path/a%20b"), search_terms
, search_terms_data_
,
1437 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1441 // Checks that the ReplaceSearchTermsInURL function works correctly
1442 // for search terms containing non-latin characters for a search engine
1443 // using non UTF-8 input encoding.
1444 TEST_F(TemplateURLTest
, ReplaceSearchTermsInNonUTF8URL
) {
1445 TemplateURLData data
;
1446 data
.SetURL("http://windows-1251.ru/?q={searchTerms}");
1447 data
.alternate_urls
.push_back("http://windows-1251.ru/#q={searchTerms}");
1448 data
.alternate_urls
.push_back("http://windows-1251.ru/path/{searchTerms}");
1449 data
.input_encodings
.push_back("windows-1251");
1450 TemplateURL
url(data
);
1452 // Russian text which will be encoded with Windows-1251.
1453 TemplateURLRef::SearchTermsArgs
search_terms(base::WideToUTF16(
1454 L
"\x0442\x0435\x043A\x0441\x0442"));
1457 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1458 GURL("http://windows-1251.ru/?q=a+b"), search_terms
, search_terms_data_
,
1460 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1463 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1464 GURL("http://windows-1251.ru/#q=a+b"), search_terms
, search_terms_data_
,
1466 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1469 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1470 GURL("http://windows-1251.ru/path/a%20b"), search_terms
,
1471 search_terms_data_
, &result
));
1472 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1476 // Test the |suggest_query_params| field of SearchTermsArgs.
1477 TEST_F(TemplateURLTest
, SuggestQueryParams
) {
1478 TemplateURLData data
;
1479 // Pick a URL with replacements before, during, and after the query, to ensure
1480 // we don't goof up any of them.
1481 data
.SetURL("{google:baseURL}search?q={searchTerms}"
1482 "#{google:originalQueryForSuggestion}x");
1483 TemplateURL
url(data
);
1485 // Baseline: no |suggest_query_params| field.
1486 TemplateURLRef::SearchTermsArgs
search_terms(ASCIIToUTF16("abc"));
1487 search_terms
.original_query
= ASCIIToUTF16("def");
1488 search_terms
.accepted_suggestion
= 0;
1489 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1490 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1492 // Set the suggest_query_params.
1493 search_terms
.suggest_query_params
= "pq=xyz";
1494 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
1495 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1497 // Add extra_query_params in the mix, and ensure it works.
1498 search_terms
.append_extra_query_params
= true;
1499 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1500 switches::kExtraSearchQueryParams
, "a=b");
1501 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
1502 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1505 // Test the |append_extra_query_params| field of SearchTermsArgs.
1506 TEST_F(TemplateURLTest
, ExtraQueryParams
) {
1507 TemplateURLData data
;
1508 // Pick a URL with replacements before, during, and after the query, to ensure
1509 // we don't goof up any of them.
1510 data
.SetURL("{google:baseURL}search?q={searchTerms}"
1511 "#{google:originalQueryForSuggestion}x");
1512 TemplateURL
url(data
);
1514 // Baseline: no command-line args, no |append_extra_query_params| flag.
1515 TemplateURLRef::SearchTermsArgs
search_terms(ASCIIToUTF16("abc"));
1516 search_terms
.original_query
= ASCIIToUTF16("def");
1517 search_terms
.accepted_suggestion
= 0;
1518 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1519 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1521 // Set the flag. Since there are no command-line args, this should have no
1523 search_terms
.append_extra_query_params
= true;
1524 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1525 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1527 // Now append the command-line arg. This should be inserted into the query.
1528 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1529 switches::kExtraSearchQueryParams
, "a=b");
1530 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
1531 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1533 // Turn off the flag. Now the command-line arg should be ignored again.
1534 search_terms
.append_extra_query_params
= false;
1535 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1536 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1539 // Tests replacing pageClassification.
1540 TEST_F(TemplateURLTest
, ReplacePageClassification
) {
1541 TemplateURLData data
;
1542 data
.input_encodings
.push_back("UTF-8");
1543 data
.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
1544 TemplateURL
url(data
);
1545 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
1546 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
1547 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1549 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1550 search_terms_data_
);
1551 EXPECT_EQ("http://www.google.com/?q=foo", result
);
1553 search_terms_args
.page_classification
= metrics::OmniboxEventProto::NTP
;
1554 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1555 search_terms_data_
);
1556 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result
);
1558 search_terms_args
.page_classification
=
1559 metrics::OmniboxEventProto::HOME_PAGE
;
1560 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1561 search_terms_data_
);
1562 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result
);
1565 // Test the IsSearchResults function.
1566 TEST_F(TemplateURLTest
, IsSearchResults
) {
1567 TemplateURLData data
;
1568 data
.SetURL("http://bar/search?q={searchTerms}");
1569 data
.instant_url
= "http://bar/instant#q={searchTerms}";
1570 data
.new_tab_url
= "http://bar/newtab";
1571 data
.alternate_urls
.push_back("http://bar/?q={searchTerms}");
1572 data
.alternate_urls
.push_back("http://bar/#q={searchTerms}");
1573 data
.alternate_urls
.push_back("http://bar/search#q{searchTerms}");
1574 data
.alternate_urls
.push_back("http://bar/webhp#q={searchTerms}");
1575 TemplateURL
search_provider(data
);
1578 const char* const url
;
1581 { "http://bar/search?q=foo&oq=foo", true, },
1582 { "http://bar/?q=foo&oq=foo", true, },
1583 { "http://bar/#output=search&q=foo&oq=foo", true, },
1584 { "http://bar/webhp#q=foo&oq=foo", true, },
1585 { "http://bar/#q=foo&oq=foo", true, },
1586 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1587 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1588 { "http://bar/", false, },
1589 { "http://foo/", false, },
1590 { "http://bar/newtab", false, },
1593 for (size_t i
= 0; i
< arraysize(url_data
); ++i
) {
1594 EXPECT_EQ(url_data
[i
].result
,
1595 search_provider
.IsSearchURL(GURL(url_data
[i
].url
),
1596 search_terms_data_
));
1600 TEST_F(TemplateURLTest
, ReflectsBookmarkBarPinned
) {
1601 TemplateURLData data
;
1602 data
.input_encodings
.push_back("UTF-8");
1603 data
.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
1604 TemplateURL
url(data
);
1605 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
1606 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
1607 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1609 // Do not add the param when InstantExtended is suppressed on SRPs.
1610 search_terms_data_
.set_is_showing_search_terms_on_search_results_pages(false);
1611 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1612 search_terms_data_
);
1613 EXPECT_EQ("http://www.google.com/?q=foo", result
);
1615 // Add the param when InstantExtended is not suppressed on SRPs.
1616 search_terms_data_
.set_is_showing_search_terms_on_search_results_pages(true);
1617 search_terms_args
.bookmark_bar_pinned
= false;
1618 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1619 search_terms_data_
);
1620 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result
);
1622 search_terms_data_
.set_is_showing_search_terms_on_search_results_pages(true);
1623 search_terms_args
.bookmark_bar_pinned
= true;
1624 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1625 search_terms_data_
);
1626 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result
);
1629 TEST_F(TemplateURLTest
, AnswersHasVersion
) {
1630 TemplateURLData data
;
1631 search_terms_data_
.set_google_base_url("http://bar/");
1632 data
.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1634 TemplateURL
url(data
);
1635 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1636 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1637 search_terms_data_
);
1638 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result
);
1640 search_terms_data_
.set_enable_answers_in_suggest(true);
1641 TemplateURL
url2(data
);
1642 result
= url2
.url_ref().ReplaceSearchTerms(search_terms_args
,
1643 search_terms_data_
);
1644 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result
);
1647 TEST_F(TemplateURLTest
, SessionToken
) {
1648 TemplateURLData data
;
1649 search_terms_data_
.set_google_base_url("http://bar/");
1650 data
.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1652 TemplateURL
url(data
);
1653 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1654 search_terms_args
.session_token
= "SESSIONTOKENGOESHERE";
1655 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1656 search_terms_data_
);
1657 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result
);
1659 TemplateURL
url2(data
);
1660 search_terms_args
.session_token
= "";
1661 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1662 search_terms_data_
);
1663 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result
);
1666 TEST_F(TemplateURLTest
, ContextualSearchParameters
) {
1667 TemplateURLData data
;
1668 search_terms_data_
.set_google_base_url("http://bar/");
1669 data
.SetURL("http://bar/_/contextualsearch?"
1670 "{google:contextualSearchVersion}"
1671 "{google:contextualSearchContextData}");
1673 TemplateURL
url(data
);
1674 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1675 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1676 search_terms_data_
);
1677 EXPECT_EQ("http://bar/_/contextualsearch?ctxsl_resolve=1", result
);
1679 TemplateURLRef::SearchTermsArgs::ContextualSearchParams
params(
1680 1, 6, 11, "allen", "woody+allen+movies", "www.wikipedia.org",
1682 search_terms_args
.contextual_search_params
= params
;
1683 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1684 search_terms_data_
);
1685 EXPECT_EQ("http://bar/_/contextualsearch?"
1690 "ctxs_content=woody+allen+movies&"
1691 "ctxsl_url=www.wikipedia.org&"
1692 "ctxs_encoding=utf-8&"
1697 TEST_F(TemplateURLTest
, GenerateKeyword
) {
1698 ASSERT_EQ(ASCIIToUTF16("foo"),
1699 TemplateURL::GenerateKeyword(GURL("http://foo")));
1700 // www. should be stripped.
1701 ASSERT_EQ(ASCIIToUTF16("foo"),
1702 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
1703 // Make sure we don't get a trailing '/'.
1704 ASSERT_EQ(ASCIIToUTF16("blah"),
1705 TemplateURL::GenerateKeyword(GURL("http://blah/")));
1706 // Don't generate the empty string.
1707 ASSERT_EQ(ASCIIToUTF16("www"),
1708 TemplateURL::GenerateKeyword(GURL("http://www.")));
1711 TEST_F(TemplateURLTest
, GenerateSearchURL
) {
1712 struct GenerateSearchURLCase
{
1713 const char* test_name
;
1715 const char* expected
;
1716 } generate_url_cases
[] = {
1717 { "invalid URL", "foo{searchTerms}", "" },
1718 { "URL with no replacements", "http://foo/", "http://foo/" },
1719 { "basic functionality", "http://foo/{searchTerms}",
1720 "http://foo/blah.blah.blah.blah.blah" }
1723 for (size_t i
= 0; i
< arraysize(generate_url_cases
); ++i
) {
1724 TemplateURLData data
;
1725 data
.SetURL(generate_url_cases
[i
].url
);
1726 TemplateURL
t_url(data
);
1727 EXPECT_EQ(t_url
.GenerateSearchURL(search_terms_data_
).spec(),
1728 generate_url_cases
[i
].expected
)
1729 << generate_url_cases
[i
].test_name
<< " failed.";
1733 TEST_F(TemplateURLTest
, PrefetchQueryParameters
) {
1734 TemplateURLData data
;
1735 search_terms_data_
.set_google_base_url("http://bar/");
1736 data
.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1738 TemplateURL
url(data
);
1739 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1740 search_terms_args
.prefetch_query
= "full query text";
1741 search_terms_args
.prefetch_query_type
= "2338";
1742 std::string result
=
1743 url
.url_ref().ReplaceSearchTerms(search_terms_args
, search_terms_data_
);
1744 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1747 TemplateURL
url2(data
);
1748 search_terms_args
.prefetch_query
.clear();
1749 search_terms_args
.prefetch_query_type
.clear();
1751 url2
.url_ref().ReplaceSearchTerms(search_terms_args
, search_terms_data_
);
1752 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result
);