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 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
569 TemplateURLData data
;
570 data
.input_encodings
.push_back("UTF-8");
571 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
572 data
.SetURL(test_data
[i
].url
);
573 TemplateURL
url(NULL
, data
);
574 EXPECT_TRUE(url
.url_ref().IsValid());
575 ASSERT_TRUE(url
.url_ref().SupportsReplacement());
576 TemplateURLRef::SearchTermsArgs
search_terms_args(test_data
[i
].search_term
);
577 search_terms_args
.cursor_position
= test_data
[i
].cursor_position
;
578 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
));
579 ASSERT_TRUE(result
.is_valid());
580 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
584 // Tests replacing currentPageUrl.
585 TEST_F(TemplateURLTest
, ReplaceCurrentPageUrl
) {
587 const base::string16 search_term
;
588 const std::string current_page_url
;
589 const std::string url
;
590 const std::string expected_result
;
592 { ASCIIToUTF16("foo"),
593 "http://www.google.com/",
594 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
595 "http://www.google.com/?foo&url=http%3A%2F%2Fwww.google.com%2F&" },
596 { ASCIIToUTF16("foo"),
598 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
599 "http://www.google.com/?foo&" },
600 { ASCIIToUTF16("foo"),
601 "http://g.com/+-/*&=",
602 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
603 "http://www.google.com/?foo&url=http%3A%2F%2Fg.com%2F%2B-%2F*%26%3D&" },
605 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
606 TemplateURLData data
;
607 data
.input_encodings
.push_back("UTF-8");
608 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
609 data
.SetURL(test_data
[i
].url
);
610 TemplateURL
url(NULL
, data
);
611 EXPECT_TRUE(url
.url_ref().IsValid());
612 ASSERT_TRUE(url
.url_ref().SupportsReplacement());
613 TemplateURLRef::SearchTermsArgs
search_terms_args(test_data
[i
].search_term
);
614 search_terms_args
.current_page_url
= test_data
[i
].current_page_url
;
615 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
));
616 ASSERT_TRUE(result
.is_valid());
617 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
621 TEST_F(TemplateURLTest
, Suggestions
) {
623 const int accepted_suggestion
;
624 const base::string16 original_query_for_suggestion
;
625 const std::string expected_result
;
627 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE
, base::string16(),
628 "http://bar/foo?q=foobar" },
629 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE
, ASCIIToUTF16("foo"),
630 "http://bar/foo?q=foobar" },
631 { TemplateURLRef::NO_SUGGESTION_CHOSEN
, base::string16(),
632 "http://bar/foo?q=foobar" },
633 { TemplateURLRef::NO_SUGGESTION_CHOSEN
, ASCIIToUTF16("foo"),
634 "http://bar/foo?q=foobar" },
635 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
636 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
638 TemplateURLData data
;
639 data
.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
641 data
.input_encodings
.push_back("UTF-8");
642 TemplateURL
url(NULL
, data
);
643 EXPECT_TRUE(url
.url_ref().IsValid());
644 ASSERT_TRUE(url
.url_ref().SupportsReplacement());
645 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
646 TemplateURLRef::SearchTermsArgs
search_terms_args(
647 ASCIIToUTF16("foobar"));
648 search_terms_args
.accepted_suggestion
= test_data
[i
].accepted_suggestion
;
649 search_terms_args
.original_query
=
650 test_data
[i
].original_query_for_suggestion
;
651 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
));
652 ASSERT_TRUE(result
.is_valid());
653 EXPECT_EQ(test_data
[i
].expected_result
, result
.spec());
657 TEST_F(TemplateURLTest
, RLZ
) {
658 base::string16 rlz_string
;
659 #if defined(ENABLE_RLZ)
661 if (google_util::GetBrand(&brand
) && !brand
.empty() &&
662 !google_util::IsOrganic(brand
)) {
663 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, &rlz_string
);
665 #elif defined(OS_ANDROID)
666 SearchTermsDataAndroid::rlz_parameter_value_
.Get() =
667 ASCIIToUTF16("android_test");
668 rlz_string
= SearchTermsDataAndroid::rlz_parameter_value_
.Get();
671 TemplateURLData data
;
672 data
.SetURL("http://bar/?{google:RLZ}{searchTerms}");
673 TemplateURL
url(NULL
, data
);
674 EXPECT_TRUE(url
.url_ref().IsValid());
675 ASSERT_TRUE(url
.url_ref().SupportsReplacement());
676 GURL
result(url
.url_ref().ReplaceSearchTerms(
677 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x"))));
678 ASSERT_TRUE(result
.is_valid());
679 std::string expected_url
= "http://bar/?";
680 if (!rlz_string
.empty())
681 expected_url
+= "rlz=" + base::UTF16ToUTF8(rlz_string
) + "&";
683 EXPECT_EQ(expected_url
, result
.spec());
686 #if !defined(OS_ANDROID) && !defined(OS_IOS)
687 TEST_F(TemplateURLTest
, RLZFromAppList
) {
688 base::string16 rlz_string
;
689 #if defined(ENABLE_RLZ)
691 if (google_util::GetBrand(&brand
) && !brand
.empty() &&
692 !google_util::IsOrganic(brand
)) {
693 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_APP_LIST
, &rlz_string
);
697 TemplateURLData data
;
698 data
.SetURL("http://bar/?{google:RLZ}{searchTerms}");
699 TemplateURL
url(NULL
, data
);
700 EXPECT_TRUE(url
.url_ref().IsValid());
701 ASSERT_TRUE(url
.url_ref().SupportsReplacement());
702 TemplateURLRef::SearchTermsArgs
args(ASCIIToUTF16("x"));
703 args
.from_app_list
= true;
704 GURL
result(url
.url_ref().ReplaceSearchTerms(args
));
705 ASSERT_TRUE(result
.is_valid());
706 std::string expected_url
= "http://bar/?";
707 if (!rlz_string
.empty())
708 expected_url
+= "rlz=" + base::UTF16ToUTF8(rlz_string
) + "&";
710 EXPECT_EQ(expected_url
, result
.spec());
714 TEST_F(TemplateURLTest
, HostAndSearchTermKey
) {
716 const std::string url
;
717 const std::string host
;
718 const std::string path
;
719 const std::string search_term_key
;
721 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
723 // No query key should result in empty values.
724 { "http://blah/{searchTerms}", "", "", ""},
726 // No term should result in empty values.
727 { "http://blah/", "", "", ""},
729 // Multiple terms should result in empty values.
730 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
732 // Term in the host shouldn't match.
733 { "http://{searchTerms}", "", "", ""},
735 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
736 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
738 // Single term with extra chars in value should match.
739 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
742 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); ++i
) {
743 TemplateURLData data
;
744 data
.SetURL(test_data
[i
].url
);
745 TemplateURL
url(NULL
, data
);
746 EXPECT_EQ(test_data
[i
].host
, url
.url_ref().GetHost());
747 EXPECT_EQ(test_data
[i
].path
, url
.url_ref().GetPath());
748 EXPECT_EQ(test_data
[i
].search_term_key
, url
.url_ref().GetSearchTermKey());
752 TEST_F(TemplateURLTest
, GoogleBaseSuggestURL
) {
753 static const struct {
754 const char* const base_url
;
755 const char* const base_suggest_url
;
757 { "http://google.com/", "http://google.com/complete/", },
758 { "http://www.google.com/", "http://www.google.com/complete/", },
759 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
760 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
761 { "http://google.com/intl/xx/", "http://google.com/complete/", },
764 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(data
); ++i
)
765 CheckSuggestBaseURL(data
[i
].base_url
, data
[i
].base_suggest_url
);
768 TEST_F(TemplateURLTest
, ParseParameterKnown
) {
769 std::string
parsed_url("{searchTerms}");
770 TemplateURLData data
;
771 data
.SetURL(parsed_url
);
772 TemplateURL
url(NULL
, data
);
773 TemplateURLRef::Replacements replacements
;
774 EXPECT_TRUE(url
.url_ref().ParseParameter(0, 12, &parsed_url
, &replacements
));
775 EXPECT_EQ(std::string(), parsed_url
);
776 ASSERT_EQ(1U, replacements
.size());
777 EXPECT_EQ(0U, replacements
[0].index
);
778 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS
, replacements
[0].type
);
781 TEST_F(TemplateURLTest
, ParseParameterUnknown
) {
782 std::string
parsed_url("{fhqwhgads}abc");
783 TemplateURLData data
;
784 data
.SetURL(parsed_url
);
785 TemplateURL
url(NULL
, data
);
786 TemplateURLRef::Replacements replacements
;
788 // By default, TemplateURLRef should not consider itself prepopulated.
789 // Therefore we should not replace the unknown parameter.
790 EXPECT_FALSE(url
.url_ref().ParseParameter(0, 10, &parsed_url
, &replacements
));
791 EXPECT_EQ("{fhqwhgads}abc", parsed_url
);
792 EXPECT_TRUE(replacements
.empty());
794 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
795 parsed_url
= "{fhqwhgads}abc";
796 data
.prepopulate_id
= 1;
797 TemplateURL
url2(NULL
, data
);
798 EXPECT_TRUE(url2
.url_ref().ParseParameter(0, 10, &parsed_url
, &replacements
));
799 EXPECT_EQ("abc", parsed_url
);
800 EXPECT_TRUE(replacements
.empty());
803 TEST_F(TemplateURLTest
, ParseURLEmpty
) {
804 TemplateURL
url(NULL
, TemplateURLData());
805 TemplateURLRef::Replacements replacements
;
807 EXPECT_EQ(std::string(),
808 url
.url_ref().ParseURL(std::string(), &replacements
, NULL
, &valid
));
809 EXPECT_TRUE(replacements
.empty());
813 TEST_F(TemplateURLTest
, ParseURLNoTemplateEnd
) {
814 TemplateURLData data
;
816 TemplateURL
url(NULL
, data
);
817 TemplateURLRef::Replacements replacements
;
819 EXPECT_EQ(std::string(), url
.url_ref().ParseURL("{", &replacements
, NULL
,
821 EXPECT_TRUE(replacements
.empty());
825 TEST_F(TemplateURLTest
, ParseURLNoKnownParameters
) {
826 TemplateURLData data
;
828 TemplateURL
url(NULL
, data
);
829 TemplateURLRef::Replacements replacements
;
831 EXPECT_EQ("{}", url
.url_ref().ParseURL("{}", &replacements
, NULL
, &valid
));
832 EXPECT_TRUE(replacements
.empty());
836 TEST_F(TemplateURLTest
, ParseURLTwoParameters
) {
837 TemplateURLData data
;
838 data
.SetURL("{}{{%s}}");
839 TemplateURL
url(NULL
, data
);
840 TemplateURLRef::Replacements replacements
;
843 url
.url_ref().ParseURL("{}{{searchTerms}}", &replacements
, NULL
,
845 ASSERT_EQ(1U, replacements
.size());
846 EXPECT_EQ(3U, replacements
[0].index
);
847 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS
, replacements
[0].type
);
851 TEST_F(TemplateURLTest
, ParseURLNestedParameter
) {
852 TemplateURLData data
;
854 TemplateURL
url(NULL
, data
);
855 TemplateURLRef::Replacements replacements
;
858 url
.url_ref().ParseURL("{{searchTerms}", &replacements
, NULL
,
860 ASSERT_EQ(1U, replacements
.size());
861 EXPECT_EQ(1U, replacements
[0].index
);
862 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS
, replacements
[0].type
);
866 #if defined(OS_ANDROID)
867 TEST_F(TemplateURLTest
, SearchClient
) {
868 const std::string
base_url_str("http://google.com/?");
869 const std::string
terms_str("{searchTerms}&{google:searchClient}");
870 const std::string full_url_str
= base_url_str
+ terms_str
;
871 const base::string16
terms(ASCIIToUTF16(terms_str
));
872 UIThreadSearchTermsData::SetGoogleBaseURL(base_url_str
);
874 TemplateURLData data
;
875 data
.SetURL(full_url_str
);
876 TemplateURL
url(NULL
, data
);
877 EXPECT_TRUE(url
.url_ref().IsValid());
878 ASSERT_TRUE(url
.url_ref().SupportsReplacement());
879 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foobar"));
881 // Check that the URL is correct when a client is not present.
882 GURL
result(url
.url_ref().ReplaceSearchTerms(search_terms_args
));
883 ASSERT_TRUE(result
.is_valid());
884 EXPECT_EQ("http://google.com/?foobar&", result
.spec());
886 // Check that the URL is correct when a client is present.
887 SearchTermsDataAndroid::search_client_
.Get() = "android_test";
888 GURL
result_2(url
.url_ref().ReplaceSearchTerms(search_terms_args
));
889 ASSERT_TRUE(result_2
.is_valid());
890 EXPECT_EQ("http://google.com/?foobar&client=android_test&", result_2
.spec());
894 TEST_F(TemplateURLTest
, GetURLNoInstantURL
) {
895 TemplateURLData data
;
896 data
.SetURL("http://google.com/?q={searchTerms}");
897 data
.suggestions_url
= "http://google.com/suggest?q={searchTerms}";
898 data
.alternate_urls
.push_back("http://google.com/alt?q={searchTerms}");
899 data
.alternate_urls
.push_back("{google:baseURL}/alt/#q={searchTerms}");
900 TemplateURL
url(NULL
, data
);
901 ASSERT_EQ(3U, url
.URLCount());
902 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url
.GetURL(0));
903 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url
.GetURL(1));
904 EXPECT_EQ("http://google.com/?q={searchTerms}", url
.GetURL(2));
907 TEST_F(TemplateURLTest
, GetURLNoSuggestionsURL
) {
908 TemplateURLData data
;
909 data
.SetURL("http://google.com/?q={searchTerms}");
910 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
911 data
.alternate_urls
.push_back("http://google.com/alt?q={searchTerms}");
912 data
.alternate_urls
.push_back("{google:baseURL}/alt/#q={searchTerms}");
913 TemplateURL
url(NULL
, data
);
914 ASSERT_EQ(3U, url
.URLCount());
915 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url
.GetURL(0));
916 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url
.GetURL(1));
917 EXPECT_EQ("http://google.com/?q={searchTerms}", url
.GetURL(2));
920 TEST_F(TemplateURLTest
, GetURLOnlyOneURL
) {
921 TemplateURLData data
;
922 data
.SetURL("http://www.google.co.uk/");
923 TemplateURL
url(NULL
, data
);
924 ASSERT_EQ(1U, url
.URLCount());
925 EXPECT_EQ("http://www.google.co.uk/", url
.GetURL(0));
928 TEST_F(TemplateURLTest
, ExtractSearchTermsFromURL
) {
929 TemplateURLData data
;
930 data
.SetURL("http://google.com/?q={searchTerms}");
931 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
932 data
.alternate_urls
.push_back("http://google.com/alt/#q={searchTerms}");
933 data
.alternate_urls
.push_back(
934 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
935 TemplateURL
url(NULL
, data
);
936 base::string16 result
;
938 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
939 GURL("http://google.com/?q=something"), &result
));
940 EXPECT_EQ(ASCIIToUTF16("something"), result
);
942 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
943 GURL("http://google.com/?espv&q=something"), &result
));
944 EXPECT_EQ(ASCIIToUTF16("something"), result
);
946 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
947 GURL("http://google.com/?espv=1&q=something"), &result
));
948 EXPECT_EQ(ASCIIToUTF16("something"), result
);
950 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
951 GURL("http://google.com/?espv=0&q=something"), &result
));
952 EXPECT_EQ(ASCIIToUTF16("something"), result
);
954 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
955 GURL("http://google.com/alt/#q=something"), &result
));
956 EXPECT_EQ(ASCIIToUTF16("something"), result
);
958 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
959 GURL("http://google.com/alt/#espv&q=something"), &result
));
960 EXPECT_EQ(ASCIIToUTF16("something"), result
);
962 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
963 GURL("http://google.com/alt/#espv=1&q=something"), &result
));
964 EXPECT_EQ(ASCIIToUTF16("something"), result
);
966 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
967 GURL("http://google.com/alt/#espv=0&q=something"), &result
));
968 EXPECT_EQ(ASCIIToUTF16("something"), result
);
970 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
971 GURL("http://google.ca/?q=something"), &result
));
972 EXPECT_EQ(base::string16(), result
);
974 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
975 GURL("http://google.ca/?q=something&q=anything"), &result
));
976 EXPECT_EQ(base::string16(), result
);
978 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
979 GURL("http://google.com/foo/?q=foo"), &result
));
980 EXPECT_EQ(base::string16(), result
);
982 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
983 GURL("https://google.com/?q=foo"), &result
));
984 EXPECT_EQ(ASCIIToUTF16("foo"), result
);
986 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
987 GURL("http://google.com:8080/?q=foo"), &result
));
988 EXPECT_EQ(base::string16(), result
);
990 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
991 GURL("http://google.com/?q=1+2+3&b=456"), &result
));
992 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result
);
994 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
995 GURL("http://google.com/alt/?q=123#q=456"), &result
));
996 EXPECT_EQ(ASCIIToUTF16("456"), result
);
998 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
999 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), &result
));
1000 EXPECT_EQ(ASCIIToUTF16("123"), result
);
1002 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(GURL(
1003 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), &result
));
1004 EXPECT_EQ(ASCIIToUTF16("789"), result
);
1006 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1007 GURL("http://google.com/alt/?q="), &result
));
1008 EXPECT_EQ(base::string16(), result
);
1010 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1011 GURL("http://google.com/alt/?#q="), &result
));
1012 EXPECT_EQ(base::string16(), result
);
1014 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1015 GURL("http://google.com/alt/?q=#q="), &result
));
1016 EXPECT_EQ(base::string16(), result
);
1018 EXPECT_FALSE(url
.ExtractSearchTermsFromURL(
1019 GURL("http://google.com/alt/?q=123#q="), &result
));
1020 EXPECT_EQ(base::string16(), result
);
1022 EXPECT_TRUE(url
.ExtractSearchTermsFromURL(
1023 GURL("http://google.com/alt/?q=#q=123"), &result
));
1024 EXPECT_EQ(ASCIIToUTF16("123"), result
);
1027 TEST_F(TemplateURLTest
, HasSearchTermsReplacementKey
) {
1028 TemplateURLData data
;
1029 data
.SetURL("http://google.com/?q={searchTerms}");
1030 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
1031 data
.alternate_urls
.push_back("http://google.com/alt/#q={searchTerms}");
1032 data
.alternate_urls
.push_back(
1033 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1034 data
.search_terms_replacement_key
= "espv";
1035 TemplateURL
url(NULL
, data
);
1037 // Test with instant enabled required.
1038 EXPECT_FALSE(url
.HasSearchTermsReplacementKey(
1039 GURL("http://google.com/")));
1041 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1042 GURL("http://google.com/?espv")));
1044 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1045 GURL("http://google.com/#espv")));
1047 EXPECT_FALSE(url
.HasSearchTermsReplacementKey(
1048 GURL("http://google.com/?q=something")));
1050 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1051 GURL("http://google.com/?q=something&espv")));
1053 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1054 GURL("http://google.com/?q=something&espv=1")));
1056 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1057 GURL("http://google.com/?q=something&espv=0")));
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/?espv=0&q=something")));
1068 EXPECT_FALSE(url
.HasSearchTermsReplacementKey(
1069 GURL("http://google.com/alt/#q=something")));
1071 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1072 GURL("http://google.com/alt/#q=something&espv")));
1074 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1075 GURL("http://google.com/alt/#q=something&espv=1")));
1077 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1078 GURL("http://google.com/alt/#q=something&espv=0")));
1080 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1081 GURL("http://google.com/alt/#espv&q=something")));
1083 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1084 GURL("http://google.com/alt/#espv=1&q=something")));
1086 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1087 GURL("http://google.com/alt/#espv=0&q=something")));
1089 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1090 GURL("http://google.com/?espv#q=something")));
1092 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1093 GURL("http://google.com/?espv=1#q=something")));
1095 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1096 GURL("http://google.com/?q=something#espv")));
1098 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1099 GURL("http://google.com/?q=something#espv=1")));
1101 // This does not ensure the domain matches.
1102 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1103 GURL("http://bing.com/?espv")));
1105 EXPECT_TRUE(url
.HasSearchTermsReplacementKey(
1106 GURL("http://bing.com/#espv")));
1109 TEST_F(TemplateURLTest
, ReplaceSearchTermsInURL
) {
1110 TemplateURLData data
;
1111 data
.SetURL("http://google.com/?q={searchTerms}");
1112 data
.instant_url
= "http://google.com/instant#q={searchTerms}";
1113 data
.alternate_urls
.push_back("http://google.com/alt/#q={searchTerms}");
1114 data
.alternate_urls
.push_back(
1115 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1116 TemplateURL
url(NULL
, data
);
1117 TemplateURLRef::SearchTermsArgs
search_terms(ASCIIToUTF16("Bob Morane"));
1120 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1121 GURL("http://google.com/?q=something"), search_terms
, &result
));
1122 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result
);
1124 result
= GURL("http://should.not.change.com");
1125 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1126 GURL("http://google.ca/?q=something"), search_terms
, &result
));
1127 EXPECT_EQ(GURL("http://should.not.change.com"), result
);
1129 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1130 GURL("http://google.com/foo/?q=foo"), search_terms
, &result
));
1132 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1133 GURL("https://google.com/?q=foo"), search_terms
, &result
));
1134 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result
);
1136 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1137 GURL("http://google.com:8080/?q=foo"), search_terms
, &result
));
1139 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1140 GURL("http://google.com/?q=1+2+3&b=456"), search_terms
, &result
));
1141 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result
);
1143 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1144 // template_url.cc for details.
1145 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1146 GURL("http://google.com/alt/?q=123#q=456"), search_terms
, &result
));
1147 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result
);
1149 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1150 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms
,
1152 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"),
1155 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1156 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1157 search_terms
, &result
));
1158 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1159 "#j=abc&q=Bob Morane&h=def9"), result
);
1161 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1162 GURL("http://google.com/alt/?q="), search_terms
, &result
));
1164 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1165 GURL("http://google.com/alt/?#q="), search_terms
, &result
));
1167 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1168 GURL("http://google.com/alt/?q=#q="), search_terms
, &result
));
1170 EXPECT_FALSE(url
.ReplaceSearchTermsInURL(
1171 GURL("http://google.com/alt/?q=123#q="), search_terms
, &result
));
1173 EXPECT_TRUE(url
.ReplaceSearchTermsInURL(
1174 GURL("http://google.com/alt/?q=#q=123"), search_terms
, &result
));
1175 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result
);
1178 // Test the |suggest_query_params| field of SearchTermsArgs.
1179 TEST_F(TemplateURLTest
, SuggestQueryParams
) {
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 |suggest_query_params| field.
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 suggest_query_params.
1196 search_terms
.suggest_query_params
= "pq=xyz";
1197 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
1198 url
.url_ref().ReplaceSearchTerms(search_terms
));
1200 // Add extra_query_params in the mix, and ensure it works.
1201 search_terms
.append_extra_query_params
= true;
1202 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1203 switches::kExtraSearchQueryParams
, "a=b");
1204 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
1205 url
.url_ref().ReplaceSearchTerms(search_terms
));
1208 // Test the |append_extra_query_params| field of SearchTermsArgs.
1209 TEST_F(TemplateURLTest
, ExtraQueryParams
) {
1210 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1211 TemplateURLData data
;
1212 // Pick a URL with replacements before, during, and after the query, to ensure
1213 // we don't goof up any of them.
1214 data
.SetURL("{google:baseURL}search?q={searchTerms}"
1215 "#{google:originalQueryForSuggestion}x");
1216 TemplateURL
url(NULL
, data
);
1218 // Baseline: no command-line args, no |append_extra_query_params| flag.
1219 TemplateURLRef::SearchTermsArgs
search_terms(ASCIIToUTF16("abc"));
1220 search_terms
.original_query
= ASCIIToUTF16("def");
1221 search_terms
.accepted_suggestion
= 0;
1222 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1223 url
.url_ref().ReplaceSearchTerms(search_terms
));
1225 // Set the flag. Since there are no command-line args, this should have no
1227 search_terms
.append_extra_query_params
= true;
1228 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1229 url
.url_ref().ReplaceSearchTerms(search_terms
));
1231 // Now append the command-line arg. This should be inserted into the query.
1232 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1233 switches::kExtraSearchQueryParams
, "a=b");
1234 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
1235 url
.url_ref().ReplaceSearchTerms(search_terms
));
1237 // Turn off the flag. Now the command-line arg should be ignored again.
1238 search_terms
.append_extra_query_params
= false;
1239 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1240 url
.url_ref().ReplaceSearchTerms(search_terms
));
1243 // Tests replacing pageClassification.
1244 TEST_F(TemplateURLTest
, ReplacePageClassification
) {
1245 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1246 TemplateURLData data
;
1247 data
.input_encodings
.push_back("UTF-8");
1248 data
.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
1249 TemplateURL
url(NULL
, data
);
1250 EXPECT_TRUE(url
.url_ref().IsValid());
1251 ASSERT_TRUE(url
.url_ref().SupportsReplacement());
1252 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1254 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
);
1255 EXPECT_EQ("http://www.google.com/?q=foo", result
);
1257 search_terms_args
.page_classification
= AutocompleteInput::NTP
;
1258 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
);
1259 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result
);
1261 search_terms_args
.page_classification
=
1262 AutocompleteInput::HOME_PAGE
;
1263 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
);
1264 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result
);
1267 // Test the IsSearchResults function.
1268 TEST_F(TemplateURLTest
, IsSearchResults
) {
1269 TemplateURLData data
;
1270 data
.SetURL("http://bar/search?q={searchTerms}");
1271 data
.instant_url
= "http://bar/instant#q={searchTerms}";
1272 data
.new_tab_url
= "http://bar/newtab";
1273 data
.alternate_urls
.push_back("http://bar/?q={searchTerms}");
1274 data
.alternate_urls
.push_back("http://bar/#q={searchTerms}");
1275 data
.alternate_urls
.push_back("http://bar/search#q{searchTerms}");
1276 data
.alternate_urls
.push_back("http://bar/webhp#q={searchTerms}");
1277 TemplateURL
search_provider(NULL
, data
);
1280 const char* const url
;
1283 { "http://bar/search?q=foo&oq=foo", true, },
1284 { "http://bar/?q=foo&oq=foo", true, },
1285 { "http://bar/#output=search&q=foo&oq=foo", true, },
1286 { "http://bar/webhp#q=foo&oq=foo", true, },
1287 { "http://bar/#q=foo&oq=foo", true, },
1288 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1289 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1290 { "http://bar/", false, },
1291 { "http://foo/", false, },
1292 { "http://bar/newtab", false, },
1295 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(url_data
); ++i
) {
1296 EXPECT_EQ(url_data
[i
].result
,
1297 search_provider
.IsSearchURL(GURL(url_data
[i
].url
)));
1301 TEST_F(TemplateURLTest
, ReflectsBookmarkBarPinned
) {
1302 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1303 TemplateURLData data
;
1304 data
.input_encodings
.push_back("UTF-8");
1305 data
.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
1306 TemplateURL
url(NULL
, data
);
1307 EXPECT_TRUE(url
.url_ref().IsValid());
1308 ASSERT_TRUE(url
.url_ref().SupportsReplacement());
1309 TemplateURLRef::SearchTermsArgs
search_terms_args(ASCIIToUTF16("foo"));
1311 // Do not add the param when InstantExtended is suppressed on SRPs.
1312 url
.url_ref_
.showing_search_terms_
= false;
1313 std::string result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
);
1314 EXPECT_EQ("http://www.google.com/?q=foo", result
);
1316 // Add the param when InstantExtended is not suppressed on SRPs.
1317 url
.url_ref_
.showing_search_terms_
= true;
1318 search_terms_args
.bookmark_bar_pinned
= false;
1319 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
);
1320 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result
);
1322 url
.url_ref_
.showing_search_terms_
= true;
1323 search_terms_args
.bookmark_bar_pinned
= true;
1324 result
= url
.url_ref().ReplaceSearchTerms(search_terms_args
);
1325 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result
);