blob: 0c69f957ab64c906c5e21ded4314a2428a46e5b6 [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"
avif57136c12015-12-25 23:27:4510#include "base/macros.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"
a-v-y02d3f50b2017-02-16 12:03:5514#include "components/google/core/browser/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 };
viettrungluu37a447b2014-10-16 18:23:2792 for (size_t i = 0; i < arraysize(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());
[email protected]93b29062013-07-12 03:09:09224 for (TemplateURLRef::PostParams::const_iterator i = post_params.begin();
225 i != post_params.end(); ++i) {
226 TemplateURLRef::Replacements::const_iterator j = replacements.begin();
227 for (; j != replacements.end(); ++j) {
228 if (j->is_post_param && j->index ==
229 static_cast<size_t>(i - post_params.begin())) {
230 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27231 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
vitbardeb285392015-02-20 14:02:55232 ExpectPostParamIs(*i, "width",
233 base::IntToString(
234 search_args.image_original_size.width()));
[email protected]2f3bc6512013-08-28 03:56:27235 break;
[email protected]1020fead2014-06-20 13:40:28236 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE:
vitbardeb285392015-02-20 14:02:55237 ExpectPostParamIs(*i, "sbisrc",
238 search_terms_data.GoogleImageSearchSource());
[email protected]1020fead2014-06-20 13:40:28239 break;
[email protected]93b29062013-07-12 03:09:09240 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
vitbardeb285392015-02-20 14:02:55241 ExpectPostParamIs(*i, "image_content",
242 search_args.image_thumbnail_content,
243 "image/jpeg");
[email protected]93b29062013-07-12 03:09:09244 break;
245 case TemplateURLRef::GOOGLE_IMAGE_URL:
vitbardeb285392015-02-20 14:02:55246 ExpectPostParamIs(*i, "image_url", search_args.image_url.spec());
[email protected]93b29062013-07-12 03:09:09247 break;
248 case TemplateURLRef::LANGUAGE:
vitbardeb285392015-02-20 14:02:55249 ExpectPostParamIs(*i, "language", "en");
[email protected]93b29062013-07-12 03:09:09250 break;
251 default:
252 ADD_FAILURE(); // Should never go here.
253 }
254 break;
255 }
256 }
257 if (j != replacements.end())
258 continue;
vitbardeb285392015-02-20 14:02:55259 if (i->name == "empty_param")
260 ExpectPostParamIs(*i, "empty_param", std::string());
261 else
262 ExpectPostParamIs(*i, "constant_param", "constant");
[email protected]93b29062013-07-12 03:09:09263 }
264}
265
Peter Kastinge59f0c32017-11-29 05:56:22266TEST_F(TemplateURLTest, ImageURLWithGetShouldNotCrash) {
267 TemplateURLData data;
268 data.SetURL("http://foo/?q={searchTerms}&t={google:imageThumbnail}");
269 TemplateURL url(data);
270 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
271 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
272
273 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
274 search_args.image_thumbnail_content = "dummy-image-thumbnail";
275 GURL result(
276 url.url_ref().ReplaceSearchTerms(search_args, search_terms_data_));
277 ASSERT_TRUE(result.is_valid());
278 EXPECT_EQ("http://foo/?q=X&t=dummy-image-thumbnail", result.spec());
279}
280
[email protected]d88cb202011-08-17 20:03:01281// Test that setting the prepopulate ID from TemplateURL causes the stored
282// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44283TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54284 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20285 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]168d08722014-06-18 07:13:28286 TemplateURL url(data);
[email protected]1a257262011-06-28 22:15:44287 TemplateURLRef::Replacements replacements;
288 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:24289 EXPECT_EQ("http://foo{fhqwhgads}bar",
290 url.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements,
291 nullptr, &valid));
[email protected]1a257262011-06-28 22:15:44292 EXPECT_TRUE(replacements.empty());
293 EXPECT_TRUE(valid);
294
[email protected]573889f22012-04-07 01:31:54295 data.prepopulate_id = 123;
[email protected]168d08722014-06-18 07:13:28296 TemplateURL url2(data);
Ivan Kotenkov75b1c3a2017-10-24 14:47:24297 EXPECT_EQ("http://foobar",
298 url2.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements,
299 nullptr, &valid));
[email protected]1a257262011-06-28 22:15:44300 EXPECT_TRUE(replacements.empty());
301 EXPECT_TRUE(valid);
302}
303
vitbar90902c42016-05-11 06:16:55304// Test that setting the prepopulate ID from TemplateURL causes the stored
305// TemplateURLRef to handle parsing the URL parameters differently.
306TEST_F(TemplateURLTest, SetPrepopulatedAndReplace) {
307 TemplateURLData data;
308 data.SetURL("http://foo{fhqwhgads}search/?q={searchTerms}");
309 data.suggestions_url = "http://foo{fhqwhgads}suggest/?q={searchTerms}";
vitbar90902c42016-05-11 06:16:55310 data.image_url = "http://foo{fhqwhgads}image/";
311 data.new_tab_url = "http://foo{fhqwhgads}newtab/";
312 data.contextual_search_url = "http://foo{fhqwhgads}context/";
313 data.alternate_urls.push_back(
314 "http://foo{fhqwhgads}alternate/?q={searchTerms}");
315
316 TemplateURLRef::SearchTermsArgs args(base::ASCIIToUTF16("X"));
317 const SearchTermsData& stdata = search_terms_data_;
318
319 TemplateURL url(data);
320 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
321 url.url_ref().ReplaceSearchTerms(args, stdata));
322 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dalternate/?q=X",
323 url.url_refs()[0].ReplaceSearchTerms(args, stdata));
324 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
325 url.url_refs()[1].ReplaceSearchTerms(args, stdata));
326 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsuggest/?q=X",
327 url.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
vitbar90902c42016-05-11 06:16:55328 EXPECT_EQ("http://foo{fhqwhgads}image/",
329 url.image_url_ref().ReplaceSearchTerms(args, stdata));
330 EXPECT_EQ("http://foo{fhqwhgads}newtab/",
331 url.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
332 EXPECT_EQ("http://foo{fhqwhgads}context/",
333 url.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
334
335 data.prepopulate_id = 123;
336 TemplateURL url2(data);
337 EXPECT_EQ("http://foosearch/?q=X",
338 url2.url_ref().ReplaceSearchTerms(args, stdata));
339 EXPECT_EQ("http://fooalternate/?q=X",
340 url2.url_refs()[0].ReplaceSearchTerms(args, stdata));
341 EXPECT_EQ("http://foosearch/?q=X",
342 url2.url_refs()[1].ReplaceSearchTerms(args, stdata));
343 EXPECT_EQ("http://foosuggest/?q=X",
344 url2.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
vitbar90902c42016-05-11 06:16:55345 EXPECT_EQ("http://fooimage/",
346 url2.image_url_ref().ReplaceSearchTerms(args, stdata));
347 EXPECT_EQ("http://foonewtab/",
348 url2.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
349 EXPECT_EQ("http://foocontext/",
350 url2.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
351}
352
[email protected]d82443b2009-01-15 19:54:56353TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54354 TemplateURLData data;
355 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]168d08722014-06-18 07:13:28356 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19357 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
358 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04359 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19360 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56361 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27362 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56363}
364
365TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54366 TemplateURLData data;
367 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]168d08722014-06-18 07:13:28368 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19369 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
370 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04371 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19372 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56373 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27374 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56375}
376
[email protected]375bd7312010-08-30 22:18:13377TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
378 struct SearchTermsCase {
379 const char* url;
[email protected]0085863a2013-12-06 21:19:03380 const base::string16 terms;
[email protected]375bd7312010-08-30 22:18:13381 const char* output;
382 } search_term_cases[] = {
[email protected]0085863a2013-12-06 21:19:03383 { "{google:baseURL}{language}{searchTerms}", base::string16(),
[email protected]b37bdfe2012-03-16 20:53:27384 "http://example.com/e/en" },
[email protected]0085863a2013-12-06 21:19:03385 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
[email protected]014010e2011-10-01 04:12:44386 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13387 };
388
hashimoto3c831812014-08-25 07:40:23389 TestingSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54390 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27391 for (size_t i = 0; i < arraysize(search_term_cases); ++i) {
[email protected]375bd7312010-08-30 22:18:13392 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54393 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:28394 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19395 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data));
396 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data));
397 GURL result(url.url_ref().ReplaceSearchTerms(
Ivan Kotenkov75b1c3a2017-10-24 14:47:24398 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data,
399 nullptr));
[email protected]375bd7312010-08-30 22:18:13400 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27401 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13402 }
403}
404
[email protected]d82443b2009-01-15 19:54:56405TEST_F(TemplateURLTest, URLRefTermToWide) {
406 struct ToWideCase {
407 const char* encoded_search_term;
[email protected]0085863a2013-12-06 21:19:03408 const base::string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56409 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30410 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56411 // Test some big-5 input.
[email protected]f911df52013-12-24 23:24:23412 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56413 // Test some UTF-8 input. We should fall back to this when the encoding
414 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
415 // Big-5 trailing byte.
[email protected]f911df52013-12-24 23:24:23416 {"%e4%bd%a05%e5%a5%bd+to+you",
417 base::WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56418 // Undecodable input should stay escaped.
[email protected]f911df52013-12-24 23:24:23419 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44420 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30421 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44422 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30423 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56424 };
425
[email protected]d82443b2009-01-15 19:54:56426 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54427 TemplateURLData data;
428 data.SetURL("http://foo?q={searchTerms}");
429 data.input_encodings.push_back("big-5");
[email protected]168d08722014-06-18 07:13:28430 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19431 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
432 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
viettrungluu37a447b2014-10-16 18:23:27433 for (size_t i = 0; i < arraysize(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07434 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13435 url.url_ref().SearchTermToString16(
436 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56437 }
438}
439
[email protected]d82443b2009-01-15 19:54:56440TEST_F(TemplateURLTest, DisplayURLToURLRef) {
441 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19442 const std::string url;
[email protected]0085863a2013-12-06 21:19:03443 const base::string16 expected_result;
[email protected]b37bdfe2012-03-16 20:53:27444 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19445 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30446 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19447 { "http://X",
[email protected]400b133f2011-01-19 18:32:30448 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19449 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30450 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19451 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30452 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56453 };
[email protected]573889f22012-04-07 01:31:54454 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27455 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54456 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28457 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19458 EXPECT_EQ(test_data[i].expected_result,
459 url.url_ref().DisplayURL(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27460 EXPECT_EQ(test_data[i].url,
[email protected]ce7ee5f2014-06-16 23:41:19461 TemplateURLRef::DisplayURLToURLRef(
462 url.url_ref().DisplayURL(search_terms_data_)));
[email protected]d82443b2009-01-15 19:54:56463 }
464}
465
466TEST_F(TemplateURLTest, ReplaceSearchTerms) {
467 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19468 const std::string url;
[email protected]d82443b2009-01-15 19:54:56469 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27470 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19471 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56472 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19473 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56474 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19475 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56476 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19477 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56478 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19479 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56480 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19481 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56482 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19483 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56484 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19485 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56486 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19487 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56488 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19489 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56490 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19491 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56492 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19493 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56494 "http://foo/UTF-8a{language}aXa" },
495 };
[email protected]573889f22012-04-07 01:31:54496 TemplateURLData data;
497 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27498 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54499 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28500 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19501 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
502 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27503 std::string expected_result = test_data[i].expected_result;
brettwe6dae462015-06-24 20:54:45504 base::ReplaceSubstringsAfterOffset(
505 &expected_result, 0, "{language}",
506 search_terms_data_.GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04507 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19508 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
509 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27510 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56511 EXPECT_EQ(expected_result, result.spec());
512 }
513}
514
515
516// Tests replacing search terms in various encodings and making sure the
517// generated URL matches the expected value.
518TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
519 struct TestData {
520 const std::string encoding;
[email protected]0085863a2013-12-06 21:19:03521 const base::string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19522 const std::string url;
[email protected]d82443b2009-01-15 19:54:56523 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27524 } test_data[] = {
Alexander Yashkin874b919d2018-02-17 15:11:18525 {"BIG5", base::WideToUTF16(L"\x60BD"),
526 "http://foo/?{searchTerms}{inputEncoding}", "http://foo/?%B1~BIG5"},
527 {"UTF-8", ASCIIToUTF16("blah"),
528 "http://foo/?{searchTerms}{inputEncoding}", "http://foo/?blahUTF-8"},
529 {"Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
530 "http://foo/{searchTerms}/bar", "http://foo/%82%A0/bar"},
531 {"Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
532 "http://foo/{searchTerms}/bar", "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56533 };
[email protected]573889f22012-04-07 01:31:54534 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27535 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54536 data.SetURL(test_data[i].url);
537 data.input_encodings.clear();
538 data.input_encodings.push_back(test_data[i].encoding);
[email protected]168d08722014-06-18 07:13:28539 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19540 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
541 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04542 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19543 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
544 search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04545 ASSERT_TRUE(result.is_valid());
546 EXPECT_EQ(test_data[i].expected_result, result.spec());
547 }
548}
549
Alexander Yashkin874b919d2018-02-17 15:11:18550// Test that encoding with several optional codepages works as intended.
551// Codepages are tried in order, fallback is UTF-8.
552TEST_F(TemplateURLTest, ReplaceSearchTermsMultipleEncodings) {
553 struct TestData {
554 const std::vector<std::string> encodings;
555 const base::string16 search_term;
556 const std::string url;
557 const std::string expected_result;
558 } test_data[] = {
559 // First and third encodings are valid. First is used.
560 {{"windows-1251", "cp-866", "UTF-8"},
561 base::UTF8ToUTF16("\xD1\x8F"),
562 "http://foo/?{searchTerms}{inputEncoding}",
563 "http://foo/?%FFwindows-1251"},
564 // Second and third encodings are valid, second is used.
565 {{"cp-866", "GB2312", "UTF-8"},
566 base::UTF8ToUTF16("\xE7\x8B\x97"),
567 "http://foo/?{searchTerms}{inputEncoding}",
568 "http://foo/?%B9%B7GB2312"},
569 // Second and third encodings are valid in another order, second is used.
570 {{"cp-866", "UTF-8", "GB2312"},
571 base::UTF8ToUTF16("\xE7\x8B\x97"),
572 "http://foo/?{searchTerms}{inputEncoding}",
573 "http://foo/?%E7%8B%97UTF-8"},
574 // Both encodings are invalid, fallback to UTF-8.
575 {{"cp-866", "windows-1251"},
576 base::UTF8ToUTF16("\xE7\x8B\x97"),
577 "http://foo/?{searchTerms}{inputEncoding}",
578 "http://foo/?%E7%8B%97UTF-8"},
579 // No encodings are given, fallback to UTF-8.
580 {{},
581 base::UTF8ToUTF16("\xE7\x8B\x97"),
582 "http://foo/?{searchTerms}{inputEncoding}",
583 "http://foo/?%E7%8B%97UTF-8"},
584 };
585
586 TemplateURLData data;
587 for (size_t i = 0; i < arraysize(test_data); ++i) {
588 data.SetURL(test_data[i].url);
589 data.input_encodings = test_data[i].encodings;
590 TemplateURL url(data);
591 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
592 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
593 GURL result(url.url_ref().ReplaceSearchTerms(
594 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
595 search_terms_data_));
596 ASSERT_TRUE(result.is_valid());
597 EXPECT_EQ(test_data[i].expected_result, result.spec());
598 }
599}
600
[email protected]bca359b2012-06-24 07:53:04601// Tests replacing assisted query stats (AQS) in various scenarios.
602TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
603 struct TestData {
[email protected]0085863a2013-12-06 21:19:03604 const base::string16 search_term;
[email protected]bca359b2012-06-24 07:53:04605 const std::string aqs;
606 const std::string base_url;
607 const std::string url;
608 const std::string expected_result;
609 } test_data[] = {
610 // No HTTPS, no AQS.
611 { ASCIIToUTF16("foo"),
612 "chrome.0.0l6",
613 "http://foo/",
614 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
615 "http://foo/?foo" },
616 // HTTPS available, AQS should be replaced.
617 { ASCIIToUTF16("foo"),
618 "chrome.0.0l6",
619 "https://foo/",
620 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
621 "https://foo/?fooaqs=chrome.0.0l6&" },
622 // HTTPS available, however AQS is empty.
623 { ASCIIToUTF16("foo"),
624 "",
625 "https://foo/",
626 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
627 "https://foo/?foo" },
628 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
629 { ASCIIToUTF16("foo"),
630 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44631 "http://www.google.com",
[email protected]bca359b2012-06-24 07:53:04632 "http://foo?{searchTerms}{google:assistedQueryStats}",
633 "http://foo/?foo" },
634 // A non-Google search provider with HTTPS should allow AQS.
635 { ASCIIToUTF16("foo"),
636 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44637 "https://www.google.com",
[email protected]bca359b2012-06-24 07:53:04638 "https://foo?{searchTerms}{google:assistedQueryStats}",
639 "https://foo/?fooaqs=chrome.0.0l6&" },
640 };
641 TemplateURLData data;
642 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27643 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04644 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28645 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19646 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
647 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04648 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
649 search_terms_args.assisted_query_stats = test_data[i].aqs;
[email protected]798baa8e2014-06-20 05:42:44650 search_terms_data_.set_google_base_url(test_data[i].base_url);
[email protected]ce7ee5f2014-06-16 23:41:19651 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
652 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27653 ASSERT_TRUE(result.is_valid());
654 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56655 }
656}
657
[email protected]00790562012-12-14 09:57:16658// Tests replacing cursor position.
659TEST_F(TemplateURLTest, ReplaceCursorPosition) {
660 struct TestData {
[email protected]0085863a2013-12-06 21:19:03661 const base::string16 search_term;
[email protected]00790562012-12-14 09:57:16662 size_t cursor_position;
663 const std::string url;
664 const std::string expected_result;
665 } test_data[] = {
666 { ASCIIToUTF16("foo"),
[email protected]0085863a2013-12-06 21:19:03667 base::string16::npos,
[email protected]00790562012-12-14 09:57:16668 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
669 "http://www.google.com/?foo&" },
670 { ASCIIToUTF16("foo"),
671 2,
672 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
673 "http://www.google.com/?foo&cp=2&" },
674 { ASCIIToUTF16("foo"),
675 15,
676 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
677 "http://www.google.com/?foo&cp=15&" },
678 };
679 TemplateURLData data;
680 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27681 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]00790562012-12-14 09:57:16682 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28683 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19684 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
685 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]00790562012-12-14 09:57:16686 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
687 search_terms_args.cursor_position = test_data[i].cursor_position;
[email protected]ce7ee5f2014-06-16 23:41:19688 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
689 search_terms_data_));
[email protected]00790562012-12-14 09:57:16690 ASSERT_TRUE(result.is_valid());
691 EXPECT_EQ(test_data[i].expected_result, result.spec());
692 }
693}
694
[email protected]420472b22014-06-10 13:34:43695// Tests replacing input type (&oit=).
696TEST_F(TemplateURLTest, ReplaceInputType) {
697 struct TestData {
698 const base::string16 search_term;
Steven Holte3696c9412017-08-24 18:38:03699 metrics::OmniboxInputType input_type;
[email protected]420472b22014-06-10 13:34:43700 const std::string url;
701 const std::string expected_result;
702 } test_data[] = {
Steven Holte3696c9412017-08-24 18:38:03703 {ASCIIToUTF16("foo"), metrics::OmniboxInputType::UNKNOWN,
704 "{google:baseURL}?{searchTerms}&{google:inputType}",
705 "http://www.google.com/?foo&oit=1&"},
706 {ASCIIToUTF16("foo"), metrics::OmniboxInputType::URL,
707 "{google:baseURL}?{searchTerms}&{google:inputType}",
708 "http://www.google.com/?foo&oit=3&"},
[email protected]420472b22014-06-10 13:34:43709 };
[email protected]420472b22014-06-10 13:34:43710 TemplateURLData data;
711 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27712 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]420472b22014-06-10 13:34:43713 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28714 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19715 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
716 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43717 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
718 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19719 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
720 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43721 ASSERT_TRUE(result.is_valid());
722 EXPECT_EQ(test_data[i].expected_result, result.spec());
723 }
724}
725
[email protected]9b9fa672013-11-07 06:04:52726// Tests replacing currentPageUrl.
727TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48728 struct TestData {
[email protected]0085863a2013-12-06 21:19:03729 const base::string16 search_term;
[email protected]9b9fa672013-11-07 06:04:52730 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48731 const std::string url;
732 const std::string expected_result;
733 } test_data[] = {
734 { ASCIIToUTF16("foo"),
735 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52736 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48737 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
738 { ASCIIToUTF16("foo"),
739 "",
[email protected]9b9fa672013-11-07 06:04:52740 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48741 "http://www.google.com/?foo&" },
742 { ASCIIToUTF16("foo"),
743 "http://g.com/+-/*&=",
[email protected]9b9fa672013-11-07 06:04:52744 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48745 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
746 };
747 TemplateURLData data;
748 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27749 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]800569d2013-05-06 07:38:48750 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28751 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19752 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
753 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]800569d2013-05-06 07:38:48754 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52755 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19756 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
757 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48758 ASSERT_TRUE(result.is_valid());
759 EXPECT_EQ(test_data[i].expected_result, result.spec());
760 }
761}
762
[email protected]d82443b2009-01-15 19:54:56763TEST_F(TemplateURLTest, Suggestions) {
764 struct TestData {
765 const int accepted_suggestion;
[email protected]0085863a2013-12-06 21:19:03766 const base::string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56767 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27768 } test_data[] = {
[email protected]0085863a2013-12-06 21:19:03769 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
[email protected]29653892013-03-02 19:25:37770 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30771 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37772 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03773 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(),
[email protected]29653892013-03-02 19:25:37774 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30775 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37776 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03777 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
[email protected]29653892013-03-02 19:25:37778 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56779 };
[email protected]573889f22012-04-07 01:31:54780 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37781 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
782 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54783 data.input_encodings.push_back("UTF-8");
[email protected]168d08722014-06-18 07:13:28784 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19785 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
786 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
viettrungluu37a447b2014-10-16 18:23:27787 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04788 TemplateURLRef::SearchTermsArgs search_terms_args(
789 ASCIIToUTF16("foobar"));
790 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
791 search_terms_args.original_query =
792 test_data[i].original_query_for_suggestion;
[email protected]ce7ee5f2014-06-16 23:41:19793 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
794 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27795 ASSERT_TRUE(result.is_valid());
796 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56797 }
798}
799
[email protected]81f808de2009-09-25 01:36:34800TEST_F(TemplateURLTest, RLZ) {
[email protected]798baa8e2014-06-20 05:42:44801 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(false);
[email protected]d82443b2009-01-15 19:54:56802
[email protected]573889f22012-04-07 01:31:54803 TemplateURLData data;
804 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28805 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19806 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
807 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04808 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19809 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56810 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44811 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
812 result.spec());
[email protected]d82443b2009-01-15 19:54:56813}
814
[email protected]c8ccc41d2014-04-10 04:42:12815TEST_F(TemplateURLTest, RLZFromAppList) {
[email protected]798baa8e2014-06-20 05:42:44816 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(true);
[email protected]c8ccc41d2014-04-10 04:42:12817
818 TemplateURLData data;
819 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28820 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19821 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
822 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12823 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
824 args.from_app_list = true;
[email protected]ce7ee5f2014-06-16 23:41:19825 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12826 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44827 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
828 result.spec());
[email protected]c8ccc41d2014-04-10 04:42:12829}
[email protected]c8ccc41d2014-04-10 04:42:12830
[email protected]d82443b2009-01-15 19:54:56831TEST_F(TemplateURLTest, HostAndSearchTermKey) {
832 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19833 const std::string url;
[email protected]d82443b2009-01-15 19:54:56834 const std::string host;
835 const std::string path;
836 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54837 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50838 {"http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
839 {"http://blah/{searchTerms}", "blah", "", ""},
[email protected]d82443b2009-01-15 19:54:56840
vitbarf2a11562017-05-15 14:09:50841 // No term should result in empty values.
842 {"http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56843
vitbarf2a11562017-05-15 14:09:50844 // Multiple terms should result in empty values.
845 {"http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56846
vitbarf2a11562017-05-15 14:09:50847 // Term in the host shouldn't match.
848 {"http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56849
vitbarf2a11562017-05-15 14:09:50850 {"http://blah/?q={searchTerms}", "blah", "/", "q"},
851 {"https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56852
vitbarf2a11562017-05-15 14:09:50853 // Single term with extra chars in value should match.
854 {"http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56855 };
856
viettrungluu37a447b2014-10-16 18:23:27857 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54858 TemplateURLData data;
859 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28860 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19861 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
862 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
863 EXPECT_EQ(test_data[i].search_term_key,
864 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56865 }
866}
867
alexmos294849f2015-03-14 00:55:06868TEST_F(TemplateURLTest, SearchTermKeyLocation) {
869 struct TestData {
870 const std::string url;
871 const url::Parsed::ComponentType location;
872 const std::string path;
vitbarf2a11562017-05-15 14:09:50873 const std::string key;
874 const std::string value_prefix;
875 const std::string value_suffix;
alexmos294849f2015-03-14 00:55:06876 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50877 {"http://blah/{searchTerms}/", url::Parsed::PATH, "", "", "/", "/"},
878 {"http://blah/{searchTerms}", url::Parsed::PATH, "", "", "/", ""},
879 {"http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "", "",
880 "/begin/", "/end"},
881 {"http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY, "/", "q",
882 "", ""},
883 {"http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF, "/", "x",
884 "", ""},
885 {"http://www.example.com/?q=chromium-{searchTerms}@chromium.org/info",
886 url::Parsed::QUERY, "/", "q", "chromium-", "@chromium.org/info"},
alexmos294849f2015-03-14 00:55:06887
vitbarf2a11562017-05-15 14:09:50888 // searchTerms is a key, not a value, so this should result in an empty
889 // value.
890 {"http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x",
891 url::Parsed::QUERY, "", "", "", ""},
alexmos294849f2015-03-14 00:55:06892
vitbarf2a11562017-05-15 14:09:50893 // Multiple search terms should result in empty values.
894 {"http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY, "", "",
895 "", ""},
896 {"http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY, "", "",
897 "", ""},
898 {"http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY, "",
899 "", "", ""},
alexmos294849f2015-03-14 00:55:06900 };
901
902 for (size_t i = 0; i < arraysize(test_data); ++i) {
903 TemplateURLData data;
904 data.SetURL(test_data[i].url);
905 TemplateURL url(data);
906 EXPECT_EQ(test_data[i].location,
907 url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
908 EXPECT_EQ(test_data[i].path,
909 url.url_ref().GetPath(search_terms_data_));
vitbarf2a11562017-05-15 14:09:50910 EXPECT_EQ(test_data[i].key,
911 url.url_ref().GetSearchTermKey(search_terms_data_));
912 EXPECT_EQ(test_data[i].value_prefix,
913 url.url_ref().GetSearchTermValuePrefix(search_terms_data_));
914 EXPECT_EQ(test_data[i].value_suffix,
915 url.url_ref().GetSearchTermValueSuffix(search_terms_data_));
alexmos294849f2015-03-14 00:55:06916 }
917}
918
[email protected]d82443b2009-01-15 19:54:56919TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
920 static const struct {
[email protected]ddd231e2010-06-29 20:35:19921 const char* const base_url;
922 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56923 } data[] = {
[email protected]014010e2011-10-01 04:12:44924 { "http://google.com/", "http://google.com/complete/", },
925 { "http://www.google.com/", "http://www.google.com/complete/", },
926 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
927 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
928 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56929 };
930
viettrungluu37a447b2014-10-16 18:23:27931 for (size_t i = 0; i < arraysize(data); ++i)
[email protected]d82443b2009-01-15 19:54:56932 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
933}
934
[email protected]81c6ef62010-01-21 09:58:47935TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19936 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:54937 TemplateURLData data;
938 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28939 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47940 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:13941 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19942 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47943 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27944 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47945 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
946}
947
948TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:20949 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:54950 TemplateURLData data;
951 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28952 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47953 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:44954
955 // By default, TemplateURLRef should not consider itself prepopulated.
956 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:13957 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20958 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:44959 EXPECT_TRUE(replacements.empty());
960
961 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:20962 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:54963 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:28964 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:15965 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20966 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47967 EXPECT_TRUE(replacements.empty());
968}
969
970TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:28971 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:47972 TemplateURLRef::Replacements replacements;
973 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:24974 EXPECT_EQ(std::string(), url.url_ref().ParseURL(std::string(), &replacements,
975 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:47976 EXPECT_TRUE(replacements.empty());
977 EXPECT_TRUE(valid);
978}
979
980TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:54981 TemplateURLData data;
982 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:28983 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47984 TemplateURLRef::Replacements replacements;
985 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:24986 EXPECT_EQ(std::string(),
987 url.url_ref().ParseURL("{", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:47988 EXPECT_TRUE(replacements.empty());
989 EXPECT_FALSE(valid);
990}
991
992TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:54993 TemplateURLData data;
994 data.SetURL("{}");
[email protected]168d08722014-06-18 07:13:28995 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47996 TemplateURLRef::Replacements replacements;
997 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:24998 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:47999 EXPECT_TRUE(replacements.empty());
1000 EXPECT_TRUE(valid);
1001}
1002
1003TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:541004 TemplateURLData data;
1005 data.SetURL("{}{{%s}}");
[email protected]168d08722014-06-18 07:13:281006 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471007 TemplateURLRef::Replacements replacements;
1008 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241009 EXPECT_EQ("{}{}", url.url_ref().ParseURL("{}{{searchTerms}}", &replacements,
1010 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471011 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271012 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471013 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1014 EXPECT_TRUE(valid);
1015}
1016
1017TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:541018 TemplateURLData data;
1019 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:281020 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:471021 TemplateURLRef::Replacements replacements;
1022 bool valid = false;
Ivan Kotenkov75b1c3a2017-10-24 14:47:241023 EXPECT_EQ("{", url.url_ref().ParseURL("{{searchTerms}", &replacements,
1024 nullptr, &valid));
[email protected]81c6ef62010-01-21 09:58:471025 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:271026 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:471027 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
1028 EXPECT_TRUE(valid);
1029}
[email protected]5b3078752012-10-09 18:54:161030
[email protected]fd6d8822012-12-08 06:56:111031TEST_F(TemplateURLTest, SearchClient) {
1032 const std::string base_url_str("http://google.com/?");
1033 const std::string terms_str("{searchTerms}&{google:searchClient}");
1034 const std::string full_url_str = base_url_str + terms_str;
[email protected]0085863a2013-12-06 21:19:031035 const base::string16 terms(ASCIIToUTF16(terms_str));
[email protected]798baa8e2014-06-20 05:42:441036 search_terms_data_.set_google_base_url(base_url_str);
[email protected]fd6d8822012-12-08 06:56:111037
1038 TemplateURLData data;
1039 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:281040 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191041 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1042 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111043 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
1044
1045 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:191046 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
1047 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111048 ASSERT_TRUE(result.is_valid());
1049 EXPECT_EQ("http://google.com/?foobar&", result.spec());
1050
1051 // Check that the URL is correct when a client is present.
[email protected]798baa8e2014-06-20 05:42:441052 search_terms_data_.set_search_client("search_client");
[email protected]ce7ee5f2014-06-16 23:41:191053 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
1054 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111055 ASSERT_TRUE(result_2.is_valid());
[email protected]798baa8e2014-06-20 05:42:441056 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
[email protected]fd6d8822012-12-08 06:56:111057}
[email protected]fd6d8822012-12-08 06:56:111058
[email protected]5b3078752012-10-09 18:54:161059TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
1060 TemplateURLData data;
1061 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161062 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
1063 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281064 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511065 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1066 ASSERT_EQ(3U, url_refs.size());
1067 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1068 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1069 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161070}
1071
1072TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
1073 TemplateURLData data;
1074 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:281075 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511076 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1077 ASSERT_EQ(1U, url_refs.size());
1078 EXPECT_EQ("http://www.google.co.uk/", url_refs[0].GetURL());
[email protected]5b3078752012-10-09 18:54:161079}
1080
1081TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1082 TemplateURLData data;
1083 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]5b3078752012-10-09 18:54:161084 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1085 data.alternate_urls.push_back(
1086 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281087 TemplateURL url(data);
[email protected]0085863a2013-12-06 21:19:031088 base::string16 result;
[email protected]5b3078752012-10-09 18:54:161089
1090 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191091 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191092 EXPECT_EQ(ASCIIToUTF16("something"), result);
1093
1094 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471095 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191096 EXPECT_EQ(ASCIIToUTF16("something"), result);
1097
1098 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471099 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191100 EXPECT_EQ(ASCIIToUTF16("something"), result);
1101
1102 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471103 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191104 EXPECT_EQ(ASCIIToUTF16("something"), result);
1105
1106 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191107 GURL("http://google.com/alt/#q=something"),
1108 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191109 EXPECT_EQ(ASCIIToUTF16("something"), result);
1110
1111 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471112 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191113 EXPECT_EQ(ASCIIToUTF16("something"), result);
1114
1115 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471116 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191117 EXPECT_EQ(ASCIIToUTF16("something"), result);
1118
1119 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
Marc Treibf0f6afc92017-10-04 09:05:471120 GURL("http://google.com/alt/#q=something"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161121 EXPECT_EQ(ASCIIToUTF16("something"), result);
1122
1123 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191124 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371125 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161126
1127 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191128 GURL("http://google.ca/?q=something&q=anything"),
1129 search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371130 EXPECT_EQ(base::string16(), result);
[email protected]67d8b752013-04-03 17:33:271131
1132 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191133 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371134 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161135
[email protected]32e2d81b2012-10-16 19:03:251136 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191137 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
[email protected]32e2d81b2012-10-16 19:03:251138 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:161139
1140 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191141 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371142 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161143
1144 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191145 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161146 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1147
1148 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191149 GURL("http://google.com/alt/?q=123#q=456"),
1150 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161151 EXPECT_EQ(ASCIIToUTF16("456"), result);
1152
1153 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191154 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1155 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161156 EXPECT_EQ(ASCIIToUTF16("123"), result);
1157
1158 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191159 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1160 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161161 EXPECT_EQ(ASCIIToUTF16("789"), result);
1162
1163 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191164 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371165 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161166
1167 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191168 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371169 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161170
1171 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191172 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371173 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161174
1175 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191176 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371177 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161178
1179 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191180 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161181 EXPECT_EQ(ASCIIToUTF16("123"), result);
1182}
[email protected]4076ea62013-01-09 01:47:191183
alexmos294849f2015-03-14 00:55:061184TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1185 TemplateURLData data;
1186 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1187 TemplateURL url(data);
1188 base::string16 result;
1189
1190 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1191 GURL("http://term-in-path.com/begin/something/end"),
1192 search_terms_data_, &result));
1193 EXPECT_EQ(ASCIIToUTF16("something"), result);
1194
1195 // "%20" must be converted to space.
1196 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1197 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1198 search_terms_data_, &result));
1199 EXPECT_EQ(ASCIIToUTF16("a b c"), result);
1200
1201 // Plus must not be converted to space.
1202 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1203 GURL("http://term-in-path.com/begin/1+2+3/end"),
1204 search_terms_data_, &result));
1205 EXPECT_EQ(ASCIIToUTF16("1+2+3"), result);
1206
1207 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1208 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
1209 EXPECT_EQ(base::string16(), result);
1210
1211 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1212 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
1213 EXPECT_EQ(base::string16(), result);
1214
1215 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1216 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
1217 EXPECT_EQ(base::string16(), result);
1218}
1219
vitbar27804d102015-04-15 10:54:411220// Checks that the ExtractSearchTermsFromURL function works correctly
1221// for urls containing non-latin characters in UTF8 encoding.
1222TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
1223 TemplateURLData data;
1224 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1225 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1226 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1227 TemplateURL url(data);
1228 base::string16 result;
1229
1230 // Russian text encoded with UTF-8.
1231 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551232 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1233 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
vitbar27804d102015-04-15 10:54:411234 search_terms_data_, &result));
1235 EXPECT_EQ(
1236 base::WideToUTF16(
1237 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1238 L"\x043C\x0438\x0440!"),
1239 result);
1240
1241 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551242 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1243 "%D0%B0"),
vitbar27804d102015-04-15 10:54:411244 search_terms_data_, &result));
1245 EXPECT_EQ(
1246 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1247 result);
1248
1249 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551250 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1251 "%D0%B8%20A"),
vitbar27804d102015-04-15 10:54:411252 search_terms_data_, &result));
1253 EXPECT_EQ(
1254 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1255 result);
1256}
1257
1258// Checks that the ExtractSearchTermsFromURL function works correctly
1259// for urls containing non-latin characters in non-UTF8 encoding.
1260TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) {
1261 TemplateURLData data;
1262 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1263 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1264 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1265 data.input_encodings.push_back("windows-1251");
1266 TemplateURL url(data);
1267 base::string16 result;
1268
1269 // Russian text encoded with Windows-1251.
1270 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1271 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1272 "%EC%E8%F0!"),
1273 search_terms_data_, &result));
1274 EXPECT_EQ(
1275 base::WideToUTF16(
1276 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1277 L"\x043C\x0438\x0440!"),
1278 result);
1279
1280 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1281 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1282 search_terms_data_, &result));
1283 EXPECT_EQ(
1284 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1285 result);
1286
1287 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1288 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1289 search_terms_data_, &result));
1290 EXPECT_EQ(
1291 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1292 result);
1293}
1294
jbroman50832022016-04-21 23:53:001295// Checks that the ExtractSearchTermsFromURL function strips constant
1296// prefix/suffix strings from the search terms param.
1297TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) {
1298 TemplateURLData data;
1299 data.alternate_urls.push_back(
1300 "http://www.example.com/?q=chromium-{searchTerms}@chromium.org");
1301 data.alternate_urls.push_back(
1302 "http://www.example.com/chromium-{searchTerms}@chromium.org/info");
1303 TemplateURL url(data);
1304 base::string16 result;
1305
1306 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1307 GURL("http://www.example.com/[email protected]"),
1308 search_terms_data_, &result));
1309 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1310
1311 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1312 GURL("http://www.example.com/[email protected]/info"),
1313 search_terms_data_, &result));
1314 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1315
1316 // Don't match if the prefix and suffix aren't there.
1317 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1318 GURL("http://www.example.com/?q=invalid"), search_terms_data_, &result));
jbromane8b8728d2017-04-12 17:29:391319
1320 // Don't match if the prefix and suffix overlap.
1321 TemplateURLData data_with_overlap;
1322 data.alternate_urls.push_back(
1323 "http://www.example.com/?q=goo{searchTerms}oogle");
1324 TemplateURL url_with_overlap(data);
1325 EXPECT_FALSE(url_with_overlap.ExtractSearchTermsFromURL(
1326 GURL("http://www.example.com/?q=google"), search_terms_data_, &result));
jbroman50832022016-04-21 23:53:001327}
1328
[email protected]f62e30f52013-03-23 03:45:151329TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1330 TemplateURLData data;
1331 data.SetURL("http://google.com/?q={searchTerms}");
[email protected]f62e30f52013-03-23 03:45:151332 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1333 data.alternate_urls.push_back(
1334 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281335 TemplateURL url(data);
[email protected]f62e30f52013-03-23 03:45:151336 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1337 GURL result;
1338
1339 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191340 GURL("http://google.com/?q=something"), search_terms,
1341 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101342 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151343
1344 result = GURL("http://should.not.change.com");
1345 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191346 GURL("http://google.ca/?q=something"), search_terms,
1347 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151348 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1349
1350 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191351 GURL("http://google.com/foo/?q=foo"), search_terms,
1352 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151353
1354 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191355 GURL("https://google.com/?q=foo"), search_terms,
1356 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101357 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151358
1359 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191360 GURL("http://google.com:8080/?q=foo"), search_terms,
1361 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151362
1363 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191364 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1365 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101366 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result);
[email protected]f62e30f52013-03-23 03:45:151367
1368 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1369 // template_url.cc for details.
1370 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191371 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1372 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101373 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151374
1375 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1376 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191377 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101378 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
[email protected]f62e30f52013-03-23 03:45:151379 result);
1380
1381 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1382 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191383 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151384 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
vitbarf298455d2015-04-21 12:58:101385 "#j=abc&q=Bob+Morane&h=def9"), result);
[email protected]f62e30f52013-03-23 03:45:151386
1387 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191388 GURL("http://google.com/alt/?q="), search_terms,
1389 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151390
1391 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191392 GURL("http://google.com/alt/?#q="), search_terms,
1393 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151394
1395 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191396 GURL("http://google.com/alt/?q=#q="), search_terms,
1397 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151398
1399 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191400 GURL("http://google.com/alt/?q=123#q="), search_terms,
1401 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151402
1403 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191404 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1405 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101406 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151407}
[email protected]56fa29592013-07-02 20:25:531408
alexmos294849f2015-03-14 00:55:061409TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1410 TemplateURLData data;
1411 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1412 TemplateURL url(data);
1413 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1414 GURL result;
1415
1416 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1417 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1418 search_terms_data_, &result));
1419 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1420
1421 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1422 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1423 search_terms_data_, &result));
1424 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1425
1426 result = GURL("http://should.not.change.com");
1427 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1428 GURL("http://term-in-path.com/about"), search_terms,
1429 search_terms_data_, &result));
1430 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1431}
1432
vitbarf298455d2015-04-21 12:58:101433// Checks that the ReplaceSearchTermsInURL function works correctly
1434// for search terms containing non-latin characters for a search engine
1435// using UTF-8 input encoding.
1436TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) {
1437 TemplateURLData data;
1438 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1439 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1440 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1441 TemplateURL url(data);
1442
1443 // Russian text which will be encoded with UTF-8.
1444 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1445 L"\x0442\x0435\x043A\x0441\x0442"));
1446 GURL result;
1447
1448 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1449 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_,
1450 &result));
1451 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1452 result);
1453
1454 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1455 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_,
1456 &result));
1457 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1458 result);
1459
1460 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1461 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_,
1462 &result));
1463 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1464 result);
1465}
1466
1467// Checks that the ReplaceSearchTermsInURL function works correctly
1468// for search terms containing non-latin characters for a search engine
1469// using non UTF-8 input encoding.
1470TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) {
1471 TemplateURLData data;
1472 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1473 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1474 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1475 data.input_encodings.push_back("windows-1251");
1476 TemplateURL url(data);
1477
1478 // Russian text which will be encoded with Windows-1251.
1479 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1480 L"\x0442\x0435\x043A\x0441\x0442"));
1481 GURL result;
1482
1483 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1484 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_,
1485 &result));
1486 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1487 result);
1488
1489 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1490 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_,
1491 &result));
1492 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1493 result);
1494
1495 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1496 GURL("http://windows-1251.ru/path/a%20b"), search_terms,
1497 search_terms_data_, &result));
1498 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1499 result);
1500}
1501
[email protected]621ade062013-10-28 06:27:431502// Test the |suggest_query_params| field of SearchTermsArgs.
1503TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431504 TemplateURLData data;
1505 // Pick a URL with replacements before, during, and after the query, to ensure
1506 // we don't goof up any of them.
1507 data.SetURL("{google:baseURL}search?q={searchTerms}"
1508 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281509 TemplateURL url(data);
[email protected]621ade062013-10-28 06:27:431510
1511 // Baseline: no |suggest_query_params| field.
1512 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1513 search_terms.original_query = ASCIIToUTF16("def");
1514 search_terms.accepted_suggestion = 0;
1515 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191516 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431517
1518 // Set the suggest_query_params.
1519 search_terms.suggest_query_params = "pq=xyz";
1520 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191521 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431522
1523 // Add extra_query_params in the mix, and ensure it works.
1524 search_terms.append_extra_query_params = 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
[email protected]56fa29592013-07-02 20:25:531531// Test the |append_extra_query_params| field of SearchTermsArgs.
1532TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531533 TemplateURLData data;
1534 // Pick a URL with replacements before, during, and after the query, to ensure
1535 // we don't goof up any of them.
1536 data.SetURL("{google:baseURL}search?q={searchTerms}"
1537 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281538 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531539
1540 // Baseline: no command-line args, no |append_extra_query_params| flag.
1541 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1542 search_terms.original_query = ASCIIToUTF16("def");
1543 search_terms.accepted_suggestion = 0;
1544 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191545 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531546
1547 // Set the flag. Since there are no command-line args, this should have no
1548 // effect.
1549 search_terms.append_extra_query_params = true;
1550 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191551 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531552
1553 // Now append the command-line arg. This should be inserted into the query.
avi1772c1a2014-12-22 22:42:331554 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]56fa29592013-07-02 20:25:531555 switches::kExtraSearchQueryParams, "a=b");
1556 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191557 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531558
1559 // Turn off the flag. Now the command-line arg should be ignored again.
1560 search_terms.append_extra_query_params = false;
1561 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191562 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531563}
[email protected]d5015ca2013-08-08 22:04:181564
1565// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171566TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181567 TemplateURLData data;
1568 data.input_encodings.push_back("UTF-8");
1569 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281570 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191571 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1572 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]d5015ca2013-08-08 22:04:181573 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1574
[email protected]ce7ee5f2014-06-16 23:41:191575 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1576 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181577 EXPECT_EQ("http://www.google.com/?q=foo", result);
1578
[email protected]332d17d22014-06-20 16:56:031579 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191580 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1581 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181582 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1583
1584 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031585 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191586 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1587 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181588 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1589}
[email protected]2328ee22013-08-08 23:00:191590
1591// Test the IsSearchResults function.
1592TEST_F(TemplateURLTest, IsSearchResults) {
1593 TemplateURLData data;
1594 data.SetURL("http://bar/search?q={searchTerms}");
[email protected]2767c0fd2013-08-16 17:44:161595 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191596 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1597 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1598 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1599 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281600 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191601
1602 const struct {
1603 const char* const url;
1604 bool result;
1605 } url_data[] = {
1606 { "http://bar/search?q=foo&oq=foo", true, },
1607 { "http://bar/?q=foo&oq=foo", true, },
1608 { "http://bar/#output=search&q=foo&oq=foo", true, },
1609 { "http://bar/webhp#q=foo&oq=foo", true, },
1610 { "http://bar/#q=foo&oq=foo", true, },
1611 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1612 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1613 { "http://bar/", false, },
1614 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161615 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191616 };
1617
viettrungluu37a447b2014-10-16 18:23:271618 for (size_t i = 0; i < arraysize(url_data); ++i) {
[email protected]2328ee22013-08-08 23:00:191619 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191620 search_provider.IsSearchURL(GURL(url_data[i].url),
1621 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191622 }
1623}
[email protected]a048de8a2013-10-02 18:30:061624
jdonnelly41c5b46a2015-07-10 21:24:381625TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) {
[email protected]9d70de12014-05-10 02:15:311626 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441627 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311628 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1629
[email protected]168d08722014-06-18 07:13:281630 TemplateURL url(data);
[email protected]9d70de12014-05-10 02:15:311631 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191632 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1633 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311634 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1635}
[email protected]20184242014-05-14 02:57:421636
1637TEST_F(TemplateURLTest, SessionToken) {
1638 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441639 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421640 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1641
[email protected]168d08722014-06-18 07:13:281642 TemplateURL url(data);
[email protected]20184242014-05-14 02:57:421643 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1644 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191645 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1646 search_terms_data_);
[email protected]20184242014-05-14 02:57:421647 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1648
[email protected]168d08722014-06-18 07:13:281649 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421650 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191651 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1652 search_terms_data_);
[email protected]20184242014-05-14 02:57:421653 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1654}
[email protected]448b17f52014-06-13 01:08:031655
1656TEST_F(TemplateURLTest, ContextualSearchParameters) {
1657 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441658 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031659 data.SetURL("http://bar/_/contextualsearch?"
1660 "{google:contextualSearchVersion}"
1661 "{google:contextualSearchContextData}");
1662
[email protected]168d08722014-06-18 07:13:281663 TemplateURL url(data);
[email protected]448b17f52014-06-13 01:08:031664 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191665 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1666 search_terms_data_);
donndbb98274e2016-11-01 21:04:401667 EXPECT_EQ("http://bar/_/contextualsearch?", result);
[email protected]448b17f52014-06-13 01:08:031668
donnd0bc73292017-01-17 22:07:561669 // Test the current common case, which uses no home country.
1670 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(2, 1,
1671 std::string());
[email protected]448b17f52014-06-13 01:08:031672 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191673 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1674 search_terms_data_);
donndd9fc4d92016-09-16 00:30:491675 EXPECT_EQ(
1676 "http://bar/_/contextualsearch?"
donnd0bc73292017-01-17 22:07:561677 "ctxs=2&"
donndd9fc4d92016-09-16 00:30:491678 "ctxsl_coca=1",
1679 result);
donndbb98274e2016-11-01 21:04:401680
donnd0bc73292017-01-17 22:07:561681 // Test the home country case.
donndbb98274e2016-11-01 21:04:401682 search_terms_args.contextual_search_params =
donnd0bc73292017-01-17 22:07:561683 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(2, 2, "CH");
donndbb98274e2016-11-01 21:04:401684 result =
1685 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1686
1687 EXPECT_EQ(
1688 "http://bar/_/contextualsearch?"
1689 "ctxs=2&"
donnd0bc73292017-01-17 22:07:561690 "ctxsl_coca=2&"
1691 "ctxs_hc=CH",
donndbb98274e2016-11-01 21:04:401692 result);
[email protected]448b17f52014-06-13 01:08:031693}
[email protected]44ccc9f2014-06-20 17:36:211694
1695TEST_F(TemplateURLTest, GenerateKeyword) {
1696 ASSERT_EQ(ASCIIToUTF16("foo"),
jshin1fb76462016-04-05 22:13:031697 TemplateURL::GenerateKeyword(GURL("http://foo")));
[email protected]44ccc9f2014-06-20 17:36:211698 // www. should be stripped.
jshin1fb76462016-04-05 22:13:031699 ASSERT_EQ(ASCIIToUTF16("foo"),
1700 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
[email protected]44ccc9f2014-06-20 17:36:211701 // Make sure we don't get a trailing '/'.
jshin1fb76462016-04-05 22:13:031702 ASSERT_EQ(ASCIIToUTF16("blah"),
1703 TemplateURL::GenerateKeyword(GURL("http://blah/")));
[email protected]44ccc9f2014-06-20 17:36:211704 // Don't generate the empty string.
jshin1fb76462016-04-05 22:13:031705 ASSERT_EQ(ASCIIToUTF16("www"),
1706 TemplateURL::GenerateKeyword(GURL("http://www.")));
alshabaline6212ac2015-07-23 08:03:001707 ASSERT_EQ(
1708 base::UTF8ToUTF16("\xd0\xb0\xd0\xb1\xd0\xb2"),
jshin1fb76462016-04-05 22:13:031709 TemplateURL::GenerateKeyword(GURL("http://xn--80acd")));
a-v-y81695d0d2017-04-20 08:22:221710
1711 // Generated keywords must always be in lowercase, because TemplateURLs always
1712 // converts keywords to lowercase in its constructor and TemplateURLService
1713 // stores TemplateURLs in maps using keyword as key.
1714 EXPECT_TRUE(IsLowerCase(TemplateURL::GenerateKeyword(GURL("http://BLAH/"))));
1715 EXPECT_TRUE(IsLowerCase(
1716 TemplateURL::GenerateKeyword(GURL("http://embeddedhtml.<head>/"))));
[email protected]44ccc9f2014-06-20 17:36:211717}
1718
1719TEST_F(TemplateURLTest, GenerateSearchURL) {
1720 struct GenerateSearchURLCase {
1721 const char* test_name;
1722 const char* url;
1723 const char* expected;
1724 } generate_url_cases[] = {
1725 { "invalid URL", "foo{searchTerms}", "" },
1726 { "URL with no replacements", "http://foo/", "http://foo/" },
1727 { "basic functionality", "http://foo/{searchTerms}",
1728 "http://foo/blah.blah.blah.blah.blah" }
1729 };
1730
viettrungluu37a447b2014-10-16 18:23:271731 for (size_t i = 0; i < arraysize(generate_url_cases); ++i) {
[email protected]44ccc9f2014-06-20 17:36:211732 TemplateURLData data;
1733 data.SetURL(generate_url_cases[i].url);
1734 TemplateURL t_url(data);
1735 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1736 generate_url_cases[i].expected)
1737 << generate_url_cases[i].test_name << " failed.";
1738 }
1739}
[email protected]9e9130ce2014-06-23 23:20:511740
1741TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1742 TemplateURLData data;
1743 search_terms_data_.set_google_base_url("http://bar/");
1744 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1745
1746 TemplateURL url(data);
1747 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1748 search_terms_args.prefetch_query = "full query text";
1749 search_terms_args.prefetch_query_type = "2338";
1750 std::string result =
1751 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1752 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1753 result);
1754
1755 TemplateURL url2(data);
1756 search_terms_args.prefetch_query.clear();
1757 search_terms_args.prefetch_query_type.clear();
1758 result =
1759 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1760 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1761}
vitbar5bd8c252016-01-29 18:44:511762
1763// Tests that TemplateURL works correctly after changing the Google base URL
1764// and invalidating cached values.
1765TEST_F(TemplateURLTest, InvalidateCachedValues) {
1766 TemplateURLData data;
1767 data.SetURL("{google:baseURL}search?q={searchTerms}");
1768 data.suggestions_url = "{google:baseSuggestURL}search?q={searchTerms}";
vitbar5bd8c252016-01-29 18:44:511769 data.image_url = "{google:baseURL}searchbyimage/upload";
1770 data.new_tab_url = "{google:baseURL}_/chrome/newtab";
1771 data.contextual_search_url = "{google:baseURL}_/contextualsearch";
1772 data.alternate_urls.push_back("{google:baseURL}s#q={searchTerms}");
1773 TemplateURL url(data);
1774 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("X"));
1775 base::string16 search_terms;
1776
1777 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1778 EXPECT_EQ("http://www.google.com/search?q=X",
1779 url.url_ref().ReplaceSearchTerms(search_terms_args,
1780 search_terms_data_));
1781 EXPECT_EQ("http://www.google.com/s#q=X",
1782 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1783 search_terms_data_));
1784 EXPECT_EQ("http://www.google.com/search?q=X",
1785 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1786 search_terms_data_));
1787 EXPECT_EQ("http://www.google.com/complete/search?q=X",
1788 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1789 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:511790 EXPECT_EQ("http://www.google.com/searchbyimage/upload",
1791 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1792 search_terms_data_));
1793 EXPECT_EQ("http://www.google.com/_/chrome/newtab",
1794 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1795 search_terms_data_));
1796 EXPECT_EQ("http://www.google.com/_/contextualsearch",
1797 url.contextual_search_url_ref().ReplaceSearchTerms(
1798 search_terms_args, search_terms_data_));
1799
1800 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1801 GURL("http://www.google.com/search?q=Y+Z"),
1802 search_terms_data_, &search_terms));
1803 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
1804 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1805 GURL("http://www.google.com/s#q=123"),
1806 search_terms_data_, &search_terms));
1807 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
1808
1809 search_terms_data_.set_google_base_url("https://www.foo.org/");
1810 url.InvalidateCachedValues();
1811
1812 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1813 EXPECT_EQ("https://www.foo.org/search?q=X",
1814 url.url_ref().ReplaceSearchTerms(search_terms_args,
1815 search_terms_data_));
1816 EXPECT_EQ("https://www.foo.org/s#q=X",
1817 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1818 search_terms_data_));
1819 EXPECT_EQ("https://www.foo.org/search?q=X",
1820 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1821 search_terms_data_));
1822 EXPECT_EQ("https://www.foo.org/complete/search?q=X",
1823 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1824 search_terms_data_));
vitbar5bd8c252016-01-29 18:44:511825 EXPECT_EQ("https://www.foo.org/searchbyimage/upload",
1826 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1827 search_terms_data_));
1828 EXPECT_EQ("https://www.foo.org/_/chrome/newtab",
1829 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1830 search_terms_data_));
1831 EXPECT_EQ("https://www.foo.org/_/contextualsearch",
1832 url.contextual_search_url_ref().ReplaceSearchTerms(
1833 search_terms_args, search_terms_data_));
1834
1835 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1836 GURL("https://www.foo.org/search?q=Y+Z"),
1837 search_terms_data_, &search_terms));
1838 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
1839 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1840 GURL("https://www.foo.org/s#q=123"),
1841 search_terms_data_, &search_terms));
1842 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
1843
1844 search_terms_data_.set_google_base_url("http://www.google.com/");
1845}