Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / search_engines / template_url_unittest.cc
blob68b907b1cb7842e1971b6649d8bd0e5699215b54
1 // Copyright (c) 2012 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 "chrome/browser/browser_process.h"
11 #include "chrome/browser/rlz/rlz.h"
12 #include "chrome/browser/search_engines/search_terms_data.h"
13 #include "chrome/browser/search_engines/template_url.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/chrome_version_info.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 #if defined(ENABLE_RLZ)
19 #include "chrome/browser/google/google_util.h"
20 #endif
22 #if defined(OS_ANDROID)
23 #include "chrome/browser/search_engines/search_terms_data_android.h"
24 #endif
26 using base::ASCIIToUTF16;
28 // TestSearchTermsData --------------------------------------------------------
30 // Simple implementation of SearchTermsData.
31 class TestSearchTermsData : public SearchTermsData {
32 public:
33 explicit TestSearchTermsData(const std::string& google_base_url);
35 virtual std::string GoogleBaseURLValue() const OVERRIDE;
37 private:
38 std::string google_base_url_;
40 DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData);
43 TestSearchTermsData::TestSearchTermsData(const std::string& google_base_url)
44 : google_base_url_(google_base_url) {
47 std::string TestSearchTermsData::GoogleBaseURLValue() const {
48 return google_base_url_;
51 // TemplateURLTest ------------------------------------------------------------
53 class TemplateURLTest : public testing::Test {
54 public:
55 void CheckSuggestBaseURL(const std::string& base_url,
56 const std::string& base_suggest_url) const;
59 void TemplateURLTest::CheckSuggestBaseURL(
60 const std::string& base_url,
61 const std::string& base_suggest_url) const {
62 TestSearchTermsData search_terms_data(base_url);
63 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue());
67 // Actual tests ---------------------------------------------------------------
69 TEST_F(TemplateURLTest, Defaults) {
70 TemplateURLData data;
71 EXPECT_FALSE(data.show_in_default_list);
72 EXPECT_FALSE(data.safe_for_autoreplace);
73 EXPECT_EQ(0, data.prepopulate_id);
76 TEST_F(TemplateURLTest, TestValidWithComplete) {
77 TemplateURLData data;
78 data.SetURL("{searchTerms}");
79 TemplateURL url(NULL, data);
80 EXPECT_TRUE(url.url_ref().IsValid());
83 TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
84 struct SearchTermsCase {
85 const char* url;
86 const base::string16 terms;
87 const std::string output;
88 } search_term_cases[] = {
89 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
90 "http://foosea%20rch/bar" },
91 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
92 "http://foosea%20rch/bar?boo=abc" },
93 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
94 "http://foo/?boo=sea+rch%2Fbar" },
95 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
96 "http://en.wikipedia.org/wiki/%3F" }
98 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
99 const SearchTermsCase& value = search_term_cases[i];
100 TemplateURLData data;
101 data.SetURL(value.url);
102 TemplateURL url(NULL, data);
103 EXPECT_TRUE(url.url_ref().IsValid());
104 ASSERT_TRUE(url.url_ref().SupportsReplacement());
105 GURL result(url.url_ref().ReplaceSearchTerms(
106 TemplateURLRef::SearchTermsArgs(value.terms)));
107 ASSERT_TRUE(result.is_valid());
108 EXPECT_EQ(value.output, result.spec());
112 TEST_F(TemplateURLTest, URLRefTestCount) {
113 TemplateURLData data;
114 data.SetURL("http://foo{searchTerms}{count?}");
115 TemplateURL url(NULL, data);
116 EXPECT_TRUE(url.url_ref().IsValid());
117 ASSERT_TRUE(url.url_ref().SupportsReplacement());
118 GURL result(url.url_ref().ReplaceSearchTerms(
119 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
120 ASSERT_TRUE(result.is_valid());
121 EXPECT_EQ("http://foox/", result.spec());
124 TEST_F(TemplateURLTest, URLRefTestCount2) {
125 TemplateURLData data;
126 data.SetURL("http://foo{searchTerms}{count}");
127 TemplateURL url(NULL, data);
128 EXPECT_TRUE(url.url_ref().IsValid());
129 ASSERT_TRUE(url.url_ref().SupportsReplacement());
130 GURL result(url.url_ref().ReplaceSearchTerms(
131 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
132 ASSERT_TRUE(result.is_valid());
133 EXPECT_EQ("http://foox10/", result.spec());
136 TEST_F(TemplateURLTest, URLRefTestIndices) {
137 TemplateURLData data;
138 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
139 TemplateURL url(NULL, data);
140 EXPECT_TRUE(url.url_ref().IsValid());
141 ASSERT_TRUE(url.url_ref().SupportsReplacement());
142 GURL result(url.url_ref().ReplaceSearchTerms(
143 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
144 ASSERT_TRUE(result.is_valid());
145 EXPECT_EQ("http://fooxxy/", result.spec());
148 TEST_F(TemplateURLTest, URLRefTestIndices2) {
149 TemplateURLData data;
150 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
151 TemplateURL url(NULL, data);
152 EXPECT_TRUE(url.url_ref().IsValid());
153 ASSERT_TRUE(url.url_ref().SupportsReplacement());
154 GURL result(url.url_ref().ReplaceSearchTerms(
155 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
156 ASSERT_TRUE(result.is_valid());
157 EXPECT_EQ("http://fooxx1y1/", result.spec());
160 TEST_F(TemplateURLTest, URLRefTestEncoding) {
161 TemplateURLData data;
162 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
163 TemplateURL url(NULL, data);
164 EXPECT_TRUE(url.url_ref().IsValid());
165 ASSERT_TRUE(url.url_ref().SupportsReplacement());
166 GURL result(url.url_ref().ReplaceSearchTerms(
167 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
168 ASSERT_TRUE(result.is_valid());
169 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
172 TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
173 const char kInvalidPostParamsString[] =
174 "unknown_template={UnknownTemplate},bad_value=bad{value},"
175 "{google:sbiSource}";
176 // List all accpectable parameter format in valid_post_params_string. it is
177 // expected like: "name0=,name1=value1,name2={template1}"
178 const char kValidPostParamsString[] =
179 "image_content={google:imageThumbnail},image_url={google:imageURL},"
180 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
181 "constant_param=constant,width={google:imageOriginalWidth}";
182 const char KImageSearchURL[] = "http://foo.com/sbi";
184 TemplateURLData data;
185 data.image_url = KImageSearchURL;
187 // Try to parse invalid post parameters.
188 data.image_url_post_params = kInvalidPostParamsString;
189 TemplateURL url_bad(NULL, data);
190 ASSERT_FALSE(url_bad.image_url_ref().IsValid());
191 const TemplateURLRef::PostParams& bad_post_params =
192 url_bad.image_url_ref().post_params_;
193 ASSERT_EQ(2U, bad_post_params.size());
194 EXPECT_EQ("unknown_template", bad_post_params[0].first);
195 EXPECT_EQ("{UnknownTemplate}", bad_post_params[0].second);
196 EXPECT_EQ("bad_value", bad_post_params[1].first);
197 EXPECT_EQ("bad{value}", bad_post_params[1].second);
199 // Try to parse valid post parameters.
200 data.image_url_post_params = kValidPostParamsString;
201 TemplateURL url(NULL, data);
202 ASSERT_TRUE(url.image_url_ref().IsValid());
203 ASSERT_FALSE(url.image_url_ref().SupportsReplacement());
205 // Check term replacement.
206 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
207 search_args.image_thumbnail_content = "dummy-image-thumbnail";
208 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
209 search_args.image_original_size = gfx::Size(10, 10);
210 // Replacement operation with no post_data buffer should still return
211 // the parsed URL.
212 GURL result(url.image_url_ref().ReplaceSearchTerms(search_args));
213 ASSERT_TRUE(result.is_valid());
214 EXPECT_EQ(KImageSearchURL, result.spec());
215 TemplateURLRef::PostContent post_content;
216 TestSearchTermsData search_terms_data("http://X");
217 result = GURL(url.image_url_ref().ReplaceSearchTermsUsingTermsData(
218 search_args, search_terms_data, &post_content));
219 ASSERT_TRUE(result.is_valid());
220 EXPECT_EQ(KImageSearchURL, result.spec());
221 ASSERT_FALSE(post_content.first.empty());
222 ASSERT_FALSE(post_content.second.empty());
224 // Check parsed result of post parameters.
225 const TemplateURLRef::Replacements& replacements =
226 url.image_url_ref().replacements_;
227 const TemplateURLRef::PostParams& post_params =
228 url.image_url_ref().post_params_;
229 EXPECT_EQ(7U, post_params.size());
230 for (TemplateURLRef::PostParams::const_iterator i = post_params.begin();
231 i != post_params.end(); ++i) {
232 TemplateURLRef::Replacements::const_iterator j = replacements.begin();
233 for (; j != replacements.end(); ++j) {
234 if (j->is_post_param && j->index ==
235 static_cast<size_t>(i - post_params.begin())) {
236 switch (j->type) {
237 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
238 EXPECT_EQ("width", i->first);
239 EXPECT_EQ(
240 base::IntToString(search_args.image_original_size.width()),
241 i->second);
242 break;
243 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
244 EXPECT_EQ("image_content", i->first);
245 EXPECT_EQ(search_args.image_thumbnail_content, i->second);
246 break;
247 case TemplateURLRef::GOOGLE_IMAGE_URL:
248 EXPECT_EQ("image_url", i->first);
249 EXPECT_EQ(search_args.image_url.spec(), i->second);
250 break;
251 case TemplateURLRef::LANGUAGE:
252 EXPECT_EQ("language", i->first);
253 EXPECT_EQ("en", i->second);
254 break;
255 default:
256 ADD_FAILURE(); // Should never go here.
258 break;
261 if (j != replacements.end())
262 continue;
263 if (i->first == "empty_param") {
264 EXPECT_TRUE(i->second.empty());
265 } else if (i->first == "sbisrc") {
266 EXPECT_FALSE(i->second.empty());
267 } else {
268 EXPECT_EQ("constant_param", i->first);
269 EXPECT_EQ("constant", i->second);
274 // Test that setting the prepopulate ID from TemplateURL causes the stored
275 // TemplateURLRef to handle parsing the URL parameters differently.
276 TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
277 TemplateURLData data;
278 data.SetURL("http://foo{fhqwhgads}bar");
279 TemplateURL url(NULL, data);
280 TemplateURLRef::Replacements replacements;
281 bool valid = false;
282 EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL(
283 "http://foo{fhqwhgads}bar", &replacements, NULL, &valid));
284 EXPECT_TRUE(replacements.empty());
285 EXPECT_TRUE(valid);
287 data.prepopulate_id = 123;
288 TemplateURL url2(NULL, data);
289 EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar",
290 &replacements, NULL,
291 &valid));
292 EXPECT_TRUE(replacements.empty());
293 EXPECT_TRUE(valid);
296 TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
297 TemplateURLData data;
298 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
299 TemplateURL url(NULL, data);
300 EXPECT_TRUE(url.url_ref().IsValid());
301 ASSERT_TRUE(url.url_ref().SupportsReplacement());
302 GURL result(url.url_ref().ReplaceSearchTerms(
303 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
304 ASSERT_TRUE(result.is_valid());
305 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
308 TEST_F(TemplateURLTest, URLRefTestEncoding2) {
309 TemplateURLData data;
310 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
311 TemplateURL url(NULL, data);
312 EXPECT_TRUE(url.url_ref().IsValid());
313 ASSERT_TRUE(url.url_ref().SupportsReplacement());
314 GURL result(url.url_ref().ReplaceSearchTerms(
315 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
316 ASSERT_TRUE(result.is_valid());
317 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
320 TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
321 struct SearchTermsCase {
322 const char* url;
323 const base::string16 terms;
324 const char* output;
325 } search_term_cases[] = {
326 { "{google:baseURL}{language}{searchTerms}", base::string16(),
327 "http://example.com/e/en" },
328 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
329 "http://example.com/complete/" }
332 TestSearchTermsData search_terms_data("http://example.com/e/");
333 TemplateURLData data;
334 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
335 const SearchTermsCase& value = search_term_cases[i];
336 data.SetURL(value.url);
337 TemplateURL url(NULL, data);
338 EXPECT_TRUE(url.url_ref().IsValid());
339 ASSERT_TRUE(url.url_ref().SupportsReplacement());
340 GURL result(url.url_ref().ReplaceSearchTermsUsingTermsData(
341 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL));
342 ASSERT_TRUE(result.is_valid());
343 EXPECT_EQ(value.output, result.spec());
347 TEST_F(TemplateURLTest, URLRefTermToWide) {
348 struct ToWideCase {
349 const char* encoded_search_term;
350 const base::string16 expected_decoded_term;
351 } to_wide_cases[] = {
352 {"hello+world", ASCIIToUTF16("hello world")},
353 // Test some big-5 input.
354 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")},
355 // Test some UTF-8 input. We should fall back to this when the encoding
356 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
357 // Big-5 trailing byte.
358 {"%e4%bd%a05%e5%a5%bd+to+you",
359 base::WideToUTF16(L"\x4f60\x35\x597d to you")},
360 // Undecodable input should stay escaped.
361 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")},
362 // Make sure we convert %2B to +.
363 {"C%2B%2B", ASCIIToUTF16("C++")},
364 // C%2B is escaped as C%252B, make sure we unescape it properly.
365 {"C%252B", ASCIIToUTF16("C%2B")},
368 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
369 TemplateURLData data;
370 data.SetURL("http://foo?q={searchTerms}");
371 data.input_encodings.push_back("big-5");
372 TemplateURL url(NULL, data);
373 EXPECT_TRUE(url.url_ref().IsValid());
374 ASSERT_TRUE(url.url_ref().SupportsReplacement());
375 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) {
376 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
377 url.url_ref().SearchTermToString16(
378 to_wide_cases[i].encoded_search_term));
382 TEST_F(TemplateURLTest, DisplayURLToURLRef) {
383 struct TestData {
384 const std::string url;
385 const base::string16 expected_result;
386 } test_data[] = {
387 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
388 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
389 { "http://X",
390 ASCIIToUTF16("http://X") },
391 { "http://foo{searchTerms",
392 ASCIIToUTF16("http://foo{searchTerms") },
393 { "http://foo{searchTerms}{language}",
394 ASCIIToUTF16("http://foo%s{language}") },
396 TemplateURLData data;
397 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
398 data.SetURL(test_data[i].url);
399 TemplateURL url(NULL, data);
400 EXPECT_EQ(test_data[i].expected_result, url.url_ref().DisplayURL());
401 EXPECT_EQ(test_data[i].url,
402 TemplateURLRef::DisplayURLToURLRef(url.url_ref().DisplayURL()));
406 TEST_F(TemplateURLTest, ReplaceSearchTerms) {
407 struct TestData {
408 const std::string url;
409 const std::string expected_result;
410 } test_data[] = {
411 { "http://foo/{language}{searchTerms}{inputEncoding}",
412 "http://foo/{language}XUTF-8" },
413 { "http://foo/{language}{inputEncoding}{searchTerms}",
414 "http://foo/{language}UTF-8X" },
415 { "http://foo/{searchTerms}{language}{inputEncoding}",
416 "http://foo/X{language}UTF-8" },
417 { "http://foo/{searchTerms}{inputEncoding}{language}",
418 "http://foo/XUTF-8{language}" },
419 { "http://foo/{inputEncoding}{searchTerms}{language}",
420 "http://foo/UTF-8X{language}" },
421 { "http://foo/{inputEncoding}{language}{searchTerms}",
422 "http://foo/UTF-8{language}X" },
423 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
424 "http://foo/{language}aXaUTF-8a" },
425 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
426 "http://foo/{language}aUTF-8aXa" },
427 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
428 "http://foo/Xa{language}aUTF-8a" },
429 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
430 "http://foo/XaUTF-8a{language}a" },
431 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
432 "http://foo/UTF-8aXa{language}a" },
433 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
434 "http://foo/UTF-8a{language}aXa" },
436 TemplateURLData data;
437 data.input_encodings.push_back("UTF-8");
438 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
439 data.SetURL(test_data[i].url);
440 TemplateURL url(NULL, data);
441 EXPECT_TRUE(url.url_ref().IsValid());
442 ASSERT_TRUE(url.url_ref().SupportsReplacement());
443 std::string expected_result = test_data[i].expected_result;
444 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}",
445 g_browser_process->GetApplicationLocale());
446 GURL result(url.url_ref().ReplaceSearchTerms(
447 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
448 ASSERT_TRUE(result.is_valid());
449 EXPECT_EQ(expected_result, result.spec());
454 // Tests replacing search terms in various encodings and making sure the
455 // generated URL matches the expected value.
456 TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
457 struct TestData {
458 const std::string encoding;
459 const base::string16 search_term;
460 const std::string url;
461 const std::string expected_result;
462 } test_data[] = {
463 { "BIG5", base::WideToUTF16(L"\x60BD"),
464 "http://foo/?{searchTerms}{inputEncoding}",
465 "http://foo/?%B1~BIG5" },
466 { "UTF-8", ASCIIToUTF16("blah"),
467 "http://foo/?{searchTerms}{inputEncoding}",
468 "http://foo/?blahUTF-8" },
469 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
470 "http://foo/{searchTerms}/bar",
471 "http://foo/%82%A0/bar"},
472 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
473 "http://foo/{searchTerms}/bar",
474 "http://foo/%82%A0%20%82%A2/bar"},
476 TemplateURLData data;
477 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
478 data.SetURL(test_data[i].url);
479 data.input_encodings.clear();
480 data.input_encodings.push_back(test_data[i].encoding);
481 TemplateURL url(NULL, data);
482 EXPECT_TRUE(url.url_ref().IsValid());
483 ASSERT_TRUE(url.url_ref().SupportsReplacement());
484 GURL result(url.url_ref().ReplaceSearchTerms(
485 TemplateURLRef::SearchTermsArgs(test_data[i].search_term)));
486 ASSERT_TRUE(result.is_valid());
487 EXPECT_EQ(test_data[i].expected_result, result.spec());
491 // Tests replacing assisted query stats (AQS) in various scenarios.
492 TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
493 struct TestData {
494 const base::string16 search_term;
495 const std::string aqs;
496 const std::string base_url;
497 const std::string url;
498 const std::string expected_result;
499 } test_data[] = {
500 // No HTTPS, no AQS.
501 { ASCIIToUTF16("foo"),
502 "chrome.0.0l6",
503 "http://foo/",
504 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
505 "http://foo/?foo" },
506 // HTTPS available, AQS should be replaced.
507 { ASCIIToUTF16("foo"),
508 "chrome.0.0l6",
509 "https://foo/",
510 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
511 "https://foo/?fooaqs=chrome.0.0l6&" },
512 // HTTPS available, however AQS is empty.
513 { ASCIIToUTF16("foo"),
515 "https://foo/",
516 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
517 "https://foo/?foo" },
518 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
519 { ASCIIToUTF16("foo"),
520 "chrome.0.0l6",
522 "http://foo?{searchTerms}{google:assistedQueryStats}",
523 "http://foo/?foo" },
524 // A non-Google search provider with HTTPS should allow AQS.
525 { ASCIIToUTF16("foo"),
526 "chrome.0.0l6",
528 "https://foo?{searchTerms}{google:assistedQueryStats}",
529 "https://foo/?fooaqs=chrome.0.0l6&" },
531 TemplateURLData data;
532 data.input_encodings.push_back("UTF-8");
533 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
534 data.SetURL(test_data[i].url);
535 TemplateURL url(NULL, data);
536 EXPECT_TRUE(url.url_ref().IsValid());
537 ASSERT_TRUE(url.url_ref().SupportsReplacement());
538 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
539 search_terms_args.assisted_query_stats = test_data[i].aqs;
540 UIThreadSearchTermsData::SetGoogleBaseURL(test_data[i].base_url);
541 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
542 ASSERT_TRUE(result.is_valid());
543 EXPECT_EQ(test_data[i].expected_result, result.spec());
547 // Tests replacing cursor position.
548 TEST_F(TemplateURLTest, ReplaceCursorPosition) {
549 struct TestData {
550 const base::string16 search_term;
551 size_t cursor_position;
552 const std::string url;
553 const std::string expected_result;
554 } test_data[] = {
555 { ASCIIToUTF16("foo"),
556 base::string16::npos,
557 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
558 "http://www.google.com/?foo&" },
559 { ASCIIToUTF16("foo"),
561 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
562 "http://www.google.com/?foo&cp=2&" },
563 { ASCIIToUTF16("foo"),
565 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
566 "http://www.google.com/?foo&cp=15&" },
568 TemplateURLData data;
569 data.input_encodings.push_back("UTF-8");
570 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
571 data.SetURL(test_data[i].url);
572 TemplateURL url(NULL, data);
573 EXPECT_TRUE(url.url_ref().IsValid());
574 ASSERT_TRUE(url.url_ref().SupportsReplacement());
575 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
576 search_terms_args.cursor_position = test_data[i].cursor_position;
577 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
578 ASSERT_TRUE(result.is_valid());
579 EXPECT_EQ(test_data[i].expected_result, result.spec());
583 // Tests replacing currentPageUrl.
584 TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
585 struct TestData {
586 const base::string16 search_term;
587 const std::string current_page_url;
588 const std::string url;
589 const std::string expected_result;
590 } test_data[] = {
591 { ASCIIToUTF16("foo"),
592 "http://www.google.com/",
593 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
594 "http://www.google.com/?foo&url=http%3A%2F%2Fwww.google.com%2F&" },
595 { ASCIIToUTF16("foo"),
597 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
598 "http://www.google.com/?foo&" },
599 { ASCIIToUTF16("foo"),
600 "http://g.com/+-/*&=",
601 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
602 "http://www.google.com/?foo&url=http%3A%2F%2Fg.com%2F%2B-%2F*%26%3D&" },
604 TemplateURLData data;
605 data.input_encodings.push_back("UTF-8");
606 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
607 data.SetURL(test_data[i].url);
608 TemplateURL url(NULL, data);
609 EXPECT_TRUE(url.url_ref().IsValid());
610 ASSERT_TRUE(url.url_ref().SupportsReplacement());
611 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
612 search_terms_args.current_page_url = test_data[i].current_page_url;
613 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
614 ASSERT_TRUE(result.is_valid());
615 EXPECT_EQ(test_data[i].expected_result, result.spec());
619 TEST_F(TemplateURLTest, Suggestions) {
620 struct TestData {
621 const int accepted_suggestion;
622 const base::string16 original_query_for_suggestion;
623 const std::string expected_result;
624 } test_data[] = {
625 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
626 "http://bar/foo?q=foobar" },
627 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
628 "http://bar/foo?q=foobar" },
629 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(),
630 "http://bar/foo?q=foobar" },
631 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
632 "http://bar/foo?q=foobar" },
633 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
634 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
636 TemplateURLData data;
637 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
638 "q={searchTerms}");
639 data.input_encodings.push_back("UTF-8");
640 TemplateURL url(NULL, data);
641 EXPECT_TRUE(url.url_ref().IsValid());
642 ASSERT_TRUE(url.url_ref().SupportsReplacement());
643 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
644 TemplateURLRef::SearchTermsArgs search_terms_args(
645 ASCIIToUTF16("foobar"));
646 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
647 search_terms_args.original_query =
648 test_data[i].original_query_for_suggestion;
649 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
650 ASSERT_TRUE(result.is_valid());
651 EXPECT_EQ(test_data[i].expected_result, result.spec());
655 TEST_F(TemplateURLTest, RLZ) {
656 base::string16 rlz_string;
657 #if defined(ENABLE_RLZ)
658 std::string brand;
659 if (google_util::GetBrand(&brand) && !brand.empty() &&
660 !google_util::IsOrganic(brand)) {
661 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz_string);
663 #elif defined(OS_ANDROID)
664 SearchTermsDataAndroid::rlz_parameter_value_.Get() =
665 ASCIIToUTF16("android_test");
666 rlz_string = SearchTermsDataAndroid::rlz_parameter_value_.Get();
667 #endif
669 TemplateURLData data;
670 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
671 TemplateURL url(NULL, data);
672 EXPECT_TRUE(url.url_ref().IsValid());
673 ASSERT_TRUE(url.url_ref().SupportsReplacement());
674 GURL result(url.url_ref().ReplaceSearchTerms(
675 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x"))));
676 ASSERT_TRUE(result.is_valid());
677 std::string expected_url = "http://bar/?";
678 if (!rlz_string.empty())
679 expected_url += "rlz=" + base::UTF16ToUTF8(rlz_string) + "&";
680 expected_url += "x";
681 EXPECT_EQ(expected_url, result.spec());
684 TEST_F(TemplateURLTest, HostAndSearchTermKey) {
685 struct TestData {
686 const std::string url;
687 const std::string host;
688 const std::string path;
689 const std::string search_term_key;
690 } test_data[] = {
691 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
693 // No query key should result in empty values.
694 { "http://blah/{searchTerms}", "", "", ""},
696 // No term should result in empty values.
697 { "http://blah/", "", "", ""},
699 // Multiple terms should result in empty values.
700 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
702 // Term in the host shouldn't match.
703 { "http://{searchTerms}", "", "", ""},
705 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
706 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
708 // Single term with extra chars in value should match.
709 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
712 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
713 TemplateURLData data;
714 data.SetURL(test_data[i].url);
715 TemplateURL url(NULL, data);
716 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost());
717 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath());
718 EXPECT_EQ(test_data[i].search_term_key, url.url_ref().GetSearchTermKey());
722 TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
723 static const struct {
724 const char* const base_url;
725 const char* const base_suggest_url;
726 } data[] = {
727 { "http://google.com/", "http://google.com/complete/", },
728 { "http://www.google.com/", "http://www.google.com/complete/", },
729 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
730 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
731 { "http://google.com/intl/xx/", "http://google.com/complete/", },
734 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i)
735 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
738 TEST_F(TemplateURLTest, ParseParameterKnown) {
739 std::string parsed_url("{searchTerms}");
740 TemplateURLData data;
741 data.SetURL(parsed_url);
742 TemplateURL url(NULL, data);
743 TemplateURLRef::Replacements replacements;
744 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
745 EXPECT_EQ(std::string(), parsed_url);
746 ASSERT_EQ(1U, replacements.size());
747 EXPECT_EQ(0U, replacements[0].index);
748 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
751 TEST_F(TemplateURLTest, ParseParameterUnknown) {
752 std::string parsed_url("{fhqwhgads}abc");
753 TemplateURLData data;
754 data.SetURL(parsed_url);
755 TemplateURL url(NULL, data);
756 TemplateURLRef::Replacements replacements;
758 // By default, TemplateURLRef should not consider itself prepopulated.
759 // Therefore we should not replace the unknown parameter.
760 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
761 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
762 EXPECT_TRUE(replacements.empty());
764 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
765 parsed_url = "{fhqwhgads}abc";
766 data.prepopulate_id = 1;
767 TemplateURL url2(NULL, data);
768 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
769 EXPECT_EQ("abc", parsed_url);
770 EXPECT_TRUE(replacements.empty());
773 TEST_F(TemplateURLTest, ParseURLEmpty) {
774 TemplateURL url(NULL, TemplateURLData());
775 TemplateURLRef::Replacements replacements;
776 bool valid = false;
777 EXPECT_EQ(std::string(),
778 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
779 EXPECT_TRUE(replacements.empty());
780 EXPECT_TRUE(valid);
783 TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
784 TemplateURLData data;
785 data.SetURL("{");
786 TemplateURL url(NULL, data);
787 TemplateURLRef::Replacements replacements;
788 bool valid = false;
789 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
790 &valid));
791 EXPECT_TRUE(replacements.empty());
792 EXPECT_FALSE(valid);
795 TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
796 TemplateURLData data;
797 data.SetURL("{}");
798 TemplateURL url(NULL, data);
799 TemplateURLRef::Replacements replacements;
800 bool valid = false;
801 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
802 EXPECT_TRUE(replacements.empty());
803 EXPECT_TRUE(valid);
806 TEST_F(TemplateURLTest, ParseURLTwoParameters) {
807 TemplateURLData data;
808 data.SetURL("{}{{%s}}");
809 TemplateURL url(NULL, data);
810 TemplateURLRef::Replacements replacements;
811 bool valid = false;
812 EXPECT_EQ("{}{}",
813 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
814 &valid));
815 ASSERT_EQ(1U, replacements.size());
816 EXPECT_EQ(3U, replacements[0].index);
817 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
818 EXPECT_TRUE(valid);
821 TEST_F(TemplateURLTest, ParseURLNestedParameter) {
822 TemplateURLData data;
823 data.SetURL("{%s");
824 TemplateURL url(NULL, data);
825 TemplateURLRef::Replacements replacements;
826 bool valid = false;
827 EXPECT_EQ("{",
828 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
829 &valid));
830 ASSERT_EQ(1U, replacements.size());
831 EXPECT_EQ(1U, replacements[0].index);
832 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
833 EXPECT_TRUE(valid);
836 #if defined(OS_ANDROID)
837 TEST_F(TemplateURLTest, SearchClient) {
838 const std::string base_url_str("http://google.com/?");
839 const std::string terms_str("{searchTerms}&{google:searchClient}");
840 const std::string full_url_str = base_url_str + terms_str;
841 const base::string16 terms(ASCIIToUTF16(terms_str));
842 UIThreadSearchTermsData::SetGoogleBaseURL(base_url_str);
844 TemplateURLData data;
845 data.SetURL(full_url_str);
846 TemplateURL url(NULL, data);
847 EXPECT_TRUE(url.url_ref().IsValid());
848 ASSERT_TRUE(url.url_ref().SupportsReplacement());
849 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
851 // Check that the URL is correct when a client is not present.
852 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
853 ASSERT_TRUE(result.is_valid());
854 EXPECT_EQ("http://google.com/?foobar&", result.spec());
856 // Check that the URL is correct when a client is present.
857 SearchTermsDataAndroid::search_client_.Get() = "android_test";
858 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args));
859 ASSERT_TRUE(result_2.is_valid());
860 EXPECT_EQ("http://google.com/?foobar&client=android_test&", result_2.spec());
862 #endif
864 TEST_F(TemplateURLTest, GetURLNoInstantURL) {
865 TemplateURLData data;
866 data.SetURL("http://google.com/?q={searchTerms}");
867 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
868 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
869 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
870 TemplateURL url(NULL, data);
871 ASSERT_EQ(3U, url.URLCount());
872 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
873 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
874 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
877 TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
878 TemplateURLData data;
879 data.SetURL("http://google.com/?q={searchTerms}");
880 data.instant_url = "http://google.com/instant#q={searchTerms}";
881 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
882 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
883 TemplateURL url(NULL, data);
884 ASSERT_EQ(3U, url.URLCount());
885 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
886 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
887 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
890 TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
891 TemplateURLData data;
892 data.SetURL("http://www.google.co.uk/");
893 TemplateURL url(NULL, data);
894 ASSERT_EQ(1U, url.URLCount());
895 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0));
898 TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
899 TemplateURLData data;
900 data.SetURL("http://google.com/?q={searchTerms}");
901 data.instant_url = "http://google.com/instant#q={searchTerms}";
902 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
903 data.alternate_urls.push_back(
904 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
905 TemplateURL url(NULL, data);
906 base::string16 result;
908 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
909 GURL("http://google.com/?q=something"), &result));
910 EXPECT_EQ(ASCIIToUTF16("something"), result);
912 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
913 GURL("http://google.com/?espv&q=something"), &result));
914 EXPECT_EQ(ASCIIToUTF16("something"), result);
916 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
917 GURL("http://google.com/?espv=1&q=something"), &result));
918 EXPECT_EQ(ASCIIToUTF16("something"), result);
920 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
921 GURL("http://google.com/?espv=0&q=something"), &result));
922 EXPECT_EQ(ASCIIToUTF16("something"), result);
924 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
925 GURL("http://google.com/alt/#q=something"), &result));
926 EXPECT_EQ(ASCIIToUTF16("something"), result);
928 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
929 GURL("http://google.com/alt/#espv&q=something"), &result));
930 EXPECT_EQ(ASCIIToUTF16("something"), result);
932 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
933 GURL("http://google.com/alt/#espv=1&q=something"), &result));
934 EXPECT_EQ(ASCIIToUTF16("something"), result);
936 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
937 GURL("http://google.com/alt/#espv=0&q=something"), &result));
938 EXPECT_EQ(ASCIIToUTF16("something"), result);
940 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
941 GURL("http://google.ca/?q=something"), &result));
942 EXPECT_EQ(base::string16(), result);
944 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
945 GURL("http://google.ca/?q=something&q=anything"), &result));
946 EXPECT_EQ(base::string16(), result);
948 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
949 GURL("http://google.com/foo/?q=foo"), &result));
950 EXPECT_EQ(base::string16(), result);
952 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
953 GURL("https://google.com/?q=foo"), &result));
954 EXPECT_EQ(ASCIIToUTF16("foo"), result);
956 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
957 GURL("http://google.com:8080/?q=foo"), &result));
958 EXPECT_EQ(base::string16(), result);
960 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
961 GURL("http://google.com/?q=1+2+3&b=456"), &result));
962 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
964 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
965 GURL("http://google.com/alt/?q=123#q=456"), &result));
966 EXPECT_EQ(ASCIIToUTF16("456"), result);
968 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
969 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), &result));
970 EXPECT_EQ(ASCIIToUTF16("123"), result);
972 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
973 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), &result));
974 EXPECT_EQ(ASCIIToUTF16("789"), result);
976 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
977 GURL("http://google.com/alt/?q="), &result));
978 EXPECT_EQ(base::string16(), result);
980 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
981 GURL("http://google.com/alt/?#q="), &result));
982 EXPECT_EQ(base::string16(), result);
984 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
985 GURL("http://google.com/alt/?q=#q="), &result));
986 EXPECT_EQ(base::string16(), result);
988 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
989 GURL("http://google.com/alt/?q=123#q="), &result));
990 EXPECT_EQ(base::string16(), result);
992 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
993 GURL("http://google.com/alt/?q=#q=123"), &result));
994 EXPECT_EQ(ASCIIToUTF16("123"), result);
997 TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
998 TemplateURLData data;
999 data.SetURL("http://google.com/?q={searchTerms}");
1000 data.instant_url = "http://google.com/instant#q={searchTerms}";
1001 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1002 data.alternate_urls.push_back(
1003 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1004 data.search_terms_replacement_key = "espv";
1005 TemplateURL url(NULL, data);
1007 // Test with instant enabled required.
1008 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1009 GURL("http://google.com/")));
1011 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1012 GURL("http://google.com/?espv")));
1014 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1015 GURL("http://google.com/#espv")));
1017 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1018 GURL("http://google.com/?q=something")));
1020 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1021 GURL("http://google.com/?q=something&espv")));
1023 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1024 GURL("http://google.com/?q=something&espv=1")));
1026 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1027 GURL("http://google.com/?q=something&espv=0")));
1029 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1030 GURL("http://google.com/?espv&q=something")));
1032 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1033 GURL("http://google.com/?espv=1&q=something")));
1035 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1036 GURL("http://google.com/?espv=0&q=something")));
1038 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1039 GURL("http://google.com/alt/#q=something")));
1041 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1042 GURL("http://google.com/alt/#q=something&espv")));
1044 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1045 GURL("http://google.com/alt/#q=something&espv=1")));
1047 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1048 GURL("http://google.com/alt/#q=something&espv=0")));
1050 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1051 GURL("http://google.com/alt/#espv&q=something")));
1053 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1054 GURL("http://google.com/alt/#espv=1&q=something")));
1056 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1057 GURL("http://google.com/alt/#espv=0&q=something")));
1059 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1060 GURL("http://google.com/?espv#q=something")));
1062 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1063 GURL("http://google.com/?espv=1#q=something")));
1065 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1066 GURL("http://google.com/?q=something#espv")));
1068 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1069 GURL("http://google.com/?q=something#espv=1")));
1071 // This does not ensure the domain matches.
1072 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1073 GURL("http://bing.com/?espv")));
1075 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1076 GURL("http://bing.com/#espv")));
1079 TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1080 TemplateURLData data;
1081 data.SetURL("http://google.com/?q={searchTerms}");
1082 data.instant_url = "http://google.com/instant#q={searchTerms}";
1083 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1084 data.alternate_urls.push_back(
1085 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1086 TemplateURL url(NULL, data);
1087 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1088 GURL result;
1090 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1091 GURL("http://google.com/?q=something"), search_terms, &result));
1092 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result);
1094 result = GURL("http://should.not.change.com");
1095 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1096 GURL("http://google.ca/?q=something"), search_terms, &result));
1097 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1099 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1100 GURL("http://google.com/foo/?q=foo"), search_terms, &result));
1102 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1103 GURL("https://google.com/?q=foo"), search_terms, &result));
1104 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result);
1106 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1107 GURL("http://google.com:8080/?q=foo"), search_terms, &result));
1109 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1110 GURL("http://google.com/?q=1+2+3&b=456"), search_terms, &result));
1111 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result);
1113 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1114 // template_url.cc for details.
1115 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1116 GURL("http://google.com/alt/?q=123#q=456"), search_terms, &result));
1117 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result);
1119 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1120 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
1121 &result));
1122 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"),
1123 result);
1125 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1126 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1127 search_terms, &result));
1128 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1129 "#j=abc&q=Bob Morane&h=def9"), result);
1131 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1132 GURL("http://google.com/alt/?q="), search_terms, &result));
1134 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1135 GURL("http://google.com/alt/?#q="), search_terms, &result));
1137 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1138 GURL("http://google.com/alt/?q=#q="), search_terms, &result));
1140 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1141 GURL("http://google.com/alt/?q=123#q="), search_terms, &result));
1143 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1144 GURL("http://google.com/alt/?q=#q=123"), search_terms, &result));
1145 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result);
1148 // Test the |suggest_query_params| field of SearchTermsArgs.
1149 TEST_F(TemplateURLTest, SuggestQueryParams) {
1150 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1151 TemplateURLData data;
1152 // Pick a URL with replacements before, during, and after the query, to ensure
1153 // we don't goof up any of them.
1154 data.SetURL("{google:baseURL}search?q={searchTerms}"
1155 "#{google:originalQueryForSuggestion}x");
1156 TemplateURL url(NULL, data);
1158 // Baseline: no |suggest_query_params| field.
1159 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1160 search_terms.original_query = ASCIIToUTF16("def");
1161 search_terms.accepted_suggestion = 0;
1162 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1163 url.url_ref().ReplaceSearchTerms(search_terms));
1165 // Set the suggest_query_params.
1166 search_terms.suggest_query_params = "pq=xyz";
1167 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
1168 url.url_ref().ReplaceSearchTerms(search_terms));
1170 // Add extra_query_params in the mix, and ensure it works.
1171 search_terms.append_extra_query_params = true;
1172 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1173 switches::kExtraSearchQueryParams, "a=b");
1174 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
1175 url.url_ref().ReplaceSearchTerms(search_terms));
1178 // Test the |append_extra_query_params| field of SearchTermsArgs.
1179 TEST_F(TemplateURLTest, ExtraQueryParams) {
1180 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1181 TemplateURLData data;
1182 // Pick a URL with replacements before, during, and after the query, to ensure
1183 // we don't goof up any of them.
1184 data.SetURL("{google:baseURL}search?q={searchTerms}"
1185 "#{google:originalQueryForSuggestion}x");
1186 TemplateURL url(NULL, data);
1188 // Baseline: no command-line args, no |append_extra_query_params| flag.
1189 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1190 search_terms.original_query = ASCIIToUTF16("def");
1191 search_terms.accepted_suggestion = 0;
1192 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1193 url.url_ref().ReplaceSearchTerms(search_terms));
1195 // Set the flag. Since there are no command-line args, this should have no
1196 // effect.
1197 search_terms.append_extra_query_params = true;
1198 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1199 url.url_ref().ReplaceSearchTerms(search_terms));
1201 // Now append the command-line arg. This should be inserted into the query.
1202 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1203 switches::kExtraSearchQueryParams, "a=b");
1204 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
1205 url.url_ref().ReplaceSearchTerms(search_terms));
1207 // Turn off the flag. Now the command-line arg should be ignored again.
1208 search_terms.append_extra_query_params = false;
1209 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1210 url.url_ref().ReplaceSearchTerms(search_terms));
1213 // Tests replacing pageClassification.
1214 TEST_F(TemplateURLTest, ReplacePageClassification) {
1215 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1216 TemplateURLData data;
1217 data.input_encodings.push_back("UTF-8");
1218 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
1219 TemplateURL url(NULL, data);
1220 EXPECT_TRUE(url.url_ref().IsValid());
1221 ASSERT_TRUE(url.url_ref().SupportsReplacement());
1222 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1224 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1225 EXPECT_EQ("http://www.google.com/?q=foo", result);
1227 search_terms_args.page_classification = AutocompleteInput::NTP;
1228 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1229 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1231 search_terms_args.page_classification =
1232 AutocompleteInput::HOME_PAGE;
1233 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1234 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1237 // Test the IsSearchResults function.
1238 TEST_F(TemplateURLTest, IsSearchResults) {
1239 TemplateURLData data;
1240 data.SetURL("http://bar/search?q={searchTerms}");
1241 data.instant_url = "http://bar/instant#q={searchTerms}";
1242 data.new_tab_url = "http://bar/newtab";
1243 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1244 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1245 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1246 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
1247 TemplateURL search_provider(NULL, data);
1249 const struct {
1250 const char* const url;
1251 bool result;
1252 } url_data[] = {
1253 { "http://bar/search?q=foo&oq=foo", true, },
1254 { "http://bar/?q=foo&oq=foo", true, },
1255 { "http://bar/#output=search&q=foo&oq=foo", true, },
1256 { "http://bar/webhp#q=foo&oq=foo", true, },
1257 { "http://bar/#q=foo&oq=foo", true, },
1258 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1259 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1260 { "http://bar/", false, },
1261 { "http://foo/", false, },
1262 { "http://bar/newtab", false, },
1265 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_data); ++i) {
1266 EXPECT_EQ(url_data[i].result,
1267 search_provider.IsSearchURL(GURL(url_data[i].url)));
1271 TEST_F(TemplateURLTest, ReflectsBookmarkBarPinned) {
1272 TemplateURLData data;
1273 data.input_encodings.push_back("UTF-8");
1274 data.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
1275 TemplateURL url(NULL, data);
1276 EXPECT_TRUE(url.url_ref().IsValid());
1277 ASSERT_TRUE(url.url_ref().SupportsReplacement());
1278 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1280 // Do not add the param when InstantExtended is suppressed on SRPs.
1281 url.url_ref_.showing_search_terms_ = false;
1282 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1283 EXPECT_EQ("http://www.google.com/?q=foo", result);
1285 // Add the param when InstantExtended is not suppressed on SRPs.
1286 url.url_ref_.showing_search_terms_ = true;
1287 search_terms_args.bookmark_bar_pinned = false;
1288 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1289 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result);
1291 url.url_ref_.showing_search_terms_ = true;
1292 search_terms_args.bookmark_bar_pinned = true;
1293 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1294 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result);