[Eraser strings] Remove/replace some supervised user strings
[chromium-blink-merge.git] / components / search_engines / template_url_unittest.cc
blob2c941f7ed51e07213c8a94c6551398477cc40894
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 base::ReplaceSubstringsAfterOffset(
428 &expected_result, 0, "{language}",
429 search_terms_data_.GetApplicationLocale());
430 GURL result(url.url_ref().ReplaceSearchTerms(
431 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
432 search_terms_data_));
433 ASSERT_TRUE(result.is_valid());
434 EXPECT_EQ(expected_result, result.spec());
439 // Tests replacing search terms in various encodings and making sure the
440 // generated URL matches the expected value.
441 TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
442 struct TestData {
443 const std::string encoding;
444 const base::string16 search_term;
445 const std::string url;
446 const std::string expected_result;
447 } test_data[] = {
448 { "BIG5", base::WideToUTF16(L"\x60BD"),
449 "http://foo/?{searchTerms}{inputEncoding}",
450 "http://foo/?%B1~BIG5" },
451 { "UTF-8", ASCIIToUTF16("blah"),
452 "http://foo/?{searchTerms}{inputEncoding}",
453 "http://foo/?blahUTF-8" },
454 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
455 "http://foo/{searchTerms}/bar",
456 "http://foo/%82%A0/bar"},
457 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
458 "http://foo/{searchTerms}/bar",
459 "http://foo/%82%A0%20%82%A2/bar"},
461 TemplateURLData data;
462 for (size_t i = 0; i < arraysize(test_data); ++i) {
463 data.SetURL(test_data[i].url);
464 data.input_encodings.clear();
465 data.input_encodings.push_back(test_data[i].encoding);
466 TemplateURL url(data);
467 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
468 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
469 GURL result(url.url_ref().ReplaceSearchTerms(
470 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
471 search_terms_data_));
472 ASSERT_TRUE(result.is_valid());
473 EXPECT_EQ(test_data[i].expected_result, result.spec());
477 // Tests replacing assisted query stats (AQS) in various scenarios.
478 TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
479 struct TestData {
480 const base::string16 search_term;
481 const std::string aqs;
482 const std::string base_url;
483 const std::string url;
484 const std::string expected_result;
485 } test_data[] = {
486 // No HTTPS, no AQS.
487 { ASCIIToUTF16("foo"),
488 "chrome.0.0l6",
489 "http://foo/",
490 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
491 "http://foo/?foo" },
492 // HTTPS available, AQS should be replaced.
493 { ASCIIToUTF16("foo"),
494 "chrome.0.0l6",
495 "https://foo/",
496 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
497 "https://foo/?fooaqs=chrome.0.0l6&" },
498 // HTTPS available, however AQS is empty.
499 { ASCIIToUTF16("foo"),
501 "https://foo/",
502 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
503 "https://foo/?foo" },
504 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
505 { ASCIIToUTF16("foo"),
506 "chrome.0.0l6",
507 "http://www.google.com",
508 "http://foo?{searchTerms}{google:assistedQueryStats}",
509 "http://foo/?foo" },
510 // A non-Google search provider with HTTPS should allow AQS.
511 { ASCIIToUTF16("foo"),
512 "chrome.0.0l6",
513 "https://www.google.com",
514 "https://foo?{searchTerms}{google:assistedQueryStats}",
515 "https://foo/?fooaqs=chrome.0.0l6&" },
517 TemplateURLData data;
518 data.input_encodings.push_back("UTF-8");
519 for (size_t i = 0; i < arraysize(test_data); ++i) {
520 data.SetURL(test_data[i].url);
521 TemplateURL url(data);
522 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
523 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
524 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
525 search_terms_args.assisted_query_stats = test_data[i].aqs;
526 search_terms_data_.set_google_base_url(test_data[i].base_url);
527 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
528 search_terms_data_));
529 ASSERT_TRUE(result.is_valid());
530 EXPECT_EQ(test_data[i].expected_result, result.spec());
534 // Tests replacing cursor position.
535 TEST_F(TemplateURLTest, ReplaceCursorPosition) {
536 struct TestData {
537 const base::string16 search_term;
538 size_t cursor_position;
539 const std::string url;
540 const std::string expected_result;
541 } test_data[] = {
542 { ASCIIToUTF16("foo"),
543 base::string16::npos,
544 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
545 "http://www.google.com/?foo&" },
546 { ASCIIToUTF16("foo"),
548 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
549 "http://www.google.com/?foo&cp=2&" },
550 { ASCIIToUTF16("foo"),
552 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
553 "http://www.google.com/?foo&cp=15&" },
555 TemplateURLData data;
556 data.input_encodings.push_back("UTF-8");
557 for (size_t i = 0; i < arraysize(test_data); ++i) {
558 data.SetURL(test_data[i].url);
559 TemplateURL url(data);
560 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
561 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
562 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
563 search_terms_args.cursor_position = test_data[i].cursor_position;
564 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
565 search_terms_data_));
566 ASSERT_TRUE(result.is_valid());
567 EXPECT_EQ(test_data[i].expected_result, result.spec());
571 // Tests replacing input type (&oit=).
572 TEST_F(TemplateURLTest, ReplaceInputType) {
573 struct TestData {
574 const base::string16 search_term;
575 metrics::OmniboxInputType::Type input_type;
576 const std::string url;
577 const std::string expected_result;
578 } test_data[] = {
579 { ASCIIToUTF16("foo"),
580 metrics::OmniboxInputType::UNKNOWN,
581 "{google:baseURL}?{searchTerms}&{google:inputType}",
582 "http://www.google.com/?foo&oit=1&" },
583 { ASCIIToUTF16("foo"),
584 metrics::OmniboxInputType::URL,
585 "{google:baseURL}?{searchTerms}&{google:inputType}",
586 "http://www.google.com/?foo&oit=3&" },
587 { ASCIIToUTF16("foo"),
588 metrics::OmniboxInputType::FORCED_QUERY,
589 "{google:baseURL}?{searchTerms}&{google:inputType}",
590 "http://www.google.com/?foo&oit=5&" },
592 TemplateURLData data;
593 data.input_encodings.push_back("UTF-8");
594 for (size_t i = 0; i < arraysize(test_data); ++i) {
595 data.SetURL(test_data[i].url);
596 TemplateURL url(data);
597 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
598 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
599 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
600 search_terms_args.input_type = test_data[i].input_type;
601 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
602 search_terms_data_));
603 ASSERT_TRUE(result.is_valid());
604 EXPECT_EQ(test_data[i].expected_result, result.spec());
608 // Tests replacing currentPageUrl.
609 TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
610 struct TestData {
611 const base::string16 search_term;
612 const std::string current_page_url;
613 const std::string url;
614 const std::string expected_result;
615 } test_data[] = {
616 { ASCIIToUTF16("foo"),
617 "http://www.google.com/",
618 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
619 "http://www.google.com/?foo&url=http%3A%2F%2Fwww.google.com%2F&" },
620 { ASCIIToUTF16("foo"),
622 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
623 "http://www.google.com/?foo&" },
624 { ASCIIToUTF16("foo"),
625 "http://g.com/+-/*&=",
626 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
627 "http://www.google.com/?foo&url=http%3A%2F%2Fg.com%2F%2B-%2F*%26%3D&" },
629 TemplateURLData data;
630 data.input_encodings.push_back("UTF-8");
631 for (size_t i = 0; i < arraysize(test_data); ++i) {
632 data.SetURL(test_data[i].url);
633 TemplateURL url(data);
634 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
635 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
636 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
637 search_terms_args.current_page_url = test_data[i].current_page_url;
638 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
639 search_terms_data_));
640 ASSERT_TRUE(result.is_valid());
641 EXPECT_EQ(test_data[i].expected_result, result.spec());
645 TEST_F(TemplateURLTest, OmniboxStartmargin) {
646 struct TestData {
647 const bool enable_omnibox_start_margin;
648 const int omnibox_start_margin;
649 const std::string expected_result;
650 } test_data[] = {
651 { false,
653 "http://bar/foo?q=foobar" },
654 { true,
656 "http://bar/foo?es_sm=0&q=foobar" },
657 { true,
659 "http://bar/foo?es_sm=42&q=foobar" },
661 TemplateURLData data;
662 data.SetURL("http://bar/foo?{google:omniboxStartMarginParameter}"
663 "q={searchTerms}");
664 data.input_encodings.push_back("UTF-8");
665 TemplateURL url(data);
666 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
667 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
668 for (size_t i = 0; i < arraysize(test_data); ++i) {
669 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
670 search_terms_args.enable_omnibox_start_margin =
671 test_data[i].enable_omnibox_start_margin;
672 search_terms_data_.set_omnibox_start_margin(
673 test_data[i].omnibox_start_margin);
674 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
675 search_terms_data_));
676 ASSERT_TRUE(result.is_valid());
677 EXPECT_EQ(test_data[i].expected_result, result.spec());
681 TEST_F(TemplateURLTest, Suggestions) {
682 struct TestData {
683 const int accepted_suggestion;
684 const base::string16 original_query_for_suggestion;
685 const std::string expected_result;
686 } test_data[] = {
687 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
688 "http://bar/foo?q=foobar" },
689 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
690 "http://bar/foo?q=foobar" },
691 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(),
692 "http://bar/foo?q=foobar" },
693 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
694 "http://bar/foo?q=foobar" },
695 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
696 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
698 TemplateURLData data;
699 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
700 "q={searchTerms}");
701 data.input_encodings.push_back("UTF-8");
702 TemplateURL url(data);
703 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
704 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
705 for (size_t i = 0; i < arraysize(test_data); ++i) {
706 TemplateURLRef::SearchTermsArgs search_terms_args(
707 ASCIIToUTF16("foobar"));
708 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
709 search_terms_args.original_query =
710 test_data[i].original_query_for_suggestion;
711 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
712 search_terms_data_));
713 ASSERT_TRUE(result.is_valid());
714 EXPECT_EQ(test_data[i].expected_result, result.spec());
718 TEST_F(TemplateURLTest, RLZ) {
719 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(false);
721 TemplateURLData data;
722 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
723 TemplateURL url(data);
724 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
725 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
726 GURL result(url.url_ref().ReplaceSearchTerms(
727 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_));
728 ASSERT_TRUE(result.is_valid());
729 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
730 result.spec());
733 TEST_F(TemplateURLTest, RLZFromAppList) {
734 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(true);
736 TemplateURLData data;
737 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
738 TemplateURL url(data);
739 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
740 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
741 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
742 args.from_app_list = true;
743 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
744 ASSERT_TRUE(result.is_valid());
745 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
746 result.spec());
749 TEST_F(TemplateURLTest, HostAndSearchTermKey) {
750 struct TestData {
751 const std::string url;
752 const std::string host;
753 const std::string path;
754 const std::string search_term_key;
755 } test_data[] = {
756 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
757 { "http://blah/{searchTerms}", "blah", "/", ""},
759 // No term should result in empty values.
760 { "http://blah/", "", "", ""},
762 // Multiple terms should result in empty values.
763 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
765 // Term in the host shouldn't match.
766 { "http://{searchTerms}", "", "", ""},
768 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
769 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
771 // Single term with extra chars in value should match.
772 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
775 for (size_t i = 0; i < arraysize(test_data); ++i) {
776 TemplateURLData data;
777 data.SetURL(test_data[i].url);
778 TemplateURL url(data);
779 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
780 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
781 EXPECT_EQ(test_data[i].search_term_key,
782 url.url_ref().GetSearchTermKey(search_terms_data_));
786 TEST_F(TemplateURLTest, SearchTermKeyLocation) {
787 struct TestData {
788 const std::string url;
789 const url::Parsed::ComponentType location;
790 const std::string path;
791 size_t position_in_path;
792 } test_data[] = {
793 { "http://blah/{searchTerms}/", url::Parsed::PATH, "//", 1 },
794 { "http://blah/{searchTerms}", url::Parsed::PATH, "/", 1 },
795 { "http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "/begin//end", 7 },
797 { "http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY,
798 "/", std::string::npos },
799 { "http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF,
800 "/", std::string::npos },
801 // searchTerms is a key, not a value, so this should result in an empty
802 // value.
803 { "http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x",
804 url::Parsed::QUERY, std::string(), std::string::npos },
806 // Multiple search terms should result in empty values.
807 { "http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY,
808 "", std::string::npos },
809 { "http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY,
810 "", std::string::npos },
811 { "http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY,
812 "", std::string::npos },
815 for (size_t i = 0; i < arraysize(test_data); ++i) {
816 TemplateURLData data;
817 data.SetURL(test_data[i].url);
818 TemplateURL url(data);
819 EXPECT_EQ(test_data[i].location,
820 url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
821 EXPECT_EQ(test_data[i].path,
822 url.url_ref().GetPath(search_terms_data_));
823 EXPECT_EQ(test_data[i].position_in_path,
824 url.url_ref().GetSearchTermPositionInPath(search_terms_data_));
828 TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
829 static const struct {
830 const char* const base_url;
831 const char* const base_suggest_url;
832 } data[] = {
833 { "http://google.com/", "http://google.com/complete/", },
834 { "http://www.google.com/", "http://www.google.com/complete/", },
835 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
836 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
837 { "http://google.com/intl/xx/", "http://google.com/complete/", },
840 for (size_t i = 0; i < arraysize(data); ++i)
841 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
844 TEST_F(TemplateURLTest, ParseParameterKnown) {
845 std::string parsed_url("{searchTerms}");
846 TemplateURLData data;
847 data.SetURL(parsed_url);
848 TemplateURL url(data);
849 TemplateURLRef::Replacements replacements;
850 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
851 EXPECT_EQ(std::string(), parsed_url);
852 ASSERT_EQ(1U, replacements.size());
853 EXPECT_EQ(0U, replacements[0].index);
854 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
857 TEST_F(TemplateURLTest, ParseParameterUnknown) {
858 std::string parsed_url("{fhqwhgads}abc");
859 TemplateURLData data;
860 data.SetURL(parsed_url);
861 TemplateURL url(data);
862 TemplateURLRef::Replacements replacements;
864 // By default, TemplateURLRef should not consider itself prepopulated.
865 // Therefore we should not replace the unknown parameter.
866 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
867 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
868 EXPECT_TRUE(replacements.empty());
870 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
871 parsed_url = "{fhqwhgads}abc";
872 data.prepopulate_id = 1;
873 TemplateURL url2(data);
874 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
875 EXPECT_EQ("abc", parsed_url);
876 EXPECT_TRUE(replacements.empty());
879 TEST_F(TemplateURLTest, ParseURLEmpty) {
880 TemplateURL url((TemplateURLData()));
881 TemplateURLRef::Replacements replacements;
882 bool valid = false;
883 EXPECT_EQ(std::string(),
884 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
885 EXPECT_TRUE(replacements.empty());
886 EXPECT_TRUE(valid);
889 TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
890 TemplateURLData data;
891 data.SetURL("{");
892 TemplateURL url(data);
893 TemplateURLRef::Replacements replacements;
894 bool valid = false;
895 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
896 &valid));
897 EXPECT_TRUE(replacements.empty());
898 EXPECT_FALSE(valid);
901 TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
902 TemplateURLData data;
903 data.SetURL("{}");
904 TemplateURL url(data);
905 TemplateURLRef::Replacements replacements;
906 bool valid = false;
907 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
908 EXPECT_TRUE(replacements.empty());
909 EXPECT_TRUE(valid);
912 TEST_F(TemplateURLTest, ParseURLTwoParameters) {
913 TemplateURLData data;
914 data.SetURL("{}{{%s}}");
915 TemplateURL url(data);
916 TemplateURLRef::Replacements replacements;
917 bool valid = false;
918 EXPECT_EQ("{}{}",
919 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
920 &valid));
921 ASSERT_EQ(1U, replacements.size());
922 EXPECT_EQ(3U, replacements[0].index);
923 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
924 EXPECT_TRUE(valid);
927 TEST_F(TemplateURLTest, ParseURLNestedParameter) {
928 TemplateURLData data;
929 data.SetURL("{%s");
930 TemplateURL url(data);
931 TemplateURLRef::Replacements replacements;
932 bool valid = false;
933 EXPECT_EQ("{",
934 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
935 &valid));
936 ASSERT_EQ(1U, replacements.size());
937 EXPECT_EQ(1U, replacements[0].index);
938 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
939 EXPECT_TRUE(valid);
942 TEST_F(TemplateURLTest, SearchClient) {
943 const std::string base_url_str("http://google.com/?");
944 const std::string terms_str("{searchTerms}&{google:searchClient}");
945 const std::string full_url_str = base_url_str + terms_str;
946 const base::string16 terms(ASCIIToUTF16(terms_str));
947 search_terms_data_.set_google_base_url(base_url_str);
949 TemplateURLData data;
950 data.SetURL(full_url_str);
951 TemplateURL url(data);
952 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
953 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
954 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
956 // Check that the URL is correct when a client is not present.
957 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
958 search_terms_data_));
959 ASSERT_TRUE(result.is_valid());
960 EXPECT_EQ("http://google.com/?foobar&", result.spec());
962 // Check that the URL is correct when a client is present.
963 search_terms_data_.set_search_client("search_client");
964 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
965 search_terms_data_));
966 ASSERT_TRUE(result_2.is_valid());
967 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
970 TEST_F(TemplateURLTest, GetURLNoInstantURL) {
971 TemplateURLData data;
972 data.SetURL("http://google.com/?q={searchTerms}");
973 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
974 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
975 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
976 TemplateURL url(data);
977 ASSERT_EQ(3U, url.URLCount());
978 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
979 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
980 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
983 TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
984 TemplateURLData data;
985 data.SetURL("http://google.com/?q={searchTerms}");
986 data.instant_url = "http://google.com/instant#q={searchTerms}";
987 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
988 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
989 TemplateURL url(data);
990 ASSERT_EQ(3U, url.URLCount());
991 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
992 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
993 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
996 TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
997 TemplateURLData data;
998 data.SetURL("http://www.google.co.uk/");
999 TemplateURL url(data);
1000 ASSERT_EQ(1U, url.URLCount());
1001 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0));
1004 TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1005 TemplateURLData data;
1006 data.SetURL("http://google.com/?q={searchTerms}");
1007 data.instant_url = "http://google.com/instant#q={searchTerms}";
1008 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1009 data.alternate_urls.push_back(
1010 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1011 TemplateURL url(data);
1012 base::string16 result;
1014 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1015 GURL("http://google.com/?q=something"), search_terms_data_, &result));
1016 EXPECT_EQ(ASCIIToUTF16("something"), result);
1018 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1019 GURL("http://google.com/?espv&q=something"),
1020 search_terms_data_, &result));
1021 EXPECT_EQ(ASCIIToUTF16("something"), result);
1023 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1024 GURL("http://google.com/?espv=1&q=something"),
1025 search_terms_data_, &result));
1026 EXPECT_EQ(ASCIIToUTF16("something"), result);
1028 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1029 GURL("http://google.com/?espv=0&q=something"),
1030 search_terms_data_, &result));
1031 EXPECT_EQ(ASCIIToUTF16("something"), result);
1033 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1034 GURL("http://google.com/alt/#q=something"),
1035 search_terms_data_, &result));
1036 EXPECT_EQ(ASCIIToUTF16("something"), result);
1038 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1039 GURL("http://google.com/alt/#espv&q=something"),
1040 search_terms_data_, &result));
1041 EXPECT_EQ(ASCIIToUTF16("something"), result);
1043 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1044 GURL("http://google.com/alt/#espv=1&q=something"),
1045 search_terms_data_, &result));
1046 EXPECT_EQ(ASCIIToUTF16("something"), result);
1048 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1049 GURL("http://google.com/alt/#espv=0&q=something"),
1050 search_terms_data_, &result));
1051 EXPECT_EQ(ASCIIToUTF16("something"), result);
1053 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1054 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
1055 EXPECT_EQ(base::string16(), result);
1057 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1058 GURL("http://google.ca/?q=something&q=anything"),
1059 search_terms_data_, &result));
1060 EXPECT_EQ(base::string16(), result);
1062 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1063 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
1064 EXPECT_EQ(base::string16(), result);
1066 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1067 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
1068 EXPECT_EQ(ASCIIToUTF16("foo"), result);
1070 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1071 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
1072 EXPECT_EQ(base::string16(), result);
1074 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1075 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
1076 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1078 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1079 GURL("http://google.com/alt/?q=123#q=456"),
1080 search_terms_data_, &result));
1081 EXPECT_EQ(ASCIIToUTF16("456"), result);
1083 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1084 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1085 search_terms_data_, &result));
1086 EXPECT_EQ(ASCIIToUTF16("123"), result);
1088 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
1089 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1090 search_terms_data_, &result));
1091 EXPECT_EQ(ASCIIToUTF16("789"), result);
1093 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1094 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
1095 EXPECT_EQ(base::string16(), result);
1097 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1098 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
1099 EXPECT_EQ(base::string16(), result);
1101 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1102 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
1103 EXPECT_EQ(base::string16(), result);
1105 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1106 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
1107 EXPECT_EQ(base::string16(), result);
1109 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1110 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
1111 EXPECT_EQ(ASCIIToUTF16("123"), result);
1114 TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1115 TemplateURLData data;
1116 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1117 TemplateURL url(data);
1118 base::string16 result;
1120 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1121 GURL("http://term-in-path.com/begin/something/end"),
1122 search_terms_data_, &result));
1123 EXPECT_EQ(ASCIIToUTF16("something"), result);
1125 // "%20" must be converted to space.
1126 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1127 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1128 search_terms_data_, &result));
1129 EXPECT_EQ(ASCIIToUTF16("a b c"), result);
1131 // Plus must not be converted to space.
1132 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1133 GURL("http://term-in-path.com/begin/1+2+3/end"),
1134 search_terms_data_, &result));
1135 EXPECT_EQ(ASCIIToUTF16("1+2+3"), result);
1137 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1138 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
1139 EXPECT_EQ(base::string16(), result);
1141 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1142 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
1143 EXPECT_EQ(base::string16(), result);
1145 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1146 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
1147 EXPECT_EQ(base::string16(), result);
1150 // Checks that the ExtractSearchTermsFromURL function works correctly
1151 // for urls containing non-latin characters in UTF8 encoding.
1152 TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
1153 TemplateURLData data;
1154 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1155 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1156 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1157 TemplateURL url(data);
1158 base::string16 result;
1160 // Russian text encoded with UTF-8.
1161 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1162 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1163 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
1164 search_terms_data_, &result));
1165 EXPECT_EQ(
1166 base::WideToUTF16(
1167 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1168 L"\x043C\x0438\x0440!"),
1169 result);
1171 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1172 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1173 "%D0%B0"),
1174 search_terms_data_, &result));
1175 EXPECT_EQ(
1176 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1177 result);
1179 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1180 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1181 "%D0%B8%20A"),
1182 search_terms_data_, &result));
1183 EXPECT_EQ(
1184 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1185 result);
1188 // Checks that the ExtractSearchTermsFromURL function works correctly
1189 // for urls containing non-latin characters in non-UTF8 encoding.
1190 TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) {
1191 TemplateURLData data;
1192 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1193 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1194 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1195 data.input_encodings.push_back("windows-1251");
1196 TemplateURL url(data);
1197 base::string16 result;
1199 // Russian text encoded with Windows-1251.
1200 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1201 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1202 "%EC%E8%F0!"),
1203 search_terms_data_, &result));
1204 EXPECT_EQ(
1205 base::WideToUTF16(
1206 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1207 L"\x043C\x0438\x0440!"),
1208 result);
1210 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1211 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1212 search_terms_data_, &result));
1213 EXPECT_EQ(
1214 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1215 result);
1217 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1218 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1219 search_terms_data_, &result));
1220 EXPECT_EQ(
1221 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1222 result);
1225 TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
1226 TemplateURLData data;
1227 data.SetURL("http://google.com/?q={searchTerms}");
1228 data.instant_url = "http://google.com/instant#q={searchTerms}";
1229 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1230 data.alternate_urls.push_back(
1231 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1232 data.search_terms_replacement_key = "espv";
1233 TemplateURL url(data);
1235 // Test with instant enabled required.
1236 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1237 GURL("http://google.com/")));
1239 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1240 GURL("http://google.com/?espv")));
1242 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1243 GURL("http://google.com/#espv")));
1245 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1246 GURL("http://google.com/?q=something")));
1248 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1249 GURL("http://google.com/?q=something&espv")));
1251 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1252 GURL("http://google.com/?q=something&espv=1")));
1254 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1255 GURL("http://google.com/?q=something&espv=0")));
1257 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1258 GURL("http://google.com/?espv&q=something")));
1260 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1261 GURL("http://google.com/?espv=1&q=something")));
1263 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1264 GURL("http://google.com/?espv=0&q=something")));
1266 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1267 GURL("http://google.com/alt/#q=something")));
1269 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1270 GURL("http://google.com/alt/#q=something&espv")));
1272 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1273 GURL("http://google.com/alt/#q=something&espv=1")));
1275 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1276 GURL("http://google.com/alt/#q=something&espv=0")));
1278 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1279 GURL("http://google.com/alt/#espv&q=something")));
1281 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1282 GURL("http://google.com/alt/#espv=1&q=something")));
1284 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1285 GURL("http://google.com/alt/#espv=0&q=something")));
1287 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1288 GURL("http://google.com/?espv#q=something")));
1290 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1291 GURL("http://google.com/?espv=1#q=something")));
1293 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1294 GURL("http://google.com/?q=something#espv")));
1296 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1297 GURL("http://google.com/?q=something#espv=1")));
1299 // This does not ensure the domain matches.
1300 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1301 GURL("http://bing.com/?espv")));
1303 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1304 GURL("http://bing.com/#espv")));
1307 TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1308 TemplateURLData data;
1309 data.SetURL("http://google.com/?q={searchTerms}");
1310 data.instant_url = "http://google.com/instant#q={searchTerms}";
1311 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1312 data.alternate_urls.push_back(
1313 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1314 TemplateURL url(data);
1315 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1316 GURL result;
1318 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1319 GURL("http://google.com/?q=something"), search_terms,
1320 search_terms_data_, &result));
1321 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result);
1323 result = GURL("http://should.not.change.com");
1324 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1325 GURL("http://google.ca/?q=something"), search_terms,
1326 search_terms_data_, &result));
1327 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1329 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1330 GURL("http://google.com/foo/?q=foo"), search_terms,
1331 search_terms_data_, &result));
1333 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1334 GURL("https://google.com/?q=foo"), search_terms,
1335 search_terms_data_, &result));
1336 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result);
1338 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1339 GURL("http://google.com:8080/?q=foo"), search_terms,
1340 search_terms_data_, &result));
1342 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1343 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1344 search_terms_data_, &result));
1345 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result);
1347 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1348 // template_url.cc for details.
1349 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1350 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1351 search_terms_data_, &result));
1352 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result);
1354 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1355 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
1356 search_terms_data_, &result));
1357 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
1358 result);
1360 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1361 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1362 search_terms, search_terms_data_, &result));
1363 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1364 "#j=abc&q=Bob+Morane&h=def9"), result);
1366 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1367 GURL("http://google.com/alt/?q="), search_terms,
1368 search_terms_data_, &result));
1370 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1371 GURL("http://google.com/alt/?#q="), search_terms,
1372 search_terms_data_, &result));
1374 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1375 GURL("http://google.com/alt/?q=#q="), search_terms,
1376 search_terms_data_, &result));
1378 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1379 GURL("http://google.com/alt/?q=123#q="), search_terms,
1380 search_terms_data_, &result));
1382 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1383 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1384 search_terms_data_, &result));
1385 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result);
1388 TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1389 TemplateURLData data;
1390 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1391 TemplateURL url(data);
1392 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1393 GURL result;
1395 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1396 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1397 search_terms_data_, &result));
1398 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1400 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1401 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1402 search_terms_data_, &result));
1403 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1405 result = GURL("http://should.not.change.com");
1406 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1407 GURL("http://term-in-path.com/about"), search_terms,
1408 search_terms_data_, &result));
1409 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1412 // Checks that the ReplaceSearchTermsInURL function works correctly
1413 // for search terms containing non-latin characters for a search engine
1414 // using UTF-8 input encoding.
1415 TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) {
1416 TemplateURLData data;
1417 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1418 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1419 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1420 TemplateURL url(data);
1422 // Russian text which will be encoded with UTF-8.
1423 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1424 L"\x0442\x0435\x043A\x0441\x0442"));
1425 GURL result;
1427 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1428 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_,
1429 &result));
1430 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1431 result);
1433 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1434 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_,
1435 &result));
1436 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1437 result);
1439 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1440 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_,
1441 &result));
1442 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1443 result);
1446 // Checks that the ReplaceSearchTermsInURL function works correctly
1447 // for search terms containing non-latin characters for a search engine
1448 // using non UTF-8 input encoding.
1449 TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) {
1450 TemplateURLData data;
1451 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1452 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1453 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1454 data.input_encodings.push_back("windows-1251");
1455 TemplateURL url(data);
1457 // Russian text which will be encoded with Windows-1251.
1458 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1459 L"\x0442\x0435\x043A\x0441\x0442"));
1460 GURL result;
1462 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1463 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_,
1464 &result));
1465 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1466 result);
1468 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1469 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_,
1470 &result));
1471 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1472 result);
1474 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1475 GURL("http://windows-1251.ru/path/a%20b"), search_terms,
1476 search_terms_data_, &result));
1477 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1478 result);
1481 // Test the |suggest_query_params| field of SearchTermsArgs.
1482 TEST_F(TemplateURLTest, SuggestQueryParams) {
1483 TemplateURLData data;
1484 // Pick a URL with replacements before, during, and after the query, to ensure
1485 // we don't goof up any of them.
1486 data.SetURL("{google:baseURL}search?q={searchTerms}"
1487 "#{google:originalQueryForSuggestion}x");
1488 TemplateURL url(data);
1490 // Baseline: no |suggest_query_params| field.
1491 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1492 search_terms.original_query = ASCIIToUTF16("def");
1493 search_terms.accepted_suggestion = 0;
1494 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1495 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1497 // Set the suggest_query_params.
1498 search_terms.suggest_query_params = "pq=xyz";
1499 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
1500 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1502 // Add extra_query_params in the mix, and ensure it works.
1503 search_terms.append_extra_query_params = true;
1504 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1505 switches::kExtraSearchQueryParams, "a=b");
1506 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
1507 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1510 // Test the |append_extra_query_params| field of SearchTermsArgs.
1511 TEST_F(TemplateURLTest, ExtraQueryParams) {
1512 TemplateURLData data;
1513 // Pick a URL with replacements before, during, and after the query, to ensure
1514 // we don't goof up any of them.
1515 data.SetURL("{google:baseURL}search?q={searchTerms}"
1516 "#{google:originalQueryForSuggestion}x");
1517 TemplateURL url(data);
1519 // Baseline: no command-line args, no |append_extra_query_params| flag.
1520 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1521 search_terms.original_query = ASCIIToUTF16("def");
1522 search_terms.accepted_suggestion = 0;
1523 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1524 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1526 // Set the flag. Since there are no command-line args, this should have no
1527 // effect.
1528 search_terms.append_extra_query_params = true;
1529 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1530 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1532 // Now append the command-line arg. This should be inserted into the query.
1533 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1534 switches::kExtraSearchQueryParams, "a=b");
1535 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
1536 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1538 // Turn off the flag. Now the command-line arg should be ignored again.
1539 search_terms.append_extra_query_params = false;
1540 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1541 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1544 // Tests replacing pageClassification.
1545 TEST_F(TemplateURLTest, ReplacePageClassification) {
1546 TemplateURLData data;
1547 data.input_encodings.push_back("UTF-8");
1548 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
1549 TemplateURL url(data);
1550 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1551 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
1552 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1554 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1555 search_terms_data_);
1556 EXPECT_EQ("http://www.google.com/?q=foo", result);
1558 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
1559 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1560 search_terms_data_);
1561 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1563 search_terms_args.page_classification =
1564 metrics::OmniboxEventProto::HOME_PAGE;
1565 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1566 search_terms_data_);
1567 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1570 // Test the IsSearchResults function.
1571 TEST_F(TemplateURLTest, IsSearchResults) {
1572 TemplateURLData data;
1573 data.SetURL("http://bar/search?q={searchTerms}");
1574 data.instant_url = "http://bar/instant#q={searchTerms}";
1575 data.new_tab_url = "http://bar/newtab";
1576 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1577 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1578 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1579 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
1580 TemplateURL search_provider(data);
1582 const struct {
1583 const char* const url;
1584 bool result;
1585 } url_data[] = {
1586 { "http://bar/search?q=foo&oq=foo", true, },
1587 { "http://bar/?q=foo&oq=foo", true, },
1588 { "http://bar/#output=search&q=foo&oq=foo", true, },
1589 { "http://bar/webhp#q=foo&oq=foo", true, },
1590 { "http://bar/#q=foo&oq=foo", true, },
1591 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1592 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1593 { "http://bar/", false, },
1594 { "http://foo/", false, },
1595 { "http://bar/newtab", false, },
1598 for (size_t i = 0; i < arraysize(url_data); ++i) {
1599 EXPECT_EQ(url_data[i].result,
1600 search_provider.IsSearchURL(GURL(url_data[i].url),
1601 search_terms_data_));
1605 TEST_F(TemplateURLTest, ReflectsBookmarkBarPinned) {
1606 TemplateURLData data;
1607 data.input_encodings.push_back("UTF-8");
1608 data.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
1609 TemplateURL url(data);
1610 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1611 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
1612 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1614 // Do not add the param when InstantExtended is suppressed on SRPs.
1615 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(false);
1616 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1617 search_terms_data_);
1618 EXPECT_EQ("http://www.google.com/?q=foo", result);
1620 // Add the param when InstantExtended is not suppressed on SRPs.
1621 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true);
1622 search_terms_args.bookmark_bar_pinned = false;
1623 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1624 search_terms_data_);
1625 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result);
1627 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true);
1628 search_terms_args.bookmark_bar_pinned = true;
1629 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1630 search_terms_data_);
1631 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result);
1634 TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) {
1635 TemplateURLData data;
1636 search_terms_data_.set_google_base_url("http://bar/");
1637 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1639 TemplateURL url(data);
1640 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1641 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1642 search_terms_data_);
1643 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1646 TEST_F(TemplateURLTest, SessionToken) {
1647 TemplateURLData data;
1648 search_terms_data_.set_google_base_url("http://bar/");
1649 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1651 TemplateURL url(data);
1652 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1653 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
1654 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1655 search_terms_data_);
1656 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1658 TemplateURL url2(data);
1659 search_terms_args.session_token = "";
1660 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1661 search_terms_data_);
1662 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1665 TEST_F(TemplateURLTest, ContextualSearchParameters) {
1666 TemplateURLData data;
1667 search_terms_data_.set_google_base_url("http://bar/");
1668 data.SetURL("http://bar/_/contextualsearch?"
1669 "{google:contextualSearchVersion}"
1670 "{google:contextualSearchContextData}");
1672 TemplateURL url(data);
1673 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1674 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1675 search_terms_data_);
1676 EXPECT_EQ("http://bar/_/contextualsearch?ctxsl_resolve=1", result);
1678 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
1679 1, 6, 11, "allen", "woody+allen+movies", "www.wikipedia.org",
1680 "utf-8", true);
1681 search_terms_args.contextual_search_params = params;
1682 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1683 search_terms_data_);
1684 EXPECT_EQ("http://bar/_/contextualsearch?"
1685 "ctxs=1&"
1686 "ctxs_start=6&"
1687 "ctxs_end=11&"
1688 "q=allen&"
1689 "ctxs_content=woody+allen+movies&"
1690 "ctxsl_url=www.wikipedia.org&"
1691 "ctxs_encoding=utf-8&"
1692 "ctxsl_resolve=1",
1693 result);
1696 TEST_F(TemplateURLTest, GenerateKeyword) {
1697 std::string accept_languages = "en,ru";
1698 ASSERT_EQ(ASCIIToUTF16("foo"),
1699 TemplateURL::GenerateKeyword(GURL("http://foo"), accept_languages));
1700 // www. should be stripped.
1701 ASSERT_EQ(ASCIIToUTF16("foo"), TemplateURL::GenerateKeyword(
1702 GURL("http://www.foo"), accept_languages));
1703 // Make sure we don't get a trailing '/'.
1704 ASSERT_EQ(ASCIIToUTF16("blah"), TemplateURL::GenerateKeyword(
1705 GURL("http://blah/"), accept_languages));
1706 // Don't generate the empty string.
1707 ASSERT_EQ(ASCIIToUTF16("www"), TemplateURL::GenerateKeyword(
1708 GURL("http://www."), accept_languages));
1709 ASSERT_EQ(
1710 base::UTF8ToUTF16("\xd0\xb0\xd0\xb1\xd0\xb2"),
1711 TemplateURL::GenerateKeyword(GURL("http://xn--80acd"), accept_languages));
1714 TEST_F(TemplateURLTest, GenerateSearchURL) {
1715 struct GenerateSearchURLCase {
1716 const char* test_name;
1717 const char* url;
1718 const char* expected;
1719 } generate_url_cases[] = {
1720 { "invalid URL", "foo{searchTerms}", "" },
1721 { "URL with no replacements", "http://foo/", "http://foo/" },
1722 { "basic functionality", "http://foo/{searchTerms}",
1723 "http://foo/blah.blah.blah.blah.blah" }
1726 for (size_t i = 0; i < arraysize(generate_url_cases); ++i) {
1727 TemplateURLData data;
1728 data.SetURL(generate_url_cases[i].url);
1729 TemplateURL t_url(data);
1730 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1731 generate_url_cases[i].expected)
1732 << generate_url_cases[i].test_name << " failed.";
1736 TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1737 TemplateURLData data;
1738 search_terms_data_.set_google_base_url("http://bar/");
1739 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1741 TemplateURL url(data);
1742 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1743 search_terms_args.prefetch_query = "full query text";
1744 search_terms_args.prefetch_query_type = "2338";
1745 std::string result =
1746 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1747 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1748 result);
1750 TemplateURL url2(data);
1751 search_terms_args.prefetch_query.clear();
1752 search_terms_args.prefetch_query_type.clear();
1753 result =
1754 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1755 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);