blob: 109f13f5cc57a985672c23723c170dd5d1ff8c6d [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"
Lei Zhangd073f65f2021-05-25 17:43:0610#include "base/cxx17_backports.h"
a-v-y81695d0d2017-04-20 08:22:2211#include "base/i18n/case_conversion.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 {
Jan Wilken Dörriefa241ba2021-03-11 17:57:0128bool IsLowerCase(const std::u16string& str) {
a-v-y81695d0d2017-04-20 08:22:2229 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;
Jan Wilken Dörriefa241ba2021-03-11 17:57:0181 const std::u16string terms;
[email protected]34e24852012-01-31 18:43:5882 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:1983 } search_term_cases[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:2484 {"http://foo{searchTerms}", u"sea rch/bar", "http://foosea%20rch/bar"},
85 {"http://foo{searchTerms}?boo=abc", u"sea rch/bar",
86 "http://foosea%20rch/bar?boo=abc"},
87 {"http://foo/?boo={searchTerms}", u"sea rch/bar",
88 "http://foo/?boo=sea+rch%2Fbar"},
89 {"http://en.wikipedia.org/{searchTerms}", u"wiki/?",
90 "http://en.wikipedia.org/wiki/%3F"}};
Avi Drissman2244c2d2018-12-25 23:08:0291 for (size_t i = 0; i < base::size(search_term_cases); ++i) {
[email protected]0d2e6a62010-01-15 20:09:1992 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:5493 TemplateURLData data;
94 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:2895 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1996 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
97 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:0498 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:1999 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data_));
[email protected]c31a979c2012-05-31 23:57:16100 ASSERT_TRUE(result.is_valid());
101 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:19102 }
[email protected]d82443b2009-01-15 19:54:56103}
104
105TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:54106 TemplateURLData data;
107 data.SetURL("http://foo{searchTerms}{count?}");
[email protected]168d08722014-06-18 07:13:28108 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19109 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
110 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04111 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24112 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56113 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27114 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:56115}
116
117TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:54118 TemplateURLData data;
119 data.SetURL("http://foo{searchTerms}{count}");
[email protected]168d08722014-06-18 07:13:28120 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19121 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
122 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04123 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24124 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56125 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27126 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56127}
128
129TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54130 TemplateURLData data;
131 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]168d08722014-06-18 07:13:28132 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19133 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
134 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04135 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24136 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56137 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27138 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56139}
140
141TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54142 TemplateURLData data;
143 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[email protected]168d08722014-06-18 07:13:28144 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19145 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
146 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04147 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24148 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56149 ASSERT_TRUE(result.is_valid());
[email protected]405aae22012-03-29 20:36:13150 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56151}
152
153TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54154 TemplateURLData data;
155 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[email protected]168d08722014-06-18 07:13:28156 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19157 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
158 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04159 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24160 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56161 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27162 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56163}
164
[email protected]93b29062013-07-12 03:09:09165TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
166 const char kInvalidPostParamsString[] =
167 "unknown_template={UnknownTemplate},bad_value=bad{value},"
168 "{google:sbiSource}";
169 // List all accpectable parameter format in valid_post_params_string. it is
170 // expected like: "name0=,name1=value1,name2={template1}"
171 const char kValidPostParamsString[] =
172 "image_content={google:imageThumbnail},image_url={google:imageURL},"
173 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
Robbie Gibson8532eaa2019-03-15 11:43:24174 "constant_param=constant,width={google:imageOriginalWidth},"
175 "base64_image_content={google:imageThumbnailBase64}";
[email protected]93b29062013-07-12 03:09:09176 const char KImageSearchURL[] = "http://foo.com/sbi";
177
178 TemplateURLData data;
179 data.image_url = KImageSearchURL;
180
181 // Try to parse invalid post parameters.
182 data.image_url_post_params = kInvalidPostParamsString;
[email protected]168d08722014-06-18 07:13:28183 TemplateURL url_bad(data);
[email protected]ce7ee5f2014-06-16 23:41:19184 ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09185 const TemplateURLRef::PostParams& bad_post_params =
186 url_bad.image_url_ref().post_params_;
187 ASSERT_EQ(2U, bad_post_params.size());
vitbardeb285392015-02-20 14:02:55188 ExpectPostParamIs(bad_post_params[0], "unknown_template",
189 "{UnknownTemplate}");
190 ExpectPostParamIs(bad_post_params[1], "bad_value", "bad{value}");
[email protected]93b29062013-07-12 03:09:09191
192 // Try to parse valid post parameters.
193 data.image_url_post_params = kValidPostParamsString;
[email protected]168d08722014-06-18 07:13:28194 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19195 ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_));
196 ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09197
198 // Check term replacement.
Jan Wilken Dörrie756999e2021-03-23 15:05:24199 TemplateURLRef::SearchTermsArgs search_args(u"X");
[email protected]93b29062013-07-12 03:09:09200 search_args.image_thumbnail_content = "dummy-image-thumbnail";
201 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
[email protected]2f3bc6512013-08-28 03:56:27202 search_args.image_original_size = gfx::Size(10, 10);
[email protected]93b29062013-07-12 03:09:09203 // Replacement operation with no post_data buffer should still return
204 // the parsed URL.
hashimoto3c831812014-08-25 07:40:23205 TestingSearchTermsData search_terms_data("http://X");
[email protected]ce7ee5f2014-06-16 23:41:19206 GURL result(url.image_url_ref().ReplaceSearchTerms(
207 search_args, search_terms_data));
[email protected]93b29062013-07-12 03:09:09208 ASSERT_TRUE(result.is_valid());
209 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48210 TemplateURLRef::PostContent post_content;
[email protected]ce7ee5f2014-06-16 23:41:19211 result = GURL(url.image_url_ref().ReplaceSearchTerms(
[email protected]7c2bcc42013-08-01 04:03:48212 search_args, search_terms_data, &post_content));
[email protected]93b29062013-07-12 03:09:09213 ASSERT_TRUE(result.is_valid());
214 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48215 ASSERT_FALSE(post_content.first.empty());
216 ASSERT_FALSE(post_content.second.empty());
[email protected]93b29062013-07-12 03:09:09217
218 // Check parsed result of post parameters.
219 const TemplateURLRef::Replacements& replacements =
220 url.image_url_ref().replacements_;
221 const TemplateURLRef::PostParams& post_params =
222 url.image_url_ref().post_params_;
Robbie Gibson8532eaa2019-03-15 11:43:24223 EXPECT_EQ(8U, post_params.size());
jdoerrie3feb1852018-10-05 12:16:44224 for (auto i = post_params.begin(); i != post_params.end(); ++i) {
225 auto j = replacements.begin();
[email protected]93b29062013-07-12 03:09:09226 for (; j != replacements.end(); ++j) {
227 if (j->is_post_param && j->index ==
228 static_cast<size_t>(i - post_params.begin())) {
229 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27230 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
Raul Tambref88e5102019-02-06 10:54:03231 ExpectPostParamIs(
232 *i, "width",
233 base::NumberToString(search_args.image_original_size.width()));
[email protected]2f3bc6512013-08-28 03:56:27234 break;
[email protected]1020fead2014-06-20 13:40:28235 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE:
vitbardeb285392015-02-20 14:02:55236 ExpectPostParamIs(*i, "sbisrc",
237 search_terms_data.GoogleImageSearchSource());
[email protected]1020fead2014-06-20 13:40:28238 break;
[email protected]93b29062013-07-12 03:09:09239 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
vitbardeb285392015-02-20 14:02:55240 ExpectPostParamIs(*i, "image_content",
241 search_args.image_thumbnail_content,
242 "image/jpeg");
[email protected]93b29062013-07-12 03:09:09243 break;
Robbie Gibson8532eaa2019-03-15 11:43:24244 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL_BASE64: {
245 std::string base64_image_content;
246 base::Base64Encode(search_args.image_thumbnail_content,
247 &base64_image_content);
248 ExpectPostParamIs(*i, "base64_image_content", base64_image_content,
249 "image/jpeg");
250 break;
251 }
[email protected]93b29062013-07-12 03:09:09252 case TemplateURLRef::GOOGLE_IMAGE_URL:
vitbardeb285392015-02-20 14:02:55253 ExpectPostParamIs(*i, "image_url", search_args.image_url.spec());
[email protected]93b29062013-07-12 03:09:09254 break;
255 case TemplateURLRef::LANGUAGE:
vitbardeb285392015-02-20 14:02:55256 ExpectPostParamIs(*i, "language", "en");
[email protected]93b29062013-07-12 03:09:09257 break;
258 default:
259 ADD_FAILURE(); // Should never go here.
260 }
261 break;
262 }
263 }
264 if (j != replacements.end())
265 continue;
vitbardeb285392015-02-20 14:02:55266 if (i->name == "empty_param")
267 ExpectPostParamIs(*i, "empty_param", std::string());
268 else
269 ExpectPostParamIs(*i, "constant_param", "constant");
[email protected]93b29062013-07-12 03:09:09270 }
271}
272
Peter Kastinge59f0c32017-11-29 05:56:22273TEST_F(TemplateURLTest, ImageURLWithGetShouldNotCrash) {
274 TemplateURLData data;
275 data.SetURL("http://foo/?q={searchTerms}&t={google:imageThumbnail}");
276 TemplateURL url(data);
277 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
278 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
279
Jan Wilken Dörrie756999e2021-03-23 15:05:24280 TemplateURLRef::SearchTermsArgs search_args(u"X");
Peter Kastinge59f0c32017-11-29 05:56:22281 search_args.image_thumbnail_content = "dummy-image-thumbnail";
282 GURL result(
283 url.url_ref().ReplaceSearchTerms(search_args, search_terms_data_));
284 ASSERT_TRUE(result.is_valid());
285 EXPECT_EQ("http://foo/?q=X&t=dummy-image-thumbnail", result.spec());
286}
287
[email protected]d88cb202011-08-17 20:03:01288// Test that setting the prepopulate ID from TemplateURL causes the stored
289// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44290TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54291 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20292 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]168d08722014-06-18 07:13:28293 TemplateURL url(data);
[email protected]1a257262011-06-28 22:15:44294 TemplateURLRef::Replacements replacements;
295 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:24296 EXPECT_EQ("http://foo{fhqwhgads}bar",
297 url.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements,
298 nullptr, &valid));
[email protected]1a257262011-06-28 22:15:44299 EXPECT_TRUE(replacements.empty());
300 EXPECT_TRUE(valid);
301
[email protected]573889f22012-04-07 01:31:54302 data.prepopulate_id = 123;
[email protected]168d08722014-06-18 07:13:28303 TemplateURL url2(data);
Ivan Kotenkov75b1c3a2017-10-24 14:47:24304 EXPECT_EQ("http://foobar",
305 url2.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements,
306 nullptr, &valid));
[email protected]1a257262011-06-28 22:15:44307 EXPECT_TRUE(replacements.empty());
308 EXPECT_TRUE(valid);
309}
310
vitbar90902c42016-05-11 06:16:55311// Test that setting the prepopulate ID from TemplateURL causes the stored
312// TemplateURLRef to handle parsing the URL parameters differently.
313TEST_F(TemplateURLTest, SetPrepopulatedAndReplace) {
314 TemplateURLData data;
315 data.SetURL("http://foo{fhqwhgads}search/?q={searchTerms}");
316 data.suggestions_url = "http://foo{fhqwhgads}suggest/?q={searchTerms}";
vitbar90902c42016-05-11 06:16:55317 data.image_url = "http://foo{fhqwhgads}image/";
318 data.new_tab_url = "http://foo{fhqwhgads}newtab/";
319 data.contextual_search_url = "http://foo{fhqwhgads}context/";
320 data.alternate_urls.push_back(
321 "http://foo{fhqwhgads}alternate/?q={searchTerms}");
322
Jan Wilken Dörrie756999e2021-03-23 15:05:24323 TemplateURLRef::SearchTermsArgs args(u"X");
vitbar90902c42016-05-11 06:16:55324 const SearchTermsData& stdata = search_terms_data_;
325
326 TemplateURL url(data);
327 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
328 url.url_ref().ReplaceSearchTerms(args, stdata));
329 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dalternate/?q=X",
330 url.url_refs()[0].ReplaceSearchTerms(args, stdata));
331 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
332 url.url_refs()[1].ReplaceSearchTerms(args, stdata));
333 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsuggest/?q=X",
334 url.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
vitbar90902c42016-05-11 06:16:55335 EXPECT_EQ("http://foo{fhqwhgads}image/",
336 url.image_url_ref().ReplaceSearchTerms(args, stdata));
337 EXPECT_EQ("http://foo{fhqwhgads}newtab/",
338 url.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
339 EXPECT_EQ("http://foo{fhqwhgads}context/",
340 url.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
341
342 data.prepopulate_id = 123;
343 TemplateURL url2(data);
344 EXPECT_EQ("http://foosearch/?q=X",
345 url2.url_ref().ReplaceSearchTerms(args, stdata));
346 EXPECT_EQ("http://fooalternate/?q=X",
347 url2.url_refs()[0].ReplaceSearchTerms(args, stdata));
348 EXPECT_EQ("http://foosearch/?q=X",
349 url2.url_refs()[1].ReplaceSearchTerms(args, stdata));
350 EXPECT_EQ("http://foosuggest/?q=X",
351 url2.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
vitbar90902c42016-05-11 06:16:55352 EXPECT_EQ("http://fooimage/",
353 url2.image_url_ref().ReplaceSearchTerms(args, stdata));
354 EXPECT_EQ("http://foonewtab/",
355 url2.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
356 EXPECT_EQ("http://foocontext/",
357 url2.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
358}
359
[email protected]d82443b2009-01-15 19:54:56360TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54361 TemplateURLData data;
362 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]168d08722014-06-18 07:13:28363 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19364 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
365 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04366 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24367 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56368 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27369 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56370}
371
372TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54373 TemplateURLData data;
374 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]168d08722014-06-18 07:13:28375 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19376 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
377 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04378 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24379 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56380 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27381 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56382}
383
[email protected]375bd7312010-08-30 22:18:13384TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
385 struct SearchTermsCase {
386 const char* url;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01387 const std::u16string terms;
[email protected]375bd7312010-08-30 22:18:13388 const char* output;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01389 } search_term_cases[] = {{"{google:baseURL}{language}{searchTerms}",
390 std::u16string(), "http://example.com/e/en"},
391 {"{google:baseSuggestURL}{searchTerms}",
392 std::u16string(), "http://example.com/complete/"}};
[email protected]375bd7312010-08-30 22:18:13393
hashimoto3c831812014-08-25 07:40:23394 TestingSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54395 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02396 for (size_t i = 0; i < base::size(search_term_cases); ++i) {
[email protected]375bd7312010-08-30 22:18:13397 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54398 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:28399 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19400 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data));
401 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data));
402 GURL result(url.url_ref().ReplaceSearchTerms(
Ivan Kotenkov75b1c3a2017-10-24 14:47:24403 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data,
404 nullptr));
[email protected]375bd7312010-08-30 22:18:13405 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27406 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13407 }
408}
409
[email protected]d82443b2009-01-15 19:54:56410TEST_F(TemplateURLTest, URLRefTermToWide) {
411 struct ToWideCase {
412 const char* encoded_search_term;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01413 const std::u16string expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56414 } to_wide_cases[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24415 {"hello+world", u"hello world"},
Jan Wilken Dörrie414bbf52021-03-19 19:06:26416 // Test some big-5 input.
Jan Wilken Dörrieeb96aa82021-03-19 19:16:33417 {"%a7A%A6%6e+to+you", u"\x4f60\x597d to you"},
Jan Wilken Dörrie414bbf52021-03-19 19:06:26418 // Test some UTF-8 input. We should fall back to this when the encoding
419 // doesn't look like big-5. We have a '5' in the middle, which is an
420 // invalid Big-5 trailing byte.
Jan Wilken Dörrieeb96aa82021-03-19 19:16:33421 {"%e4%bd%a05%e5%a5%bd+to+you", u"\x4f60\x35\x597d to you"},
Jan Wilken Dörrie414bbf52021-03-19 19:06:26422 // Undecodable input should stay escaped.
423 {"%91%01+abcd", u"%91%01 abcd"},
424 // Make sure we convert %2B to +.
Jan Wilken Dörrie756999e2021-03-23 15:05:24425 {"C%2B%2B", u"C++"},
Jan Wilken Dörrie414bbf52021-03-19 19:06:26426 // C%2B is escaped as C%252B, make sure we unescape it properly.
Jan Wilken Dörrie756999e2021-03-23 15:05:24427 {"C%252B", u"C%2B"},
[email protected]d82443b2009-01-15 19:54:56428 };
429
[email protected]d82443b2009-01-15 19:54:56430 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54431 TemplateURLData data;
432 data.SetURL("http://foo?q={searchTerms}");
433 data.input_encodings.push_back("big-5");
[email protected]168d08722014-06-18 07:13:28434 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19435 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
436 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Avi Drissman2244c2d2018-12-25 23:08:02437 for (size_t i = 0; i < base::size(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07438 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13439 url.url_ref().SearchTermToString16(
440 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56441 }
442}
443
[email protected]d82443b2009-01-15 19:54:56444TEST_F(TemplateURLTest, DisplayURLToURLRef) {
445 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19446 const std::string url;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01447 const std::u16string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27448 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24449 {"http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
450 u"http://foo%sx{inputEncoding}y{outputEncoding}a"},
451 {"http://X", u"http://X"},
452 {"http://foo{searchTerms", u"http://foo{searchTerms"},
453 {"http://foo{searchTerms}{language}", u"http://foo%s{language}"},
[email protected]d82443b2009-01-15 19:54:56454 };
[email protected]573889f22012-04-07 01:31:54455 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02456 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54457 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28458 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19459 EXPECT_EQ(test_data[i].expected_result,
460 url.url_ref().DisplayURL(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27461 EXPECT_EQ(test_data[i].url,
[email protected]ce7ee5f2014-06-16 23:41:19462 TemplateURLRef::DisplayURLToURLRef(
463 url.url_ref().DisplayURL(search_terms_data_)));
[email protected]d82443b2009-01-15 19:54:56464 }
465}
466
467TEST_F(TemplateURLTest, ReplaceSearchTerms) {
468 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19469 const std::string url;
[email protected]d82443b2009-01-15 19:54:56470 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27471 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19472 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56473 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19474 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56475 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19476 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56477 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19478 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56479 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19480 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56481 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19482 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56483 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19484 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56485 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19486 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56487 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19488 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56489 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19490 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56491 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19492 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56493 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19494 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56495 "http://foo/UTF-8a{language}aXa" },
496 };
[email protected]573889f22012-04-07 01:31:54497 TemplateURLData data;
498 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02499 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54500 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28501 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19502 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
503 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27504 std::string expected_result = test_data[i].expected_result;
brettwe6dae462015-06-24 20:54:45505 base::ReplaceSubstringsAfterOffset(
506 &expected_result, 0, "{language}",
507 search_terms_data_.GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04508 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24509 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27510 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56511 EXPECT_EQ(expected_result, result.spec());
512 }
513}
514
515
516// Tests replacing search terms in various encodings and making sure the
517// generated URL matches the expected value.
518TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
519 struct TestData {
520 const std::string encoding;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01521 const std::u16string search_term;
[email protected]ddd231e2010-06-29 20:35:19522 const std::string url;
[email protected]d82443b2009-01-15 19:54:56523 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27524 } test_data[] = {
Peter Kastingcc07b332021-05-07 22:58:25525 {"BIG5", u"悽", "http://foo/?{searchTerms}{inputEncoding}",
Jan Wilken Dörrieeb96aa82021-03-19 19:16:33526 "http://foo/?%B1~BIG5"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24527 {"UTF-8", u"blah", "http://foo/?{searchTerms}{inputEncoding}",
528 "http://foo/?blahUTF-8"},
Peter Kastingcc07b332021-05-07 22:58:25529 {"Shift_JIS", u"あ", "http://foo/{searchTerms}/bar",
530 "http://foo/%82%A0/bar"},
531 {"Shift_JIS", u"あ い", "http://foo/{searchTerms}/bar",
532 "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56533 };
[email protected]573889f22012-04-07 01:31:54534 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02535 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54536 data.SetURL(test_data[i].url);
537 data.input_encodings.clear();
538 data.input_encodings.push_back(test_data[i].encoding);
[email protected]168d08722014-06-18 07:13:28539 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19540 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
541 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04542 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19543 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
544 search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04545 ASSERT_TRUE(result.is_valid());
546 EXPECT_EQ(test_data[i].expected_result, result.spec());
547 }
548}
549
Alexander Yashkin874b919d2018-02-17 15:11:18550// Test that encoding with several optional codepages works as intended.
551// Codepages are tried in order, fallback is UTF-8.
552TEST_F(TemplateURLTest, ReplaceSearchTermsMultipleEncodings) {
553 struct TestData {
554 const std::vector<std::string> encodings;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01555 const std::u16string search_term;
Alexander Yashkin874b919d2018-02-17 15:11:18556 const std::string url;
557 const std::string expected_result;
558 } test_data[] = {
559 // First and third encodings are valid. First is used.
560 {{"windows-1251", "cp-866", "UTF-8"},
Peter Kastingcc07b332021-05-07 22:58:25561 u"я",
Alexander Yashkin874b919d2018-02-17 15:11:18562 "http://foo/?{searchTerms}{inputEncoding}",
563 "http://foo/?%FFwindows-1251"},
564 // Second and third encodings are valid, second is used.
565 {{"cp-866", "GB2312", "UTF-8"},
Peter Kastingcc07b332021-05-07 22:58:25566 u"狗",
Alexander Yashkin874b919d2018-02-17 15:11:18567 "http://foo/?{searchTerms}{inputEncoding}",
568 "http://foo/?%B9%B7GB2312"},
569 // Second and third encodings are valid in another order, second is used.
570 {{"cp-866", "UTF-8", "GB2312"},
Peter Kastingcc07b332021-05-07 22:58:25571 u"狗",
Alexander Yashkin874b919d2018-02-17 15:11:18572 "http://foo/?{searchTerms}{inputEncoding}",
573 "http://foo/?%E7%8B%97UTF-8"},
574 // Both encodings are invalid, fallback to UTF-8.
575 {{"cp-866", "windows-1251"},
Peter Kastingcc07b332021-05-07 22:58:25576 u"狗",
Alexander Yashkin874b919d2018-02-17 15:11:18577 "http://foo/?{searchTerms}{inputEncoding}",
578 "http://foo/?%E7%8B%97UTF-8"},
579 // No encodings are given, fallback to UTF-8.
580 {{},
Peter Kastingcc07b332021-05-07 22:58:25581 u"狗",
Alexander Yashkin874b919d2018-02-17 15:11:18582 "http://foo/?{searchTerms}{inputEncoding}",
583 "http://foo/?%E7%8B%97UTF-8"},
584 };
585
586 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02587 for (size_t i = 0; i < base::size(test_data); ++i) {
Alexander Yashkin874b919d2018-02-17 15:11:18588 data.SetURL(test_data[i].url);
589 data.input_encodings = test_data[i].encodings;
590 TemplateURL url(data);
591 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
592 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
593 GURL result(url.url_ref().ReplaceSearchTerms(
594 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
595 search_terms_data_));
596 ASSERT_TRUE(result.is_valid());
597 EXPECT_EQ(test_data[i].expected_result, result.spec());
598 }
599}
600
[email protected]bca359b2012-06-24 07:53:04601// Tests replacing assisted query stats (AQS) in various scenarios.
602TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
603 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01604 const std::u16string search_term;
[email protected]bca359b2012-06-24 07:53:04605 const std::string aqs;
606 const std::string base_url;
607 const std::string url;
608 const std::string expected_result;
609 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24610 // No HTTPS, no AQS.
611 {u"foo", "chrome.0.0l6", "http://foo/",
612 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
613 "http://foo/?foo"},
614 // HTTPS available, AQS should be replaced.
615 {u"foo", "chrome.0.0l6", "https://foo/",
616 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
617 "https://foo/?fooaqs=chrome.0.0l6&"},
618 // HTTPS available, however AQS is empty.
619 {u"foo", "", "https://foo/",
620 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
621 "https://foo/?foo"},
622 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
623 {u"foo", "chrome.0.0l6", "http://www.google.com",
624 "http://foo?{searchTerms}{google:assistedQueryStats}",
625 "http://foo/?foo"},
626 // A non-Google search provider with HTTPS should allow AQS.
627 {u"foo", "chrome.0.0l6", "https://www.google.com",
628 "https://foo?{searchTerms}{google:assistedQueryStats}",
629 "https://foo/?fooaqs=chrome.0.0l6&"},
[email protected]bca359b2012-06-24 07:53:04630 };
631 TemplateURLData data;
632 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02633 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04634 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28635 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19636 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
637 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04638 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
639 search_terms_args.assisted_query_stats = test_data[i].aqs;
[email protected]798baa8e2014-06-20 05:42:44640 search_terms_data_.set_google_base_url(test_data[i].base_url);
[email protected]ce7ee5f2014-06-16 23:41:19641 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
642 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27643 ASSERT_TRUE(result.is_valid());
644 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56645 }
646}
647
[email protected]00790562012-12-14 09:57:16648// Tests replacing cursor position.
649TEST_F(TemplateURLTest, ReplaceCursorPosition) {
650 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01651 const std::u16string search_term;
[email protected]00790562012-12-14 09:57:16652 size_t cursor_position;
653 const std::string url;
654 const std::string expected_result;
655 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24656 {u"foo", std::u16string::npos,
Jan Wilken Dörriefa241ba2021-03-11 17:57:01657 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
658 "http://www.google.com/?foo&"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24659 {u"foo", 2, "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
Jan Wilken Dörriefa241ba2021-03-11 17:57:01660 "http://www.google.com/?foo&cp=2&"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24661 {u"foo", 15, "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
Jan Wilken Dörriefa241ba2021-03-11 17:57:01662 "http://www.google.com/?foo&cp=15&"},
[email protected]00790562012-12-14 09:57:16663 };
664 TemplateURLData data;
665 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02666 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]00790562012-12-14 09:57:16667 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28668 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19669 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
670 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]00790562012-12-14 09:57:16671 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
672 search_terms_args.cursor_position = test_data[i].cursor_position;
[email protected]ce7ee5f2014-06-16 23:41:19673 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
674 search_terms_data_));
[email protected]00790562012-12-14 09:57:16675 ASSERT_TRUE(result.is_valid());
676 EXPECT_EQ(test_data[i].expected_result, result.spec());
677 }
678}
679
[email protected]420472b22014-06-10 13:34:43680// Tests replacing input type (&oit=).
681TEST_F(TemplateURLTest, ReplaceInputType) {
682 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01683 const std::u16string search_term;
Steven Holte3696c9412017-08-24 18:38:03684 metrics::OmniboxInputType input_type;
[email protected]420472b22014-06-10 13:34:43685 const std::string url;
686 const std::string expected_result;
687 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24688 {u"foo", metrics::OmniboxInputType::UNKNOWN,
Steven Holte3696c9412017-08-24 18:38:03689 "{google:baseURL}?{searchTerms}&{google:inputType}",
690 "http://www.google.com/?foo&oit=1&"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24691 {u"foo", metrics::OmniboxInputType::URL,
Steven Holte3696c9412017-08-24 18:38:03692 "{google:baseURL}?{searchTerms}&{google:inputType}",
693 "http://www.google.com/?foo&oit=3&"},
[email protected]420472b22014-06-10 13:34:43694 };
[email protected]420472b22014-06-10 13:34:43695 TemplateURLData data;
696 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02697 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]420472b22014-06-10 13:34:43698 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28699 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19700 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
701 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43702 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
703 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19704 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
705 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43706 ASSERT_TRUE(result.is_valid());
707 EXPECT_EQ(test_data[i].expected_result, result.spec());
708 }
709}
710
Tommy C. Li9a180482019-06-27 20:49:00711// Tests replacing omnibox focus type (&oft=).
712TEST_F(TemplateURLTest, ReplaceOmniboxFocusType) {
713 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01714 const std::u16string search_term;
Tommy Li5e883512020-08-07 19:32:04715 OmniboxFocusType focus_type;
Tommy C. Li9a180482019-06-27 20:49:00716 const std::string url;
717 const std::string expected_result;
718 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24719 {u"foo", OmniboxFocusType::DEFAULT,
Tommy C. Li9a180482019-06-27 20:49:00720 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
721 "http://www.google.com/?foo&"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24722 {u"foo", OmniboxFocusType::ON_FOCUS,
Tommy C. Li9a180482019-06-27 20:49:00723 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
724 "http://www.google.com/?foo&oft=1&"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24725 {u"foo", OmniboxFocusType::DELETED_PERMANENT_TEXT,
Tommy Li5e883512020-08-07 19:32:04726 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
727 "http://www.google.com/?foo&oft=2&"},
Tommy C. Li9a180482019-06-27 20:49:00728 };
729 TemplateURLData data;
730 data.input_encodings.push_back("UTF-8");
731 for (size_t i = 0; i < base::size(test_data); ++i) {
732 data.SetURL(test_data[i].url);
733 TemplateURL url(data);
734 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
735 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
736 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
Tommy Li5e883512020-08-07 19:32:04737 search_terms_args.focus_type = test_data[i].focus_type;
Tommy C. Li9a180482019-06-27 20:49:00738 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
739 search_terms_data_));
740 ASSERT_TRUE(result.is_valid());
741 EXPECT_EQ(test_data[i].expected_result, result.spec());
742 }
743}
744
Ryan Sturm29853d52020-11-17 02:14:04745// Tests replacing prefetch source (&pf=).
746TEST_F(TemplateURLTest, ReplaceIsPrefetch) {
747 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01748 const std::u16string search_term;
Ryan Sturm29853d52020-11-17 02:14:04749 bool is_prefetch;
750 const std::string url;
751 const std::string expected_result;
752 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24753 {u"foo", false, "{google:baseURL}?{searchTerms}&{google:prefetchSource}",
Ryan Sturm29853d52020-11-17 02:14:04754 "http://www.google.com/?foo&"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24755 {u"foo", true, "{google:baseURL}?{searchTerms}&{google:prefetchSource}",
Ryan Sturm29853d52020-11-17 02:14:04756 "http://www.google.com/?foo&pf=cs&"},
757 };
758 TemplateURLData data;
759 data.input_encodings.push_back("UTF-8");
760 for (size_t i = 0; i < base::size(test_data); ++i) {
761 data.SetURL(test_data[i].url);
762 TemplateURL url(data);
763 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
764 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
765 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
766 search_terms_args.is_prefetch = test_data[i].is_prefetch;
767 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
768 search_terms_data_));
769 ASSERT_TRUE(result.is_valid());
770 EXPECT_EQ(test_data[i].expected_result, result.spec());
771 }
772}
773
[email protected]9b9fa672013-11-07 06:04:52774// Tests replacing currentPageUrl.
775TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48776 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01777 const std::u16string search_term;
[email protected]9b9fa672013-11-07 06:04:52778 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48779 const std::string url;
780 const std::string expected_result;
781 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24782 {u"foo", "http://www.google.com/",
783 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
784 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&"},
785 {u"foo", "", "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
786 "http://www.google.com/?foo&"},
787 {u"foo", "http://g.com/+-/*&=",
788 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
789 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&"},
[email protected]800569d2013-05-06 07:38:48790 };
791 TemplateURLData data;
792 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02793 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]800569d2013-05-06 07:38:48794 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28795 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19796 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
797 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]800569d2013-05-06 07:38:48798 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52799 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19800 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
801 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48802 ASSERT_TRUE(result.is_valid());
803 EXPECT_EQ(test_data[i].expected_result, result.spec());
804 }
805}
806
Pavel Yatsuk0d6a7b5c2019-10-09 20:33:44807// Tests appending attribution parameter to queries originating from Play API
808// search engine.
809TEST_F(TemplateURLTest, PlayAPIAttribution) {
810 const struct TestData {
811 const char* url;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01812 std::u16string terms;
Pavel Yatsuk0d6a7b5c2019-10-09 20:33:44813 bool created_from_play_api;
814 const char* output;
Jan Wilken Dörrie756999e2021-03-23 15:05:24815 } test_data[] = {
816 {"http://foo/?q={searchTerms}", u"bar", false, "http://foo/?q=bar"},
817 {"http://foo/?q={searchTerms}", u"bar", true,
818 "http://foo/?q=bar&chrome_dse_attribution=1"}};
Pavel Yatsuk0d6a7b5c2019-10-09 20:33:44819 TemplateURLData data;
820 for (size_t i = 0; i < base::size(test_data); ++i) {
821 data.SetURL(test_data[i].url);
822 data.created_from_play_api = test_data[i].created_from_play_api;
823 TemplateURL url(data);
824 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
825 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
826 GURL result(url.url_ref().ReplaceSearchTerms(
827 TemplateURLRef::SearchTermsArgs(test_data[i].terms),
828 search_terms_data_));
829 ASSERT_TRUE(result.is_valid());
830 EXPECT_EQ(test_data[i].output, result.spec());
831 }
832}
833
[email protected]d82443b2009-01-15 19:54:56834TEST_F(TemplateURLTest, Suggestions) {
835 struct TestData {
836 const int accepted_suggestion;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01837 const std::u16string original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56838 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27839 } test_data[] = {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01840 {TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::u16string(),
841 "http://bar/foo?q=foobar"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24842 {TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, u"foo",
Jan Wilken Dörriefa241ba2021-03-11 17:57:01843 "http://bar/foo?q=foobar"},
844 {TemplateURLRef::NO_SUGGESTION_CHOSEN, std::u16string(),
845 "http://bar/foo?q=foobar"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24846 {TemplateURLRef::NO_SUGGESTION_CHOSEN, u"foo", "http://bar/foo?q=foobar"},
Jan Wilken Dörriefa241ba2021-03-11 17:57:01847 {0, std::u16string(), "http://bar/foo?oq=&q=foobar"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24848 {1, u"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) {
Jan Wilken Dörrie756999e2021-03-23 15:05:24858 TemplateURLRef::SearchTermsArgs search_terms_args(u"foobar");
[email protected]bca359b2012-06-24 07:53:04859 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
860 search_terms_args.original_query =
861 test_data[i].original_query_for_suggestion;
[email protected]ce7ee5f2014-06-16 23:41:19862 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
863 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27864 ASSERT_TRUE(result.is_valid());
865 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56866 }
867}
868
[email protected]81f808de2009-09-25 01:36:34869TEST_F(TemplateURLTest, RLZ) {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01870 std::u16string rlz_string = search_terms_data_.GetRlzParameterValue(false);
[email protected]d82443b2009-01-15 19:54:56871
[email protected]573889f22012-04-07 01:31:54872 TemplateURLData data;
873 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28874 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19875 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
876 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04877 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24878 TemplateURLRef::SearchTermsArgs(u"x"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56879 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44880 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
881 result.spec());
[email protected]d82443b2009-01-15 19:54:56882}
883
[email protected]c8ccc41d2014-04-10 04:42:12884TEST_F(TemplateURLTest, RLZFromAppList) {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01885 std::u16string rlz_string = search_terms_data_.GetRlzParameterValue(true);
[email protected]c8ccc41d2014-04-10 04:42:12886
887 TemplateURLData data;
888 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28889 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19890 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
891 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Jan Wilken Dörrie756999e2021-03-23 15:05:24892 TemplateURLRef::SearchTermsArgs args(u"x");
Moe Ahmadi4715d9542020-10-21 01:00:32893 args.request_source = TemplateURLRef::CROS_APP_LIST;
[email protected]ce7ee5f2014-06-16 23:41:19894 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12895 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44896 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
897 result.spec());
[email protected]c8ccc41d2014-04-10 04:42:12898}
[email protected]c8ccc41d2014-04-10 04:42:12899
[email protected]d82443b2009-01-15 19:54:56900TEST_F(TemplateURLTest, HostAndSearchTermKey) {
901 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19902 const std::string url;
[email protected]d82443b2009-01-15 19:54:56903 const std::string host;
904 const std::string path;
905 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54906 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50907 {"http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
908 {"http://blah/{searchTerms}", "blah", "", ""},
[email protected]d82443b2009-01-15 19:54:56909
vitbarf2a11562017-05-15 14:09:50910 // No term should result in empty values.
911 {"http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56912
vitbarf2a11562017-05-15 14:09:50913 // Multiple terms should result in empty values.
914 {"http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56915
vitbarf2a11562017-05-15 14:09:50916 // Term in the host shouldn't match.
917 {"http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56918
vitbarf2a11562017-05-15 14:09:50919 {"http://blah/?q={searchTerms}", "blah", "/", "q"},
920 {"https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56921
vitbarf2a11562017-05-15 14:09:50922 // Single term with extra chars in value should match.
923 {"http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56924 };
925
Avi Drissman2244c2d2018-12-25 23:08:02926 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54927 TemplateURLData data;
928 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28929 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19930 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
931 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
932 EXPECT_EQ(test_data[i].search_term_key,
933 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56934 }
935}
936
alexmos294849f2015-03-14 00:55:06937TEST_F(TemplateURLTest, SearchTermKeyLocation) {
938 struct TestData {
939 const std::string url;
940 const url::Parsed::ComponentType location;
941 const std::string path;
vitbarf2a11562017-05-15 14:09:50942 const std::string key;
943 const std::string value_prefix;
944 const std::string value_suffix;
alexmos294849f2015-03-14 00:55:06945 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50946 {"http://blah/{searchTerms}/", url::Parsed::PATH, "", "", "/", "/"},
947 {"http://blah/{searchTerms}", url::Parsed::PATH, "", "", "/", ""},
948 {"http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "", "",
949 "/begin/", "/end"},
950 {"http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY, "/", "q",
951 "", ""},
952 {"http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF, "/", "x",
953 "", ""},
954 {"http://www.example.com/?q=chromium-{searchTerms}@chromium.org/info",
955 url::Parsed::QUERY, "/", "q", "chromium-", "@chromium.org/info"},
alexmos294849f2015-03-14 00:55:06956
vitbarf2a11562017-05-15 14:09:50957 // searchTerms is a key, not a value, so this should result in an empty
958 // value.
959 {"http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x",
960 url::Parsed::QUERY, "", "", "", ""},
alexmos294849f2015-03-14 00:55:06961
vitbarf2a11562017-05-15 14:09:50962 // Multiple search terms should result in empty values.
963 {"http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY, "", "",
964 "", ""},
965 {"http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY, "", "",
966 "", ""},
967 {"http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY, "",
968 "", "", ""},
alexmos294849f2015-03-14 00:55:06969 };
970
Avi Drissman2244c2d2018-12-25 23:08:02971 for (size_t i = 0; i < base::size(test_data); ++i) {
alexmos294849f2015-03-14 00:55:06972 TemplateURLData data;
973 data.SetURL(test_data[i].url);
974 TemplateURL url(data);
975 EXPECT_EQ(test_data[i].location,
976 url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
977 EXPECT_EQ(test_data[i].path,
978 url.url_ref().GetPath(search_terms_data_));
vitbarf2a11562017-05-15 14:09:50979 EXPECT_EQ(test_data[i].key,
980 url.url_ref().GetSearchTermKey(search_terms_data_));
981 EXPECT_EQ(test_data[i].value_prefix,
982 url.url_ref().GetSearchTermValuePrefix(search_terms_data_));
983 EXPECT_EQ(test_data[i].value_suffix,
984 url.url_ref().GetSearchTermValueSuffix(search_terms_data_));
alexmos294849f2015-03-14 00:55:06985 }
986}
987
[email protected]d82443b2009-01-15 19:54:56988TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
989 static const struct {
[email protected]ddd231e2010-06-29 20:35:19990 const char* const base_url;
991 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56992 } data[] = {
[email protected]014010e2011-10-01 04:12:44993 { "http://google.com/", "http://google.com/complete/", },
994 { "http://www.google.com/", "http://www.google.com/complete/", },
995 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
996 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
997 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56998 };
999
Avi Drissman2244c2d2018-12-25 23:08:021000 for (size_t i = 0; i < base::size(data); ++i)
[email protected]d82443b2009-01-15 19:54:561001 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
1002}
1003
[email protected]81c6ef62010-01-21 09:58:471004TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:191005 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:541006 TemplateURLData data;
1007 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:281008 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471009 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:131010 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:191011 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:471012 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271013 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471014 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1015}
1016
1017TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:201018 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:541019 TemplateURLData data;
1020 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:281021 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471022 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:441023
1024 // By default, TemplateURLRef should not consider itself prepopulated.
1025 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:131026 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:201027 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:441028 EXPECT_TRUE(replacements.empty());
1029
1030 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:201031 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:541032 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:281033 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:151034 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:201035 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:471036 EXPECT_TRUE(replacements.empty());
1037}
1038
1039TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:281040 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:471041 TemplateURLRef::Replacements replacements;
1042 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241043 EXPECT_EQ(std::string(), url.url_ref().ParseURL(std::string(), &replacements,
1044 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471045 EXPECT_TRUE(replacements.empty());
1046 EXPECT_TRUE(valid);
1047}
1048
1049TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:541050 TemplateURLData data;
1051 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:281052 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471053 TemplateURLRef::Replacements replacements;
1054 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241055 EXPECT_EQ(std::string(),
1056 url.url_ref().ParseURL("{", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471057 EXPECT_TRUE(replacements.empty());
1058 EXPECT_FALSE(valid);
1059}
1060
1061TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:541062 TemplateURLData data;
1063 data.SetURL("{}");
[email protected]168d08722014-06-18 07:13:281064 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471065 TemplateURLRef::Replacements replacements;
1066 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241067 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471068 EXPECT_TRUE(replacements.empty());
1069 EXPECT_TRUE(valid);
1070}
1071
1072TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:541073 TemplateURLData data;
1074 data.SetURL("{}{{%s}}");
[email protected]168d08722014-06-18 07:13:281075 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471076 TemplateURLRef::Replacements replacements;
1077 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241078 EXPECT_EQ("{}{}", url.url_ref().ParseURL("{}{{searchTerms}}", &replacements,
1079 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471080 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271081 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471082 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1083 EXPECT_TRUE(valid);
1084}
1085
1086TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:541087 TemplateURLData data;
1088 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:281089 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471090 TemplateURLRef::Replacements replacements;
1091 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241092 EXPECT_EQ("{", url.url_ref().ParseURL("{{searchTerms}", &replacements,
1093 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471094 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271095 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471096 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1097 EXPECT_TRUE(valid);
1098}
[email protected]5b3078752012-10-09 18:54:161099
[email protected]fd6d8822012-12-08 06:56:111100TEST_F(TemplateURLTest, SearchClient) {
1101 const std::string base_url_str("http://google.com/?");
1102 const std::string terms_str("{searchTerms}&{google:searchClient}");
1103 const std::string full_url_str = base_url_str + terms_str;
Jan Wilken Dörriefa241ba2021-03-11 17:57:011104 const std::u16string terms(ASCIIToUTF16(terms_str));
[email protected]798baa8e2014-06-20 05:42:441105 search_terms_data_.set_google_base_url(base_url_str);
[email protected]fd6d8822012-12-08 06:56:111106
1107 TemplateURLData data;
1108 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:281109 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191110 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1111 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Jan Wilken Dörrie756999e2021-03-23 15:05:241112 TemplateURLRef::SearchTermsArgs search_terms_args(u"foobar");
[email protected]fd6d8822012-12-08 06:56:111113
1114 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:191115 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1116 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111117 ASSERT_TRUE(result.is_valid());
1118 EXPECT_EQ("http://google.com/?foobar&", result.spec());
1119
1120 // Check that the URL is correct when a client is present.
[email protected]798baa8e2014-06-20 05:42:441121 search_terms_data_.set_search_client("search_client");
[email protected]ce7ee5f2014-06-16 23:41:191122 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
1123 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111124 ASSERT_TRUE(result_2.is_valid());
[email protected]798baa8e2014-06-20 05:42:441125 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
[email protected]fd6d8822012-12-08 06:56:111126}
[email protected]fd6d8822012-12-08 06:56:111127
Moe Ahmadi4715d9542020-10-21 01:00:321128TEST_F(TemplateURLTest, SuggestClient) {
1129 const std::string base_url_str("http://google.com/?");
1130 const std::string query_params_str("client={google:suggestClient}");
1131 const std::string full_url_str = base_url_str + query_params_str;
1132 search_terms_data_.set_google_base_url(base_url_str);
1133
1134 TemplateURLData data;
1135 data.SetURL(full_url_str);
1136 TemplateURL url(data);
1137 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1138 ASSERT_FALSE(url.url_ref().SupportsReplacement(search_terms_data_));
1139 TemplateURLRef::SearchTermsArgs search_terms_args;
1140
1141 // Check that the URL is correct when a client is not present.
1142 GURL result(
1143 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_));
1144 ASSERT_TRUE(result.is_valid());
1145 EXPECT_EQ("http://google.com/?client=", result.spec());
1146
1147 // Check that the URL is correct when a client is present.
1148 search_terms_data_.set_suggest_client("suggest_client");
1149 GURL result_2(
1150 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_));
1151 ASSERT_TRUE(result_2.is_valid());
1152 EXPECT_EQ("http://google.com/?client=suggest_client", result_2.spec());
Moe Ahmadi4715d9542020-10-21 01:00:321153}
1154
Moe Ahmadi66fc8334c2021-11-19 22:21:581155TEST_F(TemplateURLTest, ZeroSuggestCacheDuration) {
1156 const std::string base_url_str("http://google.com/?");
1157 const std::string query_params_str("{google:clientCacheTimeToLive}");
1158 const std::string full_url_str = base_url_str + query_params_str;
1159 search_terms_data_.set_google_base_url(base_url_str);
1160 TemplateURLData data;
1161 data.SetURL(full_url_str);
1162 TemplateURL url(data);
1163 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1164 ASSERT_FALSE(url.url_ref().SupportsReplacement(search_terms_data_));
1165
1166 {
1167 // 'ccttl' query param should not be present if no cache duration is given.
1168 TemplateURLRef::SearchTermsArgs search_terms_args;
1169 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1170 search_terms_data_));
1171 ASSERT_TRUE(result.is_valid());
1172 EXPECT_EQ("http://google.com/?", result.spec());
1173 }
1174 {
1175 // 'ccttl' query param should be present if a positive cache duration is
1176 // given.
1177 TemplateURLRef::SearchTermsArgs search_terms_args;
1178 search_terms_args.zero_suggest_cache_duration_sec = 300;
1179 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1180 search_terms_data_));
1181 ASSERT_TRUE(result.is_valid());
1182 EXPECT_EQ("http://google.com/?ccttl=300&", result.spec());
1183 }
1184 {
1185 // 'ccttl' query param shouldn't be present if a zero cache duration is
1186 // given.
1187 TemplateURLRef::SearchTermsArgs search_terms_args;
1188 search_terms_args.zero_suggest_cache_duration_sec = 0;
1189 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1190 search_terms_data_));
1191 ASSERT_TRUE(result.is_valid());
1192 EXPECT_EQ("http://google.com/?", result.spec());
1193 }
1194 {
1195 // 'ccttl' query param should not be present if the request is not a
1196 // zero-prefix request.
1197 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
1198 search_terms_args.zero_suggest_cache_duration_sec = 300;
1199 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1200 search_terms_data_));
1201 ASSERT_TRUE(result.is_valid());
1202 EXPECT_EQ("http://google.com/?", result.spec());
1203 }
1204}
1205
[email protected]5b3078752012-10-09 18:54:161206TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
1207 TemplateURLData data;
1208 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161209 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
1210 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281211 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511212 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1213 ASSERT_EQ(3U, url_refs.size());
1214 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1215 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1216 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161217}
1218
1219TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
1220 TemplateURLData data;
1221 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:281222 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511223 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1224 ASSERT_EQ(1U, url_refs.size());
1225 EXPECT_EQ("http://www.google.co.uk/", url_refs[0].GetURL());
[email protected]5b3078752012-10-09 18:54:161226}
1227
1228TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1229 TemplateURLData data;
1230 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161231 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1232 data.alternate_urls.push_back(
1233 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281234 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011235 std::u16string result;
[email protected]5b3078752012-10-09 18:54:161236
1237 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191238 GURL("http://google.com/?q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241239 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191240
1241 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471242 GURL("http://google.com/?q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241243 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191244
1245 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471246 GURL("http://google.com/?q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241247 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191248
1249 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471250 GURL("http://google.com/?q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241251 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191252
1253 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191254 GURL("http://google.com/alt/#q=something"),
1255 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241256 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191257
1258 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471259 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241260 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191261
1262 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471263 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241264 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191265
1266 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471267 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241268 EXPECT_EQ(u"something", result);
[email protected]5b3078752012-10-09 18:54:161269
1270 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191271 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011272 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161273
1274 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191275 GURL("http://google.ca/?q=something&q=anything"),
1276 search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011277 EXPECT_EQ(std::u16string(), result);
[email protected]67d8b752013-04-03 17:33:271278
1279 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191280 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011281 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161282
[email protected]32e2d81b2012-10-16 19:03:251283 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191284 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241285 EXPECT_EQ(u"foo", result);
[email protected]5b3078752012-10-09 18:54:161286
1287 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191288 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011289 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161290
1291 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191292 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241293 EXPECT_EQ(u"1 2 3", result);
[email protected]5b3078752012-10-09 18:54:161294
1295 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191296 GURL("http://google.com/alt/?q=123#q=456"),
1297 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241298 EXPECT_EQ(u"456", result);
[email protected]5b3078752012-10-09 18:54:161299
1300 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191301 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1302 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241303 EXPECT_EQ(u"123", result);
[email protected]5b3078752012-10-09 18:54:161304
1305 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191306 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1307 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241308 EXPECT_EQ(u"789", result);
[email protected]5b3078752012-10-09 18:54:161309
1310 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191311 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011312 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161313
1314 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191315 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011316 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161317
1318 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191319 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011320 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161321
1322 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191323 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011324 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161325
1326 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191327 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241328 EXPECT_EQ(u"123", result);
[email protected]5b3078752012-10-09 18:54:161329}
[email protected]4076ea62013-01-09 01:47:191330
alexmos294849f2015-03-14 00:55:061331TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1332 TemplateURLData data;
1333 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1334 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011335 std::u16string result;
alexmos294849f2015-03-14 00:55:061336
1337 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1338 GURL("http://term-in-path.com/begin/something/end"),
1339 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241340 EXPECT_EQ(u"something", result);
alexmos294849f2015-03-14 00:55:061341
1342 // "%20" must be converted to space.
1343 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1344 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1345 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241346 EXPECT_EQ(u"a b c", result);
alexmos294849f2015-03-14 00:55:061347
1348 // Plus must not be converted to space.
1349 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1350 GURL("http://term-in-path.com/begin/1+2+3/end"),
1351 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241352 EXPECT_EQ(u"1+2+3", result);
alexmos294849f2015-03-14 00:55:061353
1354 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1355 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011356 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061357
1358 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1359 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011360 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061361
1362 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1363 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011364 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061365}
1366
vitbar27804d102015-04-15 10:54:411367// Checks that the ExtractSearchTermsFromURL function works correctly
1368// for urls containing non-latin characters in UTF8 encoding.
1369TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
1370 TemplateURLData data;
1371 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1372 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1373 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1374 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011375 std::u16string result;
vitbar27804d102015-04-15 10:54:411376
1377 // Russian text encoded with UTF-8.
1378 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551379 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1380 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
vitbar27804d102015-04-15 10:54:411381 search_terms_data_, &result));
1382 EXPECT_EQ(
Jan Wilken Dörrie72da4982021-03-19 19:21:091383 u"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1384 u"\x043C\x0438\x0440!",
vitbar27804d102015-04-15 10:54:411385 result);
1386
1387 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551388 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1389 "%D0%B0"),
vitbar27804d102015-04-15 10:54:411390 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331391 EXPECT_EQ(u"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430", result);
vitbar27804d102015-04-15 10:54:411392
1393 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551394 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1395 "%D0%B8%20A"),
vitbar27804d102015-04-15 10:54:411396 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331397 EXPECT_EQ(u"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A", result);
vitbar27804d102015-04-15 10:54:411398}
1399
1400// Checks that the ExtractSearchTermsFromURL function works correctly
1401// for urls containing non-latin characters in non-UTF8 encoding.
1402TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) {
1403 TemplateURLData data;
1404 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1405 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1406 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1407 data.input_encodings.push_back("windows-1251");
1408 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011409 std::u16string result;
vitbar27804d102015-04-15 10:54:411410
1411 // Russian text encoded with Windows-1251.
1412 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1413 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1414 "%EC%E8%F0!"),
1415 search_terms_data_, &result));
1416 EXPECT_EQ(
Jan Wilken Dörrie72da4982021-03-19 19:21:091417 u"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1418 u"\x043C\x0438\x0440!",
vitbar27804d102015-04-15 10:54:411419 result);
1420
1421 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1422 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1423 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331424 EXPECT_EQ(u"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430", result);
vitbar27804d102015-04-15 10:54:411425
1426 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1427 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1428 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331429 EXPECT_EQ(u"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A", result);
vitbar27804d102015-04-15 10:54:411430}
1431
jbroman50832022016-04-21 23:53:001432// Checks that the ExtractSearchTermsFromURL function strips constant
1433// prefix/suffix strings from the search terms param.
1434TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) {
1435 TemplateURLData data;
1436 data.alternate_urls.push_back(
1437 "http://www.example.com/?q=chromium-{searchTerms}@chromium.org");
1438 data.alternate_urls.push_back(
1439 "http://www.example.com/chromium-{searchTerms}@chromium.org/info");
1440 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011441 std::u16string result;
jbroman50832022016-04-21 23:53:001442
1443 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1444 GURL("http://www.example.com/[email protected]"),
1445 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241446 EXPECT_EQ(u"dev", result);
jbroman50832022016-04-21 23:53:001447
1448 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1449 GURL("http://www.example.com/[email protected]/info"),
1450 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241451 EXPECT_EQ(u"dev", result);
jbroman50832022016-04-21 23:53:001452
1453 // Don't match if the prefix and suffix aren't there.
1454 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1455 GURL("http://www.example.com/?q=invalid"), search_terms_data_, &result));
jbromane8b8728d2017-04-12 17:29:391456
1457 // Don't match if the prefix and suffix overlap.
1458 TemplateURLData data_with_overlap;
1459 data.alternate_urls.push_back(
1460 "http://www.example.com/?q=goo{searchTerms}oogle");
1461 TemplateURL url_with_overlap(data);
1462 EXPECT_FALSE(url_with_overlap.ExtractSearchTermsFromURL(
1463 GURL("http://www.example.com/?q=google"), search_terms_data_, &result));
jbroman50832022016-04-21 23:53:001464}
1465
[email protected]f62e30f52013-03-23 03:45:151466TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1467 TemplateURLData data;
1468 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]f62e30f52013-03-23 03:45:151469 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1470 data.alternate_urls.push_back(
1471 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281472 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241473 TemplateURLRef::SearchTermsArgs search_terms(u"Bob Morane");
[email protected]f62e30f52013-03-23 03:45:151474 GURL result;
1475
1476 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191477 GURL("http://google.com/?q=something"), search_terms,
1478 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101479 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151480
1481 result = GURL("http://should.not.change.com");
1482 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191483 GURL("http://google.ca/?q=something"), search_terms,
1484 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151485 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1486
1487 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191488 GURL("http://google.com/foo/?q=foo"), search_terms,
1489 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151490
1491 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191492 GURL("https://google.com/?q=foo"), search_terms,
1493 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101494 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151495
1496 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191497 GURL("http://google.com:8080/?q=foo"), search_terms,
1498 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151499
1500 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191501 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1502 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101503 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result);
[email protected]f62e30f52013-03-23 03:45:151504
1505 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1506 // template_url.cc for details.
1507 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191508 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1509 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101510 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151511
1512 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1513 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191514 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101515 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
[email protected]f62e30f52013-03-23 03:45:151516 result);
1517
1518 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1519 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191520 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151521 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
vitbarf298455d2015-04-21 12:58:101522 "#j=abc&q=Bob+Morane&h=def9"), result);
[email protected]f62e30f52013-03-23 03:45:151523
1524 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191525 GURL("http://google.com/alt/?q="), search_terms,
1526 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151527
1528 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191529 GURL("http://google.com/alt/?#q="), search_terms,
1530 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151531
1532 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191533 GURL("http://google.com/alt/?q=#q="), search_terms,
1534 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151535
1536 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191537 GURL("http://google.com/alt/?q=123#q="), search_terms,
1538 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151539
1540 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191541 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1542 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101543 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151544}
[email protected]56fa29592013-07-02 20:25:531545
alexmos294849f2015-03-14 00:55:061546TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1547 TemplateURLData data;
1548 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1549 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241550 TemplateURLRef::SearchTermsArgs search_terms(u"Bob Morane");
alexmos294849f2015-03-14 00:55:061551 GURL result;
1552
1553 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1554 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1555 search_terms_data_, &result));
1556 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1557
1558 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1559 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1560 search_terms_data_, &result));
1561 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1562
1563 result = GURL("http://should.not.change.com");
1564 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1565 GURL("http://term-in-path.com/about"), search_terms,
1566 search_terms_data_, &result));
1567 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1568}
1569
vitbarf298455d2015-04-21 12:58:101570// Checks that the ReplaceSearchTermsInURL function works correctly
1571// for search terms containing non-latin characters for a search engine
1572// using UTF-8 input encoding.
1573TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) {
1574 TemplateURLData data;
1575 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1576 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1577 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1578 TemplateURL url(data);
1579
1580 // Russian text which will be encoded with UTF-8.
Jan Wilken Dörrie72da4982021-03-19 19:21:091581 TemplateURLRef::SearchTermsArgs search_terms(
1582 u"\x0442\x0435\x043A\x0441\x0442");
vitbarf298455d2015-04-21 12:58:101583 GURL result;
1584
1585 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1586 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_,
1587 &result));
1588 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1589 result);
1590
1591 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1592 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_,
1593 &result));
1594 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1595 result);
1596
1597 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1598 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_,
1599 &result));
1600 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1601 result);
1602}
1603
1604// Checks that the ReplaceSearchTermsInURL function works correctly
1605// for search terms containing non-latin characters for a search engine
1606// using non UTF-8 input encoding.
1607TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) {
1608 TemplateURLData data;
1609 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1610 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1611 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1612 data.input_encodings.push_back("windows-1251");
1613 TemplateURL url(data);
1614
1615 // Russian text which will be encoded with Windows-1251.
Jan Wilken Dörrie72da4982021-03-19 19:21:091616 TemplateURLRef::SearchTermsArgs search_terms(
1617 u"\x0442\x0435\x043A\x0441\x0442");
vitbarf298455d2015-04-21 12:58:101618 GURL result;
1619
1620 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1621 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_,
1622 &result));
1623 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1624 result);
1625
1626 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1627 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_,
1628 &result));
1629 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1630 result);
1631
1632 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1633 GURL("http://windows-1251.ru/path/a%20b"), search_terms,
1634 search_terms_data_, &result));
1635 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1636 result);
1637}
1638
Mark Pearson247fb8a2018-08-30 05:12:261639// Test the |additional_query_params| field of SearchTermsArgs.
[email protected]621ade062013-10-28 06:27:431640TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431641 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]621ade062013-10-28 06:27:431647
Mark Pearson247fb8a2018-08-30 05:12:261648 // Baseline: no |additional_query_params| field.
Jan Wilken Dörrie756999e2021-03-23 15:05:241649 TemplateURLRef::SearchTermsArgs search_terms(u"abc");
1650 search_terms.original_query = u"def";
[email protected]621ade062013-10-28 06:27:431651 search_terms.accepted_suggestion = 0;
1652 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191653 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431654
Mark Pearson247fb8a2018-08-30 05:12:261655 // Set the |additional_query_params|.
1656 search_terms.additional_query_params = "pq=xyz";
[email protected]621ade062013-10-28 06:27:431657 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191658 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431659
Mark Pearson247fb8a2018-08-30 05:12:261660 // Add |append_extra_query_params_from_command_line| into the mix, and ensure
1661 // it works.
1662 search_terms.append_extra_query_params_from_command_line = true;
avi1772c1a2014-12-22 22:42:331663 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]621ade062013-10-28 06:27:431664 switches::kExtraSearchQueryParams, "a=b");
1665 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191666 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431667}
1668
Mark Pearson247fb8a2018-08-30 05:12:261669// Test the |search_terms.append_extra_query_params_from_command_line| field of
1670// SearchTermsArgs.
[email protected]56fa29592013-07-02 20:25:531671TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531672 TemplateURLData data;
1673 // Pick a URL with replacements before, during, and after the query, to ensure
1674 // we don't goof up any of them.
1675 data.SetURL("{google:baseURL}search?q={searchTerms}"
1676 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281677 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531678
Mark Pearson247fb8a2018-08-30 05:12:261679 // Baseline: no command-line args, no
1680 // |search_terms.append_extra_query_params_from_command_line| flag.
Jan Wilken Dörrie756999e2021-03-23 15:05:241681 TemplateURLRef::SearchTermsArgs search_terms(u"abc");
1682 search_terms.original_query = u"def";
[email protected]56fa29592013-07-02 20:25:531683 search_terms.accepted_suggestion = 0;
1684 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191685 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531686
1687 // Set the flag. Since there are no command-line args, this should have no
1688 // effect.
Mark Pearson247fb8a2018-08-30 05:12:261689 search_terms.append_extra_query_params_from_command_line = true;
[email protected]56fa29592013-07-02 20:25:531690 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191691 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531692
1693 // Now append the command-line arg. This should be inserted into the query.
avi1772c1a2014-12-22 22:42:331694 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]56fa29592013-07-02 20:25:531695 switches::kExtraSearchQueryParams, "a=b");
1696 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191697 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531698
1699 // Turn off the flag. Now the command-line arg should be ignored again.
Mark Pearson247fb8a2018-08-30 05:12:261700 search_terms.append_extra_query_params_from_command_line = false;
[email protected]56fa29592013-07-02 20:25:531701 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191702 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531703}
[email protected]d5015ca2013-08-08 22:04:181704
1705// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171706TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181707 TemplateURLData data;
1708 data.input_encodings.push_back("UTF-8");
1709 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281710 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191711 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1712 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Jan Wilken Dörrie756999e2021-03-23 15:05:241713 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]d5015ca2013-08-08 22:04:181714
[email protected]ce7ee5f2014-06-16 23:41:191715 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1716 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181717 EXPECT_EQ("http://www.google.com/?q=foo", result);
1718
[email protected]332d17d22014-06-20 16:56:031719 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191720 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1721 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181722 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1723
1724 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031725 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191726 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1727 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181728 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1729}
[email protected]2328ee22013-08-08 23:00:191730
1731// Test the IsSearchResults function.
1732TEST_F(TemplateURLTest, IsSearchResults) {
1733 TemplateURLData data;
1734 data.SetURL("http://bar/search?q={searchTerms}");
[email protected]2767c0fd2013-08-16 17:44:161735 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191736 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1737 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1738 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1739 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281740 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191741
1742 const struct {
1743 const char* const url;
1744 bool result;
1745 } url_data[] = {
1746 { "http://bar/search?q=foo&oq=foo", true, },
1747 { "http://bar/?q=foo&oq=foo", true, },
1748 { "http://bar/#output=search&q=foo&oq=foo", true, },
1749 { "http://bar/webhp#q=foo&oq=foo", true, },
1750 { "http://bar/#q=foo&oq=foo", true, },
1751 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1752 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1753 { "http://bar/", false, },
1754 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161755 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191756 };
1757
Avi Drissman2244c2d2018-12-25 23:08:021758 for (size_t i = 0; i < base::size(url_data); ++i) {
[email protected]2328ee22013-08-08 23:00:191759 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191760 search_provider.IsSearchURL(GURL(url_data[i].url),
1761 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191762 }
1763}
[email protected]a048de8a2013-10-02 18:30:061764
jdonnelly41c5b46a2015-07-10 21:24:381765TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) {
[email protected]9d70de12014-05-10 02:15:311766 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441767 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311768 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1769
[email protected]168d08722014-06-18 07:13:281770 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241771 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]ce7ee5f2014-06-16 23:41:191772 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1773 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311774 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1775}
[email protected]20184242014-05-14 02:57:421776
1777TEST_F(TemplateURLTest, SessionToken) {
1778 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441779 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421780 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1781
[email protected]168d08722014-06-18 07:13:281782 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241783 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]20184242014-05-14 02:57:421784 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191785 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1786 search_terms_data_);
[email protected]20184242014-05-14 02:57:421787 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1788
[email protected]168d08722014-06-18 07:13:281789 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421790 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191791 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1792 search_terms_data_);
[email protected]20184242014-05-14 02:57:421793 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1794}
[email protected]448b17f52014-06-13 01:08:031795
1796TEST_F(TemplateURLTest, ContextualSearchParameters) {
1797 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441798 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031799 data.SetURL("http://bar/_/contextualsearch?"
1800 "{google:contextualSearchVersion}"
1801 "{google:contextualSearchContextData}");
1802
[email protected]168d08722014-06-18 07:13:281803 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241804 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]ce7ee5f2014-06-16 23:41:191805 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1806 search_terms_data_);
donndbb98274e2016-11-01 21:04:401807 EXPECT_EQ("http://bar/_/contextualsearch?", result);
[email protected]448b17f52014-06-13 01:08:031808
Donn Denman01b1bf72018-08-14 16:25:201809 // Test the current common case, which uses no home country or previous
1810 // event.
1811 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
Donn Denman97be51df2020-06-24 01:23:191812 2, 1, std::string(), 0, 0, false, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391813 std::string(), std::string());
[email protected]448b17f52014-06-13 01:08:031814 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191815 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1816 search_terms_data_);
donndd9fc4d92016-09-16 00:30:491817 EXPECT_EQ(
1818 "http://bar/_/contextualsearch?"
donnd0bc73292017-01-17 22:07:561819 "ctxs=2&"
donndd9fc4d92016-09-16 00:30:491820 "ctxsl_coca=1",
1821 result);
donndbb98274e2016-11-01 21:04:401822
Donn Denman01b1bf72018-08-14 16:25:201823 // Test the home country and non-zero event data case.
donndbb98274e2016-11-01 21:04:401824 search_terms_args.contextual_search_params =
Donn Denmandf4ede0f2020-01-17 23:11:221825 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denman97be51df2020-06-24 01:23:191826 2, 2, "CH", 1657713458, 5, false, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391827 std::string(), std::string());
donndbb98274e2016-11-01 21:04:401828 result =
1829 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1830
1831 EXPECT_EQ(
1832 "http://bar/_/contextualsearch?"
1833 "ctxs=2&"
donnd0bc73292017-01-17 22:07:561834 "ctxsl_coca=2&"
Donn Denman01b1bf72018-08-14 16:25:201835 "ctxs_hc=CH&"
1836 "ctxsl_pid=1657713458&"
1837 "ctxsl_per=5",
donndbb98274e2016-11-01 21:04:401838 result);
Donn Denmandf4ede0f2020-01-17 23:11:221839
1840 // Test exact-search.
1841 search_terms_args.contextual_search_params =
1842 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denman97be51df2020-06-24 01:23:191843 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391844 std::string(), std::string());
Donn Denmandf4ede0f2020-01-17 23:11:221845 result =
1846 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
Donn Denmandf4ede0f2020-01-17 23:11:221847 // Find our param.
1848 size_t found_pos = result.find("ctxsl_exact=1");
1849 EXPECT_NE(found_pos, std::string::npos);
Donn Denman7a241012020-01-31 18:33:131850
1851 // Test source and target languages.
1852 search_terms_args.contextual_search_params =
1853 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denmand638f4602020-11-03 21:05:391854 2, 1, std::string(), 0, 0, true, "es", "de", std::string(),
1855 std::string());
Donn Denman7a241012020-01-31 18:33:131856 result =
1857 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1858 // Find our params.
1859 size_t source_pos = result.find("tlitesl=es");
1860 EXPECT_NE(source_pos, std::string::npos);
1861 size_t target_pos = result.find("tlitetl=de");
1862 EXPECT_NE(target_pos, std::string::npos);
Donn Denman97be51df2020-06-24 01:23:191863
1864 // Test fluent languages.
1865 search_terms_args.contextual_search_params =
1866 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
1867 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391868 "es,de", std::string());
Donn Denman97be51df2020-06-24 01:23:191869 result =
1870 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1871 // Find our param. These may actually be URL encoded.
1872 size_t fluent_pos = result.find("&ctxs_fls=es,de");
1873 EXPECT_NE(fluent_pos, std::string::npos);
Donn Denmand638f4602020-11-03 21:05:391874
1875 // Test Related Searches.
1876 search_terms_args.contextual_search_params =
1877 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
1878 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
1879 std::string(), "1RbCu");
1880 result =
1881 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1882 // Find our param.
1883 size_t ctxsl_rs_pos = result.find("&ctxsl_rs=1RbCu");
1884 EXPECT_NE(ctxsl_rs_pos, std::string::npos);
[email protected]448b17f52014-06-13 01:08:031885}
[email protected]44ccc9f2014-06-20 17:36:211886
1887TEST_F(TemplateURLTest, GenerateKeyword) {
Jan Wilken Dörrie756999e2021-03-23 15:05:241888 ASSERT_EQ(u"foo", TemplateURL::GenerateKeyword(GURL("http://foo")));
1889 ASSERT_EQ(u"foo.", TemplateURL::GenerateKeyword(GURL("http://foo.")));
Emily Stark0fb634f82020-07-14 18:00:391890 // www. should be stripped for a public hostname but not a private/intranet
1891 // hostname.
Jan Wilken Dörrie756999e2021-03-23 15:05:241892 ASSERT_EQ(u"google.com",
Emily Stark0fb634f82020-07-14 18:00:391893 TemplateURL::GenerateKeyword(GURL("http://www.google.com")));
Jan Wilken Dörrie756999e2021-03-23 15:05:241894 ASSERT_EQ(u"www.foo", TemplateURL::GenerateKeyword(GURL("http://www.foo")));
[email protected]44ccc9f2014-06-20 17:36:211895 // Make sure we don't get a trailing '/'.
Jan Wilken Dörrie756999e2021-03-23 15:05:241896 ASSERT_EQ(u"blah", TemplateURL::GenerateKeyword(GURL("http://blah/")));
[email protected]44ccc9f2014-06-20 17:36:211897 // Don't generate the empty string.
Jan Wilken Dörrie756999e2021-03-23 15:05:241898 ASSERT_EQ(u"www.", TemplateURL::GenerateKeyword(GURL("http://www.")));
Peter Kastingcc07b332021-05-07 22:58:251899 ASSERT_EQ(u"абв", TemplateURL::GenerateKeyword(GURL("http://xn--80acd")));
a-v-y81695d0d2017-04-20 08:22:221900
1901 // Generated keywords must always be in lowercase, because TemplateURLs always
1902 // converts keywords to lowercase in its constructor and TemplateURLService
1903 // stores TemplateURLs in maps using keyword as key.
1904 EXPECT_TRUE(IsLowerCase(TemplateURL::GenerateKeyword(GURL("http://BLAH/"))));
1905 EXPECT_TRUE(IsLowerCase(
1906 TemplateURL::GenerateKeyword(GURL("http://embeddedhtml.<head>/"))));
[email protected]44ccc9f2014-06-20 17:36:211907}
1908
1909TEST_F(TemplateURLTest, GenerateSearchURL) {
1910 struct GenerateSearchURLCase {
1911 const char* test_name;
1912 const char* url;
1913 const char* expected;
1914 } generate_url_cases[] = {
1915 { "invalid URL", "foo{searchTerms}", "" },
1916 { "URL with no replacements", "http://foo/", "http://foo/" },
1917 { "basic functionality", "http://foo/{searchTerms}",
1918 "http://foo/blah.blah.blah.blah.blah" }
1919 };
1920
Avi Drissman2244c2d2018-12-25 23:08:021921 for (size_t i = 0; i < base::size(generate_url_cases); ++i) {
[email protected]44ccc9f2014-06-20 17:36:211922 TemplateURLData data;
1923 data.SetURL(generate_url_cases[i].url);
1924 TemplateURL t_url(data);
1925 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1926 generate_url_cases[i].expected)
1927 << generate_url_cases[i].test_name << " failed.";
1928 }
1929}
[email protected]9e9130ce2014-06-23 23:20:511930
1931TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1932 TemplateURLData data;
1933 search_terms_data_.set_google_base_url("http://bar/");
1934 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1935
1936 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241937 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]9e9130ce2014-06-23 23:20:511938 search_terms_args.prefetch_query = "full query text";
1939 search_terms_args.prefetch_query_type = "2338";
1940 std::string result =
1941 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1942 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1943 result);
1944
1945 TemplateURL url2(data);
1946 search_terms_args.prefetch_query.clear();
1947 search_terms_args.prefetch_query_type.clear();
1948 result =
1949 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1950 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1951}
vitbar5bd8c252016-01-29 18:44:511952
1953// Tests that TemplateURL works correctly after changing the Google base URL
1954// and invalidating cached values.
1955TEST_F(TemplateURLTest, InvalidateCachedValues) {
1956 TemplateURLData data;
1957 data.SetURL("{google:baseURL}search?q={searchTerms}");
1958 data.suggestions_url = "{google:baseSuggestURL}search?q={searchTerms}";
vitbar5bd8c252016-01-29 18:44:511959 data.image_url = "{google:baseURL}searchbyimage/upload";
1960 data.new_tab_url = "{google:baseURL}_/chrome/newtab";
1961 data.contextual_search_url = "{google:baseURL}_/contextualsearch";
1962 data.alternate_urls.push_back("{google:baseURL}s#q={searchTerms}");
1963 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241964 TemplateURLRef::SearchTermsArgs search_terms_args(u"X");
Jan Wilken Dörriefa241ba2021-03-11 17:57:011965 std::u16string search_terms;
vitbar5bd8c252016-01-29 18:44:511966
1967 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1968 EXPECT_EQ("http://www.google.com/search?q=X",
1969 url.url_ref().ReplaceSearchTerms(search_terms_args,
1970 search_terms_data_));
1971 EXPECT_EQ("http://www.google.com/s#q=X",
1972 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1973 search_terms_data_));
1974 EXPECT_EQ("http://www.google.com/search?q=X",
1975 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1976 search_terms_data_));
1977 EXPECT_EQ("http://www.google.com/complete/search?q=X",
1978 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1979 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:511980 EXPECT_EQ("http://www.google.com/searchbyimage/upload",
1981 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1982 search_terms_data_));
1983 EXPECT_EQ("http://www.google.com/_/chrome/newtab",
1984 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1985 search_terms_data_));
1986 EXPECT_EQ("http://www.google.com/_/contextualsearch",
1987 url.contextual_search_url_ref().ReplaceSearchTerms(
1988 search_terms_args, search_terms_data_));
1989
1990 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1991 GURL("http://www.google.com/search?q=Y+Z"),
1992 search_terms_data_, &search_terms));
Jan Wilken Dörrie756999e2021-03-23 15:05:241993 EXPECT_EQ(u"Y Z", search_terms);
vitbar5bd8c252016-01-29 18:44:511994 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1995 GURL("http://www.google.com/s#q=123"),
1996 search_terms_data_, &search_terms));
Jan Wilken Dörrie756999e2021-03-23 15:05:241997 EXPECT_EQ(u"123", search_terms);
vitbar5bd8c252016-01-29 18:44:511998
1999 search_terms_data_.set_google_base_url("https://www.foo.org/");
2000 url.InvalidateCachedValues();
2001
2002 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
2003 EXPECT_EQ("https://www.foo.org/search?q=X",
2004 url.url_ref().ReplaceSearchTerms(search_terms_args,
2005 search_terms_data_));
2006 EXPECT_EQ("https://www.foo.org/s#q=X",
2007 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
2008 search_terms_data_));
2009 EXPECT_EQ("https://www.foo.org/search?q=X",
2010 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
2011 search_terms_data_));
2012 EXPECT_EQ("https://www.foo.org/complete/search?q=X",
2013 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
2014 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:512015 EXPECT_EQ("https://www.foo.org/searchbyimage/upload",
2016 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
2017 search_terms_data_));
2018 EXPECT_EQ("https://www.foo.org/_/chrome/newtab",
2019 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
2020 search_terms_data_));
2021 EXPECT_EQ("https://www.foo.org/_/contextualsearch",
2022 url.contextual_search_url_ref().ReplaceSearchTerms(
2023 search_terms_args, search_terms_data_));
2024
2025 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
2026 GURL("https://www.foo.org/search?q=Y+Z"),
2027 search_terms_data_, &search_terms));
Jan Wilken Dörrie756999e2021-03-23 15:05:242028 EXPECT_EQ(u"Y Z", search_terms);
vitbar5bd8c252016-01-29 18:44:512029 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
2030 GURL("https://www.foo.org/s#q=123"),
2031 search_terms_data_, &search_terms));
Jan Wilken Dörrie756999e2021-03-23 15:05:242032 EXPECT_EQ(u"123", search_terms);
vitbar5bd8c252016-01-29 18:44:512033
2034 search_terms_data_.set_google_base_url("http://www.google.com/");
2035}
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002036
2037// Tests the use of wildcards in the path to ensure both extracting search terms
2038// and generating a search URL work correctly.
2039TEST_F(TemplateURLTest, PathWildcard) {
2040 TemplateURLData data;
2041 data.SetURL(
2042 "https://www.google.com/search{google:pathWildcard}?q={searchTerms}");
2043 TemplateURL url(data);
2044
2045 // Test extracting search terms from a URL.
Jan Wilken Dörriefa241ba2021-03-11 17:57:012046 std::u16string search_terms;
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002047 url.ExtractSearchTermsFromURL(GURL("https://www.google.com/search?q=testing"),
2048 search_terms_data_, &search_terms);
Jan Wilken Dörrie756999e2021-03-23 15:05:242049 EXPECT_EQ(u"testing", search_terms);
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002050 url.ExtractSearchTermsFromURL(
2051 GURL("https://www.google.com/search;_this_is_a_test;_?q=testing"),
2052 search_terms_data_, &search_terms);
Jan Wilken Dörrie756999e2021-03-23 15:05:242053 EXPECT_EQ(u"testing", search_terms);
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002054
2055 // Tests overlapping prefix/suffix.
2056 data.SetURL(
2057 "https://www.google.com/search{google:pathWildcard}rch?q={searchTerms}");
2058 TemplateURL overlap_url(data);
2059 overlap_url.ExtractSearchTermsFromURL(
2060 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
2061 &search_terms);
2062 EXPECT_TRUE(search_terms.empty());
2063
2064 // Tests wildcard at beginning of path so we only have a suffix.
2065 data.SetURL(
2066 "https://www.google.com/{google:pathWildcard}rch?q={searchTerms}");
2067 TemplateURL suffix_url(data);
2068 suffix_url.ExtractSearchTermsFromURL(
2069 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
2070 &search_terms);
Jan Wilken Dörrie756999e2021-03-23 15:05:242071 EXPECT_EQ(u"testing", search_terms);
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002072
2073 // Tests wildcard between prefix/suffix.
2074 overlap_url.ExtractSearchTermsFromURL(
2075 GURL("https://www.google.com/search_testing_rch?q=testing"),
2076 search_terms_data_, &search_terms);
Jan Wilken Dörrie756999e2021-03-23 15:05:242077 EXPECT_EQ(u"testing", search_terms);
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002078
2079 // Test generating a URL.
Jan Wilken Dörrie756999e2021-03-23 15:05:242080 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002081 GURL generated_url;
2082 url.ReplaceSearchTermsInURL(url.GenerateSearchURL(search_terms_data_),
2083 search_terms_args, search_terms_data_,
2084 &generated_url);
2085 EXPECT_EQ("https://www.google.com/search?q=foo", generated_url.spec());
2086}