blob: ef435ac629a261ca0aaa9e53c57c9416c8e16d03 [file] [log] [blame]
[email protected]63f021882014-07-11 03:14:161// Copyright 2014 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
avif57136c12015-12-25 23:27:455#include <stddef.h>
6
Robbie Gibson8532eaa2019-03-15 11:43:247#include "base/base64.h"
[email protected]12bd05872009-03-17 19:25:068#include "base/base_paths.h"
[email protected]56fa29592013-07-02 20:25:539#include "base/command_line.h"
a-v-y81695d0d2017-04-20 08:22:2210#include "base/i18n/case_conversion.h"
Avi Drissman2244c2d2018-12-25 23:08:0211#include "base/stl_util.h"
[email protected]2f3bc6512013-08-28 03:56:2712#include "base/strings/string_number_conversions.h"
[email protected]24a555b62013-06-10 22:01:1713#include "base/strings/string_util.h"
[email protected]e309f312013-06-07 21:50:0814#include "base/strings/utf_string_conversions.h"
John Abd-El-Malek9cf3d7f02018-07-27 02:40:3915#include "components/google/core/common/google_util.h"
[email protected]5c3c6e482014-06-23 09:47:1816#include "components/search_engines/search_engines_switches.h"
[email protected]798baa8e2014-06-20 05:42:4417#include "components/search_engines/search_terms_data.h"
[email protected]d550cb02014-06-25 06:48:1118#include "components/search_engines/template_url.h"
hashimoto3c831812014-08-25 07:40:2319#include "components/search_engines/testing_search_terms_data.h"
a-v-y02d3f50b2017-02-16 12:03:5520#include "net/base/url_util.h"
[email protected]d82443b2009-01-15 19:54:5621#include "testing/gtest/include/gtest/gtest.h"
Steven Holtef9d5ed62017-10-21 02:02:3022#include "third_party/metrics_proto/omnibox_event.pb.h"
23#include "third_party/metrics_proto/omnibox_input_type.pb.h"
[email protected]d82443b2009-01-15 19:54:5624
[email protected]f911df52013-12-24 23:24:2325using base::ASCIIToUTF16;
26
a-v-y81695d0d2017-04-20 08:22:2227namespace {
28bool IsLowerCase(const base::string16& str) {
29 return str == base::i18n::ToLower(str);
30}
31}
32
[email protected]583844c2011-08-27 00:38:3533class TemplateURLTest : public testing::Test {
[email protected]d82443b2009-01-15 19:54:5634 public:
[email protected]798baa8e2014-06-20 05:42:4435 TemplateURLTest() : search_terms_data_("http://www.google.com/") {}
[email protected]b37bdfe2012-03-16 20:53:2736 void CheckSuggestBaseURL(const std::string& base_url,
37 const std::string& base_suggest_url) const;
[email protected]ce7ee5f2014-06-16 23:41:1938
vitbardeb285392015-02-20 14:02:5539 static void ExpectPostParamIs(
40 const TemplateURLRef::PostParam& param,
41 const std::string& name,
42 const std::string& value,
43 const std::string& content_type = std::string());
44
hashimoto3c831812014-08-25 07:40:2345 TestingSearchTermsData search_terms_data_;
[email protected]d82443b2009-01-15 19:54:5646};
47
[email protected]b37bdfe2012-03-16 20:53:2748void TemplateURLTest::CheckSuggestBaseURL(
49 const std::string& base_url,
50 const std::string& base_suggest_url) const {
hashimoto3c831812014-08-25 07:40:2351 TestingSearchTermsData search_terms_data(base_url);
[email protected]b37bdfe2012-03-16 20:53:2752 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue());
53}
54
vitbardeb285392015-02-20 14:02:5555// static
56void TemplateURLTest::ExpectPostParamIs(const TemplateURLRef::PostParam& param,
57 const std::string& name,
58 const std::string& value,
59 const std::string& content_type) {
60 EXPECT_EQ(name, param.name);
61 EXPECT_EQ(value, param.value);
62 EXPECT_EQ(content_type, param.content_type);
63}
64
[email protected]d82443b2009-01-15 19:54:5665TEST_F(TemplateURLTest, Defaults) {
[email protected]573889f22012-04-07 01:31:5466 TemplateURLData data;
[email protected]573889f22012-04-07 01:31:5467 EXPECT_FALSE(data.safe_for_autoreplace);
68 EXPECT_EQ(0, data.prepopulate_id);
[email protected]d82443b2009-01-15 19:54:5669}
70
71TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]573889f22012-04-07 01:31:5472 TemplateURLData data;
73 data.SetURL("{searchTerms}");
[email protected]168d08722014-06-18 07:13:2874 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1975 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:5676}
77
78TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1979 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1980 const char* url;
[email protected]0085863a2013-12-06 21:19:0381 const base::string16 terms;
[email protected]34e24852012-01-31 18:43:5882 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:1983 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:3084 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1685 "http://foosea%20rch/bar" },
[email protected]400b133f2011-01-19 18:32:3086 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1687 "http://foosea%20rch/bar?boo=abc" },
[email protected]400b133f2011-01-19 18:32:3088 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1689 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]400b133f2011-01-19 18:32:3090 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
[email protected]c31a979c2012-05-31 23:57:1691 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:1992 };
Avi Drissman2244c2d2018-12-25 23:08:0293 for (size_t i = 0; i < base::size(search_term_cases); ++i) {
[email protected]0d2e6a62010-01-15 20:09:1994 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:5495 TemplateURLData data;
96 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:2897 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1998 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
99 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04100 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19101 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data_));
[email protected]c31a979c2012-05-31 23:57:16102 ASSERT_TRUE(result.is_valid());
103 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:19104 }
[email protected]d82443b2009-01-15 19:54:56105}
106
107TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:54108 TemplateURLData data;
109 data.SetURL("http://foo{searchTerms}{count?}");
[email protected]168d08722014-06-18 07:13:28110 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19111 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
112 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04113 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19114 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56115 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27116 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:56117}
118
119TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:54120 TemplateURLData data;
121 data.SetURL("http://foo{searchTerms}{count}");
[email protected]168d08722014-06-18 07:13:28122 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19123 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
124 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04125 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19126 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56127 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27128 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56129}
130
131TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54132 TemplateURLData data;
133 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]168d08722014-06-18 07:13:28134 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19135 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
136 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04137 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19138 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56139 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27140 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56141}
142
143TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54144 TemplateURLData data;
145 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[email protected]168d08722014-06-18 07:13:28146 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19147 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
148 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04149 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19150 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56151 ASSERT_TRUE(result.is_valid());
[email protected]405aae22012-03-29 20:36:13152 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56153}
154
155TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54156 TemplateURLData data;
157 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[email protected]168d08722014-06-18 07:13:28158 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19159 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
160 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04161 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19162 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56163 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27164 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56165}
166
[email protected]93b29062013-07-12 03:09:09167TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
168 const char kInvalidPostParamsString[] =
169 "unknown_template={UnknownTemplate},bad_value=bad{value},"
170 "{google:sbiSource}";
171 // List all accpectable parameter format in valid_post_params_string. it is
172 // expected like: "name0=,name1=value1,name2={template1}"
173 const char kValidPostParamsString[] =
174 "image_content={google:imageThumbnail},image_url={google:imageURL},"
175 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
Robbie Gibson8532eaa2019-03-15 11:43:24176 "constant_param=constant,width={google:imageOriginalWidth},"
177 "base64_image_content={google:imageThumbnailBase64}";
[email protected]93b29062013-07-12 03:09:09178 const char KImageSearchURL[] = "http://foo.com/sbi";
179
180 TemplateURLData data;
181 data.image_url = KImageSearchURL;
182
183 // Try to parse invalid post parameters.
184 data.image_url_post_params = kInvalidPostParamsString;
[email protected]168d08722014-06-18 07:13:28185 TemplateURL url_bad(data);
[email protected]ce7ee5f2014-06-16 23:41:19186 ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09187 const TemplateURLRef::PostParams& bad_post_params =
188 url_bad.image_url_ref().post_params_;
189 ASSERT_EQ(2U, bad_post_params.size());
vitbardeb285392015-02-20 14:02:55190 ExpectPostParamIs(bad_post_params[0], "unknown_template",
191 "{UnknownTemplate}");
192 ExpectPostParamIs(bad_post_params[1], "bad_value", "bad{value}");
[email protected]93b29062013-07-12 03:09:09193
194 // Try to parse valid post parameters.
195 data.image_url_post_params = kValidPostParamsString;
[email protected]168d08722014-06-18 07:13:28196 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19197 ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_));
198 ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09199
200 // Check term replacement.
201 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
202 search_args.image_thumbnail_content = "dummy-image-thumbnail";
203 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
[email protected]2f3bc6512013-08-28 03:56:27204 search_args.image_original_size = gfx::Size(10, 10);
[email protected]93b29062013-07-12 03:09:09205 // Replacement operation with no post_data buffer should still return
206 // the parsed URL.
hashimoto3c831812014-08-25 07:40:23207 TestingSearchTermsData search_terms_data("http://X");
[email protected]ce7ee5f2014-06-16 23:41:19208 GURL result(url.image_url_ref().ReplaceSearchTerms(
209 search_args, search_terms_data));
[email protected]93b29062013-07-12 03:09:09210 ASSERT_TRUE(result.is_valid());
211 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48212 TemplateURLRef::PostContent post_content;
[email protected]ce7ee5f2014-06-16 23:41:19213 result = GURL(url.image_url_ref().ReplaceSearchTerms(
[email protected]7c2bcc42013-08-01 04:03:48214 search_args, search_terms_data, &post_content));
[email protected]93b29062013-07-12 03:09:09215 ASSERT_TRUE(result.is_valid());
216 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48217 ASSERT_FALSE(post_content.first.empty());
218 ASSERT_FALSE(post_content.second.empty());
[email protected]93b29062013-07-12 03:09:09219
220 // Check parsed result of post parameters.
221 const TemplateURLRef::Replacements& replacements =
222 url.image_url_ref().replacements_;
223 const TemplateURLRef::PostParams& post_params =
224 url.image_url_ref().post_params_;
Robbie Gibson8532eaa2019-03-15 11:43:24225 EXPECT_EQ(8U, post_params.size());
jdoerrie3feb1852018-10-05 12:16:44226 for (auto i = post_params.begin(); i != post_params.end(); ++i) {
227 auto j = replacements.begin();
[email protected]93b29062013-07-12 03:09:09228 for (; j != replacements.end(); ++j) {
229 if (j->is_post_param && j->index ==
230 static_cast<size_t>(i - post_params.begin())) {
231 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27232 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
Raul Tambref88e5102019-02-06 10:54:03233 ExpectPostParamIs(
234 *i, "width",
235 base::NumberToString(search_args.image_original_size.width()));
[email protected]2f3bc6512013-08-28 03:56:27236 break;
[email protected]1020fead2014-06-20 13:40:28237 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE:
vitbardeb285392015-02-20 14:02:55238 ExpectPostParamIs(*i, "sbisrc",
239 search_terms_data.GoogleImageSearchSource());
[email protected]1020fead2014-06-20 13:40:28240 break;
[email protected]93b29062013-07-12 03:09:09241 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
vitbardeb285392015-02-20 14:02:55242 ExpectPostParamIs(*i, "image_content",
243 search_args.image_thumbnail_content,
244 "image/jpeg");
[email protected]93b29062013-07-12 03:09:09245 break;
Robbie Gibson8532eaa2019-03-15 11:43:24246 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL_BASE64: {
247 std::string base64_image_content;
248 base::Base64Encode(search_args.image_thumbnail_content,
249 &base64_image_content);
250 ExpectPostParamIs(*i, "base64_image_content", base64_image_content,
251 "image/jpeg");
252 break;
253 }
[email protected]93b29062013-07-12 03:09:09254 case TemplateURLRef::GOOGLE_IMAGE_URL:
vitbardeb285392015-02-20 14:02:55255 ExpectPostParamIs(*i, "image_url", search_args.image_url.spec());
[email protected]93b29062013-07-12 03:09:09256 break;
257 case TemplateURLRef::LANGUAGE:
vitbardeb285392015-02-20 14:02:55258 ExpectPostParamIs(*i, "language", "en");
[email protected]93b29062013-07-12 03:09:09259 break;
260 default:
261 ADD_FAILURE(); // Should never go here.
262 }
263 break;
264 }
265 }
266 if (j != replacements.end())
267 continue;
vitbardeb285392015-02-20 14:02:55268 if (i->name == "empty_param")
269 ExpectPostParamIs(*i, "empty_param", std::string());
270 else
271 ExpectPostParamIs(*i, "constant_param", "constant");
[email protected]93b29062013-07-12 03:09:09272 }
273}
274
Peter Kastinge59f0c32017-11-29 05:56:22275TEST_F(TemplateURLTest, ImageURLWithGetShouldNotCrash) {
276 TemplateURLData data;
277 data.SetURL("http://foo/?q={searchTerms}&t={google:imageThumbnail}");
278 TemplateURL url(data);
279 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
280 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
281
282 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
283 search_args.image_thumbnail_content = "dummy-image-thumbnail";
284 GURL result(
285 url.url_ref().ReplaceSearchTerms(search_args, search_terms_data_));
286 ASSERT_TRUE(result.is_valid());
287 EXPECT_EQ("http://foo/?q=X&t=dummy-image-thumbnail", result.spec());
288}
289
[email protected]d88cb202011-08-17 20:03:01290// Test that setting the prepopulate ID from TemplateURL causes the stored
291// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44292TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54293 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20294 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]168d08722014-06-18 07:13:28295 TemplateURL url(data);
[email protected]1a257262011-06-28 22:15:44296 TemplateURLRef::Replacements replacements;
297 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:24298 EXPECT_EQ("http://foo{fhqwhgads}bar",
299 url.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements,
300 nullptr, &valid));
[email protected]1a257262011-06-28 22:15:44301 EXPECT_TRUE(replacements.empty());
302 EXPECT_TRUE(valid);
303
[email protected]573889f22012-04-07 01:31:54304 data.prepopulate_id = 123;
[email protected]168d08722014-06-18 07:13:28305 TemplateURL url2(data);
Ivan Kotenkov75b1c3a2017-10-24 14:47:24306 EXPECT_EQ("http://foobar",
307 url2.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements,
308 nullptr, &valid));
[email protected]1a257262011-06-28 22:15:44309 EXPECT_TRUE(replacements.empty());
310 EXPECT_TRUE(valid);
311}
312
vitbar90902c42016-05-11 06:16:55313// Test that setting the prepopulate ID from TemplateURL causes the stored
314// TemplateURLRef to handle parsing the URL parameters differently.
315TEST_F(TemplateURLTest, SetPrepopulatedAndReplace) {
316 TemplateURLData data;
317 data.SetURL("http://foo{fhqwhgads}search/?q={searchTerms}");
318 data.suggestions_url = "http://foo{fhqwhgads}suggest/?q={searchTerms}";
vitbar90902c42016-05-11 06:16:55319 data.image_url = "http://foo{fhqwhgads}image/";
320 data.new_tab_url = "http://foo{fhqwhgads}newtab/";
321 data.contextual_search_url = "http://foo{fhqwhgads}context/";
322 data.alternate_urls.push_back(
323 "http://foo{fhqwhgads}alternate/?q={searchTerms}");
324
325 TemplateURLRef::SearchTermsArgs args(base::ASCIIToUTF16("X"));
326 const SearchTermsData& stdata = search_terms_data_;
327
328 TemplateURL url(data);
329 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
330 url.url_ref().ReplaceSearchTerms(args, stdata));
331 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dalternate/?q=X",
332 url.url_refs()[0].ReplaceSearchTerms(args, stdata));
333 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
334 url.url_refs()[1].ReplaceSearchTerms(args, stdata));
335 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsuggest/?q=X",
336 url.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
vitbar90902c42016-05-11 06:16:55337 EXPECT_EQ("http://foo{fhqwhgads}image/",
338 url.image_url_ref().ReplaceSearchTerms(args, stdata));
339 EXPECT_EQ("http://foo{fhqwhgads}newtab/",
340 url.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
341 EXPECT_EQ("http://foo{fhqwhgads}context/",
342 url.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
343
344 data.prepopulate_id = 123;
345 TemplateURL url2(data);
346 EXPECT_EQ("http://foosearch/?q=X",
347 url2.url_ref().ReplaceSearchTerms(args, stdata));
348 EXPECT_EQ("http://fooalternate/?q=X",
349 url2.url_refs()[0].ReplaceSearchTerms(args, stdata));
350 EXPECT_EQ("http://foosearch/?q=X",
351 url2.url_refs()[1].ReplaceSearchTerms(args, stdata));
352 EXPECT_EQ("http://foosuggest/?q=X",
353 url2.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
vitbar90902c42016-05-11 06:16:55354 EXPECT_EQ("http://fooimage/",
355 url2.image_url_ref().ReplaceSearchTerms(args, stdata));
356 EXPECT_EQ("http://foonewtab/",
357 url2.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
358 EXPECT_EQ("http://foocontext/",
359 url2.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
360}
361
[email protected]d82443b2009-01-15 19:54:56362TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54363 TemplateURLData data;
364 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]168d08722014-06-18 07:13:28365 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19366 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
367 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04368 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19369 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56370 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27371 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56372}
373
374TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54375 TemplateURLData data;
376 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]168d08722014-06-18 07:13:28377 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19378 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
379 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04380 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19381 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56382 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27383 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56384}
385
[email protected]375bd7312010-08-30 22:18:13386TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
387 struct SearchTermsCase {
388 const char* url;
[email protected]0085863a2013-12-06 21:19:03389 const base::string16 terms;
[email protected]375bd7312010-08-30 22:18:13390 const char* output;
391 } search_term_cases[] = {
[email protected]0085863a2013-12-06 21:19:03392 { "{google:baseURL}{language}{searchTerms}", base::string16(),
[email protected]b37bdfe2012-03-16 20:53:27393 "http://example.com/e/en" },
[email protected]0085863a2013-12-06 21:19:03394 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
[email protected]014010e2011-10-01 04:12:44395 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13396 };
397
hashimoto3c831812014-08-25 07:40:23398 TestingSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54399 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02400 for (size_t i = 0; i < base::size(search_term_cases); ++i) {
[email protected]375bd7312010-08-30 22:18:13401 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54402 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:28403 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19404 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data));
405 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data));
406 GURL result(url.url_ref().ReplaceSearchTerms(
Ivan Kotenkov75b1c3a2017-10-24 14:47:24407 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data,
408 nullptr));
[email protected]375bd7312010-08-30 22:18:13409 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27410 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13411 }
412}
413
[email protected]d82443b2009-01-15 19:54:56414TEST_F(TemplateURLTest, URLRefTermToWide) {
415 struct ToWideCase {
416 const char* encoded_search_term;
[email protected]0085863a2013-12-06 21:19:03417 const base::string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56418 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30419 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56420 // Test some big-5 input.
[email protected]f911df52013-12-24 23:24:23421 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56422 // Test some UTF-8 input. We should fall back to this when the encoding
423 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
424 // Big-5 trailing byte.
[email protected]f911df52013-12-24 23:24:23425 {"%e4%bd%a05%e5%a5%bd+to+you",
426 base::WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56427 // Undecodable input should stay escaped.
[email protected]f911df52013-12-24 23:24:23428 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44429 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30430 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44431 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30432 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56433 };
434
[email protected]d82443b2009-01-15 19:54:56435 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54436 TemplateURLData data;
437 data.SetURL("http://foo?q={searchTerms}");
438 data.input_encodings.push_back("big-5");
[email protected]168d08722014-06-18 07:13:28439 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19440 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
441 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Avi Drissman2244c2d2018-12-25 23:08:02442 for (size_t i = 0; i < base::size(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07443 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13444 url.url_ref().SearchTermToString16(
445 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56446 }
447}
448
[email protected]d82443b2009-01-15 19:54:56449TEST_F(TemplateURLTest, DisplayURLToURLRef) {
450 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19451 const std::string url;
[email protected]0085863a2013-12-06 21:19:03452 const base::string16 expected_result;
[email protected]b37bdfe2012-03-16 20:53:27453 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19454 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30455 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19456 { "http://X",
[email protected]400b133f2011-01-19 18:32:30457 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19458 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30459 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19460 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30461 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56462 };
[email protected]573889f22012-04-07 01:31:54463 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02464 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54465 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28466 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19467 EXPECT_EQ(test_data[i].expected_result,
468 url.url_ref().DisplayURL(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27469 EXPECT_EQ(test_data[i].url,
[email protected]ce7ee5f2014-06-16 23:41:19470 TemplateURLRef::DisplayURLToURLRef(
471 url.url_ref().DisplayURL(search_terms_data_)));
[email protected]d82443b2009-01-15 19:54:56472 }
473}
474
475TEST_F(TemplateURLTest, ReplaceSearchTerms) {
476 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19477 const std::string url;
[email protected]d82443b2009-01-15 19:54:56478 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27479 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19480 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56481 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19482 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56483 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19484 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56485 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19486 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56487 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19488 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56489 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19490 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56491 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19492 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56493 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19494 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56495 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19496 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56497 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19498 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56499 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19500 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56501 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19502 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56503 "http://foo/UTF-8a{language}aXa" },
504 };
[email protected]573889f22012-04-07 01:31:54505 TemplateURLData data;
506 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02507 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54508 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28509 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19510 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
511 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27512 std::string expected_result = test_data[i].expected_result;
brettwe6dae462015-06-24 20:54:45513 base::ReplaceSubstringsAfterOffset(
514 &expected_result, 0, "{language}",
515 search_terms_data_.GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04516 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19517 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
518 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27519 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56520 EXPECT_EQ(expected_result, result.spec());
521 }
522}
523
524
525// Tests replacing search terms in various encodings and making sure the
526// generated URL matches the expected value.
527TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
528 struct TestData {
529 const std::string encoding;
[email protected]0085863a2013-12-06 21:19:03530 const base::string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19531 const std::string url;
[email protected]d82443b2009-01-15 19:54:56532 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27533 } test_data[] = {
Alexander Yashkin874b919d2018-02-17 15:11:18534 {"BIG5", base::WideToUTF16(L"\x60BD"),
535 "http://foo/?{searchTerms}{inputEncoding}", "http://foo/?%B1~BIG5"},
536 {"UTF-8", ASCIIToUTF16("blah"),
537 "http://foo/?{searchTerms}{inputEncoding}", "http://foo/?blahUTF-8"},
538 {"Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
539 "http://foo/{searchTerms}/bar", "http://foo/%82%A0/bar"},
540 {"Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
541 "http://foo/{searchTerms}/bar", "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56542 };
[email protected]573889f22012-04-07 01:31:54543 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02544 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54545 data.SetURL(test_data[i].url);
546 data.input_encodings.clear();
547 data.input_encodings.push_back(test_data[i].encoding);
[email protected]168d08722014-06-18 07:13:28548 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19549 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
550 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04551 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19552 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
553 search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04554 ASSERT_TRUE(result.is_valid());
555 EXPECT_EQ(test_data[i].expected_result, result.spec());
556 }
557}
558
Alexander Yashkin874b919d2018-02-17 15:11:18559// Test that encoding with several optional codepages works as intended.
560// Codepages are tried in order, fallback is UTF-8.
561TEST_F(TemplateURLTest, ReplaceSearchTermsMultipleEncodings) {
562 struct TestData {
563 const std::vector<std::string> encodings;
564 const base::string16 search_term;
565 const std::string url;
566 const std::string expected_result;
567 } test_data[] = {
568 // First and third encodings are valid. First is used.
569 {{"windows-1251", "cp-866", "UTF-8"},
570 base::UTF8ToUTF16("\xD1\x8F"),
571 "http://foo/?{searchTerms}{inputEncoding}",
572 "http://foo/?%FFwindows-1251"},
573 // Second and third encodings are valid, second is used.
574 {{"cp-866", "GB2312", "UTF-8"},
575 base::UTF8ToUTF16("\xE7\x8B\x97"),
576 "http://foo/?{searchTerms}{inputEncoding}",
577 "http://foo/?%B9%B7GB2312"},
578 // Second and third encodings are valid in another order, second is used.
579 {{"cp-866", "UTF-8", "GB2312"},
580 base::UTF8ToUTF16("\xE7\x8B\x97"),
581 "http://foo/?{searchTerms}{inputEncoding}",
582 "http://foo/?%E7%8B%97UTF-8"},
583 // Both encodings are invalid, fallback to UTF-8.
584 {{"cp-866", "windows-1251"},
585 base::UTF8ToUTF16("\xE7\x8B\x97"),
586 "http://foo/?{searchTerms}{inputEncoding}",
587 "http://foo/?%E7%8B%97UTF-8"},
588 // No encodings are given, fallback to UTF-8.
589 {{},
590 base::UTF8ToUTF16("\xE7\x8B\x97"),
591 "http://foo/?{searchTerms}{inputEncoding}",
592 "http://foo/?%E7%8B%97UTF-8"},
593 };
594
595 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02596 for (size_t i = 0; i < base::size(test_data); ++i) {
Alexander Yashkin874b919d2018-02-17 15:11:18597 data.SetURL(test_data[i].url);
598 data.input_encodings = test_data[i].encodings;
599 TemplateURL url(data);
600 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
601 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
602 GURL result(url.url_ref().ReplaceSearchTerms(
603 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
604 search_terms_data_));
605 ASSERT_TRUE(result.is_valid());
606 EXPECT_EQ(test_data[i].expected_result, result.spec());
607 }
608}
609
[email protected]bca359b2012-06-24 07:53:04610// Tests replacing assisted query stats (AQS) in various scenarios.
611TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
612 struct TestData {
[email protected]0085863a2013-12-06 21:19:03613 const base::string16 search_term;
[email protected]bca359b2012-06-24 07:53:04614 const std::string aqs;
615 const std::string base_url;
616 const std::string url;
617 const std::string expected_result;
618 } test_data[] = {
619 // No HTTPS, no AQS.
620 { ASCIIToUTF16("foo"),
621 "chrome.0.0l6",
622 "http://foo/",
623 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
624 "http://foo/?foo" },
625 // HTTPS available, AQS should be replaced.
626 { ASCIIToUTF16("foo"),
627 "chrome.0.0l6",
628 "https://foo/",
629 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
630 "https://foo/?fooaqs=chrome.0.0l6&" },
631 // HTTPS available, however AQS is empty.
632 { ASCIIToUTF16("foo"),
633 "",
634 "https://foo/",
635 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
636 "https://foo/?foo" },
637 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
638 { ASCIIToUTF16("foo"),
639 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44640 "http://www.google.com",
[email protected]bca359b2012-06-24 07:53:04641 "http://foo?{searchTerms}{google:assistedQueryStats}",
642 "http://foo/?foo" },
643 // A non-Google search provider with HTTPS should allow AQS.
644 { ASCIIToUTF16("foo"),
645 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44646 "https://www.google.com",
[email protected]bca359b2012-06-24 07:53:04647 "https://foo?{searchTerms}{google:assistedQueryStats}",
648 "https://foo/?fooaqs=chrome.0.0l6&" },
649 };
650 TemplateURLData data;
651 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02652 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04653 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28654 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19655 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
656 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04657 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
658 search_terms_args.assisted_query_stats = test_data[i].aqs;
[email protected]798baa8e2014-06-20 05:42:44659 search_terms_data_.set_google_base_url(test_data[i].base_url);
[email protected]ce7ee5f2014-06-16 23:41:19660 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
661 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27662 ASSERT_TRUE(result.is_valid());
663 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56664 }
665}
666
[email protected]00790562012-12-14 09:57:16667// Tests replacing cursor position.
668TEST_F(TemplateURLTest, ReplaceCursorPosition) {
669 struct TestData {
[email protected]0085863a2013-12-06 21:19:03670 const base::string16 search_term;
[email protected]00790562012-12-14 09:57:16671 size_t cursor_position;
672 const std::string url;
673 const std::string expected_result;
674 } test_data[] = {
675 { ASCIIToUTF16("foo"),
[email protected]0085863a2013-12-06 21:19:03676 base::string16::npos,
[email protected]00790562012-12-14 09:57:16677 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
678 "http://www.google.com/?foo&" },
679 { ASCIIToUTF16("foo"),
680 2,
681 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
682 "http://www.google.com/?foo&cp=2&" },
683 { ASCIIToUTF16("foo"),
684 15,
685 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
686 "http://www.google.com/?foo&cp=15&" },
687 };
688 TemplateURLData data;
689 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02690 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]00790562012-12-14 09:57:16691 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28692 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19693 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
694 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]00790562012-12-14 09:57:16695 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
696 search_terms_args.cursor_position = test_data[i].cursor_position;
[email protected]ce7ee5f2014-06-16 23:41:19697 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
698 search_terms_data_));
[email protected]00790562012-12-14 09:57:16699 ASSERT_TRUE(result.is_valid());
700 EXPECT_EQ(test_data[i].expected_result, result.spec());
701 }
702}
703
[email protected]420472b22014-06-10 13:34:43704// Tests replacing input type (&oit=).
705TEST_F(TemplateURLTest, ReplaceInputType) {
706 struct TestData {
707 const base::string16 search_term;
Steven Holte3696c9412017-08-24 18:38:03708 metrics::OmniboxInputType input_type;
[email protected]420472b22014-06-10 13:34:43709 const std::string url;
710 const std::string expected_result;
711 } test_data[] = {
Steven Holte3696c9412017-08-24 18:38:03712 {ASCIIToUTF16("foo"), metrics::OmniboxInputType::UNKNOWN,
713 "{google:baseURL}?{searchTerms}&{google:inputType}",
714 "http://www.google.com/?foo&oit=1&"},
715 {ASCIIToUTF16("foo"), metrics::OmniboxInputType::URL,
716 "{google:baseURL}?{searchTerms}&{google:inputType}",
717 "http://www.google.com/?foo&oit=3&"},
[email protected]420472b22014-06-10 13:34:43718 };
[email protected]420472b22014-06-10 13:34:43719 TemplateURLData data;
720 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02721 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]420472b22014-06-10 13:34:43722 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28723 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19724 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
725 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43726 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
727 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19728 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
729 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43730 ASSERT_TRUE(result.is_valid());
731 EXPECT_EQ(test_data[i].expected_result, result.spec());
732 }
733}
734
Tommy C. Li9a180482019-06-27 20:49:00735// Tests replacing omnibox focus type (&oft=).
736TEST_F(TemplateURLTest, ReplaceOmniboxFocusType) {
737 struct TestData {
738 const base::string16 search_term;
Tommy Li5e883512020-08-07 19:32:04739 OmniboxFocusType focus_type;
Tommy C. Li9a180482019-06-27 20:49:00740 const std::string url;
741 const std::string expected_result;
742 } test_data[] = {
Tommy Li5e883512020-08-07 19:32:04743 {ASCIIToUTF16("foo"), OmniboxFocusType::DEFAULT,
Tommy C. Li9a180482019-06-27 20:49:00744 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
745 "http://www.google.com/?foo&"},
Tommy Li5e883512020-08-07 19:32:04746 {ASCIIToUTF16("foo"), OmniboxFocusType::ON_FOCUS,
Tommy C. Li9a180482019-06-27 20:49:00747 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
748 "http://www.google.com/?foo&oft=1&"},
Tommy Li5e883512020-08-07 19:32:04749 {ASCIIToUTF16("foo"), OmniboxFocusType::DELETED_PERMANENT_TEXT,
750 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
751 "http://www.google.com/?foo&oft=2&"},
Tommy C. Li9a180482019-06-27 20:49:00752 };
753 TemplateURLData data;
754 data.input_encodings.push_back("UTF-8");
755 for (size_t i = 0; i < base::size(test_data); ++i) {
756 data.SetURL(test_data[i].url);
757 TemplateURL url(data);
758 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
759 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
760 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
Tommy Li5e883512020-08-07 19:32:04761 search_terms_args.focus_type = test_data[i].focus_type;
Tommy C. Li9a180482019-06-27 20:49:00762 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
763 search_terms_data_));
764 ASSERT_TRUE(result.is_valid());
765 EXPECT_EQ(test_data[i].expected_result, result.spec());
766 }
767}
768
[email protected]9b9fa672013-11-07 06:04:52769// Tests replacing currentPageUrl.
770TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48771 struct TestData {
[email protected]0085863a2013-12-06 21:19:03772 const base::string16 search_term;
[email protected]9b9fa672013-11-07 06:04:52773 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48774 const std::string url;
775 const std::string expected_result;
776 } test_data[] = {
777 { ASCIIToUTF16("foo"),
778 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52779 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48780 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
781 { ASCIIToUTF16("foo"),
782 "",
[email protected]9b9fa672013-11-07 06:04:52783 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48784 "http://www.google.com/?foo&" },
785 { ASCIIToUTF16("foo"),
786 "http://g.com/+-/*&=",
[email protected]9b9fa672013-11-07 06:04:52787 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48788 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
789 };
790 TemplateURLData data;
791 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02792 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]800569d2013-05-06 07:38:48793 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28794 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19795 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
796 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]800569d2013-05-06 07:38:48797 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52798 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19799 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
800 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48801 ASSERT_TRUE(result.is_valid());
802 EXPECT_EQ(test_data[i].expected_result, result.spec());
803 }
804}
805
Pavel Yatsuk0d6a7b5c2019-10-09 20:33:44806// Tests appending attribution parameter to queries originating from Play API
807// search engine.
808TEST_F(TemplateURLTest, PlayAPIAttribution) {
809 const struct TestData {
810 const char* url;
811 base::string16 terms;
812 bool created_from_play_api;
813 const char* output;
814 } test_data[] = {{"http://foo/?q={searchTerms}", ASCIIToUTF16("bar"), false,
815 "http://foo/?q=bar"},
816 {"http://foo/?q={searchTerms}", ASCIIToUTF16("bar"), true,
817 "http://foo/?q=bar&chrome_dse_attribution=1"}};
818 TemplateURLData data;
819 for (size_t i = 0; i < base::size(test_data); ++i) {
820 data.SetURL(test_data[i].url);
821 data.created_from_play_api = test_data[i].created_from_play_api;
822 TemplateURL url(data);
823 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
824 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
825 GURL result(url.url_ref().ReplaceSearchTerms(
826 TemplateURLRef::SearchTermsArgs(test_data[i].terms),
827 search_terms_data_));
828 ASSERT_TRUE(result.is_valid());
829 EXPECT_EQ(test_data[i].output, result.spec());
830 }
831}
832
[email protected]d82443b2009-01-15 19:54:56833TEST_F(TemplateURLTest, Suggestions) {
834 struct TestData {
835 const int accepted_suggestion;
[email protected]0085863a2013-12-06 21:19:03836 const base::string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56837 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27838 } test_data[] = {
[email protected]0085863a2013-12-06 21:19:03839 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
[email protected]29653892013-03-02 19:25:37840 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30841 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37842 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03843 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(),
[email protected]29653892013-03-02 19:25:37844 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30845 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37846 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03847 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
[email protected]29653892013-03-02 19:25:37848 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56849 };
[email protected]573889f22012-04-07 01:31:54850 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37851 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
852 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54853 data.input_encodings.push_back("UTF-8");
[email protected]168d08722014-06-18 07:13:28854 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19855 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
856 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Avi Drissman2244c2d2018-12-25 23:08:02857 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04858 TemplateURLRef::SearchTermsArgs search_terms_args(
859 ASCIIToUTF16("foobar"));
860 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
861 search_terms_args.original_query =
862 test_data[i].original_query_for_suggestion;
[email protected]ce7ee5f2014-06-16 23:41:19863 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
864 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27865 ASSERT_TRUE(result.is_valid());
866 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56867 }
868}
869
[email protected]81f808de2009-09-25 01:36:34870TEST_F(TemplateURLTest, RLZ) {
[email protected]798baa8e2014-06-20 05:42:44871 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(false);
[email protected]d82443b2009-01-15 19:54:56872
[email protected]573889f22012-04-07 01:31:54873 TemplateURLData data;
874 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28875 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19876 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
877 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04878 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19879 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56880 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44881 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
882 result.spec());
[email protected]d82443b2009-01-15 19:54:56883}
884
[email protected]c8ccc41d2014-04-10 04:42:12885TEST_F(TemplateURLTest, RLZFromAppList) {
[email protected]798baa8e2014-06-20 05:42:44886 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(true);
[email protected]c8ccc41d2014-04-10 04:42:12887
888 TemplateURLData data;
889 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28890 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19891 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
892 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12893 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
Moe Ahmadi4715d9542020-10-21 01:00:32894 args.request_source = TemplateURLRef::CROS_APP_LIST;
[email protected]ce7ee5f2014-06-16 23:41:19895 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12896 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44897 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
898 result.spec());
[email protected]c8ccc41d2014-04-10 04:42:12899}
[email protected]c8ccc41d2014-04-10 04:42:12900
[email protected]d82443b2009-01-15 19:54:56901TEST_F(TemplateURLTest, HostAndSearchTermKey) {
902 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19903 const std::string url;
[email protected]d82443b2009-01-15 19:54:56904 const std::string host;
905 const std::string path;
906 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54907 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50908 {"http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
909 {"http://blah/{searchTerms}", "blah", "", ""},
[email protected]d82443b2009-01-15 19:54:56910
vitbarf2a11562017-05-15 14:09:50911 // No term should result in empty values.
912 {"http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56913
vitbarf2a11562017-05-15 14:09:50914 // Multiple terms should result in empty values.
915 {"http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56916
vitbarf2a11562017-05-15 14:09:50917 // Term in the host shouldn't match.
918 {"http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56919
vitbarf2a11562017-05-15 14:09:50920 {"http://blah/?q={searchTerms}", "blah", "/", "q"},
921 {"https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56922
vitbarf2a11562017-05-15 14:09:50923 // Single term with extra chars in value should match.
924 {"http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56925 };
926
Avi Drissman2244c2d2018-12-25 23:08:02927 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54928 TemplateURLData data;
929 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28930 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19931 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
932 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
933 EXPECT_EQ(test_data[i].search_term_key,
934 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56935 }
936}
937
alexmos294849f2015-03-14 00:55:06938TEST_F(TemplateURLTest, SearchTermKeyLocation) {
939 struct TestData {
940 const std::string url;
941 const url::Parsed::ComponentType location;
942 const std::string path;
vitbarf2a11562017-05-15 14:09:50943 const std::string key;
944 const std::string value_prefix;
945 const std::string value_suffix;
alexmos294849f2015-03-14 00:55:06946 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50947 {"http://blah/{searchTerms}/", url::Parsed::PATH, "", "", "/", "/"},
948 {"http://blah/{searchTerms}", url::Parsed::PATH, "", "", "/", ""},
949 {"http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "", "",
950 "/begin/", "/end"},
951 {"http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY, "/", "q",
952 "", ""},
953 {"http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF, "/", "x",
954 "", ""},
955 {"http://www.example.com/?q=chromium-{searchTerms}@chromium.org/info",
956 url::Parsed::QUERY, "/", "q", "chromium-", "@chromium.org/info"},
alexmos294849f2015-03-14 00:55:06957
vitbarf2a11562017-05-15 14:09:50958 // searchTerms is a key, not a value, so this should result in an empty
959 // value.
960 {"http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x",
961 url::Parsed::QUERY, "", "", "", ""},
alexmos294849f2015-03-14 00:55:06962
vitbarf2a11562017-05-15 14:09:50963 // Multiple search terms should result in empty values.
964 {"http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY, "", "",
965 "", ""},
966 {"http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY, "", "",
967 "", ""},
968 {"http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY, "",
969 "", "", ""},
alexmos294849f2015-03-14 00:55:06970 };
971
Avi Drissman2244c2d2018-12-25 23:08:02972 for (size_t i = 0; i < base::size(test_data); ++i) {
alexmos294849f2015-03-14 00:55:06973 TemplateURLData data;
974 data.SetURL(test_data[i].url);
975 TemplateURL url(data);
976 EXPECT_EQ(test_data[i].location,
977 url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
978 EXPECT_EQ(test_data[i].path,
979 url.url_ref().GetPath(search_terms_data_));
vitbarf2a11562017-05-15 14:09:50980 EXPECT_EQ(test_data[i].key,
981 url.url_ref().GetSearchTermKey(search_terms_data_));
982 EXPECT_EQ(test_data[i].value_prefix,
983 url.url_ref().GetSearchTermValuePrefix(search_terms_data_));
984 EXPECT_EQ(test_data[i].value_suffix,
985 url.url_ref().GetSearchTermValueSuffix(search_terms_data_));
alexmos294849f2015-03-14 00:55:06986 }
987}
988
[email protected]d82443b2009-01-15 19:54:56989TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
990 static const struct {
[email protected]ddd231e2010-06-29 20:35:19991 const char* const base_url;
992 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56993 } data[] = {
[email protected]014010e2011-10-01 04:12:44994 { "http://google.com/", "http://google.com/complete/", },
995 { "http://www.google.com/", "http://www.google.com/complete/", },
996 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
997 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
998 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56999 };
1000
Avi Drissman2244c2d2018-12-25 23:08:021001 for (size_t i = 0; i < base::size(data); ++i)
[email protected]d82443b2009-01-15 19:54:561002 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
1003}
1004
[email protected]81c6ef62010-01-21 09:58:471005TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:191006 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:541007 TemplateURLData data;
1008 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:281009 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471010 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:131011 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:191012 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:471013 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271014 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471015 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1016}
1017
1018TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:201019 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:541020 TemplateURLData data;
1021 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:281022 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471023 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:441024
1025 // By default, TemplateURLRef should not consider itself prepopulated.
1026 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:131027 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:201028 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:441029 EXPECT_TRUE(replacements.empty());
1030
1031 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:201032 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:541033 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:281034 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:151035 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:201036 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:471037 EXPECT_TRUE(replacements.empty());
1038}
1039
1040TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:281041 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:471042 TemplateURLRef::Replacements replacements;
1043 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241044 EXPECT_EQ(std::string(), url.url_ref().ParseURL(std::string(), &replacements,
1045 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471046 EXPECT_TRUE(replacements.empty());
1047 EXPECT_TRUE(valid);
1048}
1049
1050TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:541051 TemplateURLData data;
1052 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:281053 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471054 TemplateURLRef::Replacements replacements;
1055 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241056 EXPECT_EQ(std::string(),
1057 url.url_ref().ParseURL("{", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471058 EXPECT_TRUE(replacements.empty());
1059 EXPECT_FALSE(valid);
1060}
1061
1062TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:541063 TemplateURLData data;
1064 data.SetURL("{}");
[email protected]168d08722014-06-18 07:13:281065 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471066 TemplateURLRef::Replacements replacements;
1067 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241068 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471069 EXPECT_TRUE(replacements.empty());
1070 EXPECT_TRUE(valid);
1071}
1072
1073TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:541074 TemplateURLData data;
1075 data.SetURL("{}{{%s}}");
[email protected]168d08722014-06-18 07:13:281076 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471077 TemplateURLRef::Replacements replacements;
1078 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241079 EXPECT_EQ("{}{}", url.url_ref().ParseURL("{}{{searchTerms}}", &replacements,
1080 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471081 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271082 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471083 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1084 EXPECT_TRUE(valid);
1085}
1086
1087TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:541088 TemplateURLData data;
1089 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:281090 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471091 TemplateURLRef::Replacements replacements;
1092 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241093 EXPECT_EQ("{", url.url_ref().ParseURL("{{searchTerms}", &replacements,
1094 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471095 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271096 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471097 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1098 EXPECT_TRUE(valid);
1099}
[email protected]5b3078752012-10-09 18:54:161100
[email protected]fd6d8822012-12-08 06:56:111101TEST_F(TemplateURLTest, SearchClient) {
1102 const std::string base_url_str("http://google.com/?");
1103 const std::string terms_str("{searchTerms}&{google:searchClient}");
1104 const std::string full_url_str = base_url_str + terms_str;
[email protected]0085863a2013-12-06 21:19:031105 const base::string16 terms(ASCIIToUTF16(terms_str));
[email protected]798baa8e2014-06-20 05:42:441106 search_terms_data_.set_google_base_url(base_url_str);
[email protected]fd6d8822012-12-08 06:56:111107
1108 TemplateURLData data;
1109 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:281110 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191111 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1112 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111113 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
1114
1115 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:191116 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1117 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111118 ASSERT_TRUE(result.is_valid());
1119 EXPECT_EQ("http://google.com/?foobar&", result.spec());
1120
1121 // Check that the URL is correct when a client is present.
[email protected]798baa8e2014-06-20 05:42:441122 search_terms_data_.set_search_client("search_client");
[email protected]ce7ee5f2014-06-16 23:41:191123 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
1124 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111125 ASSERT_TRUE(result_2.is_valid());
[email protected]798baa8e2014-06-20 05:42:441126 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
[email protected]fd6d8822012-12-08 06:56:111127}
[email protected]fd6d8822012-12-08 06:56:111128
Moe Ahmadi4715d9542020-10-21 01:00:321129TEST_F(TemplateURLTest, SuggestClient) {
1130 const std::string base_url_str("http://google.com/?");
1131 const std::string query_params_str("client={google:suggestClient}");
1132 const std::string full_url_str = base_url_str + query_params_str;
1133 search_terms_data_.set_google_base_url(base_url_str);
1134
1135 TemplateURLData data;
1136 data.SetURL(full_url_str);
1137 TemplateURL url(data);
1138 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1139 ASSERT_FALSE(url.url_ref().SupportsReplacement(search_terms_data_));
1140 TemplateURLRef::SearchTermsArgs search_terms_args;
1141
1142 // Check that the URL is correct when a client is not present.
1143 GURL result(
1144 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_));
1145 ASSERT_TRUE(result.is_valid());
1146 EXPECT_EQ("http://google.com/?client=", result.spec());
1147
1148 // Check that the URL is correct when a client is present.
1149 search_terms_data_.set_suggest_client("suggest_client");
1150 GURL result_2(
1151 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_));
1152 ASSERT_TRUE(result_2.is_valid());
1153 EXPECT_EQ("http://google.com/?client=suggest_client", result_2.spec());
1154
1155 // Check that the URL is correct when a suggest request is made from a
1156 // non-searchbox NTP surface.
1157 search_terms_args.request_source = TemplateURLRef::NON_SEARCHBOX_NTP;
1158 GURL result_3(
1159 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_));
1160 ASSERT_TRUE(result_3.is_valid());
1161 EXPECT_EQ("http://google.com/?client=suggest_client_from_ntp",
1162 result_3.spec());
1163}
1164
[email protected]5b3078752012-10-09 18:54:161165TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
1166 TemplateURLData data;
1167 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161168 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
1169 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281170 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511171 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1172 ASSERT_EQ(3U, url_refs.size());
1173 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1174 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1175 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161176}
1177
1178TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
1179 TemplateURLData data;
1180 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:281181 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511182 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1183 ASSERT_EQ(1U, url_refs.size());
1184 EXPECT_EQ("http://www.google.co.uk/", url_refs[0].GetURL());
[email protected]5b3078752012-10-09 18:54:161185}
1186
1187TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1188 TemplateURLData data;
1189 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161190 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1191 data.alternate_urls.push_back(
1192 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281193 TemplateURL url(data);
[email protected]0085863a2013-12-06 21:19:031194 base::string16 result;
[email protected]5b3078752012-10-09 18:54:161195
1196 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191197 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191198 EXPECT_EQ(ASCIIToUTF16("something"), result);
1199
1200 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471201 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191202 EXPECT_EQ(ASCIIToUTF16("something"), result);
1203
1204 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471205 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191206 EXPECT_EQ(ASCIIToUTF16("something"), result);
1207
1208 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471209 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191210 EXPECT_EQ(ASCIIToUTF16("something"), result);
1211
1212 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191213 GURL("http://google.com/alt/#q=something"),
1214 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191215 EXPECT_EQ(ASCIIToUTF16("something"), result);
1216
1217 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471218 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191219 EXPECT_EQ(ASCIIToUTF16("something"), result);
1220
1221 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471222 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191223 EXPECT_EQ(ASCIIToUTF16("something"), result);
1224
1225 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471226 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161227 EXPECT_EQ(ASCIIToUTF16("something"), result);
1228
1229 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191230 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371231 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161232
1233 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191234 GURL("http://google.ca/?q=something&q=anything"),
1235 search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371236 EXPECT_EQ(base::string16(), result);
[email protected]67d8b752013-04-03 17:33:271237
1238 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191239 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371240 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161241
[email protected]32e2d81b2012-10-16 19:03:251242 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191243 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
[email protected]32e2d81b2012-10-16 19:03:251244 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:161245
1246 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191247 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371248 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161249
1250 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191251 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161252 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1253
1254 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191255 GURL("http://google.com/alt/?q=123#q=456"),
1256 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161257 EXPECT_EQ(ASCIIToUTF16("456"), result);
1258
1259 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191260 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1261 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161262 EXPECT_EQ(ASCIIToUTF16("123"), result);
1263
1264 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191265 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1266 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161267 EXPECT_EQ(ASCIIToUTF16("789"), result);
1268
1269 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191270 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371271 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161272
1273 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191274 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371275 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161276
1277 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191278 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371279 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161280
1281 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191282 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371283 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161284
1285 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191286 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161287 EXPECT_EQ(ASCIIToUTF16("123"), result);
1288}
[email protected]4076ea62013-01-09 01:47:191289
alexmos294849f2015-03-14 00:55:061290TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1291 TemplateURLData data;
1292 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1293 TemplateURL url(data);
1294 base::string16 result;
1295
1296 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1297 GURL("http://term-in-path.com/begin/something/end"),
1298 search_terms_data_, &result));
1299 EXPECT_EQ(ASCIIToUTF16("something"), result);
1300
1301 // "%20" must be converted to space.
1302 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1303 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1304 search_terms_data_, &result));
1305 EXPECT_EQ(ASCIIToUTF16("a b c"), result);
1306
1307 // Plus must not be converted to space.
1308 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1309 GURL("http://term-in-path.com/begin/1+2+3/end"),
1310 search_terms_data_, &result));
1311 EXPECT_EQ(ASCIIToUTF16("1+2+3"), result);
1312
1313 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1314 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
1315 EXPECT_EQ(base::string16(), result);
1316
1317 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1318 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
1319 EXPECT_EQ(base::string16(), result);
1320
1321 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1322 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
1323 EXPECT_EQ(base::string16(), result);
1324}
1325
vitbar27804d102015-04-15 10:54:411326// Checks that the ExtractSearchTermsFromURL function works correctly
1327// for urls containing non-latin characters in UTF8 encoding.
1328TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
1329 TemplateURLData data;
1330 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1331 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1332 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1333 TemplateURL url(data);
1334 base::string16 result;
1335
1336 // Russian text encoded with UTF-8.
1337 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551338 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1339 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
vitbar27804d102015-04-15 10:54:411340 search_terms_data_, &result));
1341 EXPECT_EQ(
1342 base::WideToUTF16(
1343 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1344 L"\x043C\x0438\x0440!"),
1345 result);
1346
1347 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551348 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1349 "%D0%B0"),
vitbar27804d102015-04-15 10:54:411350 search_terms_data_, &result));
1351 EXPECT_EQ(
1352 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1353 result);
1354
1355 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551356 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1357 "%D0%B8%20A"),
vitbar27804d102015-04-15 10:54:411358 search_terms_data_, &result));
1359 EXPECT_EQ(
1360 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1361 result);
1362}
1363
1364// Checks that the ExtractSearchTermsFromURL function works correctly
1365// for urls containing non-latin characters in non-UTF8 encoding.
1366TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) {
1367 TemplateURLData data;
1368 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1369 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1370 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1371 data.input_encodings.push_back("windows-1251");
1372 TemplateURL url(data);
1373 base::string16 result;
1374
1375 // Russian text encoded with Windows-1251.
1376 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1377 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1378 "%EC%E8%F0!"),
1379 search_terms_data_, &result));
1380 EXPECT_EQ(
1381 base::WideToUTF16(
1382 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1383 L"\x043C\x0438\x0440!"),
1384 result);
1385
1386 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1387 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1388 search_terms_data_, &result));
1389 EXPECT_EQ(
1390 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1391 result);
1392
1393 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1394 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1395 search_terms_data_, &result));
1396 EXPECT_EQ(
1397 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1398 result);
1399}
1400
jbroman50832022016-04-21 23:53:001401// Checks that the ExtractSearchTermsFromURL function strips constant
1402// prefix/suffix strings from the search terms param.
1403TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) {
1404 TemplateURLData data;
1405 data.alternate_urls.push_back(
1406 "http://www.example.com/?q=chromium-{searchTerms}@chromium.org");
1407 data.alternate_urls.push_back(
1408 "http://www.example.com/chromium-{searchTerms}@chromium.org/info");
1409 TemplateURL url(data);
1410 base::string16 result;
1411
1412 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1413 GURL("http://www.example.com/[email protected]"),
1414 search_terms_data_, &result));
1415 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1416
1417 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1418 GURL("http://www.example.com/[email protected]/info"),
1419 search_terms_data_, &result));
1420 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1421
1422 // Don't match if the prefix and suffix aren't there.
1423 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1424 GURL("http://www.example.com/?q=invalid"), search_terms_data_, &result));
jbromane8b8728d2017-04-12 17:29:391425
1426 // Don't match if the prefix and suffix overlap.
1427 TemplateURLData data_with_overlap;
1428 data.alternate_urls.push_back(
1429 "http://www.example.com/?q=goo{searchTerms}oogle");
1430 TemplateURL url_with_overlap(data);
1431 EXPECT_FALSE(url_with_overlap.ExtractSearchTermsFromURL(
1432 GURL("http://www.example.com/?q=google"), search_terms_data_, &result));
jbroman50832022016-04-21 23:53:001433}
1434
[email protected]f62e30f52013-03-23 03:45:151435TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1436 TemplateURLData data;
1437 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]f62e30f52013-03-23 03:45:151438 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1439 data.alternate_urls.push_back(
1440 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281441 TemplateURL url(data);
[email protected]f62e30f52013-03-23 03:45:151442 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1443 GURL result;
1444
1445 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191446 GURL("http://google.com/?q=something"), search_terms,
1447 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101448 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151449
1450 result = GURL("http://should.not.change.com");
1451 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191452 GURL("http://google.ca/?q=something"), search_terms,
1453 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151454 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1455
1456 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191457 GURL("http://google.com/foo/?q=foo"), search_terms,
1458 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151459
1460 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191461 GURL("https://google.com/?q=foo"), search_terms,
1462 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101463 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151464
1465 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191466 GURL("http://google.com:8080/?q=foo"), search_terms,
1467 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151468
1469 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191470 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1471 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101472 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result);
[email protected]f62e30f52013-03-23 03:45:151473
1474 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1475 // template_url.cc for details.
1476 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191477 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1478 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101479 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151480
1481 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1482 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191483 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101484 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
[email protected]f62e30f52013-03-23 03:45:151485 result);
1486
1487 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1488 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191489 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151490 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
vitbarf298455d2015-04-21 12:58:101491 "#j=abc&q=Bob+Morane&h=def9"), result);
[email protected]f62e30f52013-03-23 03:45:151492
1493 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191494 GURL("http://google.com/alt/?q="), search_terms,
1495 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151496
1497 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191498 GURL("http://google.com/alt/?#q="), search_terms,
1499 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151500
1501 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191502 GURL("http://google.com/alt/?q=#q="), search_terms,
1503 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151504
1505 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191506 GURL("http://google.com/alt/?q=123#q="), search_terms,
1507 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151508
1509 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191510 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1511 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101512 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151513}
[email protected]56fa29592013-07-02 20:25:531514
alexmos294849f2015-03-14 00:55:061515TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1516 TemplateURLData data;
1517 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1518 TemplateURL url(data);
1519 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1520 GURL result;
1521
1522 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1523 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1524 search_terms_data_, &result));
1525 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1526
1527 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1528 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1529 search_terms_data_, &result));
1530 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1531
1532 result = GURL("http://should.not.change.com");
1533 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1534 GURL("http://term-in-path.com/about"), search_terms,
1535 search_terms_data_, &result));
1536 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1537}
1538
vitbarf298455d2015-04-21 12:58:101539// Checks that the ReplaceSearchTermsInURL function works correctly
1540// for search terms containing non-latin characters for a search engine
1541// using UTF-8 input encoding.
1542TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) {
1543 TemplateURLData data;
1544 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1545 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1546 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1547 TemplateURL url(data);
1548
1549 // Russian text which will be encoded with UTF-8.
1550 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1551 L"\x0442\x0435\x043A\x0441\x0442"));
1552 GURL result;
1553
1554 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1555 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_,
1556 &result));
1557 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1558 result);
1559
1560 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1561 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_,
1562 &result));
1563 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1564 result);
1565
1566 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1567 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_,
1568 &result));
1569 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1570 result);
1571}
1572
1573// Checks that the ReplaceSearchTermsInURL function works correctly
1574// for search terms containing non-latin characters for a search engine
1575// using non UTF-8 input encoding.
1576TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) {
1577 TemplateURLData data;
1578 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1579 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1580 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1581 data.input_encodings.push_back("windows-1251");
1582 TemplateURL url(data);
1583
1584 // Russian text which will be encoded with Windows-1251.
1585 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1586 L"\x0442\x0435\x043A\x0441\x0442"));
1587 GURL result;
1588
1589 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1590 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_,
1591 &result));
1592 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1593 result);
1594
1595 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1596 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_,
1597 &result));
1598 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1599 result);
1600
1601 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1602 GURL("http://windows-1251.ru/path/a%20b"), search_terms,
1603 search_terms_data_, &result));
1604 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1605 result);
1606}
1607
Mark Pearson247fb8a2018-08-30 05:12:261608// Test the |additional_query_params| field of SearchTermsArgs.
[email protected]621ade062013-10-28 06:27:431609TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431610 TemplateURLData data;
1611 // Pick a URL with replacements before, during, and after the query, to ensure
1612 // we don't goof up any of them.
1613 data.SetURL("{google:baseURL}search?q={searchTerms}"
1614 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281615 TemplateURL url(data);
[email protected]621ade062013-10-28 06:27:431616
Mark Pearson247fb8a2018-08-30 05:12:261617 // Baseline: no |additional_query_params| field.
[email protected]621ade062013-10-28 06:27:431618 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1619 search_terms.original_query = ASCIIToUTF16("def");
1620 search_terms.accepted_suggestion = 0;
1621 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191622 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431623
Mark Pearson247fb8a2018-08-30 05:12:261624 // Set the |additional_query_params|.
1625 search_terms.additional_query_params = "pq=xyz";
[email protected]621ade062013-10-28 06:27:431626 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191627 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431628
Mark Pearson247fb8a2018-08-30 05:12:261629 // Add |append_extra_query_params_from_command_line| into the mix, and ensure
1630 // it works.
1631 search_terms.append_extra_query_params_from_command_line = true;
avi1772c1a2014-12-22 22:42:331632 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]621ade062013-10-28 06:27:431633 switches::kExtraSearchQueryParams, "a=b");
1634 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191635 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431636}
1637
Mark Pearson247fb8a2018-08-30 05:12:261638// Test the |search_terms.append_extra_query_params_from_command_line| field of
1639// SearchTermsArgs.
[email protected]56fa29592013-07-02 20:25:531640TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531641 TemplateURLData data;
1642 // Pick a URL with replacements before, during, and after the query, to ensure
1643 // we don't goof up any of them.
1644 data.SetURL("{google:baseURL}search?q={searchTerms}"
1645 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281646 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531647
Mark Pearson247fb8a2018-08-30 05:12:261648 // Baseline: no command-line args, no
1649 // |search_terms.append_extra_query_params_from_command_line| flag.
[email protected]56fa29592013-07-02 20:25:531650 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1651 search_terms.original_query = ASCIIToUTF16("def");
1652 search_terms.accepted_suggestion = 0;
1653 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191654 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531655
1656 // Set the flag. Since there are no command-line args, this should have no
1657 // effect.
Mark Pearson247fb8a2018-08-30 05:12:261658 search_terms.append_extra_query_params_from_command_line = true;
[email protected]56fa29592013-07-02 20:25:531659 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191660 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531661
1662 // Now append the command-line arg. This should be inserted into the query.
avi1772c1a2014-12-22 22:42:331663 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]56fa29592013-07-02 20:25:531664 switches::kExtraSearchQueryParams, "a=b");
1665 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191666 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531667
1668 // Turn off the flag. Now the command-line arg should be ignored again.
Mark Pearson247fb8a2018-08-30 05:12:261669 search_terms.append_extra_query_params_from_command_line = false;
[email protected]56fa29592013-07-02 20:25:531670 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191671 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531672}
[email protected]d5015ca2013-08-08 22:04:181673
1674// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171675TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181676 TemplateURLData data;
1677 data.input_encodings.push_back("UTF-8");
1678 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281679 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191680 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1681 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]d5015ca2013-08-08 22:04:181682 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1683
[email protected]ce7ee5f2014-06-16 23:41:191684 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1685 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181686 EXPECT_EQ("http://www.google.com/?q=foo", result);
1687
[email protected]332d17d22014-06-20 16:56:031688 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191689 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1690 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181691 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1692
1693 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031694 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191695 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1696 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181697 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1698}
[email protected]2328ee22013-08-08 23:00:191699
1700// Test the IsSearchResults function.
1701TEST_F(TemplateURLTest, IsSearchResults) {
1702 TemplateURLData data;
1703 data.SetURL("http://bar/search?q={searchTerms}");
[email protected]2767c0fd2013-08-16 17:44:161704 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191705 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1706 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1707 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1708 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281709 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191710
1711 const struct {
1712 const char* const url;
1713 bool result;
1714 } url_data[] = {
1715 { "http://bar/search?q=foo&oq=foo", true, },
1716 { "http://bar/?q=foo&oq=foo", true, },
1717 { "http://bar/#output=search&q=foo&oq=foo", true, },
1718 { "http://bar/webhp#q=foo&oq=foo", true, },
1719 { "http://bar/#q=foo&oq=foo", true, },
1720 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1721 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1722 { "http://bar/", false, },
1723 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161724 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191725 };
1726
Avi Drissman2244c2d2018-12-25 23:08:021727 for (size_t i = 0; i < base::size(url_data); ++i) {
[email protected]2328ee22013-08-08 23:00:191728 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191729 search_provider.IsSearchURL(GURL(url_data[i].url),
1730 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191731 }
1732}
[email protected]a048de8a2013-10-02 18:30:061733
jdonnelly41c5b46a2015-07-10 21:24:381734TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) {
[email protected]9d70de12014-05-10 02:15:311735 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441736 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311737 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1738
[email protected]168d08722014-06-18 07:13:281739 TemplateURL url(data);
[email protected]9d70de12014-05-10 02:15:311740 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191741 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1742 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311743 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1744}
[email protected]20184242014-05-14 02:57:421745
1746TEST_F(TemplateURLTest, SessionToken) {
1747 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441748 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421749 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1750
[email protected]168d08722014-06-18 07:13:281751 TemplateURL url(data);
[email protected]20184242014-05-14 02:57:421752 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1753 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191754 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1755 search_terms_data_);
[email protected]20184242014-05-14 02:57:421756 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1757
[email protected]168d08722014-06-18 07:13:281758 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421759 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191760 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1761 search_terms_data_);
[email protected]20184242014-05-14 02:57:421762 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1763}
[email protected]448b17f52014-06-13 01:08:031764
1765TEST_F(TemplateURLTest, ContextualSearchParameters) {
1766 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441767 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031768 data.SetURL("http://bar/_/contextualsearch?"
1769 "{google:contextualSearchVersion}"
1770 "{google:contextualSearchContextData}");
1771
[email protected]168d08722014-06-18 07:13:281772 TemplateURL url(data);
[email protected]448b17f52014-06-13 01:08:031773 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191774 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1775 search_terms_data_);
donndbb98274e2016-11-01 21:04:401776 EXPECT_EQ("http://bar/_/contextualsearch?", result);
[email protected]448b17f52014-06-13 01:08:031777
Donn Denman01b1bf72018-08-14 16:25:201778 // Test the current common case, which uses no home country or previous
1779 // event.
1780 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
Donn Denman97be51df2020-06-24 01:23:191781 2, 1, std::string(), 0, 0, false, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391782 std::string(), std::string());
[email protected]448b17f52014-06-13 01:08:031783 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191784 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1785 search_terms_data_);
donndd9fc4d92016-09-16 00:30:491786 EXPECT_EQ(
1787 "http://bar/_/contextualsearch?"
donnd0bc73292017-01-17 22:07:561788 "ctxs=2&"
donndd9fc4d92016-09-16 00:30:491789 "ctxsl_coca=1",
1790 result);
donndbb98274e2016-11-01 21:04:401791
Donn Denman01b1bf72018-08-14 16:25:201792 // Test the home country and non-zero event data case.
donndbb98274e2016-11-01 21:04:401793 search_terms_args.contextual_search_params =
Donn Denmandf4ede0f2020-01-17 23:11:221794 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denman97be51df2020-06-24 01:23:191795 2, 2, "CH", 1657713458, 5, false, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391796 std::string(), std::string());
donndbb98274e2016-11-01 21:04:401797 result =
1798 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1799
1800 EXPECT_EQ(
1801 "http://bar/_/contextualsearch?"
1802 "ctxs=2&"
donnd0bc73292017-01-17 22:07:561803 "ctxsl_coca=2&"
Donn Denman01b1bf72018-08-14 16:25:201804 "ctxs_hc=CH&"
1805 "ctxsl_pid=1657713458&"
1806 "ctxsl_per=5",
donndbb98274e2016-11-01 21:04:401807 result);
Donn Denmandf4ede0f2020-01-17 23:11:221808
1809 // Test exact-search.
1810 search_terms_args.contextual_search_params =
1811 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denman97be51df2020-06-24 01:23:191812 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391813 std::string(), std::string());
Donn Denmandf4ede0f2020-01-17 23:11:221814 result =
1815 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
Donn Denmandf4ede0f2020-01-17 23:11:221816 // Find our param.
1817 size_t found_pos = result.find("ctxsl_exact=1");
1818 EXPECT_NE(found_pos, std::string::npos);
Donn Denman7a241012020-01-31 18:33:131819
1820 // Test source and target languages.
1821 search_terms_args.contextual_search_params =
1822 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denmand638f4602020-11-03 21:05:391823 2, 1, std::string(), 0, 0, true, "es", "de", std::string(),
1824 std::string());
Donn Denman7a241012020-01-31 18:33:131825 result =
1826 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1827 // Find our params.
1828 size_t source_pos = result.find("tlitesl=es");
1829 EXPECT_NE(source_pos, std::string::npos);
1830 size_t target_pos = result.find("tlitetl=de");
1831 EXPECT_NE(target_pos, std::string::npos);
Donn Denman97be51df2020-06-24 01:23:191832
1833 // Test fluent languages.
1834 search_terms_args.contextual_search_params =
1835 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
1836 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391837 "es,de", std::string());
Donn Denman97be51df2020-06-24 01:23:191838 result =
1839 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1840 // Find our param. These may actually be URL encoded.
1841 size_t fluent_pos = result.find("&ctxs_fls=es,de");
1842 EXPECT_NE(fluent_pos, std::string::npos);
Donn Denmand638f4602020-11-03 21:05:391843
1844 // Test Related Searches.
1845 search_terms_args.contextual_search_params =
1846 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
1847 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
1848 std::string(), "1RbCu");
1849 result =
1850 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1851 // Find our param.
1852 size_t ctxsl_rs_pos = result.find("&ctxsl_rs=1RbCu");
1853 EXPECT_NE(ctxsl_rs_pos, std::string::npos);
[email protected]448b17f52014-06-13 01:08:031854}
[email protected]44ccc9f2014-06-20 17:36:211855
1856TEST_F(TemplateURLTest, GenerateKeyword) {
1857 ASSERT_EQ(ASCIIToUTF16("foo"),
jshin1fb76462016-04-05 22:13:031858 TemplateURL::GenerateKeyword(GURL("http://foo")));
Emily Stark0fb634f82020-07-14 18:00:391859 ASSERT_EQ(ASCIIToUTF16("foo."),
1860 TemplateURL::GenerateKeyword(GURL("http://foo.")));
1861 // www. should be stripped for a public hostname but not a private/intranet
1862 // hostname.
1863 ASSERT_EQ(ASCIIToUTF16("google.com"),
1864 TemplateURL::GenerateKeyword(GURL("http://www.google.com")));
1865 ASSERT_EQ(ASCIIToUTF16("www.foo"),
jshin1fb76462016-04-05 22:13:031866 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
[email protected]44ccc9f2014-06-20 17:36:211867 // Make sure we don't get a trailing '/'.
jshin1fb76462016-04-05 22:13:031868 ASSERT_EQ(ASCIIToUTF16("blah"),
1869 TemplateURL::GenerateKeyword(GURL("http://blah/")));
[email protected]44ccc9f2014-06-20 17:36:211870 // Don't generate the empty string.
Emily Stark0fb634f82020-07-14 18:00:391871 ASSERT_EQ(ASCIIToUTF16("www."),
jshin1fb76462016-04-05 22:13:031872 TemplateURL::GenerateKeyword(GURL("http://www.")));
alshabaline6212ac2015-07-23 08:03:001873 ASSERT_EQ(
1874 base::UTF8ToUTF16("\xd0\xb0\xd0\xb1\xd0\xb2"),
jshin1fb76462016-04-05 22:13:031875 TemplateURL::GenerateKeyword(GURL("http://xn--80acd")));
a-v-y81695d0d2017-04-20 08:22:221876
1877 // Generated keywords must always be in lowercase, because TemplateURLs always
1878 // converts keywords to lowercase in its constructor and TemplateURLService
1879 // stores TemplateURLs in maps using keyword as key.
1880 EXPECT_TRUE(IsLowerCase(TemplateURL::GenerateKeyword(GURL("http://BLAH/"))));
1881 EXPECT_TRUE(IsLowerCase(
1882 TemplateURL::GenerateKeyword(GURL("http://embeddedhtml.<head>/"))));
[email protected]44ccc9f2014-06-20 17:36:211883}
1884
1885TEST_F(TemplateURLTest, GenerateSearchURL) {
1886 struct GenerateSearchURLCase {
1887 const char* test_name;
1888 const char* url;
1889 const char* expected;
1890 } generate_url_cases[] = {
1891 { "invalid URL", "foo{searchTerms}", "" },
1892 { "URL with no replacements", "http://foo/", "http://foo/" },
1893 { "basic functionality", "http://foo/{searchTerms}",
1894 "http://foo/blah.blah.blah.blah.blah" }
1895 };
1896
Avi Drissman2244c2d2018-12-25 23:08:021897 for (size_t i = 0; i < base::size(generate_url_cases); ++i) {
[email protected]44ccc9f2014-06-20 17:36:211898 TemplateURLData data;
1899 data.SetURL(generate_url_cases[i].url);
1900 TemplateURL t_url(data);
1901 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1902 generate_url_cases[i].expected)
1903 << generate_url_cases[i].test_name << " failed.";
1904 }
1905}
[email protected]9e9130ce2014-06-23 23:20:511906
1907TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1908 TemplateURLData data;
1909 search_terms_data_.set_google_base_url("http://bar/");
1910 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1911
1912 TemplateURL url(data);
1913 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1914 search_terms_args.prefetch_query = "full query text";
1915 search_terms_args.prefetch_query_type = "2338";
1916 std::string result =
1917 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1918 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1919 result);
1920
1921 TemplateURL url2(data);
1922 search_terms_args.prefetch_query.clear();
1923 search_terms_args.prefetch_query_type.clear();
1924 result =
1925 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1926 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1927}
vitbar5bd8c252016-01-29 18:44:511928
1929// Tests that TemplateURL works correctly after changing the Google base URL
1930// and invalidating cached values.
1931TEST_F(TemplateURLTest, InvalidateCachedValues) {
1932 TemplateURLData data;
1933 data.SetURL("{google:baseURL}search?q={searchTerms}");
1934 data.suggestions_url = "{google:baseSuggestURL}search?q={searchTerms}";
vitbar5bd8c252016-01-29 18:44:511935 data.image_url = "{google:baseURL}searchbyimage/upload";
1936 data.new_tab_url = "{google:baseURL}_/chrome/newtab";
1937 data.contextual_search_url = "{google:baseURL}_/contextualsearch";
1938 data.alternate_urls.push_back("{google:baseURL}s#q={searchTerms}");
1939 TemplateURL url(data);
1940 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("X"));
1941 base::string16 search_terms;
1942
1943 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1944 EXPECT_EQ("http://www.google.com/search?q=X",
1945 url.url_ref().ReplaceSearchTerms(search_terms_args,
1946 search_terms_data_));
1947 EXPECT_EQ("http://www.google.com/s#q=X",
1948 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1949 search_terms_data_));
1950 EXPECT_EQ("http://www.google.com/search?q=X",
1951 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1952 search_terms_data_));
1953 EXPECT_EQ("http://www.google.com/complete/search?q=X",
1954 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1955 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:511956 EXPECT_EQ("http://www.google.com/searchbyimage/upload",
1957 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1958 search_terms_data_));
1959 EXPECT_EQ("http://www.google.com/_/chrome/newtab",
1960 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1961 search_terms_data_));
1962 EXPECT_EQ("http://www.google.com/_/contextualsearch",
1963 url.contextual_search_url_ref().ReplaceSearchTerms(
1964 search_terms_args, search_terms_data_));
1965
1966 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1967 GURL("http://www.google.com/search?q=Y+Z"),
1968 search_terms_data_, &search_terms));
1969 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
1970 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1971 GURL("http://www.google.com/s#q=123"),
1972 search_terms_data_, &search_terms));
1973 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
1974
1975 search_terms_data_.set_google_base_url("https://www.foo.org/");
1976 url.InvalidateCachedValues();
1977
1978 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1979 EXPECT_EQ("https://www.foo.org/search?q=X",
1980 url.url_ref().ReplaceSearchTerms(search_terms_args,
1981 search_terms_data_));
1982 EXPECT_EQ("https://www.foo.org/s#q=X",
1983 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1984 search_terms_data_));
1985 EXPECT_EQ("https://www.foo.org/search?q=X",
1986 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1987 search_terms_data_));
1988 EXPECT_EQ("https://www.foo.org/complete/search?q=X",
1989 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1990 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:511991 EXPECT_EQ("https://www.foo.org/searchbyimage/upload",
1992 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1993 search_terms_data_));
1994 EXPECT_EQ("https://www.foo.org/_/chrome/newtab",
1995 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1996 search_terms_data_));
1997 EXPECT_EQ("https://www.foo.org/_/contextualsearch",
1998 url.contextual_search_url_ref().ReplaceSearchTerms(
1999 search_terms_args, search_terms_data_));
2000
2001 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
2002 GURL("https://www.foo.org/search?q=Y+Z"),
2003 search_terms_data_, &search_terms));
2004 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
2005 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
2006 GURL("https://www.foo.org/s#q=123"),
2007 search_terms_data_, &search_terms));
2008 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
2009
2010 search_terms_data_.set_google_base_url("http://www.google.com/");
2011}
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002012
2013// Tests the use of wildcards in the path to ensure both extracting search terms
2014// and generating a search URL work correctly.
2015TEST_F(TemplateURLTest, PathWildcard) {
2016 TemplateURLData data;
2017 data.SetURL(
2018 "https://www.google.com/search{google:pathWildcard}?q={searchTerms}");
2019 TemplateURL url(data);
2020
2021 // Test extracting search terms from a URL.
2022 base::string16 search_terms;
2023 url.ExtractSearchTermsFromURL(GURL("https://www.google.com/search?q=testing"),
2024 search_terms_data_, &search_terms);
2025 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
2026 url.ExtractSearchTermsFromURL(
2027 GURL("https://www.google.com/search;_this_is_a_test;_?q=testing"),
2028 search_terms_data_, &search_terms);
2029 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
2030
2031 // Tests overlapping prefix/suffix.
2032 data.SetURL(
2033 "https://www.google.com/search{google:pathWildcard}rch?q={searchTerms}");
2034 TemplateURL overlap_url(data);
2035 overlap_url.ExtractSearchTermsFromURL(
2036 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
2037 &search_terms);
2038 EXPECT_TRUE(search_terms.empty());
2039
2040 // Tests wildcard at beginning of path so we only have a suffix.
2041 data.SetURL(
2042 "https://www.google.com/{google:pathWildcard}rch?q={searchTerms}");
2043 TemplateURL suffix_url(data);
2044 suffix_url.ExtractSearchTermsFromURL(
2045 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
2046 &search_terms);
2047 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
2048
2049 // Tests wildcard between prefix/suffix.
2050 overlap_url.ExtractSearchTermsFromURL(
2051 GURL("https://www.google.com/search_testing_rch?q=testing"),
2052 search_terms_data_, &search_terms);
2053 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
2054
2055 // Test generating a URL.
2056 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
2057 GURL generated_url;
2058 url.ReplaceSearchTermsInURL(url.GenerateSearchURL(search_terms_data_),
2059 search_terms_args, search_terms_data_,
2060 &generated_url);
2061 EXPECT_EQ("https://www.google.com/search?q=foo", generated_url.spec());
2062}