blob: d44e737b5d4bd16b37e6d771ae0e8f41f7b93461 [file] [log] [blame]
[email protected]63f021882014-07-11 03:14:161// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]d82443b2009-01-15 19:54:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avif57136c12015-12-25 23:27:455#include <stddef.h>
6
Robbie Gibson8532eaa2019-03-15 11:43:247#include "base/base64.h"
[email protected]12bd05872009-03-17 19:25:068#include "base/base_paths.h"
[email protected]56fa29592013-07-02 20:25:539#include "base/command_line.h"
a-v-y81695d0d2017-04-20 08:22:2210#include "base/i18n/case_conversion.h"
Avi Drissman2244c2d2018-12-25 23:08:0211#include "base/stl_util.h"
[email protected]2f3bc6512013-08-28 03:56:2712#include "base/strings/string_number_conversions.h"
[email protected]24a555b62013-06-10 22:01:1713#include "base/strings/string_util.h"
[email protected]e309f312013-06-07 21:50:0814#include "base/strings/utf_string_conversions.h"
John Abd-El-Malek9cf3d7f02018-07-27 02:40:3915#include "components/google/core/common/google_util.h"
[email protected]5c3c6e482014-06-23 09:47:1816#include "components/search_engines/search_engines_switches.h"
[email protected]798baa8e2014-06-20 05:42:4417#include "components/search_engines/search_terms_data.h"
[email protected]d550cb02014-06-25 06:48:1118#include "components/search_engines/template_url.h"
hashimoto3c831812014-08-25 07:40:2319#include "components/search_engines/testing_search_terms_data.h"
a-v-y02d3f50b2017-02-16 12:03:5520#include "net/base/url_util.h"
[email protected]d82443b2009-01-15 19:54:5621#include "testing/gtest/include/gtest/gtest.h"
Steven Holtef9d5ed62017-10-21 02:02:3022#include "third_party/metrics_proto/omnibox_event.pb.h"
23#include "third_party/metrics_proto/omnibox_input_type.pb.h"
[email protected]d82443b2009-01-15 19:54:5624
[email protected]f911df52013-12-24 23:24:2325using base::ASCIIToUTF16;
26
a-v-y81695d0d2017-04-20 08:22:2227namespace {
Jan Wilken Dörriefa241ba2021-03-11 17:57:0128bool IsLowerCase(const std::u16string& str) {
a-v-y81695d0d2017-04-20 08:22:2229 return str == base::i18n::ToLower(str);
30}
31}
32
[email protected]583844c2011-08-27 00:38:3533class TemplateURLTest : public testing::Test {
[email protected]d82443b2009-01-15 19:54:5634 public:
[email protected]798baa8e2014-06-20 05:42:4435 TemplateURLTest() : search_terms_data_("http://www.google.com/") {}
[email protected]b37bdfe2012-03-16 20:53:2736 void CheckSuggestBaseURL(const std::string& base_url,
37 const std::string& base_suggest_url) const;
[email protected]ce7ee5f2014-06-16 23:41:1938
vitbardeb285392015-02-20 14:02:5539 static void ExpectPostParamIs(
40 const TemplateURLRef::PostParam& param,
41 const std::string& name,
42 const std::string& value,
43 const std::string& content_type = std::string());
44
hashimoto3c831812014-08-25 07:40:2345 TestingSearchTermsData search_terms_data_;
[email protected]d82443b2009-01-15 19:54:5646};
47
[email protected]b37bdfe2012-03-16 20:53:2748void TemplateURLTest::CheckSuggestBaseURL(
49 const std::string& base_url,
50 const std::string& base_suggest_url) const {
hashimoto3c831812014-08-25 07:40:2351 TestingSearchTermsData search_terms_data(base_url);
[email protected]b37bdfe2012-03-16 20:53:2752 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue());
53}
54
vitbardeb285392015-02-20 14:02:5555// static
56void TemplateURLTest::ExpectPostParamIs(const TemplateURLRef::PostParam& param,
57 const std::string& name,
58 const std::string& value,
59 const std::string& content_type) {
60 EXPECT_EQ(name, param.name);
61 EXPECT_EQ(value, param.value);
62 EXPECT_EQ(content_type, param.content_type);
63}
64
[email protected]d82443b2009-01-15 19:54:5665TEST_F(TemplateURLTest, Defaults) {
[email protected]573889f22012-04-07 01:31:5466 TemplateURLData data;
[email protected]573889f22012-04-07 01:31:5467 EXPECT_FALSE(data.safe_for_autoreplace);
68 EXPECT_EQ(0, data.prepopulate_id);
[email protected]d82443b2009-01-15 19:54:5669}
70
71TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]573889f22012-04-07 01:31:5472 TemplateURLData data;
73 data.SetURL("{searchTerms}");
[email protected]168d08722014-06-18 07:13:2874 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1975 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:5676}
77
78TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1979 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1980 const char* url;
Jan Wilken Dörriefa241ba2021-03-11 17:57:0181 const std::u16string terms;
[email protected]34e24852012-01-31 18:43:5882 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:1983 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:3084 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1685 "http://foosea%20rch/bar" },
[email protected]400b133f2011-01-19 18:32:3086 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1687 "http://foosea%20rch/bar?boo=abc" },
[email protected]400b133f2011-01-19 18:32:3088 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1689 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]400b133f2011-01-19 18:32:3090 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
[email protected]c31a979c2012-05-31 23:57:1691 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:1992 };
Avi Drissman2244c2d2018-12-25 23:08:0293 for (size_t i = 0; i < base::size(search_term_cases); ++i) {
[email protected]0d2e6a62010-01-15 20:09:1994 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:5495 TemplateURLData data;
96 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:2897 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1998 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
99 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04100 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19101 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data_));
[email protected]c31a979c2012-05-31 23:57:16102 ASSERT_TRUE(result.is_valid());
103 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:19104 }
[email protected]d82443b2009-01-15 19:54:56105}
106
107TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:54108 TemplateURLData data;
109 data.SetURL("http://foo{searchTerms}{count?}");
[email protected]168d08722014-06-18 07:13:28110 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19111 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
112 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04113 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19114 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56115 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27116 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:56117}
118
119TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:54120 TemplateURLData data;
121 data.SetURL("http://foo{searchTerms}{count}");
[email protected]168d08722014-06-18 07:13:28122 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19123 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
124 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04125 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19126 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56127 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27128 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56129}
130
131TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54132 TemplateURLData data;
133 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]168d08722014-06-18 07:13:28134 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19135 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
136 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04137 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19138 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56139 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27140 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56141}
142
143TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54144 TemplateURLData data;
145 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[email protected]168d08722014-06-18 07:13:28146 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19147 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
148 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04149 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19150 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56151 ASSERT_TRUE(result.is_valid());
[email protected]405aae22012-03-29 20:36:13152 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56153}
154
155TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54156 TemplateURLData data;
157 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[email protected]168d08722014-06-18 07:13:28158 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19159 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
160 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04161 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19162 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56163 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27164 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56165}
166
[email protected]93b29062013-07-12 03:09:09167TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
168 const char kInvalidPostParamsString[] =
169 "unknown_template={UnknownTemplate},bad_value=bad{value},"
170 "{google:sbiSource}";
171 // List all accpectable parameter format in valid_post_params_string. it is
172 // expected like: "name0=,name1=value1,name2={template1}"
173 const char kValidPostParamsString[] =
174 "image_content={google:imageThumbnail},image_url={google:imageURL},"
175 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
Robbie Gibson8532eaa2019-03-15 11:43:24176 "constant_param=constant,width={google:imageOriginalWidth},"
177 "base64_image_content={google:imageThumbnailBase64}";
[email protected]93b29062013-07-12 03:09:09178 const char KImageSearchURL[] = "http://foo.com/sbi";
179
180 TemplateURLData data;
181 data.image_url = KImageSearchURL;
182
183 // Try to parse invalid post parameters.
184 data.image_url_post_params = kInvalidPostParamsString;
[email protected]168d08722014-06-18 07:13:28185 TemplateURL url_bad(data);
[email protected]ce7ee5f2014-06-16 23:41:19186 ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09187 const TemplateURLRef::PostParams& bad_post_params =
188 url_bad.image_url_ref().post_params_;
189 ASSERT_EQ(2U, bad_post_params.size());
vitbardeb285392015-02-20 14:02:55190 ExpectPostParamIs(bad_post_params[0], "unknown_template",
191 "{UnknownTemplate}");
192 ExpectPostParamIs(bad_post_params[1], "bad_value", "bad{value}");
[email protected]93b29062013-07-12 03:09:09193
194 // Try to parse valid post parameters.
195 data.image_url_post_params = kValidPostParamsString;
[email protected]168d08722014-06-18 07:13:28196 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19197 ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_));
198 ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09199
200 // Check term replacement.
201 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
202 search_args.image_thumbnail_content = "dummy-image-thumbnail";
203 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
[email protected]2f3bc6512013-08-28 03:56:27204 search_args.image_original_size = gfx::Size(10, 10);
[email protected]93b29062013-07-12 03:09:09205 // Replacement operation with no post_data buffer should still return
206 // the parsed URL.
hashimoto3c831812014-08-25 07:40:23207 TestingSearchTermsData search_terms_data("http://X");
[email protected]ce7ee5f2014-06-16 23:41:19208 GURL result(url.image_url_ref().ReplaceSearchTerms(
209 search_args, search_terms_data));
[email protected]93b29062013-07-12 03:09:09210 ASSERT_TRUE(result.is_valid());
211 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48212 TemplateURLRef::PostContent post_content;
[email protected]ce7ee5f2014-06-16 23:41:19213 result = GURL(url.image_url_ref().ReplaceSearchTerms(
[email protected]7c2bcc42013-08-01 04:03:48214 search_args, search_terms_data, &post_content));
[email protected]93b29062013-07-12 03:09:09215 ASSERT_TRUE(result.is_valid());
216 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48217 ASSERT_FALSE(post_content.first.empty());
218 ASSERT_FALSE(post_content.second.empty());
[email protected]93b29062013-07-12 03:09:09219
220 // Check parsed result of post parameters.
221 const TemplateURLRef::Replacements& replacements =
222 url.image_url_ref().replacements_;
223 const TemplateURLRef::PostParams& post_params =
224 url.image_url_ref().post_params_;
Robbie Gibson8532eaa2019-03-15 11:43:24225 EXPECT_EQ(8U, post_params.size());
jdoerrie3feb1852018-10-05 12:16:44226 for (auto i = post_params.begin(); i != post_params.end(); ++i) {
227 auto j = replacements.begin();
[email protected]93b29062013-07-12 03:09:09228 for (; j != replacements.end(); ++j) {
229 if (j->is_post_param && j->index ==
230 static_cast<size_t>(i - post_params.begin())) {
231 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27232 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
Raul Tambref88e5102019-02-06 10:54:03233 ExpectPostParamIs(
234 *i, "width",
235 base::NumberToString(search_args.image_original_size.width()));
[email protected]2f3bc6512013-08-28 03:56:27236 break;
[email protected]1020fead2014-06-20 13:40:28237 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE:
vitbardeb285392015-02-20 14:02:55238 ExpectPostParamIs(*i, "sbisrc",
239 search_terms_data.GoogleImageSearchSource());
[email protected]1020fead2014-06-20 13:40:28240 break;
[email protected]93b29062013-07-12 03:09:09241 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
vitbardeb285392015-02-20 14:02:55242 ExpectPostParamIs(*i, "image_content",
243 search_args.image_thumbnail_content,
244 "image/jpeg");
[email protected]93b29062013-07-12 03:09:09245 break;
Robbie Gibson8532eaa2019-03-15 11:43:24246 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL_BASE64: {
247 std::string base64_image_content;
248 base::Base64Encode(search_args.image_thumbnail_content,
249 &base64_image_content);
250 ExpectPostParamIs(*i, "base64_image_content", base64_image_content,
251 "image/jpeg");
252 break;
253 }
[email protected]93b29062013-07-12 03:09:09254 case TemplateURLRef::GOOGLE_IMAGE_URL:
vitbardeb285392015-02-20 14:02:55255 ExpectPostParamIs(*i, "image_url", search_args.image_url.spec());
[email protected]93b29062013-07-12 03:09:09256 break;
257 case TemplateURLRef::LANGUAGE:
vitbardeb285392015-02-20 14:02:55258 ExpectPostParamIs(*i, "language", "en");
[email protected]93b29062013-07-12 03:09:09259 break;
260 default:
261 ADD_FAILURE(); // Should never go here.
262 }
263 break;
264 }
265 }
266 if (j != replacements.end())
267 continue;
vitbardeb285392015-02-20 14:02:55268 if (i->name == "empty_param")
269 ExpectPostParamIs(*i, "empty_param", std::string());
270 else
271 ExpectPostParamIs(*i, "constant_param", "constant");
[email protected]93b29062013-07-12 03:09:09272 }
273}
274
Peter Kastinge59f0c32017-11-29 05:56:22275TEST_F(TemplateURLTest, ImageURLWithGetShouldNotCrash) {
276 TemplateURLData data;
277 data.SetURL("http://foo/?q={searchTerms}&t={google:imageThumbnail}");
278 TemplateURL url(data);
279 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
280 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
281
282 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
283 search_args.image_thumbnail_content = "dummy-image-thumbnail";
284 GURL result(
285 url.url_ref().ReplaceSearchTerms(search_args, search_terms_data_));
286 ASSERT_TRUE(result.is_valid());
287 EXPECT_EQ("http://foo/?q=X&t=dummy-image-thumbnail", result.spec());
288}
289
[email protected]d88cb202011-08-17 20:03:01290// Test that setting the prepopulate ID from TemplateURL causes the stored
291// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44292TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54293 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20294 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]168d08722014-06-18 07:13:28295 TemplateURL url(data);
[email protected]1a257262011-06-28 22:15:44296 TemplateURLRef::Replacements replacements;
297 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:24298 EXPECT_EQ("http://foo{fhqwhgads}bar",
299 url.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements,
300 nullptr, &valid));
[email protected]1a257262011-06-28 22:15:44301 EXPECT_TRUE(replacements.empty());
302 EXPECT_TRUE(valid);
303
[email protected]573889f22012-04-07 01:31:54304 data.prepopulate_id = 123;
[email protected]168d08722014-06-18 07:13:28305 TemplateURL url2(data);
Ivan Kotenkov75b1c3a2017-10-24 14:47:24306 EXPECT_EQ("http://foobar",
307 url2.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements,
308 nullptr, &valid));
[email protected]1a257262011-06-28 22:15:44309 EXPECT_TRUE(replacements.empty());
310 EXPECT_TRUE(valid);
311}
312
vitbar90902c42016-05-11 06:16:55313// Test that setting the prepopulate ID from TemplateURL causes the stored
314// TemplateURLRef to handle parsing the URL parameters differently.
315TEST_F(TemplateURLTest, SetPrepopulatedAndReplace) {
316 TemplateURLData data;
317 data.SetURL("http://foo{fhqwhgads}search/?q={searchTerms}");
318 data.suggestions_url = "http://foo{fhqwhgads}suggest/?q={searchTerms}";
vitbar90902c42016-05-11 06:16:55319 data.image_url = "http://foo{fhqwhgads}image/";
320 data.new_tab_url = "http://foo{fhqwhgads}newtab/";
321 data.contextual_search_url = "http://foo{fhqwhgads}context/";
322 data.alternate_urls.push_back(
323 "http://foo{fhqwhgads}alternate/?q={searchTerms}");
324
325 TemplateURLRef::SearchTermsArgs args(base::ASCIIToUTF16("X"));
326 const SearchTermsData& stdata = search_terms_data_;
327
328 TemplateURL url(data);
329 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
330 url.url_ref().ReplaceSearchTerms(args, stdata));
331 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dalternate/?q=X",
332 url.url_refs()[0].ReplaceSearchTerms(args, stdata));
333 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
334 url.url_refs()[1].ReplaceSearchTerms(args, stdata));
335 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsuggest/?q=X",
336 url.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
vitbar90902c42016-05-11 06:16:55337 EXPECT_EQ("http://foo{fhqwhgads}image/",
338 url.image_url_ref().ReplaceSearchTerms(args, stdata));
339 EXPECT_EQ("http://foo{fhqwhgads}newtab/",
340 url.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
341 EXPECT_EQ("http://foo{fhqwhgads}context/",
342 url.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
343
344 data.prepopulate_id = 123;
345 TemplateURL url2(data);
346 EXPECT_EQ("http://foosearch/?q=X",
347 url2.url_ref().ReplaceSearchTerms(args, stdata));
348 EXPECT_EQ("http://fooalternate/?q=X",
349 url2.url_refs()[0].ReplaceSearchTerms(args, stdata));
350 EXPECT_EQ("http://foosearch/?q=X",
351 url2.url_refs()[1].ReplaceSearchTerms(args, stdata));
352 EXPECT_EQ("http://foosuggest/?q=X",
353 url2.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
vitbar90902c42016-05-11 06:16:55354 EXPECT_EQ("http://fooimage/",
355 url2.image_url_ref().ReplaceSearchTerms(args, stdata));
356 EXPECT_EQ("http://foonewtab/",
357 url2.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
358 EXPECT_EQ("http://foocontext/",
359 url2.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
360}
361
[email protected]d82443b2009-01-15 19:54:56362TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54363 TemplateURLData data;
364 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]168d08722014-06-18 07:13:28365 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19366 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
367 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04368 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19369 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56370 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27371 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56372}
373
374TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54375 TemplateURLData data;
376 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]168d08722014-06-18 07:13:28377 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19378 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
379 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04380 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19381 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56382 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27383 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56384}
385
[email protected]375bd7312010-08-30 22:18:13386TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
387 struct SearchTermsCase {
388 const char* url;
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örrie414bbf52021-03-19 19:06:26417 {"hello+world", ASCIIToUTF16("hello world")},
418 // 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 +.
427 {"C%2B%2B", ASCIIToUTF16("C++")},
428 // C%2B is escaped as C%252B, make sure we unescape it properly.
429 {"C%252B", ASCIIToUTF16("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[] = {
[email protected]ddd231e2010-06-29 20:35:19451 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30452 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19453 { "http://X",
[email protected]400b133f2011-01-19 18:32:30454 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19455 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30456 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19457 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30458 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56459 };
[email protected]573889f22012-04-07 01:31:54460 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02461 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54462 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28463 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19464 EXPECT_EQ(test_data[i].expected_result,
465 url.url_ref().DisplayURL(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27466 EXPECT_EQ(test_data[i].url,
[email protected]ce7ee5f2014-06-16 23:41:19467 TemplateURLRef::DisplayURLToURLRef(
468 url.url_ref().DisplayURL(search_terms_data_)));
[email protected]d82443b2009-01-15 19:54:56469 }
470}
471
472TEST_F(TemplateURLTest, ReplaceSearchTerms) {
473 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19474 const std::string url;
[email protected]d82443b2009-01-15 19:54:56475 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27476 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19477 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56478 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19479 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56480 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19481 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56482 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19483 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56484 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19485 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56486 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19487 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56488 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19489 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56490 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19491 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56492 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19493 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56494 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19495 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56496 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19497 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56498 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19499 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56500 "http://foo/UTF-8a{language}aXa" },
501 };
[email protected]573889f22012-04-07 01:31:54502 TemplateURLData data;
503 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02504 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54505 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28506 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19507 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
508 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27509 std::string expected_result = test_data[i].expected_result;
brettwe6dae462015-06-24 20:54:45510 base::ReplaceSubstringsAfterOffset(
511 &expected_result, 0, "{language}",
512 search_terms_data_.GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04513 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19514 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
515 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27516 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56517 EXPECT_EQ(expected_result, result.spec());
518 }
519}
520
521
522// Tests replacing search terms in various encodings and making sure the
523// generated URL matches the expected value.
524TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
525 struct TestData {
526 const std::string encoding;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01527 const std::u16string search_term;
[email protected]ddd231e2010-06-29 20:35:19528 const std::string url;
[email protected]d82443b2009-01-15 19:54:56529 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27530 } test_data[] = {
Jan Wilken Dörrieeb96aa82021-03-19 19:16:33531 {"BIG5", u"\x60BD", "http://foo/?{searchTerms}{inputEncoding}",
532 "http://foo/?%B1~BIG5"},
Alexander Yashkin874b919d2018-02-17 15:11:18533 {"UTF-8", ASCIIToUTF16("blah"),
534 "http://foo/?{searchTerms}{inputEncoding}", "http://foo/?blahUTF-8"},
535 {"Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
536 "http://foo/{searchTerms}/bar", "http://foo/%82%A0/bar"},
537 {"Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
538 "http://foo/{searchTerms}/bar", "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56539 };
[email protected]573889f22012-04-07 01:31:54540 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02541 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54542 data.SetURL(test_data[i].url);
543 data.input_encodings.clear();
544 data.input_encodings.push_back(test_data[i].encoding);
[email protected]168d08722014-06-18 07:13:28545 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19546 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
547 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04548 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19549 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
550 search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04551 ASSERT_TRUE(result.is_valid());
552 EXPECT_EQ(test_data[i].expected_result, result.spec());
553 }
554}
555
Alexander Yashkin874b919d2018-02-17 15:11:18556// Test that encoding with several optional codepages works as intended.
557// Codepages are tried in order, fallback is UTF-8.
558TEST_F(TemplateURLTest, ReplaceSearchTermsMultipleEncodings) {
559 struct TestData {
560 const std::vector<std::string> encodings;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01561 const std::u16string search_term;
Alexander Yashkin874b919d2018-02-17 15:11:18562 const std::string url;
563 const std::string expected_result;
564 } test_data[] = {
565 // First and third encodings are valid. First is used.
566 {{"windows-1251", "cp-866", "UTF-8"},
567 base::UTF8ToUTF16("\xD1\x8F"),
568 "http://foo/?{searchTerms}{inputEncoding}",
569 "http://foo/?%FFwindows-1251"},
570 // Second and third encodings are valid, second is used.
571 {{"cp-866", "GB2312", "UTF-8"},
572 base::UTF8ToUTF16("\xE7\x8B\x97"),
573 "http://foo/?{searchTerms}{inputEncoding}",
574 "http://foo/?%B9%B7GB2312"},
575 // Second and third encodings are valid in another order, second is used.
576 {{"cp-866", "UTF-8", "GB2312"},
577 base::UTF8ToUTF16("\xE7\x8B\x97"),
578 "http://foo/?{searchTerms}{inputEncoding}",
579 "http://foo/?%E7%8B%97UTF-8"},
580 // Both encodings are invalid, fallback to UTF-8.
581 {{"cp-866", "windows-1251"},
582 base::UTF8ToUTF16("\xE7\x8B\x97"),
583 "http://foo/?{searchTerms}{inputEncoding}",
584 "http://foo/?%E7%8B%97UTF-8"},
585 // No encodings are given, fallback to UTF-8.
586 {{},
587 base::UTF8ToUTF16("\xE7\x8B\x97"),
588 "http://foo/?{searchTerms}{inputEncoding}",
589 "http://foo/?%E7%8B%97UTF-8"},
590 };
591
592 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02593 for (size_t i = 0; i < base::size(test_data); ++i) {
Alexander Yashkin874b919d2018-02-17 15:11:18594 data.SetURL(test_data[i].url);
595 data.input_encodings = test_data[i].encodings;
596 TemplateURL url(data);
597 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
598 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
599 GURL result(url.url_ref().ReplaceSearchTerms(
600 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
601 search_terms_data_));
602 ASSERT_TRUE(result.is_valid());
603 EXPECT_EQ(test_data[i].expected_result, result.spec());
604 }
605}
606
[email protected]bca359b2012-06-24 07:53:04607// Tests replacing assisted query stats (AQS) in various scenarios.
608TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
609 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01610 const std::u16string search_term;
[email protected]bca359b2012-06-24 07:53:04611 const std::string aqs;
612 const std::string base_url;
613 const std::string url;
614 const std::string expected_result;
615 } test_data[] = {
616 // No HTTPS, no AQS.
617 { ASCIIToUTF16("foo"),
618 "chrome.0.0l6",
619 "http://foo/",
620 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
621 "http://foo/?foo" },
622 // HTTPS available, AQS should be replaced.
623 { ASCIIToUTF16("foo"),
624 "chrome.0.0l6",
625 "https://foo/",
626 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
627 "https://foo/?fooaqs=chrome.0.0l6&" },
628 // HTTPS available, however AQS is empty.
629 { ASCIIToUTF16("foo"),
630 "",
631 "https://foo/",
632 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
633 "https://foo/?foo" },
634 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
635 { ASCIIToUTF16("foo"),
636 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44637 "http://www.google.com",
[email protected]bca359b2012-06-24 07:53:04638 "http://foo?{searchTerms}{google:assistedQueryStats}",
639 "http://foo/?foo" },
640 // A non-Google search provider with HTTPS should allow AQS.
641 { ASCIIToUTF16("foo"),
642 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44643 "https://www.google.com",
[email protected]bca359b2012-06-24 07:53:04644 "https://foo?{searchTerms}{google:assistedQueryStats}",
645 "https://foo/?fooaqs=chrome.0.0l6&" },
646 };
647 TemplateURLData data;
648 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02649 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04650 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28651 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19652 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
653 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04654 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
655 search_terms_args.assisted_query_stats = test_data[i].aqs;
[email protected]798baa8e2014-06-20 05:42:44656 search_terms_data_.set_google_base_url(test_data[i].base_url);
[email protected]ce7ee5f2014-06-16 23:41:19657 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
658 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27659 ASSERT_TRUE(result.is_valid());
660 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56661 }
662}
663
[email protected]00790562012-12-14 09:57:16664// Tests replacing cursor position.
665TEST_F(TemplateURLTest, ReplaceCursorPosition) {
666 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01667 const std::u16string search_term;
[email protected]00790562012-12-14 09:57:16668 size_t cursor_position;
669 const std::string url;
670 const std::string expected_result;
671 } test_data[] = {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01672 {ASCIIToUTF16("foo"), std::u16string::npos,
673 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
674 "http://www.google.com/?foo&"},
675 {ASCIIToUTF16("foo"), 2,
676 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
677 "http://www.google.com/?foo&cp=2&"},
678 {ASCIIToUTF16("foo"), 15,
679 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
680 "http://www.google.com/?foo&cp=15&"},
[email protected]00790562012-12-14 09:57:16681 };
682 TemplateURLData data;
683 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02684 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]00790562012-12-14 09:57:16685 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28686 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19687 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
688 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]00790562012-12-14 09:57:16689 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
690 search_terms_args.cursor_position = test_data[i].cursor_position;
[email protected]ce7ee5f2014-06-16 23:41:19691 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
692 search_terms_data_));
[email protected]00790562012-12-14 09:57:16693 ASSERT_TRUE(result.is_valid());
694 EXPECT_EQ(test_data[i].expected_result, result.spec());
695 }
696}
697
[email protected]420472b22014-06-10 13:34:43698// Tests replacing input type (&oit=).
699TEST_F(TemplateURLTest, ReplaceInputType) {
700 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01701 const std::u16string search_term;
Steven Holte3696c9412017-08-24 18:38:03702 metrics::OmniboxInputType input_type;
[email protected]420472b22014-06-10 13:34:43703 const std::string url;
704 const std::string expected_result;
705 } test_data[] = {
Steven Holte3696c9412017-08-24 18:38:03706 {ASCIIToUTF16("foo"), metrics::OmniboxInputType::UNKNOWN,
707 "{google:baseURL}?{searchTerms}&{google:inputType}",
708 "http://www.google.com/?foo&oit=1&"},
709 {ASCIIToUTF16("foo"), metrics::OmniboxInputType::URL,
710 "{google:baseURL}?{searchTerms}&{google:inputType}",
711 "http://www.google.com/?foo&oit=3&"},
[email protected]420472b22014-06-10 13:34:43712 };
[email protected]420472b22014-06-10 13:34:43713 TemplateURLData data;
714 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02715 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]420472b22014-06-10 13:34:43716 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28717 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19718 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
719 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43720 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
721 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19722 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
723 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43724 ASSERT_TRUE(result.is_valid());
725 EXPECT_EQ(test_data[i].expected_result, result.spec());
726 }
727}
728
Tommy C. Li9a180482019-06-27 20:49:00729// Tests replacing omnibox focus type (&oft=).
730TEST_F(TemplateURLTest, ReplaceOmniboxFocusType) {
731 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01732 const std::u16string search_term;
Tommy Li5e883512020-08-07 19:32:04733 OmniboxFocusType focus_type;
Tommy C. Li9a180482019-06-27 20:49:00734 const std::string url;
735 const std::string expected_result;
736 } test_data[] = {
Tommy Li5e883512020-08-07 19:32:04737 {ASCIIToUTF16("foo"), OmniboxFocusType::DEFAULT,
Tommy C. Li9a180482019-06-27 20:49:00738 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
739 "http://www.google.com/?foo&"},
Tommy Li5e883512020-08-07 19:32:04740 {ASCIIToUTF16("foo"), OmniboxFocusType::ON_FOCUS,
Tommy C. Li9a180482019-06-27 20:49:00741 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
742 "http://www.google.com/?foo&oft=1&"},
Tommy Li5e883512020-08-07 19:32:04743 {ASCIIToUTF16("foo"), OmniboxFocusType::DELETED_PERMANENT_TEXT,
744 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
745 "http://www.google.com/?foo&oft=2&"},
Tommy C. Li9a180482019-06-27 20:49:00746 };
747 TemplateURLData data;
748 data.input_encodings.push_back("UTF-8");
749 for (size_t i = 0; i < base::size(test_data); ++i) {
750 data.SetURL(test_data[i].url);
751 TemplateURL url(data);
752 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
753 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
754 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
Tommy Li5e883512020-08-07 19:32:04755 search_terms_args.focus_type = test_data[i].focus_type;
Tommy C. Li9a180482019-06-27 20:49:00756 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
757 search_terms_data_));
758 ASSERT_TRUE(result.is_valid());
759 EXPECT_EQ(test_data[i].expected_result, result.spec());
760 }
761}
762
Ryan Sturm29853d52020-11-17 02:14:04763// Tests replacing prefetch source (&pf=).
764TEST_F(TemplateURLTest, ReplaceIsPrefetch) {
765 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01766 const std::u16string search_term;
Ryan Sturm29853d52020-11-17 02:14:04767 bool is_prefetch;
768 const std::string url;
769 const std::string expected_result;
770 } test_data[] = {
771 {ASCIIToUTF16("foo"), false,
772 "{google:baseURL}?{searchTerms}&{google:prefetchSource}",
773 "http://www.google.com/?foo&"},
774 {ASCIIToUTF16("foo"), true,
775 "{google:baseURL}?{searchTerms}&{google:prefetchSource}",
776 "http://www.google.com/?foo&pf=cs&"},
777 };
778 TemplateURLData data;
779 data.input_encodings.push_back("UTF-8");
780 for (size_t i = 0; i < base::size(test_data); ++i) {
781 data.SetURL(test_data[i].url);
782 TemplateURL url(data);
783 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
784 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
785 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
786 search_terms_args.is_prefetch = test_data[i].is_prefetch;
787 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
788 search_terms_data_));
789 ASSERT_TRUE(result.is_valid());
790 EXPECT_EQ(test_data[i].expected_result, result.spec());
791 }
792}
793
[email protected]9b9fa672013-11-07 06:04:52794// Tests replacing currentPageUrl.
795TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48796 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01797 const std::u16string search_term;
[email protected]9b9fa672013-11-07 06:04:52798 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48799 const std::string url;
800 const std::string expected_result;
801 } test_data[] = {
802 { ASCIIToUTF16("foo"),
803 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52804 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48805 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
806 { ASCIIToUTF16("foo"),
807 "",
[email protected]9b9fa672013-11-07 06:04:52808 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48809 "http://www.google.com/?foo&" },
810 { ASCIIToUTF16("foo"),
811 "http://g.com/+-/*&=",
[email protected]9b9fa672013-11-07 06:04:52812 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48813 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
814 };
815 TemplateURLData data;
816 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02817 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]800569d2013-05-06 07:38:48818 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28819 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19820 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
821 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]800569d2013-05-06 07:38:48822 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52823 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19824 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
825 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48826 ASSERT_TRUE(result.is_valid());
827 EXPECT_EQ(test_data[i].expected_result, result.spec());
828 }
829}
830
Pavel Yatsuk0d6a7b5c2019-10-09 20:33:44831// Tests appending attribution parameter to queries originating from Play API
832// search engine.
833TEST_F(TemplateURLTest, PlayAPIAttribution) {
834 const struct TestData {
835 const char* url;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01836 std::u16string terms;
Pavel Yatsuk0d6a7b5c2019-10-09 20:33:44837 bool created_from_play_api;
838 const char* output;
839 } test_data[] = {{"http://foo/?q={searchTerms}", ASCIIToUTF16("bar"), false,
840 "http://foo/?q=bar"},
841 {"http://foo/?q={searchTerms}", ASCIIToUTF16("bar"), true,
842 "http://foo/?q=bar&chrome_dse_attribution=1"}};
843 TemplateURLData data;
844 for (size_t i = 0; i < base::size(test_data); ++i) {
845 data.SetURL(test_data[i].url);
846 data.created_from_play_api = test_data[i].created_from_play_api;
847 TemplateURL url(data);
848 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
849 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
850 GURL result(url.url_ref().ReplaceSearchTerms(
851 TemplateURLRef::SearchTermsArgs(test_data[i].terms),
852 search_terms_data_));
853 ASSERT_TRUE(result.is_valid());
854 EXPECT_EQ(test_data[i].output, result.spec());
855 }
856}
857
[email protected]d82443b2009-01-15 19:54:56858TEST_F(TemplateURLTest, Suggestions) {
859 struct TestData {
860 const int accepted_suggestion;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01861 const std::u16string original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56862 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27863 } test_data[] = {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01864 {TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::u16string(),
865 "http://bar/foo?q=foobar"},
866 {TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
867 "http://bar/foo?q=foobar"},
868 {TemplateURLRef::NO_SUGGESTION_CHOSEN, std::u16string(),
869 "http://bar/foo?q=foobar"},
870 {TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
871 "http://bar/foo?q=foobar"},
872 {0, std::u16string(), "http://bar/foo?oq=&q=foobar"},
873 {1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar"},
[email protected]d82443b2009-01-15 19:54:56874 };
[email protected]573889f22012-04-07 01:31:54875 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37876 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
877 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54878 data.input_encodings.push_back("UTF-8");
[email protected]168d08722014-06-18 07:13:28879 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19880 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
881 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Avi Drissman2244c2d2018-12-25 23:08:02882 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04883 TemplateURLRef::SearchTermsArgs search_terms_args(
884 ASCIIToUTF16("foobar"));
885 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
886 search_terms_args.original_query =
887 test_data[i].original_query_for_suggestion;
[email protected]ce7ee5f2014-06-16 23:41:19888 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
889 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27890 ASSERT_TRUE(result.is_valid());
891 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56892 }
893}
894
[email protected]81f808de2009-09-25 01:36:34895TEST_F(TemplateURLTest, RLZ) {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01896 std::u16string rlz_string = search_terms_data_.GetRlzParameterValue(false);
[email protected]d82443b2009-01-15 19:54:56897
[email protected]573889f22012-04-07 01:31:54898 TemplateURLData data;
899 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28900 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19901 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
902 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04903 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19904 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56905 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44906 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
907 result.spec());
[email protected]d82443b2009-01-15 19:54:56908}
909
[email protected]c8ccc41d2014-04-10 04:42:12910TEST_F(TemplateURLTest, RLZFromAppList) {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01911 std::u16string rlz_string = search_terms_data_.GetRlzParameterValue(true);
[email protected]c8ccc41d2014-04-10 04:42:12912
913 TemplateURLData data;
914 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28915 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19916 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
917 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12918 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
Moe Ahmadi4715d9542020-10-21 01:00:32919 args.request_source = TemplateURLRef::CROS_APP_LIST;
[email protected]ce7ee5f2014-06-16 23:41:19920 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12921 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44922 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
923 result.spec());
[email protected]c8ccc41d2014-04-10 04:42:12924}
[email protected]c8ccc41d2014-04-10 04:42:12925
[email protected]d82443b2009-01-15 19:54:56926TEST_F(TemplateURLTest, HostAndSearchTermKey) {
927 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19928 const std::string url;
[email protected]d82443b2009-01-15 19:54:56929 const std::string host;
930 const std::string path;
931 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54932 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50933 {"http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
934 {"http://blah/{searchTerms}", "blah", "", ""},
[email protected]d82443b2009-01-15 19:54:56935
vitbarf2a11562017-05-15 14:09:50936 // No term should result in empty values.
937 {"http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56938
vitbarf2a11562017-05-15 14:09:50939 // Multiple terms should result in empty values.
940 {"http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56941
vitbarf2a11562017-05-15 14:09:50942 // Term in the host shouldn't match.
943 {"http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56944
vitbarf2a11562017-05-15 14:09:50945 {"http://blah/?q={searchTerms}", "blah", "/", "q"},
946 {"https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56947
vitbarf2a11562017-05-15 14:09:50948 // Single term with extra chars in value should match.
949 {"http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56950 };
951
Avi Drissman2244c2d2018-12-25 23:08:02952 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54953 TemplateURLData data;
954 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28955 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19956 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
957 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
958 EXPECT_EQ(test_data[i].search_term_key,
959 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56960 }
961}
962
alexmos294849f2015-03-14 00:55:06963TEST_F(TemplateURLTest, SearchTermKeyLocation) {
964 struct TestData {
965 const std::string url;
966 const url::Parsed::ComponentType location;
967 const std::string path;
vitbarf2a11562017-05-15 14:09:50968 const std::string key;
969 const std::string value_prefix;
970 const std::string value_suffix;
alexmos294849f2015-03-14 00:55:06971 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50972 {"http://blah/{searchTerms}/", url::Parsed::PATH, "", "", "/", "/"},
973 {"http://blah/{searchTerms}", url::Parsed::PATH, "", "", "/", ""},
974 {"http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "", "",
975 "/begin/", "/end"},
976 {"http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY, "/", "q",
977 "", ""},
978 {"http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF, "/", "x",
979 "", ""},
980 {"http://www.example.com/?q=chromium-{searchTerms}@chromium.org/info",
981 url::Parsed::QUERY, "/", "q", "chromium-", "@chromium.org/info"},
alexmos294849f2015-03-14 00:55:06982
vitbarf2a11562017-05-15 14:09:50983 // searchTerms is a key, not a value, so this should result in an empty
984 // value.
985 {"http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x",
986 url::Parsed::QUERY, "", "", "", ""},
alexmos294849f2015-03-14 00:55:06987
vitbarf2a11562017-05-15 14:09:50988 // Multiple search terms should result in empty values.
989 {"http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY, "", "",
990 "", ""},
991 {"http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY, "", "",
992 "", ""},
993 {"http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY, "",
994 "", "", ""},
alexmos294849f2015-03-14 00:55:06995 };
996
Avi Drissman2244c2d2018-12-25 23:08:02997 for (size_t i = 0; i < base::size(test_data); ++i) {
alexmos294849f2015-03-14 00:55:06998 TemplateURLData data;
999 data.SetURL(test_data[i].url);
1000 TemplateURL url(data);
1001 EXPECT_EQ(test_data[i].location,
1002 url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
1003 EXPECT_EQ(test_data[i].path,
1004 url.url_ref().GetPath(search_terms_data_));
vitbarf2a11562017-05-15 14:09:501005 EXPECT_EQ(test_data[i].key,
1006 url.url_ref().GetSearchTermKey(search_terms_data_));
1007 EXPECT_EQ(test_data[i].value_prefix,
1008 url.url_ref().GetSearchTermValuePrefix(search_terms_data_));
1009 EXPECT_EQ(test_data[i].value_suffix,
1010 url.url_ref().GetSearchTermValueSuffix(search_terms_data_));
alexmos294849f2015-03-14 00:55:061011 }
1012}
1013
[email protected]d82443b2009-01-15 19:54:561014TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
1015 static const struct {
[email protected]ddd231e2010-06-29 20:35:191016 const char* const base_url;
1017 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:561018 } data[] = {
[email protected]014010e2011-10-01 04:12:441019 { "http://google.com/", "http://google.com/complete/", },
1020 { "http://www.google.com/", "http://www.google.com/complete/", },
1021 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
1022 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
1023 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:561024 };
1025
Avi Drissman2244c2d2018-12-25 23:08:021026 for (size_t i = 0; i < base::size(data); ++i)
[email protected]d82443b2009-01-15 19:54:561027 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
1028}
1029
[email protected]81c6ef62010-01-21 09:58:471030TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:191031 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:541032 TemplateURLData data;
1033 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:281034 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471035 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:131036 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:191037 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:471038 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271039 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471040 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1041}
1042
1043TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:201044 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:541045 TemplateURLData data;
1046 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:281047 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471048 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:441049
1050 // By default, TemplateURLRef should not consider itself prepopulated.
1051 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:131052 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:201053 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:441054 EXPECT_TRUE(replacements.empty());
1055
1056 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:201057 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:541058 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:281059 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:151060 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:201061 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:471062 EXPECT_TRUE(replacements.empty());
1063}
1064
1065TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:281066 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:471067 TemplateURLRef::Replacements replacements;
1068 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241069 EXPECT_EQ(std::string(), url.url_ref().ParseURL(std::string(), &replacements,
1070 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471071 EXPECT_TRUE(replacements.empty());
1072 EXPECT_TRUE(valid);
1073}
1074
1075TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:541076 TemplateURLData data;
1077 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:281078 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471079 TemplateURLRef::Replacements replacements;
1080 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241081 EXPECT_EQ(std::string(),
1082 url.url_ref().ParseURL("{", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471083 EXPECT_TRUE(replacements.empty());
1084 EXPECT_FALSE(valid);
1085}
1086
1087TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:541088 TemplateURLData data;
1089 data.SetURL("{}");
[email protected]168d08722014-06-18 07:13:281090 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471091 TemplateURLRef::Replacements replacements;
1092 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241093 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471094 EXPECT_TRUE(replacements.empty());
1095 EXPECT_TRUE(valid);
1096}
1097
1098TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:541099 TemplateURLData data;
1100 data.SetURL("{}{{%s}}");
[email protected]168d08722014-06-18 07:13:281101 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471102 TemplateURLRef::Replacements replacements;
1103 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241104 EXPECT_EQ("{}{}", url.url_ref().ParseURL("{}{{searchTerms}}", &replacements,
1105 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471106 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271107 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471108 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1109 EXPECT_TRUE(valid);
1110}
1111
1112TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:541113 TemplateURLData data;
1114 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:281115 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471116 TemplateURLRef::Replacements replacements;
1117 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241118 EXPECT_EQ("{", url.url_ref().ParseURL("{{searchTerms}", &replacements,
1119 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471120 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271121 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471122 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1123 EXPECT_TRUE(valid);
1124}
[email protected]5b3078752012-10-09 18:54:161125
[email protected]fd6d8822012-12-08 06:56:111126TEST_F(TemplateURLTest, SearchClient) {
1127 const std::string base_url_str("http://google.com/?");
1128 const std::string terms_str("{searchTerms}&{google:searchClient}");
1129 const std::string full_url_str = base_url_str + terms_str;
Jan Wilken Dörriefa241ba2021-03-11 17:57:011130 const std::u16string terms(ASCIIToUTF16(terms_str));
[email protected]798baa8e2014-06-20 05:42:441131 search_terms_data_.set_google_base_url(base_url_str);
[email protected]fd6d8822012-12-08 06:56:111132
1133 TemplateURLData data;
1134 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:281135 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191136 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1137 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111138 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
1139
1140 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:191141 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1142 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111143 ASSERT_TRUE(result.is_valid());
1144 EXPECT_EQ("http://google.com/?foobar&", result.spec());
1145
1146 // Check that the URL is correct when a client is present.
[email protected]798baa8e2014-06-20 05:42:441147 search_terms_data_.set_search_client("search_client");
[email protected]ce7ee5f2014-06-16 23:41:191148 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
1149 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111150 ASSERT_TRUE(result_2.is_valid());
[email protected]798baa8e2014-06-20 05:42:441151 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
[email protected]fd6d8822012-12-08 06:56:111152}
[email protected]fd6d8822012-12-08 06:56:111153
Moe Ahmadi4715d9542020-10-21 01:00:321154TEST_F(TemplateURLTest, SuggestClient) {
1155 const std::string base_url_str("http://google.com/?");
1156 const std::string query_params_str("client={google:suggestClient}");
1157 const std::string full_url_str = base_url_str + query_params_str;
1158 search_terms_data_.set_google_base_url(base_url_str);
1159
1160 TemplateURLData data;
1161 data.SetURL(full_url_str);
1162 TemplateURL url(data);
1163 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1164 ASSERT_FALSE(url.url_ref().SupportsReplacement(search_terms_data_));
1165 TemplateURLRef::SearchTermsArgs search_terms_args;
1166
1167 // Check that the URL is correct when a client is not present.
1168 GURL result(
1169 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_));
1170 ASSERT_TRUE(result.is_valid());
1171 EXPECT_EQ("http://google.com/?client=", result.spec());
1172
1173 // Check that the URL is correct when a client is present.
1174 search_terms_data_.set_suggest_client("suggest_client");
1175 GURL result_2(
1176 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_));
1177 ASSERT_TRUE(result_2.is_valid());
1178 EXPECT_EQ("http://google.com/?client=suggest_client", result_2.spec());
1179
1180 // Check that the URL is correct when a suggest request is made from a
1181 // non-searchbox NTP surface.
1182 search_terms_args.request_source = TemplateURLRef::NON_SEARCHBOX_NTP;
1183 GURL result_3(
1184 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_));
1185 ASSERT_TRUE(result_3.is_valid());
1186 EXPECT_EQ("http://google.com/?client=suggest_client_from_ntp",
1187 result_3.spec());
1188}
1189
[email protected]5b3078752012-10-09 18:54:161190TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
1191 TemplateURLData data;
1192 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161193 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
1194 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281195 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511196 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1197 ASSERT_EQ(3U, url_refs.size());
1198 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1199 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1200 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161201}
1202
1203TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
1204 TemplateURLData data;
1205 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:281206 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511207 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1208 ASSERT_EQ(1U, url_refs.size());
1209 EXPECT_EQ("http://www.google.co.uk/", url_refs[0].GetURL());
[email protected]5b3078752012-10-09 18:54:161210}
1211
1212TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1213 TemplateURLData data;
1214 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161215 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1216 data.alternate_urls.push_back(
1217 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281218 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011219 std::u16string result;
[email protected]5b3078752012-10-09 18:54:161220
1221 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191222 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191223 EXPECT_EQ(ASCIIToUTF16("something"), result);
1224
1225 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471226 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191227 EXPECT_EQ(ASCIIToUTF16("something"), result);
1228
1229 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471230 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191231 EXPECT_EQ(ASCIIToUTF16("something"), result);
1232
1233 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471234 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191235 EXPECT_EQ(ASCIIToUTF16("something"), result);
1236
1237 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191238 GURL("http://google.com/alt/#q=something"),
1239 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191240 EXPECT_EQ(ASCIIToUTF16("something"), result);
1241
1242 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471243 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191244 EXPECT_EQ(ASCIIToUTF16("something"), result);
1245
1246 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471247 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191248 EXPECT_EQ(ASCIIToUTF16("something"), result);
1249
1250 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471251 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161252 EXPECT_EQ(ASCIIToUTF16("something"), result);
1253
1254 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191255 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011256 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161257
1258 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191259 GURL("http://google.ca/?q=something&q=anything"),
1260 search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011261 EXPECT_EQ(std::u16string(), result);
[email protected]67d8b752013-04-03 17:33:271262
1263 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191264 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011265 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161266
[email protected]32e2d81b2012-10-16 19:03:251267 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191268 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
[email protected]32e2d81b2012-10-16 19:03:251269 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:161270
1271 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191272 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011273 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161274
1275 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191276 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161277 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1278
1279 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191280 GURL("http://google.com/alt/?q=123#q=456"),
1281 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161282 EXPECT_EQ(ASCIIToUTF16("456"), result);
1283
1284 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191285 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1286 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161287 EXPECT_EQ(ASCIIToUTF16("123"), result);
1288
1289 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191290 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1291 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161292 EXPECT_EQ(ASCIIToUTF16("789"), result);
1293
1294 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191295 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011296 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161297
1298 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191299 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011300 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161301
1302 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191303 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011304 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161305
1306 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191307 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011308 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161309
1310 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191311 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161312 EXPECT_EQ(ASCIIToUTF16("123"), result);
1313}
[email protected]4076ea62013-01-09 01:47:191314
alexmos294849f2015-03-14 00:55:061315TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1316 TemplateURLData data;
1317 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1318 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011319 std::u16string result;
alexmos294849f2015-03-14 00:55:061320
1321 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1322 GURL("http://term-in-path.com/begin/something/end"),
1323 search_terms_data_, &result));
1324 EXPECT_EQ(ASCIIToUTF16("something"), result);
1325
1326 // "%20" must be converted to space.
1327 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1328 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1329 search_terms_data_, &result));
1330 EXPECT_EQ(ASCIIToUTF16("a b c"), result);
1331
1332 // Plus must not be converted to space.
1333 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1334 GURL("http://term-in-path.com/begin/1+2+3/end"),
1335 search_terms_data_, &result));
1336 EXPECT_EQ(ASCIIToUTF16("1+2+3"), result);
1337
1338 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1339 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011340 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061341
1342 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1343 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011344 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061345
1346 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1347 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011348 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061349}
1350
vitbar27804d102015-04-15 10:54:411351// Checks that the ExtractSearchTermsFromURL function works correctly
1352// for urls containing non-latin characters in UTF8 encoding.
1353TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
1354 TemplateURLData data;
1355 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1356 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1357 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1358 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011359 std::u16string result;
vitbar27804d102015-04-15 10:54:411360
1361 // Russian text encoded with UTF-8.
1362 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551363 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1364 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
vitbar27804d102015-04-15 10:54:411365 search_terms_data_, &result));
1366 EXPECT_EQ(
1367 base::WideToUTF16(
1368 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1369 L"\x043C\x0438\x0440!"),
1370 result);
1371
1372 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551373 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1374 "%D0%B0"),
vitbar27804d102015-04-15 10:54:411375 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331376 EXPECT_EQ(u"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430", result);
vitbar27804d102015-04-15 10:54:411377
1378 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551379 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1380 "%D0%B8%20A"),
vitbar27804d102015-04-15 10:54:411381 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331382 EXPECT_EQ(u"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A", result);
vitbar27804d102015-04-15 10:54:411383}
1384
1385// Checks that the ExtractSearchTermsFromURL function works correctly
1386// for urls containing non-latin characters in non-UTF8 encoding.
1387TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) {
1388 TemplateURLData data;
1389 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1390 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1391 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1392 data.input_encodings.push_back("windows-1251");
1393 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011394 std::u16string result;
vitbar27804d102015-04-15 10:54:411395
1396 // Russian text encoded with Windows-1251.
1397 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1398 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1399 "%EC%E8%F0!"),
1400 search_terms_data_, &result));
1401 EXPECT_EQ(
1402 base::WideToUTF16(
1403 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1404 L"\x043C\x0438\x0440!"),
1405 result);
1406
1407 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1408 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1409 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331410 EXPECT_EQ(u"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430", result);
vitbar27804d102015-04-15 10:54:411411
1412 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1413 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1414 search_terms_data_, &result));
Jan Wilken Dörrieeb96aa82021-03-19 19:16:331415 EXPECT_EQ(u"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A", result);
vitbar27804d102015-04-15 10:54:411416}
1417
jbroman50832022016-04-21 23:53:001418// Checks that the ExtractSearchTermsFromURL function strips constant
1419// prefix/suffix strings from the search terms param.
1420TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) {
1421 TemplateURLData data;
1422 data.alternate_urls.push_back(
1423 "http://www.example.com/?q=chromium-{searchTerms}@chromium.org");
1424 data.alternate_urls.push_back(
1425 "http://www.example.com/chromium-{searchTerms}@chromium.org/info");
1426 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011427 std::u16string result;
jbroman50832022016-04-21 23:53:001428
1429 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1430 GURL("http://www.example.com/[email protected]"),
1431 search_terms_data_, &result));
1432 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1433
1434 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1435 GURL("http://www.example.com/[email protected]/info"),
1436 search_terms_data_, &result));
1437 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1438
1439 // Don't match if the prefix and suffix aren't there.
1440 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1441 GURL("http://www.example.com/?q=invalid"), search_terms_data_, &result));
jbromane8b8728d2017-04-12 17:29:391442
1443 // Don't match if the prefix and suffix overlap.
1444 TemplateURLData data_with_overlap;
1445 data.alternate_urls.push_back(
1446 "http://www.example.com/?q=goo{searchTerms}oogle");
1447 TemplateURL url_with_overlap(data);
1448 EXPECT_FALSE(url_with_overlap.ExtractSearchTermsFromURL(
1449 GURL("http://www.example.com/?q=google"), search_terms_data_, &result));
jbroman50832022016-04-21 23:53:001450}
1451
[email protected]f62e30f52013-03-23 03:45:151452TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1453 TemplateURLData data;
1454 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]f62e30f52013-03-23 03:45:151455 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1456 data.alternate_urls.push_back(
1457 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281458 TemplateURL url(data);
[email protected]f62e30f52013-03-23 03:45:151459 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1460 GURL result;
1461
1462 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191463 GURL("http://google.com/?q=something"), search_terms,
1464 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101465 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151466
1467 result = GURL("http://should.not.change.com");
1468 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191469 GURL("http://google.ca/?q=something"), search_terms,
1470 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151471 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1472
1473 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191474 GURL("http://google.com/foo/?q=foo"), search_terms,
1475 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151476
1477 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191478 GURL("https://google.com/?q=foo"), search_terms,
1479 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101480 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151481
1482 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191483 GURL("http://google.com:8080/?q=foo"), search_terms,
1484 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151485
1486 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191487 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1488 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101489 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result);
[email protected]f62e30f52013-03-23 03:45:151490
1491 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1492 // template_url.cc for details.
1493 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191494 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1495 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101496 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151497
1498 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1499 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191500 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101501 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
[email protected]f62e30f52013-03-23 03:45:151502 result);
1503
1504 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1505 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191506 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151507 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
vitbarf298455d2015-04-21 12:58:101508 "#j=abc&q=Bob+Morane&h=def9"), result);
[email protected]f62e30f52013-03-23 03:45:151509
1510 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191511 GURL("http://google.com/alt/?q="), search_terms,
1512 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151513
1514 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191515 GURL("http://google.com/alt/?#q="), search_terms,
1516 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151517
1518 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191519 GURL("http://google.com/alt/?q=#q="), search_terms,
1520 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151521
1522 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191523 GURL("http://google.com/alt/?q=123#q="), search_terms,
1524 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151525
1526 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191527 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1528 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101529 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151530}
[email protected]56fa29592013-07-02 20:25:531531
alexmos294849f2015-03-14 00:55:061532TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1533 TemplateURLData data;
1534 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1535 TemplateURL url(data);
1536 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1537 GURL result;
1538
1539 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1540 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1541 search_terms_data_, &result));
1542 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1543
1544 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1545 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1546 search_terms_data_, &result));
1547 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1548
1549 result = GURL("http://should.not.change.com");
1550 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1551 GURL("http://term-in-path.com/about"), search_terms,
1552 search_terms_data_, &result));
1553 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1554}
1555
vitbarf298455d2015-04-21 12:58:101556// Checks that the ReplaceSearchTermsInURL function works correctly
1557// for search terms containing non-latin characters for a search engine
1558// using UTF-8 input encoding.
1559TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) {
1560 TemplateURLData data;
1561 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1562 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1563 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1564 TemplateURL url(data);
1565
1566 // Russian text which will be encoded with UTF-8.
1567 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1568 L"\x0442\x0435\x043A\x0441\x0442"));
1569 GURL result;
1570
1571 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1572 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_,
1573 &result));
1574 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1575 result);
1576
1577 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1578 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_,
1579 &result));
1580 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1581 result);
1582
1583 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1584 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_,
1585 &result));
1586 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1587 result);
1588}
1589
1590// Checks that the ReplaceSearchTermsInURL function works correctly
1591// for search terms containing non-latin characters for a search engine
1592// using non UTF-8 input encoding.
1593TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) {
1594 TemplateURLData data;
1595 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1596 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1597 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1598 data.input_encodings.push_back("windows-1251");
1599 TemplateURL url(data);
1600
1601 // Russian text which will be encoded with Windows-1251.
1602 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1603 L"\x0442\x0435\x043A\x0441\x0442"));
1604 GURL result;
1605
1606 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1607 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_,
1608 &result));
1609 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1610 result);
1611
1612 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1613 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_,
1614 &result));
1615 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1616 result);
1617
1618 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1619 GURL("http://windows-1251.ru/path/a%20b"), search_terms,
1620 search_terms_data_, &result));
1621 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1622 result);
1623}
1624
Mark Pearson247fb8a2018-08-30 05:12:261625// Test the |additional_query_params| field of SearchTermsArgs.
[email protected]621ade062013-10-28 06:27:431626TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431627 TemplateURLData data;
1628 // Pick a URL with replacements before, during, and after the query, to ensure
1629 // we don't goof up any of them.
1630 data.SetURL("{google:baseURL}search?q={searchTerms}"
1631 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281632 TemplateURL url(data);
[email protected]621ade062013-10-28 06:27:431633
Mark Pearson247fb8a2018-08-30 05:12:261634 // Baseline: no |additional_query_params| field.
[email protected]621ade062013-10-28 06:27:431635 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1636 search_terms.original_query = ASCIIToUTF16("def");
1637 search_terms.accepted_suggestion = 0;
1638 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191639 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431640
Mark Pearson247fb8a2018-08-30 05:12:261641 // Set the |additional_query_params|.
1642 search_terms.additional_query_params = "pq=xyz";
[email protected]621ade062013-10-28 06:27:431643 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191644 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431645
Mark Pearson247fb8a2018-08-30 05:12:261646 // Add |append_extra_query_params_from_command_line| into the mix, and ensure
1647 // it works.
1648 search_terms.append_extra_query_params_from_command_line = true;
avi1772c1a2014-12-22 22:42:331649 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]621ade062013-10-28 06:27:431650 switches::kExtraSearchQueryParams, "a=b");
1651 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191652 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431653}
1654
Mark Pearson247fb8a2018-08-30 05:12:261655// Test the |search_terms.append_extra_query_params_from_command_line| field of
1656// SearchTermsArgs.
[email protected]56fa29592013-07-02 20:25:531657TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531658 TemplateURLData data;
1659 // Pick a URL with replacements before, during, and after the query, to ensure
1660 // we don't goof up any of them.
1661 data.SetURL("{google:baseURL}search?q={searchTerms}"
1662 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281663 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531664
Mark Pearson247fb8a2018-08-30 05:12:261665 // Baseline: no command-line args, no
1666 // |search_terms.append_extra_query_params_from_command_line| flag.
[email protected]56fa29592013-07-02 20:25:531667 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1668 search_terms.original_query = ASCIIToUTF16("def");
1669 search_terms.accepted_suggestion = 0;
1670 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191671 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531672
1673 // Set the flag. Since there are no command-line args, this should have no
1674 // effect.
Mark Pearson247fb8a2018-08-30 05:12:261675 search_terms.append_extra_query_params_from_command_line = true;
[email protected]56fa29592013-07-02 20:25:531676 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191677 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531678
1679 // Now append the command-line arg. This should be inserted into the query.
avi1772c1a2014-12-22 22:42:331680 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]56fa29592013-07-02 20:25:531681 switches::kExtraSearchQueryParams, "a=b");
1682 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191683 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531684
1685 // Turn off the flag. Now the command-line arg should be ignored again.
Mark Pearson247fb8a2018-08-30 05:12:261686 search_terms.append_extra_query_params_from_command_line = false;
[email protected]56fa29592013-07-02 20:25:531687 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191688 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531689}
[email protected]d5015ca2013-08-08 22:04:181690
1691// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171692TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181693 TemplateURLData data;
1694 data.input_encodings.push_back("UTF-8");
1695 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281696 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191697 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1698 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]d5015ca2013-08-08 22:04:181699 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1700
[email protected]ce7ee5f2014-06-16 23:41:191701 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1702 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181703 EXPECT_EQ("http://www.google.com/?q=foo", result);
1704
[email protected]332d17d22014-06-20 16:56:031705 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191706 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1707 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181708 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1709
1710 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031711 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191712 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1713 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181714 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1715}
[email protected]2328ee22013-08-08 23:00:191716
1717// Test the IsSearchResults function.
1718TEST_F(TemplateURLTest, IsSearchResults) {
1719 TemplateURLData data;
1720 data.SetURL("http://bar/search?q={searchTerms}");
[email protected]2767c0fd2013-08-16 17:44:161721 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191722 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1723 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1724 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1725 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281726 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191727
1728 const struct {
1729 const char* const url;
1730 bool result;
1731 } url_data[] = {
1732 { "http://bar/search?q=foo&oq=foo", true, },
1733 { "http://bar/?q=foo&oq=foo", true, },
1734 { "http://bar/#output=search&q=foo&oq=foo", true, },
1735 { "http://bar/webhp#q=foo&oq=foo", true, },
1736 { "http://bar/#q=foo&oq=foo", true, },
1737 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1738 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1739 { "http://bar/", false, },
1740 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161741 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191742 };
1743
Avi Drissman2244c2d2018-12-25 23:08:021744 for (size_t i = 0; i < base::size(url_data); ++i) {
[email protected]2328ee22013-08-08 23:00:191745 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191746 search_provider.IsSearchURL(GURL(url_data[i].url),
1747 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191748 }
1749}
[email protected]a048de8a2013-10-02 18:30:061750
jdonnelly41c5b46a2015-07-10 21:24:381751TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) {
[email protected]9d70de12014-05-10 02:15:311752 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441753 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311754 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1755
[email protected]168d08722014-06-18 07:13:281756 TemplateURL url(data);
[email protected]9d70de12014-05-10 02:15:311757 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191758 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1759 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311760 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1761}
[email protected]20184242014-05-14 02:57:421762
1763TEST_F(TemplateURLTest, SessionToken) {
1764 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441765 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421766 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1767
[email protected]168d08722014-06-18 07:13:281768 TemplateURL url(data);
[email protected]20184242014-05-14 02:57:421769 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1770 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191771 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1772 search_terms_data_);
[email protected]20184242014-05-14 02:57:421773 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1774
[email protected]168d08722014-06-18 07:13:281775 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421776 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191777 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1778 search_terms_data_);
[email protected]20184242014-05-14 02:57:421779 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1780}
[email protected]448b17f52014-06-13 01:08:031781
1782TEST_F(TemplateURLTest, ContextualSearchParameters) {
1783 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441784 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031785 data.SetURL("http://bar/_/contextualsearch?"
1786 "{google:contextualSearchVersion}"
1787 "{google:contextualSearchContextData}");
1788
[email protected]168d08722014-06-18 07:13:281789 TemplateURL url(data);
[email protected]448b17f52014-06-13 01:08:031790 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191791 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1792 search_terms_data_);
donndbb98274e2016-11-01 21:04:401793 EXPECT_EQ("http://bar/_/contextualsearch?", result);
[email protected]448b17f52014-06-13 01:08:031794
Donn Denman01b1bf72018-08-14 16:25:201795 // Test the current common case, which uses no home country or previous
1796 // event.
1797 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
Donn Denman97be51df2020-06-24 01:23:191798 2, 1, std::string(), 0, 0, false, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391799 std::string(), std::string());
[email protected]448b17f52014-06-13 01:08:031800 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191801 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1802 search_terms_data_);
donndd9fc4d92016-09-16 00:30:491803 EXPECT_EQ(
1804 "http://bar/_/contextualsearch?"
donnd0bc73292017-01-17 22:07:561805 "ctxs=2&"
donndd9fc4d92016-09-16 00:30:491806 "ctxsl_coca=1",
1807 result);
donndbb98274e2016-11-01 21:04:401808
Donn Denman01b1bf72018-08-14 16:25:201809 // Test the home country and non-zero event data case.
donndbb98274e2016-11-01 21:04:401810 search_terms_args.contextual_search_params =
Donn Denmandf4ede0f2020-01-17 23:11:221811 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denman97be51df2020-06-24 01:23:191812 2, 2, "CH", 1657713458, 5, false, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391813 std::string(), std::string());
donndbb98274e2016-11-01 21:04:401814 result =
1815 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1816
1817 EXPECT_EQ(
1818 "http://bar/_/contextualsearch?"
1819 "ctxs=2&"
donnd0bc73292017-01-17 22:07:561820 "ctxsl_coca=2&"
Donn Denman01b1bf72018-08-14 16:25:201821 "ctxs_hc=CH&"
1822 "ctxsl_pid=1657713458&"
1823 "ctxsl_per=5",
donndbb98274e2016-11-01 21:04:401824 result);
Donn Denmandf4ede0f2020-01-17 23:11:221825
1826 // Test exact-search.
1827 search_terms_args.contextual_search_params =
1828 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denman97be51df2020-06-24 01:23:191829 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391830 std::string(), std::string());
Donn Denmandf4ede0f2020-01-17 23:11:221831 result =
1832 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
Donn Denmandf4ede0f2020-01-17 23:11:221833 // Find our param.
1834 size_t found_pos = result.find("ctxsl_exact=1");
1835 EXPECT_NE(found_pos, std::string::npos);
Donn Denman7a241012020-01-31 18:33:131836
1837 // Test source and target languages.
1838 search_terms_args.contextual_search_params =
1839 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denmand638f4602020-11-03 21:05:391840 2, 1, std::string(), 0, 0, true, "es", "de", std::string(),
1841 std::string());
Donn Denman7a241012020-01-31 18:33:131842 result =
1843 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1844 // Find our params.
1845 size_t source_pos = result.find("tlitesl=es");
1846 EXPECT_NE(source_pos, std::string::npos);
1847 size_t target_pos = result.find("tlitetl=de");
1848 EXPECT_NE(target_pos, std::string::npos);
Donn Denman97be51df2020-06-24 01:23:191849
1850 // Test fluent languages.
1851 search_terms_args.contextual_search_params =
1852 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
1853 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391854 "es,de", std::string());
Donn Denman97be51df2020-06-24 01:23:191855 result =
1856 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1857 // Find our param. These may actually be URL encoded.
1858 size_t fluent_pos = result.find("&ctxs_fls=es,de");
1859 EXPECT_NE(fluent_pos, std::string::npos);
Donn Denmand638f4602020-11-03 21:05:391860
1861 // Test Related Searches.
1862 search_terms_args.contextual_search_params =
1863 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
1864 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
1865 std::string(), "1RbCu");
1866 result =
1867 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1868 // Find our param.
1869 size_t ctxsl_rs_pos = result.find("&ctxsl_rs=1RbCu");
1870 EXPECT_NE(ctxsl_rs_pos, std::string::npos);
[email protected]448b17f52014-06-13 01:08:031871}
[email protected]44ccc9f2014-06-20 17:36:211872
1873TEST_F(TemplateURLTest, GenerateKeyword) {
1874 ASSERT_EQ(ASCIIToUTF16("foo"),
jshin1fb76462016-04-05 22:13:031875 TemplateURL::GenerateKeyword(GURL("http://foo")));
Emily Stark0fb634f82020-07-14 18:00:391876 ASSERT_EQ(ASCIIToUTF16("foo."),
1877 TemplateURL::GenerateKeyword(GURL("http://foo.")));
1878 // www. should be stripped for a public hostname but not a private/intranet
1879 // hostname.
1880 ASSERT_EQ(ASCIIToUTF16("google.com"),
1881 TemplateURL::GenerateKeyword(GURL("http://www.google.com")));
1882 ASSERT_EQ(ASCIIToUTF16("www.foo"),
jshin1fb76462016-04-05 22:13:031883 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
[email protected]44ccc9f2014-06-20 17:36:211884 // Make sure we don't get a trailing '/'.
jshin1fb76462016-04-05 22:13:031885 ASSERT_EQ(ASCIIToUTF16("blah"),
1886 TemplateURL::GenerateKeyword(GURL("http://blah/")));
[email protected]44ccc9f2014-06-20 17:36:211887 // Don't generate the empty string.
Emily Stark0fb634f82020-07-14 18:00:391888 ASSERT_EQ(ASCIIToUTF16("www."),
jshin1fb76462016-04-05 22:13:031889 TemplateURL::GenerateKeyword(GURL("http://www.")));
alshabaline6212ac2015-07-23 08:03:001890 ASSERT_EQ(
1891 base::UTF8ToUTF16("\xd0\xb0\xd0\xb1\xd0\xb2"),
jshin1fb76462016-04-05 22:13:031892 TemplateURL::GenerateKeyword(GURL("http://xn--80acd")));
a-v-y81695d0d2017-04-20 08:22:221893
1894 // Generated keywords must always be in lowercase, because TemplateURLs always
1895 // converts keywords to lowercase in its constructor and TemplateURLService
1896 // stores TemplateURLs in maps using keyword as key.
1897 EXPECT_TRUE(IsLowerCase(TemplateURL::GenerateKeyword(GURL("http://BLAH/"))));
1898 EXPECT_TRUE(IsLowerCase(
1899 TemplateURL::GenerateKeyword(GURL("http://embeddedhtml.<head>/"))));
[email protected]44ccc9f2014-06-20 17:36:211900}
1901
1902TEST_F(TemplateURLTest, GenerateSearchURL) {
1903 struct GenerateSearchURLCase {
1904 const char* test_name;
1905 const char* url;
1906 const char* expected;
1907 } generate_url_cases[] = {
1908 { "invalid URL", "foo{searchTerms}", "" },
1909 { "URL with no replacements", "http://foo/", "http://foo/" },
1910 { "basic functionality", "http://foo/{searchTerms}",
1911 "http://foo/blah.blah.blah.blah.blah" }
1912 };
1913
Avi Drissman2244c2d2018-12-25 23:08:021914 for (size_t i = 0; i < base::size(generate_url_cases); ++i) {
[email protected]44ccc9f2014-06-20 17:36:211915 TemplateURLData data;
1916 data.SetURL(generate_url_cases[i].url);
1917 TemplateURL t_url(data);
1918 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1919 generate_url_cases[i].expected)
1920 << generate_url_cases[i].test_name << " failed.";
1921 }
1922}
[email protected]9e9130ce2014-06-23 23:20:511923
1924TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1925 TemplateURLData data;
1926 search_terms_data_.set_google_base_url("http://bar/");
1927 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1928
1929 TemplateURL url(data);
1930 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1931 search_terms_args.prefetch_query = "full query text";
1932 search_terms_args.prefetch_query_type = "2338";
1933 std::string result =
1934 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1935 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1936 result);
1937
1938 TemplateURL url2(data);
1939 search_terms_args.prefetch_query.clear();
1940 search_terms_args.prefetch_query_type.clear();
1941 result =
1942 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1943 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1944}
vitbar5bd8c252016-01-29 18:44:511945
1946// Tests that TemplateURL works correctly after changing the Google base URL
1947// and invalidating cached values.
1948TEST_F(TemplateURLTest, InvalidateCachedValues) {
1949 TemplateURLData data;
1950 data.SetURL("{google:baseURL}search?q={searchTerms}");
1951 data.suggestions_url = "{google:baseSuggestURL}search?q={searchTerms}";
vitbar5bd8c252016-01-29 18:44:511952 data.image_url = "{google:baseURL}searchbyimage/upload";
1953 data.new_tab_url = "{google:baseURL}_/chrome/newtab";
1954 data.contextual_search_url = "{google:baseURL}_/contextualsearch";
1955 data.alternate_urls.push_back("{google:baseURL}s#q={searchTerms}");
1956 TemplateURL url(data);
1957 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("X"));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011958 std::u16string search_terms;
vitbar5bd8c252016-01-29 18:44:511959
1960 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1961 EXPECT_EQ("http://www.google.com/search?q=X",
1962 url.url_ref().ReplaceSearchTerms(search_terms_args,
1963 search_terms_data_));
1964 EXPECT_EQ("http://www.google.com/s#q=X",
1965 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1966 search_terms_data_));
1967 EXPECT_EQ("http://www.google.com/search?q=X",
1968 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1969 search_terms_data_));
1970 EXPECT_EQ("http://www.google.com/complete/search?q=X",
1971 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1972 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:511973 EXPECT_EQ("http://www.google.com/searchbyimage/upload",
1974 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1975 search_terms_data_));
1976 EXPECT_EQ("http://www.google.com/_/chrome/newtab",
1977 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1978 search_terms_data_));
1979 EXPECT_EQ("http://www.google.com/_/contextualsearch",
1980 url.contextual_search_url_ref().ReplaceSearchTerms(
1981 search_terms_args, search_terms_data_));
1982
1983 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1984 GURL("http://www.google.com/search?q=Y+Z"),
1985 search_terms_data_, &search_terms));
1986 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
1987 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1988 GURL("http://www.google.com/s#q=123"),
1989 search_terms_data_, &search_terms));
1990 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
1991
1992 search_terms_data_.set_google_base_url("https://www.foo.org/");
1993 url.InvalidateCachedValues();
1994
1995 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1996 EXPECT_EQ("https://www.foo.org/search?q=X",
1997 url.url_ref().ReplaceSearchTerms(search_terms_args,
1998 search_terms_data_));
1999 EXPECT_EQ("https://www.foo.org/s#q=X",
2000 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
2001 search_terms_data_));
2002 EXPECT_EQ("https://www.foo.org/search?q=X",
2003 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
2004 search_terms_data_));
2005 EXPECT_EQ("https://www.foo.org/complete/search?q=X",
2006 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
2007 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:512008 EXPECT_EQ("https://www.foo.org/searchbyimage/upload",
2009 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
2010 search_terms_data_));
2011 EXPECT_EQ("https://www.foo.org/_/chrome/newtab",
2012 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
2013 search_terms_data_));
2014 EXPECT_EQ("https://www.foo.org/_/contextualsearch",
2015 url.contextual_search_url_ref().ReplaceSearchTerms(
2016 search_terms_args, search_terms_data_));
2017
2018 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
2019 GURL("https://www.foo.org/search?q=Y+Z"),
2020 search_terms_data_, &search_terms));
2021 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
2022 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
2023 GURL("https://www.foo.org/s#q=123"),
2024 search_terms_data_, &search_terms));
2025 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
2026
2027 search_terms_data_.set_google_base_url("http://www.google.com/");
2028}
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002029
2030// Tests the use of wildcards in the path to ensure both extracting search terms
2031// and generating a search URL work correctly.
2032TEST_F(TemplateURLTest, PathWildcard) {
2033 TemplateURLData data;
2034 data.SetURL(
2035 "https://www.google.com/search{google:pathWildcard}?q={searchTerms}");
2036 TemplateURL url(data);
2037
2038 // Test extracting search terms from a URL.
Jan Wilken Dörriefa241ba2021-03-11 17:57:012039 std::u16string search_terms;
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002040 url.ExtractSearchTermsFromURL(GURL("https://www.google.com/search?q=testing"),
2041 search_terms_data_, &search_terms);
2042 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
2043 url.ExtractSearchTermsFromURL(
2044 GURL("https://www.google.com/search;_this_is_a_test;_?q=testing"),
2045 search_terms_data_, &search_terms);
2046 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
2047
2048 // Tests overlapping prefix/suffix.
2049 data.SetURL(
2050 "https://www.google.com/search{google:pathWildcard}rch?q={searchTerms}");
2051 TemplateURL overlap_url(data);
2052 overlap_url.ExtractSearchTermsFromURL(
2053 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
2054 &search_terms);
2055 EXPECT_TRUE(search_terms.empty());
2056
2057 // Tests wildcard at beginning of path so we only have a suffix.
2058 data.SetURL(
2059 "https://www.google.com/{google:pathWildcard}rch?q={searchTerms}");
2060 TemplateURL suffix_url(data);
2061 suffix_url.ExtractSearchTermsFromURL(
2062 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
2063 &search_terms);
2064 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
2065
2066 // Tests wildcard between prefix/suffix.
2067 overlap_url.ExtractSearchTermsFromURL(
2068 GURL("https://www.google.com/search_testing_rch?q=testing"),
2069 search_terms_data_, &search_terms);
2070 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
2071
2072 // Test generating a URL.
2073 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
2074 GURL generated_url;
2075 url.ReplaceSearchTermsInURL(url.GenerateSearchURL(search_terms_data_),
2076 search_terms_args, search_terms_data_,
2077 &generated_url);
2078 EXPECT_EQ("https://www.google.com/search?q=foo", generated_url.spec());
2079}