blob: 68b907b1cb7842e1971b6649d8bd0e5699215b54 [file] [log] [blame]
[email protected]34e24852012-01-31 18:43:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d82443b2009-01-15 19:54:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]12bd05872009-03-17 19:25:065#include "base/base_paths.h"
[email protected]56fa29592013-07-02 20:25:536#include "base/command_line.h"
[email protected]2f3bc6512013-08-28 03:56:277#include "base/strings/string_number_conversions.h"
[email protected]24a555b62013-06-10 22:01:178#include "base/strings/string_util.h"
[email protected]e309f312013-06-07 21:50:089#include "base/strings/utf_string_conversions.h"
[email protected]d82443b2009-01-15 19:54:5610#include "chrome/browser/browser_process.h"
11#include "chrome/browser/rlz/rlz.h"
[email protected]375bd7312010-08-30 22:18:1312#include "chrome/browser/search_engines/search_terms_data.h"
[email protected]d54e03a52009-01-16 00:31:0413#include "chrome/browser/search_engines/template_url.h"
[email protected]56fa29592013-07-02 20:25:5314#include "chrome/common/chrome_switches.h"
[email protected]93b29062013-07-12 03:09:0915#include "chrome/common/chrome_version_info.h"
[email protected]d82443b2009-01-15 19:54:5616#include "testing/gtest/include/gtest/gtest.h"
17
[email protected]81d9b72d2012-03-26 22:29:1718#if defined(ENABLE_RLZ)
19#include "chrome/browser/google/google_util.h"
20#endif
21
[email protected]fd6d8822012-12-08 06:56:1122#if defined(OS_ANDROID)
23#include "chrome/browser/search_engines/search_terms_data_android.h"
24#endif
25
[email protected]f911df52013-12-24 23:24:2326using base::ASCIIToUTF16;
27
[email protected]b37bdfe2012-03-16 20:53:2728// TestSearchTermsData --------------------------------------------------------
29
[email protected]375bd7312010-08-30 22:18:1330// Simple implementation of SearchTermsData.
31class TestSearchTermsData : public SearchTermsData {
32 public:
[email protected]b37bdfe2012-03-16 20:53:2733 explicit TestSearchTermsData(const std::string& google_base_url);
[email protected]375bd7312010-08-30 22:18:1334
[email protected]b37bdfe2012-03-16 20:53:2735 virtual std::string GoogleBaseURLValue() const OVERRIDE;
[email protected]375bd7312010-08-30 22:18:1336
37 private:
38 std::string google_base_url_;
39
40 DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData);
41};
42
[email protected]b37bdfe2012-03-16 20:53:2743TestSearchTermsData::TestSearchTermsData(const std::string& google_base_url)
[email protected]2767c0fd2013-08-16 17:44:1644 : google_base_url_(google_base_url) {
[email protected]b37bdfe2012-03-16 20:53:2745}
46
47std::string TestSearchTermsData::GoogleBaseURLValue() const {
48 return google_base_url_;
49}
50
[email protected]b37bdfe2012-03-16 20:53:2751// TemplateURLTest ------------------------------------------------------------
52
[email protected]583844c2011-08-27 00:38:3553class TemplateURLTest : public testing::Test {
[email protected]d82443b2009-01-15 19:54:5654 public:
[email protected]b37bdfe2012-03-16 20:53:2755 void CheckSuggestBaseURL(const std::string& base_url,
56 const std::string& base_suggest_url) const;
[email protected]d82443b2009-01-15 19:54:5657};
58
[email protected]b37bdfe2012-03-16 20:53:2759void 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());
64}
65
66
67// Actual tests ---------------------------------------------------------------
68
[email protected]d82443b2009-01-15 19:54:5669TEST_F(TemplateURLTest, Defaults) {
[email protected]573889f22012-04-07 01:31:5470 TemplateURLData data;
71 EXPECT_FALSE(data.show_in_default_list);
72 EXPECT_FALSE(data.safe_for_autoreplace);
73 EXPECT_EQ(0, data.prepopulate_id);
[email protected]d82443b2009-01-15 19:54:5674}
75
76TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]573889f22012-04-07 01:31:5477 TemplateURLData data;
78 data.SetURL("{searchTerms}");
[email protected]16fca9b82012-04-23 18:40:2679 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:1380 EXPECT_TRUE(url.url_ref().IsValid());
[email protected]d82443b2009-01-15 19:54:5681}
82
83TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1984 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1985 const char* url;
[email protected]0085863a2013-12-06 21:19:0386 const base::string16 terms;
[email protected]34e24852012-01-31 18:43:5887 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:1988 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:3089 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1690 "http://foosea%20rch/bar" },
[email protected]400b133f2011-01-19 18:32:3091 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1692 "http://foosea%20rch/bar?boo=abc" },
[email protected]400b133f2011-01-19 18:32:3093 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1694 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]400b133f2011-01-19 18:32:3095 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
[email protected]c31a979c2012-05-31 23:57:1696 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:1997 };
98 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
99 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54100 TemplateURLData data;
101 data.SetURL(value.url);
[email protected]16fca9b82012-04-23 18:40:26102 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13103 EXPECT_TRUE(url.url_ref().IsValid());
104 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04105 GURL result(url.url_ref().ReplaceSearchTerms(
106 TemplateURLRef::SearchTermsArgs(value.terms)));
[email protected]c31a979c2012-05-31 23:57:16107 ASSERT_TRUE(result.is_valid());
108 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:19109 }
[email protected]d82443b2009-01-15 19:54:56110}
111
112TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:54113 TemplateURLData data;
114 data.SetURL("http://foo{searchTerms}{count?}");
[email protected]16fca9b82012-04-23 18:40:26115 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13116 EXPECT_TRUE(url.url_ref().IsValid());
117 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04118 GURL result(url.url_ref().ReplaceSearchTerms(
119 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56120 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27121 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:56122}
123
124TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:54125 TemplateURLData data;
126 data.SetURL("http://foo{searchTerms}{count}");
[email protected]16fca9b82012-04-23 18:40:26127 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13128 EXPECT_TRUE(url.url_ref().IsValid());
129 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04130 GURL result(url.url_ref().ReplaceSearchTerms(
131 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56132 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27133 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56134}
135
136TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54137 TemplateURLData data;
138 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]16fca9b82012-04-23 18:40:26139 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13140 EXPECT_TRUE(url.url_ref().IsValid());
141 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04142 GURL result(url.url_ref().ReplaceSearchTerms(
143 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56144 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27145 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56146}
147
148TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54149 TemplateURLData data;
150 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[email protected]16fca9b82012-04-23 18:40:26151 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13152 EXPECT_TRUE(url.url_ref().IsValid());
153 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04154 GURL result(url.url_ref().ReplaceSearchTerms(
155 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56156 ASSERT_TRUE(result.is_valid());
[email protected]405aae22012-03-29 20:36:13157 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56158}
159
160TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54161 TemplateURLData data;
162 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[email protected]16fca9b82012-04-23 18:40:26163 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13164 EXPECT_TRUE(url.url_ref().IsValid());
165 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04166 GURL result(url.url_ref().ReplaceSearchTerms(
167 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56168 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27169 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56170}
171
[email protected]93b29062013-07-12 03:09:09172TEST_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=,"
[email protected]2f3bc6512013-08-28 03:56:27181 "constant_param=constant,width={google:imageOriginalWidth}";
[email protected]93b29062013-07-12 03:09:09182 const char KImageSearchURL[] = "http://foo.com/sbi";
183
184 TemplateURLData data;
185 data.image_url = KImageSearchURL;
186
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);
198
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());
204
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");
[email protected]2f3bc6512013-08-28 03:56:27209 search_args.image_original_size = gfx::Size(10, 10);
[email protected]93b29062013-07-12 03:09:09210 // Replacement operation with no post_data buffer should still return
211 // the parsed URL.
212 GURL result(url.image_url_ref().ReplaceSearchTerms(search_args));
213 ASSERT_TRUE(result.is_valid());
214 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48215 TemplateURLRef::PostContent post_content;
[email protected]93b29062013-07-12 03:09:09216 TestSearchTermsData search_terms_data("http://X");
217 result = GURL(url.image_url_ref().ReplaceSearchTermsUsingTermsData(
[email protected]7c2bcc42013-08-01 04:03:48218 search_args, search_terms_data, &post_content));
[email protected]93b29062013-07-12 03:09:09219 ASSERT_TRUE(result.is_valid());
220 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48221 ASSERT_FALSE(post_content.first.empty());
222 ASSERT_FALSE(post_content.second.empty());
[email protected]93b29062013-07-12 03:09:09223
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_;
[email protected]2f3bc6512013-08-28 03:56:27229 EXPECT_EQ(7U, post_params.size());
[email protected]93b29062013-07-12 03:09:09230 for (TemplateURLRef::PostParams::const_iterator i = post_params.begin();
231 i != post_params.end(); ++i) {
232 TemplateURLRef::Replacements::const_iterator j = replacements.begin();
233 for (; j != replacements.end(); ++j) {
234 if (j->is_post_param && j->index ==
235 static_cast<size_t>(i - post_params.begin())) {
236 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27237 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
238 EXPECT_EQ("width", i->first);
239 EXPECT_EQ(
240 base::IntToString(search_args.image_original_size.width()),
241 i->second);
242 break;
[email protected]93b29062013-07-12 03:09:09243 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
244 EXPECT_EQ("image_content", i->first);
245 EXPECT_EQ(search_args.image_thumbnail_content, i->second);
246 break;
247 case TemplateURLRef::GOOGLE_IMAGE_URL:
248 EXPECT_EQ("image_url", i->first);
249 EXPECT_EQ(search_args.image_url.spec(), i->second);
250 break;
251 case TemplateURLRef::LANGUAGE:
252 EXPECT_EQ("language", i->first);
253 EXPECT_EQ("en", i->second);
254 break;
255 default:
256 ADD_FAILURE(); // Should never go here.
257 }
258 break;
259 }
260 }
261 if (j != replacements.end())
262 continue;
263 if (i->first == "empty_param") {
264 EXPECT_TRUE(i->second.empty());
265 } else if (i->first == "sbisrc") {
[email protected]7c2bcc42013-08-01 04:03:48266 EXPECT_FALSE(i->second.empty());
[email protected]93b29062013-07-12 03:09:09267 } else {
268 EXPECT_EQ("constant_param", i->first);
269 EXPECT_EQ("constant", i->second);
270 }
271 }
272}
273
[email protected]d88cb202011-08-17 20:03:01274// Test that setting the prepopulate ID from TemplateURL causes the stored
275// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44276TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54277 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20278 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]16fca9b82012-04-23 18:40:26279 TemplateURL url(NULL, data);
[email protected]1a257262011-06-28 22:15:44280 TemplateURLRef::Replacements replacements;
281 bool valid = false;
[email protected]243abf32012-05-15 18:28:20282 EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL(
[email protected]93b29062013-07-12 03:09:09283 "http://foo{fhqwhgads}bar", &replacements, NULL, &valid));
[email protected]1a257262011-06-28 22:15:44284 EXPECT_TRUE(replacements.empty());
285 EXPECT_TRUE(valid);
286
[email protected]573889f22012-04-07 01:31:54287 data.prepopulate_id = 123;
[email protected]16fca9b82012-04-23 18:40:26288 TemplateURL url2(NULL, data);
[email protected]243abf32012-05-15 18:28:20289 EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar",
[email protected]93b29062013-07-12 03:09:09290 &replacements, NULL,
291 &valid));
[email protected]1a257262011-06-28 22:15:44292 EXPECT_TRUE(replacements.empty());
293 EXPECT_TRUE(valid);
294}
295
[email protected]d82443b2009-01-15 19:54:56296TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54297 TemplateURLData data;
298 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]16fca9b82012-04-23 18:40:26299 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13300 EXPECT_TRUE(url.url_ref().IsValid());
301 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04302 GURL result(url.url_ref().ReplaceSearchTerms(
303 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56304 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27305 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56306}
307
308TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54309 TemplateURLData data;
310 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]16fca9b82012-04-23 18:40:26311 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13312 EXPECT_TRUE(url.url_ref().IsValid());
313 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04314 GURL result(url.url_ref().ReplaceSearchTerms(
315 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56316 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27317 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56318}
319
[email protected]375bd7312010-08-30 22:18:13320TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
321 struct SearchTermsCase {
322 const char* url;
[email protected]0085863a2013-12-06 21:19:03323 const base::string16 terms;
[email protected]375bd7312010-08-30 22:18:13324 const char* output;
325 } search_term_cases[] = {
[email protected]0085863a2013-12-06 21:19:03326 { "{google:baseURL}{language}{searchTerms}", base::string16(),
[email protected]b37bdfe2012-03-16 20:53:27327 "http://example.com/e/en" },
[email protected]0085863a2013-12-06 21:19:03328 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
[email protected]014010e2011-10-01 04:12:44329 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13330 };
331
332 TestSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54333 TemplateURLData data;
[email protected]375bd7312010-08-30 22:18:13334 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
335 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54336 data.SetURL(value.url);
[email protected]16fca9b82012-04-23 18:40:26337 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13338 EXPECT_TRUE(url.url_ref().IsValid());
339 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04340 GURL result(url.url_ref().ReplaceSearchTermsUsingTermsData(
[email protected]93b29062013-07-12 03:09:09341 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL));
[email protected]375bd7312010-08-30 22:18:13342 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27343 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13344 }
345}
346
[email protected]d82443b2009-01-15 19:54:56347TEST_F(TemplateURLTest, URLRefTermToWide) {
348 struct ToWideCase {
349 const char* encoded_search_term;
[email protected]0085863a2013-12-06 21:19:03350 const base::string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56351 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30352 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56353 // Test some big-5 input.
[email protected]f911df52013-12-24 23:24:23354 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56355 // 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.
[email protected]f911df52013-12-24 23:24:23358 {"%e4%bd%a05%e5%a5%bd+to+you",
359 base::WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56360 // Undecodable input should stay escaped.
[email protected]f911df52013-12-24 23:24:23361 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44362 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30363 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44364 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30365 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56366 };
367
[email protected]d82443b2009-01-15 19:54:56368 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54369 TemplateURLData data;
370 data.SetURL("http://foo?q={searchTerms}");
371 data.input_encodings.push_back("big-5");
[email protected]16fca9b82012-04-23 18:40:26372 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13373 EXPECT_TRUE(url.url_ref().IsValid());
374 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]f63ae312009-02-04 17:58:46375 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07376 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13377 url.url_ref().SearchTermToString16(
378 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56379 }
380}
381
[email protected]d82443b2009-01-15 19:54:56382TEST_F(TemplateURLTest, DisplayURLToURLRef) {
383 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19384 const std::string url;
[email protected]0085863a2013-12-06 21:19:03385 const base::string16 expected_result;
[email protected]b37bdfe2012-03-16 20:53:27386 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19387 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30388 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19389 { "http://X",
[email protected]400b133f2011-01-19 18:32:30390 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19391 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30392 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19393 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30394 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56395 };
[email protected]573889f22012-04-07 01:31:54396 TemplateURLData data;
[email protected]b37bdfe2012-03-16 20:53:27397 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54398 data.SetURL(test_data[i].url);
[email protected]16fca9b82012-04-23 18:40:26399 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13400 EXPECT_EQ(test_data[i].expected_result, url.url_ref().DisplayURL());
[email protected]b37bdfe2012-03-16 20:53:27401 EXPECT_EQ(test_data[i].url,
[email protected]360ba052012-04-04 17:26:13402 TemplateURLRef::DisplayURLToURLRef(url.url_ref().DisplayURL()));
[email protected]d82443b2009-01-15 19:54:56403 }
404}
405
406TEST_F(TemplateURLTest, ReplaceSearchTerms) {
407 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19408 const std::string url;
[email protected]d82443b2009-01-15 19:54:56409 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27410 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19411 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56412 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19413 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56414 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19415 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56416 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19417 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56418 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19419 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56420 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19421 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56422 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19423 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56424 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19425 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56426 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19427 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56428 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19429 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56430 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19431 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56432 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19433 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56434 "http://foo/UTF-8a{language}aXa" },
435 };
[email protected]573889f22012-04-07 01:31:54436 TemplateURLData data;
437 data.input_encodings.push_back("UTF-8");
[email protected]b37bdfe2012-03-16 20:53:27438 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54439 data.SetURL(test_data[i].url);
[email protected]16fca9b82012-04-23 18:40:26440 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13441 EXPECT_TRUE(url.url_ref().IsValid());
[email protected]3954c3a2012-04-10 20:17:55442 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]b37bdfe2012-03-16 20:53:27443 std::string expected_result = test_data[i].expected_result;
[email protected]d82443b2009-01-15 19:54:56444 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}",
[email protected]d70539de2009-06-24 22:17:06445 g_browser_process->GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04446 GURL result(url.url_ref().ReplaceSearchTerms(
447 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]b37bdfe2012-03-16 20:53:27448 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56449 EXPECT_EQ(expected_result, result.spec());
450 }
451}
452
453
454// Tests replacing search terms in various encodings and making sure the
455// generated URL matches the expected value.
456TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
457 struct TestData {
458 const std::string encoding;
[email protected]0085863a2013-12-06 21:19:03459 const base::string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19460 const std::string url;
[email protected]d82443b2009-01-15 19:54:56461 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27462 } test_data[] = {
[email protected]f911df52013-12-24 23:24:23463 { "BIG5", base::WideToUTF16(L"\x60BD"),
[email protected]400b133f2011-01-19 18:32:30464 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17465 "http://foo/?%B1~BIG5" },
[email protected]400b133f2011-01-19 18:32:30466 { "UTF-8", ASCIIToUTF16("blah"),
467 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17468 "http://foo/?blahUTF-8" },
[email protected]f911df52013-12-24 23:24:23469 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
[email protected]34e24852012-01-31 18:43:58470 "http://foo/{searchTerms}/bar",
471 "http://foo/%82%A0/bar"},
[email protected]f911df52013-12-24 23:24:23472 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
[email protected]34e24852012-01-31 18:43:58473 "http://foo/{searchTerms}/bar",
474 "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56475 };
[email protected]573889f22012-04-07 01:31:54476 TemplateURLData data;
[email protected]b37bdfe2012-03-16 20:53:27477 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54478 data.SetURL(test_data[i].url);
479 data.input_encodings.clear();
480 data.input_encodings.push_back(test_data[i].encoding);
[email protected]16fca9b82012-04-23 18:40:26481 TemplateURL url(NULL, data);
[email protected]3954c3a2012-04-10 20:17:55482 EXPECT_TRUE(url.url_ref().IsValid());
483 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04484 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());
488 }
489}
490
491// Tests replacing assisted query stats (AQS) in various scenarios.
492TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
493 struct TestData {
[email protected]0085863a2013-12-06 21:19:03494 const base::string16 search_term;
[email protected]bca359b2012-06-24 07:53:04495 const std::string aqs;
496 const std::string base_url;
497 const std::string url;
498 const std::string expected_result;
499 } test_data[] = {
500 // No HTTPS, no AQS.
501 { ASCIIToUTF16("foo"),
502 "chrome.0.0l6",
503 "http://foo/",
504 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
505 "http://foo/?foo" },
506 // HTTPS available, AQS should be replaced.
507 { ASCIIToUTF16("foo"),
508 "chrome.0.0l6",
509 "https://foo/",
510 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
511 "https://foo/?fooaqs=chrome.0.0l6&" },
512 // HTTPS available, however AQS is empty.
513 { ASCIIToUTF16("foo"),
514 "",
515 "https://foo/",
516 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
517 "https://foo/?foo" },
518 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
519 { ASCIIToUTF16("foo"),
520 "chrome.0.0l6",
521 "",
522 "http://foo?{searchTerms}{google:assistedQueryStats}",
523 "http://foo/?foo" },
524 // A non-Google search provider with HTTPS should allow AQS.
525 { ASCIIToUTF16("foo"),
526 "chrome.0.0l6",
527 "",
528 "https://foo?{searchTerms}{google:assistedQueryStats}",
529 "https://foo/?fooaqs=chrome.0.0l6&" },
530 };
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));
[email protected]b37bdfe2012-03-16 20:53:27542 ASSERT_TRUE(result.is_valid());
543 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56544 }
545}
546
[email protected]00790562012-12-14 09:57:16547// Tests replacing cursor position.
548TEST_F(TemplateURLTest, ReplaceCursorPosition) {
549 struct TestData {
[email protected]0085863a2013-12-06 21:19:03550 const base::string16 search_term;
[email protected]00790562012-12-14 09:57:16551 size_t cursor_position;
552 const std::string url;
553 const std::string expected_result;
554 } test_data[] = {
555 { ASCIIToUTF16("foo"),
[email protected]0085863a2013-12-06 21:19:03556 base::string16::npos,
[email protected]00790562012-12-14 09:57:16557 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
558 "http://www.google.com/?foo&" },
559 { ASCIIToUTF16("foo"),
560 2,
561 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
562 "http://www.google.com/?foo&cp=2&" },
563 { ASCIIToUTF16("foo"),
564 15,
565 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
566 "http://www.google.com/?foo&cp=15&" },
567 };
568 TemplateURLData data;
569 data.input_encodings.push_back("UTF-8");
570 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
571 data.SetURL(test_data[i].url);
572 TemplateURL url(NULL, data);
573 EXPECT_TRUE(url.url_ref().IsValid());
574 ASSERT_TRUE(url.url_ref().SupportsReplacement());
575 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
576 search_terms_args.cursor_position = test_data[i].cursor_position;
577 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
578 ASSERT_TRUE(result.is_valid());
579 EXPECT_EQ(test_data[i].expected_result, result.spec());
580 }
581}
582
[email protected]9b9fa672013-11-07 06:04:52583// Tests replacing currentPageUrl.
584TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48585 struct TestData {
[email protected]0085863a2013-12-06 21:19:03586 const base::string16 search_term;
[email protected]9b9fa672013-11-07 06:04:52587 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48588 const std::string url;
589 const std::string expected_result;
590 } test_data[] = {
591 { ASCIIToUTF16("foo"),
592 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52593 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48594 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
595 { ASCIIToUTF16("foo"),
596 "",
[email protected]9b9fa672013-11-07 06:04:52597 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48598 "http://www.google.com/?foo&" },
599 { ASCIIToUTF16("foo"),
600 "http://g.com/+-/*&=",
[email protected]9b9fa672013-11-07 06:04:52601 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48602 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
603 };
604 TemplateURLData data;
605 data.input_encodings.push_back("UTF-8");
606 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
607 data.SetURL(test_data[i].url);
608 TemplateURL url(NULL, data);
609 EXPECT_TRUE(url.url_ref().IsValid());
610 ASSERT_TRUE(url.url_ref().SupportsReplacement());
611 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52612 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]800569d2013-05-06 07:38:48613 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
614 ASSERT_TRUE(result.is_valid());
615 EXPECT_EQ(test_data[i].expected_result, result.spec());
616 }
617}
618
[email protected]d82443b2009-01-15 19:54:56619TEST_F(TemplateURLTest, Suggestions) {
620 struct TestData {
621 const int accepted_suggestion;
[email protected]0085863a2013-12-06 21:19:03622 const base::string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56623 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27624 } test_data[] = {
[email protected]0085863a2013-12-06 21:19:03625 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
[email protected]29653892013-03-02 19:25:37626 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30627 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37628 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03629 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(),
[email protected]29653892013-03-02 19:25:37630 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30631 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37632 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03633 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
[email protected]29653892013-03-02 19:25:37634 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56635 };
[email protected]573889f22012-04-07 01:31:54636 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37637 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
638 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54639 data.input_encodings.push_back("UTF-8");
[email protected]16fca9b82012-04-23 18:40:26640 TemplateURL url(NULL, data);
[email protected]3954c3a2012-04-10 20:17:55641 EXPECT_TRUE(url.url_ref().IsValid());
[email protected]360ba052012-04-04 17:26:13642 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]b37bdfe2012-03-16 20:53:27643 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04644 TemplateURLRef::SearchTermsArgs search_terms_args(
645 ASCIIToUTF16("foobar"));
646 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
647 search_terms_args.original_query =
648 test_data[i].original_query_for_suggestion;
649 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
[email protected]b37bdfe2012-03-16 20:53:27650 ASSERT_TRUE(result.is_valid());
651 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56652 }
653}
654
[email protected]81f808de2009-09-25 01:36:34655TEST_F(TemplateURLTest, RLZ) {
[email protected]0085863a2013-12-06 21:19:03656 base::string16 rlz_string;
[email protected]81d9b72d2012-03-26 22:29:17657#if defined(ENABLE_RLZ)
658 std::string brand;
659 if (google_util::GetBrand(&brand) && !brand.empty() &&
660 !google_util::IsOrganic(brand)) {
[email protected]7e6f4ed5c2012-10-31 21:10:34661 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz_string);
[email protected]81d9b72d2012-03-26 22:29:17662 }
[email protected]fd6d8822012-12-08 06:56:11663#elif defined(OS_ANDROID)
664 SearchTermsDataAndroid::rlz_parameter_value_.Get() =
665 ASCIIToUTF16("android_test");
666 rlz_string = SearchTermsDataAndroid::rlz_parameter_value_.Get();
[email protected]58b64332010-08-13 16:09:39667#endif
[email protected]d82443b2009-01-15 19:54:56668
[email protected]573889f22012-04-07 01:31:54669 TemplateURLData data;
670 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]16fca9b82012-04-23 18:40:26671 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13672 EXPECT_TRUE(url.url_ref().IsValid());
673 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04674 GURL result(url.url_ref().ReplaceSearchTerms(
675 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x"))));
[email protected]d82443b2009-01-15 19:54:56676 ASSERT_TRUE(result.is_valid());
[email protected]12bd05872009-03-17 19:25:06677 std::string expected_url = "http://bar/?";
[email protected]b37bdfe2012-03-16 20:53:27678 if (!rlz_string.empty())
[email protected]f911df52013-12-24 23:24:23679 expected_url += "rlz=" + base::UTF16ToUTF8(rlz_string) + "&";
[email protected]12bd05872009-03-17 19:25:06680 expected_url += "x";
[email protected]b37bdfe2012-03-16 20:53:27681 EXPECT_EQ(expected_url, result.spec());
[email protected]d82443b2009-01-15 19:54:56682}
683
684TEST_F(TemplateURLTest, HostAndSearchTermKey) {
685 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19686 const std::string url;
[email protected]d82443b2009-01-15 19:54:56687 const std::string host;
688 const std::string path;
689 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54690 } test_data[] = {
[email protected]7c60f5042013-02-14 03:39:32691 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56692
693 // No query key should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32694 { "http://blah/{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56695
696 // No term should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32697 { "http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56698
699 // Multiple terms should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32700 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56701
702 // Term in the host shouldn't match.
[email protected]7c60f5042013-02-14 03:39:32703 { "http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56704
[email protected]7c60f5042013-02-14 03:39:32705 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
706 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56707
708 // Single term with extra chars in value should match.
[email protected]7c60f5042013-02-14 03:39:32709 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56710 };
711
[email protected]573889f22012-04-07 01:31:54712 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
713 TemplateURLData data;
714 data.SetURL(test_data[i].url);
[email protected]16fca9b82012-04-23 18:40:26715 TemplateURL url(NULL, data);
[email protected]573889f22012-04-07 01:31:54716 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost());
717 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath());
718 EXPECT_EQ(test_data[i].search_term_key, url.url_ref().GetSearchTermKey());
[email protected]d82443b2009-01-15 19:54:56719 }
720}
721
722TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
723 static const struct {
[email protected]ddd231e2010-06-29 20:35:19724 const char* const base_url;
725 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56726 } data[] = {
[email protected]014010e2011-10-01 04:12:44727 { "http://google.com/", "http://google.com/complete/", },
728 { "http://www.google.com/", "http://www.google.com/complete/", },
729 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
730 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
731 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56732 };
733
[email protected]f63ae312009-02-04 17:58:46734 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i)
[email protected]d82443b2009-01-15 19:54:56735 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
736}
737
[email protected]81c6ef62010-01-21 09:58:47738TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19739 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:54740 TemplateURLData data;
741 data.SetURL(parsed_url);
[email protected]16fca9b82012-04-23 18:40:26742 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47743 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:13744 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19745 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47746 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27747 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47748 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
749}
750
751TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:20752 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:54753 TemplateURLData data;
754 data.SetURL(parsed_url);
[email protected]16fca9b82012-04-23 18:40:26755 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47756 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:44757
758 // By default, TemplateURLRef should not consider itself prepopulated.
759 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:13760 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20761 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:44762 EXPECT_TRUE(replacements.empty());
763
764 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:20765 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:54766 data.prepopulate_id = 1;
[email protected]16fca9b82012-04-23 18:40:26767 TemplateURL url2(NULL, data);
[email protected]495c30b2012-05-15 18:48:15768 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20769 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47770 EXPECT_TRUE(replacements.empty());
771}
772
773TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]16fca9b82012-04-23 18:40:26774 TemplateURL url(NULL, TemplateURLData());
[email protected]81c6ef62010-01-21 09:58:47775 TemplateURLRef::Replacements replacements;
776 bool valid = false;
[email protected]b37bdfe2012-03-16 20:53:27777 EXPECT_EQ(std::string(),
[email protected]93b29062013-07-12 03:09:09778 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47779 EXPECT_TRUE(replacements.empty());
780 EXPECT_TRUE(valid);
781}
782
783TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:54784 TemplateURLData data;
785 data.SetURL("{");
[email protected]16fca9b82012-04-23 18:40:26786 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47787 TemplateURLRef::Replacements replacements;
788 bool valid = false;
[email protected]93b29062013-07-12 03:09:09789 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
790 &valid));
[email protected]81c6ef62010-01-21 09:58:47791 EXPECT_TRUE(replacements.empty());
792 EXPECT_FALSE(valid);
793}
794
795TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:54796 TemplateURLData data;
797 data.SetURL("{}");
[email protected]16fca9b82012-04-23 18:40:26798 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47799 TemplateURLRef::Replacements replacements;
800 bool valid = false;
[email protected]93b29062013-07-12 03:09:09801 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47802 EXPECT_TRUE(replacements.empty());
803 EXPECT_TRUE(valid);
804}
805
806TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:54807 TemplateURLData data;
808 data.SetURL("{}{{%s}}");
[email protected]16fca9b82012-04-23 18:40:26809 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47810 TemplateURLRef::Replacements replacements;
811 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19812 EXPECT_EQ("{}{}",
[email protected]93b29062013-07-12 03:09:09813 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
814 &valid));
[email protected]81c6ef62010-01-21 09:58:47815 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27816 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47817 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
818 EXPECT_TRUE(valid);
819}
820
821TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:54822 TemplateURLData data;
823 data.SetURL("{%s");
[email protected]16fca9b82012-04-23 18:40:26824 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47825 TemplateURLRef::Replacements replacements;
826 bool valid = false;
[email protected]360ba052012-04-04 17:26:13827 EXPECT_EQ("{",
[email protected]93b29062013-07-12 03:09:09828 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
829 &valid));
[email protected]81c6ef62010-01-21 09:58:47830 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27831 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47832 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
833 EXPECT_TRUE(valid);
834}
[email protected]5b3078752012-10-09 18:54:16835
[email protected]fd6d8822012-12-08 06:56:11836#if defined(OS_ANDROID)
837TEST_F(TemplateURLTest, SearchClient) {
838 const std::string base_url_str("http://google.com/?");
839 const std::string terms_str("{searchTerms}&{google:searchClient}");
840 const std::string full_url_str = base_url_str + terms_str;
[email protected]0085863a2013-12-06 21:19:03841 const base::string16 terms(ASCIIToUTF16(terms_str));
[email protected]fd6d8822012-12-08 06:56:11842 UIThreadSearchTermsData::SetGoogleBaseURL(base_url_str);
843
844 TemplateURLData data;
845 data.SetURL(full_url_str);
846 TemplateURL url(NULL, data);
847 EXPECT_TRUE(url.url_ref().IsValid());
848 ASSERT_TRUE(url.url_ref().SupportsReplacement());
849 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
850
851 // Check that the URL is correct when a client is not present.
852 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
853 ASSERT_TRUE(result.is_valid());
854 EXPECT_EQ("http://google.com/?foobar&", result.spec());
855
856 // Check that the URL is correct when a client is present.
857 SearchTermsDataAndroid::search_client_.Get() = "android_test";
858 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args));
859 ASSERT_TRUE(result_2.is_valid());
860 EXPECT_EQ("http://google.com/?foobar&client=android_test&", result_2.spec());
861}
862#endif
863
[email protected]5b3078752012-10-09 18:54:16864TEST_F(TemplateURLTest, GetURLNoInstantURL) {
865 TemplateURLData data;
866 data.SetURL("http://google.com/?q={searchTerms}");
867 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
868 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
869 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
870 TemplateURL url(NULL, data);
871 ASSERT_EQ(3U, url.URLCount());
872 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
873 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
874 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
875}
876
877TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
878 TemplateURLData data;
879 data.SetURL("http://google.com/?q={searchTerms}");
880 data.instant_url = "http://google.com/instant#q={searchTerms}";
881 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
882 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
883 TemplateURL url(NULL, data);
884 ASSERT_EQ(3U, url.URLCount());
885 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
886 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
887 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
888}
889
890TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
891 TemplateURLData data;
892 data.SetURL("http://www.google.co.uk/");
893 TemplateURL url(NULL, data);
894 ASSERT_EQ(1U, url.URLCount());
895 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0));
896}
897
898TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
899 TemplateURLData data;
900 data.SetURL("http://google.com/?q={searchTerms}");
901 data.instant_url = "http://google.com/instant#q={searchTerms}";
902 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
903 data.alternate_urls.push_back(
904 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
905 TemplateURL url(NULL, data);
[email protected]0085863a2013-12-06 21:19:03906 base::string16 result;
[email protected]5b3078752012-10-09 18:54:16907
908 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
909 GURL("http://google.com/?q=something"), &result));
[email protected]4076ea62013-01-09 01:47:19910 EXPECT_EQ(ASCIIToUTF16("something"), result);
911
912 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
913 GURL("http://google.com/?espv&q=something"), &result));
914 EXPECT_EQ(ASCIIToUTF16("something"), result);
915
916 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
917 GURL("http://google.com/?espv=1&q=something"), &result));
918 EXPECT_EQ(ASCIIToUTF16("something"), result);
919
920 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
921 GURL("http://google.com/?espv=0&q=something"), &result));
922 EXPECT_EQ(ASCIIToUTF16("something"), result);
923
924 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
925 GURL("http://google.com/alt/#q=something"), &result));
926 EXPECT_EQ(ASCIIToUTF16("something"), result);
927
928 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
929 GURL("http://google.com/alt/#espv&q=something"), &result));
930 EXPECT_EQ(ASCIIToUTF16("something"), result);
931
932 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
933 GURL("http://google.com/alt/#espv=1&q=something"), &result));
934 EXPECT_EQ(ASCIIToUTF16("something"), result);
935
936 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
937 GURL("http://google.com/alt/#espv=0&q=something"), &result));
[email protected]5b3078752012-10-09 18:54:16938 EXPECT_EQ(ASCIIToUTF16("something"), result);
939
940 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
941 GURL("http://google.ca/?q=something"), &result));
[email protected]b959d7d42013-12-13 17:26:37942 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:16943
944 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]67d8b752013-04-03 17:33:27945 GURL("http://google.ca/?q=something&q=anything"), &result));
[email protected]b959d7d42013-12-13 17:26:37946 EXPECT_EQ(base::string16(), result);
[email protected]67d8b752013-04-03 17:33:27947
948 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]5b3078752012-10-09 18:54:16949 GURL("http://google.com/foo/?q=foo"), &result));
[email protected]b959d7d42013-12-13 17:26:37950 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:16951
[email protected]32e2d81b2012-10-16 19:03:25952 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]5b3078752012-10-09 18:54:16953 GURL("https://google.com/?q=foo"), &result));
[email protected]32e2d81b2012-10-16 19:03:25954 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:16955
956 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
957 GURL("http://google.com:8080/?q=foo"), &result));
[email protected]b959d7d42013-12-13 17:26:37958 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:16959
960 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
961 GURL("http://google.com/?q=1+2+3&b=456"), &result));
962 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
963
964 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
965 GURL("http://google.com/alt/?q=123#q=456"), &result));
966 EXPECT_EQ(ASCIIToUTF16("456"), result);
967
968 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
969 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), &result));
970 EXPECT_EQ(ASCIIToUTF16("123"), result);
971
972 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
973 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), &result));
974 EXPECT_EQ(ASCIIToUTF16("789"), result);
975
976 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
977 GURL("http://google.com/alt/?q="), &result));
[email protected]b959d7d42013-12-13 17:26:37978 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:16979
980 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
981 GURL("http://google.com/alt/?#q="), &result));
[email protected]b959d7d42013-12-13 17:26:37982 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:16983
984 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
985 GURL("http://google.com/alt/?q=#q="), &result));
[email protected]b959d7d42013-12-13 17:26:37986 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:16987
988 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
989 GURL("http://google.com/alt/?q=123#q="), &result));
[email protected]b959d7d42013-12-13 17:26:37990 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:16991
992 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
993 GURL("http://google.com/alt/?q=#q=123"), &result));
994 EXPECT_EQ(ASCIIToUTF16("123"), result);
995}
[email protected]4076ea62013-01-09 01:47:19996
997TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
998 TemplateURLData data;
999 data.SetURL("http://google.com/?q={searchTerms}");
1000 data.instant_url = "http://google.com/instant#q={searchTerms}";
1001 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1002 data.alternate_urls.push_back(
1003 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1004 data.search_terms_replacement_key = "espv";
1005 TemplateURL url(NULL, data);
1006
1007 // Test with instant enabled required.
1008 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1009 GURL("http://google.com/")));
1010
1011 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1012 GURL("http://google.com/?espv")));
1013
1014 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1015 GURL("http://google.com/#espv")));
1016
1017 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1018 GURL("http://google.com/?q=something")));
1019
1020 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1021 GURL("http://google.com/?q=something&espv")));
1022
1023 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1024 GURL("http://google.com/?q=something&espv=1")));
1025
1026 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1027 GURL("http://google.com/?q=something&espv=0")));
1028
1029 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1030 GURL("http://google.com/?espv&q=something")));
1031
1032 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1033 GURL("http://google.com/?espv=1&q=something")));
1034
1035 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1036 GURL("http://google.com/?espv=0&q=something")));
1037
1038 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1039 GURL("http://google.com/alt/#q=something")));
1040
1041 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1042 GURL("http://google.com/alt/#q=something&espv")));
1043
1044 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1045 GURL("http://google.com/alt/#q=something&espv=1")));
1046
1047 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1048 GURL("http://google.com/alt/#q=something&espv=0")));
1049
1050 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1051 GURL("http://google.com/alt/#espv&q=something")));
1052
1053 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1054 GURL("http://google.com/alt/#espv=1&q=something")));
1055
1056 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1057 GURL("http://google.com/alt/#espv=0&q=something")));
1058
1059 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1060 GURL("http://google.com/?espv#q=something")));
1061
1062 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1063 GURL("http://google.com/?espv=1#q=something")));
1064
1065 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1066 GURL("http://google.com/?q=something#espv")));
1067
1068 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1069 GURL("http://google.com/?q=something#espv=1")));
1070
1071 // This does not ensure the domain matches.
1072 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1073 GURL("http://bing.com/?espv")));
1074
1075 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1076 GURL("http://bing.com/#espv")));
1077}
[email protected]f62e30f52013-03-23 03:45:151078
1079TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1080 TemplateURLData data;
1081 data.SetURL("http://google.com/?q={searchTerms}");
1082 data.instant_url = "http://google.com/instant#q={searchTerms}";
1083 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1084 data.alternate_urls.push_back(
1085 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1086 TemplateURL url(NULL, data);
1087 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1088 GURL result;
1089
1090 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1091 GURL("http://google.com/?q=something"), search_terms, &result));
1092 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result);
1093
1094 result = GURL("http://should.not.change.com");
1095 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1096 GURL("http://google.ca/?q=something"), search_terms, &result));
1097 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1098
1099 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1100 GURL("http://google.com/foo/?q=foo"), search_terms, &result));
1101
1102 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1103 GURL("https://google.com/?q=foo"), search_terms, &result));
1104 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result);
1105
1106 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1107 GURL("http://google.com:8080/?q=foo"), search_terms, &result));
1108
1109 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1110 GURL("http://google.com/?q=1+2+3&b=456"), search_terms, &result));
1111 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result);
1112
1113 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1114 // template_url.cc for details.
1115 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1116 GURL("http://google.com/alt/?q=123#q=456"), search_terms, &result));
1117 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result);
1118
1119 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1120 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
1121 &result));
1122 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"),
1123 result);
1124
1125 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1126 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1127 search_terms, &result));
1128 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1129 "#j=abc&q=Bob Morane&h=def9"), result);
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="), search_terms, &result));
1136
1137 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1138 GURL("http://google.com/alt/?q=#q="), search_terms, &result));
1139
1140 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1141 GURL("http://google.com/alt/?q=123#q="), search_terms, &result));
1142
1143 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1144 GURL("http://google.com/alt/?q=#q=123"), search_terms, &result));
1145 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result);
1146}
[email protected]56fa29592013-07-02 20:25:531147
[email protected]621ade062013-10-28 06:27:431148// Test the |suggest_query_params| field of SearchTermsArgs.
1149TEST_F(TemplateURLTest, SuggestQueryParams) {
1150 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1151 TemplateURLData data;
1152 // Pick a URL with replacements before, during, and after the query, to ensure
1153 // we don't goof up any of them.
1154 data.SetURL("{google:baseURL}search?q={searchTerms}"
1155 "#{google:originalQueryForSuggestion}x");
1156 TemplateURL url(NULL, data);
1157
1158 // Baseline: no |suggest_query_params| field.
1159 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1160 search_terms.original_query = ASCIIToUTF16("def");
1161 search_terms.accepted_suggestion = 0;
1162 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1163 url.url_ref().ReplaceSearchTerms(search_terms));
1164
1165 // Set the suggest_query_params.
1166 search_terms.suggest_query_params = "pq=xyz";
1167 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
1168 url.url_ref().ReplaceSearchTerms(search_terms));
1169
1170 // Add extra_query_params in the mix, and ensure it works.
1171 search_terms.append_extra_query_params = true;
1172 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1173 switches::kExtraSearchQueryParams, "a=b");
1174 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
1175 url.url_ref().ReplaceSearchTerms(search_terms));
1176}
1177
[email protected]56fa29592013-07-02 20:25:531178// Test the |append_extra_query_params| field of SearchTermsArgs.
1179TEST_F(TemplateURLTest, ExtraQueryParams) {
1180 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1181 TemplateURLData data;
1182 // Pick a URL with replacements before, during, and after the query, to ensure
1183 // we don't goof up any of them.
1184 data.SetURL("{google:baseURL}search?q={searchTerms}"
1185 "#{google:originalQueryForSuggestion}x");
1186 TemplateURL url(NULL, data);
1187
1188 // Baseline: no command-line args, no |append_extra_query_params| flag.
1189 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1190 search_terms.original_query = ASCIIToUTF16("def");
1191 search_terms.accepted_suggestion = 0;
1192 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1193 url.url_ref().ReplaceSearchTerms(search_terms));
1194
1195 // Set the flag. Since there are no command-line args, this should have no
1196 // effect.
1197 search_terms.append_extra_query_params = true;
1198 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1199 url.url_ref().ReplaceSearchTerms(search_terms));
1200
1201 // Now append the command-line arg. This should be inserted into the query.
1202 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1203 switches::kExtraSearchQueryParams, "a=b");
1204 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
1205 url.url_ref().ReplaceSearchTerms(search_terms));
1206
1207 // Turn off the flag. Now the command-line arg should be ignored again.
1208 search_terms.append_extra_query_params = false;
1209 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1210 url.url_ref().ReplaceSearchTerms(search_terms));
1211}
[email protected]d5015ca2013-08-08 22:04:181212
1213// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171214TEST_F(TemplateURLTest, ReplacePageClassification) {
1215 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
[email protected]d5015ca2013-08-08 22:04:181216 TemplateURLData data;
1217 data.input_encodings.push_back("UTF-8");
1218 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
1219 TemplateURL url(NULL, data);
1220 EXPECT_TRUE(url.url_ref().IsValid());
1221 ASSERT_TRUE(url.url_ref().SupportsReplacement());
1222 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1223
1224 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1225 EXPECT_EQ("http://www.google.com/?q=foo", result);
1226
[email protected]12b5cd052013-11-08 20:59:371227 search_terms_args.page_classification = AutocompleteInput::NTP;
[email protected]d5015ca2013-08-08 22:04:181228 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1229 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1230
1231 search_terms_args.page_classification =
[email protected]0b1a1112013-08-28 05:23:311232 AutocompleteInput::HOME_PAGE;
[email protected]d5015ca2013-08-08 22:04:181233 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1234 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1235}
[email protected]2328ee22013-08-08 23:00:191236
1237// Test the IsSearchResults function.
1238TEST_F(TemplateURLTest, IsSearchResults) {
1239 TemplateURLData data;
1240 data.SetURL("http://bar/search?q={searchTerms}");
1241 data.instant_url = "http://bar/instant#q={searchTerms}";
[email protected]2767c0fd2013-08-16 17:44:161242 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191243 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1244 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1245 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1246 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
1247 TemplateURL search_provider(NULL, data);
1248
1249 const struct {
1250 const char* const url;
1251 bool result;
1252 } url_data[] = {
1253 { "http://bar/search?q=foo&oq=foo", true, },
1254 { "http://bar/?q=foo&oq=foo", true, },
1255 { "http://bar/#output=search&q=foo&oq=foo", true, },
1256 { "http://bar/webhp#q=foo&oq=foo", true, },
1257 { "http://bar/#q=foo&oq=foo", true, },
1258 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1259 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1260 { "http://bar/", false, },
1261 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161262 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191263 };
1264
1265 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_data); ++i) {
1266 EXPECT_EQ(url_data[i].result,
1267 search_provider.IsSearchURL(GURL(url_data[i].url)));
1268 }
1269}
[email protected]a048de8a2013-10-02 18:30:061270
1271TEST_F(TemplateURLTest, ReflectsBookmarkBarPinned) {
1272 TemplateURLData data;
1273 data.input_encodings.push_back("UTF-8");
1274 data.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
1275 TemplateURL url(NULL, data);
1276 EXPECT_TRUE(url.url_ref().IsValid());
1277 ASSERT_TRUE(url.url_ref().SupportsReplacement());
1278 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1279
1280 // Do not add the param when InstantExtended is suppressed on SRPs.
1281 url.url_ref_.showing_search_terms_ = false;
1282 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1283 EXPECT_EQ("http://www.google.com/?q=foo", result);
1284
1285 // Add the param when InstantExtended is not suppressed on SRPs.
1286 url.url_ref_.showing_search_terms_ = true;
1287 search_terms_args.bookmark_bar_pinned = false;
1288 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1289 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result);
1290
1291 url.url_ref_.showing_search_terms_ = true;
1292 search_terms_args.bookmark_bar_pinned = true;
1293 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1294 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result);
1295}