blob: 0242cb5bf7b1b8cf9b51d1bbe2c5c636ac70bcc2 [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[] = {
[email protected]400b133f2011-01-19 18:32:30417 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56418 // Test some big-5 input.
[email protected]f911df52013-12-24 23:24:23419 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56420 // 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 invalid
422 // Big-5 trailing byte.
[email protected]f911df52013-12-24 23:24:23423 {"%e4%bd%a05%e5%a5%bd+to+you",
424 base::WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56425 // Undecodable input should stay escaped.
[email protected]f911df52013-12-24 23:24:23426 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44427 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30428 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44429 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30430 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56431 };
432
[email protected]d82443b2009-01-15 19:54:56433 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54434 TemplateURLData data;
435 data.SetURL("http://foo?q={searchTerms}");
436 data.input_encodings.push_back("big-5");
[email protected]168d08722014-06-18 07:13:28437 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19438 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
439 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Avi Drissman2244c2d2018-12-25 23:08:02440 for (size_t i = 0; i < base::size(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07441 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13442 url.url_ref().SearchTermToString16(
443 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56444 }
445}
446
[email protected]d82443b2009-01-15 19:54:56447TEST_F(TemplateURLTest, DisplayURLToURLRef) {
448 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19449 const std::string url;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01450 const std::u16string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27451 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19452 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30453 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19454 { "http://X",
[email protected]400b133f2011-01-19 18:32:30455 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19456 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30457 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19458 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30459 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56460 };
[email protected]573889f22012-04-07 01:31:54461 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02462 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54463 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28464 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19465 EXPECT_EQ(test_data[i].expected_result,
466 url.url_ref().DisplayURL(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27467 EXPECT_EQ(test_data[i].url,
[email protected]ce7ee5f2014-06-16 23:41:19468 TemplateURLRef::DisplayURLToURLRef(
469 url.url_ref().DisplayURL(search_terms_data_)));
[email protected]d82443b2009-01-15 19:54:56470 }
471}
472
473TEST_F(TemplateURLTest, ReplaceSearchTerms) {
474 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19475 const std::string url;
[email protected]d82443b2009-01-15 19:54:56476 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27477 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19478 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56479 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19480 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56481 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19482 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56483 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19484 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56485 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19486 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56487 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19488 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56489 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19490 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56491 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19492 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56493 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19494 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56495 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19496 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56497 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19498 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56499 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19500 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56501 "http://foo/UTF-8a{language}aXa" },
502 };
[email protected]573889f22012-04-07 01:31:54503 TemplateURLData data;
504 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02505 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54506 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28507 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19508 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
509 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27510 std::string expected_result = test_data[i].expected_result;
brettwe6dae462015-06-24 20:54:45511 base::ReplaceSubstringsAfterOffset(
512 &expected_result, 0, "{language}",
513 search_terms_data_.GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04514 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19515 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
516 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27517 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56518 EXPECT_EQ(expected_result, result.spec());
519 }
520}
521
522
523// Tests replacing search terms in various encodings and making sure the
524// generated URL matches the expected value.
525TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
526 struct TestData {
527 const std::string encoding;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01528 const std::u16string search_term;
[email protected]ddd231e2010-06-29 20:35:19529 const std::string url;
[email protected]d82443b2009-01-15 19:54:56530 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27531 } test_data[] = {
Alexander Yashkin874b919d2018-02-17 15:11:18532 {"BIG5", base::WideToUTF16(L"\x60BD"),
533 "http://foo/?{searchTerms}{inputEncoding}", "http://foo/?%B1~BIG5"},
534 {"UTF-8", ASCIIToUTF16("blah"),
535 "http://foo/?{searchTerms}{inputEncoding}", "http://foo/?blahUTF-8"},
536 {"Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
537 "http://foo/{searchTerms}/bar", "http://foo/%82%A0/bar"},
538 {"Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
539 "http://foo/{searchTerms}/bar", "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56540 };
[email protected]573889f22012-04-07 01:31:54541 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02542 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54543 data.SetURL(test_data[i].url);
544 data.input_encodings.clear();
545 data.input_encodings.push_back(test_data[i].encoding);
[email protected]168d08722014-06-18 07:13:28546 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19547 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
548 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04549 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19550 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
551 search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04552 ASSERT_TRUE(result.is_valid());
553 EXPECT_EQ(test_data[i].expected_result, result.spec());
554 }
555}
556
Alexander Yashkin874b919d2018-02-17 15:11:18557// Test that encoding with several optional codepages works as intended.
558// Codepages are tried in order, fallback is UTF-8.
559TEST_F(TemplateURLTest, ReplaceSearchTermsMultipleEncodings) {
560 struct TestData {
561 const std::vector<std::string> encodings;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01562 const std::u16string search_term;
Alexander Yashkin874b919d2018-02-17 15:11:18563 const std::string url;
564 const std::string expected_result;
565 } test_data[] = {
566 // First and third encodings are valid. First is used.
567 {{"windows-1251", "cp-866", "UTF-8"},
568 base::UTF8ToUTF16("\xD1\x8F"),
569 "http://foo/?{searchTerms}{inputEncoding}",
570 "http://foo/?%FFwindows-1251"},
571 // Second and third encodings are valid, second is used.
572 {{"cp-866", "GB2312", "UTF-8"},
573 base::UTF8ToUTF16("\xE7\x8B\x97"),
574 "http://foo/?{searchTerms}{inputEncoding}",
575 "http://foo/?%B9%B7GB2312"},
576 // Second and third encodings are valid in another order, second is used.
577 {{"cp-866", "UTF-8", "GB2312"},
578 base::UTF8ToUTF16("\xE7\x8B\x97"),
579 "http://foo/?{searchTerms}{inputEncoding}",
580 "http://foo/?%E7%8B%97UTF-8"},
581 // Both encodings are invalid, fallback to UTF-8.
582 {{"cp-866", "windows-1251"},
583 base::UTF8ToUTF16("\xE7\x8B\x97"),
584 "http://foo/?{searchTerms}{inputEncoding}",
585 "http://foo/?%E7%8B%97UTF-8"},
586 // No encodings are given, fallback to UTF-8.
587 {{},
588 base::UTF8ToUTF16("\xE7\x8B\x97"),
589 "http://foo/?{searchTerms}{inputEncoding}",
590 "http://foo/?%E7%8B%97UTF-8"},
591 };
592
593 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02594 for (size_t i = 0; i < base::size(test_data); ++i) {
Alexander Yashkin874b919d2018-02-17 15:11:18595 data.SetURL(test_data[i].url);
596 data.input_encodings = test_data[i].encodings;
597 TemplateURL url(data);
598 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
599 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
600 GURL result(url.url_ref().ReplaceSearchTerms(
601 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
602 search_terms_data_));
603 ASSERT_TRUE(result.is_valid());
604 EXPECT_EQ(test_data[i].expected_result, result.spec());
605 }
606}
607
[email protected]bca359b2012-06-24 07:53:04608// Tests replacing assisted query stats (AQS) in various scenarios.
609TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
610 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01611 const std::u16string search_term;
[email protected]bca359b2012-06-24 07:53:04612 const std::string aqs;
613 const std::string base_url;
614 const std::string url;
615 const std::string expected_result;
616 } test_data[] = {
617 // No HTTPS, no AQS.
618 { ASCIIToUTF16("foo"),
619 "chrome.0.0l6",
620 "http://foo/",
621 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
622 "http://foo/?foo" },
623 // HTTPS available, AQS should be replaced.
624 { ASCIIToUTF16("foo"),
625 "chrome.0.0l6",
626 "https://foo/",
627 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
628 "https://foo/?fooaqs=chrome.0.0l6&" },
629 // HTTPS available, however AQS is empty.
630 { ASCIIToUTF16("foo"),
631 "",
632 "https://foo/",
633 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
634 "https://foo/?foo" },
635 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
636 { ASCIIToUTF16("foo"),
637 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44638 "http://www.google.com",
[email protected]bca359b2012-06-24 07:53:04639 "http://foo?{searchTerms}{google:assistedQueryStats}",
640 "http://foo/?foo" },
641 // A non-Google search provider with HTTPS should allow AQS.
642 { ASCIIToUTF16("foo"),
643 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44644 "https://www.google.com",
[email protected]bca359b2012-06-24 07:53:04645 "https://foo?{searchTerms}{google:assistedQueryStats}",
646 "https://foo/?fooaqs=chrome.0.0l6&" },
647 };
648 TemplateURLData data;
649 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02650 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04651 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28652 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19653 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
654 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04655 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
656 search_terms_args.assisted_query_stats = test_data[i].aqs;
[email protected]798baa8e2014-06-20 05:42:44657 search_terms_data_.set_google_base_url(test_data[i].base_url);
[email protected]ce7ee5f2014-06-16 23:41:19658 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
659 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27660 ASSERT_TRUE(result.is_valid());
661 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56662 }
663}
664
[email protected]00790562012-12-14 09:57:16665// Tests replacing cursor position.
666TEST_F(TemplateURLTest, ReplaceCursorPosition) {
667 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01668 const std::u16string search_term;
[email protected]00790562012-12-14 09:57:16669 size_t cursor_position;
670 const std::string url;
671 const std::string expected_result;
672 } test_data[] = {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01673 {ASCIIToUTF16("foo"), std::u16string::npos,
674 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
675 "http://www.google.com/?foo&"},
676 {ASCIIToUTF16("foo"), 2,
677 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
678 "http://www.google.com/?foo&cp=2&"},
679 {ASCIIToUTF16("foo"), 15,
680 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
681 "http://www.google.com/?foo&cp=15&"},
[email protected]00790562012-12-14 09:57:16682 };
683 TemplateURLData data;
684 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02685 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]00790562012-12-14 09:57:16686 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28687 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19688 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
689 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]00790562012-12-14 09:57:16690 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
691 search_terms_args.cursor_position = test_data[i].cursor_position;
[email protected]ce7ee5f2014-06-16 23:41:19692 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
693 search_terms_data_));
[email protected]00790562012-12-14 09:57:16694 ASSERT_TRUE(result.is_valid());
695 EXPECT_EQ(test_data[i].expected_result, result.spec());
696 }
697}
698
[email protected]420472b22014-06-10 13:34:43699// Tests replacing input type (&oit=).
700TEST_F(TemplateURLTest, ReplaceInputType) {
701 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01702 const std::u16string search_term;
Steven Holte3696c9412017-08-24 18:38:03703 metrics::OmniboxInputType input_type;
[email protected]420472b22014-06-10 13:34:43704 const std::string url;
705 const std::string expected_result;
706 } test_data[] = {
Steven Holte3696c9412017-08-24 18:38:03707 {ASCIIToUTF16("foo"), metrics::OmniboxInputType::UNKNOWN,
708 "{google:baseURL}?{searchTerms}&{google:inputType}",
709 "http://www.google.com/?foo&oit=1&"},
710 {ASCIIToUTF16("foo"), metrics::OmniboxInputType::URL,
711 "{google:baseURL}?{searchTerms}&{google:inputType}",
712 "http://www.google.com/?foo&oit=3&"},
[email protected]420472b22014-06-10 13:34:43713 };
[email protected]420472b22014-06-10 13:34:43714 TemplateURLData data;
715 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02716 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]420472b22014-06-10 13:34:43717 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28718 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19719 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
720 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43721 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
722 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19723 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
724 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43725 ASSERT_TRUE(result.is_valid());
726 EXPECT_EQ(test_data[i].expected_result, result.spec());
727 }
728}
729
Tommy C. Li9a180482019-06-27 20:49:00730// Tests replacing omnibox focus type (&oft=).
731TEST_F(TemplateURLTest, ReplaceOmniboxFocusType) {
732 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01733 const std::u16string search_term;
Tommy Li5e883512020-08-07 19:32:04734 OmniboxFocusType focus_type;
Tommy C. Li9a180482019-06-27 20:49:00735 const std::string url;
736 const std::string expected_result;
737 } test_data[] = {
Tommy Li5e883512020-08-07 19:32:04738 {ASCIIToUTF16("foo"), OmniboxFocusType::DEFAULT,
Tommy C. Li9a180482019-06-27 20:49:00739 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
740 "http://www.google.com/?foo&"},
Tommy Li5e883512020-08-07 19:32:04741 {ASCIIToUTF16("foo"), OmniboxFocusType::ON_FOCUS,
Tommy C. Li9a180482019-06-27 20:49:00742 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
743 "http://www.google.com/?foo&oft=1&"},
Tommy Li5e883512020-08-07 19:32:04744 {ASCIIToUTF16("foo"), OmniboxFocusType::DELETED_PERMANENT_TEXT,
745 "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}",
746 "http://www.google.com/?foo&oft=2&"},
Tommy C. Li9a180482019-06-27 20:49:00747 };
748 TemplateURLData data;
749 data.input_encodings.push_back("UTF-8");
750 for (size_t i = 0; i < base::size(test_data); ++i) {
751 data.SetURL(test_data[i].url);
752 TemplateURL url(data);
753 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
754 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
755 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
Tommy Li5e883512020-08-07 19:32:04756 search_terms_args.focus_type = test_data[i].focus_type;
Tommy C. Li9a180482019-06-27 20:49:00757 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
758 search_terms_data_));
759 ASSERT_TRUE(result.is_valid());
760 EXPECT_EQ(test_data[i].expected_result, result.spec());
761 }
762}
763
Ryan Sturm29853d52020-11-17 02:14:04764// Tests replacing prefetch source (&pf=).
765TEST_F(TemplateURLTest, ReplaceIsPrefetch) {
766 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01767 const std::u16string search_term;
Ryan Sturm29853d52020-11-17 02:14:04768 bool is_prefetch;
769 const std::string url;
770 const std::string expected_result;
771 } test_data[] = {
772 {ASCIIToUTF16("foo"), false,
773 "{google:baseURL}?{searchTerms}&{google:prefetchSource}",
774 "http://www.google.com/?foo&"},
775 {ASCIIToUTF16("foo"), true,
776 "{google:baseURL}?{searchTerms}&{google:prefetchSource}",
777 "http://www.google.com/?foo&pf=cs&"},
778 };
779 TemplateURLData data;
780 data.input_encodings.push_back("UTF-8");
781 for (size_t i = 0; i < base::size(test_data); ++i) {
782 data.SetURL(test_data[i].url);
783 TemplateURL url(data);
784 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
785 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
786 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
787 search_terms_args.is_prefetch = test_data[i].is_prefetch;
788 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
789 search_terms_data_));
790 ASSERT_TRUE(result.is_valid());
791 EXPECT_EQ(test_data[i].expected_result, result.spec());
792 }
793}
794
[email protected]9b9fa672013-11-07 06:04:52795// Tests replacing currentPageUrl.
796TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48797 struct TestData {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01798 const std::u16string search_term;
[email protected]9b9fa672013-11-07 06:04:52799 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48800 const std::string url;
801 const std::string expected_result;
802 } test_data[] = {
803 { ASCIIToUTF16("foo"),
804 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52805 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48806 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
807 { ASCIIToUTF16("foo"),
808 "",
[email protected]9b9fa672013-11-07 06:04:52809 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48810 "http://www.google.com/?foo&" },
811 { ASCIIToUTF16("foo"),
812 "http://g.com/+-/*&=",
[email protected]9b9fa672013-11-07 06:04:52813 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48814 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
815 };
816 TemplateURLData data;
817 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02818 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]800569d2013-05-06 07:38:48819 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28820 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19821 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
822 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]800569d2013-05-06 07:38:48823 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52824 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19825 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
826 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48827 ASSERT_TRUE(result.is_valid());
828 EXPECT_EQ(test_data[i].expected_result, result.spec());
829 }
830}
831
Pavel Yatsuk0d6a7b5c2019-10-09 20:33:44832// Tests appending attribution parameter to queries originating from Play API
833// search engine.
834TEST_F(TemplateURLTest, PlayAPIAttribution) {
835 const struct TestData {
836 const char* url;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01837 std::u16string terms;
Pavel Yatsuk0d6a7b5c2019-10-09 20:33:44838 bool created_from_play_api;
839 const char* output;
840 } test_data[] = {{"http://foo/?q={searchTerms}", ASCIIToUTF16("bar"), false,
841 "http://foo/?q=bar"},
842 {"http://foo/?q={searchTerms}", ASCIIToUTF16("bar"), true,
843 "http://foo/?q=bar&chrome_dse_attribution=1"}};
844 TemplateURLData data;
845 for (size_t i = 0; i < base::size(test_data); ++i) {
846 data.SetURL(test_data[i].url);
847 data.created_from_play_api = test_data[i].created_from_play_api;
848 TemplateURL url(data);
849 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
850 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
851 GURL result(url.url_ref().ReplaceSearchTerms(
852 TemplateURLRef::SearchTermsArgs(test_data[i].terms),
853 search_terms_data_));
854 ASSERT_TRUE(result.is_valid());
855 EXPECT_EQ(test_data[i].output, result.spec());
856 }
857}
858
[email protected]d82443b2009-01-15 19:54:56859TEST_F(TemplateURLTest, Suggestions) {
860 struct TestData {
861 const int accepted_suggestion;
Jan Wilken Dörriefa241ba2021-03-11 17:57:01862 const std::u16string original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56863 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27864 } test_data[] = {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01865 {TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::u16string(),
866 "http://bar/foo?q=foobar"},
867 {TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
868 "http://bar/foo?q=foobar"},
869 {TemplateURLRef::NO_SUGGESTION_CHOSEN, std::u16string(),
870 "http://bar/foo?q=foobar"},
871 {TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
872 "http://bar/foo?q=foobar"},
873 {0, std::u16string(), "http://bar/foo?oq=&q=foobar"},
874 {1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar"},
[email protected]d82443b2009-01-15 19:54:56875 };
[email protected]573889f22012-04-07 01:31:54876 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37877 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
878 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54879 data.input_encodings.push_back("UTF-8");
[email protected]168d08722014-06-18 07:13:28880 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19881 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
882 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Avi Drissman2244c2d2018-12-25 23:08:02883 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04884 TemplateURLRef::SearchTermsArgs search_terms_args(
885 ASCIIToUTF16("foobar"));
886 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
887 search_terms_args.original_query =
888 test_data[i].original_query_for_suggestion;
[email protected]ce7ee5f2014-06-16 23:41:19889 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
890 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27891 ASSERT_TRUE(result.is_valid());
892 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56893 }
894}
895
[email protected]81f808de2009-09-25 01:36:34896TEST_F(TemplateURLTest, RLZ) {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01897 std::u16string rlz_string = search_terms_data_.GetRlzParameterValue(false);
[email protected]d82443b2009-01-15 19:54:56898
[email protected]573889f22012-04-07 01:31:54899 TemplateURLData data;
900 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28901 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19902 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
903 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04904 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19905 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56906 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44907 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
908 result.spec());
[email protected]d82443b2009-01-15 19:54:56909}
910
[email protected]c8ccc41d2014-04-10 04:42:12911TEST_F(TemplateURLTest, RLZFromAppList) {
Jan Wilken Dörriefa241ba2021-03-11 17:57:01912 std::u16string rlz_string = search_terms_data_.GetRlzParameterValue(true);
[email protected]c8ccc41d2014-04-10 04:42:12913
914 TemplateURLData data;
915 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28916 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19917 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
918 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12919 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
Moe Ahmadi4715d9542020-10-21 01:00:32920 args.request_source = TemplateURLRef::CROS_APP_LIST;
[email protected]ce7ee5f2014-06-16 23:41:19921 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12922 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44923 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
924 result.spec());
[email protected]c8ccc41d2014-04-10 04:42:12925}
[email protected]c8ccc41d2014-04-10 04:42:12926
[email protected]d82443b2009-01-15 19:54:56927TEST_F(TemplateURLTest, HostAndSearchTermKey) {
928 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19929 const std::string url;
[email protected]d82443b2009-01-15 19:54:56930 const std::string host;
931 const std::string path;
932 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54933 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50934 {"http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
935 {"http://blah/{searchTerms}", "blah", "", ""},
[email protected]d82443b2009-01-15 19:54:56936
vitbarf2a11562017-05-15 14:09:50937 // No term should result in empty values.
938 {"http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56939
vitbarf2a11562017-05-15 14:09:50940 // Multiple terms should result in empty values.
941 {"http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56942
vitbarf2a11562017-05-15 14:09:50943 // Term in the host shouldn't match.
944 {"http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56945
vitbarf2a11562017-05-15 14:09:50946 {"http://blah/?q={searchTerms}", "blah", "/", "q"},
947 {"https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56948
vitbarf2a11562017-05-15 14:09:50949 // Single term with extra chars in value should match.
950 {"http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56951 };
952
Avi Drissman2244c2d2018-12-25 23:08:02953 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54954 TemplateURLData data;
955 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28956 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19957 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
958 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
959 EXPECT_EQ(test_data[i].search_term_key,
960 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56961 }
962}
963
alexmos294849f2015-03-14 00:55:06964TEST_F(TemplateURLTest, SearchTermKeyLocation) {
965 struct TestData {
966 const std::string url;
967 const url::Parsed::ComponentType location;
968 const std::string path;
vitbarf2a11562017-05-15 14:09:50969 const std::string key;
970 const std::string value_prefix;
971 const std::string value_suffix;
alexmos294849f2015-03-14 00:55:06972 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50973 {"http://blah/{searchTerms}/", url::Parsed::PATH, "", "", "/", "/"},
974 {"http://blah/{searchTerms}", url::Parsed::PATH, "", "", "/", ""},
975 {"http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "", "",
976 "/begin/", "/end"},
977 {"http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY, "/", "q",
978 "", ""},
979 {"http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF, "/", "x",
980 "", ""},
981 {"http://www.example.com/?q=chromium-{searchTerms}@chromium.org/info",
982 url::Parsed::QUERY, "/", "q", "chromium-", "@chromium.org/info"},
alexmos294849f2015-03-14 00:55:06983
vitbarf2a11562017-05-15 14:09:50984 // searchTerms is a key, not a value, so this should result in an empty
985 // value.
986 {"http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x",
987 url::Parsed::QUERY, "", "", "", ""},
alexmos294849f2015-03-14 00:55:06988
vitbarf2a11562017-05-15 14:09:50989 // Multiple search terms should result in empty values.
990 {"http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY, "", "",
991 "", ""},
992 {"http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY, "", "",
993 "", ""},
994 {"http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY, "",
995 "", "", ""},
alexmos294849f2015-03-14 00:55:06996 };
997
Avi Drissman2244c2d2018-12-25 23:08:02998 for (size_t i = 0; i < base::size(test_data); ++i) {
alexmos294849f2015-03-14 00:55:06999 TemplateURLData data;
1000 data.SetURL(test_data[i].url);
1001 TemplateURL url(data);
1002 EXPECT_EQ(test_data[i].location,
1003 url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
1004 EXPECT_EQ(test_data[i].path,
1005 url.url_ref().GetPath(search_terms_data_));
vitbarf2a11562017-05-15 14:09:501006 EXPECT_EQ(test_data[i].key,
1007 url.url_ref().GetSearchTermKey(search_terms_data_));
1008 EXPECT_EQ(test_data[i].value_prefix,
1009 url.url_ref().GetSearchTermValuePrefix(search_terms_data_));
1010 EXPECT_EQ(test_data[i].value_suffix,
1011 url.url_ref().GetSearchTermValueSuffix(search_terms_data_));
alexmos294849f2015-03-14 00:55:061012 }
1013}
1014
[email protected]d82443b2009-01-15 19:54:561015TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
1016 static const struct {
[email protected]ddd231e2010-06-29 20:35:191017 const char* const base_url;
1018 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:561019 } data[] = {
[email protected]014010e2011-10-01 04:12:441020 { "http://google.com/", "http://google.com/complete/", },
1021 { "http://www.google.com/", "http://www.google.com/complete/", },
1022 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
1023 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
1024 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:561025 };
1026
Avi Drissman2244c2d2018-12-25 23:08:021027 for (size_t i = 0; i < base::size(data); ++i)
[email protected]d82443b2009-01-15 19:54:561028 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
1029}
1030
[email protected]81c6ef62010-01-21 09:58:471031TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:191032 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:541033 TemplateURLData data;
1034 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:281035 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471036 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:131037 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:191038 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:471039 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271040 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471041 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1042}
1043
1044TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:201045 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:541046 TemplateURLData data;
1047 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:281048 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471049 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:441050
1051 // By default, TemplateURLRef should not consider itself prepopulated.
1052 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:131053 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:201054 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:441055 EXPECT_TRUE(replacements.empty());
1056
1057 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:201058 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:541059 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:281060 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:151061 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:201062 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:471063 EXPECT_TRUE(replacements.empty());
1064}
1065
1066TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:281067 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:471068 TemplateURLRef::Replacements replacements;
1069 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241070 EXPECT_EQ(std::string(), url.url_ref().ParseURL(std::string(), &replacements,
1071 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471072 EXPECT_TRUE(replacements.empty());
1073 EXPECT_TRUE(valid);
1074}
1075
1076TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:541077 TemplateURLData data;
1078 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:281079 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471080 TemplateURLRef::Replacements replacements;
1081 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241082 EXPECT_EQ(std::string(),
1083 url.url_ref().ParseURL("{", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471084 EXPECT_TRUE(replacements.empty());
1085 EXPECT_FALSE(valid);
1086}
1087
1088TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:541089 TemplateURLData data;
1090 data.SetURL("{}");
[email protected]168d08722014-06-18 07:13:281091 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471092 TemplateURLRef::Replacements replacements;
1093 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241094 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471095 EXPECT_TRUE(replacements.empty());
1096 EXPECT_TRUE(valid);
1097}
1098
1099TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:541100 TemplateURLData data;
1101 data.SetURL("{}{{%s}}");
[email protected]168d08722014-06-18 07:13:281102 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471103 TemplateURLRef::Replacements replacements;
1104 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241105 EXPECT_EQ("{}{}", url.url_ref().ParseURL("{}{{searchTerms}}", &replacements,
1106 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471107 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271108 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471109 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1110 EXPECT_TRUE(valid);
1111}
1112
1113TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:541114 TemplateURLData data;
1115 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:281116 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471117 TemplateURLRef::Replacements replacements;
1118 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241119 EXPECT_EQ("{", url.url_ref().ParseURL("{{searchTerms}", &replacements,
1120 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471121 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271122 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471123 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1124 EXPECT_TRUE(valid);
1125}
[email protected]5b3078752012-10-09 18:54:161126
[email protected]fd6d8822012-12-08 06:56:111127TEST_F(TemplateURLTest, SearchClient) {
1128 const std::string base_url_str("http://google.com/?");
1129 const std::string terms_str("{searchTerms}&{google:searchClient}");
1130 const std::string full_url_str = base_url_str + terms_str;
Jan Wilken Dörriefa241ba2021-03-11 17:57:011131 const std::u16string terms(ASCIIToUTF16(terms_str));
[email protected]798baa8e2014-06-20 05:42:441132 search_terms_data_.set_google_base_url(base_url_str);
[email protected]fd6d8822012-12-08 06:56:111133
1134 TemplateURLData data;
1135 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:281136 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191137 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1138 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111139 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
1140
1141 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:191142 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1143 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111144 ASSERT_TRUE(result.is_valid());
1145 EXPECT_EQ("http://google.com/?foobar&", result.spec());
1146
1147 // Check that the URL is correct when a client is present.
[email protected]798baa8e2014-06-20 05:42:441148 search_terms_data_.set_search_client("search_client");
[email protected]ce7ee5f2014-06-16 23:41:191149 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
1150 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111151 ASSERT_TRUE(result_2.is_valid());
[email protected]798baa8e2014-06-20 05:42:441152 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
[email protected]fd6d8822012-12-08 06:56:111153}
[email protected]fd6d8822012-12-08 06:56:111154
Moe Ahmadi4715d9542020-10-21 01:00:321155TEST_F(TemplateURLTest, SuggestClient) {
1156 const std::string base_url_str("http://google.com/?");
1157 const std::string query_params_str("client={google:suggestClient}");
1158 const std::string full_url_str = base_url_str + query_params_str;
1159 search_terms_data_.set_google_base_url(base_url_str);
1160
1161 TemplateURLData data;
1162 data.SetURL(full_url_str);
1163 TemplateURL url(data);
1164 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1165 ASSERT_FALSE(url.url_ref().SupportsReplacement(search_terms_data_));
1166 TemplateURLRef::SearchTermsArgs search_terms_args;
1167
1168 // Check that the URL is correct when a client is not present.
1169 GURL result(
1170 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_));
1171 ASSERT_TRUE(result.is_valid());
1172 EXPECT_EQ("http://google.com/?client=", result.spec());
1173
1174 // Check that the URL is correct when a client is present.
1175 search_terms_data_.set_suggest_client("suggest_client");
1176 GURL result_2(
1177 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_));
1178 ASSERT_TRUE(result_2.is_valid());
1179 EXPECT_EQ("http://google.com/?client=suggest_client", result_2.spec());
1180
1181 // Check that the URL is correct when a suggest request is made from a
1182 // non-searchbox NTP surface.
1183 search_terms_args.request_source = TemplateURLRef::NON_SEARCHBOX_NTP;
1184 GURL result_3(
1185 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_));
1186 ASSERT_TRUE(result_3.is_valid());
1187 EXPECT_EQ("http://google.com/?client=suggest_client_from_ntp",
1188 result_3.spec());
1189}
1190
[email protected]5b3078752012-10-09 18:54:161191TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
1192 TemplateURLData data;
1193 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161194 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
1195 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281196 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511197 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1198 ASSERT_EQ(3U, url_refs.size());
1199 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1200 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1201 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161202}
1203
1204TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
1205 TemplateURLData data;
1206 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:281207 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511208 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1209 ASSERT_EQ(1U, url_refs.size());
1210 EXPECT_EQ("http://www.google.co.uk/", url_refs[0].GetURL());
[email protected]5b3078752012-10-09 18:54:161211}
1212
1213TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1214 TemplateURLData data;
1215 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161216 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1217 data.alternate_urls.push_back(
1218 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281219 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011220 std::u16string result;
[email protected]5b3078752012-10-09 18:54:161221
1222 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191223 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191224 EXPECT_EQ(ASCIIToUTF16("something"), result);
1225
1226 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471227 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191228 EXPECT_EQ(ASCIIToUTF16("something"), result);
1229
1230 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471231 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191232 EXPECT_EQ(ASCIIToUTF16("something"), result);
1233
1234 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471235 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191236 EXPECT_EQ(ASCIIToUTF16("something"), result);
1237
1238 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191239 GURL("http://google.com/alt/#q=something"),
1240 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191241 EXPECT_EQ(ASCIIToUTF16("something"), result);
1242
1243 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471244 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191245 EXPECT_EQ(ASCIIToUTF16("something"), result);
1246
1247 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471248 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191249 EXPECT_EQ(ASCIIToUTF16("something"), result);
1250
1251 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471252 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161253 EXPECT_EQ(ASCIIToUTF16("something"), result);
1254
1255 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191256 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011257 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161258
1259 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191260 GURL("http://google.ca/?q=something&q=anything"),
1261 search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011262 EXPECT_EQ(std::u16string(), result);
[email protected]67d8b752013-04-03 17:33:271263
1264 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191265 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011266 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161267
[email protected]32e2d81b2012-10-16 19:03:251268 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191269 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
[email protected]32e2d81b2012-10-16 19:03:251270 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:161271
1272 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191273 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011274 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161275
1276 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191277 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161278 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1279
1280 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191281 GURL("http://google.com/alt/?q=123#q=456"),
1282 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161283 EXPECT_EQ(ASCIIToUTF16("456"), result);
1284
1285 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191286 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1287 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161288 EXPECT_EQ(ASCIIToUTF16("123"), result);
1289
1290 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191291 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1292 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161293 EXPECT_EQ(ASCIIToUTF16("789"), result);
1294
1295 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191296 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011297 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161298
1299 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191300 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011301 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161302
1303 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191304 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011305 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161306
1307 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191308 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011309 EXPECT_EQ(std::u16string(), result);
[email protected]5b3078752012-10-09 18:54:161310
1311 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191312 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161313 EXPECT_EQ(ASCIIToUTF16("123"), result);
1314}
[email protected]4076ea62013-01-09 01:47:191315
alexmos294849f2015-03-14 00:55:061316TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1317 TemplateURLData data;
1318 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1319 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011320 std::u16string result;
alexmos294849f2015-03-14 00:55:061321
1322 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1323 GURL("http://term-in-path.com/begin/something/end"),
1324 search_terms_data_, &result));
1325 EXPECT_EQ(ASCIIToUTF16("something"), result);
1326
1327 // "%20" must be converted to space.
1328 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1329 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1330 search_terms_data_, &result));
1331 EXPECT_EQ(ASCIIToUTF16("a b c"), result);
1332
1333 // Plus must not be converted to space.
1334 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1335 GURL("http://term-in-path.com/begin/1+2+3/end"),
1336 search_terms_data_, &result));
1337 EXPECT_EQ(ASCIIToUTF16("1+2+3"), result);
1338
1339 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1340 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011341 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061342
1343 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1344 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011345 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061346
1347 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1348 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011349 EXPECT_EQ(std::u16string(), result);
alexmos294849f2015-03-14 00:55:061350}
1351
vitbar27804d102015-04-15 10:54:411352// Checks that the ExtractSearchTermsFromURL function works correctly
1353// for urls containing non-latin characters in UTF8 encoding.
1354TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
1355 TemplateURLData data;
1356 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1357 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1358 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1359 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011360 std::u16string result;
vitbar27804d102015-04-15 10:54:411361
1362 // Russian text encoded with UTF-8.
1363 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551364 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1365 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
vitbar27804d102015-04-15 10:54:411366 search_terms_data_, &result));
1367 EXPECT_EQ(
1368 base::WideToUTF16(
1369 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1370 L"\x043C\x0438\x0440!"),
1371 result);
1372
1373 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551374 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1375 "%D0%B0"),
vitbar27804d102015-04-15 10:54:411376 search_terms_data_, &result));
1377 EXPECT_EQ(
1378 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1379 result);
1380
1381 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551382 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1383 "%D0%B8%20A"),
vitbar27804d102015-04-15 10:54:411384 search_terms_data_, &result));
1385 EXPECT_EQ(
1386 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1387 result);
1388}
1389
1390// Checks that the ExtractSearchTermsFromURL function works correctly
1391// for urls containing non-latin characters in non-UTF8 encoding.
1392TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) {
1393 TemplateURLData data;
1394 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1395 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1396 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1397 data.input_encodings.push_back("windows-1251");
1398 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011399 std::u16string result;
vitbar27804d102015-04-15 10:54:411400
1401 // Russian text encoded with Windows-1251.
1402 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1403 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1404 "%EC%E8%F0!"),
1405 search_terms_data_, &result));
1406 EXPECT_EQ(
1407 base::WideToUTF16(
1408 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1409 L"\x043C\x0438\x0440!"),
1410 result);
1411
1412 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1413 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1414 search_terms_data_, &result));
1415 EXPECT_EQ(
1416 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1417 result);
1418
1419 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1420 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1421 search_terms_data_, &result));
1422 EXPECT_EQ(
1423 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1424 result);
1425}
1426
jbroman50832022016-04-21 23:53:001427// Checks that the ExtractSearchTermsFromURL function strips constant
1428// prefix/suffix strings from the search terms param.
1429TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) {
1430 TemplateURLData data;
1431 data.alternate_urls.push_back(
1432 "http://www.example.com/?q=chromium-{searchTerms}@chromium.org");
1433 data.alternate_urls.push_back(
1434 "http://www.example.com/chromium-{searchTerms}@chromium.org/info");
1435 TemplateURL url(data);
Jan Wilken Dörriefa241ba2021-03-11 17:57:011436 std::u16string result;
jbroman50832022016-04-21 23:53:001437
1438 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1439 GURL("http://www.example.com/[email protected]"),
1440 search_terms_data_, &result));
1441 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1442
1443 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1444 GURL("http://www.example.com/[email protected]/info"),
1445 search_terms_data_, &result));
1446 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1447
1448 // Don't match if the prefix and suffix aren't there.
1449 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1450 GURL("http://www.example.com/?q=invalid"), search_terms_data_, &result));
jbromane8b8728d2017-04-12 17:29:391451
1452 // Don't match if the prefix and suffix overlap.
1453 TemplateURLData data_with_overlap;
1454 data.alternate_urls.push_back(
1455 "http://www.example.com/?q=goo{searchTerms}oogle");
1456 TemplateURL url_with_overlap(data);
1457 EXPECT_FALSE(url_with_overlap.ExtractSearchTermsFromURL(
1458 GURL("http://www.example.com/?q=google"), search_terms_data_, &result));
jbroman50832022016-04-21 23:53:001459}
1460
[email protected]f62e30f52013-03-23 03:45:151461TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1462 TemplateURLData data;
1463 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]f62e30f52013-03-23 03:45:151464 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1465 data.alternate_urls.push_back(
1466 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281467 TemplateURL url(data);
[email protected]f62e30f52013-03-23 03:45:151468 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1469 GURL result;
1470
1471 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191472 GURL("http://google.com/?q=something"), search_terms,
1473 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101474 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151475
1476 result = GURL("http://should.not.change.com");
1477 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191478 GURL("http://google.ca/?q=something"), search_terms,
1479 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151480 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1481
1482 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191483 GURL("http://google.com/foo/?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("https://google.com/?q=foo"), search_terms,
1488 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101489 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151490
1491 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191492 GURL("http://google.com:8080/?q=foo"), search_terms,
1493 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151494
1495 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191496 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1497 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101498 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result);
[email protected]f62e30f52013-03-23 03:45:151499
1500 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1501 // template_url.cc for details.
1502 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191503 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1504 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101505 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151506
1507 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1508 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191509 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101510 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
[email protected]f62e30f52013-03-23 03:45:151511 result);
1512
1513 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1514 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191515 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151516 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
vitbarf298455d2015-04-21 12:58:101517 "#j=abc&q=Bob+Morane&h=def9"), result);
[email protected]f62e30f52013-03-23 03:45:151518
1519 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191520 GURL("http://google.com/alt/?q="), search_terms,
1521 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151522
1523 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191524 GURL("http://google.com/alt/?#q="), search_terms,
1525 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151526
1527 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191528 GURL("http://google.com/alt/?q=#q="), search_terms,
1529 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151530
1531 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191532 GURL("http://google.com/alt/?q=123#q="), search_terms,
1533 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151534
1535 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191536 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1537 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101538 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151539}
[email protected]56fa29592013-07-02 20:25:531540
alexmos294849f2015-03-14 00:55:061541TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1542 TemplateURLData data;
1543 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1544 TemplateURL url(data);
1545 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1546 GURL result;
1547
1548 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1549 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1550 search_terms_data_, &result));
1551 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1552
1553 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1554 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1555 search_terms_data_, &result));
1556 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1557
1558 result = GURL("http://should.not.change.com");
1559 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1560 GURL("http://term-in-path.com/about"), search_terms,
1561 search_terms_data_, &result));
1562 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1563}
1564
vitbarf298455d2015-04-21 12:58:101565// Checks that the ReplaceSearchTermsInURL function works correctly
1566// for search terms containing non-latin characters for a search engine
1567// using UTF-8 input encoding.
1568TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) {
1569 TemplateURLData data;
1570 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1571 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1572 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1573 TemplateURL url(data);
1574
1575 // Russian text which will be encoded with UTF-8.
1576 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1577 L"\x0442\x0435\x043A\x0441\x0442"));
1578 GURL result;
1579
1580 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1581 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_,
1582 &result));
1583 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1584 result);
1585
1586 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1587 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_,
1588 &result));
1589 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1590 result);
1591
1592 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1593 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_,
1594 &result));
1595 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1596 result);
1597}
1598
1599// Checks that the ReplaceSearchTermsInURL function works correctly
1600// for search terms containing non-latin characters for a search engine
1601// using non UTF-8 input encoding.
1602TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) {
1603 TemplateURLData data;
1604 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1605 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1606 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1607 data.input_encodings.push_back("windows-1251");
1608 TemplateURL url(data);
1609
1610 // Russian text which will be encoded with Windows-1251.
1611 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1612 L"\x0442\x0435\x043A\x0441\x0442"));
1613 GURL result;
1614
1615 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1616 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_,
1617 &result));
1618 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1619 result);
1620
1621 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1622 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_,
1623 &result));
1624 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1625 result);
1626
1627 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1628 GURL("http://windows-1251.ru/path/a%20b"), search_terms,
1629 search_terms_data_, &result));
1630 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1631 result);
1632}
1633
Mark Pearson247fb8a2018-08-30 05:12:261634// Test the |additional_query_params| field of SearchTermsArgs.
[email protected]621ade062013-10-28 06:27:431635TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431636 TemplateURLData data;
1637 // Pick a URL with replacements before, during, and after the query, to ensure
1638 // we don't goof up any of them.
1639 data.SetURL("{google:baseURL}search?q={searchTerms}"
1640 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281641 TemplateURL url(data);
[email protected]621ade062013-10-28 06:27:431642
Mark Pearson247fb8a2018-08-30 05:12:261643 // Baseline: no |additional_query_params| field.
[email protected]621ade062013-10-28 06:27:431644 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1645 search_terms.original_query = ASCIIToUTF16("def");
1646 search_terms.accepted_suggestion = 0;
1647 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191648 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431649
Mark Pearson247fb8a2018-08-30 05:12:261650 // Set the |additional_query_params|.
1651 search_terms.additional_query_params = "pq=xyz";
[email protected]621ade062013-10-28 06:27:431652 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191653 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431654
Mark Pearson247fb8a2018-08-30 05:12:261655 // Add |append_extra_query_params_from_command_line| into the mix, and ensure
1656 // it works.
1657 search_terms.append_extra_query_params_from_command_line = true;
avi1772c1a2014-12-22 22:42:331658 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]621ade062013-10-28 06:27:431659 switches::kExtraSearchQueryParams, "a=b");
1660 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191661 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431662}
1663
Mark Pearson247fb8a2018-08-30 05:12:261664// Test the |search_terms.append_extra_query_params_from_command_line| field of
1665// SearchTermsArgs.
[email protected]56fa29592013-07-02 20:25:531666TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531667 TemplateURLData data;
1668 // Pick a URL with replacements before, during, and after the query, to ensure
1669 // we don't goof up any of them.
1670 data.SetURL("{google:baseURL}search?q={searchTerms}"
1671 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281672 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531673
Mark Pearson247fb8a2018-08-30 05:12:261674 // Baseline: no command-line args, no
1675 // |search_terms.append_extra_query_params_from_command_line| flag.
[email protected]56fa29592013-07-02 20:25:531676 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1677 search_terms.original_query = ASCIIToUTF16("def");
1678 search_terms.accepted_suggestion = 0;
1679 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191680 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531681
1682 // Set the flag. Since there are no command-line args, this should have no
1683 // effect.
Mark Pearson247fb8a2018-08-30 05:12:261684 search_terms.append_extra_query_params_from_command_line = true;
[email protected]56fa29592013-07-02 20:25:531685 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191686 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531687
1688 // Now append the command-line arg. This should be inserted into the query.
avi1772c1a2014-12-22 22:42:331689 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]56fa29592013-07-02 20:25:531690 switches::kExtraSearchQueryParams, "a=b");
1691 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191692 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531693
1694 // Turn off the flag. Now the command-line arg should be ignored again.
Mark Pearson247fb8a2018-08-30 05:12:261695 search_terms.append_extra_query_params_from_command_line = false;
[email protected]56fa29592013-07-02 20:25:531696 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191697 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531698}
[email protected]d5015ca2013-08-08 22:04:181699
1700// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171701TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181702 TemplateURLData data;
1703 data.input_encodings.push_back("UTF-8");
1704 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281705 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191706 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1707 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]d5015ca2013-08-08 22:04:181708 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1709
[email protected]ce7ee5f2014-06-16 23:41:191710 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1711 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181712 EXPECT_EQ("http://www.google.com/?q=foo", result);
1713
[email protected]332d17d22014-06-20 16:56:031714 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191715 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1716 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181717 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1718
1719 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031720 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191721 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1722 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181723 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1724}
[email protected]2328ee22013-08-08 23:00:191725
1726// Test the IsSearchResults function.
1727TEST_F(TemplateURLTest, IsSearchResults) {
1728 TemplateURLData data;
1729 data.SetURL("http://bar/search?q={searchTerms}");
[email protected]2767c0fd2013-08-16 17:44:161730 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191731 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1732 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1733 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1734 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281735 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191736
1737 const struct {
1738 const char* const url;
1739 bool result;
1740 } url_data[] = {
1741 { "http://bar/search?q=foo&oq=foo", true, },
1742 { "http://bar/?q=foo&oq=foo", true, },
1743 { "http://bar/#output=search&q=foo&oq=foo", true, },
1744 { "http://bar/webhp#q=foo&oq=foo", true, },
1745 { "http://bar/#q=foo&oq=foo", true, },
1746 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1747 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1748 { "http://bar/", false, },
1749 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161750 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191751 };
1752
Avi Drissman2244c2d2018-12-25 23:08:021753 for (size_t i = 0; i < base::size(url_data); ++i) {
[email protected]2328ee22013-08-08 23:00:191754 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191755 search_provider.IsSearchURL(GURL(url_data[i].url),
1756 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191757 }
1758}
[email protected]a048de8a2013-10-02 18:30:061759
jdonnelly41c5b46a2015-07-10 21:24:381760TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) {
[email protected]9d70de12014-05-10 02:15:311761 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441762 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311763 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1764
[email protected]168d08722014-06-18 07:13:281765 TemplateURL url(data);
[email protected]9d70de12014-05-10 02:15:311766 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191767 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1768 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311769 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1770}
[email protected]20184242014-05-14 02:57:421771
1772TEST_F(TemplateURLTest, SessionToken) {
1773 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441774 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421775 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1776
[email protected]168d08722014-06-18 07:13:281777 TemplateURL url(data);
[email protected]20184242014-05-14 02:57:421778 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1779 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191780 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1781 search_terms_data_);
[email protected]20184242014-05-14 02:57:421782 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1783
[email protected]168d08722014-06-18 07:13:281784 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421785 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191786 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1787 search_terms_data_);
[email protected]20184242014-05-14 02:57:421788 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1789}
[email protected]448b17f52014-06-13 01:08:031790
1791TEST_F(TemplateURLTest, ContextualSearchParameters) {
1792 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441793 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031794 data.SetURL("http://bar/_/contextualsearch?"
1795 "{google:contextualSearchVersion}"
1796 "{google:contextualSearchContextData}");
1797
[email protected]168d08722014-06-18 07:13:281798 TemplateURL url(data);
[email protected]448b17f52014-06-13 01:08:031799 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191800 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1801 search_terms_data_);
donndbb98274e2016-11-01 21:04:401802 EXPECT_EQ("http://bar/_/contextualsearch?", result);
[email protected]448b17f52014-06-13 01:08:031803
Donn Denman01b1bf72018-08-14 16:25:201804 // Test the current common case, which uses no home country or previous
1805 // event.
1806 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
Donn Denman97be51df2020-06-24 01:23:191807 2, 1, std::string(), 0, 0, false, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391808 std::string(), std::string());
[email protected]448b17f52014-06-13 01:08:031809 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191810 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1811 search_terms_data_);
donndd9fc4d92016-09-16 00:30:491812 EXPECT_EQ(
1813 "http://bar/_/contextualsearch?"
donnd0bc73292017-01-17 22:07:561814 "ctxs=2&"
donndd9fc4d92016-09-16 00:30:491815 "ctxsl_coca=1",
1816 result);
donndbb98274e2016-11-01 21:04:401817
Donn Denman01b1bf72018-08-14 16:25:201818 // Test the home country and non-zero event data case.
donndbb98274e2016-11-01 21:04:401819 search_terms_args.contextual_search_params =
Donn Denmandf4ede0f2020-01-17 23:11:221820 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denman97be51df2020-06-24 01:23:191821 2, 2, "CH", 1657713458, 5, false, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391822 std::string(), std::string());
donndbb98274e2016-11-01 21:04:401823 result =
1824 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1825
1826 EXPECT_EQ(
1827 "http://bar/_/contextualsearch?"
1828 "ctxs=2&"
donnd0bc73292017-01-17 22:07:561829 "ctxsl_coca=2&"
Donn Denman01b1bf72018-08-14 16:25:201830 "ctxs_hc=CH&"
1831 "ctxsl_pid=1657713458&"
1832 "ctxsl_per=5",
donndbb98274e2016-11-01 21:04:401833 result);
Donn Denmandf4ede0f2020-01-17 23:11:221834
1835 // Test exact-search.
1836 search_terms_args.contextual_search_params =
1837 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denman97be51df2020-06-24 01:23:191838 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391839 std::string(), std::string());
Donn Denmandf4ede0f2020-01-17 23:11:221840 result =
1841 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
Donn Denmandf4ede0f2020-01-17 23:11:221842 // Find our param.
1843 size_t found_pos = result.find("ctxsl_exact=1");
1844 EXPECT_NE(found_pos, std::string::npos);
Donn Denman7a241012020-01-31 18:33:131845
1846 // Test source and target languages.
1847 search_terms_args.contextual_search_params =
1848 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
Donn Denmand638f4602020-11-03 21:05:391849 2, 1, std::string(), 0, 0, true, "es", "de", std::string(),
1850 std::string());
Donn Denman7a241012020-01-31 18:33:131851 result =
1852 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1853 // Find our params.
1854 size_t source_pos = result.find("tlitesl=es");
1855 EXPECT_NE(source_pos, std::string::npos);
1856 size_t target_pos = result.find("tlitetl=de");
1857 EXPECT_NE(target_pos, std::string::npos);
Donn Denman97be51df2020-06-24 01:23:191858
1859 // Test fluent languages.
1860 search_terms_args.contextual_search_params =
1861 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
1862 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
Donn Denmand638f4602020-11-03 21:05:391863 "es,de", std::string());
Donn Denman97be51df2020-06-24 01:23:191864 result =
1865 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1866 // Find our param. These may actually be URL encoded.
1867 size_t fluent_pos = result.find("&ctxs_fls=es,de");
1868 EXPECT_NE(fluent_pos, std::string::npos);
Donn Denmand638f4602020-11-03 21:05:391869
1870 // Test Related Searches.
1871 search_terms_args.contextual_search_params =
1872 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(
1873 2, 1, std::string(), 0, 0, true, std::string(), std::string(),
1874 std::string(), "1RbCu");
1875 result =
1876 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1877 // Find our param.
1878 size_t ctxsl_rs_pos = result.find("&ctxsl_rs=1RbCu");
1879 EXPECT_NE(ctxsl_rs_pos, std::string::npos);
[email protected]448b17f52014-06-13 01:08:031880}
[email protected]44ccc9f2014-06-20 17:36:211881
1882TEST_F(TemplateURLTest, GenerateKeyword) {
1883 ASSERT_EQ(ASCIIToUTF16("foo"),
jshin1fb76462016-04-05 22:13:031884 TemplateURL::GenerateKeyword(GURL("http://foo")));
Emily Stark0fb634f82020-07-14 18:00:391885 ASSERT_EQ(ASCIIToUTF16("foo."),
1886 TemplateURL::GenerateKeyword(GURL("http://foo.")));
1887 // www. should be stripped for a public hostname but not a private/intranet
1888 // hostname.
1889 ASSERT_EQ(ASCIIToUTF16("google.com"),
1890 TemplateURL::GenerateKeyword(GURL("http://www.google.com")));
1891 ASSERT_EQ(ASCIIToUTF16("www.foo"),
jshin1fb76462016-04-05 22:13:031892 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
[email protected]44ccc9f2014-06-20 17:36:211893 // Make sure we don't get a trailing '/'.
jshin1fb76462016-04-05 22:13:031894 ASSERT_EQ(ASCIIToUTF16("blah"),
1895 TemplateURL::GenerateKeyword(GURL("http://blah/")));
[email protected]44ccc9f2014-06-20 17:36:211896 // Don't generate the empty string.
Emily Stark0fb634f82020-07-14 18:00:391897 ASSERT_EQ(ASCIIToUTF16("www."),
jshin1fb76462016-04-05 22:13:031898 TemplateURL::GenerateKeyword(GURL("http://www.")));
alshabaline6212ac2015-07-23 08:03:001899 ASSERT_EQ(
1900 base::UTF8ToUTF16("\xd0\xb0\xd0\xb1\xd0\xb2"),
jshin1fb76462016-04-05 22:13:031901 TemplateURL::GenerateKeyword(GURL("http://xn--80acd")));
a-v-y81695d0d2017-04-20 08:22:221902
1903 // Generated keywords must always be in lowercase, because TemplateURLs always
1904 // converts keywords to lowercase in its constructor and TemplateURLService
1905 // stores TemplateURLs in maps using keyword as key.
1906 EXPECT_TRUE(IsLowerCase(TemplateURL::GenerateKeyword(GURL("http://BLAH/"))));
1907 EXPECT_TRUE(IsLowerCase(
1908 TemplateURL::GenerateKeyword(GURL("http://embeddedhtml.<head>/"))));
[email protected]44ccc9f2014-06-20 17:36:211909}
1910
1911TEST_F(TemplateURLTest, GenerateSearchURL) {
1912 struct GenerateSearchURLCase {
1913 const char* test_name;
1914 const char* url;
1915 const char* expected;
1916 } generate_url_cases[] = {
1917 { "invalid URL", "foo{searchTerms}", "" },
1918 { "URL with no replacements", "http://foo/", "http://foo/" },
1919 { "basic functionality", "http://foo/{searchTerms}",
1920 "http://foo/blah.blah.blah.blah.blah" }
1921 };
1922
Avi Drissman2244c2d2018-12-25 23:08:021923 for (size_t i = 0; i < base::size(generate_url_cases); ++i) {
[email protected]44ccc9f2014-06-20 17:36:211924 TemplateURLData data;
1925 data.SetURL(generate_url_cases[i].url);
1926 TemplateURL t_url(data);
1927 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1928 generate_url_cases[i].expected)
1929 << generate_url_cases[i].test_name << " failed.";
1930 }
1931}
[email protected]9e9130ce2014-06-23 23:20:511932
1933TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1934 TemplateURLData data;
1935 search_terms_data_.set_google_base_url("http://bar/");
1936 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1937
1938 TemplateURL url(data);
1939 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1940 search_terms_args.prefetch_query = "full query text";
1941 search_terms_args.prefetch_query_type = "2338";
1942 std::string result =
1943 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1944 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1945 result);
1946
1947 TemplateURL url2(data);
1948 search_terms_args.prefetch_query.clear();
1949 search_terms_args.prefetch_query_type.clear();
1950 result =
1951 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1952 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1953}
vitbar5bd8c252016-01-29 18:44:511954
1955// Tests that TemplateURL works correctly after changing the Google base URL
1956// and invalidating cached values.
1957TEST_F(TemplateURLTest, InvalidateCachedValues) {
1958 TemplateURLData data;
1959 data.SetURL("{google:baseURL}search?q={searchTerms}");
1960 data.suggestions_url = "{google:baseSuggestURL}search?q={searchTerms}";
vitbar5bd8c252016-01-29 18:44:511961 data.image_url = "{google:baseURL}searchbyimage/upload";
1962 data.new_tab_url = "{google:baseURL}_/chrome/newtab";
1963 data.contextual_search_url = "{google:baseURL}_/contextualsearch";
1964 data.alternate_urls.push_back("{google:baseURL}s#q={searchTerms}");
1965 TemplateURL url(data);
1966 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("X"));
Jan Wilken Dörriefa241ba2021-03-11 17:57:011967 std::u16string search_terms;
vitbar5bd8c252016-01-29 18:44:511968
1969 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1970 EXPECT_EQ("http://www.google.com/search?q=X",
1971 url.url_ref().ReplaceSearchTerms(search_terms_args,
1972 search_terms_data_));
1973 EXPECT_EQ("http://www.google.com/s#q=X",
1974 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1975 search_terms_data_));
1976 EXPECT_EQ("http://www.google.com/search?q=X",
1977 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1978 search_terms_data_));
1979 EXPECT_EQ("http://www.google.com/complete/search?q=X",
1980 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1981 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:511982 EXPECT_EQ("http://www.google.com/searchbyimage/upload",
1983 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1984 search_terms_data_));
1985 EXPECT_EQ("http://www.google.com/_/chrome/newtab",
1986 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1987 search_terms_data_));
1988 EXPECT_EQ("http://www.google.com/_/contextualsearch",
1989 url.contextual_search_url_ref().ReplaceSearchTerms(
1990 search_terms_args, search_terms_data_));
1991
1992 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1993 GURL("http://www.google.com/search?q=Y+Z"),
1994 search_terms_data_, &search_terms));
1995 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
1996 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1997 GURL("http://www.google.com/s#q=123"),
1998 search_terms_data_, &search_terms));
1999 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
2000
2001 search_terms_data_.set_google_base_url("https://www.foo.org/");
2002 url.InvalidateCachedValues();
2003
2004 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
2005 EXPECT_EQ("https://www.foo.org/search?q=X",
2006 url.url_ref().ReplaceSearchTerms(search_terms_args,
2007 search_terms_data_));
2008 EXPECT_EQ("https://www.foo.org/s#q=X",
2009 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
2010 search_terms_data_));
2011 EXPECT_EQ("https://www.foo.org/search?q=X",
2012 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
2013 search_terms_data_));
2014 EXPECT_EQ("https://www.foo.org/complete/search?q=X",
2015 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
2016 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:512017 EXPECT_EQ("https://www.foo.org/searchbyimage/upload",
2018 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
2019 search_terms_data_));
2020 EXPECT_EQ("https://www.foo.org/_/chrome/newtab",
2021 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
2022 search_terms_data_));
2023 EXPECT_EQ("https://www.foo.org/_/contextualsearch",
2024 url.contextual_search_url_ref().ReplaceSearchTerms(
2025 search_terms_args, search_terms_data_));
2026
2027 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
2028 GURL("https://www.foo.org/search?q=Y+Z"),
2029 search_terms_data_, &search_terms));
2030 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
2031 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
2032 GURL("https://www.foo.org/s#q=123"),
2033 search_terms_data_, &search_terms));
2034 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
2035
2036 search_terms_data_.set_google_base_url("http://www.google.com/");
2037}
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002038
2039// Tests the use of wildcards in the path to ensure both extracting search terms
2040// and generating a search URL work correctly.
2041TEST_F(TemplateURLTest, PathWildcard) {
2042 TemplateURLData data;
2043 data.SetURL(
2044 "https://www.google.com/search{google:pathWildcard}?q={searchTerms}");
2045 TemplateURL url(data);
2046
2047 // Test extracting search terms from a URL.
Jan Wilken Dörriefa241ba2021-03-11 17:57:012048 std::u16string search_terms;
Troy Hildebrandt47ac4d8b2018-05-10 22:40:002049 url.ExtractSearchTermsFromURL(GURL("https://www.google.com/search?q=testing"),
2050 search_terms_data_, &search_terms);
2051 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
2052 url.ExtractSearchTermsFromURL(
2053 GURL("https://www.google.com/search;_this_is_a_test;_?q=testing"),
2054 search_terms_data_, &search_terms);
2055 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
2056
2057 // Tests overlapping prefix/suffix.
2058 data.SetURL(
2059 "https://www.google.com/search{google:pathWildcard}rch?q={searchTerms}");
2060 TemplateURL overlap_url(data);
2061 overlap_url.ExtractSearchTermsFromURL(
2062 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
2063 &search_terms);
2064 EXPECT_TRUE(search_terms.empty());
2065
2066 // Tests wildcard at beginning of path so we only have a suffix.
2067 data.SetURL(
2068 "https://www.google.com/{google:pathWildcard}rch?q={searchTerms}");
2069 TemplateURL suffix_url(data);
2070 suffix_url.ExtractSearchTermsFromURL(
2071 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
2072 &search_terms);
2073 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
2074
2075 // Tests wildcard between prefix/suffix.
2076 overlap_url.ExtractSearchTermsFromURL(
2077 GURL("https://www.google.com/search_testing_rch?q=testing"),
2078 search_terms_data_, &search_terms);
2079 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
2080
2081 // Test generating a URL.
2082 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
2083 GURL generated_url;
2084 url.ReplaceSearchTermsInURL(url.GenerateSearchURL(search_terms_data_),
2085 search_terms_args, search_terms_data_,
2086 &generated_url);
2087 EXPECT_EQ("https://www.google.com/search?q=foo", generated_url.spec());
2088}