blob: cf14fd08b8bce2045219c03f0aaa402271a1b874 [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
[email protected]5b3078752012-10-09 18:54:161155TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
1156 TemplateURLData data;
1157 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161158 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
1159 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281160 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511161 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1162 ASSERT_EQ(3U, url_refs.size());
1163 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1164 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1165 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161166}
1167
1168TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
1169 TemplateURLData data;
1170 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:281171 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511172 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1173 ASSERT_EQ(1U, url_refs.size());
1174 EXPECT_EQ("http://www.google.co.uk/", url_refs[0].GetURL());
[email protected]5b3078752012-10-09 18:54:161175}
1176
1177TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1178 TemplateURLData data;
1179 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161180 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1181 data.alternate_urls.push_back(
1182 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281183 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011184 std::u16string result;
[email protected]5b3078752012-10-09 18:54:161185
1186 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191187 GURL("http://google.com/?q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241188 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191189
1190 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471191 GURL("http://google.com/?q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241192 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191193
1194 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471195 GURL("http://google.com/?q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241196 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191197
1198 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471199 GURL("http://google.com/?q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241200 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191201
1202 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191203 GURL("http://google.com/alt/#q=something"),
1204 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241205 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191206
1207 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471208 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241209 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191210
1211 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471212 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241213 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191214
1215 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471216 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241217 EXPECT_EQ(u"something", result);
[email protected]5b3078752012-10-09 18:54:161218
1219 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191220 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011221 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161222
1223 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191224 GURL("http://google.ca/?q=something&q=anything"),
1225 search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011226 EXPECT_EQ(std::u16string(), result);
[email protected]67d8b752013-04-03 17:33:271227
1228 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191229 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011230 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161231
[email protected]32e2d81b2012-10-16 19:03:251232 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191233 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241234 EXPECT_EQ(u"foo", result);
[email protected]5b3078752012-10-09 18:54:161235
1236 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191237 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011238 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161239
1240 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191241 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241242 EXPECT_EQ(u"1 2 3", result);
[email protected]5b3078752012-10-09 18:54:161243
1244 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191245 GURL("http://google.com/alt/?q=123#q=456"),
1246 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241247 EXPECT_EQ(u"456", result);
[email protected]5b3078752012-10-09 18:54:161248
1249 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191250 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1251 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241252 EXPECT_EQ(u"123", result);
[email protected]5b3078752012-10-09 18:54:161253
1254 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191255 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1256 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241257 EXPECT_EQ(u"789", result);
[email protected]5b3078752012-10-09 18:54:161258
1259 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191260 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011261 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161262
1263 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191264 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011265 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161266
1267 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191268 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011269 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161270
1271 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191272 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011273 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161274
1275 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191276 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241277 EXPECT_EQ(u"123", result);
[email protected]5b3078752012-10-09 18:54:161278}
[email protected]4076ea62013-01-09 01:47:191279
alexmos294849f2015-03-14 00:55:061280TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1281 TemplateURLData data;
1282 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1283 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011284 std::u16string result;
alexmos294849f2015-03-14 00:55:061285
1286 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1287 GURL("http://term-in-path.com/begin/something/end"),
1288 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241289 EXPECT_EQ(u"something", result);
alexmos294849f2015-03-14 00:55:061290
1291 // "%20" must be converted to space.
1292 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1293 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1294 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241295 EXPECT_EQ(u"a b c", result);
alexmos294849f2015-03-14 00:55:061296
1297 // Plus must not be converted to space.
1298 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1299 GURL("http://term-in-path.com/begin/1+2+3/end"),
1300 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241301 EXPECT_EQ(u"1+2+3", result);
alexmos294849f2015-03-14 00:55:061302
1303 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1304 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011305 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061306
1307 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1308 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011309 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061310
1311 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1312 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011313 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061314}
1315
vitbar27804d102015-04-15 10:54:411316// Checks that the ExtractSearchTermsFromURL function works correctly
1317// for urls containing non-latin characters in UTF8 encoding.
1318TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
1319 TemplateURLData data;
1320 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1321 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1322 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1323 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011324 std::u16string result;
vitbar27804d102015-04-15 10:54:411325
1326 // Russian text encoded with UTF-8.
1327 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551328 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1329 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
vitbar27804d102015-04-15 10:54:411330 search_terms_data_, &result));
1331 EXPECT_EQ(
Jan Wilken Dörrie72da4982021-03-19 19:21:091332 u"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1333 u"\x043C\x0438\x0440!",
vitbar27804d102015-04-15 10:54:411334 result);
1335
1336 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551337 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1338 "%D0%B0"),
vitbar27804d102015-04-15 10:54:411339 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331340 EXPECT_EQ(u"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430", result);
vitbar27804d102015-04-15 10:54:411341
1342 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551343 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1344 "%D0%B8%20A"),
vitbar27804d102015-04-15 10:54:411345 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331346 EXPECT_EQ(u"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A", result);
vitbar27804d102015-04-15 10:54:411347}
1348
1349// Checks that the ExtractSearchTermsFromURL function works correctly
1350// for urls containing non-latin characters in non-UTF8 encoding.
1351TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) {
1352 TemplateURLData data;
1353 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1354 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1355 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1356 data.input_encodings.push_back("windows-1251");
1357 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011358 std::u16string result;
vitbar27804d102015-04-15 10:54:411359
1360 // Russian text encoded with Windows-1251.
1361 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1362 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1363 "%EC%E8%F0!"),
1364 search_terms_data_, &result));
1365 EXPECT_EQ(
Jan Wilken Dörrie72da4982021-03-19 19:21:091366 u"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1367 u"\x043C\x0438\x0440!",
vitbar27804d102015-04-15 10:54:411368 result);
1369
1370 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1371 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1372 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331373 EXPECT_EQ(u"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430", result);
vitbar27804d102015-04-15 10:54:411374
1375 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1376 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1377 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331378 EXPECT_EQ(u"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A", result);
vitbar27804d102015-04-15 10:54:411379}
1380
jbroman50832022016-04-21 23:53:001381// Checks that the ExtractSearchTermsFromURL function strips constant
1382// prefix/suffix strings from the search terms param.
1383TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) {
1384 TemplateURLData data;
1385 data.alternate_urls.push_back(
1386 "http://www.example.com/?q=chromium-{searchTerms}@chromium.org");
1387 data.alternate_urls.push_back(
1388 "http://www.example.com/chromium-{searchTerms}@chromium.org/info");
1389 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011390 std::u16string result;
jbroman50832022016-04-21 23:53:001391
1392 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1393 GURL("http://www.example.com/[email protected]"),
1394 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241395 EXPECT_EQ(u"dev", result);
jbroman50832022016-04-21 23:53:001396
1397 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1398 GURL("http://www.example.com/[email protected]/info"),
1399 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241400 EXPECT_EQ(u"dev", result);
jbroman50832022016-04-21 23:53:001401
1402 // Don't match if the prefix and suffix aren't there.
1403 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1404 GURL("http://www.example.com/?q=invalid"), search_terms_data_, &result));
jbromane8b8728d2017-04-12 17:29:391405
1406 // Don't match if the prefix and suffix overlap.
1407 TemplateURLData data_with_overlap;
1408 data.alternate_urls.push_back(
1409 "http://www.example.com/?q=goo{searchTerms}oogle");
1410 TemplateURL url_with_overlap(data);
1411 EXPECT_FALSE(url_with_overlap.ExtractSearchTermsFromURL(
1412 GURL("http://www.example.com/?q=google"), search_terms_data_, &result));
jbroman50832022016-04-21 23:53:001413}
1414
[email protected]f62e30f52013-03-23 03:45:151415TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1416 TemplateURLData data;
1417 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]f62e30f52013-03-23 03:45:151418 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1419 data.alternate_urls.push_back(
1420 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281421 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241422 TemplateURLRef::SearchTermsArgs search_terms(u"Bob Morane");
[email protected]f62e30f52013-03-23 03:45:151423 GURL result;
1424
1425 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191426 GURL("http://google.com/?q=something"), search_terms,
1427 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101428 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151429
1430 result = GURL("http://should.not.change.com");
1431 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191432 GURL("http://google.ca/?q=something"), search_terms,
1433 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151434 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1435
1436 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191437 GURL("http://google.com/foo/?q=foo"), search_terms,
1438 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151439
1440 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191441 GURL("https://google.com/?q=foo"), search_terms,
1442 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101443 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151444
1445 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191446 GURL("http://google.com:8080/?q=foo"), search_terms,
1447 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151448
1449 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191450 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1451 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101452 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result);
[email protected]f62e30f52013-03-23 03:45:151453
1454 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1455 // template_url.cc for details.
1456 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191457 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1458 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101459 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151460
1461 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1462 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191463 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101464 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
[email protected]f62e30f52013-03-23 03:45:151465 result);
1466
1467 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1468 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191469 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151470 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
vitbarf298455d2015-04-21 12:58:101471 "#j=abc&q=Bob+Morane&h=def9"), result);
[email protected]f62e30f52013-03-23 03:45:151472
1473 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191474 GURL("http://google.com/alt/?q="), search_terms,
1475 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151476
1477 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191478 GURL("http://google.com/alt/?#q="), search_terms,
1479 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151480
1481 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191482 GURL("http://google.com/alt/?q=#q="), search_terms,
1483 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151484
1485 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191486 GURL("http://google.com/alt/?q=123#q="), search_terms,
1487 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151488
1489 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191490 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1491 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101492 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151493}
[email protected]56fa29592013-07-02 20:25:531494
alexmos294849f2015-03-14 00:55:061495TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1496 TemplateURLData data;
1497 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1498 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241499 TemplateURLRef::SearchTermsArgs search_terms(u"Bob Morane");
alexmos294849f2015-03-14 00:55:061500 GURL result;
1501
1502 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1503 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1504 search_terms_data_, &result));
1505 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1506
1507 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1508 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1509 search_terms_data_, &result));
1510 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1511
1512 result = GURL("http://should.not.change.com");
1513 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1514 GURL("http://term-in-path.com/about"), search_terms,
1515 search_terms_data_, &result));
1516 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1517}
1518
vitbarf298455d2015-04-21 12:58:101519// Checks that the ReplaceSearchTermsInURL function works correctly
1520// for search terms containing non-latin characters for a search engine
1521// using UTF-8 input encoding.
1522TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) {
1523 TemplateURLData data;
1524 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1525 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1526 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1527 TemplateURL url(data);
1528
1529 // Russian text which will be encoded with UTF-8.
Jan Wilken Dörrie72da4982021-03-19 19:21:091530 TemplateURLRef::SearchTermsArgs search_terms(
1531 u"\x0442\x0435\x043A\x0441\x0442");
vitbarf298455d2015-04-21 12:58:101532 GURL result;
1533
1534 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1535 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_,
1536 &result));
1537 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1538 result);
1539
1540 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1541 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_,
1542 &result));
1543 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1544 result);
1545
1546 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1547 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_,
1548 &result));
1549 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1550 result);
1551}
1552
1553// Checks that the ReplaceSearchTermsInURL function works correctly
1554// for search terms containing non-latin characters for a search engine
1555// using non UTF-8 input encoding.
1556TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) {
1557 TemplateURLData data;
1558 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1559 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1560 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1561 data.input_encodings.push_back("windows-1251");
1562 TemplateURL url(data);
1563
1564 // Russian text which will be encoded with Windows-1251.
Jan Wilken Dörrie72da4982021-03-19 19:21:091565 TemplateURLRef::SearchTermsArgs search_terms(
1566 u"\x0442\x0435\x043A\x0441\x0442");
vitbarf298455d2015-04-21 12:58:101567 GURL result;
1568
1569 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1570 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_,
1571 &result));
1572 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1573 result);
1574
1575 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1576 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_,
1577 &result));
1578 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1579 result);
1580
1581 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1582 GURL("http://windows-1251.ru/path/a%20b"), search_terms,
1583 search_terms_data_, &result));
1584 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1585 result);
1586}
1587
Mark Pearson247fb8a2018-08-30 05:12:261588// Test the |additional_query_params| field of SearchTermsArgs.
[email protected]621ade062013-10-28 06:27:431589TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431590 TemplateURLData data;
1591 // Pick a URL with replacements before, during, and after the query, to ensure
1592 // we don't goof up any of them.
1593 data.SetURL("{google:baseURL}search?q={searchTerms}"
1594 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281595 TemplateURL url(data);
[email protected]621ade062013-10-28 06:27:431596
Mark Pearson247fb8a2018-08-30 05:12:261597 // Baseline: no |additional_query_params| field.
Jan Wilken Dörrie756999e2021-03-23 15:05:241598 TemplateURLRef::SearchTermsArgs search_terms(u"abc");
1599 search_terms.original_query = u"def";
[email protected]621ade062013-10-28 06:27:431600 search_terms.accepted_suggestion = 0;
1601 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191602 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431603
Mark Pearson247fb8a2018-08-30 05:12:261604 // Set the |additional_query_params|.
1605 search_terms.additional_query_params = "pq=xyz";
[email protected]621ade062013-10-28 06:27:431606 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191607 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431608
Mark Pearson247fb8a2018-08-30 05:12:261609 // Add |append_extra_query_params_from_command_line| into the mix, and ensure
1610 // it works.
1611 search_terms.append_extra_query_params_from_command_line = true;
avi1772c1a2014-12-22 22:42:331612 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]621ade062013-10-28 06:27:431613 switches::kExtraSearchQueryParams, "a=b");
1614 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191615 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431616}
1617
Mark Pearson247fb8a2018-08-30 05:12:261618// Test the |search_terms.append_extra_query_params_from_command_line| field of
1619// SearchTermsArgs.
[email protected]56fa29592013-07-02 20:25:531620TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531621 TemplateURLData data;
1622 // Pick a URL with replacements before, during, and after the query, to ensure
1623 // we don't goof up any of them.
1624 data.SetURL("{google:baseURL}search?q={searchTerms}"
1625 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281626 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531627
Mark Pearson247fb8a2018-08-30 05:12:261628 // Baseline: no command-line args, no
1629 // |search_terms.append_extra_query_params_from_command_line| flag.
Jan Wilken Dörrie756999e2021-03-23 15:05:241630 TemplateURLRef::SearchTermsArgs search_terms(u"abc");
1631 search_terms.original_query = u"def";
[email protected]56fa29592013-07-02 20:25:531632 search_terms.accepted_suggestion = 0;
1633 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191634 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531635
1636 // Set the flag. Since there are no command-line args, this should have no
1637 // effect.
Mark Pearson247fb8a2018-08-30 05:12:261638 search_terms.append_extra_query_params_from_command_line = true;
[email protected]56fa29592013-07-02 20:25:531639 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191640 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531641
1642 // Now append the command-line arg. This should be inserted into the query.
avi1772c1a2014-12-22 22:42:331643 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]56fa29592013-07-02 20:25:531644 switches::kExtraSearchQueryParams, "a=b");
1645 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191646 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531647
1648 // Turn off the flag. Now the command-line arg should be ignored again.
Mark Pearson247fb8a2018-08-30 05:12:261649 search_terms.append_extra_query_params_from_command_line = false;
[email protected]56fa29592013-07-02 20:25:531650 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191651 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531652}
[email protected]d5015ca2013-08-08 22:04:181653
1654// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171655TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181656 TemplateURLData data;
1657 data.input_encodings.push_back("UTF-8");
1658 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281659 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191660 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1661 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Jan Wilken Dörrie756999e2021-03-23 15:05:241662 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]d5015ca2013-08-08 22:04:181663
[email protected]ce7ee5f2014-06-16 23:41:191664 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1665 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181666 EXPECT_EQ("http://www.google.com/?q=foo", result);
1667
[email protected]332d17d22014-06-20 16:56:031668 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191669 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1670 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181671 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1672
1673 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031674 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191675 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1676 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181677 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1678}
[email protected]2328ee22013-08-08 23:00:191679
1680// Test the IsSearchResults function.
1681TEST_F(TemplateURLTest, IsSearchResults) {
1682 TemplateURLData data;
1683 data.SetURL("http://bar/search?q={searchTerms}");
[email protected]2767c0fd2013-08-16 17:44:161684 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191685 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1686 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1687 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1688 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281689 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191690
1691 const struct {
1692 const char* const url;
1693 bool result;
1694 } url_data[] = {
1695 { "http://bar/search?q=foo&oq=foo", true, },
1696 { "http://bar/?q=foo&oq=foo", true, },
1697 { "http://bar/#output=search&q=foo&oq=foo", true, },
1698 { "http://bar/webhp#q=foo&oq=foo", true, },
1699 { "http://bar/#q=foo&oq=foo", true, },
1700 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1701 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1702 { "http://bar/", false, },
1703 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161704 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191705 };
1706
Avi Drissman2244c2d2018-12-25 23:08:021707 for (size_t i = 0; i < base::size(url_data); ++i) {
[email protected]2328ee22013-08-08 23:00:191708 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191709 search_provider.IsSearchURL(GURL(url_data[i].url),
1710 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191711 }
1712}
[email protected]a048de8a2013-10-02 18:30:061713
jdonnelly41c5b46a2015-07-10 21:24:381714TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) {
[email protected]9d70de12014-05-10 02:15:311715 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441716 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311717 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1718
[email protected]168d08722014-06-18 07:13:281719 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241720 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]ce7ee5f2014-06-16 23:41:191721 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1722 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311723 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1724}
[email protected]20184242014-05-14 02:57:421725
1726TEST_F(TemplateURLTest, SessionToken) {
1727 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441728 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421729 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1730
[email protected]168d08722014-06-18 07:13:281731 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241732 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]20184242014-05-14 02:57:421733 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191734 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1735 search_terms_data_);
[email protected]20184242014-05-14 02:57:421736 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1737
[email protected]168d08722014-06-18 07:13:281738 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421739 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191740 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1741 search_terms_data_);
[email protected]20184242014-05-14 02:57:421742 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1743}
[email protected]448b17f52014-06-13 01:08:031744
1745TEST_F(TemplateURLTest, ContextualSearchParameters) {
1746 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441747 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031748 data.SetURL("http://bar/_/contextualsearch?"
1749 "{google:contextualSearchVersion}"
1750 "{google:contextualSearchContextData}");
1751
[email protected]168d08722014-06-18 07:13:281752 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241753 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]ce7ee5f2014-06-16 23:41:191754 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1755 search_terms_data_);
donndbb98274e2016-11-01 21:04:401756 EXPECT_EQ("http://bar/_/contextualsearch?", result);
[email protected]448b17f52014-06-13 01:08:031757
Donn Denman01b1bf72018-08-14 16:25:201758 // Test the current common case, which uses no home country or previous
1759 // event.
1760 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
Donn Denman97be51df2020-06-24 01:23:191761 2, 1, std::string(), 0, 0, false, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391762 std::string(), std::string());
[email protected]448b17f52014-06-13 01:08:031763 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191764 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1765 search_terms_data_);
donndd9fc4d92016-09-16 00:30:491766 EXPECT_EQ(
1767 "http://bar/_/contextualsearch?"
donnd0bc73292017-01-17 22:07:561768 "ctxs=2&"
donndd9fc4d92016-09-16 00:30:491769 "ctxsl_coca=1",
1770 result);
donndbb98274e2016-11-01 21:04:401771
Donn Denman01b1bf72018-08-14 16:25:201772 // Test the home country and non-zero event data case.
donndbb98274e2016-11-01 21:04:401773 search_terms_args.contextual_search_params =
Donn Denmandf4ede0f2020-01-17 23:11:221774 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denman97be51df2020-06-24 01:23:191775 2, 2, "CH", 1657713458, 5, false, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391776 std::string(), std::string());
donndbb98274e2016-11-01 21:04:401777 result =
1778 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1779
1780 EXPECT_EQ(
1781 "http://bar/_/contextualsearch?"
1782 "ctxs=2&"
donnd0bc73292017-01-17 22:07:561783 "ctxsl_coca=2&"
Donn Denman01b1bf72018-08-14 16:25:201784 "ctxs_hc=CH&"
1785 "ctxsl_pid=1657713458&"
1786 "ctxsl_per=5",
donndbb98274e2016-11-01 21:04:401787 result);
Donn Denmandf4ede0f2020-01-17 23:11:221788
1789 // Test exact-search.
1790 search_terms_args.contextual_search_params =
1791 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denman97be51df2020-06-24 01:23:191792 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391793 std::string(), std::string());
Donn Denmandf4ede0f2020-01-17 23:11:221794 result =
1795 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
Donn Denmandf4ede0f2020-01-17 23:11:221796 // Find our param.
1797 size_t found_pos = result.find("ctxsl_exact=1");
1798 EXPECT_NE(found_pos, std::string::npos);
Donn Denman7a241012020-01-31 18:33:131799
1800 // Test source and target languages.
1801 search_terms_args.contextual_search_params =
1802 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denmand638f4602020-11-03 21:05:391803 2, 1, std::string(), 0, 0, true, "es", "de", std::string(),
1804 std::string());
Donn Denman7a241012020-01-31 18:33:131805 result =
1806 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1807 // Find our params.
1808 size_t source_pos = result.find("tlitesl=es");
1809 EXPECT_NE(source_pos, std::string::npos);
1810 size_t target_pos = result.find("tlitetl=de");
1811 EXPECT_NE(target_pos, std::string::npos);
Donn Denman97be51df2020-06-24 01:23:191812
1813 // Test fluent languages.
1814 search_terms_args.contextual_search_params =
1815 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
1816 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391817 "es,de", std::string());
Donn Denman97be51df2020-06-24 01:23:191818 result =
1819 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1820 // Find our param. These may actually be URL encoded.
1821 size_t fluent_pos = result.find("&ctxs_fls=es,de");
1822 EXPECT_NE(fluent_pos, std::string::npos);
Donn Denmand638f4602020-11-03 21:05:391823
1824 // Test Related Searches.
1825 search_terms_args.contextual_search_params =
1826 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
1827 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
1828 std::string(), "1RbCu");
1829 result =
1830 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1831 // Find our param.
1832 size_t ctxsl_rs_pos = result.find("&ctxsl_rs=1RbCu");
1833 EXPECT_NE(ctxsl_rs_pos, std::string::npos);
[email protected]448b17f52014-06-13 01:08:031834}
[email protected]44ccc9f2014-06-20 17:36:211835
1836TEST_F(TemplateURLTest, GenerateKeyword) {
Jan Wilken Dörrie756999e2021-03-23 15:05:241837 ASSERT_EQ(u"foo", TemplateURL::GenerateKeyword(GURL("http://foo")));
1838 ASSERT_EQ(u"foo.", TemplateURL::GenerateKeyword(GURL("http://foo.")));
Emily Stark0fb634f82020-07-14 18:00:391839 // www. should be stripped for a public hostname but not a private/intranet
1840 // hostname.
Jan Wilken Dörrie756999e2021-03-23 15:05:241841 ASSERT_EQ(u"google.com",
Emily Stark0fb634f82020-07-14 18:00:391842 TemplateURL::GenerateKeyword(GURL("http://www.google.com")));
Jan Wilken Dörrie756999e2021-03-23 15:05:241843 ASSERT_EQ(u"www.foo", TemplateURL::GenerateKeyword(GURL("http://www.foo")));
[email protected]44ccc9f2014-06-20 17:36:211844 // Make sure we don't get a trailing '/'.
Jan Wilken Dörrie756999e2021-03-23 15:05:241845 ASSERT_EQ(u"blah", TemplateURL::GenerateKeyword(GURL("http://blah/")));
[email protected]44ccc9f2014-06-20 17:36:211846 // Don't generate the empty string.
Jan Wilken Dörrie756999e2021-03-23 15:05:241847 ASSERT_EQ(u"www.", TemplateURL::GenerateKeyword(GURL("http://www.")));
Peter Kastingcc07b332021-05-07 22:58:251848 ASSERT_EQ(u"абв", TemplateURL::GenerateKeyword(GURL("http://xn--80acd")));
a-v-y81695d0d2017-04-20 08:22:221849
1850 // Generated keywords must always be in lowercase, because TemplateURLs always
1851 // converts keywords to lowercase in its constructor and TemplateURLService
1852 // stores TemplateURLs in maps using keyword as key.
1853 EXPECT_TRUE(IsLowerCase(TemplateURL::GenerateKeyword(GURL("http://BLAH/"))));
1854 EXPECT_TRUE(IsLowerCase(
1855 TemplateURL::GenerateKeyword(GURL("http://embeddedhtml.<head>/"))));
[email protected]44ccc9f2014-06-20 17:36:211856}
1857
1858TEST_F(TemplateURLTest, GenerateSearchURL) {
1859 struct GenerateSearchURLCase {
1860 const char* test_name;
1861 const char* url;
1862 const char* expected;
1863 } generate_url_cases[] = {
1864 { "invalid URL", "foo{searchTerms}", "" },
1865 { "URL with no replacements", "http://foo/", "http://foo/" },
1866 { "basic functionality", "http://foo/{searchTerms}",
1867 "http://foo/blah.blah.blah.blah.blah" }
1868 };
1869
Avi Drissman2244c2d2018-12-25 23:08:021870 for (size_t i = 0; i < base::size(generate_url_cases); ++i) {
[email protected]44ccc9f2014-06-20 17:36:211871 TemplateURLData data;
1872 data.SetURL(generate_url_cases[i].url);
1873 TemplateURL t_url(data);
1874 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1875 generate_url_cases[i].expected)
1876 << generate_url_cases[i].test_name << " failed.";
1877 }
1878}
[email protected]9e9130ce2014-06-23 23:20:511879
1880TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1881 TemplateURLData data;
1882 search_terms_data_.set_google_base_url("http://bar/");
1883 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1884
1885 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241886 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]9e9130ce2014-06-23 23:20:511887 search_terms_args.prefetch_query = "full query text";
1888 search_terms_args.prefetch_query_type = "2338";
1889 std::string result =
1890 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1891 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1892 result);
1893
1894 TemplateURL url2(data);
1895 search_terms_args.prefetch_query.clear();
1896 search_terms_args.prefetch_query_type.clear();
1897 result =
1898 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1899 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1900}
vitbar5bd8c252016-01-29 18:44:511901
1902// Tests that TemplateURL works correctly after changing the Google base URL
1903// and invalidating cached values.
1904TEST_F(TemplateURLTest, InvalidateCachedValues) {
1905 TemplateURLData data;
1906 data.SetURL("{google:baseURL}search?q={searchTerms}");
1907 data.suggestions_url = "{google:baseSuggestURL}search?q={searchTerms}";
vitbar5bd8c252016-01-29 18:44:511908 data.image_url = "{google:baseURL}searchbyimage/upload";
1909 data.new_tab_url = "{google:baseURL}_/chrome/newtab";
1910 data.contextual_search_url = "{google:baseURL}_/contextualsearch";
1911 data.alternate_urls.push_back("{google:baseURL}s#q={searchTerms}");
1912 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241913 TemplateURLRef::SearchTermsArgs search_terms_args(u"X");
Jan Wilken Dörriefa241ba2021-03-11 17:57:011914 std::u16string search_terms;
vitbar5bd8c252016-01-29 18:44:511915
1916 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1917 EXPECT_EQ("http://www.google.com/search?q=X",
1918 url.url_ref().ReplaceSearchTerms(search_terms_args,
1919 search_terms_data_));
1920 EXPECT_EQ("http://www.google.com/s#q=X",
1921 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1922 search_terms_data_));
1923 EXPECT_EQ("http://www.google.com/search?q=X",
1924 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1925 search_terms_data_));
1926 EXPECT_EQ("http://www.google.com/complete/search?q=X",
1927 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1928 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:511929 EXPECT_EQ("http://www.google.com/searchbyimage/upload",
1930 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1931 search_terms_data_));
1932 EXPECT_EQ("http://www.google.com/_/chrome/newtab",
1933 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1934 search_terms_data_));
1935 EXPECT_EQ("http://www.google.com/_/contextualsearch",
1936 url.contextual_search_url_ref().ReplaceSearchTerms(
1937 search_terms_args, search_terms_data_));
1938
1939 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1940 GURL("http://www.google.com/search?q=Y+Z"),
1941 search_terms_data_, &search_terms));
Jan Wilken Dörrie756999e2021-03-23 15:05:241942 EXPECT_EQ(u"Y Z", search_terms);
vitbar5bd8c252016-01-29 18:44:511943 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1944 GURL("http://www.google.com/s#q=123"),
1945 search_terms_data_, &search_terms));
Jan Wilken Dörrie756999e2021-03-23 15:05:241946 EXPECT_EQ(u"123", search_terms);
vitbar5bd8c252016-01-29 18:44:511947
1948 search_terms_data_.set_google_base_url("https://www.foo.org/");
1949 url.InvalidateCachedValues();
1950
1951 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1952 EXPECT_EQ("https://www.foo.org/search?q=X",
1953 url.url_ref().ReplaceSearchTerms(search_terms_args,
1954 search_terms_data_));
1955 EXPECT_EQ("https://www.foo.org/s#q=X",
1956 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1957 search_terms_data_));
1958 EXPECT_EQ("https://www.foo.org/search?q=X",
1959 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1960 search_terms_data_));
1961 EXPECT_EQ("https://www.foo.org/complete/search?q=X",
1962 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1963 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:511964 EXPECT_EQ("https://www.foo.org/searchbyimage/upload",
1965 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1966 search_terms_data_));
1967 EXPECT_EQ("https://www.foo.org/_/chrome/newtab",
1968 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1969 search_terms_data_));
1970 EXPECT_EQ("https://www.foo.org/_/contextualsearch",
1971 url.contextual_search_url_ref().ReplaceSearchTerms(
1972 search_terms_args, search_terms_data_));
1973
1974 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1975 GURL("https://www.foo.org/search?q=Y+Z"),
1976 search_terms_data_, &search_terms));
Jan Wilken Dörrie756999e2021-03-23 15:05:241977 EXPECT_EQ(u"Y Z", search_terms);
vitbar5bd8c252016-01-29 18:44:511978 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1979 GURL("https://www.foo.org/s#q=123"),
1980 search_terms_data_, &search_terms));
Jan Wilken Dörrie756999e2021-03-23 15:05:241981 EXPECT_EQ(u"123", search_terms);
vitbar5bd8c252016-01-29 18:44:511982
1983 search_terms_data_.set_google_base_url("http://www.google.com/");
1984}
Troy Hildebrandt47ac4d8b2018-05-10 22:40:001985
1986// Tests the use of wildcards in the path to ensure both extracting search terms
1987// and generating a search URL work correctly.
1988TEST_F(TemplateURLTest, PathWildcard) {
1989 TemplateURLData data;
1990 data.SetURL(
1991 "https://www.google.com/search{google:pathWildcard}?q={searchTerms}");
1992 TemplateURL url(data);
1993
1994 // Test extracting search terms from a URL.
Jan Wilken Dörriefa241ba2021-03-11 17:57:011995 std::u16string search_terms;
Troy Hildebrandt47ac4d8b2018-05-10 22:40:001996 url.ExtractSearchTermsFromURL(GURL("https://www.google.com/search?q=testing"),
1997 search_terms_data_, &search_terms);
Jan Wilken Dörrie756999e2021-03-23 15:05:241998 EXPECT_EQ(u"testing", search_terms);
Troy Hildebrandt47ac4d8b2018-05-10 22:40:001999 url.ExtractSearchTermsFromURL(
2000 GURL("https://www.google.com/search;_this_is_a_test;_?q=testing"),
2001 search_terms_data_, &search_terms);
Jan Wilken Dörrie756999e2021-03-23 15:05:242002 EXPECT_EQ(u"testing", search_terms);
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002003
2004 // Tests overlapping prefix/suffix.
2005 data.SetURL(
2006 "https://www.google.com/search{google:pathWildcard}rch?q={searchTerms}");
2007 TemplateURL overlap_url(data);
2008 overlap_url.ExtractSearchTermsFromURL(
2009 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
2010 &search_terms);
2011 EXPECT_TRUE(search_terms.empty());
2012
2013 // Tests wildcard at beginning of path so we only have a suffix.
2014 data.SetURL(
2015 "https://www.google.com/{google:pathWildcard}rch?q={searchTerms}");
2016 TemplateURL suffix_url(data);
2017 suffix_url.ExtractSearchTermsFromURL(
2018 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
2019 &search_terms);
Jan Wilken Dörrie756999e2021-03-23 15:05:242020 EXPECT_EQ(u"testing", search_terms);
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002021
2022 // Tests wildcard between prefix/suffix.
2023 overlap_url.ExtractSearchTermsFromURL(
2024 GURL("https://www.google.com/search_testing_rch?q=testing"),
2025 search_terms_data_, &search_terms);
Jan Wilken Dörrie756999e2021-03-23 15:05:242026 EXPECT_EQ(u"testing", search_terms);
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002027
2028 // Test generating a URL.
Jan Wilken Dörrie756999e2021-03-23 15:05:242029 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002030 GURL generated_url;
2031 url.ReplaceSearchTermsInURL(url.GenerateSearchURL(search_terms_data_),
2032 search_terms_args, search_terms_data_,
2033 &generated_url);
2034 EXPECT_EQ("https://www.google.com/search?q=foo", generated_url.spec());
2035}