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