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"
22 #if defined(OS_ANDROID)
23 #include "chrome/browser/search_engines/search_terms_data_android.h"
26 using base::ASCIIToUTF16
;
28 // TestSearchTermsData --------------------------------------------------------
30 // Simple implementation of SearchTermsData.
31 class TestSearchTermsData
: public SearchTermsData
{
33 explicit TestSearchTermsData(const std::string
& google_base_url
);
35 virtual std::string
GoogleBaseURLValue() const OVERRIDE
;
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
{
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
) {
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
) {
78 data
.SetURL("{searchTerms}");
79 TemplateURL
url(NULL
, data
);
80 EXPECT_TRUE(url
.url_ref().IsValid());
83 TEST_F(TemplateURLTest
, URLRefTestSearchTerms
) {
84 struct SearchTermsCase
{
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
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())) {
237 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH
:
238 EXPECT_EQ("width", i
->first
);
240 base::IntToString(search_args
.image_original_size
.width()),
243 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL
:
244 EXPECT_EQ("image_content", i
->first
);
245 EXPECT_EQ(search_args
.image_thumbnail_content
, i
->second
);
247 case TemplateURLRef::GOOGLE_IMAGE_URL
:
248 EXPECT_EQ("image_url", i
->first
);
249 EXPECT_EQ(search_args
.image_url
.spec(), i
->second
);
251 case TemplateURLRef::LANGUAGE
:
252 EXPECT_EQ("language", i
->first
);
253 EXPECT_EQ("en", i
->second
);
256 ADD_FAILURE(); // Should never go here.
261 if (j
!= replacements
.end())
263 if (i
->first
== "empty_param") {
264 EXPECT_TRUE(i
->second
.empty());
265 } else if (i
->first
== "sbisrc") {
266 EXPECT_FALSE(i
->second
.empty());
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
;
282 EXPECT_EQ("http://foo{fhqwhgads}bar", url
.url_ref().ParseURL(
283 "http://foo{fhqwhgads}bar", &replacements
, NULL
, &valid
));
284 EXPECT_TRUE(replacements
.empty());
287 data
.prepopulate_id
= 123;
288 TemplateURL
url2(NULL
, data
);
289 EXPECT_EQ("http://foobar", url2
.url_ref().ParseURL("http://foo{fhqwhgads}bar",
292 EXPECT_TRUE(replacements
.empty());
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
{
323 const base::string16 terms
;
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
) {
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
) {
384 const std::string url
;
385 const base::string16 expected_result
;
387 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
388 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
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
) {
408 const std::string url
;
409 const std::string expected_result
;
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
) {
458 const std::string encoding
;
459 const base::string16 search_term
;
460 const std::string url
;
461 const std::string expected_result
;
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
) {
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
;
501 { ASCIIToUTF16("foo"),
504 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
506 // HTTPS available, AQS should be replaced.
507 { ASCIIToUTF16("foo"),
510 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
511 "https://foo/?fooaqs=chrome.0.0l6&" },
512 // HTTPS available, however AQS is empty.
513 { ASCIIToUTF16("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"),
522 "http://foo?{searchTerms}{google:assistedQueryStats}",
524 // A non-Google search provider with HTTPS should allow AQS.
525 { ASCIIToUTF16("foo"),
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
) {
550 const base::string16 search_term
;
551 size_t cursor_position
;
552 const std::string url
;
553 const std::string expected_result
;
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
) {
586 const base::string16 search_term
;
587 const std::string current_page_url
;
588 const std::string url
;
589 const std::string expected_result
;
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
) {
621 const int accepted_suggestion
;
622 const base::string16 original_query_for_suggestion
;
623 const std::string expected_result
;
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}"
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)
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();
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
) + "&";
681 EXPECT_EQ(expected_url
, result
.spec());
684 #if !defined(OS_ANDROID) && !defined(OS_IOS)
685 TEST_F(TemplateURLTest
, RLZFromAppList
) {
686 base::string16 rlz_string
;
687 #if defined(ENABLE_RLZ)
689 if (google_util::GetBrand(&brand
) && !brand
.empty() &&
690 !google_util::IsOrganic(brand
)) {
691 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_APP_LIST
, &rlz_string
);
695 TemplateURLData data
;
696 data
.SetURL("http://bar/?{google:RLZ}{searchTerms}");
697 TemplateURL
url(NULL
, data
);
698 EXPECT_TRUE(url
.url_ref().IsValid());
699 ASSERT_TRUE(url
.url_ref().SupportsReplacement());
700 TemplateURLRef::SearchTermsArgs
args(ASCIIToUTF16("x"));
701 args
.from_app_list
= true;
702 GURL
result(url
.url_ref().ReplaceSearchTerms(args
));
703 ASSERT_TRUE(result
.is_valid());
704 std::string expected_url
= "http://bar/?";
705 if (!rlz_string
.empty())
706 expected_url
+= "rlz=" + base::UTF16ToUTF8(rlz_string
) + "&";
708 EXPECT_EQ(expected_url
, result
.spec());
712 TEST_F(TemplateURLTest
, HostAndSearchTermKey
) {
714 const std::string url
;
715 const std::string host
;
716 const std::string path
;
717 const std::string search_term_key
;
719 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
721 // No query key should result in empty values.
722 { "http://blah/{searchTerms}", "", "", ""},
724 // No term should result in empty values.
725 { "http://blah/", "", "", ""},
727 // Multiple terms should result in empty values.
728 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
730 // Term in the host shouldn't match.
731 { "http://{searchTerms}", "", "", ""},
733 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
734 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
736 // Single term with extra chars in value should match.
737 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
740 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
741 TemplateURLData data
;
742 data
.SetURL(test_data
[i
].url
);
743 TemplateURL
url(NULL
, data
);
744 EXPECT_EQ(test_data
[i
].host
, url
.url_ref().GetHost());
745 EXPECT_EQ(test_data
[i
].path
, url
.url_ref().GetPath());
746 EXPECT_EQ(test_data
[i
].search_term_key
, url
.url_ref().GetSearchTermKey());
750 TEST_F(TemplateURLTest
, GoogleBaseSuggestURL
) {
751 static const struct {
752 const char* const base_url
;
753 const char* const base_suggest_url
;
755 { "http://google.com/", "http://google.com/complete/", },
756 { "http://www.google.com/", "http://www.google.com/complete/", },
757 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
758 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
759 { "http://google.com/intl/xx/", "http://google.com/complete/", },
762 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(data
); ++i
)
763 CheckSuggestBaseURL(data
[i
].base_url
, data
[i
].base_suggest_url
);
766 TEST_F(TemplateURLTest
, ParseParameterKnown
) {
767 std::string
parsed_url("{searchTerms}");
768 TemplateURLData data
;
769 data
.SetURL(parsed_url
);
770 TemplateURL
url(NULL
, data
);
771 TemplateURLRef::Replacements replacements
;
772 EXPECT_TRUE(url
.url_ref().ParseParameter(0, 12, &parsed_url
, &replacements
));
773 EXPECT_EQ(std::string(), parsed_url
);
774 ASSERT_EQ(1U, replacements
.size());
775 EXPECT_EQ(0U, replacements
[0].index
);
776 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS
, replacements
[0].type
);
779 TEST_F(TemplateURLTest
, ParseParameterUnknown
) {
780 std::string
parsed_url("{fhqwhgads}abc");
781 TemplateURLData data
;
782 data
.SetURL(parsed_url
);
783 TemplateURL
url(NULL
, data
);
784 TemplateURLRef::Replacements replacements
;
786 // By default, TemplateURLRef should not consider itself prepopulated.
787 // Therefore we should not replace the unknown parameter.
788 EXPECT_FALSE(url
.url_ref().ParseParameter(0, 10, &parsed_url
, &replacements
));
789 EXPECT_EQ("{fhqwhgads}abc", parsed_url
);
790 EXPECT_TRUE(replacements
.empty());
792 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
793 parsed_url
= "{fhqwhgads}abc";
794 data
.prepopulate_id
= 1;
795 TemplateURL
url2(NULL
, data
);
796 EXPECT_TRUE(url2
.url_ref().ParseParameter(0, 10, &parsed_url
, &replacements
));
797 EXPECT_EQ("abc", parsed_url
);
798 EXPECT_TRUE(replacements
.empty());
801 TEST_F(TemplateURLTest
, ParseURLEmpty
) {
802 TemplateURL
url(NULL
, TemplateURLData());
803 TemplateURLRef::Replacements replacements
;
805 EXPECT_EQ(std::string(),
806 url
.url_ref().ParseURL(std::string(), &replacements
, NULL
, &valid
));
807 EXPECT_TRUE(replacements
.empty());
811 TEST_F(TemplateURLTest
, ParseURLNoTemplateEnd
) {
812 TemplateURLData data
;
814 TemplateURL
url(NULL
, data
);
815 TemplateURLRef::Replacements replacements
;
817 EXPECT_EQ(std::string(), url
.url_ref().ParseURL("{", &replacements
, NULL
,
819 EXPECT_TRUE(replacements
.empty());
823 TEST_F(TemplateURLTest
, ParseURLNoKnownParameters
) {
824 TemplateURLData data
;
826 TemplateURL
url(NULL
, data
);
827 TemplateURLRef::Replacements replacements
;
829 EXPECT_EQ("{}", url
.url_ref().ParseURL("{}", &replacements
, NULL
, &valid
));
830 EXPECT_TRUE(replacements
.empty());
834 TEST_F(TemplateURLTest
, ParseURLTwoParameters
) {
835 TemplateURLData data
;
836 data
.SetURL("{}{{%s}}");
837 TemplateURL
url(NULL
, data
);
838 TemplateURLRef::Replacements replacements
;
841 url
.url_ref().ParseURL("{}{{searchTerms}}", &replacements
, NULL
,
843 ASSERT_EQ(1U, replacements
.size());
844 EXPECT_EQ(3U, replacements
[0].index
);
845 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS
, replacements
[0].type
);
849 TEST_F(TemplateURLTest
, ParseURLNestedParameter
) {
850 TemplateURLData data
;
852 TemplateURL
url(NULL
, data
);
853 TemplateURLRef::Replacements replacements
;
856 url
.url_ref().ParseURL("{{searchTerms}", &replacements
, NULL
,
858 ASSERT_EQ(1U, replacements
.size());
859 EXPECT_EQ(1U, replacements
[0].index
);
860 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS
, replacements
[0].type
);
864 #if defined(OS_ANDROID)
865 TEST_F(TemplateURLTest
, SearchClient
) {
866 const std::string
base_url_str("http://google.com/?");
867 const std::string
terms_str("{searchTerms}&{google:searchClient}");
868 const std::string full_url_str
= base_url_str
+ terms_str
;
869 const base::string16
terms(ASCIIToUTF16(terms_str
));
870 UIThreadSearchTermsData::SetGoogleBaseURL(base_url_str
);
872 TemplateURLData data
;
873 data
.SetURL(full_url_str
);
874 TemplateURL
url(NULL
, data
);
875 EXPECT_TRUE(url
.url_ref().IsValid());
876 ASSERT_TRUE(url
.url_ref().SupportsReplacement());
877 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foobar"));
879 // Check that the URL is correct when a client is not present.
880 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
));
881 ASSERT_TRUE(result
.is_valid());
882 EXPECT_EQ("http://google.com/?foobar&", result
.spec());
884 // Check that the URL is correct when a client is present.
885 SearchTermsDataAndroid::search_client_
.Get() = "android_test";
886 GURL
result_2(url
.url_ref().ReplaceSearchTerms(search_terms_args
));
887 ASSERT_TRUE(result_2
.is_valid());
888 EXPECT_EQ("http://google.com/?foobar&client=android_test&", result_2
.spec());
892 TEST_F(TemplateURLTest
, GetURLNoInstantURL
) {
893 TemplateURLData data
;
894 data
.SetURL("http://google.com/?q={searchTerms}");
895 data
.suggestions_url
= "http://google.com/suggest?q={searchTerms}";
896 data
.alternate_urls
.push_back("http://google.com/alt?q={searchTerms}");
897 data
.alternate_urls
.push_back("{google:baseURL}/alt/#q={searchTerms}");
898 TemplateURL
url(NULL
, data
);
899 ASSERT_EQ(3U, url
.URLCount());
900 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url
.GetURL(0));
901 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url
.GetURL(1));
902 EXPECT_EQ("http://google.com/?q={searchTerms}", url
.GetURL(2));
905 TEST_F(TemplateURLTest
, GetURLNoSuggestionsURL
) {
906 TemplateURLData data
;
907 data
.SetURL("http://google.com/?q={searchTerms}");
908 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
909 data
.alternate_urls
.push_back("http://google.com/alt?q={searchTerms}");
910 data
.alternate_urls
.push_back("{google:baseURL}/alt/#q={searchTerms}");
911 TemplateURL
url(NULL
, data
);
912 ASSERT_EQ(3U, url
.URLCount());
913 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url
.GetURL(0));
914 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url
.GetURL(1));
915 EXPECT_EQ("http://google.com/?q={searchTerms}", url
.GetURL(2));
918 TEST_F(TemplateURLTest
, GetURLOnlyOneURL
) {
919 TemplateURLData data
;
920 data
.SetURL("http://www.google.co.uk/");
921 TemplateURL
url(NULL
, data
);
922 ASSERT_EQ(1U, url
.URLCount());
923 EXPECT_EQ("http://www.google.co.uk/", url
.GetURL(0));
926 TEST_F(TemplateURLTest
, ExtractSearchTermsFromURL
) {
927 TemplateURLData data
;
928 data
.SetURL("http://google.com/?q={searchTerms}");
929 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
930 data
.alternate_urls
.push_back("http://google.com/alt/#q={searchTerms}");
931 data
.alternate_urls
.push_back(
932 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
933 TemplateURL
url(NULL
, data
);
934 base::string16 result
;
936 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
937 GURL("http://google.com/?q=something"), &result
));
938 EXPECT_EQ(ASCIIToUTF16("something"), result
);
940 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
941 GURL("http://google.com/?espv&q=something"), &result
));
942 EXPECT_EQ(ASCIIToUTF16("something"), result
);
944 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
945 GURL("http://google.com/?espv=1&q=something"), &result
));
946 EXPECT_EQ(ASCIIToUTF16("something"), result
);
948 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
949 GURL("http://google.com/?espv=0&q=something"), &result
));
950 EXPECT_EQ(ASCIIToUTF16("something"), result
);
952 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
953 GURL("http://google.com/alt/#q=something"), &result
));
954 EXPECT_EQ(ASCIIToUTF16("something"), result
);
956 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
957 GURL("http://google.com/alt/#espv&q=something"), &result
));
958 EXPECT_EQ(ASCIIToUTF16("something"), result
);
960 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
961 GURL("http://google.com/alt/#espv=1&q=something"), &result
));
962 EXPECT_EQ(ASCIIToUTF16("something"), result
);
964 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
965 GURL("http://google.com/alt/#espv=0&q=something"), &result
));
966 EXPECT_EQ(ASCIIToUTF16("something"), result
);
968 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
969 GURL("http://google.ca/?q=something"), &result
));
970 EXPECT_EQ(base::string16(), result
);
972 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
973 GURL("http://google.ca/?q=something&q=anything"), &result
));
974 EXPECT_EQ(base::string16(), result
);
976 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
977 GURL("http://google.com/foo/?q=foo"), &result
));
978 EXPECT_EQ(base::string16(), result
);
980 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
981 GURL("https://google.com/?q=foo"), &result
));
982 EXPECT_EQ(ASCIIToUTF16("foo"), result
);
984 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
985 GURL("http://google.com:8080/?q=foo"), &result
));
986 EXPECT_EQ(base::string16(), result
);
988 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
989 GURL("http://google.com/?q=1+2+3&b=456"), &result
));
990 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result
);
992 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
993 GURL("http://google.com/alt/?q=123#q=456"), &result
));
994 EXPECT_EQ(ASCIIToUTF16("456"), result
);
996 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
997 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), &result
));
998 EXPECT_EQ(ASCIIToUTF16("123"), result
);
1000 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(GURL(
1001 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), &result
));
1002 EXPECT_EQ(ASCIIToUTF16("789"), result
);
1004 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1005 GURL("http://google.com/alt/?q="), &result
));
1006 EXPECT_EQ(base::string16(), result
);
1008 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1009 GURL("http://google.com/alt/?#q="), &result
));
1010 EXPECT_EQ(base::string16(), result
);
1012 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1013 GURL("http://google.com/alt/?q=#q="), &result
));
1014 EXPECT_EQ(base::string16(), result
);
1016 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1017 GURL("http://google.com/alt/?q=123#q="), &result
));
1018 EXPECT_EQ(base::string16(), result
);
1020 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1021 GURL("http://google.com/alt/?q=#q=123"), &result
));
1022 EXPECT_EQ(ASCIIToUTF16("123"), result
);
1025 TEST_F(TemplateURLTest
, HasSearchTermsReplacementKey
) {
1026 TemplateURLData data
;
1027 data
.SetURL("http://google.com/?q={searchTerms}");
1028 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
1029 data
.alternate_urls
.push_back("http://google.com/alt/#q={searchTerms}");
1030 data
.alternate_urls
.push_back(
1031 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1032 data
.search_terms_replacement_key
= "espv";
1033 TemplateURL
url(NULL
, data
);
1035 // Test with instant enabled required.
1036 EXPECT_FALSE(url
.HasSearchTermsReplacementKey(
1037 GURL("http://google.com/")));
1039 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1040 GURL("http://google.com/?espv")));
1042 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1043 GURL("http://google.com/#espv")));
1045 EXPECT_FALSE(url
.HasSearchTermsReplacementKey(
1046 GURL("http://google.com/?q=something")));
1048 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1049 GURL("http://google.com/?q=something&espv")));
1051 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1052 GURL("http://google.com/?q=something&espv=1")));
1054 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1055 GURL("http://google.com/?q=something&espv=0")));
1057 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1058 GURL("http://google.com/?espv&q=something")));
1060 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1061 GURL("http://google.com/?espv=1&q=something")));
1063 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1064 GURL("http://google.com/?espv=0&q=something")));
1066 EXPECT_FALSE(url
.HasSearchTermsReplacementKey(
1067 GURL("http://google.com/alt/#q=something")));
1069 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1070 GURL("http://google.com/alt/#q=something&espv")));
1072 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1073 GURL("http://google.com/alt/#q=something&espv=1")));
1075 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1076 GURL("http://google.com/alt/#q=something&espv=0")));
1078 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1079 GURL("http://google.com/alt/#espv&q=something")));
1081 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1082 GURL("http://google.com/alt/#espv=1&q=something")));
1084 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1085 GURL("http://google.com/alt/#espv=0&q=something")));
1087 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1088 GURL("http://google.com/?espv#q=something")));
1090 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1091 GURL("http://google.com/?espv=1#q=something")));
1093 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1094 GURL("http://google.com/?q=something#espv")));
1096 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1097 GURL("http://google.com/?q=something#espv=1")));
1099 // This does not ensure the domain matches.
1100 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1101 GURL("http://bing.com/?espv")));
1103 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1104 GURL("http://bing.com/#espv")));
1107 TEST_F(TemplateURLTest
, ReplaceSearchTermsInURL
) {
1108 TemplateURLData data
;
1109 data
.SetURL("http://google.com/?q={searchTerms}");
1110 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
1111 data
.alternate_urls
.push_back("http://google.com/alt/#q={searchTerms}");
1112 data
.alternate_urls
.push_back(
1113 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1114 TemplateURL
url(NULL
, data
);
1115 TemplateURLRef::SearchTermsArgs
search_terms(ASCIIToUTF16("Bob Morane"));
1118 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1119 GURL("http://google.com/?q=something"), search_terms
, &result
));
1120 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result
);
1122 result
= GURL("http://should.not.change.com");
1123 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1124 GURL("http://google.ca/?q=something"), search_terms
, &result
));
1125 EXPECT_EQ(GURL("http://should.not.change.com"), result
);
1127 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1128 GURL("http://google.com/foo/?q=foo"), search_terms
, &result
));
1130 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1131 GURL("https://google.com/?q=foo"), search_terms
, &result
));
1132 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result
);
1134 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1135 GURL("http://google.com:8080/?q=foo"), search_terms
, &result
));
1137 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1138 GURL("http://google.com/?q=1+2+3&b=456"), search_terms
, &result
));
1139 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result
);
1141 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1142 // template_url.cc for details.
1143 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1144 GURL("http://google.com/alt/?q=123#q=456"), search_terms
, &result
));
1145 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result
);
1147 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1148 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms
,
1150 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"),
1153 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1154 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1155 search_terms
, &result
));
1156 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1157 "#j=abc&q=Bob Morane&h=def9"), result
);
1159 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1160 GURL("http://google.com/alt/?q="), search_terms
, &result
));
1162 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1163 GURL("http://google.com/alt/?#q="), search_terms
, &result
));
1165 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1166 GURL("http://google.com/alt/?q=#q="), search_terms
, &result
));
1168 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1169 GURL("http://google.com/alt/?q=123#q="), search_terms
, &result
));
1171 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1172 GURL("http://google.com/alt/?q=#q=123"), search_terms
, &result
));
1173 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result
);
1176 // Test the |suggest_query_params| field of SearchTermsArgs.
1177 TEST_F(TemplateURLTest
, SuggestQueryParams
) {
1178 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1179 TemplateURLData data
;
1180 // Pick a URL with replacements before, during, and after the query, to ensure
1181 // we don't goof up any of them.
1182 data
.SetURL("{google:baseURL}search?q={searchTerms}"
1183 "#{google:originalQueryForSuggestion}x");
1184 TemplateURL
url(NULL
, data
);
1186 // Baseline: no |suggest_query_params| field.
1187 TemplateURLRef::SearchTermsArgs
search_terms(ASCIIToUTF16("abc"));
1188 search_terms
.original_query
= ASCIIToUTF16("def");
1189 search_terms
.accepted_suggestion
= 0;
1190 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1191 url
.url_ref().ReplaceSearchTerms(search_terms
));
1193 // Set the suggest_query_params.
1194 search_terms
.suggest_query_params
= "pq=xyz";
1195 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
1196 url
.url_ref().ReplaceSearchTerms(search_terms
));
1198 // Add extra_query_params in the mix, and ensure it works.
1199 search_terms
.append_extra_query_params
= true;
1200 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1201 switches::kExtraSearchQueryParams
, "a=b");
1202 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
1203 url
.url_ref().ReplaceSearchTerms(search_terms
));
1206 // Test the |append_extra_query_params| field of SearchTermsArgs.
1207 TEST_F(TemplateURLTest
, ExtraQueryParams
) {
1208 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1209 TemplateURLData data
;
1210 // Pick a URL with replacements before, during, and after the query, to ensure
1211 // we don't goof up any of them.
1212 data
.SetURL("{google:baseURL}search?q={searchTerms}"
1213 "#{google:originalQueryForSuggestion}x");
1214 TemplateURL
url(NULL
, data
);
1216 // Baseline: no command-line args, no |append_extra_query_params| flag.
1217 TemplateURLRef::SearchTermsArgs
search_terms(ASCIIToUTF16("abc"));
1218 search_terms
.original_query
= ASCIIToUTF16("def");
1219 search_terms
.accepted_suggestion
= 0;
1220 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1221 url
.url_ref().ReplaceSearchTerms(search_terms
));
1223 // Set the flag. Since there are no command-line args, this should have no
1225 search_terms
.append_extra_query_params
= true;
1226 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1227 url
.url_ref().ReplaceSearchTerms(search_terms
));
1229 // Now append the command-line arg. This should be inserted into the query.
1230 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1231 switches::kExtraSearchQueryParams
, "a=b");
1232 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
1233 url
.url_ref().ReplaceSearchTerms(search_terms
));
1235 // Turn off the flag. Now the command-line arg should be ignored again.
1236 search_terms
.append_extra_query_params
= false;
1237 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1238 url
.url_ref().ReplaceSearchTerms(search_terms
));
1241 // Tests replacing pageClassification.
1242 TEST_F(TemplateURLTest
, ReplacePageClassification
) {
1243 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1244 TemplateURLData data
;
1245 data
.input_encodings
.push_back("UTF-8");
1246 data
.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
1247 TemplateURL
url(NULL
, data
);
1248 EXPECT_TRUE(url
.url_ref().IsValid());
1249 ASSERT_TRUE(url
.url_ref().SupportsReplacement());
1250 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1252 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
);
1253 EXPECT_EQ("http://www.google.com/?q=foo", result
);
1255 search_terms_args
.page_classification
= AutocompleteInput::NTP
;
1256 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
);
1257 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result
);
1259 search_terms_args
.page_classification
=
1260 AutocompleteInput::HOME_PAGE
;
1261 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
);
1262 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result
);
1265 // Test the IsSearchResults function.
1266 TEST_F(TemplateURLTest
, IsSearchResults
) {
1267 TemplateURLData data
;
1268 data
.SetURL("http://bar/search?q={searchTerms}");
1269 data
.instant_url
= "http://bar/instant#q={searchTerms}";
1270 data
.new_tab_url
= "http://bar/newtab";
1271 data
.alternate_urls
.push_back("http://bar/?q={searchTerms}");
1272 data
.alternate_urls
.push_back("http://bar/#q={searchTerms}");
1273 data
.alternate_urls
.push_back("http://bar/search#q{searchTerms}");
1274 data
.alternate_urls
.push_back("http://bar/webhp#q={searchTerms}");
1275 TemplateURL
search_provider(NULL
, data
);
1278 const char* const url
;
1281 { "http://bar/search?q=foo&oq=foo", true, },
1282 { "http://bar/?q=foo&oq=foo", true, },
1283 { "http://bar/#output=search&q=foo&oq=foo", true, },
1284 { "http://bar/webhp#q=foo&oq=foo", true, },
1285 { "http://bar/#q=foo&oq=foo", true, },
1286 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1287 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1288 { "http://bar/", false, },
1289 { "http://foo/", false, },
1290 { "http://bar/newtab", false, },
1293 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(url_data
); ++i
) {
1294 EXPECT_EQ(url_data
[i
].result
,
1295 search_provider
.IsSearchURL(GURL(url_data
[i
].url
)));
1299 TEST_F(TemplateURLTest
, ReflectsBookmarkBarPinned
) {
1300 TemplateURLData data
;
1301 data
.input_encodings
.push_back("UTF-8");
1302 data
.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
1303 TemplateURL
url(NULL
, data
);
1304 EXPECT_TRUE(url
.url_ref().IsValid());
1305 ASSERT_TRUE(url
.url_ref().SupportsReplacement());
1306 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1308 // Do not add the param when InstantExtended is suppressed on SRPs.
1309 url
.url_ref_
.showing_search_terms_
= false;
1310 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
);
1311 EXPECT_EQ("http://www.google.com/?q=foo", result
);
1313 // Add the param when InstantExtended is not suppressed on SRPs.
1314 url
.url_ref_
.showing_search_terms_
= true;
1315 search_terms_args
.bookmark_bar_pinned
= false;
1316 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
);
1317 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result
);
1319 url
.url_ref_
.showing_search_terms_
= true;
1320 search_terms_args
.bookmark_bar_pinned
= true;
1321 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
);
1322 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result
);