Android Chromoting: Remove exit-fullscreen button.
[chromium-blink-merge.git] / components / search_engines / template_url_unittest.cc
blob6d09bf89c38f3db31adc90576fe14af1150ab690
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"},
756 { "http://blah/{searchTerms}", "blah", "/", ""},
758 // No term should result in empty values.
759 { "http://blah/", "", "", ""},
761 // Multiple terms should result in empty values.
762 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
764 // Term in the host shouldn't match.
765 { "http://{searchTerms}", "", "", ""},
767 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
768 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
770 // Single term with extra chars in value should match.
771 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
774 for (size_t i = 0; i < arraysize(test_data); ++i) {
775 TemplateURLData data;
776 data.SetURL(test_data[i].url);
777 TemplateURL url(data);
778 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
779 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
780 EXPECT_EQ(test_data[i].search_term_key,
781 url.url_ref().GetSearchTermKey(search_terms_data_));
785 TEST_F(TemplateURLTest, SearchTermKeyLocation) {
786 struct TestData {
787 const std::string url;
788 const url::Parsed::ComponentType location;
789 const std::string path;
790 size_t position_in_path;
791 } test_data[] = {
792 { "http://blah/{searchTerms}/", url::Parsed::PATH, "//", 1 },
793 { "http://blah/{searchTerms}", url::Parsed::PATH, "/", 1 },
794 { "http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "/begin//end", 7 },
796 { "http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY,
797 "/", std::string::npos },
798 { "http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF,
799 "/", std::string::npos },
801 // Multiple search terms should result in empty values.
802 { "http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY,
803 "", std::string::npos },
804 { "http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY,
805 "", std::string::npos },
806 { "http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY,
807 "", std::string::npos },
810 for (size_t i = 0; i < arraysize(test_data); ++i) {
811 TemplateURLData data;
812 data.SetURL(test_data[i].url);
813 TemplateURL url(data);
814 EXPECT_EQ(test_data[i].location,
815 url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
816 EXPECT_EQ(test_data[i].path,
817 url.url_ref().GetPath(search_terms_data_));
818 EXPECT_EQ(test_data[i].position_in_path,
819 url.url_ref().GetSearchTermPositionInPath(search_terms_data_));
823 TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
824 static const struct {
825 const char* const base_url;
826 const char* const base_suggest_url;
827 } data[] = {
828 { "http://google.com/", "http://google.com/complete/", },
829 { "http://www.google.com/", "http://www.google.com/complete/", },
830 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
831 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
832 { "http://google.com/intl/xx/", "http://google.com/complete/", },
835 for (size_t i = 0; i < arraysize(data); ++i)
836 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
839 TEST_F(TemplateURLTest, ParseParameterKnown) {
840 std::string parsed_url("{searchTerms}");
841 TemplateURLData data;
842 data.SetURL(parsed_url);
843 TemplateURL url(data);
844 TemplateURLRef::Replacements replacements;
845 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
846 EXPECT_EQ(std::string(), parsed_url);
847 ASSERT_EQ(1U, replacements.size());
848 EXPECT_EQ(0U, replacements[0].index);
849 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
852 TEST_F(TemplateURLTest, ParseParameterUnknown) {
853 std::string parsed_url("{fhqwhgads}abc");
854 TemplateURLData data;
855 data.SetURL(parsed_url);
856 TemplateURL url(data);
857 TemplateURLRef::Replacements replacements;
859 // By default, TemplateURLRef should not consider itself prepopulated.
860 // Therefore we should not replace the unknown parameter.
861 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
862 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
863 EXPECT_TRUE(replacements.empty());
865 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
866 parsed_url = "{fhqwhgads}abc";
867 data.prepopulate_id = 1;
868 TemplateURL url2(data);
869 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
870 EXPECT_EQ("abc", parsed_url);
871 EXPECT_TRUE(replacements.empty());
874 TEST_F(TemplateURLTest, ParseURLEmpty) {
875 TemplateURL url((TemplateURLData()));
876 TemplateURLRef::Replacements replacements;
877 bool valid = false;
878 EXPECT_EQ(std::string(),
879 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
880 EXPECT_TRUE(replacements.empty());
881 EXPECT_TRUE(valid);
884 TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
885 TemplateURLData data;
886 data.SetURL("{");
887 TemplateURL url(data);
888 TemplateURLRef::Replacements replacements;
889 bool valid = false;
890 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
891 &valid));
892 EXPECT_TRUE(replacements.empty());
893 EXPECT_FALSE(valid);
896 TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
897 TemplateURLData data;
898 data.SetURL("{}");
899 TemplateURL url(data);
900 TemplateURLRef::Replacements replacements;
901 bool valid = false;
902 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
903 EXPECT_TRUE(replacements.empty());
904 EXPECT_TRUE(valid);
907 TEST_F(TemplateURLTest, ParseURLTwoParameters) {
908 TemplateURLData data;
909 data.SetURL("{}{{%s}}");
910 TemplateURL url(data);
911 TemplateURLRef::Replacements replacements;
912 bool valid = false;
913 EXPECT_EQ("{}{}",
914 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
915 &valid));
916 ASSERT_EQ(1U, replacements.size());
917 EXPECT_EQ(3U, replacements[0].index);
918 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
919 EXPECT_TRUE(valid);
922 TEST_F(TemplateURLTest, ParseURLNestedParameter) {
923 TemplateURLData data;
924 data.SetURL("{%s");
925 TemplateURL url(data);
926 TemplateURLRef::Replacements replacements;
927 bool valid = false;
928 EXPECT_EQ("{",
929 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
930 &valid));
931 ASSERT_EQ(1U, replacements.size());
932 EXPECT_EQ(1U, replacements[0].index);
933 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
934 EXPECT_TRUE(valid);
937 TEST_F(TemplateURLTest, SearchClient) {
938 const std::string base_url_str("http://google.com/?");
939 const std::string terms_str("{searchTerms}&{google:searchClient}");
940 const std::string full_url_str = base_url_str + terms_str;
941 const base::string16 terms(ASCIIToUTF16(terms_str));
942 search_terms_data_.set_google_base_url(base_url_str);
944 TemplateURLData data;
945 data.SetURL(full_url_str);
946 TemplateURL url(data);
947 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
948 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
949 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
951 // Check that the URL is correct when a client is not present.
952 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
953 search_terms_data_));
954 ASSERT_TRUE(result.is_valid());
955 EXPECT_EQ("http://google.com/?foobar&", result.spec());
957 // Check that the URL is correct when a client is present.
958 search_terms_data_.set_search_client("search_client");
959 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
960 search_terms_data_));
961 ASSERT_TRUE(result_2.is_valid());
962 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
965 TEST_F(TemplateURLTest, GetURLNoInstantURL) {
966 TemplateURLData data;
967 data.SetURL("http://google.com/?q={searchTerms}");
968 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
969 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
970 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
971 TemplateURL url(data);
972 ASSERT_EQ(3U, url.URLCount());
973 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
974 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
975 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
978 TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
979 TemplateURLData data;
980 data.SetURL("http://google.com/?q={searchTerms}");
981 data.instant_url = "http://google.com/instant#q={searchTerms}";
982 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
983 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
984 TemplateURL url(data);
985 ASSERT_EQ(3U, url.URLCount());
986 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
987 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
988 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
991 TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
992 TemplateURLData data;
993 data.SetURL("http://www.google.co.uk/");
994 TemplateURL url(data);
995 ASSERT_EQ(1U, url.URLCount());
996 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0));
999 TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1000 TemplateURLData data;
1001 data.SetURL("http://google.com/?q={searchTerms}");
1002 data.instant_url = "http://google.com/instant#q={searchTerms}";
1003 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1004 data.alternate_urls.push_back(
1005 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1006 TemplateURL url(data);
1007 base::string16 result;
1009 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1010 GURL("http://google.com/?q=something"), search_terms_data_, &result));
1011 EXPECT_EQ(ASCIIToUTF16("something"), result);
1013 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1014 GURL("http://google.com/?espv&q=something"),
1015 search_terms_data_, &result));
1016 EXPECT_EQ(ASCIIToUTF16("something"), result);
1018 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1019 GURL("http://google.com/?espv=1&q=something"),
1020 search_terms_data_, &result));
1021 EXPECT_EQ(ASCIIToUTF16("something"), result);
1023 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1024 GURL("http://google.com/?espv=0&q=something"),
1025 search_terms_data_, &result));
1026 EXPECT_EQ(ASCIIToUTF16("something"), result);
1028 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1029 GURL("http://google.com/alt/#q=something"),
1030 search_terms_data_, &result));
1031 EXPECT_EQ(ASCIIToUTF16("something"), result);
1033 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1034 GURL("http://google.com/alt/#espv&q=something"),
1035 search_terms_data_, &result));
1036 EXPECT_EQ(ASCIIToUTF16("something"), result);
1038 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1039 GURL("http://google.com/alt/#espv=1&q=something"),
1040 search_terms_data_, &result));
1041 EXPECT_EQ(ASCIIToUTF16("something"), result);
1043 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1044 GURL("http://google.com/alt/#espv=0&q=something"),
1045 search_terms_data_, &result));
1046 EXPECT_EQ(ASCIIToUTF16("something"), result);
1048 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1049 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
1050 EXPECT_EQ(base::string16(), result);
1052 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1053 GURL("http://google.ca/?q=something&q=anything"),
1054 search_terms_data_, &result));
1055 EXPECT_EQ(base::string16(), result);
1057 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1058 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
1059 EXPECT_EQ(base::string16(), result);
1061 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1062 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
1063 EXPECT_EQ(ASCIIToUTF16("foo"), result);
1065 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1066 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
1067 EXPECT_EQ(base::string16(), result);
1069 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1070 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
1071 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1073 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1074 GURL("http://google.com/alt/?q=123#q=456"),
1075 search_terms_data_, &result));
1076 EXPECT_EQ(ASCIIToUTF16("456"), result);
1078 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1079 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1080 search_terms_data_, &result));
1081 EXPECT_EQ(ASCIIToUTF16("123"), result);
1083 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
1084 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1085 search_terms_data_, &result));
1086 EXPECT_EQ(ASCIIToUTF16("789"), result);
1088 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1089 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
1090 EXPECT_EQ(base::string16(), result);
1092 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1093 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
1094 EXPECT_EQ(base::string16(), result);
1096 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1097 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
1098 EXPECT_EQ(base::string16(), result);
1100 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1101 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
1102 EXPECT_EQ(base::string16(), result);
1104 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1105 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
1106 EXPECT_EQ(ASCIIToUTF16("123"), result);
1109 TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1110 TemplateURLData data;
1111 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1112 TemplateURL url(data);
1113 base::string16 result;
1115 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1116 GURL("http://term-in-path.com/begin/something/end"),
1117 search_terms_data_, &result));
1118 EXPECT_EQ(ASCIIToUTF16("something"), result);
1120 // "%20" must be converted to space.
1121 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1122 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1123 search_terms_data_, &result));
1124 EXPECT_EQ(ASCIIToUTF16("a b c"), result);
1126 // Plus must not be converted to space.
1127 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1128 GURL("http://term-in-path.com/begin/1+2+3/end"),
1129 search_terms_data_, &result));
1130 EXPECT_EQ(ASCIIToUTF16("1+2+3"), result);
1132 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1133 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
1134 EXPECT_EQ(base::string16(), result);
1136 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1137 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
1138 EXPECT_EQ(base::string16(), result);
1140 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1141 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
1142 EXPECT_EQ(base::string16(), result);
1145 TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
1146 TemplateURLData data;
1147 data.SetURL("http://google.com/?q={searchTerms}");
1148 data.instant_url = "http://google.com/instant#q={searchTerms}";
1149 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1150 data.alternate_urls.push_back(
1151 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1152 data.search_terms_replacement_key = "espv";
1153 TemplateURL url(data);
1155 // Test with instant enabled required.
1156 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1157 GURL("http://google.com/")));
1159 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1160 GURL("http://google.com/?espv")));
1162 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1163 GURL("http://google.com/#espv")));
1165 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1166 GURL("http://google.com/?q=something")));
1168 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1169 GURL("http://google.com/?q=something&espv")));
1171 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1172 GURL("http://google.com/?q=something&espv=1")));
1174 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1175 GURL("http://google.com/?q=something&espv=0")));
1177 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1178 GURL("http://google.com/?espv&q=something")));
1180 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1181 GURL("http://google.com/?espv=1&q=something")));
1183 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1184 GURL("http://google.com/?espv=0&q=something")));
1186 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1187 GURL("http://google.com/alt/#q=something")));
1189 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1190 GURL("http://google.com/alt/#q=something&espv")));
1192 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1193 GURL("http://google.com/alt/#q=something&espv=1")));
1195 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1196 GURL("http://google.com/alt/#q=something&espv=0")));
1198 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1199 GURL("http://google.com/alt/#espv&q=something")));
1201 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1202 GURL("http://google.com/alt/#espv=1&q=something")));
1204 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1205 GURL("http://google.com/alt/#espv=0&q=something")));
1207 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1208 GURL("http://google.com/?espv#q=something")));
1210 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1211 GURL("http://google.com/?espv=1#q=something")));
1213 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1214 GURL("http://google.com/?q=something#espv")));
1216 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1217 GURL("http://google.com/?q=something#espv=1")));
1219 // This does not ensure the domain matches.
1220 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1221 GURL("http://bing.com/?espv")));
1223 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1224 GURL("http://bing.com/#espv")));
1227 TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1228 TemplateURLData data;
1229 data.SetURL("http://google.com/?q={searchTerms}");
1230 data.instant_url = "http://google.com/instant#q={searchTerms}";
1231 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1232 data.alternate_urls.push_back(
1233 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1234 TemplateURL url(data);
1235 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1236 GURL result;
1238 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1239 GURL("http://google.com/?q=something"), search_terms,
1240 search_terms_data_, &result));
1241 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result);
1243 result = GURL("http://should.not.change.com");
1244 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1245 GURL("http://google.ca/?q=something"), search_terms,
1246 search_terms_data_, &result));
1247 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1249 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1250 GURL("http://google.com/foo/?q=foo"), search_terms,
1251 search_terms_data_, &result));
1253 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1254 GURL("https://google.com/?q=foo"), search_terms,
1255 search_terms_data_, &result));
1256 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result);
1258 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1259 GURL("http://google.com:8080/?q=foo"), search_terms,
1260 search_terms_data_, &result));
1262 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1263 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1264 search_terms_data_, &result));
1265 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result);
1267 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1268 // template_url.cc for details.
1269 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1270 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1271 search_terms_data_, &result));
1272 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result);
1274 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1275 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
1276 search_terms_data_, &result));
1277 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"),
1278 result);
1280 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1281 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1282 search_terms, search_terms_data_, &result));
1283 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1284 "#j=abc&q=Bob Morane&h=def9"), result);
1286 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1287 GURL("http://google.com/alt/?q="), search_terms,
1288 search_terms_data_, &result));
1290 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1291 GURL("http://google.com/alt/?#q="), search_terms,
1292 search_terms_data_, &result));
1294 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1295 GURL("http://google.com/alt/?q=#q="), search_terms,
1296 search_terms_data_, &result));
1298 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1299 GURL("http://google.com/alt/?q=123#q="), search_terms,
1300 search_terms_data_, &result));
1302 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1303 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1304 search_terms_data_, &result));
1305 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result);
1308 TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1309 TemplateURLData data;
1310 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1311 TemplateURL url(data);
1312 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1313 GURL result;
1315 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1316 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1317 search_terms_data_, &result));
1318 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1320 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1321 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1322 search_terms_data_, &result));
1323 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1325 result = GURL("http://should.not.change.com");
1326 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1327 GURL("http://term-in-path.com/about"), search_terms,
1328 search_terms_data_, &result));
1329 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1332 // Test the |suggest_query_params| field of SearchTermsArgs.
1333 TEST_F(TemplateURLTest, SuggestQueryParams) {
1334 TemplateURLData data;
1335 // Pick a URL with replacements before, during, and after the query, to ensure
1336 // we don't goof up any of them.
1337 data.SetURL("{google:baseURL}search?q={searchTerms}"
1338 "#{google:originalQueryForSuggestion}x");
1339 TemplateURL url(data);
1341 // Baseline: no |suggest_query_params| field.
1342 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1343 search_terms.original_query = ASCIIToUTF16("def");
1344 search_terms.accepted_suggestion = 0;
1345 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1346 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1348 // Set the suggest_query_params.
1349 search_terms.suggest_query_params = "pq=xyz";
1350 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
1351 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1353 // Add extra_query_params in the mix, and ensure it works.
1354 search_terms.append_extra_query_params = true;
1355 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1356 switches::kExtraSearchQueryParams, "a=b");
1357 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
1358 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1361 // Test the |append_extra_query_params| field of SearchTermsArgs.
1362 TEST_F(TemplateURLTest, ExtraQueryParams) {
1363 TemplateURLData data;
1364 // Pick a URL with replacements before, during, and after the query, to ensure
1365 // we don't goof up any of them.
1366 data.SetURL("{google:baseURL}search?q={searchTerms}"
1367 "#{google:originalQueryForSuggestion}x");
1368 TemplateURL url(data);
1370 // Baseline: no command-line args, no |append_extra_query_params| flag.
1371 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1372 search_terms.original_query = ASCIIToUTF16("def");
1373 search_terms.accepted_suggestion = 0;
1374 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1375 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1377 // Set the flag. Since there are no command-line args, this should have no
1378 // effect.
1379 search_terms.append_extra_query_params = true;
1380 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1381 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1383 // Now append the command-line arg. This should be inserted into the query.
1384 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1385 switches::kExtraSearchQueryParams, "a=b");
1386 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
1387 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1389 // Turn off the flag. Now the command-line arg should be ignored again.
1390 search_terms.append_extra_query_params = false;
1391 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1392 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
1395 // Tests replacing pageClassification.
1396 TEST_F(TemplateURLTest, ReplacePageClassification) {
1397 TemplateURLData data;
1398 data.input_encodings.push_back("UTF-8");
1399 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
1400 TemplateURL url(data);
1401 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1402 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
1403 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1405 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1406 search_terms_data_);
1407 EXPECT_EQ("http://www.google.com/?q=foo", result);
1409 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
1410 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1411 search_terms_data_);
1412 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1414 search_terms_args.page_classification =
1415 metrics::OmniboxEventProto::HOME_PAGE;
1416 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1417 search_terms_data_);
1418 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1421 // Test the IsSearchResults function.
1422 TEST_F(TemplateURLTest, IsSearchResults) {
1423 TemplateURLData data;
1424 data.SetURL("http://bar/search?q={searchTerms}");
1425 data.instant_url = "http://bar/instant#q={searchTerms}";
1426 data.new_tab_url = "http://bar/newtab";
1427 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1428 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1429 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1430 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
1431 TemplateURL search_provider(data);
1433 const struct {
1434 const char* const url;
1435 bool result;
1436 } url_data[] = {
1437 { "http://bar/search?q=foo&oq=foo", true, },
1438 { "http://bar/?q=foo&oq=foo", true, },
1439 { "http://bar/#output=search&q=foo&oq=foo", true, },
1440 { "http://bar/webhp#q=foo&oq=foo", true, },
1441 { "http://bar/#q=foo&oq=foo", true, },
1442 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1443 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1444 { "http://bar/", false, },
1445 { "http://foo/", false, },
1446 { "http://bar/newtab", false, },
1449 for (size_t i = 0; i < arraysize(url_data); ++i) {
1450 EXPECT_EQ(url_data[i].result,
1451 search_provider.IsSearchURL(GURL(url_data[i].url),
1452 search_terms_data_));
1456 TEST_F(TemplateURLTest, ReflectsBookmarkBarPinned) {
1457 TemplateURLData data;
1458 data.input_encodings.push_back("UTF-8");
1459 data.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
1460 TemplateURL url(data);
1461 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1462 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
1463 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1465 // Do not add the param when InstantExtended is suppressed on SRPs.
1466 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(false);
1467 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1468 search_terms_data_);
1469 EXPECT_EQ("http://www.google.com/?q=foo", result);
1471 // Add the param when InstantExtended is not suppressed on SRPs.
1472 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true);
1473 search_terms_args.bookmark_bar_pinned = false;
1474 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1475 search_terms_data_);
1476 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result);
1478 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true);
1479 search_terms_args.bookmark_bar_pinned = true;
1480 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1481 search_terms_data_);
1482 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result);
1485 TEST_F(TemplateURLTest, AnswersHasVersion) {
1486 TemplateURLData data;
1487 search_terms_data_.set_google_base_url("http://bar/");
1488 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1490 TemplateURL url(data);
1491 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1492 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1493 search_terms_data_);
1494 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1496 search_terms_data_.set_enable_answers_in_suggest(true);
1497 TemplateURL url2(data);
1498 result = url2.url_ref().ReplaceSearchTerms(search_terms_args,
1499 search_terms_data_);
1500 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1503 TEST_F(TemplateURLTest, SessionToken) {
1504 TemplateURLData data;
1505 search_terms_data_.set_google_base_url("http://bar/");
1506 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1508 TemplateURL url(data);
1509 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1510 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
1511 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1512 search_terms_data_);
1513 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1515 TemplateURL url2(data);
1516 search_terms_args.session_token = "";
1517 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1518 search_terms_data_);
1519 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1522 TEST_F(TemplateURLTest, ContextualSearchParameters) {
1523 TemplateURLData data;
1524 search_terms_data_.set_google_base_url("http://bar/");
1525 data.SetURL("http://bar/_/contextualsearch?"
1526 "{google:contextualSearchVersion}"
1527 "{google:contextualSearchContextData}");
1529 TemplateURL url(data);
1530 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1531 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1532 search_terms_data_);
1533 EXPECT_EQ("http://bar/_/contextualsearch?ctxsl_resolve=1", result);
1535 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
1536 1, 6, 11, "allen", "woody+allen+movies", "www.wikipedia.org",
1537 "utf-8", true);
1538 search_terms_args.contextual_search_params = params;
1539 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1540 search_terms_data_);
1541 EXPECT_EQ("http://bar/_/contextualsearch?"
1542 "ctxs=1&"
1543 "ctxs_start=6&"
1544 "ctxs_end=11&"
1545 "q=allen&"
1546 "ctxs_content=woody+allen+movies&"
1547 "ctxsl_url=www.wikipedia.org&"
1548 "ctxs_encoding=utf-8&"
1549 "ctxsl_resolve=1",
1550 result);
1553 TEST_F(TemplateURLTest, GenerateKeyword) {
1554 ASSERT_EQ(ASCIIToUTF16("foo"),
1555 TemplateURL::GenerateKeyword(GURL("http://foo")));
1556 // www. should be stripped.
1557 ASSERT_EQ(ASCIIToUTF16("foo"),
1558 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
1559 // Make sure we don't get a trailing '/'.
1560 ASSERT_EQ(ASCIIToUTF16("blah"),
1561 TemplateURL::GenerateKeyword(GURL("http://blah/")));
1562 // Don't generate the empty string.
1563 ASSERT_EQ(ASCIIToUTF16("www"),
1564 TemplateURL::GenerateKeyword(GURL("http://www.")));
1567 TEST_F(TemplateURLTest, GenerateSearchURL) {
1568 struct GenerateSearchURLCase {
1569 const char* test_name;
1570 const char* url;
1571 const char* expected;
1572 } generate_url_cases[] = {
1573 { "invalid URL", "foo{searchTerms}", "" },
1574 { "URL with no replacements", "http://foo/", "http://foo/" },
1575 { "basic functionality", "http://foo/{searchTerms}",
1576 "http://foo/blah.blah.blah.blah.blah" }
1579 for (size_t i = 0; i < arraysize(generate_url_cases); ++i) {
1580 TemplateURLData data;
1581 data.SetURL(generate_url_cases[i].url);
1582 TemplateURL t_url(data);
1583 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1584 generate_url_cases[i].expected)
1585 << generate_url_cases[i].test_name << " failed.";
1589 TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1590 TemplateURLData data;
1591 search_terms_data_.set_google_base_url("http://bar/");
1592 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1594 TemplateURL url(data);
1595 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1596 search_terms_args.prefetch_query = "full query text";
1597 search_terms_args.prefetch_query_type = "2338";
1598 std::string result =
1599 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1600 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1601 result);
1603 TemplateURL url2(data);
1604 search_terms_args.prefetch_query.clear();
1605 search_terms_args.prefetch_query_type.clear();
1606 result =
1607 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1608 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);