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 "testing/gtest/include/gtest/gtest.h"
17 using base::ASCIIToUTF16
;
19 // TestSearchTermsData --------------------------------------------------------
21 // Simple implementation of SearchTermsData.
22 class TestSearchTermsData
: public SearchTermsData
{
24 explicit TestSearchTermsData(const std::string
& google_base_url
);
26 virtual std::string
GoogleBaseURLValue() const OVERRIDE
;
27 virtual base::string16
GetRlzParameterValue(
28 bool from_app_list
) const OVERRIDE
;
29 virtual std::string
GetSearchClient() const OVERRIDE
;
30 virtual std::string
GoogleImageSearchSource() const OVERRIDE
;
31 virtual bool EnableAnswersInSuggest() const OVERRIDE
;
32 virtual bool IsShowingSearchTermsOnSearchResultsPages() const OVERRIDE
;
33 virtual int OmniboxStartMargin() const OVERRIDE
;
35 void set_google_base_url(const std::string
& google_base_url
) {
36 google_base_url_
= google_base_url
;
38 void set_search_client(const std::string
& search_client
) {
39 search_client_
= search_client
;
41 void set_enable_answers_in_suggest(bool enable_answers_in_suggest
) {
42 enable_answers_in_suggest_
= enable_answers_in_suggest
;
44 void set_is_showing_search_terms_on_search_results_pages(bool value
) {
45 is_showing_search_terms_on_search_results_pages_
= value
;
47 void set_omnibox_start_margin(int omnibox_start_margin
) {
48 omnibox_start_margin_
= omnibox_start_margin
;
52 std::string google_base_url_
;
53 std::string search_client_
;
54 bool enable_answers_in_suggest_
;
55 bool is_showing_search_terms_on_search_results_pages_
;
56 int omnibox_start_margin_
;
58 DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData
);
61 TestSearchTermsData::TestSearchTermsData(const std::string
& google_base_url
)
62 : google_base_url_(google_base_url
),
63 enable_answers_in_suggest_(false),
64 is_showing_search_terms_on_search_results_pages_(false) {
67 std::string
TestSearchTermsData::GoogleBaseURLValue() const {
68 return google_base_url_
;
71 base::string16
TestSearchTermsData::GetRlzParameterValue(
72 bool from_app_list
) const {
74 from_app_list
? "rlz_parameter_from_app_list" : "rlz_parameter");
77 std::string
TestSearchTermsData::GetSearchClient() const {
78 return search_client_
;
81 std::string
TestSearchTermsData::GoogleImageSearchSource() const {
82 return "google_image_search_source";
85 bool TestSearchTermsData::EnableAnswersInSuggest() const {
86 return enable_answers_in_suggest_
;
89 bool TestSearchTermsData::IsShowingSearchTermsOnSearchResultsPages() const {
90 return is_showing_search_terms_on_search_results_pages_
;
93 int TestSearchTermsData::OmniboxStartMargin() const {
94 return omnibox_start_margin_
;
97 // TemplateURLTest ------------------------------------------------------------
99 class TemplateURLTest
: public testing::Test
{
101 TemplateURLTest() : search_terms_data_("http://www.google.com/") {}
102 void CheckSuggestBaseURL(const std::string
& base_url
,
103 const std::string
& base_suggest_url
) const;
105 TestSearchTermsData search_terms_data_
;
108 void TemplateURLTest::CheckSuggestBaseURL(
109 const std::string
& base_url
,
110 const std::string
& base_suggest_url
) const {
111 TestSearchTermsData
search_terms_data(base_url
);
112 EXPECT_EQ(base_suggest_url
, search_terms_data
.GoogleBaseSuggestURLValue());
116 // Actual tests ---------------------------------------------------------------
118 TEST_F(TemplateURLTest
, Defaults
) {
119 TemplateURLData data
;
120 EXPECT_FALSE(data
.show_in_default_list
);
121 EXPECT_FALSE(data
.safe_for_autoreplace
);
122 EXPECT_EQ(0, data
.prepopulate_id
);
125 TEST_F(TemplateURLTest
, TestValidWithComplete
) {
126 TemplateURLData data
;
127 data
.SetURL("{searchTerms}");
128 TemplateURL
url(data
);
129 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
132 TEST_F(TemplateURLTest
, URLRefTestSearchTerms
) {
133 struct SearchTermsCase
{
135 const base::string16 terms
;
136 const std::string output
;
137 } search_term_cases
[] = {
138 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
139 "http://foosea%20rch/bar" },
140 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
141 "http://foosea%20rch/bar?boo=abc" },
142 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
143 "http://foo/?boo=sea+rch%2Fbar" },
144 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
145 "http://en.wikipedia.org/wiki/%3F" }
147 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(search_term_cases
); ++i
) {
148 const SearchTermsCase
& value
= search_term_cases
[i
];
149 TemplateURLData data
;
150 data
.SetURL(value
.url
);
151 TemplateURL
url(data
);
152 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
153 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
154 GURL
result(url
.url_ref().ReplaceSearchTerms(
155 TemplateURLRef::SearchTermsArgs(value
.terms
), search_terms_data_
));
156 ASSERT_TRUE(result
.is_valid());
157 EXPECT_EQ(value
.output
, result
.spec());
161 TEST_F(TemplateURLTest
, URLRefTestCount
) {
162 TemplateURLData data
;
163 data
.SetURL("http://foo{searchTerms}{count?}");
164 TemplateURL
url(data
);
165 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
166 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
167 GURL
result(url
.url_ref().ReplaceSearchTerms(
168 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
169 ASSERT_TRUE(result
.is_valid());
170 EXPECT_EQ("http://foox/", result
.spec());
173 TEST_F(TemplateURLTest
, URLRefTestCount2
) {
174 TemplateURLData data
;
175 data
.SetURL("http://foo{searchTerms}{count}");
176 TemplateURL
url(data
);
177 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
178 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
179 GURL
result(url
.url_ref().ReplaceSearchTerms(
180 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
181 ASSERT_TRUE(result
.is_valid());
182 EXPECT_EQ("http://foox10/", result
.spec());
185 TEST_F(TemplateURLTest
, URLRefTestIndices
) {
186 TemplateURLData data
;
187 data
.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
188 TemplateURL
url(data
);
189 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
190 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
191 GURL
result(url
.url_ref().ReplaceSearchTerms(
192 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
193 ASSERT_TRUE(result
.is_valid());
194 EXPECT_EQ("http://fooxxy/", result
.spec());
197 TEST_F(TemplateURLTest
, URLRefTestIndices2
) {
198 TemplateURLData data
;
199 data
.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
200 TemplateURL
url(data
);
201 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
202 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
203 GURL
result(url
.url_ref().ReplaceSearchTerms(
204 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
205 ASSERT_TRUE(result
.is_valid());
206 EXPECT_EQ("http://fooxx1y1/", result
.spec());
209 TEST_F(TemplateURLTest
, URLRefTestEncoding
) {
210 TemplateURLData data
;
211 data
.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
212 TemplateURL
url(data
);
213 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
214 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
215 GURL
result(url
.url_ref().ReplaceSearchTerms(
216 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
217 ASSERT_TRUE(result
.is_valid());
218 EXPECT_EQ("http://fooxxutf-8ya/", result
.spec());
221 TEST_F(TemplateURLTest
, URLRefTestImageURLWithPOST
) {
222 const char kInvalidPostParamsString
[] =
223 "unknown_template={UnknownTemplate},bad_value=bad{value},"
224 "{google:sbiSource}";
225 // List all accpectable parameter format in valid_post_params_string. it is
226 // expected like: "name0=,name1=value1,name2={template1}"
227 const char kValidPostParamsString
[] =
228 "image_content={google:imageThumbnail},image_url={google:imageURL},"
229 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
230 "constant_param=constant,width={google:imageOriginalWidth}";
231 const char KImageSearchURL
[] = "http://foo.com/sbi";
233 TemplateURLData data
;
234 data
.image_url
= KImageSearchURL
;
236 // Try to parse invalid post parameters.
237 data
.image_url_post_params
= kInvalidPostParamsString
;
238 TemplateURL
url_bad(data
);
239 ASSERT_FALSE(url_bad
.image_url_ref().IsValid(search_terms_data_
));
240 const TemplateURLRef::PostParams
& bad_post_params
=
241 url_bad
.image_url_ref().post_params_
;
242 ASSERT_EQ(2U, bad_post_params
.size());
243 EXPECT_EQ("unknown_template", bad_post_params
[0].first
);
244 EXPECT_EQ("{UnknownTemplate}", bad_post_params
[0].second
);
245 EXPECT_EQ("bad_value", bad_post_params
[1].first
);
246 EXPECT_EQ("bad{value}", bad_post_params
[1].second
);
248 // Try to parse valid post parameters.
249 data
.image_url_post_params
= kValidPostParamsString
;
250 TemplateURL
url(data
);
251 ASSERT_TRUE(url
.image_url_ref().IsValid(search_terms_data_
));
252 ASSERT_FALSE(url
.image_url_ref().SupportsReplacement(search_terms_data_
));
254 // Check term replacement.
255 TemplateURLRef::SearchTermsArgs
search_args(ASCIIToUTF16("X"));
256 search_args
.image_thumbnail_content
= "dummy-image-thumbnail";
257 search_args
.image_url
= GURL("http://dummyimage.com/dummy.jpg");
258 search_args
.image_original_size
= gfx::Size(10, 10);
259 // Replacement operation with no post_data buffer should still return
261 TestSearchTermsData
search_terms_data("http://X");
262 GURL
result(url
.image_url_ref().ReplaceSearchTerms(
263 search_args
, search_terms_data
));
264 ASSERT_TRUE(result
.is_valid());
265 EXPECT_EQ(KImageSearchURL
, result
.spec());
266 TemplateURLRef::PostContent post_content
;
267 result
= GURL(url
.image_url_ref().ReplaceSearchTerms(
268 search_args
, search_terms_data
, &post_content
));
269 ASSERT_TRUE(result
.is_valid());
270 EXPECT_EQ(KImageSearchURL
, result
.spec());
271 ASSERT_FALSE(post_content
.first
.empty());
272 ASSERT_FALSE(post_content
.second
.empty());
274 // Check parsed result of post parameters.
275 const TemplateURLRef::Replacements
& replacements
=
276 url
.image_url_ref().replacements_
;
277 const TemplateURLRef::PostParams
& post_params
=
278 url
.image_url_ref().post_params_
;
279 EXPECT_EQ(7U, post_params
.size());
280 for (TemplateURLRef::PostParams::const_iterator i
= post_params
.begin();
281 i
!= post_params
.end(); ++i
) {
282 TemplateURLRef::Replacements::const_iterator j
= replacements
.begin();
283 for (; j
!= replacements
.end(); ++j
) {
284 if (j
->is_post_param
&& j
->index
==
285 static_cast<size_t>(i
- post_params
.begin())) {
287 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH
:
288 EXPECT_EQ("width", i
->first
);
290 base::IntToString(search_args
.image_original_size
.width()),
293 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE
:
294 EXPECT_EQ("sbisrc", i
->first
);
295 EXPECT_EQ(search_terms_data
.GoogleImageSearchSource(), i
->second
);
297 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL
:
298 EXPECT_EQ("image_content", i
->first
);
299 EXPECT_EQ(search_args
.image_thumbnail_content
, i
->second
);
301 case TemplateURLRef::GOOGLE_IMAGE_URL
:
302 EXPECT_EQ("image_url", i
->first
);
303 EXPECT_EQ(search_args
.image_url
.spec(), i
->second
);
305 case TemplateURLRef::LANGUAGE
:
306 EXPECT_EQ("language", i
->first
);
307 EXPECT_EQ("en", i
->second
);
310 ADD_FAILURE(); // Should never go here.
315 if (j
!= replacements
.end())
317 if (i
->first
== "empty_param") {
318 EXPECT_TRUE(i
->second
.empty());
319 } else if (i
->first
== "sbisrc") {
320 EXPECT_FALSE(i
->second
.empty());
322 EXPECT_EQ("constant_param", i
->first
);
323 EXPECT_EQ("constant", i
->second
);
328 // Test that setting the prepopulate ID from TemplateURL causes the stored
329 // TemplateURLRef to handle parsing the URL parameters differently.
330 TEST_F(TemplateURLTest
, SetPrepopulatedAndParse
) {
331 TemplateURLData data
;
332 data
.SetURL("http://foo{fhqwhgads}bar");
333 TemplateURL
url(data
);
334 TemplateURLRef::Replacements replacements
;
336 EXPECT_EQ("http://foo{fhqwhgads}bar", url
.url_ref().ParseURL(
337 "http://foo{fhqwhgads}bar", &replacements
, NULL
, &valid
));
338 EXPECT_TRUE(replacements
.empty());
341 data
.prepopulate_id
= 123;
342 TemplateURL
url2(data
);
343 EXPECT_EQ("http://foobar", url2
.url_ref().ParseURL("http://foo{fhqwhgads}bar",
346 EXPECT_TRUE(replacements
.empty());
350 TEST_F(TemplateURLTest
, InputEncodingBeforeSearchTerm
) {
351 TemplateURLData data
;
352 data
.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
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 GURL
result(url
.url_ref().ReplaceSearchTerms(
357 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
358 ASSERT_TRUE(result
.is_valid());
359 EXPECT_EQ("http://fooxutf-8axyb/", result
.spec());
362 TEST_F(TemplateURLTest
, URLRefTestEncoding2
) {
363 TemplateURLData data
;
364 data
.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
365 TemplateURL
url(data
);
366 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
367 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
368 GURL
result(url
.url_ref().ReplaceSearchTerms(
369 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_
));
370 ASSERT_TRUE(result
.is_valid());
371 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result
.spec());
374 TEST_F(TemplateURLTest
, URLRefTestSearchTermsUsingTermsData
) {
375 struct SearchTermsCase
{
377 const base::string16 terms
;
379 } search_term_cases
[] = {
380 { "{google:baseURL}{language}{searchTerms}", base::string16(),
381 "http://example.com/e/en" },
382 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
383 "http://example.com/complete/" }
386 TestSearchTermsData
search_terms_data("http://example.com/e/");
387 TemplateURLData data
;
388 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(search_term_cases
); ++i
) {
389 const SearchTermsCase
& value
= search_term_cases
[i
];
390 data
.SetURL(value
.url
);
391 TemplateURL
url(data
);
392 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data
));
393 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data
));
394 GURL
result(url
.url_ref().ReplaceSearchTerms(
395 TemplateURLRef::SearchTermsArgs(value
.terms
), search_terms_data
, NULL
));
396 ASSERT_TRUE(result
.is_valid());
397 EXPECT_EQ(value
.output
, result
.spec());
401 TEST_F(TemplateURLTest
, URLRefTermToWide
) {
403 const char* encoded_search_term
;
404 const base::string16 expected_decoded_term
;
405 } to_wide_cases
[] = {
406 {"hello+world", ASCIIToUTF16("hello world")},
407 // Test some big-5 input.
408 {"%a7A%A6%6e+to+you", base::WideToUTF16(L
"\x4f60\x597d to you")},
409 // Test some UTF-8 input. We should fall back to this when the encoding
410 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
411 // Big-5 trailing byte.
412 {"%e4%bd%a05%e5%a5%bd+to+you",
413 base::WideToUTF16(L
"\x4f60\x35\x597d to you")},
414 // Undecodable input should stay escaped.
415 {"%91%01+abcd", base::WideToUTF16(L
"%91%01 abcd")},
416 // Make sure we convert %2B to +.
417 {"C%2B%2B", ASCIIToUTF16("C++")},
418 // C%2B is escaped as C%252B, make sure we unescape it properly.
419 {"C%252B", ASCIIToUTF16("C%2B")},
422 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
423 TemplateURLData data
;
424 data
.SetURL("http://foo?q={searchTerms}");
425 data
.input_encodings
.push_back("big-5");
426 TemplateURL
url(data
);
427 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
428 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
429 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(to_wide_cases
); i
++) {
430 EXPECT_EQ(to_wide_cases
[i
].expected_decoded_term
,
431 url
.url_ref().SearchTermToString16(
432 to_wide_cases
[i
].encoded_search_term
));
436 TEST_F(TemplateURLTest
, DisplayURLToURLRef
) {
438 const std::string url
;
439 const base::string16 expected_result
;
441 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
442 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
444 ASCIIToUTF16("http://X") },
445 { "http://foo{searchTerms",
446 ASCIIToUTF16("http://foo{searchTerms") },
447 { "http://foo{searchTerms}{language}",
448 ASCIIToUTF16("http://foo%s{language}") },
450 TemplateURLData data
;
451 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
452 data
.SetURL(test_data
[i
].url
);
453 TemplateURL
url(data
);
454 EXPECT_EQ(test_data
[i
].expected_result
,
455 url
.url_ref().DisplayURL(search_terms_data_
));
456 EXPECT_EQ(test_data
[i
].url
,
457 TemplateURLRef::DisplayURLToURLRef(
458 url
.url_ref().DisplayURL(search_terms_data_
)));
462 TEST_F(TemplateURLTest
, ReplaceSearchTerms
) {
464 const std::string url
;
465 const std::string expected_result
;
467 { "http://foo/{language}{searchTerms}{inputEncoding}",
468 "http://foo/{language}XUTF-8" },
469 { "http://foo/{language}{inputEncoding}{searchTerms}",
470 "http://foo/{language}UTF-8X" },
471 { "http://foo/{searchTerms}{language}{inputEncoding}",
472 "http://foo/X{language}UTF-8" },
473 { "http://foo/{searchTerms}{inputEncoding}{language}",
474 "http://foo/XUTF-8{language}" },
475 { "http://foo/{inputEncoding}{searchTerms}{language}",
476 "http://foo/UTF-8X{language}" },
477 { "http://foo/{inputEncoding}{language}{searchTerms}",
478 "http://foo/UTF-8{language}X" },
479 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
480 "http://foo/{language}aXaUTF-8a" },
481 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
482 "http://foo/{language}aUTF-8aXa" },
483 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
484 "http://foo/Xa{language}aUTF-8a" },
485 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
486 "http://foo/XaUTF-8a{language}a" },
487 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
488 "http://foo/UTF-8aXa{language}a" },
489 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
490 "http://foo/UTF-8a{language}aXa" },
492 TemplateURLData data
;
493 data
.input_encodings
.push_back("UTF-8");
494 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
495 data
.SetURL(test_data
[i
].url
);
496 TemplateURL
url(data
);
497 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
498 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
499 std::string expected_result
= test_data
[i
].expected_result
;
500 ReplaceSubstringsAfterOffset(&expected_result
, 0, "{language}",
501 search_terms_data_
.GetApplicationLocale());
502 GURL
result(url
.url_ref().ReplaceSearchTerms(
503 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
504 search_terms_data_
));
505 ASSERT_TRUE(result
.is_valid());
506 EXPECT_EQ(expected_result
, result
.spec());
511 // Tests replacing search terms in various encodings and making sure the
512 // generated URL matches the expected value.
513 TEST_F(TemplateURLTest
, ReplaceArbitrarySearchTerms
) {
515 const std::string encoding
;
516 const base::string16 search_term
;
517 const std::string url
;
518 const std::string expected_result
;
520 { "BIG5", base::WideToUTF16(L
"\x60BD"),
521 "http://foo/?{searchTerms}{inputEncoding}",
522 "http://foo/?%B1~BIG5" },
523 { "UTF-8", ASCIIToUTF16("blah"),
524 "http://foo/?{searchTerms}{inputEncoding}",
525 "http://foo/?blahUTF-8" },
526 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
527 "http://foo/{searchTerms}/bar",
528 "http://foo/%82%A0/bar"},
529 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
530 "http://foo/{searchTerms}/bar",
531 "http://foo/%82%A0%20%82%A2/bar"},
533 TemplateURLData data
;
534 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
535 data
.SetURL(test_data
[i
].url
);
536 data
.input_encodings
.clear();
537 data
.input_encodings
.push_back(test_data
[i
].encoding
);
538 TemplateURL
url(data
);
539 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
540 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
541 GURL
result(url
.url_ref().ReplaceSearchTerms(
542 TemplateURLRef::SearchTermsArgs(test_data
[i
].search_term
),
543 search_terms_data_
));
544 ASSERT_TRUE(result
.is_valid());
545 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
549 // Tests replacing assisted query stats (AQS) in various scenarios.
550 TEST_F(TemplateURLTest
, ReplaceAssistedQueryStats
) {
552 const base::string16 search_term
;
553 const std::string aqs
;
554 const std::string base_url
;
555 const std::string url
;
556 const std::string expected_result
;
559 { ASCIIToUTF16("foo"),
562 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
564 // HTTPS available, AQS should be replaced.
565 { ASCIIToUTF16("foo"),
568 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
569 "https://foo/?fooaqs=chrome.0.0l6&" },
570 // HTTPS available, however AQS is empty.
571 { ASCIIToUTF16("foo"),
574 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
575 "https://foo/?foo" },
576 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
577 { ASCIIToUTF16("foo"),
579 "http://www.google.com",
580 "http://foo?{searchTerms}{google:assistedQueryStats}",
582 // A non-Google search provider with HTTPS should allow AQS.
583 { ASCIIToUTF16("foo"),
585 "https://www.google.com",
586 "https://foo?{searchTerms}{google:assistedQueryStats}",
587 "https://foo/?fooaqs=chrome.0.0l6&" },
589 TemplateURLData data
;
590 data
.input_encodings
.push_back("UTF-8");
591 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
592 data
.SetURL(test_data
[i
].url
);
593 TemplateURL
url(data
);
594 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
595 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
596 TemplateURLRef::SearchTermsArgs
search_terms_args(test_data
[i
].search_term
);
597 search_terms_args
.assisted_query_stats
= test_data
[i
].aqs
;
598 search_terms_data_
.set_google_base_url(test_data
[i
].base_url
);
599 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
600 search_terms_data_
));
601 ASSERT_TRUE(result
.is_valid());
602 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
606 // Tests replacing cursor position.
607 TEST_F(TemplateURLTest
, ReplaceCursorPosition
) {
609 const base::string16 search_term
;
610 size_t cursor_position
;
611 const std::string url
;
612 const std::string expected_result
;
614 { ASCIIToUTF16("foo"),
615 base::string16::npos
,
616 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
617 "http://www.google.com/?foo&" },
618 { ASCIIToUTF16("foo"),
620 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
621 "http://www.google.com/?foo&cp=2&" },
622 { ASCIIToUTF16("foo"),
624 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
625 "http://www.google.com/?foo&cp=15&" },
627 TemplateURLData data
;
628 data
.input_encodings
.push_back("UTF-8");
629 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
630 data
.SetURL(test_data
[i
].url
);
631 TemplateURL
url(data
);
632 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
633 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
634 TemplateURLRef::SearchTermsArgs
search_terms_args(test_data
[i
].search_term
);
635 search_terms_args
.cursor_position
= test_data
[i
].cursor_position
;
636 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
637 search_terms_data_
));
638 ASSERT_TRUE(result
.is_valid());
639 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
643 // Tests replacing input type (&oit=).
644 TEST_F(TemplateURLTest
, ReplaceInputType
) {
646 const base::string16 search_term
;
647 metrics::OmniboxInputType::Type input_type
;
648 const std::string url
;
649 const std::string expected_result
;
651 { ASCIIToUTF16("foo"),
652 metrics::OmniboxInputType::UNKNOWN
,
653 "{google:baseURL}?{searchTerms}&{google:inputType}",
654 "http://www.google.com/?foo&oit=1&" },
655 { ASCIIToUTF16("foo"),
656 metrics::OmniboxInputType::URL
,
657 "{google:baseURL}?{searchTerms}&{google:inputType}",
658 "http://www.google.com/?foo&oit=3&" },
659 { ASCIIToUTF16("foo"),
660 metrics::OmniboxInputType::FORCED_QUERY
,
661 "{google:baseURL}?{searchTerms}&{google:inputType}",
662 "http://www.google.com/?foo&oit=5&" },
664 TemplateURLData data
;
665 data
.input_encodings
.push_back("UTF-8");
666 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
667 data
.SetURL(test_data
[i
].url
);
668 TemplateURL
url(data
);
669 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
670 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
671 TemplateURLRef::SearchTermsArgs
search_terms_args(test_data
[i
].search_term
);
672 search_terms_args
.input_type
= test_data
[i
].input_type
;
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 // Tests replacing currentPageUrl.
681 TEST_F(TemplateURLTest
, ReplaceCurrentPageUrl
) {
683 const base::string16 search_term
;
684 const std::string current_page_url
;
685 const std::string url
;
686 const std::string expected_result
;
688 { ASCIIToUTF16("foo"),
689 "http://www.google.com/",
690 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
691 "http://www.google.com/?foo&url=http%3A%2F%2Fwww.google.com%2F&" },
692 { ASCIIToUTF16("foo"),
694 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
695 "http://www.google.com/?foo&" },
696 { ASCIIToUTF16("foo"),
697 "http://g.com/+-/*&=",
698 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
699 "http://www.google.com/?foo&url=http%3A%2F%2Fg.com%2F%2B-%2F*%26%3D&" },
701 TemplateURLData data
;
702 data
.input_encodings
.push_back("UTF-8");
703 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
704 data
.SetURL(test_data
[i
].url
);
705 TemplateURL
url(data
);
706 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
707 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
708 TemplateURLRef::SearchTermsArgs
search_terms_args(test_data
[i
].search_term
);
709 search_terms_args
.current_page_url
= test_data
[i
].current_page_url
;
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
, OmniboxStartmargin
) {
719 const bool enable_omnibox_start_margin
;
720 const int omnibox_start_margin
;
721 const std::string expected_result
;
725 "http://bar/foo?q=foobar" },
728 "http://bar/foo?es_sm=0&q=foobar" },
731 "http://bar/foo?es_sm=42&q=foobar" },
733 TemplateURLData data
;
734 data
.SetURL("http://bar/foo?{google:omniboxStartMarginParameter}"
736 data
.input_encodings
.push_back("UTF-8");
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 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
741 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foobar"));
742 search_terms_args
.enable_omnibox_start_margin
=
743 test_data
[i
].enable_omnibox_start_margin
;
744 search_terms_data_
.set_omnibox_start_margin(
745 test_data
[i
].omnibox_start_margin
);
746 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
747 search_terms_data_
));
748 ASSERT_TRUE(result
.is_valid());
749 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
753 TEST_F(TemplateURLTest
, Suggestions
) {
755 const int accepted_suggestion
;
756 const base::string16 original_query_for_suggestion
;
757 const std::string expected_result
;
759 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE
, base::string16(),
760 "http://bar/foo?q=foobar" },
761 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE
, ASCIIToUTF16("foo"),
762 "http://bar/foo?q=foobar" },
763 { TemplateURLRef::NO_SUGGESTION_CHOSEN
, base::string16(),
764 "http://bar/foo?q=foobar" },
765 { TemplateURLRef::NO_SUGGESTION_CHOSEN
, ASCIIToUTF16("foo"),
766 "http://bar/foo?q=foobar" },
767 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
768 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
770 TemplateURLData data
;
771 data
.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
773 data
.input_encodings
.push_back("UTF-8");
774 TemplateURL
url(data
);
775 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
776 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
777 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
778 TemplateURLRef::SearchTermsArgs
search_terms_args(
779 ASCIIToUTF16("foobar"));
780 search_terms_args
.accepted_suggestion
= test_data
[i
].accepted_suggestion
;
781 search_terms_args
.original_query
=
782 test_data
[i
].original_query_for_suggestion
;
783 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
784 search_terms_data_
));
785 ASSERT_TRUE(result
.is_valid());
786 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
790 TEST_F(TemplateURLTest
, RLZ
) {
791 base::string16 rlz_string
= search_terms_data_
.GetRlzParameterValue(false);
793 TemplateURLData data
;
794 data
.SetURL("http://bar/?{google:RLZ}{searchTerms}");
795 TemplateURL
url(data
);
796 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
797 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
798 GURL
result(url
.url_ref().ReplaceSearchTerms(
799 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_
));
800 ASSERT_TRUE(result
.is_valid());
801 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string
) + "&x",
805 TEST_F(TemplateURLTest
, RLZFromAppList
) {
806 base::string16 rlz_string
= search_terms_data_
.GetRlzParameterValue(true);
808 TemplateURLData data
;
809 data
.SetURL("http://bar/?{google:RLZ}{searchTerms}");
810 TemplateURL
url(data
);
811 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
812 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
813 TemplateURLRef::SearchTermsArgs
args(ASCIIToUTF16("x"));
814 args
.from_app_list
= true;
815 GURL
result(url
.url_ref().ReplaceSearchTerms(args
, search_terms_data_
));
816 ASSERT_TRUE(result
.is_valid());
817 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string
) + "&x",
821 TEST_F(TemplateURLTest
, HostAndSearchTermKey
) {
823 const std::string url
;
824 const std::string host
;
825 const std::string path
;
826 const std::string search_term_key
;
828 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
830 // No query key should result in empty values.
831 { "http://blah/{searchTerms}", "", "", ""},
833 // No term should result in empty values.
834 { "http://blah/", "", "", ""},
836 // Multiple terms should result in empty values.
837 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
839 // Term in the host shouldn't match.
840 { "http://{searchTerms}", "", "", ""},
842 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
843 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
845 // Single term with extra chars in value should match.
846 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
849 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
850 TemplateURLData data
;
851 data
.SetURL(test_data
[i
].url
);
852 TemplateURL
url(data
);
853 EXPECT_EQ(test_data
[i
].host
, url
.url_ref().GetHost(search_terms_data_
));
854 EXPECT_EQ(test_data
[i
].path
, url
.url_ref().GetPath(search_terms_data_
));
855 EXPECT_EQ(test_data
[i
].search_term_key
,
856 url
.url_ref().GetSearchTermKey(search_terms_data_
));
860 TEST_F(TemplateURLTest
, GoogleBaseSuggestURL
) {
861 static const struct {
862 const char* const base_url
;
863 const char* const base_suggest_url
;
865 { "http://google.com/", "http://google.com/complete/", },
866 { "http://www.google.com/", "http://www.google.com/complete/", },
867 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
868 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
869 { "http://google.com/intl/xx/", "http://google.com/complete/", },
872 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(data
); ++i
)
873 CheckSuggestBaseURL(data
[i
].base_url
, data
[i
].base_suggest_url
);
876 TEST_F(TemplateURLTest
, ParseParameterKnown
) {
877 std::string
parsed_url("{searchTerms}");
878 TemplateURLData data
;
879 data
.SetURL(parsed_url
);
880 TemplateURL
url(data
);
881 TemplateURLRef::Replacements replacements
;
882 EXPECT_TRUE(url
.url_ref().ParseParameter(0, 12, &parsed_url
, &replacements
));
883 EXPECT_EQ(std::string(), parsed_url
);
884 ASSERT_EQ(1U, replacements
.size());
885 EXPECT_EQ(0U, replacements
[0].index
);
886 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS
, replacements
[0].type
);
889 TEST_F(TemplateURLTest
, ParseParameterUnknown
) {
890 std::string
parsed_url("{fhqwhgads}abc");
891 TemplateURLData data
;
892 data
.SetURL(parsed_url
);
893 TemplateURL
url(data
);
894 TemplateURLRef::Replacements replacements
;
896 // By default, TemplateURLRef should not consider itself prepopulated.
897 // Therefore we should not replace the unknown parameter.
898 EXPECT_FALSE(url
.url_ref().ParseParameter(0, 10, &parsed_url
, &replacements
));
899 EXPECT_EQ("{fhqwhgads}abc", parsed_url
);
900 EXPECT_TRUE(replacements
.empty());
902 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
903 parsed_url
= "{fhqwhgads}abc";
904 data
.prepopulate_id
= 1;
905 TemplateURL
url2(data
);
906 EXPECT_TRUE(url2
.url_ref().ParseParameter(0, 10, &parsed_url
, &replacements
));
907 EXPECT_EQ("abc", parsed_url
);
908 EXPECT_TRUE(replacements
.empty());
911 TEST_F(TemplateURLTest
, ParseURLEmpty
) {
912 TemplateURL
url((TemplateURLData()));
913 TemplateURLRef::Replacements replacements
;
915 EXPECT_EQ(std::string(),
916 url
.url_ref().ParseURL(std::string(), &replacements
, NULL
, &valid
));
917 EXPECT_TRUE(replacements
.empty());
921 TEST_F(TemplateURLTest
, ParseURLNoTemplateEnd
) {
922 TemplateURLData data
;
924 TemplateURL
url(data
);
925 TemplateURLRef::Replacements replacements
;
927 EXPECT_EQ(std::string(), url
.url_ref().ParseURL("{", &replacements
, NULL
,
929 EXPECT_TRUE(replacements
.empty());
933 TEST_F(TemplateURLTest
, ParseURLNoKnownParameters
) {
934 TemplateURLData data
;
936 TemplateURL
url(data
);
937 TemplateURLRef::Replacements replacements
;
939 EXPECT_EQ("{}", url
.url_ref().ParseURL("{}", &replacements
, NULL
, &valid
));
940 EXPECT_TRUE(replacements
.empty());
944 TEST_F(TemplateURLTest
, ParseURLTwoParameters
) {
945 TemplateURLData data
;
946 data
.SetURL("{}{{%s}}");
947 TemplateURL
url(data
);
948 TemplateURLRef::Replacements replacements
;
951 url
.url_ref().ParseURL("{}{{searchTerms}}", &replacements
, NULL
,
953 ASSERT_EQ(1U, replacements
.size());
954 EXPECT_EQ(3U, replacements
[0].index
);
955 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS
, replacements
[0].type
);
959 TEST_F(TemplateURLTest
, ParseURLNestedParameter
) {
960 TemplateURLData data
;
962 TemplateURL
url(data
);
963 TemplateURLRef::Replacements replacements
;
966 url
.url_ref().ParseURL("{{searchTerms}", &replacements
, NULL
,
968 ASSERT_EQ(1U, replacements
.size());
969 EXPECT_EQ(1U, replacements
[0].index
);
970 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS
, replacements
[0].type
);
974 TEST_F(TemplateURLTest
, SearchClient
) {
975 const std::string
base_url_str("http://google.com/?");
976 const std::string
terms_str("{searchTerms}&{google:searchClient}");
977 const std::string full_url_str
= base_url_str
+ terms_str
;
978 const base::string16
terms(ASCIIToUTF16(terms_str
));
979 search_terms_data_
.set_google_base_url(base_url_str
);
981 TemplateURLData data
;
982 data
.SetURL(full_url_str
);
983 TemplateURL
url(data
);
984 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
985 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
986 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foobar"));
988 // Check that the URL is correct when a client is not present.
989 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
990 search_terms_data_
));
991 ASSERT_TRUE(result
.is_valid());
992 EXPECT_EQ("http://google.com/?foobar&", result
.spec());
994 // Check that the URL is correct when a client is present.
995 search_terms_data_
.set_search_client("search_client");
996 GURL
result_2(url
.url_ref().ReplaceSearchTerms(search_terms_args
,
997 search_terms_data_
));
998 ASSERT_TRUE(result_2
.is_valid());
999 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2
.spec());
1002 TEST_F(TemplateURLTest
, GetURLNoInstantURL
) {
1003 TemplateURLData data
;
1004 data
.SetURL("http://google.com/?q={searchTerms}");
1005 data
.suggestions_url
= "http://google.com/suggest?q={searchTerms}";
1006 data
.alternate_urls
.push_back("http://google.com/alt?q={searchTerms}");
1007 data
.alternate_urls
.push_back("{google:baseURL}/alt/#q={searchTerms}");
1008 TemplateURL
url(data
);
1009 ASSERT_EQ(3U, url
.URLCount());
1010 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url
.GetURL(0));
1011 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url
.GetURL(1));
1012 EXPECT_EQ("http://google.com/?q={searchTerms}", url
.GetURL(2));
1015 TEST_F(TemplateURLTest
, GetURLNoSuggestionsURL
) {
1016 TemplateURLData data
;
1017 data
.SetURL("http://google.com/?q={searchTerms}");
1018 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
1019 data
.alternate_urls
.push_back("http://google.com/alt?q={searchTerms}");
1020 data
.alternate_urls
.push_back("{google:baseURL}/alt/#q={searchTerms}");
1021 TemplateURL
url(data
);
1022 ASSERT_EQ(3U, url
.URLCount());
1023 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url
.GetURL(0));
1024 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url
.GetURL(1));
1025 EXPECT_EQ("http://google.com/?q={searchTerms}", url
.GetURL(2));
1028 TEST_F(TemplateURLTest
, GetURLOnlyOneURL
) {
1029 TemplateURLData data
;
1030 data
.SetURL("http://www.google.co.uk/");
1031 TemplateURL
url(data
);
1032 ASSERT_EQ(1U, url
.URLCount());
1033 EXPECT_EQ("http://www.google.co.uk/", url
.GetURL(0));
1036 TEST_F(TemplateURLTest
, ExtractSearchTermsFromURL
) {
1037 TemplateURLData data
;
1038 data
.SetURL("http://google.com/?q={searchTerms}");
1039 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
1040 data
.alternate_urls
.push_back("http://google.com/alt/#q={searchTerms}");
1041 data
.alternate_urls
.push_back(
1042 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1043 TemplateURL
url(data
);
1044 base::string16 result
;
1046 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1047 GURL("http://google.com/?q=something"), search_terms_data_
, &result
));
1048 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1050 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1051 GURL("http://google.com/?espv&q=something"),
1052 search_terms_data_
, &result
));
1053 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1055 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1056 GURL("http://google.com/?espv=1&q=something"),
1057 search_terms_data_
, &result
));
1058 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1060 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1061 GURL("http://google.com/?espv=0&q=something"),
1062 search_terms_data_
, &result
));
1063 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1065 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1066 GURL("http://google.com/alt/#q=something"),
1067 search_terms_data_
, &result
));
1068 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1070 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1071 GURL("http://google.com/alt/#espv&q=something"),
1072 search_terms_data_
, &result
));
1073 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1075 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1076 GURL("http://google.com/alt/#espv=1&q=something"),
1077 search_terms_data_
, &result
));
1078 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1080 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1081 GURL("http://google.com/alt/#espv=0&q=something"),
1082 search_terms_data_
, &result
));
1083 EXPECT_EQ(ASCIIToUTF16("something"), result
);
1085 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1086 GURL("http://google.ca/?q=something"), search_terms_data_
, &result
));
1087 EXPECT_EQ(base::string16(), result
);
1089 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1090 GURL("http://google.ca/?q=something&q=anything"),
1091 search_terms_data_
, &result
));
1092 EXPECT_EQ(base::string16(), result
);
1094 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1095 GURL("http://google.com/foo/?q=foo"), search_terms_data_
, &result
));
1096 EXPECT_EQ(base::string16(), result
);
1098 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1099 GURL("https://google.com/?q=foo"), search_terms_data_
, &result
));
1100 EXPECT_EQ(ASCIIToUTF16("foo"), result
);
1102 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1103 GURL("http://google.com:8080/?q=foo"), search_terms_data_
, &result
));
1104 EXPECT_EQ(base::string16(), result
);
1106 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1107 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_
, &result
));
1108 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result
);
1110 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1111 GURL("http://google.com/alt/?q=123#q=456"),
1112 search_terms_data_
, &result
));
1113 EXPECT_EQ(ASCIIToUTF16("456"), result
);
1115 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1116 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1117 search_terms_data_
, &result
));
1118 EXPECT_EQ(ASCIIToUTF16("123"), result
);
1120 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(GURL(
1121 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1122 search_terms_data_
, &result
));
1123 EXPECT_EQ(ASCIIToUTF16("789"), result
);
1125 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1126 GURL("http://google.com/alt/?q="), search_terms_data_
, &result
));
1127 EXPECT_EQ(base::string16(), result
);
1129 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1130 GURL("http://google.com/alt/?#q="), search_terms_data_
, &result
));
1131 EXPECT_EQ(base::string16(), result
);
1133 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1134 GURL("http://google.com/alt/?q=#q="), search_terms_data_
, &result
));
1135 EXPECT_EQ(base::string16(), result
);
1137 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1138 GURL("http://google.com/alt/?q=123#q="), search_terms_data_
, &result
));
1139 EXPECT_EQ(base::string16(), result
);
1141 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1142 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_
, &result
));
1143 EXPECT_EQ(ASCIIToUTF16("123"), result
);
1146 TEST_F(TemplateURLTest
, HasSearchTermsReplacementKey
) {
1147 TemplateURLData data
;
1148 data
.SetURL("http://google.com/?q={searchTerms}");
1149 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
1150 data
.alternate_urls
.push_back("http://google.com/alt/#q={searchTerms}");
1151 data
.alternate_urls
.push_back(
1152 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1153 data
.search_terms_replacement_key
= "espv";
1154 TemplateURL
url(data
);
1156 // Test with instant enabled required.
1157 EXPECT_FALSE(url
.HasSearchTermsReplacementKey(
1158 GURL("http://google.com/")));
1160 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1161 GURL("http://google.com/?espv")));
1163 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1164 GURL("http://google.com/#espv")));
1166 EXPECT_FALSE(url
.HasSearchTermsReplacementKey(
1167 GURL("http://google.com/?q=something")));
1169 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1170 GURL("http://google.com/?q=something&espv")));
1172 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1173 GURL("http://google.com/?q=something&espv=1")));
1175 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1176 GURL("http://google.com/?q=something&espv=0")));
1178 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1179 GURL("http://google.com/?espv&q=something")));
1181 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1182 GURL("http://google.com/?espv=1&q=something")));
1184 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1185 GURL("http://google.com/?espv=0&q=something")));
1187 EXPECT_FALSE(url
.HasSearchTermsReplacementKey(
1188 GURL("http://google.com/alt/#q=something")));
1190 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1191 GURL("http://google.com/alt/#q=something&espv")));
1193 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1194 GURL("http://google.com/alt/#q=something&espv=1")));
1196 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1197 GURL("http://google.com/alt/#q=something&espv=0")));
1199 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1200 GURL("http://google.com/alt/#espv&q=something")));
1202 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1203 GURL("http://google.com/alt/#espv=1&q=something")));
1205 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1206 GURL("http://google.com/alt/#espv=0&q=something")));
1208 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1209 GURL("http://google.com/?espv#q=something")));
1211 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1212 GURL("http://google.com/?espv=1#q=something")));
1214 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1215 GURL("http://google.com/?q=something#espv")));
1217 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1218 GURL("http://google.com/?q=something#espv=1")));
1220 // This does not ensure the domain matches.
1221 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1222 GURL("http://bing.com/?espv")));
1224 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1225 GURL("http://bing.com/#espv")));
1228 TEST_F(TemplateURLTest
, ReplaceSearchTermsInURL
) {
1229 TemplateURLData data
;
1230 data
.SetURL("http://google.com/?q={searchTerms}");
1231 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
1232 data
.alternate_urls
.push_back("http://google.com/alt/#q={searchTerms}");
1233 data
.alternate_urls
.push_back(
1234 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1235 TemplateURL
url(data
);
1236 TemplateURLRef::SearchTermsArgs
search_terms(ASCIIToUTF16("Bob Morane"));
1239 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1240 GURL("http://google.com/?q=something"), search_terms
,
1241 search_terms_data_
, &result
));
1242 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result
);
1244 result
= GURL("http://should.not.change.com");
1245 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1246 GURL("http://google.ca/?q=something"), search_terms
,
1247 search_terms_data_
, &result
));
1248 EXPECT_EQ(GURL("http://should.not.change.com"), result
);
1250 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1251 GURL("http://google.com/foo/?q=foo"), search_terms
,
1252 search_terms_data_
, &result
));
1254 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1255 GURL("https://google.com/?q=foo"), search_terms
,
1256 search_terms_data_
, &result
));
1257 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result
);
1259 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1260 GURL("http://google.com:8080/?q=foo"), search_terms
,
1261 search_terms_data_
, &result
));
1263 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1264 GURL("http://google.com/?q=1+2+3&b=456"), search_terms
,
1265 search_terms_data_
, &result
));
1266 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result
);
1268 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1269 // template_url.cc for details.
1270 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1271 GURL("http://google.com/alt/?q=123#q=456"), search_terms
,
1272 search_terms_data_
, &result
));
1273 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result
);
1275 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1276 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms
,
1277 search_terms_data_
, &result
));
1278 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"),
1281 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1282 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1283 search_terms
, search_terms_data_
, &result
));
1284 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1285 "#j=abc&q=Bob Morane&h=def9"), result
);
1287 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1288 GURL("http://google.com/alt/?q="), search_terms
,
1289 search_terms_data_
, &result
));
1291 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1292 GURL("http://google.com/alt/?#q="), search_terms
,
1293 search_terms_data_
, &result
));
1295 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1296 GURL("http://google.com/alt/?q=#q="), search_terms
,
1297 search_terms_data_
, &result
));
1299 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1300 GURL("http://google.com/alt/?q=123#q="), search_terms
,
1301 search_terms_data_
, &result
));
1303 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1304 GURL("http://google.com/alt/?q=#q=123"), search_terms
,
1305 search_terms_data_
, &result
));
1306 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result
);
1309 // Test the |suggest_query_params| field of SearchTermsArgs.
1310 TEST_F(TemplateURLTest
, SuggestQueryParams
) {
1311 TemplateURLData data
;
1312 // Pick a URL with replacements before, during, and after the query, to ensure
1313 // we don't goof up any of them.
1314 data
.SetURL("{google:baseURL}search?q={searchTerms}"
1315 "#{google:originalQueryForSuggestion}x");
1316 TemplateURL
url(data
);
1318 // Baseline: no |suggest_query_params| field.
1319 TemplateURLRef::SearchTermsArgs
search_terms(ASCIIToUTF16("abc"));
1320 search_terms
.original_query
= ASCIIToUTF16("def");
1321 search_terms
.accepted_suggestion
= 0;
1322 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1323 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1325 // Set the suggest_query_params.
1326 search_terms
.suggest_query_params
= "pq=xyz";
1327 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
1328 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1330 // Add extra_query_params in the mix, and ensure it works.
1331 search_terms
.append_extra_query_params
= true;
1332 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1333 switches::kExtraSearchQueryParams
, "a=b");
1334 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
1335 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1338 // Test the |append_extra_query_params| field of SearchTermsArgs.
1339 TEST_F(TemplateURLTest
, ExtraQueryParams
) {
1340 TemplateURLData data
;
1341 // Pick a URL with replacements before, during, and after the query, to ensure
1342 // we don't goof up any of them.
1343 data
.SetURL("{google:baseURL}search?q={searchTerms}"
1344 "#{google:originalQueryForSuggestion}x");
1345 TemplateURL
url(data
);
1347 // Baseline: no command-line args, no |append_extra_query_params| flag.
1348 TemplateURLRef::SearchTermsArgs
search_terms(ASCIIToUTF16("abc"));
1349 search_terms
.original_query
= ASCIIToUTF16("def");
1350 search_terms
.accepted_suggestion
= 0;
1351 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1352 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1354 // Set the flag. Since there are no command-line args, this should have no
1356 search_terms
.append_extra_query_params
= true;
1357 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1358 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1360 // Now append the command-line arg. This should be inserted into the query.
1361 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1362 switches::kExtraSearchQueryParams
, "a=b");
1363 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
1364 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1366 // Turn off the flag. Now the command-line arg should be ignored again.
1367 search_terms
.append_extra_query_params
= false;
1368 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1369 url
.url_ref().ReplaceSearchTerms(search_terms
, search_terms_data_
));
1372 // Tests replacing pageClassification.
1373 TEST_F(TemplateURLTest
, ReplacePageClassification
) {
1374 TemplateURLData data
;
1375 data
.input_encodings
.push_back("UTF-8");
1376 data
.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
1377 TemplateURL
url(data
);
1378 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
1379 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
1380 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1382 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1383 search_terms_data_
);
1384 EXPECT_EQ("http://www.google.com/?q=foo", result
);
1386 search_terms_args
.page_classification
= metrics::OmniboxEventProto::NTP
;
1387 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1388 search_terms_data_
);
1389 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result
);
1391 search_terms_args
.page_classification
=
1392 metrics::OmniboxEventProto::HOME_PAGE
;
1393 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1394 search_terms_data_
);
1395 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result
);
1398 // Test the IsSearchResults function.
1399 TEST_F(TemplateURLTest
, IsSearchResults
) {
1400 TemplateURLData data
;
1401 data
.SetURL("http://bar/search?q={searchTerms}");
1402 data
.instant_url
= "http://bar/instant#q={searchTerms}";
1403 data
.new_tab_url
= "http://bar/newtab";
1404 data
.alternate_urls
.push_back("http://bar/?q={searchTerms}");
1405 data
.alternate_urls
.push_back("http://bar/#q={searchTerms}");
1406 data
.alternate_urls
.push_back("http://bar/search#q{searchTerms}");
1407 data
.alternate_urls
.push_back("http://bar/webhp#q={searchTerms}");
1408 TemplateURL
search_provider(data
);
1411 const char* const url
;
1414 { "http://bar/search?q=foo&oq=foo", true, },
1415 { "http://bar/?q=foo&oq=foo", true, },
1416 { "http://bar/#output=search&q=foo&oq=foo", true, },
1417 { "http://bar/webhp#q=foo&oq=foo", true, },
1418 { "http://bar/#q=foo&oq=foo", true, },
1419 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1420 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1421 { "http://bar/", false, },
1422 { "http://foo/", false, },
1423 { "http://bar/newtab", false, },
1426 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(url_data
); ++i
) {
1427 EXPECT_EQ(url_data
[i
].result
,
1428 search_provider
.IsSearchURL(GURL(url_data
[i
].url
),
1429 search_terms_data_
));
1433 TEST_F(TemplateURLTest
, ReflectsBookmarkBarPinned
) {
1434 TemplateURLData data
;
1435 data
.input_encodings
.push_back("UTF-8");
1436 data
.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
1437 TemplateURL
url(data
);
1438 EXPECT_TRUE(url
.url_ref().IsValid(search_terms_data_
));
1439 ASSERT_TRUE(url
.url_ref().SupportsReplacement(search_terms_data_
));
1440 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1442 // Do not add the param when InstantExtended is suppressed on SRPs.
1443 search_terms_data_
.set_is_showing_search_terms_on_search_results_pages(false);
1444 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1445 search_terms_data_
);
1446 EXPECT_EQ("http://www.google.com/?q=foo", result
);
1448 // Add the param when InstantExtended is not suppressed on SRPs.
1449 search_terms_data_
.set_is_showing_search_terms_on_search_results_pages(true);
1450 search_terms_args
.bookmark_bar_pinned
= false;
1451 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1452 search_terms_data_
);
1453 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result
);
1455 search_terms_data_
.set_is_showing_search_terms_on_search_results_pages(true);
1456 search_terms_args
.bookmark_bar_pinned
= true;
1457 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1458 search_terms_data_
);
1459 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result
);
1462 TEST_F(TemplateURLTest
, AnswersHasVersion
) {
1463 TemplateURLData data
;
1464 search_terms_data_
.set_google_base_url("http://bar/");
1465 data
.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1467 TemplateURL
url(data
);
1468 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1469 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1470 search_terms_data_
);
1471 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result
);
1473 search_terms_data_
.set_enable_answers_in_suggest(true);
1474 TemplateURL
url2(data
);
1475 result
= url2
.url_ref().ReplaceSearchTerms(search_terms_args
,
1476 search_terms_data_
);
1477 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result
);
1480 TEST_F(TemplateURLTest
, SessionToken
) {
1481 TemplateURLData data
;
1482 search_terms_data_
.set_google_base_url("http://bar/");
1483 data
.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1485 TemplateURL
url(data
);
1486 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1487 search_terms_args
.session_token
= "SESSIONTOKENGOESHERE";
1488 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1489 search_terms_data_
);
1490 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result
);
1492 TemplateURL
url2(data
);
1493 search_terms_args
.session_token
= "";
1494 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1495 search_terms_data_
);
1496 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result
);
1499 TEST_F(TemplateURLTest
, ContextualSearchParameters
) {
1500 TemplateURLData data
;
1501 search_terms_data_
.set_google_base_url("http://bar/");
1502 data
.SetURL("http://bar/_/contextualsearch?"
1503 "{google:contextualSearchVersion}"
1504 "{google:contextualSearchContextData}");
1506 TemplateURL
url(data
);
1507 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1508 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1509 search_terms_data_
);
1510 EXPECT_EQ("http://bar/_/contextualsearch?ctxsl_resolve=1", result
);
1512 TemplateURLRef::SearchTermsArgs::ContextualSearchParams
params(
1513 1, 6, 11, "allen", "woody+allen+movies", "www.wikipedia.org",
1515 search_terms_args
.contextual_search_params
= params
;
1516 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
,
1517 search_terms_data_
);
1518 EXPECT_EQ("http://bar/_/contextualsearch?"
1523 "ctxs_content=woody+allen+movies&"
1524 "ctxsl_url=www.wikipedia.org&"
1525 "ctxs_encoding=utf-8&"
1530 TEST_F(TemplateURLTest
, GenerateKeyword
) {
1531 ASSERT_EQ(ASCIIToUTF16("foo"),
1532 TemplateURL::GenerateKeyword(GURL("http://foo")));
1533 // www. should be stripped.
1534 ASSERT_EQ(ASCIIToUTF16("foo"),
1535 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
1536 // Make sure we don't get a trailing '/'.
1537 ASSERT_EQ(ASCIIToUTF16("blah"),
1538 TemplateURL::GenerateKeyword(GURL("http://blah/")));
1539 // Don't generate the empty string.
1540 ASSERT_EQ(ASCIIToUTF16("www"),
1541 TemplateURL::GenerateKeyword(GURL("http://www.")));
1544 TEST_F(TemplateURLTest
, GenerateSearchURL
) {
1545 struct GenerateSearchURLCase
{
1546 const char* test_name
;
1548 const char* expected
;
1549 } generate_url_cases
[] = {
1550 { "invalid URL", "foo{searchTerms}", "" },
1551 { "URL with no replacements", "http://foo/", "http://foo/" },
1552 { "basic functionality", "http://foo/{searchTerms}",
1553 "http://foo/blah.blah.blah.blah.blah" }
1556 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(generate_url_cases
); ++i
) {
1557 TemplateURLData data
;
1558 data
.SetURL(generate_url_cases
[i
].url
);
1559 TemplateURL
t_url(data
);
1560 EXPECT_EQ(t_url
.GenerateSearchURL(search_terms_data_
).spec(),
1561 generate_url_cases
[i
].expected
)
1562 << generate_url_cases
[i
].test_name
<< " failed.";
1566 TEST_F(TemplateURLTest
, PrefetchQueryParameters
) {
1567 TemplateURLData data
;
1568 search_terms_data_
.set_google_base_url("http://bar/");
1569 data
.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1571 TemplateURL
url(data
);
1572 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1573 search_terms_args
.prefetch_query
= "full query text";
1574 search_terms_args
.prefetch_query_type
= "2338";
1575 std::string result
=
1576 url
.url_ref().ReplaceSearchTerms(search_terms_args
, search_terms_data_
);
1577 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1580 TemplateURL
url2(data
);
1581 search_terms_args
.prefetch_query
.clear();
1582 search_terms_args
.prefetch_query_type
.clear();
1584 url2
.url_ref().ReplaceSearchTerms(search_terms_args
, search_terms_data_
);
1585 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result
);