blob: 89f642524d5dc9e2ae56e9c82b233fffebd20566 [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"
Moe Ahmadi8edf65682022-01-25 02:44:0711#include "base/feature_list.h"
a-v-y81695d0d2017-04-20 08:22:2212#include "base/i18n/case_conversion.h"
[email protected]2f3bc6512013-08-28 03:56:2713#include "base/strings/string_number_conversions.h"
[email protected]24a555b62013-06-10 22:01:1714#include "base/strings/string_util.h"
[email protected]e309f312013-06-07 21:50:0815#include "base/strings/utf_string_conversions.h"
John Abd-El-Malek9cf3d7f02018-07-27 02:40:3916#include "components/google/core/common/google_util.h"
Moe Ahmadi8edf65682022-01-25 02:44:0717#include "components/omnibox/common/omnibox_features.h"
[email protected]5c3c6e482014-06-23 09:47:1818#include "components/search_engines/search_engines_switches.h"
[email protected]798baa8e2014-06-20 05:42:4419#include "components/search_engines/search_terms_data.h"
[email protected]d550cb02014-06-25 06:48:1120#include "components/search_engines/template_url.h"
hashimoto3c831812014-08-25 07:40:2321#include "components/search_engines/testing_search_terms_data.h"
a-v-y02d3f50b2017-02-16 12:03:5522#include "net/base/url_util.h"
[email protected]d82443b2009-01-15 19:54:5623#include "testing/gtest/include/gtest/gtest.h"
Steven Holtef9d5ed62017-10-21 02:02:3024#include "third_party/metrics_proto/omnibox_event.pb.h"
25#include "third_party/metrics_proto/omnibox_input_type.pb.h"
[email protected]d82443b2009-01-15 19:54:5626
[email protected]f911df52013-12-24 23:24:2327using base::ASCIIToUTF16;
28
a-v-y81695d0d2017-04-20 08:22:2229namespace {
Jan Wilken Dörriefa241ba2021-03-11 17:57:0130bool IsLowerCase(const std::u16string& str) {
a-v-y81695d0d2017-04-20 08:22:2231 return str == base::i18n::ToLower(str);
32}
33}
34
[email protected]583844c2011-08-27 00:38:3535class TemplateURLTest : public testing::Test {
[email protected]d82443b2009-01-15 19:54:5636 public:
[email protected]798baa8e2014-06-20 05:42:4437 TemplateURLTest() : search_terms_data_("http://www.google.com/") {}
[email protected]b37bdfe2012-03-16 20:53:2738 void CheckSuggestBaseURL(const std::string& base_url,
39 const std::string& base_suggest_url) const;
[email protected]ce7ee5f2014-06-16 23:41:1940
vitbardeb285392015-02-20 14:02:5541 static void ExpectPostParamIs(
42 const TemplateURLRef::PostParam& param,
43 const std::string& name,
44 const std::string& value,
45 const std::string& content_type = std::string());
46
hashimoto3c831812014-08-25 07:40:2347 TestingSearchTermsData search_terms_data_;
[email protected]d82443b2009-01-15 19:54:5648};
49
[email protected]b37bdfe2012-03-16 20:53:2750void TemplateURLTest::CheckSuggestBaseURL(
51 const std::string& base_url,
52 const std::string& base_suggest_url) const {
hashimoto3c831812014-08-25 07:40:2353 TestingSearchTermsData search_terms_data(base_url);
[email protected]b37bdfe2012-03-16 20:53:2754 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue());
55}
56
vitbardeb285392015-02-20 14:02:5557// static
58void TemplateURLTest::ExpectPostParamIs(const TemplateURLRef::PostParam& param,
59 const std::string& name,
60 const std::string& value,
61 const std::string& content_type) {
62 EXPECT_EQ(name, param.name);
63 EXPECT_EQ(value, param.value);
64 EXPECT_EQ(content_type, param.content_type);
65}
66
[email protected]d82443b2009-01-15 19:54:5667TEST_F(TemplateURLTest, Defaults) {
[email protected]573889f22012-04-07 01:31:5468 TemplateURLData data;
[email protected]573889f22012-04-07 01:31:5469 EXPECT_FALSE(data.safe_for_autoreplace);
70 EXPECT_EQ(0, data.prepopulate_id);
[email protected]d82443b2009-01-15 19:54:5671}
72
73TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]573889f22012-04-07 01:31:5474 TemplateURLData data;
75 data.SetURL("{searchTerms}");
[email protected]168d08722014-06-18 07:13:2876 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1977 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:5678}
79
80TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1981 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1982 const char* url;
Jan Wilken Dörriefa241ba2021-03-11 17:57:0183 const std::u16string terms;
[email protected]34e24852012-01-31 18:43:5884 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:1985 } search_term_cases[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:2486 {"http://foo{searchTerms}", u"sea rch/bar", "http://foosea%20rch/bar"},
87 {"http://foo{searchTerms}?boo=abc", u"sea rch/bar",
88 "http://foosea%20rch/bar?boo=abc"},
89 {"http://foo/?boo={searchTerms}", u"sea rch/bar",
90 "http://foo/?boo=sea+rch%2Fbar"},
91 {"http://en.wikipedia.org/{searchTerms}", u"wiki/?",
92 "http://en.wikipedia.org/wiki/%3F"}};
Avi Drissman2244c2d2018-12-25 23:08:0293 for (size_t i = 0; i < base::size(search_term_cases); ++i) {
[email protected]0d2e6a62010-01-15 20:09:1994 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:5495 TemplateURLData data;
96 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:2897 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1998 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
99 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04100 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19101 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data_));
[email protected]c31a979c2012-05-31 23:57:16102 ASSERT_TRUE(result.is_valid());
103 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:19104 }
[email protected]d82443b2009-01-15 19:54:56105}
106
107TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:54108 TemplateURLData data;
109 data.SetURL("http://foo{searchTerms}{count?}");
[email protected]168d08722014-06-18 07:13:28110 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19111 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
112 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04113 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24114 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56115 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27116 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:56117}
118
119TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:54120 TemplateURLData data;
121 data.SetURL("http://foo{searchTerms}{count}");
[email protected]168d08722014-06-18 07:13:28122 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19123 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
124 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04125 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24126 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56127 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27128 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56129}
130
131TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54132 TemplateURLData data;
133 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]168d08722014-06-18 07:13:28134 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19135 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
136 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04137 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24138 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56139 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27140 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56141}
142
143TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54144 TemplateURLData data;
145 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[email protected]168d08722014-06-18 07:13:28146 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19147 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
148 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04149 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24150 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56151 ASSERT_TRUE(result.is_valid());
[email protected]405aae22012-03-29 20:36:13152 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56153}
154
155TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54156 TemplateURLData data;
157 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[email protected]168d08722014-06-18 07:13:28158 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19159 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
160 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04161 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24162 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56163 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27164 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56165}
166
[email protected]93b29062013-07-12 03:09:09167TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
168 const char kInvalidPostParamsString[] =
169 "unknown_template={UnknownTemplate},bad_value=bad{value},"
170 "{google:sbiSource}";
171 // List all accpectable parameter format in valid_post_params_string. it is
172 // expected like: "name0=,name1=value1,name2={template1}"
173 const char kValidPostParamsString[] =
174 "image_content={google:imageThumbnail},image_url={google:imageURL},"
175 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
Robbie Gibson8532eaa2019-03-15 11:43:24176 "constant_param=constant,width={google:imageOriginalWidth},"
177 "base64_image_content={google:imageThumbnailBase64}";
[email protected]93b29062013-07-12 03:09:09178 const char KImageSearchURL[] = "http://foo.com/sbi";
179
180 TemplateURLData data;
181 data.image_url = KImageSearchURL;
182
183 // Try to parse invalid post parameters.
184 data.image_url_post_params = kInvalidPostParamsString;
[email protected]168d08722014-06-18 07:13:28185 TemplateURL url_bad(data);
[email protected]ce7ee5f2014-06-16 23:41:19186 ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09187 const TemplateURLRef::PostParams& bad_post_params =
188 url_bad.image_url_ref().post_params_;
189 ASSERT_EQ(2U, bad_post_params.size());
vitbardeb285392015-02-20 14:02:55190 ExpectPostParamIs(bad_post_params[0], "unknown_template",
191 "{UnknownTemplate}");
192 ExpectPostParamIs(bad_post_params[1], "bad_value", "bad{value}");
[email protected]93b29062013-07-12 03:09:09193
194 // Try to parse valid post parameters.
195 data.image_url_post_params = kValidPostParamsString;
[email protected]168d08722014-06-18 07:13:28196 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19197 ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_));
198 ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09199
200 // Check term replacement.
Jan Wilken Dörrie756999e2021-03-23 15:05:24201 TemplateURLRef::SearchTermsArgs search_args(u"X");
[email protected]93b29062013-07-12 03:09:09202 search_args.image_thumbnail_content = "dummy-image-thumbnail";
203 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
[email protected]2f3bc6512013-08-28 03:56:27204 search_args.image_original_size = gfx::Size(10, 10);
[email protected]93b29062013-07-12 03:09:09205 // Replacement operation with no post_data buffer should still return
206 // the parsed URL.
hashimoto3c831812014-08-25 07:40:23207 TestingSearchTermsData search_terms_data("http://X");
[email protected]ce7ee5f2014-06-16 23:41:19208 GURL result(url.image_url_ref().ReplaceSearchTerms(
209 search_args, search_terms_data));
[email protected]93b29062013-07-12 03:09:09210 ASSERT_TRUE(result.is_valid());
211 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48212 TemplateURLRef::PostContent post_content;
[email protected]ce7ee5f2014-06-16 23:41:19213 result = GURL(url.image_url_ref().ReplaceSearchTerms(
[email protected]7c2bcc42013-08-01 04:03:48214 search_args, search_terms_data, &post_content));
[email protected]93b29062013-07-12 03:09:09215 ASSERT_TRUE(result.is_valid());
216 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48217 ASSERT_FALSE(post_content.first.empty());
218 ASSERT_FALSE(post_content.second.empty());
[email protected]93b29062013-07-12 03:09:09219
220 // Check parsed result of post parameters.
221 const TemplateURLRef::Replacements& replacements =
222 url.image_url_ref().replacements_;
223 const TemplateURLRef::PostParams& post_params =
224 url.image_url_ref().post_params_;
Robbie Gibson8532eaa2019-03-15 11:43:24225 EXPECT_EQ(8U, post_params.size());
jdoerrie3feb1852018-10-05 12:16:44226 for (auto i = post_params.begin(); i != post_params.end(); ++i) {
227 auto j = replacements.begin();
[email protected]93b29062013-07-12 03:09:09228 for (; j != replacements.end(); ++j) {
229 if (j->is_post_param && j->index ==
230 static_cast<size_t>(i - post_params.begin())) {
231 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27232 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
Raul Tambref88e5102019-02-06 10:54:03233 ExpectPostParamIs(
234 *i, "width",
235 base::NumberToString(search_args.image_original_size.width()));
[email protected]2f3bc6512013-08-28 03:56:27236 break;
[email protected]1020fead2014-06-20 13:40:28237 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE:
vitbardeb285392015-02-20 14:02:55238 ExpectPostParamIs(*i, "sbisrc",
239 search_terms_data.GoogleImageSearchSource());
[email protected]1020fead2014-06-20 13:40:28240 break;
[email protected]93b29062013-07-12 03:09:09241 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
vitbardeb285392015-02-20 14:02:55242 ExpectPostParamIs(*i, "image_content",
243 search_args.image_thumbnail_content,
244 "image/jpeg");
[email protected]93b29062013-07-12 03:09:09245 break;
Robbie Gibson8532eaa2019-03-15 11:43:24246 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL_BASE64: {
247 std::string base64_image_content;
248 base::Base64Encode(search_args.image_thumbnail_content,
249 &base64_image_content);
250 ExpectPostParamIs(*i, "base64_image_content", base64_image_content,
251 "image/jpeg");
252 break;
253 }
[email protected]93b29062013-07-12 03:09:09254 case TemplateURLRef::GOOGLE_IMAGE_URL:
vitbardeb285392015-02-20 14:02:55255 ExpectPostParamIs(*i, "image_url", search_args.image_url.spec());
[email protected]93b29062013-07-12 03:09:09256 break;
257 case TemplateURLRef::LANGUAGE:
vitbardeb285392015-02-20 14:02:55258 ExpectPostParamIs(*i, "language", "en");
[email protected]93b29062013-07-12 03:09:09259 break;
260 default:
261 ADD_FAILURE(); // Should never go here.
262 }
263 break;
264 }
265 }
266 if (j != replacements.end())
267 continue;
vitbardeb285392015-02-20 14:02:55268 if (i->name == "empty_param")
269 ExpectPostParamIs(*i, "empty_param", std::string());
270 else
271 ExpectPostParamIs(*i, "constant_param", "constant");
[email protected]93b29062013-07-12 03:09:09272 }
273}
274
Peter Kastinge59f0c32017-11-29 05:56:22275TEST_F(TemplateURLTest, ImageURLWithGetShouldNotCrash) {
276 TemplateURLData data;
277 data.SetURL("http://foo/?q={searchTerms}&t={google:imageThumbnail}");
278 TemplateURL url(data);
279 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
280 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
281
Jan Wilken Dörrie756999e2021-03-23 15:05:24282 TemplateURLRef::SearchTermsArgs search_args(u"X");
Peter Kastinge59f0c32017-11-29 05:56:22283 search_args.image_thumbnail_content = "dummy-image-thumbnail";
284 GURL result(
285 url.url_ref().ReplaceSearchTerms(search_args, search_terms_data_));
286 ASSERT_TRUE(result.is_valid());
287 EXPECT_EQ("http://foo/?q=X&t=dummy-image-thumbnail", result.spec());
288}
289
[email protected]d88cb202011-08-17 20:03:01290// Test that setting the prepopulate ID from TemplateURL causes the stored
291// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44292TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54293 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20294 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]168d08722014-06-18 07:13:28295 TemplateURL url(data);
[email protected]1a257262011-06-28 22:15:44296 TemplateURLRef::Replacements replacements;
297 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:24298 EXPECT_EQ("http://foo{fhqwhgads}bar",
299 url.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements,
300 nullptr, &valid));
[email protected]1a257262011-06-28 22:15:44301 EXPECT_TRUE(replacements.empty());
302 EXPECT_TRUE(valid);
303
[email protected]573889f22012-04-07 01:31:54304 data.prepopulate_id = 123;
[email protected]168d08722014-06-18 07:13:28305 TemplateURL url2(data);
Ivan Kotenkov75b1c3a2017-10-24 14:47:24306 EXPECT_EQ("http://foobar",
307 url2.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements,
308 nullptr, &valid));
[email protected]1a257262011-06-28 22:15:44309 EXPECT_TRUE(replacements.empty());
310 EXPECT_TRUE(valid);
311}
312
vitbar90902c42016-05-11 06:16:55313// Test that setting the prepopulate ID from TemplateURL causes the stored
314// TemplateURLRef to handle parsing the URL parameters differently.
315TEST_F(TemplateURLTest, SetPrepopulatedAndReplace) {
316 TemplateURLData data;
317 data.SetURL("http://foo{fhqwhgads}search/?q={searchTerms}");
318 data.suggestions_url = "http://foo{fhqwhgads}suggest/?q={searchTerms}";
vitbar90902c42016-05-11 06:16:55319 data.image_url = "http://foo{fhqwhgads}image/";
320 data.new_tab_url = "http://foo{fhqwhgads}newtab/";
321 data.contextual_search_url = "http://foo{fhqwhgads}context/";
322 data.alternate_urls.push_back(
323 "http://foo{fhqwhgads}alternate/?q={searchTerms}");
324
Jan Wilken Dörrie756999e2021-03-23 15:05:24325 TemplateURLRef::SearchTermsArgs args(u"X");
vitbar90902c42016-05-11 06:16:55326 const SearchTermsData& stdata = search_terms_data_;
327
328 TemplateURL url(data);
329 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
330 url.url_ref().ReplaceSearchTerms(args, stdata));
331 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dalternate/?q=X",
332 url.url_refs()[0].ReplaceSearchTerms(args, stdata));
333 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
334 url.url_refs()[1].ReplaceSearchTerms(args, stdata));
335 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsuggest/?q=X",
336 url.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
vitbar90902c42016-05-11 06:16:55337 EXPECT_EQ("http://foo{fhqwhgads}image/",
338 url.image_url_ref().ReplaceSearchTerms(args, stdata));
339 EXPECT_EQ("http://foo{fhqwhgads}newtab/",
340 url.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
341 EXPECT_EQ("http://foo{fhqwhgads}context/",
342 url.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
343
344 data.prepopulate_id = 123;
345 TemplateURL url2(data);
346 EXPECT_EQ("http://foosearch/?q=X",
347 url2.url_ref().ReplaceSearchTerms(args, stdata));
348 EXPECT_EQ("http://fooalternate/?q=X",
349 url2.url_refs()[0].ReplaceSearchTerms(args, stdata));
350 EXPECT_EQ("http://foosearch/?q=X",
351 url2.url_refs()[1].ReplaceSearchTerms(args, stdata));
352 EXPECT_EQ("http://foosuggest/?q=X",
353 url2.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
vitbar90902c42016-05-11 06:16:55354 EXPECT_EQ("http://fooimage/",
355 url2.image_url_ref().ReplaceSearchTerms(args, stdata));
356 EXPECT_EQ("http://foonewtab/",
357 url2.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
358 EXPECT_EQ("http://foocontext/",
359 url2.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
360}
361
[email protected]d82443b2009-01-15 19:54:56362TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54363 TemplateURLData data;
364 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]168d08722014-06-18 07:13:28365 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19366 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
367 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04368 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24369 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56370 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27371 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56372}
373
374TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54375 TemplateURLData data;
376 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]168d08722014-06-18 07:13:28377 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19378 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
379 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04380 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24381 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56382 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27383 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56384}
385
[email protected]375bd7312010-08-30 22:18:13386TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
387 struct SearchTermsCase {
388 const char* url;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01389 const std::u16string terms;
[email protected]375bd7312010-08-30 22:18:13390 const char* output;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01391 } search_term_cases[] = {{"{google:baseURL}{language}{searchTerms}",
392 std::u16string(), "http://example.com/e/en"},
393 {"{google:baseSuggestURL}{searchTerms}",
394 std::u16string(), "http://example.com/complete/"}};
[email protected]375bd7312010-08-30 22:18:13395
hashimoto3c831812014-08-25 07:40:23396 TestingSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54397 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02398 for (size_t i = 0; i < base::size(search_term_cases); ++i) {
[email protected]375bd7312010-08-30 22:18:13399 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54400 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:28401 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19402 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data));
403 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data));
404 GURL result(url.url_ref().ReplaceSearchTerms(
Ivan Kotenkov75b1c3a2017-10-24 14:47:24405 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data,
406 nullptr));
[email protected]375bd7312010-08-30 22:18:13407 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27408 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13409 }
410}
411
[email protected]d82443b2009-01-15 19:54:56412TEST_F(TemplateURLTest, URLRefTermToWide) {
413 struct ToWideCase {
414 const char* encoded_search_term;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01415 const std::u16string expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56416 } to_wide_cases[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24417 {"hello+world", u"hello world"},
Jan Wilken Dörrie414bbf52021-03-19 19:06:26418 // Test some big-5 input.
Jan Wilken Dörrieeb96aa82021-03-19 19:16:33419 {"%a7A%A6%6e+to+you", u"\x4f60\x597d to you"},
Jan Wilken Dörrie414bbf52021-03-19 19:06:26420 // Test some UTF-8 input. We should fall back to this when the encoding
421 // doesn't look like big-5. We have a '5' in the middle, which is an
422 // invalid Big-5 trailing byte.
Jan Wilken Dörrieeb96aa82021-03-19 19:16:33423 {"%e4%bd%a05%e5%a5%bd+to+you", u"\x4f60\x35\x597d to you"},
Jan Wilken Dörrie414bbf52021-03-19 19:06:26424 // Undecodable input should stay escaped.
425 {"%91%01+abcd", u"%91%01 abcd"},
426 // Make sure we convert %2B to +.
Jan Wilken Dörrie756999e2021-03-23 15:05:24427 {"C%2B%2B", u"C++"},
Jan Wilken Dörrie414bbf52021-03-19 19:06:26428 // C%2B is escaped as C%252B, make sure we unescape it properly.
Jan Wilken Dörrie756999e2021-03-23 15:05:24429 {"C%252B", u"C%2B"},
[email protected]d82443b2009-01-15 19:54:56430 };
431
[email protected]d82443b2009-01-15 19:54:56432 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54433 TemplateURLData data;
434 data.SetURL("http://foo?q={searchTerms}");
435 data.input_encodings.push_back("big-5");
[email protected]168d08722014-06-18 07:13:28436 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19437 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
438 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Avi Drissman2244c2d2018-12-25 23:08:02439 for (size_t i = 0; i < base::size(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07440 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13441 url.url_ref().SearchTermToString16(
442 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56443 }
444}
445
[email protected]d82443b2009-01-15 19:54:56446TEST_F(TemplateURLTest, DisplayURLToURLRef) {
447 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19448 const std::string url;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01449 const std::u16string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27450 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24451 {"http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
452 u"http://foo%sx{inputEncoding}y{outputEncoding}a"},
453 {"http://X", u"http://X"},
454 {"http://foo{searchTerms", u"http://foo{searchTerms"},
455 {"http://foo{searchTerms}{language}", u"http://foo%s{language}"},
[email protected]d82443b2009-01-15 19:54:56456 };
[email protected]573889f22012-04-07 01:31:54457 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02458 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54459 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28460 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19461 EXPECT_EQ(test_data[i].expected_result,
462 url.url_ref().DisplayURL(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27463 EXPECT_EQ(test_data[i].url,
[email protected]ce7ee5f2014-06-16 23:41:19464 TemplateURLRef::DisplayURLToURLRef(
465 url.url_ref().DisplayURL(search_terms_data_)));
[email protected]d82443b2009-01-15 19:54:56466 }
467}
468
469TEST_F(TemplateURLTest, ReplaceSearchTerms) {
470 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19471 const std::string url;
[email protected]d82443b2009-01-15 19:54:56472 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27473 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19474 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56475 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19476 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56477 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19478 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56479 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19480 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56481 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19482 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56483 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19484 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56485 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19486 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56487 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19488 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56489 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19490 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56491 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19492 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56493 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19494 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56495 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19496 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56497 "http://foo/UTF-8a{language}aXa" },
498 };
[email protected]573889f22012-04-07 01:31:54499 TemplateURLData data;
500 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02501 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54502 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28503 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19504 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
505 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27506 std::string expected_result = test_data[i].expected_result;
brettwe6dae462015-06-24 20:54:45507 base::ReplaceSubstringsAfterOffset(
508 &expected_result, 0, "{language}",
509 search_terms_data_.GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04510 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24511 TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27512 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56513 EXPECT_EQ(expected_result, result.spec());
514 }
515}
516
517
518// Tests replacing search terms in various encodings and making sure the
519// generated URL matches the expected value.
520TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
521 struct TestData {
522 const std::string encoding;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01523 const std::u16string search_term;
[email protected]ddd231e2010-06-29 20:35:19524 const std::string url;
[email protected]d82443b2009-01-15 19:54:56525 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27526 } test_data[] = {
Peter Kastingcc07b332021-05-07 22:58:25527 {"BIG5", u"悽", "http://foo/?{searchTerms}{inputEncoding}",
Jan Wilken Dörrieeb96aa82021-03-19 19:16:33528 "http://foo/?%B1~BIG5"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24529 {"UTF-8", u"blah", "http://foo/?{searchTerms}{inputEncoding}",
530 "http://foo/?blahUTF-8"},
Peter Kastingcc07b332021-05-07 22:58:25531 {"Shift_JIS", u"あ", "http://foo/{searchTerms}/bar",
532 "http://foo/%82%A0/bar"},
533 {"Shift_JIS", u"あ い", "http://foo/{searchTerms}/bar",
534 "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56535 };
[email protected]573889f22012-04-07 01:31:54536 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02537 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54538 data.SetURL(test_data[i].url);
539 data.input_encodings.clear();
540 data.input_encodings.push_back(test_data[i].encoding);
[email protected]168d08722014-06-18 07:13:28541 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19542 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
543 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04544 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19545 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
546 search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04547 ASSERT_TRUE(result.is_valid());
548 EXPECT_EQ(test_data[i].expected_result, result.spec());
549 }
550}
551
Alexander Yashkin874b919d2018-02-17 15:11:18552// Test that encoding with several optional codepages works as intended.
553// Codepages are tried in order, fallback is UTF-8.
554TEST_F(TemplateURLTest, ReplaceSearchTermsMultipleEncodings) {
555 struct TestData {
556 const std::vector<std::string> encodings;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01557 const std::u16string search_term;
Alexander Yashkin874b919d2018-02-17 15:11:18558 const std::string url;
559 const std::string expected_result;
560 } test_data[] = {
561 // First and third encodings are valid. First is used.
562 {{"windows-1251", "cp-866", "UTF-8"},
Peter Kastingcc07b332021-05-07 22:58:25563 u"я",
Alexander Yashkin874b919d2018-02-17 15:11:18564 "http://foo/?{searchTerms}{inputEncoding}",
565 "http://foo/?%FFwindows-1251"},
566 // Second and third encodings are valid, second is used.
567 {{"cp-866", "GB2312", "UTF-8"},
Peter Kastingcc07b332021-05-07 22:58:25568 u"狗",
Alexander Yashkin874b919d2018-02-17 15:11:18569 "http://foo/?{searchTerms}{inputEncoding}",
570 "http://foo/?%B9%B7GB2312"},
571 // Second and third encodings are valid in another order, second is used.
572 {{"cp-866", "UTF-8", "GB2312"},
Peter Kastingcc07b332021-05-07 22:58:25573 u"狗",
Alexander Yashkin874b919d2018-02-17 15:11:18574 "http://foo/?{searchTerms}{inputEncoding}",
575 "http://foo/?%E7%8B%97UTF-8"},
576 // Both encodings are invalid, fallback to UTF-8.
577 {{"cp-866", "windows-1251"},
Peter Kastingcc07b332021-05-07 22:58:25578 u"狗",
Alexander Yashkin874b919d2018-02-17 15:11:18579 "http://foo/?{searchTerms}{inputEncoding}",
580 "http://foo/?%E7%8B%97UTF-8"},
581 // No encodings are given, fallback to UTF-8.
582 {{},
Peter Kastingcc07b332021-05-07 22:58:25583 u"狗",
Alexander Yashkin874b919d2018-02-17 15:11:18584 "http://foo/?{searchTerms}{inputEncoding}",
585 "http://foo/?%E7%8B%97UTF-8"},
586 };
587
588 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02589 for (size_t i = 0; i < base::size(test_data); ++i) {
Alexander Yashkin874b919d2018-02-17 15:11:18590 data.SetURL(test_data[i].url);
591 data.input_encodings = test_data[i].encodings;
592 TemplateURL url(data);
593 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
594 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
595 GURL result(url.url_ref().ReplaceSearchTerms(
596 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
597 search_terms_data_));
598 ASSERT_TRUE(result.is_valid());
599 EXPECT_EQ(test_data[i].expected_result, result.spec());
600 }
601}
602
[email protected]bca359b2012-06-24 07:53:04603// Tests replacing assisted query stats (AQS) in various scenarios.
604TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
605 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01606 const std::u16string search_term;
[email protected]bca359b2012-06-24 07:53:04607 const std::string aqs;
608 const std::string base_url;
609 const std::string url;
610 const std::string expected_result;
611 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24612 // No HTTPS, no AQS.
613 {u"foo", "chrome.0.0l6", "http://foo/",
614 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
615 "http://foo/?foo"},
616 // HTTPS available, AQS should be replaced.
617 {u"foo", "chrome.0.0l6", "https://foo/",
618 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
619 "https://foo/?fooaqs=chrome.0.0l6&"},
620 // HTTPS available, however AQS is empty.
621 {u"foo", "", "https://foo/",
622 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
623 "https://foo/?foo"},
624 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
625 {u"foo", "chrome.0.0l6", "http://www.google.com",
626 "http://foo?{searchTerms}{google:assistedQueryStats}",
627 "http://foo/?foo"},
628 // A non-Google search provider with HTTPS should allow AQS.
629 {u"foo", "chrome.0.0l6", "https://www.google.com",
630 "https://foo?{searchTerms}{google:assistedQueryStats}",
631 "https://foo/?fooaqs=chrome.0.0l6&"},
[email protected]bca359b2012-06-24 07:53:04632 };
633 TemplateURLData data;
634 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02635 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04636 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28637 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19638 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
639 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04640 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
641 search_terms_args.assisted_query_stats = test_data[i].aqs;
[email protected]798baa8e2014-06-20 05:42:44642 search_terms_data_.set_google_base_url(test_data[i].base_url);
[email protected]ce7ee5f2014-06-16 23:41:19643 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
644 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27645 ASSERT_TRUE(result.is_valid());
646 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56647 }
648}
649
[email protected]00790562012-12-14 09:57:16650// Tests replacing cursor position.
651TEST_F(TemplateURLTest, ReplaceCursorPosition) {
652 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01653 const std::u16string search_term;
[email protected]00790562012-12-14 09:57:16654 size_t cursor_position;
655 const std::string url;
656 const std::string expected_result;
657 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24658 {u"foo", std::u16string::npos,
Jan Wilken Dörriefa241ba2021-03-11 17:57:01659 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
660 "http://www.google.com/?foo&"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24661 {u"foo", 2, "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
Jan Wilken Dörriefa241ba2021-03-11 17:57:01662 "http://www.google.com/?foo&cp=2&"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24663 {u"foo", 15, "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
Jan Wilken Dörriefa241ba2021-03-11 17:57:01664 "http://www.google.com/?foo&cp=15&"},
[email protected]00790562012-12-14 09:57:16665 };
666 TemplateURLData data;
667 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02668 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]00790562012-12-14 09:57:16669 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28670 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19671 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
672 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]00790562012-12-14 09:57:16673 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
674 search_terms_args.cursor_position = test_data[i].cursor_position;
[email protected]ce7ee5f2014-06-16 23:41:19675 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
676 search_terms_data_));
[email protected]00790562012-12-14 09:57:16677 ASSERT_TRUE(result.is_valid());
678 EXPECT_EQ(test_data[i].expected_result, result.spec());
679 }
680}
681
[email protected]420472b22014-06-10 13:34:43682// Tests replacing input type (&oit=).
683TEST_F(TemplateURLTest, ReplaceInputType) {
684 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01685 const std::u16string search_term;
Steven Holte3696c9412017-08-24 18:38:03686 metrics::OmniboxInputType input_type;
[email protected]420472b22014-06-10 13:34:43687 const std::string url;
688 const std::string expected_result;
689 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24690 {u"foo", metrics::OmniboxInputType::UNKNOWN,
Steven Holte3696c9412017-08-24 18:38:03691 "{google:baseURL}?{searchTerms}&{google:inputType}",
692 "http://www.google.com/?foo&oit=1&"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24693 {u"foo", metrics::OmniboxInputType::URL,
Steven Holte3696c9412017-08-24 18:38:03694 "{google:baseURL}?{searchTerms}&{google:inputType}",
695 "http://www.google.com/?foo&oit=3&"},
[email protected]420472b22014-06-10 13:34:43696 };
[email protected]420472b22014-06-10 13:34:43697 TemplateURLData data;
698 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02699 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]420472b22014-06-10 13:34:43700 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28701 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19702 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
703 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43704 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
705 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19706 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
707 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43708 ASSERT_TRUE(result.is_valid());
709 EXPECT_EQ(test_data[i].expected_result, result.spec());
710 }
711}
712
Tommy C. Li9a180482019-06-27 20:49:00713// Tests replacing omnibox focus type (&oft=).
714TEST_F(TemplateURLTest, ReplaceOmniboxFocusType) {
715 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01716 const std::u16string search_term;
Tommy Li5e883512020-08-07 19:32:04717 OmniboxFocusType focus_type;
Tommy C. Li9a180482019-06-27 20:49:00718 const std::string url;
719 const std::string expected_result;
720 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24721 {u"foo", OmniboxFocusType::DEFAULT,
Tommy C. Li9a180482019-06-27 20:49:00722 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
723 "http://www.google.com/?foo&"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24724 {u"foo", OmniboxFocusType::ON_FOCUS,
Tommy C. Li9a180482019-06-27 20:49:00725 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
726 "http://www.google.com/?foo&oft=1&"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24727 {u"foo", OmniboxFocusType::DELETED_PERMANENT_TEXT,
Tommy Li5e883512020-08-07 19:32:04728 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
729 "http://www.google.com/?foo&oft=2&"},
Tommy C. Li9a180482019-06-27 20:49:00730 };
731 TemplateURLData data;
732 data.input_encodings.push_back("UTF-8");
733 for (size_t i = 0; i < base::size(test_data); ++i) {
734 data.SetURL(test_data[i].url);
735 TemplateURL url(data);
736 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
737 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
738 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
Tommy Li5e883512020-08-07 19:32:04739 search_terms_args.focus_type = test_data[i].focus_type;
Tommy C. Li9a180482019-06-27 20:49:00740 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
741 search_terms_data_));
742 ASSERT_TRUE(result.is_valid());
743 EXPECT_EQ(test_data[i].expected_result, result.spec());
744 }
745}
746
Ryan Sturm29853d52020-11-17 02:14:04747// Tests replacing prefetch source (&pf=).
748TEST_F(TemplateURLTest, ReplaceIsPrefetch) {
749 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01750 const std::u16string search_term;
Ryan Sturm29853d52020-11-17 02:14:04751 bool is_prefetch;
752 const std::string url;
753 const std::string expected_result;
754 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24755 {u"foo", false, "{google:baseURL}?{searchTerms}&{google:prefetchSource}",
Ryan Sturm29853d52020-11-17 02:14:04756 "http://www.google.com/?foo&"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24757 {u"foo", true, "{google:baseURL}?{searchTerms}&{google:prefetchSource}",
Ryan Sturm29853d52020-11-17 02:14:04758 "http://www.google.com/?foo&pf=cs&"},
759 };
760 TemplateURLData data;
761 data.input_encodings.push_back("UTF-8");
762 for (size_t i = 0; i < base::size(test_data); ++i) {
763 data.SetURL(test_data[i].url);
764 TemplateURL url(data);
765 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
766 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
767 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
768 search_terms_args.is_prefetch = test_data[i].is_prefetch;
769 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
770 search_terms_data_));
771 ASSERT_TRUE(result.is_valid());
772 EXPECT_EQ(test_data[i].expected_result, result.spec());
773 }
774}
775
[email protected]9b9fa672013-11-07 06:04:52776// Tests replacing currentPageUrl.
777TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48778 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01779 const std::u16string search_term;
[email protected]9b9fa672013-11-07 06:04:52780 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48781 const std::string url;
782 const std::string expected_result;
783 } test_data[] = {
Jan Wilken Dörrie756999e2021-03-23 15:05:24784 {u"foo", "http://www.google.com/",
785 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
786 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&"},
787 {u"foo", "", "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
788 "http://www.google.com/?foo&"},
789 {u"foo", "http://g.com/+-/*&=",
790 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
791 "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:48792 };
793 TemplateURLData data;
794 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02795 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]800569d2013-05-06 07:38:48796 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28797 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19798 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
799 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]800569d2013-05-06 07:38:48800 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52801 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19802 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
803 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48804 ASSERT_TRUE(result.is_valid());
805 EXPECT_EQ(test_data[i].expected_result, result.spec());
806 }
807}
808
Pavel Yatsuk0d6a7b5c2019-10-09 20:33:44809// Tests appending attribution parameter to queries originating from Play API
810// search engine.
811TEST_F(TemplateURLTest, PlayAPIAttribution) {
812 const struct TestData {
813 const char* url;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01814 std::u16string terms;
Pavel Yatsuk0d6a7b5c2019-10-09 20:33:44815 bool created_from_play_api;
816 const char* output;
Jan Wilken Dörrie756999e2021-03-23 15:05:24817 } test_data[] = {
818 {"http://foo/?q={searchTerms}", u"bar", false, "http://foo/?q=bar"},
819 {"http://foo/?q={searchTerms}", u"bar", true,
820 "http://foo/?q=bar&chrome_dse_attribution=1"}};
Pavel Yatsuk0d6a7b5c2019-10-09 20:33:44821 TemplateURLData data;
822 for (size_t i = 0; i < base::size(test_data); ++i) {
823 data.SetURL(test_data[i].url);
824 data.created_from_play_api = test_data[i].created_from_play_api;
825 TemplateURL url(data);
826 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
827 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
828 GURL result(url.url_ref().ReplaceSearchTerms(
829 TemplateURLRef::SearchTermsArgs(test_data[i].terms),
830 search_terms_data_));
831 ASSERT_TRUE(result.is_valid());
832 EXPECT_EQ(test_data[i].output, result.spec());
833 }
834}
835
[email protected]d82443b2009-01-15 19:54:56836TEST_F(TemplateURLTest, Suggestions) {
837 struct TestData {
838 const int accepted_suggestion;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01839 const std::u16string original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56840 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27841 } test_data[] = {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01842 {TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::u16string(),
843 "http://bar/foo?q=foobar"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24844 {TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, u"foo",
Jan Wilken Dörriefa241ba2021-03-11 17:57:01845 "http://bar/foo?q=foobar"},
846 {TemplateURLRef::NO_SUGGESTION_CHOSEN, std::u16string(),
847 "http://bar/foo?q=foobar"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24848 {TemplateURLRef::NO_SUGGESTION_CHOSEN, u"foo", "http://bar/foo?q=foobar"},
Jan Wilken Dörriefa241ba2021-03-11 17:57:01849 {0, std::u16string(), "http://bar/foo?oq=&q=foobar"},
Jan Wilken Dörrie756999e2021-03-23 15:05:24850 {1, u"foo", "http://bar/foo?oq=foo&q=foobar"},
[email protected]d82443b2009-01-15 19:54:56851 };
[email protected]573889f22012-04-07 01:31:54852 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37853 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
854 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54855 data.input_encodings.push_back("UTF-8");
[email protected]168d08722014-06-18 07:13:28856 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19857 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
858 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Avi Drissman2244c2d2018-12-25 23:08:02859 for (size_t i = 0; i < base::size(test_data); ++i) {
Jan Wilken Dörrie756999e2021-03-23 15:05:24860 TemplateURLRef::SearchTermsArgs search_terms_args(u"foobar");
[email protected]bca359b2012-06-24 07:53:04861 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
862 search_terms_args.original_query =
863 test_data[i].original_query_for_suggestion;
[email protected]ce7ee5f2014-06-16 23:41:19864 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
865 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27866 ASSERT_TRUE(result.is_valid());
867 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56868 }
869}
870
[email protected]81f808de2009-09-25 01:36:34871TEST_F(TemplateURLTest, RLZ) {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01872 std::u16string rlz_string = search_terms_data_.GetRlzParameterValue(false);
[email protected]d82443b2009-01-15 19:54:56873
[email protected]573889f22012-04-07 01:31:54874 TemplateURLData data;
875 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28876 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19877 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
878 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04879 GURL result(url.url_ref().ReplaceSearchTerms(
Jan Wilken Dörrie756999e2021-03-23 15:05:24880 TemplateURLRef::SearchTermsArgs(u"x"), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56881 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44882 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
883 result.spec());
[email protected]d82443b2009-01-15 19:54:56884}
885
[email protected]c8ccc41d2014-04-10 04:42:12886TEST_F(TemplateURLTest, RLZFromAppList) {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01887 std::u16string rlz_string = search_terms_data_.GetRlzParameterValue(true);
[email protected]c8ccc41d2014-04-10 04:42:12888
889 TemplateURLData data;
890 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28891 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19892 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
893 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Jan Wilken Dörrie756999e2021-03-23 15:05:24894 TemplateURLRef::SearchTermsArgs args(u"x");
Moe Ahmadi4715d9542020-10-21 01:00:32895 args.request_source = TemplateURLRef::CROS_APP_LIST;
[email protected]ce7ee5f2014-06-16 23:41:19896 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12897 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44898 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
899 result.spec());
[email protected]c8ccc41d2014-04-10 04:42:12900}
[email protected]c8ccc41d2014-04-10 04:42:12901
[email protected]d82443b2009-01-15 19:54:56902TEST_F(TemplateURLTest, HostAndSearchTermKey) {
903 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19904 const std::string url;
[email protected]d82443b2009-01-15 19:54:56905 const std::string host;
906 const std::string path;
907 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54908 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50909 {"http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
910 {"http://blah/{searchTerms}", "blah", "", ""},
[email protected]d82443b2009-01-15 19:54:56911
vitbarf2a11562017-05-15 14:09:50912 // No term should result in empty values.
913 {"http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56914
vitbarf2a11562017-05-15 14:09:50915 // Multiple terms should result in empty values.
916 {"http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56917
vitbarf2a11562017-05-15 14:09:50918 // Term in the host shouldn't match.
919 {"http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56920
vitbarf2a11562017-05-15 14:09:50921 {"http://blah/?q={searchTerms}", "blah", "/", "q"},
922 {"https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56923
vitbarf2a11562017-05-15 14:09:50924 // Single term with extra chars in value should match.
925 {"http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56926 };
927
Avi Drissman2244c2d2018-12-25 23:08:02928 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54929 TemplateURLData data;
930 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28931 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19932 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
933 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
934 EXPECT_EQ(test_data[i].search_term_key,
935 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56936 }
937}
938
alexmos294849f2015-03-14 00:55:06939TEST_F(TemplateURLTest, SearchTermKeyLocation) {
940 struct TestData {
941 const std::string url;
942 const url::Parsed::ComponentType location;
943 const std::string path;
vitbarf2a11562017-05-15 14:09:50944 const std::string key;
945 const std::string value_prefix;
946 const std::string value_suffix;
alexmos294849f2015-03-14 00:55:06947 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50948 {"http://blah/{searchTerms}/", url::Parsed::PATH, "", "", "/", "/"},
949 {"http://blah/{searchTerms}", url::Parsed::PATH, "", "", "/", ""},
950 {"http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "", "",
951 "/begin/", "/end"},
952 {"http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY, "/", "q",
953 "", ""},
954 {"http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF, "/", "x",
955 "", ""},
956 {"http://www.example.com/?q=chromium-{searchTerms}@chromium.org/info",
957 url::Parsed::QUERY, "/", "q", "chromium-", "@chromium.org/info"},
alexmos294849f2015-03-14 00:55:06958
vitbarf2a11562017-05-15 14:09:50959 // searchTerms is a key, not a value, so this should result in an empty
960 // value.
961 {"http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x",
962 url::Parsed::QUERY, "", "", "", ""},
alexmos294849f2015-03-14 00:55:06963
vitbarf2a11562017-05-15 14:09:50964 // Multiple search terms should result in empty values.
965 {"http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY, "", "",
966 "", ""},
967 {"http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY, "", "",
968 "", ""},
969 {"http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY, "",
970 "", "", ""},
alexmos294849f2015-03-14 00:55:06971 };
972
Avi Drissman2244c2d2018-12-25 23:08:02973 for (size_t i = 0; i < base::size(test_data); ++i) {
alexmos294849f2015-03-14 00:55:06974 TemplateURLData data;
975 data.SetURL(test_data[i].url);
976 TemplateURL url(data);
977 EXPECT_EQ(test_data[i].location,
978 url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
979 EXPECT_EQ(test_data[i].path,
980 url.url_ref().GetPath(search_terms_data_));
vitbarf2a11562017-05-15 14:09:50981 EXPECT_EQ(test_data[i].key,
982 url.url_ref().GetSearchTermKey(search_terms_data_));
983 EXPECT_EQ(test_data[i].value_prefix,
984 url.url_ref().GetSearchTermValuePrefix(search_terms_data_));
985 EXPECT_EQ(test_data[i].value_suffix,
986 url.url_ref().GetSearchTermValueSuffix(search_terms_data_));
alexmos294849f2015-03-14 00:55:06987 }
988}
989
[email protected]d82443b2009-01-15 19:54:56990TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
991 static const struct {
[email protected]ddd231e2010-06-29 20:35:19992 const char* const base_url;
993 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56994 } data[] = {
[email protected]014010e2011-10-01 04:12:44995 { "http://google.com/", "http://google.com/complete/", },
996 { "http://www.google.com/", "http://www.google.com/complete/", },
997 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
998 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
999 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:561000 };
1001
Avi Drissman2244c2d2018-12-25 23:08:021002 for (size_t i = 0; i < base::size(data); ++i)
[email protected]d82443b2009-01-15 19:54:561003 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
1004}
1005
[email protected]81c6ef62010-01-21 09:58:471006TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:191007 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:541008 TemplateURLData data;
1009 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:281010 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471011 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:131012 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:191013 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:471014 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271015 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471016 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1017}
1018
1019TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:201020 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:541021 TemplateURLData data;
1022 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:281023 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471024 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:441025
1026 // By default, TemplateURLRef should not consider itself prepopulated.
1027 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:131028 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:201029 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:441030 EXPECT_TRUE(replacements.empty());
1031
1032 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:201033 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:541034 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:281035 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:151036 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:201037 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:471038 EXPECT_TRUE(replacements.empty());
1039}
1040
1041TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:281042 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:471043 TemplateURLRef::Replacements replacements;
1044 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241045 EXPECT_EQ(std::string(), url.url_ref().ParseURL(std::string(), &replacements,
1046 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471047 EXPECT_TRUE(replacements.empty());
1048 EXPECT_TRUE(valid);
1049}
1050
1051TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:541052 TemplateURLData data;
1053 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:281054 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471055 TemplateURLRef::Replacements replacements;
1056 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241057 EXPECT_EQ(std::string(),
1058 url.url_ref().ParseURL("{", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471059 EXPECT_TRUE(replacements.empty());
1060 EXPECT_FALSE(valid);
1061}
1062
1063TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:541064 TemplateURLData data;
1065 data.SetURL("{}");
[email protected]168d08722014-06-18 07:13:281066 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471067 TemplateURLRef::Replacements replacements;
1068 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241069 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471070 EXPECT_TRUE(replacements.empty());
1071 EXPECT_TRUE(valid);
1072}
1073
1074TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:541075 TemplateURLData data;
1076 data.SetURL("{}{{%s}}");
[email protected]168d08722014-06-18 07:13:281077 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471078 TemplateURLRef::Replacements replacements;
1079 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241080 EXPECT_EQ("{}{}", url.url_ref().ParseURL("{}{{searchTerms}}", &replacements,
1081 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471082 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271083 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471084 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1085 EXPECT_TRUE(valid);
1086}
1087
1088TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:541089 TemplateURLData data;
1090 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:281091 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471092 TemplateURLRef::Replacements replacements;
1093 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241094 EXPECT_EQ("{", url.url_ref().ParseURL("{{searchTerms}", &replacements,
1095 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471096 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271097 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471098 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1099 EXPECT_TRUE(valid);
1100}
[email protected]5b3078752012-10-09 18:54:161101
[email protected]fd6d8822012-12-08 06:56:111102TEST_F(TemplateURLTest, SearchClient) {
1103 const std::string base_url_str("http://google.com/?");
1104 const std::string terms_str("{searchTerms}&{google:searchClient}");
1105 const std::string full_url_str = base_url_str + terms_str;
Jan Wilken Dörriefa241ba2021-03-11 17:57:011106 const std::u16string terms(ASCIIToUTF16(terms_str));
[email protected]798baa8e2014-06-20 05:42:441107 search_terms_data_.set_google_base_url(base_url_str);
[email protected]fd6d8822012-12-08 06:56:111108
1109 TemplateURLData data;
1110 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:281111 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191112 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1113 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Jan Wilken Dörrie756999e2021-03-23 15:05:241114 TemplateURLRef::SearchTermsArgs search_terms_args(u"foobar");
[email protected]fd6d8822012-12-08 06:56:111115
1116 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:191117 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1118 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111119 ASSERT_TRUE(result.is_valid());
1120 EXPECT_EQ("http://google.com/?foobar&", result.spec());
1121
1122 // Check that the URL is correct when a client is present.
[email protected]798baa8e2014-06-20 05:42:441123 search_terms_data_.set_search_client("search_client");
[email protected]ce7ee5f2014-06-16 23:41:191124 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
1125 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111126 ASSERT_TRUE(result_2.is_valid());
[email protected]798baa8e2014-06-20 05:42:441127 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
[email protected]fd6d8822012-12-08 06:56:111128}
[email protected]fd6d8822012-12-08 06:56:111129
Moe Ahmadi4715d9542020-10-21 01:00:321130TEST_F(TemplateURLTest, SuggestClient) {
1131 const std::string base_url_str("http://google.com/?");
1132 const std::string query_params_str("client={google:suggestClient}");
1133 const std::string full_url_str = base_url_str + query_params_str;
1134 search_terms_data_.set_google_base_url(base_url_str);
1135
1136 TemplateURLData data;
1137 data.SetURL(full_url_str);
1138 TemplateURL url(data);
1139 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1140 ASSERT_FALSE(url.url_ref().SupportsReplacement(search_terms_data_));
1141 TemplateURLRef::SearchTermsArgs search_terms_args;
1142
1143 // Check that the URL is correct when a client is not present.
1144 GURL result(
1145 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_));
1146 ASSERT_TRUE(result.is_valid());
1147 EXPECT_EQ("http://google.com/?client=", result.spec());
1148
1149 // Check that the URL is correct when a client is present.
1150 search_terms_data_.set_suggest_client("suggest_client");
1151 GURL result_2(
1152 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_));
1153 ASSERT_TRUE(result_2.is_valid());
1154 EXPECT_EQ("http://google.com/?client=suggest_client", result_2.spec());
Moe Ahmadi4715d9542020-10-21 01:00:321155}
1156
Moe Ahmadi66fc8334c2021-11-19 22:21:581157TEST_F(TemplateURLTest, ZeroSuggestCacheDuration) {
1158 const std::string base_url_str("http://google.com/?");
1159 const std::string query_params_str("{google:clientCacheTimeToLive}");
1160 const std::string full_url_str = base_url_str + query_params_str;
1161 search_terms_data_.set_google_base_url(base_url_str);
1162 TemplateURLData data;
1163 data.SetURL(full_url_str);
1164 TemplateURL url(data);
1165 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1166 ASSERT_FALSE(url.url_ref().SupportsReplacement(search_terms_data_));
1167
1168 {
1169 // 'ccttl' query param should not be present if no cache duration is given.
1170 TemplateURLRef::SearchTermsArgs search_terms_args;
1171 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1172 search_terms_data_));
1173 ASSERT_TRUE(result.is_valid());
1174 EXPECT_EQ("http://google.com/?", result.spec());
1175 }
1176 {
1177 // 'ccttl' query param should be present if a positive cache duration is
1178 // given.
1179 TemplateURLRef::SearchTermsArgs search_terms_args;
1180 search_terms_args.zero_suggest_cache_duration_sec = 300;
1181 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1182 search_terms_data_));
1183 ASSERT_TRUE(result.is_valid());
1184 EXPECT_EQ("http://google.com/?ccttl=300&", result.spec());
1185 }
1186 {
1187 // 'ccttl' query param shouldn't be present if a zero cache duration is
1188 // given.
1189 TemplateURLRef::SearchTermsArgs search_terms_args;
1190 search_terms_args.zero_suggest_cache_duration_sec = 0;
1191 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1192 search_terms_data_));
1193 ASSERT_TRUE(result.is_valid());
1194 EXPECT_EQ("http://google.com/?", result.spec());
1195 }
1196 {
1197 // 'ccttl' query param should not be present if the request is not a
1198 // zero-prefix request.
1199 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
1200 search_terms_args.zero_suggest_cache_duration_sec = 300;
1201 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1202 search_terms_data_));
1203 ASSERT_TRUE(result.is_valid());
1204 EXPECT_EQ("http://google.com/?", result.spec());
1205 }
1206}
1207
[email protected]5b3078752012-10-09 18:54:161208TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
1209 TemplateURLData data;
1210 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161211 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
1212 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281213 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511214 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1215 ASSERT_EQ(3U, url_refs.size());
1216 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1217 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1218 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161219}
1220
1221TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
1222 TemplateURLData data;
1223 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:281224 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511225 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1226 ASSERT_EQ(1U, url_refs.size());
1227 EXPECT_EQ("http://www.google.co.uk/", url_refs[0].GetURL());
[email protected]5b3078752012-10-09 18:54:161228}
1229
1230TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1231 TemplateURLData data;
1232 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161233 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1234 data.alternate_urls.push_back(
1235 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281236 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011237 std::u16string result;
[email protected]5b3078752012-10-09 18:54:161238
1239 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191240 GURL("http://google.com/?q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241241 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191242
1243 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471244 GURL("http://google.com/?q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241245 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191246
1247 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471248 GURL("http://google.com/?q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241249 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191250
1251 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471252 GURL("http://google.com/?q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241253 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191254
1255 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191256 GURL("http://google.com/alt/#q=something"),
1257 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241258 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191259
1260 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471261 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241262 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191263
1264 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471265 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241266 EXPECT_EQ(u"something", result);
[email protected]4076ea62013-01-09 01:47:191267
1268 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471269 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241270 EXPECT_EQ(u"something", result);
[email protected]5b3078752012-10-09 18:54:161271
1272 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191273 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011274 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161275
1276 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191277 GURL("http://google.ca/?q=something&q=anything"),
1278 search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011279 EXPECT_EQ(std::u16string(), result);
[email protected]67d8b752013-04-03 17:33:271280
1281 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191282 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011283 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161284
[email protected]32e2d81b2012-10-16 19:03:251285 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191286 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241287 EXPECT_EQ(u"foo", result);
[email protected]5b3078752012-10-09 18:54:161288
1289 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191290 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011291 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161292
1293 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191294 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241295 EXPECT_EQ(u"1 2 3", result);
[email protected]5b3078752012-10-09 18:54:161296
1297 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191298 GURL("http://google.com/alt/?q=123#q=456"),
1299 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241300 EXPECT_EQ(u"456", result);
[email protected]5b3078752012-10-09 18:54:161301
1302 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191303 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1304 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241305 EXPECT_EQ(u"123", result);
[email protected]5b3078752012-10-09 18:54:161306
1307 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191308 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1309 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241310 EXPECT_EQ(u"789", result);
[email protected]5b3078752012-10-09 18:54:161311
1312 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191313 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011314 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161315
1316 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191317 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011318 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161319
1320 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191321 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011322 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161323
1324 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191325 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011326 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161327
1328 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191329 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241330 EXPECT_EQ(u"123", result);
[email protected]5b3078752012-10-09 18:54:161331}
[email protected]4076ea62013-01-09 01:47:191332
alexmos294849f2015-03-14 00:55:061333TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1334 TemplateURLData data;
1335 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1336 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011337 std::u16string result;
alexmos294849f2015-03-14 00:55:061338
1339 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1340 GURL("http://term-in-path.com/begin/something/end"),
1341 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241342 EXPECT_EQ(u"something", result);
alexmos294849f2015-03-14 00:55:061343
1344 // "%20" must be converted to space.
1345 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1346 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1347 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241348 EXPECT_EQ(u"a b c", result);
alexmos294849f2015-03-14 00:55:061349
1350 // Plus must not be converted to space.
1351 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1352 GURL("http://term-in-path.com/begin/1+2+3/end"),
1353 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241354 EXPECT_EQ(u"1+2+3", result);
alexmos294849f2015-03-14 00:55:061355
1356 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1357 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011358 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061359
1360 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1361 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011362 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061363
1364 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1365 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011366 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061367}
1368
vitbar27804d102015-04-15 10:54:411369// Checks that the ExtractSearchTermsFromURL function works correctly
1370// for urls containing non-latin characters in UTF8 encoding.
1371TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
1372 TemplateURLData data;
1373 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1374 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1375 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1376 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011377 std::u16string result;
vitbar27804d102015-04-15 10:54:411378
1379 // Russian text encoded with UTF-8.
1380 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551381 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1382 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
vitbar27804d102015-04-15 10:54:411383 search_terms_data_, &result));
1384 EXPECT_EQ(
Jan Wilken Dörrie72da4982021-03-19 19:21:091385 u"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1386 u"\x043C\x0438\x0440!",
vitbar27804d102015-04-15 10:54:411387 result);
1388
1389 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551390 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1391 "%D0%B0"),
vitbar27804d102015-04-15 10:54:411392 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331393 EXPECT_EQ(u"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430", result);
vitbar27804d102015-04-15 10:54:411394
1395 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551396 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1397 "%D0%B8%20A"),
vitbar27804d102015-04-15 10:54:411398 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331399 EXPECT_EQ(u"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A", result);
vitbar27804d102015-04-15 10:54:411400}
1401
1402// Checks that the ExtractSearchTermsFromURL function works correctly
1403// for urls containing non-latin characters in non-UTF8 encoding.
1404TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) {
1405 TemplateURLData data;
1406 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1407 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1408 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1409 data.input_encodings.push_back("windows-1251");
1410 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011411 std::u16string result;
vitbar27804d102015-04-15 10:54:411412
1413 // Russian text encoded with Windows-1251.
1414 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1415 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1416 "%EC%E8%F0!"),
1417 search_terms_data_, &result));
1418 EXPECT_EQ(
Jan Wilken Dörrie72da4982021-03-19 19:21:091419 u"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1420 u"\x043C\x0438\x0440!",
vitbar27804d102015-04-15 10:54:411421 result);
1422
1423 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1424 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1425 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331426 EXPECT_EQ(u"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430", result);
vitbar27804d102015-04-15 10:54:411427
1428 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1429 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1430 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331431 EXPECT_EQ(u"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A", result);
vitbar27804d102015-04-15 10:54:411432}
1433
jbroman50832022016-04-21 23:53:001434// Checks that the ExtractSearchTermsFromURL function strips constant
1435// prefix/suffix strings from the search terms param.
1436TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) {
1437 TemplateURLData data;
1438 data.alternate_urls.push_back(
1439 "http://www.example.com/?q=chromium-{searchTerms}@chromium.org");
1440 data.alternate_urls.push_back(
1441 "http://www.example.com/chromium-{searchTerms}@chromium.org/info");
1442 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011443 std::u16string result;
jbroman50832022016-04-21 23:53:001444
1445 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1446 GURL("http://www.example.com/[email protected]"),
1447 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241448 EXPECT_EQ(u"dev", result);
jbroman50832022016-04-21 23:53:001449
1450 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1451 GURL("http://www.example.com/[email protected]/info"),
1452 search_terms_data_, &result));
Jan Wilken Dörrie756999e2021-03-23 15:05:241453 EXPECT_EQ(u"dev", result);
jbroman50832022016-04-21 23:53:001454
1455 // Don't match if the prefix and suffix aren't there.
1456 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1457 GURL("http://www.example.com/?q=invalid"), search_terms_data_, &result));
jbromane8b8728d2017-04-12 17:29:391458
1459 // Don't match if the prefix and suffix overlap.
1460 TemplateURLData data_with_overlap;
1461 data.alternate_urls.push_back(
1462 "http://www.example.com/?q=goo{searchTerms}oogle");
1463 TemplateURL url_with_overlap(data);
1464 EXPECT_FALSE(url_with_overlap.ExtractSearchTermsFromURL(
1465 GURL("http://www.example.com/?q=google"), search_terms_data_, &result));
jbroman50832022016-04-21 23:53:001466}
1467
[email protected]f62e30f52013-03-23 03:45:151468TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1469 TemplateURLData data;
1470 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]f62e30f52013-03-23 03:45:151471 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1472 data.alternate_urls.push_back(
1473 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281474 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241475 TemplateURLRef::SearchTermsArgs search_terms(u"Bob Morane");
[email protected]f62e30f52013-03-23 03:45:151476 GURL result;
1477
1478 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191479 GURL("http://google.com/?q=something"), search_terms,
1480 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101481 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151482
1483 result = GURL("http://should.not.change.com");
1484 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191485 GURL("http://google.ca/?q=something"), search_terms,
1486 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151487 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1488
1489 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191490 GURL("http://google.com/foo/?q=foo"), search_terms,
1491 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151492
1493 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191494 GURL("https://google.com/?q=foo"), search_terms,
1495 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101496 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151497
1498 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191499 GURL("http://google.com:8080/?q=foo"), search_terms,
1500 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151501
1502 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191503 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1504 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101505 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result);
[email protected]f62e30f52013-03-23 03:45:151506
1507 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1508 // template_url.cc for details.
1509 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191510 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1511 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101512 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151513
1514 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1515 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191516 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101517 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
[email protected]f62e30f52013-03-23 03:45:151518 result);
1519
1520 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1521 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191522 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151523 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
vitbarf298455d2015-04-21 12:58:101524 "#j=abc&q=Bob+Morane&h=def9"), result);
[email protected]f62e30f52013-03-23 03:45:151525
1526 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191527 GURL("http://google.com/alt/?q="), search_terms,
1528 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151529
1530 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191531 GURL("http://google.com/alt/?#q="), search_terms,
1532 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151533
1534 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191535 GURL("http://google.com/alt/?q=#q="), search_terms,
1536 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151537
1538 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191539 GURL("http://google.com/alt/?q=123#q="), search_terms,
1540 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151541
1542 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191543 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1544 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101545 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151546}
[email protected]56fa29592013-07-02 20:25:531547
alexmos294849f2015-03-14 00:55:061548TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1549 TemplateURLData data;
1550 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1551 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241552 TemplateURLRef::SearchTermsArgs search_terms(u"Bob Morane");
alexmos294849f2015-03-14 00:55:061553 GURL result;
1554
1555 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1556 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1557 search_terms_data_, &result));
1558 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1559
1560 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1561 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1562 search_terms_data_, &result));
1563 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1564
1565 result = GURL("http://should.not.change.com");
1566 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1567 GURL("http://term-in-path.com/about"), search_terms,
1568 search_terms_data_, &result));
1569 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1570}
1571
vitbarf298455d2015-04-21 12:58:101572// Checks that the ReplaceSearchTermsInURL function works correctly
1573// for search terms containing non-latin characters for a search engine
1574// using UTF-8 input encoding.
1575TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) {
1576 TemplateURLData data;
1577 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1578 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1579 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1580 TemplateURL url(data);
1581
1582 // Russian text which will be encoded with UTF-8.
Jan Wilken Dörrie72da4982021-03-19 19:21:091583 TemplateURLRef::SearchTermsArgs search_terms(
1584 u"\x0442\x0435\x043A\x0441\x0442");
vitbarf298455d2015-04-21 12:58:101585 GURL result;
1586
1587 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1588 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_,
1589 &result));
1590 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1591 result);
1592
1593 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1594 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_,
1595 &result));
1596 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1597 result);
1598
1599 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1600 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_,
1601 &result));
1602 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1603 result);
1604}
1605
1606// Checks that the ReplaceSearchTermsInURL function works correctly
1607// for search terms containing non-latin characters for a search engine
1608// using non UTF-8 input encoding.
1609TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) {
1610 TemplateURLData data;
1611 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1612 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1613 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1614 data.input_encodings.push_back("windows-1251");
1615 TemplateURL url(data);
1616
1617 // Russian text which will be encoded with Windows-1251.
Jan Wilken Dörrie72da4982021-03-19 19:21:091618 TemplateURLRef::SearchTermsArgs search_terms(
1619 u"\x0442\x0435\x043A\x0441\x0442");
vitbarf298455d2015-04-21 12:58:101620 GURL result;
1621
1622 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1623 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_,
1624 &result));
1625 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1626 result);
1627
1628 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1629 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_,
1630 &result));
1631 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1632 result);
1633
1634 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1635 GURL("http://windows-1251.ru/path/a%20b"), search_terms,
1636 search_terms_data_, &result));
1637 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1638 result);
1639}
1640
Mark Pearson247fb8a2018-08-30 05:12:261641// Test the |additional_query_params| field of SearchTermsArgs.
[email protected]621ade062013-10-28 06:27:431642TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431643 TemplateURLData data;
1644 // Pick a URL with replacements before, during, and after the query, to ensure
1645 // we don't goof up any of them.
1646 data.SetURL("{google:baseURL}search?q={searchTerms}"
1647 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281648 TemplateURL url(data);
[email protected]621ade062013-10-28 06:27:431649
Mark Pearson247fb8a2018-08-30 05:12:261650 // Baseline: no |additional_query_params| field.
Jan Wilken Dörrie756999e2021-03-23 15:05:241651 TemplateURLRef::SearchTermsArgs search_terms(u"abc");
1652 search_terms.original_query = u"def";
[email protected]621ade062013-10-28 06:27:431653 search_terms.accepted_suggestion = 0;
1654 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191655 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431656
Mark Pearson247fb8a2018-08-30 05:12:261657 // Set the |additional_query_params|.
1658 search_terms.additional_query_params = "pq=xyz";
[email protected]621ade062013-10-28 06:27:431659 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191660 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431661
Mark Pearson247fb8a2018-08-30 05:12:261662 // Add |append_extra_query_params_from_command_line| into the mix, and ensure
1663 // it works.
1664 search_terms.append_extra_query_params_from_command_line = true;
avi1772c1a2014-12-22 22:42:331665 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]621ade062013-10-28 06:27:431666 switches::kExtraSearchQueryParams, "a=b");
1667 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191668 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431669}
1670
Mark Pearson247fb8a2018-08-30 05:12:261671// Test the |search_terms.append_extra_query_params_from_command_line| field of
1672// SearchTermsArgs.
[email protected]56fa29592013-07-02 20:25:531673TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531674 TemplateURLData data;
1675 // Pick a URL with replacements before, during, and after the query, to ensure
1676 // we don't goof up any of them.
1677 data.SetURL("{google:baseURL}search?q={searchTerms}"
1678 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281679 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531680
Mark Pearson247fb8a2018-08-30 05:12:261681 // Baseline: no command-line args, no
1682 // |search_terms.append_extra_query_params_from_command_line| flag.
Jan Wilken Dörrie756999e2021-03-23 15:05:241683 TemplateURLRef::SearchTermsArgs search_terms(u"abc");
1684 search_terms.original_query = u"def";
[email protected]56fa29592013-07-02 20:25:531685 search_terms.accepted_suggestion = 0;
1686 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191687 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531688
1689 // Set the flag. Since there are no command-line args, this should have no
1690 // effect.
Mark Pearson247fb8a2018-08-30 05:12:261691 search_terms.append_extra_query_params_from_command_line = true;
[email protected]56fa29592013-07-02 20:25:531692 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191693 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531694
1695 // Now append the command-line arg. This should be inserted into the query.
avi1772c1a2014-12-22 22:42:331696 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]56fa29592013-07-02 20:25:531697 switches::kExtraSearchQueryParams, "a=b");
1698 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191699 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531700
1701 // Turn off the flag. Now the command-line arg should be ignored again.
Mark Pearson247fb8a2018-08-30 05:12:261702 search_terms.append_extra_query_params_from_command_line = false;
[email protected]56fa29592013-07-02 20:25:531703 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191704 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531705}
[email protected]d5015ca2013-08-08 22:04:181706
1707// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171708TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181709 TemplateURLData data;
1710 data.input_encodings.push_back("UTF-8");
1711 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281712 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191713 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1714 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Jan Wilken Dörrie756999e2021-03-23 15:05:241715 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]d5015ca2013-08-08 22:04:181716
[email protected]ce7ee5f2014-06-16 23:41:191717 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1718 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181719 EXPECT_EQ("http://www.google.com/?q=foo", result);
1720
[email protected]332d17d22014-06-20 16:56:031721 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191722 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1723 search_terms_data_);
Moe Ahmadi8edf65682022-01-25 02:44:071724 EXPECT_EQ(base::FeatureList::IsEnabled(omnibox::kZeroSuggestPrefetching)
1725 ? "http://www.google.com/?q=foo"
1726 : "http://www.google.com/?pgcl=1&q=foo",
1727 result);
[email protected]d5015ca2013-08-08 22:04:181728
1729 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031730 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191731 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1732 search_terms_data_);
Moe Ahmadi8edf65682022-01-25 02:44:071733 EXPECT_EQ(base::FeatureList::IsEnabled(omnibox::kZeroSuggestPrefetching)
1734 ? "http://www.google.com/?q=foo"
1735 : "http://www.google.com/?pgcl=3&q=foo",
1736 result);
[email protected]d5015ca2013-08-08 22:04:181737}
[email protected]2328ee22013-08-08 23:00:191738
1739// Test the IsSearchResults function.
1740TEST_F(TemplateURLTest, IsSearchResults) {
1741 TemplateURLData data;
1742 data.SetURL("http://bar/search?q={searchTerms}");
[email protected]2767c0fd2013-08-16 17:44:161743 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191744 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1745 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1746 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1747 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281748 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191749
1750 const struct {
1751 const char* const url;
1752 bool result;
1753 } url_data[] = {
1754 { "http://bar/search?q=foo&oq=foo", true, },
1755 { "http://bar/?q=foo&oq=foo", true, },
1756 { "http://bar/#output=search&q=foo&oq=foo", true, },
1757 { "http://bar/webhp#q=foo&oq=foo", true, },
1758 { "http://bar/#q=foo&oq=foo", true, },
1759 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1760 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1761 { "http://bar/", false, },
1762 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161763 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191764 };
1765
Avi Drissman2244c2d2018-12-25 23:08:021766 for (size_t i = 0; i < base::size(url_data); ++i) {
[email protected]2328ee22013-08-08 23:00:191767 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191768 search_provider.IsSearchURL(GURL(url_data[i].url),
1769 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191770 }
1771}
[email protected]a048de8a2013-10-02 18:30:061772
jdonnelly41c5b46a2015-07-10 21:24:381773TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) {
[email protected]9d70de12014-05-10 02:15:311774 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441775 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311776 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1777
[email protected]168d08722014-06-18 07:13:281778 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241779 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]ce7ee5f2014-06-16 23:41:191780 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1781 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311782 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1783}
[email protected]20184242014-05-14 02:57:421784
1785TEST_F(TemplateURLTest, SessionToken) {
1786 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441787 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421788 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1789
[email protected]168d08722014-06-18 07:13:281790 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241791 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]20184242014-05-14 02:57:421792 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191793 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1794 search_terms_data_);
[email protected]20184242014-05-14 02:57:421795 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1796
[email protected]168d08722014-06-18 07:13:281797 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421798 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191799 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1800 search_terms_data_);
[email protected]20184242014-05-14 02:57:421801 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1802}
[email protected]448b17f52014-06-13 01:08:031803
1804TEST_F(TemplateURLTest, ContextualSearchParameters) {
1805 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441806 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031807 data.SetURL("http://bar/_/contextualsearch?"
1808 "{google:contextualSearchVersion}"
1809 "{google:contextualSearchContextData}");
1810
[email protected]168d08722014-06-18 07:13:281811 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241812 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]ce7ee5f2014-06-16 23:41:191813 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1814 search_terms_data_);
donndbb98274e2016-11-01 21:04:401815 EXPECT_EQ("http://bar/_/contextualsearch?", result);
[email protected]448b17f52014-06-13 01:08:031816
Donn Denman01b1bf72018-08-14 16:25:201817 // Test the current common case, which uses no home country or previous
1818 // event.
1819 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
Donn Denman97be51df2020-06-24 01:23:191820 2, 1, std::string(), 0, 0, false, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391821 std::string(), std::string());
[email protected]448b17f52014-06-13 01:08:031822 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191823 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1824 search_terms_data_);
donndd9fc4d92016-09-16 00:30:491825 EXPECT_EQ(
1826 "http://bar/_/contextualsearch?"
donnd0bc73292017-01-17 22:07:561827 "ctxs=2&"
donndd9fc4d92016-09-16 00:30:491828 "ctxsl_coca=1",
1829 result);
donndbb98274e2016-11-01 21:04:401830
Donn Denman01b1bf72018-08-14 16:25:201831 // Test the home country and non-zero event data case.
donndbb98274e2016-11-01 21:04:401832 search_terms_args.contextual_search_params =
Donn Denmandf4ede0f2020-01-17 23:11:221833 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denman97be51df2020-06-24 01:23:191834 2, 2, "CH", 1657713458, 5, false, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391835 std::string(), std::string());
donndbb98274e2016-11-01 21:04:401836 result =
1837 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1838
1839 EXPECT_EQ(
1840 "http://bar/_/contextualsearch?"
1841 "ctxs=2&"
donnd0bc73292017-01-17 22:07:561842 "ctxsl_coca=2&"
Donn Denman01b1bf72018-08-14 16:25:201843 "ctxs_hc=CH&"
1844 "ctxsl_pid=1657713458&"
1845 "ctxsl_per=5",
donndbb98274e2016-11-01 21:04:401846 result);
Donn Denmandf4ede0f2020-01-17 23:11:221847
1848 // Test exact-search.
1849 search_terms_args.contextual_search_params =
1850 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denman97be51df2020-06-24 01:23:191851 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391852 std::string(), std::string());
Donn Denmandf4ede0f2020-01-17 23:11:221853 result =
1854 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
Donn Denmandf4ede0f2020-01-17 23:11:221855 // Find our param.
1856 size_t found_pos = result.find("ctxsl_exact=1");
1857 EXPECT_NE(found_pos, std::string::npos);
Donn Denman7a241012020-01-31 18:33:131858
1859 // Test source and target languages.
1860 search_terms_args.contextual_search_params =
1861 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denmand638f4602020-11-03 21:05:391862 2, 1, std::string(), 0, 0, true, "es", "de", std::string(),
1863 std::string());
Donn Denman7a241012020-01-31 18:33:131864 result =
1865 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1866 // Find our params.
1867 size_t source_pos = result.find("tlitesl=es");
1868 EXPECT_NE(source_pos, std::string::npos);
1869 size_t target_pos = result.find("tlitetl=de");
1870 EXPECT_NE(target_pos, std::string::npos);
Donn Denman97be51df2020-06-24 01:23:191871
1872 // Test fluent languages.
1873 search_terms_args.contextual_search_params =
1874 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
1875 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391876 "es,de", std::string());
Donn Denman97be51df2020-06-24 01:23:191877 result =
1878 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1879 // Find our param. These may actually be URL encoded.
1880 size_t fluent_pos = result.find("&ctxs_fls=es,de");
1881 EXPECT_NE(fluent_pos, std::string::npos);
Donn Denmand638f4602020-11-03 21:05:391882
1883 // Test Related Searches.
1884 search_terms_args.contextual_search_params =
1885 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
1886 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
1887 std::string(), "1RbCu");
1888 result =
1889 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1890 // Find our param.
1891 size_t ctxsl_rs_pos = result.find("&ctxsl_rs=1RbCu");
1892 EXPECT_NE(ctxsl_rs_pos, std::string::npos);
[email protected]448b17f52014-06-13 01:08:031893}
[email protected]44ccc9f2014-06-20 17:36:211894
1895TEST_F(TemplateURLTest, GenerateKeyword) {
Jan Wilken Dörrie756999e2021-03-23 15:05:241896 ASSERT_EQ(u"foo", TemplateURL::GenerateKeyword(GURL("http://foo")));
1897 ASSERT_EQ(u"foo.", TemplateURL::GenerateKeyword(GURL("http://foo.")));
Emily Stark0fb634f82020-07-14 18:00:391898 // www. should be stripped for a public hostname but not a private/intranet
1899 // hostname.
Jan Wilken Dörrie756999e2021-03-23 15:05:241900 ASSERT_EQ(u"google.com",
Emily Stark0fb634f82020-07-14 18:00:391901 TemplateURL::GenerateKeyword(GURL("http://www.google.com")));
Jan Wilken Dörrie756999e2021-03-23 15:05:241902 ASSERT_EQ(u"www.foo", TemplateURL::GenerateKeyword(GURL("http://www.foo")));
[email protected]44ccc9f2014-06-20 17:36:211903 // Make sure we don't get a trailing '/'.
Jan Wilken Dörrie756999e2021-03-23 15:05:241904 ASSERT_EQ(u"blah", TemplateURL::GenerateKeyword(GURL("http://blah/")));
[email protected]44ccc9f2014-06-20 17:36:211905 // Don't generate the empty string.
Jan Wilken Dörrie756999e2021-03-23 15:05:241906 ASSERT_EQ(u"www.", TemplateURL::GenerateKeyword(GURL("http://www.")));
Peter Kastingcc07b332021-05-07 22:58:251907 ASSERT_EQ(u"абв", TemplateURL::GenerateKeyword(GURL("http://xn--80acd")));
a-v-y81695d0d2017-04-20 08:22:221908
1909 // Generated keywords must always be in lowercase, because TemplateURLs always
1910 // converts keywords to lowercase in its constructor and TemplateURLService
1911 // stores TemplateURLs in maps using keyword as key.
1912 EXPECT_TRUE(IsLowerCase(TemplateURL::GenerateKeyword(GURL("http://BLAH/"))));
1913 EXPECT_TRUE(IsLowerCase(
1914 TemplateURL::GenerateKeyword(GURL("http://embeddedhtml.<head>/"))));
[email protected]44ccc9f2014-06-20 17:36:211915}
1916
1917TEST_F(TemplateURLTest, GenerateSearchURL) {
1918 struct GenerateSearchURLCase {
1919 const char* test_name;
1920 const char* url;
1921 const char* expected;
1922 } generate_url_cases[] = {
1923 { "invalid URL", "foo{searchTerms}", "" },
1924 { "URL with no replacements", "http://foo/", "http://foo/" },
1925 { "basic functionality", "http://foo/{searchTerms}",
1926 "http://foo/blah.blah.blah.blah.blah" }
1927 };
1928
Avi Drissman2244c2d2018-12-25 23:08:021929 for (size_t i = 0; i < base::size(generate_url_cases); ++i) {
[email protected]44ccc9f2014-06-20 17:36:211930 TemplateURLData data;
1931 data.SetURL(generate_url_cases[i].url);
1932 TemplateURL t_url(data);
1933 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1934 generate_url_cases[i].expected)
1935 << generate_url_cases[i].test_name << " failed.";
1936 }
1937}
[email protected]9e9130ce2014-06-23 23:20:511938
1939TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1940 TemplateURLData data;
1941 search_terms_data_.set_google_base_url("http://bar/");
1942 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1943
1944 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241945 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
[email protected]9e9130ce2014-06-23 23:20:511946 search_terms_args.prefetch_query = "full query text";
1947 search_terms_args.prefetch_query_type = "2338";
1948 std::string result =
1949 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1950 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1951 result);
1952
1953 TemplateURL url2(data);
1954 search_terms_args.prefetch_query.clear();
1955 search_terms_args.prefetch_query_type.clear();
1956 result =
1957 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1958 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1959}
vitbar5bd8c252016-01-29 18:44:511960
1961// Tests that TemplateURL works correctly after changing the Google base URL
1962// and invalidating cached values.
1963TEST_F(TemplateURLTest, InvalidateCachedValues) {
1964 TemplateURLData data;
1965 data.SetURL("{google:baseURL}search?q={searchTerms}");
1966 data.suggestions_url = "{google:baseSuggestURL}search?q={searchTerms}";
vitbar5bd8c252016-01-29 18:44:511967 data.image_url = "{google:baseURL}searchbyimage/upload";
1968 data.new_tab_url = "{google:baseURL}_/chrome/newtab";
1969 data.contextual_search_url = "{google:baseURL}_/contextualsearch";
1970 data.alternate_urls.push_back("{google:baseURL}s#q={searchTerms}");
1971 TemplateURL url(data);
Jan Wilken Dörrie756999e2021-03-23 15:05:241972 TemplateURLRef::SearchTermsArgs search_terms_args(u"X");
Jan Wilken Dörriefa241ba2021-03-11 17:57:011973 std::u16string search_terms;
vitbar5bd8c252016-01-29 18:44:511974
1975 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1976 EXPECT_EQ("http://www.google.com/search?q=X",
1977 url.url_ref().ReplaceSearchTerms(search_terms_args,
1978 search_terms_data_));
1979 EXPECT_EQ("http://www.google.com/s#q=X",
1980 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1981 search_terms_data_));
1982 EXPECT_EQ("http://www.google.com/search?q=X",
1983 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1984 search_terms_data_));
1985 EXPECT_EQ("http://www.google.com/complete/search?q=X",
1986 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1987 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:511988 EXPECT_EQ("http://www.google.com/searchbyimage/upload",
1989 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1990 search_terms_data_));
1991 EXPECT_EQ("http://www.google.com/_/chrome/newtab",
1992 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1993 search_terms_data_));
1994 EXPECT_EQ("http://www.google.com/_/contextualsearch",
1995 url.contextual_search_url_ref().ReplaceSearchTerms(
1996 search_terms_args, search_terms_data_));
1997
1998 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1999 GURL("http://www.google.com/search?q=Y+Z"),
2000 search_terms_data_, &search_terms));
Jan Wilken Dörrie756999e2021-03-23 15:05:242001 EXPECT_EQ(u"Y Z", search_terms);
vitbar5bd8c252016-01-29 18:44:512002 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
2003 GURL("http://www.google.com/s#q=123"),
2004 search_terms_data_, &search_terms));
Jan Wilken Dörrie756999e2021-03-23 15:05:242005 EXPECT_EQ(u"123", search_terms);
vitbar5bd8c252016-01-29 18:44:512006
2007 search_terms_data_.set_google_base_url("https://www.foo.org/");
2008 url.InvalidateCachedValues();
2009
2010 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
2011 EXPECT_EQ("https://www.foo.org/search?q=X",
2012 url.url_ref().ReplaceSearchTerms(search_terms_args,
2013 search_terms_data_));
2014 EXPECT_EQ("https://www.foo.org/s#q=X",
2015 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
2016 search_terms_data_));
2017 EXPECT_EQ("https://www.foo.org/search?q=X",
2018 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
2019 search_terms_data_));
2020 EXPECT_EQ("https://www.foo.org/complete/search?q=X",
2021 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
2022 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:512023 EXPECT_EQ("https://www.foo.org/searchbyimage/upload",
2024 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
2025 search_terms_data_));
2026 EXPECT_EQ("https://www.foo.org/_/chrome/newtab",
2027 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
2028 search_terms_data_));
2029 EXPECT_EQ("https://www.foo.org/_/contextualsearch",
2030 url.contextual_search_url_ref().ReplaceSearchTerms(
2031 search_terms_args, search_terms_data_));
2032
2033 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
2034 GURL("https://www.foo.org/search?q=Y+Z"),
2035 search_terms_data_, &search_terms));
Jan Wilken Dörrie756999e2021-03-23 15:05:242036 EXPECT_EQ(u"Y Z", search_terms);
vitbar5bd8c252016-01-29 18:44:512037 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
2038 GURL("https://www.foo.org/s#q=123"),
2039 search_terms_data_, &search_terms));
Jan Wilken Dörrie756999e2021-03-23 15:05:242040 EXPECT_EQ(u"123", search_terms);
vitbar5bd8c252016-01-29 18:44:512041
2042 search_terms_data_.set_google_base_url("http://www.google.com/");
2043}
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002044
2045// Tests the use of wildcards in the path to ensure both extracting search terms
2046// and generating a search URL work correctly.
2047TEST_F(TemplateURLTest, PathWildcard) {
2048 TemplateURLData data;
2049 data.SetURL(
2050 "https://www.google.com/search{google:pathWildcard}?q={searchTerms}");
2051 TemplateURL url(data);
2052
2053 // Test extracting search terms from a URL.
Jan Wilken Dörriefa241ba2021-03-11 17:57:012054 std::u16string search_terms;
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002055 url.ExtractSearchTermsFromURL(GURL("https://www.google.com/search?q=testing"),
2056 search_terms_data_, &search_terms);
Jan Wilken Dörrie756999e2021-03-23 15:05:242057 EXPECT_EQ(u"testing", search_terms);
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002058 url.ExtractSearchTermsFromURL(
2059 GURL("https://www.google.com/search;_this_is_a_test;_?q=testing"),
2060 search_terms_data_, &search_terms);
Jan Wilken Dörrie756999e2021-03-23 15:05:242061 EXPECT_EQ(u"testing", search_terms);
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002062
2063 // Tests overlapping prefix/suffix.
2064 data.SetURL(
2065 "https://www.google.com/search{google:pathWildcard}rch?q={searchTerms}");
2066 TemplateURL overlap_url(data);
2067 overlap_url.ExtractSearchTermsFromURL(
2068 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
2069 &search_terms);
2070 EXPECT_TRUE(search_terms.empty());
2071
2072 // Tests wildcard at beginning of path so we only have a suffix.
2073 data.SetURL(
2074 "https://www.google.com/{google:pathWildcard}rch?q={searchTerms}");
2075 TemplateURL suffix_url(data);
2076 suffix_url.ExtractSearchTermsFromURL(
2077 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
2078 &search_terms);
Jan Wilken Dörrie756999e2021-03-23 15:05:242079 EXPECT_EQ(u"testing", search_terms);
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002080
2081 // Tests wildcard between prefix/suffix.
2082 overlap_url.ExtractSearchTermsFromURL(
2083 GURL("https://www.google.com/search_testing_rch?q=testing"),
2084 search_terms_data_, &search_terms);
Jan Wilken Dörrie756999e2021-03-23 15:05:242085 EXPECT_EQ(u"testing", search_terms);
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002086
2087 // Test generating a URL.
Jan Wilken Dörrie756999e2021-03-23 15:05:242088 TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002089 GURL generated_url;
2090 url.ReplaceSearchTermsInURL(url.GenerateSearchURL(search_terms_data_),
2091 search_terms_args, search_terms_data_,
2092 &generated_url);
2093 EXPECT_EQ("https://www.google.com/search?q=foo", generated_url.spec());
2094}