blob: 8a4efdf3f5d477557ceb26eb979a1618a740920b [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
[email protected]12bd05872009-03-17 19:25:067#include "base/base_paths.h"
[email protected]56fa29592013-07-02 20:25:538#include "base/command_line.h"
a-v-y81695d0d2017-04-20 08:22:229#include "base/i18n/case_conversion.h"
Avi Drissman2244c2d2018-12-25 23:08:0210#include "base/stl_util.h"
[email protected]2f3bc6512013-08-28 03:56:2711#include "base/strings/string_number_conversions.h"
[email protected]24a555b62013-06-10 22:01:1712#include "base/strings/string_util.h"
[email protected]e309f312013-06-07 21:50:0813#include "base/strings/utf_string_conversions.h"
John Abd-El-Malek9cf3d7f02018-07-27 02:40:3914#include "components/google/core/common/google_util.h"
[email protected]5c3c6e482014-06-23 09:47:1815#include "components/search_engines/search_engines_switches.h"
[email protected]798baa8e2014-06-20 05:42:4416#include "components/search_engines/search_terms_data.h"
[email protected]d550cb02014-06-25 06:48:1117#include "components/search_engines/template_url.h"
hashimoto3c831812014-08-25 07:40:2318#include "components/search_engines/testing_search_terms_data.h"
a-v-y02d3f50b2017-02-16 12:03:5519#include "net/base/url_util.h"
[email protected]d82443b2009-01-15 19:54:5620#include "testing/gtest/include/gtest/gtest.h"
Steven Holtef9d5ed62017-10-21 02:02:3021#include "third_party/metrics_proto/omnibox_event.pb.h"
22#include "third_party/metrics_proto/omnibox_input_type.pb.h"
[email protected]d82443b2009-01-15 19:54:5623
[email protected]f911df52013-12-24 23:24:2324using base::ASCIIToUTF16;
25
a-v-y81695d0d2017-04-20 08:22:2226namespace {
27bool IsLowerCase(const base::string16& str) {
28 return str == base::i18n::ToLower(str);
29}
30}
31
[email protected]583844c2011-08-27 00:38:3532class TemplateURLTest : public testing::Test {
[email protected]d82443b2009-01-15 19:54:5633 public:
[email protected]798baa8e2014-06-20 05:42:4434 TemplateURLTest() : search_terms_data_("http://www.google.com/") {}
[email protected]b37bdfe2012-03-16 20:53:2735 void CheckSuggestBaseURL(const std::string& base_url,
36 const std::string& base_suggest_url) const;
[email protected]ce7ee5f2014-06-16 23:41:1937
vitbardeb285392015-02-20 14:02:5538 static void ExpectPostParamIs(
39 const TemplateURLRef::PostParam& param,
40 const std::string& name,
41 const std::string& value,
42 const std::string& content_type = std::string());
43
hashimoto3c831812014-08-25 07:40:2344 TestingSearchTermsData search_terms_data_;
[email protected]d82443b2009-01-15 19:54:5645};
46
[email protected]b37bdfe2012-03-16 20:53:2747void TemplateURLTest::CheckSuggestBaseURL(
48 const std::string& base_url,
49 const std::string& base_suggest_url) const {
hashimoto3c831812014-08-25 07:40:2350 TestingSearchTermsData search_terms_data(base_url);
[email protected]b37bdfe2012-03-16 20:53:2751 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue());
52}
53
vitbardeb285392015-02-20 14:02:5554// static
55void TemplateURLTest::ExpectPostParamIs(const TemplateURLRef::PostParam& param,
56 const std::string& name,
57 const std::string& value,
58 const std::string& content_type) {
59 EXPECT_EQ(name, param.name);
60 EXPECT_EQ(value, param.value);
61 EXPECT_EQ(content_type, param.content_type);
62}
63
[email protected]d82443b2009-01-15 19:54:5664TEST_F(TemplateURLTest, Defaults) {
[email protected]573889f22012-04-07 01:31:5465 TemplateURLData data;
[email protected]573889f22012-04-07 01:31:5466 EXPECT_FALSE(data.safe_for_autoreplace);
67 EXPECT_EQ(0, data.prepopulate_id);
[email protected]d82443b2009-01-15 19:54:5668}
69
70TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]573889f22012-04-07 01:31:5471 TemplateURLData data;
72 data.SetURL("{searchTerms}");
[email protected]168d08722014-06-18 07:13:2873 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1974 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:5675}
76
77TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1978 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1979 const char* url;
[email protected]0085863a2013-12-06 21:19:0380 const base::string16 terms;
[email protected]34e24852012-01-31 18:43:5881 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:1982 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:3083 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1684 "http://foosea%20rch/bar" },
[email protected]400b133f2011-01-19 18:32:3085 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1686 "http://foosea%20rch/bar?boo=abc" },
[email protected]400b133f2011-01-19 18:32:3087 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1688 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]400b133f2011-01-19 18:32:3089 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
[email protected]c31a979c2012-05-31 23:57:1690 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:1991 };
Avi Drissman2244c2d2018-12-25 23:08:0292 for (size_t i = 0; i < base::size(search_term_cases); ++i) {
[email protected]0d2e6a62010-01-15 20:09:1993 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:5494 TemplateURLData data;
95 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:2896 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1997 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
98 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:0499 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19100 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data_));
[email protected]c31a979c2012-05-31 23:57:16101 ASSERT_TRUE(result.is_valid());
102 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:19103 }
[email protected]d82443b2009-01-15 19:54:56104}
105
106TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:54107 TemplateURLData data;
108 data.SetURL("http://foo{searchTerms}{count?}");
[email protected]168d08722014-06-18 07:13:28109 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19110 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
111 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04112 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19113 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56114 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27115 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:56116}
117
118TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:54119 TemplateURLData data;
120 data.SetURL("http://foo{searchTerms}{count}");
[email protected]168d08722014-06-18 07:13:28121 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19122 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
123 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04124 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19125 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56126 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27127 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56128}
129
130TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54131 TemplateURLData data;
132 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]168d08722014-06-18 07:13:28133 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19134 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
135 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04136 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19137 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56138 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27139 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56140}
141
142TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54143 TemplateURLData data;
144 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[email protected]168d08722014-06-18 07:13:28145 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19146 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
147 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04148 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19149 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56150 ASSERT_TRUE(result.is_valid());
[email protected]405aae22012-03-29 20:36:13151 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56152}
153
154TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54155 TemplateURLData data;
156 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[email protected]168d08722014-06-18 07:13:28157 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19158 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
159 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04160 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19161 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56162 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27163 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56164}
165
[email protected]93b29062013-07-12 03:09:09166TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
167 const char kInvalidPostParamsString[] =
168 "unknown_template={UnknownTemplate},bad_value=bad{value},"
169 "{google:sbiSource}";
170 // List all accpectable parameter format in valid_post_params_string. it is
171 // expected like: "name0=,name1=value1,name2={template1}"
172 const char kValidPostParamsString[] =
173 "image_content={google:imageThumbnail},image_url={google:imageURL},"
174 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
[email protected]2f3bc6512013-08-28 03:56:27175 "constant_param=constant,width={google:imageOriginalWidth}";
[email protected]93b29062013-07-12 03:09:09176 const char KImageSearchURL[] = "http://foo.com/sbi";
177
178 TemplateURLData data;
179 data.image_url = KImageSearchURL;
180
181 // Try to parse invalid post parameters.
182 data.image_url_post_params = kInvalidPostParamsString;
[email protected]168d08722014-06-18 07:13:28183 TemplateURL url_bad(data);
[email protected]ce7ee5f2014-06-16 23:41:19184 ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09185 const TemplateURLRef::PostParams& bad_post_params =
186 url_bad.image_url_ref().post_params_;
187 ASSERT_EQ(2U, bad_post_params.size());
vitbardeb285392015-02-20 14:02:55188 ExpectPostParamIs(bad_post_params[0], "unknown_template",
189 "{UnknownTemplate}");
190 ExpectPostParamIs(bad_post_params[1], "bad_value", "bad{value}");
[email protected]93b29062013-07-12 03:09:09191
192 // Try to parse valid post parameters.
193 data.image_url_post_params = kValidPostParamsString;
[email protected]168d08722014-06-18 07:13:28194 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19195 ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_));
196 ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09197
198 // Check term replacement.
199 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
200 search_args.image_thumbnail_content = "dummy-image-thumbnail";
201 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
[email protected]2f3bc6512013-08-28 03:56:27202 search_args.image_original_size = gfx::Size(10, 10);
[email protected]93b29062013-07-12 03:09:09203 // Replacement operation with no post_data buffer should still return
204 // the parsed URL.
hashimoto3c831812014-08-25 07:40:23205 TestingSearchTermsData search_terms_data("http://X");
[email protected]ce7ee5f2014-06-16 23:41:19206 GURL result(url.image_url_ref().ReplaceSearchTerms(
207 search_args, search_terms_data));
[email protected]93b29062013-07-12 03:09:09208 ASSERT_TRUE(result.is_valid());
209 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48210 TemplateURLRef::PostContent post_content;
[email protected]ce7ee5f2014-06-16 23:41:19211 result = GURL(url.image_url_ref().ReplaceSearchTerms(
[email protected]7c2bcc42013-08-01 04:03:48212 search_args, search_terms_data, &post_content));
[email protected]93b29062013-07-12 03:09:09213 ASSERT_TRUE(result.is_valid());
214 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48215 ASSERT_FALSE(post_content.first.empty());
216 ASSERT_FALSE(post_content.second.empty());
[email protected]93b29062013-07-12 03:09:09217
218 // Check parsed result of post parameters.
219 const TemplateURLRef::Replacements& replacements =
220 url.image_url_ref().replacements_;
221 const TemplateURLRef::PostParams& post_params =
222 url.image_url_ref().post_params_;
[email protected]2f3bc6512013-08-28 03:56:27223 EXPECT_EQ(7U, post_params.size());
jdoerrie3feb1852018-10-05 12:16:44224 for (auto i = post_params.begin(); i != post_params.end(); ++i) {
225 auto j = replacements.begin();
[email protected]93b29062013-07-12 03:09:09226 for (; j != replacements.end(); ++j) {
227 if (j->is_post_param && j->index ==
228 static_cast<size_t>(i - post_params.begin())) {
229 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27230 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
vitbardeb285392015-02-20 14:02:55231 ExpectPostParamIs(*i, "width",
232 base::IntToString(
233 search_args.image_original_size.width()));
[email protected]2f3bc6512013-08-28 03:56:27234 break;
[email protected]1020fead2014-06-20 13:40:28235 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE:
vitbardeb285392015-02-20 14:02:55236 ExpectPostParamIs(*i, "sbisrc",
237 search_terms_data.GoogleImageSearchSource());
[email protected]1020fead2014-06-20 13:40:28238 break;
[email protected]93b29062013-07-12 03:09:09239 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
vitbardeb285392015-02-20 14:02:55240 ExpectPostParamIs(*i, "image_content",
241 search_args.image_thumbnail_content,
242 "image/jpeg");
[email protected]93b29062013-07-12 03:09:09243 break;
244 case TemplateURLRef::GOOGLE_IMAGE_URL:
vitbardeb285392015-02-20 14:02:55245 ExpectPostParamIs(*i, "image_url", search_args.image_url.spec());
[email protected]93b29062013-07-12 03:09:09246 break;
247 case TemplateURLRef::LANGUAGE:
vitbardeb285392015-02-20 14:02:55248 ExpectPostParamIs(*i, "language", "en");
[email protected]93b29062013-07-12 03:09:09249 break;
250 default:
251 ADD_FAILURE(); // Should never go here.
252 }
253 break;
254 }
255 }
256 if (j != replacements.end())
257 continue;
vitbardeb285392015-02-20 14:02:55258 if (i->name == "empty_param")
259 ExpectPostParamIs(*i, "empty_param", std::string());
260 else
261 ExpectPostParamIs(*i, "constant_param", "constant");
[email protected]93b29062013-07-12 03:09:09262 }
263}
264
Peter Kastinge59f0c32017-11-29 05:56:22265TEST_F(TemplateURLTest, ImageURLWithGetShouldNotCrash) {
266 TemplateURLData data;
267 data.SetURL("http://foo/?q={searchTerms}&t={google:imageThumbnail}");
268 TemplateURL url(data);
269 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
270 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
271
272 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
273 search_args.image_thumbnail_content = "dummy-image-thumbnail";
274 GURL result(
275 url.url_ref().ReplaceSearchTerms(search_args, search_terms_data_));
276 ASSERT_TRUE(result.is_valid());
277 EXPECT_EQ("http://foo/?q=X&t=dummy-image-thumbnail", result.spec());
278}
279
[email protected]d88cb202011-08-17 20:03:01280// Test that setting the prepopulate ID from TemplateURL causes the stored
281// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44282TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54283 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20284 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]168d08722014-06-18 07:13:28285 TemplateURL url(data);
[email protected]1a257262011-06-28 22:15:44286 TemplateURLRef::Replacements replacements;
287 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:24288 EXPECT_EQ("http://foo{fhqwhgads}bar",
289 url.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements,
290 nullptr, &valid));
[email protected]1a257262011-06-28 22:15:44291 EXPECT_TRUE(replacements.empty());
292 EXPECT_TRUE(valid);
293
[email protected]573889f22012-04-07 01:31:54294 data.prepopulate_id = 123;
[email protected]168d08722014-06-18 07:13:28295 TemplateURL url2(data);
Ivan Kotenkov75b1c3a2017-10-24 14:47:24296 EXPECT_EQ("http://foobar",
297 url2.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements,
298 nullptr, &valid));
[email protected]1a257262011-06-28 22:15:44299 EXPECT_TRUE(replacements.empty());
300 EXPECT_TRUE(valid);
301}
302
vitbar90902c42016-05-11 06:16:55303// Test that setting the prepopulate ID from TemplateURL causes the stored
304// TemplateURLRef to handle parsing the URL parameters differently.
305TEST_F(TemplateURLTest, SetPrepopulatedAndReplace) {
306 TemplateURLData data;
307 data.SetURL("http://foo{fhqwhgads}search/?q={searchTerms}");
308 data.suggestions_url = "http://foo{fhqwhgads}suggest/?q={searchTerms}";
vitbar90902c42016-05-11 06:16:55309 data.image_url = "http://foo{fhqwhgads}image/";
310 data.new_tab_url = "http://foo{fhqwhgads}newtab/";
311 data.contextual_search_url = "http://foo{fhqwhgads}context/";
312 data.alternate_urls.push_back(
313 "http://foo{fhqwhgads}alternate/?q={searchTerms}");
314
315 TemplateURLRef::SearchTermsArgs args(base::ASCIIToUTF16("X"));
316 const SearchTermsData& stdata = search_terms_data_;
317
318 TemplateURL url(data);
319 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
320 url.url_ref().ReplaceSearchTerms(args, stdata));
321 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dalternate/?q=X",
322 url.url_refs()[0].ReplaceSearchTerms(args, stdata));
323 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
324 url.url_refs()[1].ReplaceSearchTerms(args, stdata));
325 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsuggest/?q=X",
326 url.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
vitbar90902c42016-05-11 06:16:55327 EXPECT_EQ("http://foo{fhqwhgads}image/",
328 url.image_url_ref().ReplaceSearchTerms(args, stdata));
329 EXPECT_EQ("http://foo{fhqwhgads}newtab/",
330 url.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
331 EXPECT_EQ("http://foo{fhqwhgads}context/",
332 url.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
333
334 data.prepopulate_id = 123;
335 TemplateURL url2(data);
336 EXPECT_EQ("http://foosearch/?q=X",
337 url2.url_ref().ReplaceSearchTerms(args, stdata));
338 EXPECT_EQ("http://fooalternate/?q=X",
339 url2.url_refs()[0].ReplaceSearchTerms(args, stdata));
340 EXPECT_EQ("http://foosearch/?q=X",
341 url2.url_refs()[1].ReplaceSearchTerms(args, stdata));
342 EXPECT_EQ("http://foosuggest/?q=X",
343 url2.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
vitbar90902c42016-05-11 06:16:55344 EXPECT_EQ("http://fooimage/",
345 url2.image_url_ref().ReplaceSearchTerms(args, stdata));
346 EXPECT_EQ("http://foonewtab/",
347 url2.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
348 EXPECT_EQ("http://foocontext/",
349 url2.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
350}
351
[email protected]d82443b2009-01-15 19:54:56352TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54353 TemplateURLData data;
354 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]168d08722014-06-18 07:13:28355 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19356 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
357 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04358 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19359 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56360 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27361 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56362}
363
364TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54365 TemplateURLData data;
366 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]168d08722014-06-18 07:13:28367 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19368 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
369 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04370 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19371 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56372 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27373 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56374}
375
[email protected]375bd7312010-08-30 22:18:13376TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
377 struct SearchTermsCase {
378 const char* url;
[email protected]0085863a2013-12-06 21:19:03379 const base::string16 terms;
[email protected]375bd7312010-08-30 22:18:13380 const char* output;
381 } search_term_cases[] = {
[email protected]0085863a2013-12-06 21:19:03382 { "{google:baseURL}{language}{searchTerms}", base::string16(),
[email protected]b37bdfe2012-03-16 20:53:27383 "http://example.com/e/en" },
[email protected]0085863a2013-12-06 21:19:03384 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
[email protected]014010e2011-10-01 04:12:44385 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13386 };
387
hashimoto3c831812014-08-25 07:40:23388 TestingSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54389 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02390 for (size_t i = 0; i < base::size(search_term_cases); ++i) {
[email protected]375bd7312010-08-30 22:18:13391 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54392 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:28393 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19394 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data));
395 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data));
396 GURL result(url.url_ref().ReplaceSearchTerms(
Ivan Kotenkov75b1c3a2017-10-24 14:47:24397 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data,
398 nullptr));
[email protected]375bd7312010-08-30 22:18:13399 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27400 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13401 }
402}
403
[email protected]d82443b2009-01-15 19:54:56404TEST_F(TemplateURLTest, URLRefTermToWide) {
405 struct ToWideCase {
406 const char* encoded_search_term;
[email protected]0085863a2013-12-06 21:19:03407 const base::string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56408 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30409 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56410 // Test some big-5 input.
[email protected]f911df52013-12-24 23:24:23411 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56412 // Test some UTF-8 input. We should fall back to this when the encoding
413 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
414 // Big-5 trailing byte.
[email protected]f911df52013-12-24 23:24:23415 {"%e4%bd%a05%e5%a5%bd+to+you",
416 base::WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56417 // Undecodable input should stay escaped.
[email protected]f911df52013-12-24 23:24:23418 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44419 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30420 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44421 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30422 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56423 };
424
[email protected]d82443b2009-01-15 19:54:56425 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54426 TemplateURLData data;
427 data.SetURL("http://foo?q={searchTerms}");
428 data.input_encodings.push_back("big-5");
[email protected]168d08722014-06-18 07:13:28429 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19430 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
431 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Avi Drissman2244c2d2018-12-25 23:08:02432 for (size_t i = 0; i < base::size(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07433 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13434 url.url_ref().SearchTermToString16(
435 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56436 }
437}
438
[email protected]d82443b2009-01-15 19:54:56439TEST_F(TemplateURLTest, DisplayURLToURLRef) {
440 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19441 const std::string url;
[email protected]0085863a2013-12-06 21:19:03442 const base::string16 expected_result;
[email protected]b37bdfe2012-03-16 20:53:27443 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19444 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30445 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19446 { "http://X",
[email protected]400b133f2011-01-19 18:32:30447 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19448 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30449 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19450 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30451 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56452 };
[email protected]573889f22012-04-07 01:31:54453 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02454 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54455 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28456 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19457 EXPECT_EQ(test_data[i].expected_result,
458 url.url_ref().DisplayURL(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27459 EXPECT_EQ(test_data[i].url,
[email protected]ce7ee5f2014-06-16 23:41:19460 TemplateURLRef::DisplayURLToURLRef(
461 url.url_ref().DisplayURL(search_terms_data_)));
[email protected]d82443b2009-01-15 19:54:56462 }
463}
464
465TEST_F(TemplateURLTest, ReplaceSearchTerms) {
466 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19467 const std::string url;
[email protected]d82443b2009-01-15 19:54:56468 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27469 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19470 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56471 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19472 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56473 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19474 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56475 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19476 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56477 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19478 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56479 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19480 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56481 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19482 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56483 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19484 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56485 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19486 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56487 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19488 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56489 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19490 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56491 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19492 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56493 "http://foo/UTF-8a{language}aXa" },
494 };
[email protected]573889f22012-04-07 01:31:54495 TemplateURLData data;
496 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02497 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54498 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28499 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19500 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
501 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27502 std::string expected_result = test_data[i].expected_result;
brettwe6dae462015-06-24 20:54:45503 base::ReplaceSubstringsAfterOffset(
504 &expected_result, 0, "{language}",
505 search_terms_data_.GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04506 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19507 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
508 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27509 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56510 EXPECT_EQ(expected_result, result.spec());
511 }
512}
513
514
515// Tests replacing search terms in various encodings and making sure the
516// generated URL matches the expected value.
517TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
518 struct TestData {
519 const std::string encoding;
[email protected]0085863a2013-12-06 21:19:03520 const base::string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19521 const std::string url;
[email protected]d82443b2009-01-15 19:54:56522 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27523 } test_data[] = {
Alexander Yashkin874b919d2018-02-17 15:11:18524 {"BIG5", base::WideToUTF16(L"\x60BD"),
525 "http://foo/?{searchTerms}{inputEncoding}", "http://foo/?%B1~BIG5"},
526 {"UTF-8", ASCIIToUTF16("blah"),
527 "http://foo/?{searchTerms}{inputEncoding}", "http://foo/?blahUTF-8"},
528 {"Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
529 "http://foo/{searchTerms}/bar", "http://foo/%82%A0/bar"},
530 {"Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
531 "http://foo/{searchTerms}/bar", "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56532 };
[email protected]573889f22012-04-07 01:31:54533 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02534 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54535 data.SetURL(test_data[i].url);
536 data.input_encodings.clear();
537 data.input_encodings.push_back(test_data[i].encoding);
[email protected]168d08722014-06-18 07:13:28538 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19539 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
540 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04541 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19542 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
543 search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04544 ASSERT_TRUE(result.is_valid());
545 EXPECT_EQ(test_data[i].expected_result, result.spec());
546 }
547}
548
Alexander Yashkin874b919d2018-02-17 15:11:18549// Test that encoding with several optional codepages works as intended.
550// Codepages are tried in order, fallback is UTF-8.
551TEST_F(TemplateURLTest, ReplaceSearchTermsMultipleEncodings) {
552 struct TestData {
553 const std::vector<std::string> encodings;
554 const base::string16 search_term;
555 const std::string url;
556 const std::string expected_result;
557 } test_data[] = {
558 // First and third encodings are valid. First is used.
559 {{"windows-1251", "cp-866", "UTF-8"},
560 base::UTF8ToUTF16("\xD1\x8F"),
561 "http://foo/?{searchTerms}{inputEncoding}",
562 "http://foo/?%FFwindows-1251"},
563 // Second and third encodings are valid, second is used.
564 {{"cp-866", "GB2312", "UTF-8"},
565 base::UTF8ToUTF16("\xE7\x8B\x97"),
566 "http://foo/?{searchTerms}{inputEncoding}",
567 "http://foo/?%B9%B7GB2312"},
568 // Second and third encodings are valid in another order, second is used.
569 {{"cp-866", "UTF-8", "GB2312"},
570 base::UTF8ToUTF16("\xE7\x8B\x97"),
571 "http://foo/?{searchTerms}{inputEncoding}",
572 "http://foo/?%E7%8B%97UTF-8"},
573 // Both encodings are invalid, fallback to UTF-8.
574 {{"cp-866", "windows-1251"},
575 base::UTF8ToUTF16("\xE7\x8B\x97"),
576 "http://foo/?{searchTerms}{inputEncoding}",
577 "http://foo/?%E7%8B%97UTF-8"},
578 // No encodings are given, fallback to UTF-8.
579 {{},
580 base::UTF8ToUTF16("\xE7\x8B\x97"),
581 "http://foo/?{searchTerms}{inputEncoding}",
582 "http://foo/?%E7%8B%97UTF-8"},
583 };
584
585 TemplateURLData data;
Avi Drissman2244c2d2018-12-25 23:08:02586 for (size_t i = 0; i < base::size(test_data); ++i) {
Alexander Yashkin874b919d2018-02-17 15:11:18587 data.SetURL(test_data[i].url);
588 data.input_encodings = test_data[i].encodings;
589 TemplateURL url(data);
590 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
591 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
592 GURL result(url.url_ref().ReplaceSearchTerms(
593 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
594 search_terms_data_));
595 ASSERT_TRUE(result.is_valid());
596 EXPECT_EQ(test_data[i].expected_result, result.spec());
597 }
598}
599
[email protected]bca359b2012-06-24 07:53:04600// Tests replacing assisted query stats (AQS) in various scenarios.
601TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
602 struct TestData {
[email protected]0085863a2013-12-06 21:19:03603 const base::string16 search_term;
[email protected]bca359b2012-06-24 07:53:04604 const std::string aqs;
605 const std::string base_url;
606 const std::string url;
607 const std::string expected_result;
608 } test_data[] = {
609 // No HTTPS, no AQS.
610 { ASCIIToUTF16("foo"),
611 "chrome.0.0l6",
612 "http://foo/",
613 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
614 "http://foo/?foo" },
615 // HTTPS available, AQS should be replaced.
616 { ASCIIToUTF16("foo"),
617 "chrome.0.0l6",
618 "https://foo/",
619 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
620 "https://foo/?fooaqs=chrome.0.0l6&" },
621 // HTTPS available, however AQS is empty.
622 { ASCIIToUTF16("foo"),
623 "",
624 "https://foo/",
625 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
626 "https://foo/?foo" },
627 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
628 { ASCIIToUTF16("foo"),
629 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44630 "http://www.google.com",
[email protected]bca359b2012-06-24 07:53:04631 "http://foo?{searchTerms}{google:assistedQueryStats}",
632 "http://foo/?foo" },
633 // A non-Google search provider with HTTPS should allow AQS.
634 { ASCIIToUTF16("foo"),
635 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44636 "https://www.google.com",
[email protected]bca359b2012-06-24 07:53:04637 "https://foo?{searchTerms}{google:assistedQueryStats}",
638 "https://foo/?fooaqs=chrome.0.0l6&" },
639 };
640 TemplateURLData data;
641 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02642 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04643 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28644 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19645 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
646 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04647 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
648 search_terms_args.assisted_query_stats = test_data[i].aqs;
[email protected]798baa8e2014-06-20 05:42:44649 search_terms_data_.set_google_base_url(test_data[i].base_url);
[email protected]ce7ee5f2014-06-16 23:41:19650 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
651 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27652 ASSERT_TRUE(result.is_valid());
653 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56654 }
655}
656
[email protected]00790562012-12-14 09:57:16657// Tests replacing cursor position.
658TEST_F(TemplateURLTest, ReplaceCursorPosition) {
659 struct TestData {
[email protected]0085863a2013-12-06 21:19:03660 const base::string16 search_term;
[email protected]00790562012-12-14 09:57:16661 size_t cursor_position;
662 const std::string url;
663 const std::string expected_result;
664 } test_data[] = {
665 { ASCIIToUTF16("foo"),
[email protected]0085863a2013-12-06 21:19:03666 base::string16::npos,
[email protected]00790562012-12-14 09:57:16667 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
668 "http://www.google.com/?foo&" },
669 { ASCIIToUTF16("foo"),
670 2,
671 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
672 "http://www.google.com/?foo&cp=2&" },
673 { ASCIIToUTF16("foo"),
674 15,
675 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
676 "http://www.google.com/?foo&cp=15&" },
677 };
678 TemplateURLData data;
679 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02680 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]00790562012-12-14 09:57:16681 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28682 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19683 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
684 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]00790562012-12-14 09:57:16685 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
686 search_terms_args.cursor_position = test_data[i].cursor_position;
[email protected]ce7ee5f2014-06-16 23:41:19687 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
688 search_terms_data_));
[email protected]00790562012-12-14 09:57:16689 ASSERT_TRUE(result.is_valid());
690 EXPECT_EQ(test_data[i].expected_result, result.spec());
691 }
692}
693
[email protected]420472b22014-06-10 13:34:43694// Tests replacing input type (&oit=).
695TEST_F(TemplateURLTest, ReplaceInputType) {
696 struct TestData {
697 const base::string16 search_term;
Steven Holte3696c9412017-08-24 18:38:03698 metrics::OmniboxInputType input_type;
[email protected]420472b22014-06-10 13:34:43699 const std::string url;
700 const std::string expected_result;
701 } test_data[] = {
Steven Holte3696c9412017-08-24 18:38:03702 {ASCIIToUTF16("foo"), metrics::OmniboxInputType::UNKNOWN,
703 "{google:baseURL}?{searchTerms}&{google:inputType}",
704 "http://www.google.com/?foo&oit=1&"},
705 {ASCIIToUTF16("foo"), metrics::OmniboxInputType::URL,
706 "{google:baseURL}?{searchTerms}&{google:inputType}",
707 "http://www.google.com/?foo&oit=3&"},
[email protected]420472b22014-06-10 13:34:43708 };
[email protected]420472b22014-06-10 13:34:43709 TemplateURLData data;
710 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02711 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]420472b22014-06-10 13:34:43712 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28713 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19714 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
715 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43716 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
717 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19718 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
719 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43720 ASSERT_TRUE(result.is_valid());
721 EXPECT_EQ(test_data[i].expected_result, result.spec());
722 }
723}
724
[email protected]9b9fa672013-11-07 06:04:52725// Tests replacing currentPageUrl.
726TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48727 struct TestData {
[email protected]0085863a2013-12-06 21:19:03728 const base::string16 search_term;
[email protected]9b9fa672013-11-07 06:04:52729 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48730 const std::string url;
731 const std::string expected_result;
732 } test_data[] = {
733 { ASCIIToUTF16("foo"),
734 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52735 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48736 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
737 { ASCIIToUTF16("foo"),
738 "",
[email protected]9b9fa672013-11-07 06:04:52739 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48740 "http://www.google.com/?foo&" },
741 { ASCIIToUTF16("foo"),
742 "http://g.com/+-/*&=",
[email protected]9b9fa672013-11-07 06:04:52743 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48744 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
745 };
746 TemplateURLData data;
747 data.input_encodings.push_back("UTF-8");
Avi Drissman2244c2d2018-12-25 23:08:02748 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]800569d2013-05-06 07:38:48749 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28750 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19751 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
752 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]800569d2013-05-06 07:38:48753 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52754 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19755 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
756 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48757 ASSERT_TRUE(result.is_valid());
758 EXPECT_EQ(test_data[i].expected_result, result.spec());
759 }
760}
761
[email protected]d82443b2009-01-15 19:54:56762TEST_F(TemplateURLTest, Suggestions) {
763 struct TestData {
764 const int accepted_suggestion;
[email protected]0085863a2013-12-06 21:19:03765 const base::string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56766 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27767 } test_data[] = {
[email protected]0085863a2013-12-06 21:19:03768 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
[email protected]29653892013-03-02 19:25:37769 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30770 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37771 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03772 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(),
[email protected]29653892013-03-02 19:25:37773 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30774 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37775 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03776 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
[email protected]29653892013-03-02 19:25:37777 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56778 };
[email protected]573889f22012-04-07 01:31:54779 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37780 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
781 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54782 data.input_encodings.push_back("UTF-8");
[email protected]168d08722014-06-18 07:13:28783 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19784 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
785 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
Avi Drissman2244c2d2018-12-25 23:08:02786 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04787 TemplateURLRef::SearchTermsArgs search_terms_args(
788 ASCIIToUTF16("foobar"));
789 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
790 search_terms_args.original_query =
791 test_data[i].original_query_for_suggestion;
[email protected]ce7ee5f2014-06-16 23:41:19792 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
793 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27794 ASSERT_TRUE(result.is_valid());
795 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56796 }
797}
798
[email protected]81f808de2009-09-25 01:36:34799TEST_F(TemplateURLTest, RLZ) {
[email protected]798baa8e2014-06-20 05:42:44800 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(false);
[email protected]d82443b2009-01-15 19:54:56801
[email protected]573889f22012-04-07 01:31:54802 TemplateURLData data;
803 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28804 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19805 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
806 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04807 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19808 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56809 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44810 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
811 result.spec());
[email protected]d82443b2009-01-15 19:54:56812}
813
[email protected]c8ccc41d2014-04-10 04:42:12814TEST_F(TemplateURLTest, RLZFromAppList) {
[email protected]798baa8e2014-06-20 05:42:44815 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(true);
[email protected]c8ccc41d2014-04-10 04:42:12816
817 TemplateURLData data;
818 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28819 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19820 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
821 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12822 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
823 args.from_app_list = true;
[email protected]ce7ee5f2014-06-16 23:41:19824 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12825 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44826 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
827 result.spec());
[email protected]c8ccc41d2014-04-10 04:42:12828}
[email protected]c8ccc41d2014-04-10 04:42:12829
[email protected]d82443b2009-01-15 19:54:56830TEST_F(TemplateURLTest, HostAndSearchTermKey) {
831 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19832 const std::string url;
[email protected]d82443b2009-01-15 19:54:56833 const std::string host;
834 const std::string path;
835 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54836 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50837 {"http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
838 {"http://blah/{searchTerms}", "blah", "", ""},
[email protected]d82443b2009-01-15 19:54:56839
vitbarf2a11562017-05-15 14:09:50840 // No term should result in empty values.
841 {"http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56842
vitbarf2a11562017-05-15 14:09:50843 // Multiple terms should result in empty values.
844 {"http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56845
vitbarf2a11562017-05-15 14:09:50846 // Term in the host shouldn't match.
847 {"http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56848
vitbarf2a11562017-05-15 14:09:50849 {"http://blah/?q={searchTerms}", "blah", "/", "q"},
850 {"https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56851
vitbarf2a11562017-05-15 14:09:50852 // Single term with extra chars in value should match.
853 {"http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56854 };
855
Avi Drissman2244c2d2018-12-25 23:08:02856 for (size_t i = 0; i < base::size(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54857 TemplateURLData data;
858 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28859 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19860 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
861 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
862 EXPECT_EQ(test_data[i].search_term_key,
863 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56864 }
865}
866
alexmos294849f2015-03-14 00:55:06867TEST_F(TemplateURLTest, SearchTermKeyLocation) {
868 struct TestData {
869 const std::string url;
870 const url::Parsed::ComponentType location;
871 const std::string path;
vitbarf2a11562017-05-15 14:09:50872 const std::string key;
873 const std::string value_prefix;
874 const std::string value_suffix;
alexmos294849f2015-03-14 00:55:06875 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50876 {"http://blah/{searchTerms}/", url::Parsed::PATH, "", "", "/", "/"},
877 {"http://blah/{searchTerms}", url::Parsed::PATH, "", "", "/", ""},
878 {"http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "", "",
879 "/begin/", "/end"},
880 {"http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY, "/", "q",
881 "", ""},
882 {"http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF, "/", "x",
883 "", ""},
884 {"http://www.example.com/?q=chromium-{searchTerms}@chromium.org/info",
885 url::Parsed::QUERY, "/", "q", "chromium-", "@chromium.org/info"},
alexmos294849f2015-03-14 00:55:06886
vitbarf2a11562017-05-15 14:09:50887 // searchTerms is a key, not a value, so this should result in an empty
888 // value.
889 {"http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x",
890 url::Parsed::QUERY, "", "", "", ""},
alexmos294849f2015-03-14 00:55:06891
vitbarf2a11562017-05-15 14:09:50892 // Multiple search terms should result in empty values.
893 {"http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY, "", "",
894 "", ""},
895 {"http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY, "", "",
896 "", ""},
897 {"http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY, "",
898 "", "", ""},
alexmos294849f2015-03-14 00:55:06899 };
900
Avi Drissman2244c2d2018-12-25 23:08:02901 for (size_t i = 0; i < base::size(test_data); ++i) {
alexmos294849f2015-03-14 00:55:06902 TemplateURLData data;
903 data.SetURL(test_data[i].url);
904 TemplateURL url(data);
905 EXPECT_EQ(test_data[i].location,
906 url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
907 EXPECT_EQ(test_data[i].path,
908 url.url_ref().GetPath(search_terms_data_));
vitbarf2a11562017-05-15 14:09:50909 EXPECT_EQ(test_data[i].key,
910 url.url_ref().GetSearchTermKey(search_terms_data_));
911 EXPECT_EQ(test_data[i].value_prefix,
912 url.url_ref().GetSearchTermValuePrefix(search_terms_data_));
913 EXPECT_EQ(test_data[i].value_suffix,
914 url.url_ref().GetSearchTermValueSuffix(search_terms_data_));
alexmos294849f2015-03-14 00:55:06915 }
916}
917
[email protected]d82443b2009-01-15 19:54:56918TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
919 static const struct {
[email protected]ddd231e2010-06-29 20:35:19920 const char* const base_url;
921 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56922 } data[] = {
[email protected]014010e2011-10-01 04:12:44923 { "http://google.com/", "http://google.com/complete/", },
924 { "http://www.google.com/", "http://www.google.com/complete/", },
925 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
926 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
927 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56928 };
929
Avi Drissman2244c2d2018-12-25 23:08:02930 for (size_t i = 0; i < base::size(data); ++i)
[email protected]d82443b2009-01-15 19:54:56931 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
932}
933
[email protected]81c6ef62010-01-21 09:58:47934TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19935 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:54936 TemplateURLData data;
937 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28938 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47939 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:13940 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19941 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47942 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27943 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47944 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
945}
946
947TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:20948 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:54949 TemplateURLData data;
950 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28951 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47952 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:44953
954 // By default, TemplateURLRef should not consider itself prepopulated.
955 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:13956 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20957 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:44958 EXPECT_TRUE(replacements.empty());
959
960 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:20961 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:54962 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:28963 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:15964 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20965 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47966 EXPECT_TRUE(replacements.empty());
967}
968
969TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:28970 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:47971 TemplateURLRef::Replacements replacements;
972 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:24973 EXPECT_EQ(std::string(), url.url_ref().ParseURL(std::string(), &replacements,
974 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:47975 EXPECT_TRUE(replacements.empty());
976 EXPECT_TRUE(valid);
977}
978
979TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:54980 TemplateURLData data;
981 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:28982 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47983 TemplateURLRef::Replacements replacements;
984 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:24985 EXPECT_EQ(std::string(),
986 url.url_ref().ParseURL("{", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:47987 EXPECT_TRUE(replacements.empty());
988 EXPECT_FALSE(valid);
989}
990
991TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:54992 TemplateURLData data;
993 data.SetURL("{}");
[email protected]168d08722014-06-18 07:13:28994 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47995 TemplateURLRef::Replacements replacements;
996 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:24997 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:47998 EXPECT_TRUE(replacements.empty());
999 EXPECT_TRUE(valid);
1000}
1001
1002TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:541003 TemplateURLData data;
1004 data.SetURL("{}{{%s}}");
[email protected]168d08722014-06-18 07:13:281005 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471006 TemplateURLRef::Replacements replacements;
1007 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241008 EXPECT_EQ("{}{}", url.url_ref().ParseURL("{}{{searchTerms}}", &replacements,
1009 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471010 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271011 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471012 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1013 EXPECT_TRUE(valid);
1014}
1015
1016TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:541017 TemplateURLData data;
1018 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:281019 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471020 TemplateURLRef::Replacements replacements;
1021 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241022 EXPECT_EQ("{", url.url_ref().ParseURL("{{searchTerms}", &replacements,
1023 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471024 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271025 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471026 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1027 EXPECT_TRUE(valid);
1028}
[email protected]5b3078752012-10-09 18:54:161029
[email protected]fd6d8822012-12-08 06:56:111030TEST_F(TemplateURLTest, SearchClient) {
1031 const std::string base_url_str("http://google.com/?");
1032 const std::string terms_str("{searchTerms}&{google:searchClient}");
1033 const std::string full_url_str = base_url_str + terms_str;
[email protected]0085863a2013-12-06 21:19:031034 const base::string16 terms(ASCIIToUTF16(terms_str));
[email protected]798baa8e2014-06-20 05:42:441035 search_terms_data_.set_google_base_url(base_url_str);
[email protected]fd6d8822012-12-08 06:56:111036
1037 TemplateURLData data;
1038 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:281039 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191040 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1041 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111042 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
1043
1044 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:191045 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1046 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111047 ASSERT_TRUE(result.is_valid());
1048 EXPECT_EQ("http://google.com/?foobar&", result.spec());
1049
1050 // Check that the URL is correct when a client is present.
[email protected]798baa8e2014-06-20 05:42:441051 search_terms_data_.set_search_client("search_client");
[email protected]ce7ee5f2014-06-16 23:41:191052 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
1053 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111054 ASSERT_TRUE(result_2.is_valid());
[email protected]798baa8e2014-06-20 05:42:441055 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
[email protected]fd6d8822012-12-08 06:56:111056}
[email protected]fd6d8822012-12-08 06:56:111057
[email protected]5b3078752012-10-09 18:54:161058TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
1059 TemplateURLData data;
1060 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161061 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
1062 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281063 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511064 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1065 ASSERT_EQ(3U, url_refs.size());
1066 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1067 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1068 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161069}
1070
1071TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
1072 TemplateURLData data;
1073 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:281074 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511075 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1076 ASSERT_EQ(1U, url_refs.size());
1077 EXPECT_EQ("http://www.google.co.uk/", url_refs[0].GetURL());
[email protected]5b3078752012-10-09 18:54:161078}
1079
1080TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1081 TemplateURLData data;
1082 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161083 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1084 data.alternate_urls.push_back(
1085 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281086 TemplateURL url(data);
[email protected]0085863a2013-12-06 21:19:031087 base::string16 result;
[email protected]5b3078752012-10-09 18:54:161088
1089 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191090 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191091 EXPECT_EQ(ASCIIToUTF16("something"), result);
1092
1093 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471094 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191095 EXPECT_EQ(ASCIIToUTF16("something"), result);
1096
1097 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471098 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191099 EXPECT_EQ(ASCIIToUTF16("something"), result);
1100
1101 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471102 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191103 EXPECT_EQ(ASCIIToUTF16("something"), result);
1104
1105 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191106 GURL("http://google.com/alt/#q=something"),
1107 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191108 EXPECT_EQ(ASCIIToUTF16("something"), result);
1109
1110 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471111 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191112 EXPECT_EQ(ASCIIToUTF16("something"), result);
1113
1114 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471115 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191116 EXPECT_EQ(ASCIIToUTF16("something"), result);
1117
1118 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471119 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161120 EXPECT_EQ(ASCIIToUTF16("something"), result);
1121
1122 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191123 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371124 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161125
1126 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191127 GURL("http://google.ca/?q=something&q=anything"),
1128 search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371129 EXPECT_EQ(base::string16(), result);
[email protected]67d8b752013-04-03 17:33:271130
1131 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191132 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371133 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161134
[email protected]32e2d81b2012-10-16 19:03:251135 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191136 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
[email protected]32e2d81b2012-10-16 19:03:251137 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:161138
1139 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191140 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371141 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161142
1143 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191144 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161145 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1146
1147 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191148 GURL("http://google.com/alt/?q=123#q=456"),
1149 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161150 EXPECT_EQ(ASCIIToUTF16("456"), result);
1151
1152 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191153 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1154 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161155 EXPECT_EQ(ASCIIToUTF16("123"), result);
1156
1157 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191158 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1159 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161160 EXPECT_EQ(ASCIIToUTF16("789"), result);
1161
1162 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191163 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371164 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161165
1166 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191167 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371168 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161169
1170 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191171 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371172 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161173
1174 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191175 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371176 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161177
1178 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191179 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161180 EXPECT_EQ(ASCIIToUTF16("123"), result);
1181}
[email protected]4076ea62013-01-09 01:47:191182
alexmos294849f2015-03-14 00:55:061183TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1184 TemplateURLData data;
1185 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1186 TemplateURL url(data);
1187 base::string16 result;
1188
1189 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1190 GURL("http://term-in-path.com/begin/something/end"),
1191 search_terms_data_, &result));
1192 EXPECT_EQ(ASCIIToUTF16("something"), result);
1193
1194 // "%20" must be converted to space.
1195 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1196 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1197 search_terms_data_, &result));
1198 EXPECT_EQ(ASCIIToUTF16("a b c"), result);
1199
1200 // Plus must not be converted to space.
1201 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1202 GURL("http://term-in-path.com/begin/1+2+3/end"),
1203 search_terms_data_, &result));
1204 EXPECT_EQ(ASCIIToUTF16("1+2+3"), result);
1205
1206 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1207 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
1208 EXPECT_EQ(base::string16(), result);
1209
1210 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1211 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
1212 EXPECT_EQ(base::string16(), result);
1213
1214 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1215 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
1216 EXPECT_EQ(base::string16(), result);
1217}
1218
vitbar27804d102015-04-15 10:54:411219// Checks that the ExtractSearchTermsFromURL function works correctly
1220// for urls containing non-latin characters in UTF8 encoding.
1221TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
1222 TemplateURLData data;
1223 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1224 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1225 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1226 TemplateURL url(data);
1227 base::string16 result;
1228
1229 // Russian text encoded with UTF-8.
1230 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551231 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1232 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
vitbar27804d102015-04-15 10:54:411233 search_terms_data_, &result));
1234 EXPECT_EQ(
1235 base::WideToUTF16(
1236 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1237 L"\x043C\x0438\x0440!"),
1238 result);
1239
1240 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551241 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1242 "%D0%B0"),
vitbar27804d102015-04-15 10:54:411243 search_terms_data_, &result));
1244 EXPECT_EQ(
1245 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1246 result);
1247
1248 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551249 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1250 "%D0%B8%20A"),
vitbar27804d102015-04-15 10:54:411251 search_terms_data_, &result));
1252 EXPECT_EQ(
1253 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1254 result);
1255}
1256
1257// Checks that the ExtractSearchTermsFromURL function works correctly
1258// for urls containing non-latin characters in non-UTF8 encoding.
1259TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) {
1260 TemplateURLData data;
1261 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1262 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1263 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1264 data.input_encodings.push_back("windows-1251");
1265 TemplateURL url(data);
1266 base::string16 result;
1267
1268 // Russian text encoded with Windows-1251.
1269 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1270 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1271 "%EC%E8%F0!"),
1272 search_terms_data_, &result));
1273 EXPECT_EQ(
1274 base::WideToUTF16(
1275 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1276 L"\x043C\x0438\x0440!"),
1277 result);
1278
1279 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1280 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1281 search_terms_data_, &result));
1282 EXPECT_EQ(
1283 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1284 result);
1285
1286 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1287 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1288 search_terms_data_, &result));
1289 EXPECT_EQ(
1290 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1291 result);
1292}
1293
jbroman50832022016-04-21 23:53:001294// Checks that the ExtractSearchTermsFromURL function strips constant
1295// prefix/suffix strings from the search terms param.
1296TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) {
1297 TemplateURLData data;
1298 data.alternate_urls.push_back(
1299 "http://www.example.com/?q=chromium-{searchTerms}@chromium.org");
1300 data.alternate_urls.push_back(
1301 "http://www.example.com/chromium-{searchTerms}@chromium.org/info");
1302 TemplateURL url(data);
1303 base::string16 result;
1304
1305 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1306 GURL("http://www.example.com/[email protected]"),
1307 search_terms_data_, &result));
1308 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1309
1310 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1311 GURL("http://www.example.com/[email protected]/info"),
1312 search_terms_data_, &result));
1313 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1314
1315 // Don't match if the prefix and suffix aren't there.
1316 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1317 GURL("http://www.example.com/?q=invalid"), search_terms_data_, &result));
jbromane8b8728d2017-04-12 17:29:391318
1319 // Don't match if the prefix and suffix overlap.
1320 TemplateURLData data_with_overlap;
1321 data.alternate_urls.push_back(
1322 "http://www.example.com/?q=goo{searchTerms}oogle");
1323 TemplateURL url_with_overlap(data);
1324 EXPECT_FALSE(url_with_overlap.ExtractSearchTermsFromURL(
1325 GURL("http://www.example.com/?q=google"), search_terms_data_, &result));
jbroman50832022016-04-21 23:53:001326}
1327
[email protected]f62e30f52013-03-23 03:45:151328TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1329 TemplateURLData data;
1330 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]f62e30f52013-03-23 03:45:151331 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1332 data.alternate_urls.push_back(
1333 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281334 TemplateURL url(data);
[email protected]f62e30f52013-03-23 03:45:151335 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1336 GURL result;
1337
1338 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191339 GURL("http://google.com/?q=something"), search_terms,
1340 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101341 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151342
1343 result = GURL("http://should.not.change.com");
1344 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191345 GURL("http://google.ca/?q=something"), search_terms,
1346 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151347 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1348
1349 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191350 GURL("http://google.com/foo/?q=foo"), search_terms,
1351 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151352
1353 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191354 GURL("https://google.com/?q=foo"), search_terms,
1355 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101356 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151357
1358 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191359 GURL("http://google.com:8080/?q=foo"), search_terms,
1360 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151361
1362 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191363 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1364 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101365 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result);
[email protected]f62e30f52013-03-23 03:45:151366
1367 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1368 // template_url.cc for details.
1369 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191370 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1371 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101372 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151373
1374 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1375 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191376 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101377 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
[email protected]f62e30f52013-03-23 03:45:151378 result);
1379
1380 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1381 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191382 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151383 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
vitbarf298455d2015-04-21 12:58:101384 "#j=abc&q=Bob+Morane&h=def9"), result);
[email protected]f62e30f52013-03-23 03:45:151385
1386 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191387 GURL("http://google.com/alt/?q="), search_terms,
1388 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151389
1390 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191391 GURL("http://google.com/alt/?#q="), search_terms,
1392 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151393
1394 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191395 GURL("http://google.com/alt/?q=#q="), search_terms,
1396 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151397
1398 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191399 GURL("http://google.com/alt/?q=123#q="), search_terms,
1400 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151401
1402 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191403 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1404 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101405 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151406}
[email protected]56fa29592013-07-02 20:25:531407
alexmos294849f2015-03-14 00:55:061408TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1409 TemplateURLData data;
1410 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1411 TemplateURL url(data);
1412 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1413 GURL result;
1414
1415 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1416 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1417 search_terms_data_, &result));
1418 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1419
1420 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1421 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1422 search_terms_data_, &result));
1423 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1424
1425 result = GURL("http://should.not.change.com");
1426 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1427 GURL("http://term-in-path.com/about"), search_terms,
1428 search_terms_data_, &result));
1429 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1430}
1431
vitbarf298455d2015-04-21 12:58:101432// Checks that the ReplaceSearchTermsInURL function works correctly
1433// for search terms containing non-latin characters for a search engine
1434// using UTF-8 input encoding.
1435TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) {
1436 TemplateURLData data;
1437 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1438 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1439 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1440 TemplateURL url(data);
1441
1442 // Russian text which will be encoded with UTF-8.
1443 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1444 L"\x0442\x0435\x043A\x0441\x0442"));
1445 GURL result;
1446
1447 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1448 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_,
1449 &result));
1450 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1451 result);
1452
1453 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1454 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_,
1455 &result));
1456 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1457 result);
1458
1459 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1460 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_,
1461 &result));
1462 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1463 result);
1464}
1465
1466// Checks that the ReplaceSearchTermsInURL function works correctly
1467// for search terms containing non-latin characters for a search engine
1468// using non UTF-8 input encoding.
1469TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) {
1470 TemplateURLData data;
1471 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1472 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1473 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1474 data.input_encodings.push_back("windows-1251");
1475 TemplateURL url(data);
1476
1477 // Russian text which will be encoded with Windows-1251.
1478 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1479 L"\x0442\x0435\x043A\x0441\x0442"));
1480 GURL result;
1481
1482 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1483 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_,
1484 &result));
1485 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1486 result);
1487
1488 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1489 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_,
1490 &result));
1491 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1492 result);
1493
1494 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1495 GURL("http://windows-1251.ru/path/a%20b"), search_terms,
1496 search_terms_data_, &result));
1497 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1498 result);
1499}
1500
Mark Pearson247fb8a2018-08-30 05:12:261501// Test the |additional_query_params| field of SearchTermsArgs.
[email protected]621ade062013-10-28 06:27:431502TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431503 TemplateURLData data;
1504 // Pick a URL with replacements before, during, and after the query, to ensure
1505 // we don't goof up any of them.
1506 data.SetURL("{google:baseURL}search?q={searchTerms}"
1507 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281508 TemplateURL url(data);
[email protected]621ade062013-10-28 06:27:431509
Mark Pearson247fb8a2018-08-30 05:12:261510 // Baseline: no |additional_query_params| field.
[email protected]621ade062013-10-28 06:27:431511 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1512 search_terms.original_query = ASCIIToUTF16("def");
1513 search_terms.accepted_suggestion = 0;
1514 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191515 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431516
Mark Pearson247fb8a2018-08-30 05:12:261517 // Set the |additional_query_params|.
1518 search_terms.additional_query_params = "pq=xyz";
[email protected]621ade062013-10-28 06:27:431519 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191520 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431521
Mark Pearson247fb8a2018-08-30 05:12:261522 // Add |append_extra_query_params_from_command_line| into the mix, and ensure
1523 // it works.
1524 search_terms.append_extra_query_params_from_command_line = true;
avi1772c1a2014-12-22 22:42:331525 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]621ade062013-10-28 06:27:431526 switches::kExtraSearchQueryParams, "a=b");
1527 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191528 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431529}
1530
Mark Pearson247fb8a2018-08-30 05:12:261531// Test the |search_terms.append_extra_query_params_from_command_line| field of
1532// SearchTermsArgs.
[email protected]56fa29592013-07-02 20:25:531533TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531534 TemplateURLData data;
1535 // Pick a URL with replacements before, during, and after the query, to ensure
1536 // we don't goof up any of them.
1537 data.SetURL("{google:baseURL}search?q={searchTerms}"
1538 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281539 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531540
Mark Pearson247fb8a2018-08-30 05:12:261541 // Baseline: no command-line args, no
1542 // |search_terms.append_extra_query_params_from_command_line| flag.
[email protected]56fa29592013-07-02 20:25:531543 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1544 search_terms.original_query = ASCIIToUTF16("def");
1545 search_terms.accepted_suggestion = 0;
1546 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191547 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531548
1549 // Set the flag. Since there are no command-line args, this should have no
1550 // effect.
Mark Pearson247fb8a2018-08-30 05:12:261551 search_terms.append_extra_query_params_from_command_line = true;
[email protected]56fa29592013-07-02 20:25:531552 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191553 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531554
1555 // Now append the command-line arg. This should be inserted into the query.
avi1772c1a2014-12-22 22:42:331556 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]56fa29592013-07-02 20:25:531557 switches::kExtraSearchQueryParams, "a=b");
1558 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191559 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531560
1561 // Turn off the flag. Now the command-line arg should be ignored again.
Mark Pearson247fb8a2018-08-30 05:12:261562 search_terms.append_extra_query_params_from_command_line = false;
[email protected]56fa29592013-07-02 20:25:531563 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191564 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531565}
[email protected]d5015ca2013-08-08 22:04:181566
1567// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171568TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181569 TemplateURLData data;
1570 data.input_encodings.push_back("UTF-8");
1571 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281572 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191573 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1574 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]d5015ca2013-08-08 22:04:181575 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1576
[email protected]ce7ee5f2014-06-16 23:41:191577 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1578 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181579 EXPECT_EQ("http://www.google.com/?q=foo", result);
1580
[email protected]332d17d22014-06-20 16:56:031581 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191582 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1583 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181584 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1585
1586 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031587 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191588 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1589 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181590 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1591}
[email protected]2328ee22013-08-08 23:00:191592
1593// Test the IsSearchResults function.
1594TEST_F(TemplateURLTest, IsSearchResults) {
1595 TemplateURLData data;
1596 data.SetURL("http://bar/search?q={searchTerms}");
[email protected]2767c0fd2013-08-16 17:44:161597 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191598 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1599 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1600 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1601 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281602 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191603
1604 const struct {
1605 const char* const url;
1606 bool result;
1607 } url_data[] = {
1608 { "http://bar/search?q=foo&oq=foo", true, },
1609 { "http://bar/?q=foo&oq=foo", true, },
1610 { "http://bar/#output=search&q=foo&oq=foo", true, },
1611 { "http://bar/webhp#q=foo&oq=foo", true, },
1612 { "http://bar/#q=foo&oq=foo", true, },
1613 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1614 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1615 { "http://bar/", false, },
1616 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161617 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191618 };
1619
Avi Drissman2244c2d2018-12-25 23:08:021620 for (size_t i = 0; i < base::size(url_data); ++i) {
[email protected]2328ee22013-08-08 23:00:191621 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191622 search_provider.IsSearchURL(GURL(url_data[i].url),
1623 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191624 }
1625}
[email protected]a048de8a2013-10-02 18:30:061626
jdonnelly41c5b46a2015-07-10 21:24:381627TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) {
[email protected]9d70de12014-05-10 02:15:311628 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441629 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311630 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1631
[email protected]168d08722014-06-18 07:13:281632 TemplateURL url(data);
[email protected]9d70de12014-05-10 02:15:311633 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191634 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1635 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311636 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1637}
[email protected]20184242014-05-14 02:57:421638
1639TEST_F(TemplateURLTest, SessionToken) {
1640 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441641 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421642 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1643
[email protected]168d08722014-06-18 07:13:281644 TemplateURL url(data);
[email protected]20184242014-05-14 02:57:421645 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1646 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191647 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1648 search_terms_data_);
[email protected]20184242014-05-14 02:57:421649 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1650
[email protected]168d08722014-06-18 07:13:281651 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421652 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191653 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1654 search_terms_data_);
[email protected]20184242014-05-14 02:57:421655 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1656}
[email protected]448b17f52014-06-13 01:08:031657
1658TEST_F(TemplateURLTest, ContextualSearchParameters) {
1659 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441660 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031661 data.SetURL("http://bar/_/contextualsearch?"
1662 "{google:contextualSearchVersion}"
1663 "{google:contextualSearchContextData}");
1664
[email protected]168d08722014-06-18 07:13:281665 TemplateURL url(data);
[email protected]448b17f52014-06-13 01:08:031666 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191667 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1668 search_terms_data_);
donndbb98274e2016-11-01 21:04:401669 EXPECT_EQ("http://bar/_/contextualsearch?", result);
[email protected]448b17f52014-06-13 01:08:031670
Donn Denman01b1bf72018-08-14 16:25:201671 // Test the current common case, which uses no home country or previous
1672 // event.
1673 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
1674 2, 1, std::string(), 0, 0);
[email protected]448b17f52014-06-13 01:08:031675 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191676 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1677 search_terms_data_);
donndd9fc4d92016-09-16 00:30:491678 EXPECT_EQ(
1679 "http://bar/_/contextualsearch?"
donnd0bc73292017-01-17 22:07:561680 "ctxs=2&"
donndd9fc4d92016-09-16 00:30:491681 "ctxsl_coca=1",
1682 result);
donndbb98274e2016-11-01 21:04:401683
Donn Denman01b1bf72018-08-14 16:25:201684 // Test the home country and non-zero event data case.
donndbb98274e2016-11-01 21:04:401685 search_terms_args.contextual_search_params =
Donn Denman01b1bf72018-08-14 16:25:201686 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(2, 2, "CH",
1687 1657713458, 5);
donndbb98274e2016-11-01 21:04:401688 result =
1689 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1690
1691 EXPECT_EQ(
1692 "http://bar/_/contextualsearch?"
1693 "ctxs=2&"
donnd0bc73292017-01-17 22:07:561694 "ctxsl_coca=2&"
Donn Denman01b1bf72018-08-14 16:25:201695 "ctxs_hc=CH&"
1696 "ctxsl_pid=1657713458&"
1697 "ctxsl_per=5",
donndbb98274e2016-11-01 21:04:401698 result);
[email protected]448b17f52014-06-13 01:08:031699}
[email protected]44ccc9f2014-06-20 17:36:211700
1701TEST_F(TemplateURLTest, GenerateKeyword) {
1702 ASSERT_EQ(ASCIIToUTF16("foo"),
jshin1fb76462016-04-05 22:13:031703 TemplateURL::GenerateKeyword(GURL("http://foo")));
[email protected]44ccc9f2014-06-20 17:36:211704 // www. should be stripped.
jshin1fb76462016-04-05 22:13:031705 ASSERT_EQ(ASCIIToUTF16("foo"),
1706 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
[email protected]44ccc9f2014-06-20 17:36:211707 // Make sure we don't get a trailing '/'.
jshin1fb76462016-04-05 22:13:031708 ASSERT_EQ(ASCIIToUTF16("blah"),
1709 TemplateURL::GenerateKeyword(GURL("http://blah/")));
[email protected]44ccc9f2014-06-20 17:36:211710 // Don't generate the empty string.
jshin1fb76462016-04-05 22:13:031711 ASSERT_EQ(ASCIIToUTF16("www"),
1712 TemplateURL::GenerateKeyword(GURL("http://www.")));
alshabaline6212ac2015-07-23 08:03:001713 ASSERT_EQ(
1714 base::UTF8ToUTF16("\xd0\xb0\xd0\xb1\xd0\xb2"),
jshin1fb76462016-04-05 22:13:031715 TemplateURL::GenerateKeyword(GURL("http://xn--80acd")));
a-v-y81695d0d2017-04-20 08:22:221716
1717 // Generated keywords must always be in lowercase, because TemplateURLs always
1718 // converts keywords to lowercase in its constructor and TemplateURLService
1719 // stores TemplateURLs in maps using keyword as key.
1720 EXPECT_TRUE(IsLowerCase(TemplateURL::GenerateKeyword(GURL("http://BLAH/"))));
1721 EXPECT_TRUE(IsLowerCase(
1722 TemplateURL::GenerateKeyword(GURL("http://embeddedhtml.<head>/"))));
[email protected]44ccc9f2014-06-20 17:36:211723}
1724
1725TEST_F(TemplateURLTest, GenerateSearchURL) {
1726 struct GenerateSearchURLCase {
1727 const char* test_name;
1728 const char* url;
1729 const char* expected;
1730 } generate_url_cases[] = {
1731 { "invalid URL", "foo{searchTerms}", "" },
1732 { "URL with no replacements", "http://foo/", "http://foo/" },
1733 { "basic functionality", "http://foo/{searchTerms}",
1734 "http://foo/blah.blah.blah.blah.blah" }
1735 };
1736
Avi Drissman2244c2d2018-12-25 23:08:021737 for (size_t i = 0; i < base::size(generate_url_cases); ++i) {
[email protected]44ccc9f2014-06-20 17:36:211738 TemplateURLData data;
1739 data.SetURL(generate_url_cases[i].url);
1740 TemplateURL t_url(data);
1741 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1742 generate_url_cases[i].expected)
1743 << generate_url_cases[i].test_name << " failed.";
1744 }
1745}
[email protected]9e9130ce2014-06-23 23:20:511746
1747TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1748 TemplateURLData data;
1749 search_terms_data_.set_google_base_url("http://bar/");
1750 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1751
1752 TemplateURL url(data);
1753 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1754 search_terms_args.prefetch_query = "full query text";
1755 search_terms_args.prefetch_query_type = "2338";
1756 std::string result =
1757 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1758 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1759 result);
1760
1761 TemplateURL url2(data);
1762 search_terms_args.prefetch_query.clear();
1763 search_terms_args.prefetch_query_type.clear();
1764 result =
1765 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1766 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1767}
vitbar5bd8c252016-01-29 18:44:511768
1769// Tests that TemplateURL works correctly after changing the Google base URL
1770// and invalidating cached values.
1771TEST_F(TemplateURLTest, InvalidateCachedValues) {
1772 TemplateURLData data;
1773 data.SetURL("{google:baseURL}search?q={searchTerms}");
1774 data.suggestions_url = "{google:baseSuggestURL}search?q={searchTerms}";
vitbar5bd8c252016-01-29 18:44:511775 data.image_url = "{google:baseURL}searchbyimage/upload";
1776 data.new_tab_url = "{google:baseURL}_/chrome/newtab";
1777 data.contextual_search_url = "{google:baseURL}_/contextualsearch";
1778 data.alternate_urls.push_back("{google:baseURL}s#q={searchTerms}");
1779 TemplateURL url(data);
1780 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("X"));
1781 base::string16 search_terms;
1782
1783 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1784 EXPECT_EQ("http://www.google.com/search?q=X",
1785 url.url_ref().ReplaceSearchTerms(search_terms_args,
1786 search_terms_data_));
1787 EXPECT_EQ("http://www.google.com/s#q=X",
1788 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1789 search_terms_data_));
1790 EXPECT_EQ("http://www.google.com/search?q=X",
1791 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1792 search_terms_data_));
1793 EXPECT_EQ("http://www.google.com/complete/search?q=X",
1794 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1795 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:511796 EXPECT_EQ("http://www.google.com/searchbyimage/upload",
1797 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1798 search_terms_data_));
1799 EXPECT_EQ("http://www.google.com/_/chrome/newtab",
1800 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1801 search_terms_data_));
1802 EXPECT_EQ("http://www.google.com/_/contextualsearch",
1803 url.contextual_search_url_ref().ReplaceSearchTerms(
1804 search_terms_args, search_terms_data_));
1805
1806 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1807 GURL("http://www.google.com/search?q=Y+Z"),
1808 search_terms_data_, &search_terms));
1809 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
1810 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1811 GURL("http://www.google.com/s#q=123"),
1812 search_terms_data_, &search_terms));
1813 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
1814
1815 search_terms_data_.set_google_base_url("https://www.foo.org/");
1816 url.InvalidateCachedValues();
1817
1818 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1819 EXPECT_EQ("https://www.foo.org/search?q=X",
1820 url.url_ref().ReplaceSearchTerms(search_terms_args,
1821 search_terms_data_));
1822 EXPECT_EQ("https://www.foo.org/s#q=X",
1823 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1824 search_terms_data_));
1825 EXPECT_EQ("https://www.foo.org/search?q=X",
1826 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1827 search_terms_data_));
1828 EXPECT_EQ("https://www.foo.org/complete/search?q=X",
1829 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1830 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:511831 EXPECT_EQ("https://www.foo.org/searchbyimage/upload",
1832 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1833 search_terms_data_));
1834 EXPECT_EQ("https://www.foo.org/_/chrome/newtab",
1835 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1836 search_terms_data_));
1837 EXPECT_EQ("https://www.foo.org/_/contextualsearch",
1838 url.contextual_search_url_ref().ReplaceSearchTerms(
1839 search_terms_args, search_terms_data_));
1840
1841 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1842 GURL("https://www.foo.org/search?q=Y+Z"),
1843 search_terms_data_, &search_terms));
1844 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
1845 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1846 GURL("https://www.foo.org/s#q=123"),
1847 search_terms_data_, &search_terms));
1848 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
1849
1850 search_terms_data_.set_google_base_url("http://www.google.com/");
1851}
Troy Hildebrandt47ac4d8b2018-05-10 22:40:001852
1853// Tests the use of wildcards in the path to ensure both extracting search terms
1854// and generating a search URL work correctly.
1855TEST_F(TemplateURLTest, PathWildcard) {
1856 TemplateURLData data;
1857 data.SetURL(
1858 "https://www.google.com/search{google:pathWildcard}?q={searchTerms}");
1859 TemplateURL url(data);
1860
1861 // Test extracting search terms from a URL.
1862 base::string16 search_terms;
1863 url.ExtractSearchTermsFromURL(GURL("https://www.google.com/search?q=testing"),
1864 search_terms_data_, &search_terms);
1865 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
1866 url.ExtractSearchTermsFromURL(
1867 GURL("https://www.google.com/search;_this_is_a_test;_?q=testing"),
1868 search_terms_data_, &search_terms);
1869 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
1870
1871 // Tests overlapping prefix/suffix.
1872 data.SetURL(
1873 "https://www.google.com/search{google:pathWildcard}rch?q={searchTerms}");
1874 TemplateURL overlap_url(data);
1875 overlap_url.ExtractSearchTermsFromURL(
1876 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
1877 &search_terms);
1878 EXPECT_TRUE(search_terms.empty());
1879
1880 // Tests wildcard at beginning of path so we only have a suffix.
1881 data.SetURL(
1882 "https://www.google.com/{google:pathWildcard}rch?q={searchTerms}");
1883 TemplateURL suffix_url(data);
1884 suffix_url.ExtractSearchTermsFromURL(
1885 GURL("https://www.google.com/search?q=testing"), search_terms_data_,
1886 &search_terms);
1887 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
1888
1889 // Tests wildcard between prefix/suffix.
1890 overlap_url.ExtractSearchTermsFromURL(
1891 GURL("https://www.google.com/search_testing_rch?q=testing"),
1892 search_terms_data_, &search_terms);
1893 EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms);
1894
1895 // Test generating a URL.
1896 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1897 GURL generated_url;
1898 url.ReplaceSearchTermsInURL(url.GenerateSearchURL(search_terms_data_),
1899 search_terms_args, search_terms_data_,
1900 &generated_url);
1901 EXPECT_EQ("https://www.google.com/search?q=foo", generated_url.spec());
1902}