Upstreaming TransitionPageHelper.
[chromium-blink-merge.git] / components / search_engines / template_url_unittest.cc
blob83a7867ab14dd985cbb01236ea6e422610317ff8
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 {
21 public:
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());
42 // static
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) {
53 TemplateURLData data;
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) {
60 TemplateURLData data;
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 {
68 const char* url;
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];
83 TemplateURLData data;
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) {
96 TemplateURLData data;
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
193 // the parsed URL.
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())) {
219 switch (j->type) {
220 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
221 ExpectPostParamIs(*i, "width",
222 base::IntToString(
223 search_args.image_original_size.width()));
224 break;
225 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE:
226 ExpectPostParamIs(*i, "sbisrc",
227 search_terms_data.GoogleImageSearchSource());
228 break;
229 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
230 ExpectPostParamIs(*i, "image_content",
231 search_args.image_thumbnail_content,
232 "image/jpeg");
233 break;
234 case TemplateURLRef::GOOGLE_IMAGE_URL:
235 ExpectPostParamIs(*i, "image_url", search_args.image_url.spec());
236 break;
237 case TemplateURLRef::LANGUAGE:
238 ExpectPostParamIs(*i, "language", "en");
239 break;
240 default:
241 ADD_FAILURE(); // Should never go here.
243 break;
246 if (j != replacements.end())
247 continue;
248 if (i->name == "empty_param")
249 ExpectPostParamIs(*i, "empty_param", std::string());
250 else
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;
262 bool valid = false;
263 EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL(
264 "http://foo{fhqwhgads}bar", &replacements, NULL, &valid));
265 EXPECT_TRUE(replacements.empty());
266 EXPECT_TRUE(valid);
268 data.prepopulate_id = 123;
269 TemplateURL url2(data);
270 EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar",
271 &replacements, NULL,
272 &valid));
273 EXPECT_TRUE(replacements.empty());
274 EXPECT_TRUE(valid);
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 {
303 const char* url;
304 const base::string16 terms;
305 const char* output;
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) {
329 struct ToWideCase {
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) {
364 struct TestData {
365 const std::string url;
366 const base::string16 expected_result;
367 } test_data[] = {
368 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
369 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
370 { "http://X",
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) {
390 struct TestData {
391 const std::string url;
392 const std::string expected_result;
393 } test_data[] = {
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) {
441 struct TestData {
442 const std::string encoding;
443 const base::string16 search_term;
444 const std::string url;
445 const std::string expected_result;
446 } test_data[] = {
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) {
478 struct TestData {
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;
484 } test_data[] = {
485 // No HTTPS, no AQS.
486 { ASCIIToUTF16("foo"),
487 "chrome.0.0l6",
488 "http://foo/",
489 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
490 "http://foo/?foo" },
491 // HTTPS available, AQS should be replaced.
492 { ASCIIToUTF16("foo"),
493 "chrome.0.0l6",
494 "https://foo/",
495 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
496 "https://foo/?fooaqs=chrome.0.0l6&" },
497 // HTTPS available, however AQS is empty.
498 { ASCIIToUTF16("foo"),
500 "https://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"),
505 "chrome.0.0l6",
506 "http://www.google.com",
507 "http://foo?{searchTerms}{google:assistedQueryStats}",
508 "http://foo/?foo" },
509 // A non-Google search provider with HTTPS should allow AQS.
510 { ASCIIToUTF16("foo"),
511 "chrome.0.0l6",
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) {
535 struct TestData {
536 const base::string16 search_term;
537 size_t cursor_position;
538 const std::string url;
539 const std::string expected_result;
540 } test_data[] = {
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) {
572 struct TestData {
573 const base::string16 search_term;
574 metrics::OmniboxInputType::Type input_type;
575 const std::string url;
576 const std::string expected_result;
577 } test_data[] = {
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) {
609 struct TestData {
610 const base::string16 search_term;
611 const std::string current_page_url;
612 const std::string url;
613 const std::string expected_result;
614 } test_data[] = {
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) {
645 struct TestData {
646 const bool enable_omnibox_start_margin;
647 const int omnibox_start_margin;
648 const std::string expected_result;
649 } test_data[] = {
650 { false,
652 "http://bar/foo?q=foobar" },
653 { true,
655 "http://bar/foo?es_sm=0&q=foobar" },
656 { true,
658 "http://bar/foo?es_sm=42&q=foobar" },
660 TemplateURLData data;
661 data.SetURL("http://bar/foo?{google:omniboxStartMarginParameter}"
662 "q={searchTerms}");
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) {
681 struct TestData {
682 const int accepted_suggestion;
683 const base::string16 original_query_for_suggestion;
684 const std::string expected_result;
685 } test_data[] = {
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}"
699 "q={searchTerms}");
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",
729 result.spec());
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",
745 result.spec());
748 TEST_F(TemplateURLTest, HostAndSearchTermKey) {
749 struct TestData {
750 const std::string url;
751 const std::string host;
752 const std::string path;
753 const std::string search_term_key;
754 } test_data[] = {
755 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
757 // No query key should result in empty values.
758 { "http://blah/{searchTerms}", "", "", ""},
760 // No term should result in empty values.
761 { "http://blah/", "", "", ""},
763 // Multiple terms should result in empty values.
764 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
766 // Term in the host shouldn't match.
767 { "http://{searchTerms}", "", "", ""},
769 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
770 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
772 // Single term with extra chars in value should match.
773 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
776 for (size_t i = 0; i < arraysize(test_data); ++i) {
777 TemplateURLData data;
778 data.SetURL(test_data[i].url);
779 TemplateURL url(data);
780 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
781 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
782 EXPECT_EQ(test_data[i].search_term_key,
783 url.url_ref().GetSearchTermKey(search_terms_data_));
787 TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
788 static const struct {
789 const char* const base_url;
790 const char* const base_suggest_url;
791 } data[] = {
792 { "http://google.com/", "http://google.com/complete/", },
793 { "http://www.google.com/", "http://www.google.com/complete/", },
794 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
795 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
796 { "http://google.com/intl/xx/", "http://google.com/complete/", },
799 for (size_t i = 0; i < arraysize(data); ++i)
800 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
803 TEST_F(TemplateURLTest, ParseParameterKnown) {
804 std::string parsed_url("{searchTerms}");
805 TemplateURLData data;
806 data.SetURL(parsed_url);
807 TemplateURL url(data);
808 TemplateURLRef::Replacements replacements;
809 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
810 EXPECT_EQ(std::string(), parsed_url);
811 ASSERT_EQ(1U, replacements.size());
812 EXPECT_EQ(0U, replacements[0].index);
813 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
816 TEST_F(TemplateURLTest, ParseParameterUnknown) {
817 std::string parsed_url("{fhqwhgads}abc");
818 TemplateURLData data;
819 data.SetURL(parsed_url);
820 TemplateURL url(data);
821 TemplateURLRef::Replacements replacements;
823 // By default, TemplateURLRef should not consider itself prepopulated.
824 // Therefore we should not replace the unknown parameter.
825 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
826 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
827 EXPECT_TRUE(replacements.empty());
829 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
830 parsed_url = "{fhqwhgads}abc";
831 data.prepopulate_id = 1;
832 TemplateURL url2(data);
833 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
834 EXPECT_EQ("abc", parsed_url);
835 EXPECT_TRUE(replacements.empty());
838 TEST_F(TemplateURLTest, ParseURLEmpty) {
839 TemplateURL url((TemplateURLData()));
840 TemplateURLRef::Replacements replacements;
841 bool valid = false;
842 EXPECT_EQ(std::string(),
843 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
844 EXPECT_TRUE(replacements.empty());
845 EXPECT_TRUE(valid);
848 TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
849 TemplateURLData data;
850 data.SetURL("{");
851 TemplateURL url(data);
852 TemplateURLRef::Replacements replacements;
853 bool valid = false;
854 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
855 &valid));
856 EXPECT_TRUE(replacements.empty());
857 EXPECT_FALSE(valid);
860 TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
861 TemplateURLData data;
862 data.SetURL("{}");
863 TemplateURL url(data);
864 TemplateURLRef::Replacements replacements;
865 bool valid = false;
866 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
867 EXPECT_TRUE(replacements.empty());
868 EXPECT_TRUE(valid);
871 TEST_F(TemplateURLTest, ParseURLTwoParameters) {
872 TemplateURLData data;
873 data.SetURL("{}{{%s}}");
874 TemplateURL url(data);
875 TemplateURLRef::Replacements replacements;
876 bool valid = false;
877 EXPECT_EQ("{}{}",
878 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
879 &valid));
880 ASSERT_EQ(1U, replacements.size());
881 EXPECT_EQ(3U, replacements[0].index);
882 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
883 EXPECT_TRUE(valid);
886 TEST_F(TemplateURLTest, ParseURLNestedParameter) {
887 TemplateURLData data;
888 data.SetURL("{%s");
889 TemplateURL url(data);
890 TemplateURLRef::Replacements replacements;
891 bool valid = false;
892 EXPECT_EQ("{",
893 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
894 &valid));
895 ASSERT_EQ(1U, replacements.size());
896 EXPECT_EQ(1U, replacements[0].index);
897 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
898 EXPECT_TRUE(valid);
901 TEST_F(TemplateURLTest, SearchClient) {
902 const std::string base_url_str("http://google.com/?");
903 const std::string terms_str("{searchTerms}&{google:searchClient}");
904 const std::string full_url_str = base_url_str + terms_str;
905 const base::string16 terms(ASCIIToUTF16(terms_str));
906 search_terms_data_.set_google_base_url(base_url_str);
908 TemplateURLData data;
909 data.SetURL(full_url_str);
910 TemplateURL url(data);
911 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
912 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
913 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
915 // Check that the URL is correct when a client is not present.
916 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
917 search_terms_data_));
918 ASSERT_TRUE(result.is_valid());
919 EXPECT_EQ("http://google.com/?foobar&", result.spec());
921 // Check that the URL is correct when a client is present.
922 search_terms_data_.set_search_client("search_client");
923 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
924 search_terms_data_));
925 ASSERT_TRUE(result_2.is_valid());
926 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
929 TEST_F(TemplateURLTest, GetURLNoInstantURL) {
930 TemplateURLData data;
931 data.SetURL("http://google.com/?q={searchTerms}");
932 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
933 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
934 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
935 TemplateURL url(data);
936 ASSERT_EQ(3U, url.URLCount());
937 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
938 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
939 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
942 TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
943 TemplateURLData data;
944 data.SetURL("http://google.com/?q={searchTerms}");
945 data.instant_url = "http://google.com/instant#q={searchTerms}";
946 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
947 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
948 TemplateURL url(data);
949 ASSERT_EQ(3U, url.URLCount());
950 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
951 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
952 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
955 TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
956 TemplateURLData data;
957 data.SetURL("http://www.google.co.uk/");
958 TemplateURL url(data);
959 ASSERT_EQ(1U, url.URLCount());
960 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0));
963 TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
964 TemplateURLData data;
965 data.SetURL("http://google.com/?q={searchTerms}");
966 data.instant_url = "http://google.com/instant#q={searchTerms}";
967 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
968 data.alternate_urls.push_back(
969 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
970 TemplateURL url(data);
971 base::string16 result;
973 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
974 GURL("http://google.com/?q=something"), search_terms_data_, &result));
975 EXPECT_EQ(ASCIIToUTF16("something"), result);
977 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
978 GURL("http://google.com/?espv&q=something"),
979 search_terms_data_, &result));
980 EXPECT_EQ(ASCIIToUTF16("something"), result);
982 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
983 GURL("http://google.com/?espv=1&q=something"),
984 search_terms_data_, &result));
985 EXPECT_EQ(ASCIIToUTF16("something"), result);
987 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
988 GURL("http://google.com/?espv=0&q=something"),
989 search_terms_data_, &result));
990 EXPECT_EQ(ASCIIToUTF16("something"), result);
992 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
993 GURL("http://google.com/alt/#q=something"),
994 search_terms_data_, &result));
995 EXPECT_EQ(ASCIIToUTF16("something"), result);
997 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
998 GURL("http://google.com/alt/#espv&q=something"),
999 search_terms_data_, &result));
1000 EXPECT_EQ(ASCIIToUTF16("something"), result);
1002 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1003 GURL("http://google.com/alt/#espv=1&q=something"),
1004 search_terms_data_, &result));
1005 EXPECT_EQ(ASCIIToUTF16("something"), result);
1007 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1008 GURL("http://google.com/alt/#espv=0&q=something"),
1009 search_terms_data_, &result));
1010 EXPECT_EQ(ASCIIToUTF16("something"), result);
1012 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1013 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
1014 EXPECT_EQ(base::string16(), result);
1016 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1017 GURL("http://google.ca/?q=something&q=anything"),
1018 search_terms_data_, &result));
1019 EXPECT_EQ(base::string16(), result);
1021 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1022 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
1023 EXPECT_EQ(base::string16(), result);
1025 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1026 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
1027 EXPECT_EQ(ASCIIToUTF16("foo"), result);
1029 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1030 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
1031 EXPECT_EQ(base::string16(), result);
1033 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1034 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
1035 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1037 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1038 GURL("http://google.com/alt/?q=123#q=456"),
1039 search_terms_data_, &result));
1040 EXPECT_EQ(ASCIIToUTF16("456"), result);
1042 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1043 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1044 search_terms_data_, &result));
1045 EXPECT_EQ(ASCIIToUTF16("123"), result);
1047 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
1048 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1049 search_terms_data_, &result));
1050 EXPECT_EQ(ASCIIToUTF16("789"), result);
1052 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1053 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
1054 EXPECT_EQ(base::string16(), result);
1056 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1057 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
1058 EXPECT_EQ(base::string16(), result);
1060 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1061 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
1062 EXPECT_EQ(base::string16(), result);
1064 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1065 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
1066 EXPECT_EQ(base::string16(), result);
1068 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1069 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
1070 EXPECT_EQ(ASCIIToUTF16("123"), result);
1073 TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
1074 TemplateURLData data;
1075 data.SetURL("http://google.com/?q={searchTerms}");
1076 data.instant_url = "http://google.com/instant#q={searchTerms}";
1077 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1078 data.alternate_urls.push_back(
1079 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1080 data.search_terms_replacement_key = "espv";
1081 TemplateURL url(data);
1083 // Test with instant enabled required.
1084 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1085 GURL("http://google.com/")));
1087 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1088 GURL("http://google.com/?espv")));
1090 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1091 GURL("http://google.com/#espv")));
1093 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1094 GURL("http://google.com/?q=something")));
1096 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1097 GURL("http://google.com/?q=something&espv")));
1099 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1100 GURL("http://google.com/?q=something&espv=1")));
1102 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1103 GURL("http://google.com/?q=something&espv=0")));
1105 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1106 GURL("http://google.com/?espv&q=something")));
1108 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1109 GURL("http://google.com/?espv=1&q=something")));
1111 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1112 GURL("http://google.com/?espv=0&q=something")));
1114 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1115 GURL("http://google.com/alt/#q=something")));
1117 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1118 GURL("http://google.com/alt/#q=something&espv")));
1120 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1121 GURL("http://google.com/alt/#q=something&espv=1")));
1123 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1124 GURL("http://google.com/alt/#q=something&espv=0")));
1126 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1127 GURL("http://google.com/alt/#espv&q=something")));
1129 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1130 GURL("http://google.com/alt/#espv=1&q=something")));
1132 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1133 GURL("http://google.com/alt/#espv=0&q=something")));
1135 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1136 GURL("http://google.com/?espv#q=something")));
1138 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1139 GURL("http://google.com/?espv=1#q=something")));
1141 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1142 GURL("http://google.com/?q=something#espv")));
1144 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1145 GURL("http://google.com/?q=something#espv=1")));
1147 // This does not ensure the domain matches.
1148 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1149 GURL("http://bing.com/?espv")));
1151 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1152 GURL("http://bing.com/#espv")));
1155 TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1156 TemplateURLData data;
1157 data.SetURL("http://google.com/?q={searchTerms}");
1158 data.instant_url = "http://google.com/instant#q={searchTerms}";
1159 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1160 data.alternate_urls.push_back(
1161 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1162 TemplateURL url(data);
1163 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1164 GURL result;
1166 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1167 GURL("http://google.com/?q=something"), search_terms,
1168 search_terms_data_, &result));
1169 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result);
1171 result = GURL("http://should.not.change.com");
1172 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1173 GURL("http://google.ca/?q=something"), search_terms,
1174 search_terms_data_, &result));
1175 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1177 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1178 GURL("http://google.com/foo/?q=foo"), search_terms,
1179 search_terms_data_, &result));
1181 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1182 GURL("https://google.com/?q=foo"), search_terms,
1183 search_terms_data_, &result));
1184 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result);
1186 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1187 GURL("http://google.com:8080/?q=foo"), search_terms,
1188 search_terms_data_, &result));
1190 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1191 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1192 search_terms_data_, &result));
1193 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result);
1195 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1196 // template_url.cc for details.
1197 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1198 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1199 search_terms_data_, &result));
1200 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result);
1202 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1203 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
1204 search_terms_data_, &result));
1205 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"),
1206 result);
1208 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1209 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1210 search_terms, search_terms_data_, &result));
1211 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1212 "#j=abc&q=Bob Morane&h=def9"), result);
1214 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1215 GURL("http://google.com/alt/?q="), search_terms,
1216 search_terms_data_, &result));
1218 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1219 GURL("http://google.com/alt/?#q="), search_terms,
1220 search_terms_data_, &result));
1222 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1223 GURL("http://google.com/alt/?q=#q="), search_terms,
1224 search_terms_data_, &result));
1226 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1227 GURL("http://google.com/alt/?q=123#q="), search_terms,
1228 search_terms_data_, &result));
1230 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1231 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1232 search_terms_data_, &result));
1233 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result);
1236 // Test the |suggest_query_params| field of SearchTermsArgs.
1237 TEST_F(TemplateURLTest, SuggestQueryParams) {
1238 TemplateURLData data;
1239 // Pick a URL with replacements before, during, and after the query, to ensure
1240 // we don't goof up any of them.
1241 data.SetURL("{google:baseURL}search?q={searchTerms}"
1242 "#{google:originalQueryForSuggestion}x");
1243 TemplateURL url(data);
1245 // Baseline: no |suggest_query_params| field.
1246 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1247 search_terms.original_query = ASCIIToUTF16("def");
1248 search_terms.accepted_suggestion = 0;
1249 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1250 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1252 // Set the suggest_query_params.
1253 search_terms.suggest_query_params = "pq=xyz";
1254 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
1255 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1257 // Add extra_query_params in the mix, and ensure it works.
1258 search_terms.append_extra_query_params = true;
1259 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1260 switches::kExtraSearchQueryParams, "a=b");
1261 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
1262 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1265 // Test the |append_extra_query_params| field of SearchTermsArgs.
1266 TEST_F(TemplateURLTest, ExtraQueryParams) {
1267 TemplateURLData data;
1268 // Pick a URL with replacements before, during, and after the query, to ensure
1269 // we don't goof up any of them.
1270 data.SetURL("{google:baseURL}search?q={searchTerms}"
1271 "#{google:originalQueryForSuggestion}x");
1272 TemplateURL url(data);
1274 // Baseline: no command-line args, no |append_extra_query_params| flag.
1275 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1276 search_terms.original_query = ASCIIToUTF16("def");
1277 search_terms.accepted_suggestion = 0;
1278 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1279 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1281 // Set the flag. Since there are no command-line args, this should have no
1282 // effect.
1283 search_terms.append_extra_query_params = true;
1284 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1285 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1287 // Now append the command-line arg. This should be inserted into the query.
1288 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1289 switches::kExtraSearchQueryParams, "a=b");
1290 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
1291 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1293 // Turn off the flag. Now the command-line arg should be ignored again.
1294 search_terms.append_extra_query_params = false;
1295 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1296 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1299 // Tests replacing pageClassification.
1300 TEST_F(TemplateURLTest, ReplacePageClassification) {
1301 TemplateURLData data;
1302 data.input_encodings.push_back("UTF-8");
1303 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
1304 TemplateURL url(data);
1305 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1306 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
1307 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1309 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1310 search_terms_data_);
1311 EXPECT_EQ("http://www.google.com/?q=foo", result);
1313 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
1314 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1315 search_terms_data_);
1316 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1318 search_terms_args.page_classification =
1319 metrics::OmniboxEventProto::HOME_PAGE;
1320 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1321 search_terms_data_);
1322 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1325 // Test the IsSearchResults function.
1326 TEST_F(TemplateURLTest, IsSearchResults) {
1327 TemplateURLData data;
1328 data.SetURL("http://bar/search?q={searchTerms}");
1329 data.instant_url = "http://bar/instant#q={searchTerms}";
1330 data.new_tab_url = "http://bar/newtab";
1331 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1332 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1333 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1334 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
1335 TemplateURL search_provider(data);
1337 const struct {
1338 const char* const url;
1339 bool result;
1340 } url_data[] = {
1341 { "http://bar/search?q=foo&oq=foo", true, },
1342 { "http://bar/?q=foo&oq=foo", true, },
1343 { "http://bar/#output=search&q=foo&oq=foo", true, },
1344 { "http://bar/webhp#q=foo&oq=foo", true, },
1345 { "http://bar/#q=foo&oq=foo", true, },
1346 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1347 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1348 { "http://bar/", false, },
1349 { "http://foo/", false, },
1350 { "http://bar/newtab", false, },
1353 for (size_t i = 0; i < arraysize(url_data); ++i) {
1354 EXPECT_EQ(url_data[i].result,
1355 search_provider.IsSearchURL(GURL(url_data[i].url),
1356 search_terms_data_));
1360 TEST_F(TemplateURLTest, ReflectsBookmarkBarPinned) {
1361 TemplateURLData data;
1362 data.input_encodings.push_back("UTF-8");
1363 data.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
1364 TemplateURL url(data);
1365 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1366 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
1367 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1369 // Do not add the param when InstantExtended is suppressed on SRPs.
1370 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(false);
1371 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1372 search_terms_data_);
1373 EXPECT_EQ("http://www.google.com/?q=foo", result);
1375 // Add the param when InstantExtended is not suppressed on SRPs.
1376 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true);
1377 search_terms_args.bookmark_bar_pinned = false;
1378 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1379 search_terms_data_);
1380 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result);
1382 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true);
1383 search_terms_args.bookmark_bar_pinned = true;
1384 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1385 search_terms_data_);
1386 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result);
1389 TEST_F(TemplateURLTest, AnswersHasVersion) {
1390 TemplateURLData data;
1391 search_terms_data_.set_google_base_url("http://bar/");
1392 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1394 TemplateURL url(data);
1395 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1396 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1397 search_terms_data_);
1398 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1400 search_terms_data_.set_enable_answers_in_suggest(true);
1401 TemplateURL url2(data);
1402 result = url2.url_ref().ReplaceSearchTerms(search_terms_args,
1403 search_terms_data_);
1404 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1407 TEST_F(TemplateURLTest, SessionToken) {
1408 TemplateURLData data;
1409 search_terms_data_.set_google_base_url("http://bar/");
1410 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1412 TemplateURL url(data);
1413 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1414 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
1415 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1416 search_terms_data_);
1417 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1419 TemplateURL url2(data);
1420 search_terms_args.session_token = "";
1421 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1422 search_terms_data_);
1423 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1426 TEST_F(TemplateURLTest, ContextualSearchParameters) {
1427 TemplateURLData data;
1428 search_terms_data_.set_google_base_url("http://bar/");
1429 data.SetURL("http://bar/_/contextualsearch?"
1430 "{google:contextualSearchVersion}"
1431 "{google:contextualSearchContextData}");
1433 TemplateURL url(data);
1434 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1435 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1436 search_terms_data_);
1437 EXPECT_EQ("http://bar/_/contextualsearch?ctxsl_resolve=1", result);
1439 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
1440 1, 6, 11, "allen", "woody+allen+movies", "www.wikipedia.org",
1441 "utf-8", true);
1442 search_terms_args.contextual_search_params = params;
1443 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1444 search_terms_data_);
1445 EXPECT_EQ("http://bar/_/contextualsearch?"
1446 "ctxs=1&"
1447 "ctxs_start=6&"
1448 "ctxs_end=11&"
1449 "q=allen&"
1450 "ctxs_content=woody+allen+movies&"
1451 "ctxsl_url=www.wikipedia.org&"
1452 "ctxs_encoding=utf-8&"
1453 "ctxsl_resolve=1",
1454 result);
1457 TEST_F(TemplateURLTest, GenerateKeyword) {
1458 ASSERT_EQ(ASCIIToUTF16("foo"),
1459 TemplateURL::GenerateKeyword(GURL("http://foo")));
1460 // www. should be stripped.
1461 ASSERT_EQ(ASCIIToUTF16("foo"),
1462 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
1463 // Make sure we don't get a trailing '/'.
1464 ASSERT_EQ(ASCIIToUTF16("blah"),
1465 TemplateURL::GenerateKeyword(GURL("http://blah/")));
1466 // Don't generate the empty string.
1467 ASSERT_EQ(ASCIIToUTF16("www"),
1468 TemplateURL::GenerateKeyword(GURL("http://www.")));
1471 TEST_F(TemplateURLTest, GenerateSearchURL) {
1472 struct GenerateSearchURLCase {
1473 const char* test_name;
1474 const char* url;
1475 const char* expected;
1476 } generate_url_cases[] = {
1477 { "invalid URL", "foo{searchTerms}", "" },
1478 { "URL with no replacements", "http://foo/", "http://foo/" },
1479 { "basic functionality", "http://foo/{searchTerms}",
1480 "http://foo/blah.blah.blah.blah.blah" }
1483 for (size_t i = 0; i < arraysize(generate_url_cases); ++i) {
1484 TemplateURLData data;
1485 data.SetURL(generate_url_cases[i].url);
1486 TemplateURL t_url(data);
1487 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1488 generate_url_cases[i].expected)
1489 << generate_url_cases[i].test_name << " failed.";
1493 TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1494 TemplateURLData data;
1495 search_terms_data_.set_google_base_url("http://bar/");
1496 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1498 TemplateURL url(data);
1499 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1500 search_terms_args.prefetch_query = "full query text";
1501 search_terms_args.prefetch_query_type = "2338";
1502 std::string result =
1503 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1504 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1505 result);
1507 TemplateURL url2(data);
1508 search_terms_args.prefetch_query.clear();
1509 search_terms_args.prefetch_query_type.clear();
1510 result =
1511 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1512 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);