blob: 211be3d7eab9bce8d172684fe952f11909f2b836 [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]332d17d22014-06-20 16:56:0315#include "components/metrics/proto/omnibox_event.pb.h"
[email protected]420472b22014-06-10 13:34:4316#include "components/metrics/proto/omnibox_input_type.pb.h"
[email protected]5c3c6e482014-06-23 09:47:1817#include "components/search_engines/search_engines_switches.h"
[email protected]798baa8e2014-06-20 05:42:4418#include "components/search_engines/search_terms_data.h"
[email protected]d550cb02014-06-25 06:48:1119#include "components/search_engines/template_url.h"
hashimoto3c831812014-08-25 07:40:2320#include "components/search_engines/testing_search_terms_data.h"
a-v-y02d3f50b2017-02-16 12:03:5521#include "net/base/url_util.h"
[email protected]d82443b2009-01-15 19:54:5622#include "testing/gtest/include/gtest/gtest.h"
23
[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
[email protected]d88cb202011-08-17 20:03:01266// Test that setting the prepopulate ID from TemplateURL causes the stored
267// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44268TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54269 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20270 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]168d08722014-06-18 07:13:28271 TemplateURL url(data);
[email protected]1a257262011-06-28 22:15:44272 TemplateURLRef::Replacements replacements;
273 bool valid = false;
[email protected]243abf32012-05-15 18:28:20274 EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL(
[email protected]93b29062013-07-12 03:09:09275 "http://foo{fhqwhgads}bar", &replacements, NULL, &valid));
[email protected]1a257262011-06-28 22:15:44276 EXPECT_TRUE(replacements.empty());
277 EXPECT_TRUE(valid);
278
[email protected]573889f22012-04-07 01:31:54279 data.prepopulate_id = 123;
[email protected]168d08722014-06-18 07:13:28280 TemplateURL url2(data);
[email protected]243abf32012-05-15 18:28:20281 EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar",
[email protected]93b29062013-07-12 03:09:09282 &replacements, NULL,
283 &valid));
[email protected]1a257262011-06-28 22:15:44284 EXPECT_TRUE(replacements.empty());
285 EXPECT_TRUE(valid);
286}
287
vitbar90902c42016-05-11 06:16:55288// Test that setting the prepopulate ID from TemplateURL causes the stored
289// TemplateURLRef to handle parsing the URL parameters differently.
290TEST_F(TemplateURLTest, SetPrepopulatedAndReplace) {
291 TemplateURLData data;
292 data.SetURL("http://foo{fhqwhgads}search/?q={searchTerms}");
293 data.suggestions_url = "http://foo{fhqwhgads}suggest/?q={searchTerms}";
294 data.instant_url = "http://foo{fhqwhgads}instant/";
295 data.image_url = "http://foo{fhqwhgads}image/";
296 data.new_tab_url = "http://foo{fhqwhgads}newtab/";
297 data.contextual_search_url = "http://foo{fhqwhgads}context/";
298 data.alternate_urls.push_back(
299 "http://foo{fhqwhgads}alternate/?q={searchTerms}");
300
301 TemplateURLRef::SearchTermsArgs args(base::ASCIIToUTF16("X"));
302 const SearchTermsData& stdata = search_terms_data_;
303
304 TemplateURL url(data);
305 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
306 url.url_ref().ReplaceSearchTerms(args, stdata));
307 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dalternate/?q=X",
308 url.url_refs()[0].ReplaceSearchTerms(args, stdata));
309 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
310 url.url_refs()[1].ReplaceSearchTerms(args, stdata));
311 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsuggest/?q=X",
312 url.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
313 EXPECT_EQ("http://foo{fhqwhgads}instant/",
314 url.instant_url_ref().ReplaceSearchTerms(args, stdata));
315 EXPECT_EQ("http://foo{fhqwhgads}image/",
316 url.image_url_ref().ReplaceSearchTerms(args, stdata));
317 EXPECT_EQ("http://foo{fhqwhgads}newtab/",
318 url.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
319 EXPECT_EQ("http://foo{fhqwhgads}context/",
320 url.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
321
322 data.prepopulate_id = 123;
323 TemplateURL url2(data);
324 EXPECT_EQ("http://foosearch/?q=X",
325 url2.url_ref().ReplaceSearchTerms(args, stdata));
326 EXPECT_EQ("http://fooalternate/?q=X",
327 url2.url_refs()[0].ReplaceSearchTerms(args, stdata));
328 EXPECT_EQ("http://foosearch/?q=X",
329 url2.url_refs()[1].ReplaceSearchTerms(args, stdata));
330 EXPECT_EQ("http://foosuggest/?q=X",
331 url2.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
332 EXPECT_EQ("http://fooinstant/",
333 url2.instant_url_ref().ReplaceSearchTerms(args, stdata));
334 EXPECT_EQ("http://fooimage/",
335 url2.image_url_ref().ReplaceSearchTerms(args, stdata));
336 EXPECT_EQ("http://foonewtab/",
337 url2.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
338 EXPECT_EQ("http://foocontext/",
339 url2.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
340}
341
[email protected]d82443b2009-01-15 19:54:56342TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54343 TemplateURLData data;
344 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]168d08722014-06-18 07:13:28345 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19346 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
347 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04348 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19349 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56350 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27351 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56352}
353
354TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54355 TemplateURLData data;
356 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]168d08722014-06-18 07:13:28357 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19358 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
359 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04360 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19361 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56362 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27363 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56364}
365
[email protected]375bd7312010-08-30 22:18:13366TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
367 struct SearchTermsCase {
368 const char* url;
[email protected]0085863a2013-12-06 21:19:03369 const base::string16 terms;
[email protected]375bd7312010-08-30 22:18:13370 const char* output;
371 } search_term_cases[] = {
[email protected]0085863a2013-12-06 21:19:03372 { "{google:baseURL}{language}{searchTerms}", base::string16(),
[email protected]b37bdfe2012-03-16 20:53:27373 "http://example.com/e/en" },
[email protected]0085863a2013-12-06 21:19:03374 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
[email protected]014010e2011-10-01 04:12:44375 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13376 };
377
hashimoto3c831812014-08-25 07:40:23378 TestingSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54379 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27380 for (size_t i = 0; i < arraysize(search_term_cases); ++i) {
[email protected]375bd7312010-08-30 22:18:13381 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54382 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:28383 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19384 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data));
385 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data));
386 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]93b29062013-07-12 03:09:09387 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL));
[email protected]375bd7312010-08-30 22:18:13388 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27389 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13390 }
391}
392
[email protected]d82443b2009-01-15 19:54:56393TEST_F(TemplateURLTest, URLRefTermToWide) {
394 struct ToWideCase {
395 const char* encoded_search_term;
[email protected]0085863a2013-12-06 21:19:03396 const base::string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56397 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30398 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56399 // Test some big-5 input.
[email protected]f911df52013-12-24 23:24:23400 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56401 // Test some UTF-8 input. We should fall back to this when the encoding
402 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
403 // Big-5 trailing byte.
[email protected]f911df52013-12-24 23:24:23404 {"%e4%bd%a05%e5%a5%bd+to+you",
405 base::WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56406 // Undecodable input should stay escaped.
[email protected]f911df52013-12-24 23:24:23407 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44408 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30409 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44410 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30411 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56412 };
413
[email protected]d82443b2009-01-15 19:54:56414 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54415 TemplateURLData data;
416 data.SetURL("http://foo?q={searchTerms}");
417 data.input_encodings.push_back("big-5");
[email protected]168d08722014-06-18 07:13:28418 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19419 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
420 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
viettrungluu37a447b2014-10-16 18:23:27421 for (size_t i = 0; i < arraysize(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07422 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13423 url.url_ref().SearchTermToString16(
424 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56425 }
426}
427
[email protected]d82443b2009-01-15 19:54:56428TEST_F(TemplateURLTest, DisplayURLToURLRef) {
429 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19430 const std::string url;
[email protected]0085863a2013-12-06 21:19:03431 const base::string16 expected_result;
[email protected]b37bdfe2012-03-16 20:53:27432 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19433 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30434 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19435 { "http://X",
[email protected]400b133f2011-01-19 18:32:30436 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19437 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30438 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19439 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30440 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56441 };
[email protected]573889f22012-04-07 01:31:54442 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27443 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54444 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28445 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19446 EXPECT_EQ(test_data[i].expected_result,
447 url.url_ref().DisplayURL(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27448 EXPECT_EQ(test_data[i].url,
[email protected]ce7ee5f2014-06-16 23:41:19449 TemplateURLRef::DisplayURLToURLRef(
450 url.url_ref().DisplayURL(search_terms_data_)));
[email protected]d82443b2009-01-15 19:54:56451 }
452}
453
454TEST_F(TemplateURLTest, ReplaceSearchTerms) {
455 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19456 const std::string url;
[email protected]d82443b2009-01-15 19:54:56457 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27458 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19459 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56460 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19461 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56462 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19463 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56464 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19465 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56466 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19467 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56468 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19469 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56470 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19471 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56472 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19473 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56474 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19475 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56476 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19477 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56478 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19479 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56480 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19481 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56482 "http://foo/UTF-8a{language}aXa" },
483 };
[email protected]573889f22012-04-07 01:31:54484 TemplateURLData data;
485 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27486 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54487 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28488 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19489 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
490 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27491 std::string expected_result = test_data[i].expected_result;
brettwe6dae462015-06-24 20:54:45492 base::ReplaceSubstringsAfterOffset(
493 &expected_result, 0, "{language}",
494 search_terms_data_.GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04495 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19496 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
497 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27498 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56499 EXPECT_EQ(expected_result, result.spec());
500 }
501}
502
503
504// Tests replacing search terms in various encodings and making sure the
505// generated URL matches the expected value.
506TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
507 struct TestData {
508 const std::string encoding;
[email protected]0085863a2013-12-06 21:19:03509 const base::string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19510 const std::string url;
[email protected]d82443b2009-01-15 19:54:56511 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27512 } test_data[] = {
[email protected]f911df52013-12-24 23:24:23513 { "BIG5", base::WideToUTF16(L"\x60BD"),
[email protected]400b133f2011-01-19 18:32:30514 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17515 "http://foo/?%B1~BIG5" },
[email protected]400b133f2011-01-19 18:32:30516 { "UTF-8", ASCIIToUTF16("blah"),
517 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17518 "http://foo/?blahUTF-8" },
[email protected]f911df52013-12-24 23:24:23519 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
[email protected]34e24852012-01-31 18:43:58520 "http://foo/{searchTerms}/bar",
521 "http://foo/%82%A0/bar"},
[email protected]f911df52013-12-24 23:24:23522 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
[email protected]34e24852012-01-31 18:43:58523 "http://foo/{searchTerms}/bar",
524 "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56525 };
[email protected]573889f22012-04-07 01:31:54526 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27527 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54528 data.SetURL(test_data[i].url);
529 data.input_encodings.clear();
530 data.input_encodings.push_back(test_data[i].encoding);
[email protected]168d08722014-06-18 07:13:28531 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19532 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
533 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04534 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19535 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
536 search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04537 ASSERT_TRUE(result.is_valid());
538 EXPECT_EQ(test_data[i].expected_result, result.spec());
539 }
540}
541
542// Tests replacing assisted query stats (AQS) in various scenarios.
543TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
544 struct TestData {
[email protected]0085863a2013-12-06 21:19:03545 const base::string16 search_term;
[email protected]bca359b2012-06-24 07:53:04546 const std::string aqs;
547 const std::string base_url;
548 const std::string url;
549 const std::string expected_result;
550 } test_data[] = {
551 // No HTTPS, no AQS.
552 { ASCIIToUTF16("foo"),
553 "chrome.0.0l6",
554 "http://foo/",
555 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
556 "http://foo/?foo" },
557 // HTTPS available, AQS should be replaced.
558 { ASCIIToUTF16("foo"),
559 "chrome.0.0l6",
560 "https://foo/",
561 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
562 "https://foo/?fooaqs=chrome.0.0l6&" },
563 // HTTPS available, however AQS is empty.
564 { ASCIIToUTF16("foo"),
565 "",
566 "https://foo/",
567 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
568 "https://foo/?foo" },
569 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
570 { ASCIIToUTF16("foo"),
571 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44572 "http://www.google.com",
[email protected]bca359b2012-06-24 07:53:04573 "http://foo?{searchTerms}{google:assistedQueryStats}",
574 "http://foo/?foo" },
575 // A non-Google search provider with HTTPS should allow AQS.
576 { ASCIIToUTF16("foo"),
577 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44578 "https://www.google.com",
[email protected]bca359b2012-06-24 07:53:04579 "https://foo?{searchTerms}{google:assistedQueryStats}",
580 "https://foo/?fooaqs=chrome.0.0l6&" },
581 };
582 TemplateURLData data;
583 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27584 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04585 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28586 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19587 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
588 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04589 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
590 search_terms_args.assisted_query_stats = test_data[i].aqs;
[email protected]798baa8e2014-06-20 05:42:44591 search_terms_data_.set_google_base_url(test_data[i].base_url);
[email protected]ce7ee5f2014-06-16 23:41:19592 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
593 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27594 ASSERT_TRUE(result.is_valid());
595 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56596 }
597}
598
[email protected]00790562012-12-14 09:57:16599// Tests replacing cursor position.
600TEST_F(TemplateURLTest, ReplaceCursorPosition) {
601 struct TestData {
[email protected]0085863a2013-12-06 21:19:03602 const base::string16 search_term;
[email protected]00790562012-12-14 09:57:16603 size_t cursor_position;
604 const std::string url;
605 const std::string expected_result;
606 } test_data[] = {
607 { ASCIIToUTF16("foo"),
[email protected]0085863a2013-12-06 21:19:03608 base::string16::npos,
[email protected]00790562012-12-14 09:57:16609 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
610 "http://www.google.com/?foo&" },
611 { ASCIIToUTF16("foo"),
612 2,
613 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
614 "http://www.google.com/?foo&cp=2&" },
615 { ASCIIToUTF16("foo"),
616 15,
617 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
618 "http://www.google.com/?foo&cp=15&" },
619 };
620 TemplateURLData data;
621 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27622 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]00790562012-12-14 09:57:16623 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28624 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19625 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
626 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]00790562012-12-14 09:57:16627 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
628 search_terms_args.cursor_position = test_data[i].cursor_position;
[email protected]ce7ee5f2014-06-16 23:41:19629 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
630 search_terms_data_));
[email protected]00790562012-12-14 09:57:16631 ASSERT_TRUE(result.is_valid());
632 EXPECT_EQ(test_data[i].expected_result, result.spec());
633 }
634}
635
[email protected]420472b22014-06-10 13:34:43636// Tests replacing input type (&oit=).
637TEST_F(TemplateURLTest, ReplaceInputType) {
638 struct TestData {
639 const base::string16 search_term;
Steven Holte3696c9412017-08-24 18:38:03640 metrics::OmniboxInputType input_type;
[email protected]420472b22014-06-10 13:34:43641 const std::string url;
642 const std::string expected_result;
643 } test_data[] = {
Steven Holte3696c9412017-08-24 18:38:03644 {ASCIIToUTF16("foo"), metrics::OmniboxInputType::UNKNOWN,
645 "{google:baseURL}?{searchTerms}&{google:inputType}",
646 "http://www.google.com/?foo&oit=1&"},
647 {ASCIIToUTF16("foo"), metrics::OmniboxInputType::URL,
648 "{google:baseURL}?{searchTerms}&{google:inputType}",
649 "http://www.google.com/?foo&oit=3&"},
[email protected]420472b22014-06-10 13:34:43650 };
[email protected]420472b22014-06-10 13:34:43651 TemplateURLData data;
652 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27653 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]420472b22014-06-10 13:34:43654 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28655 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19656 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
657 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43658 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
659 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19660 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
661 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43662 ASSERT_TRUE(result.is_valid());
663 EXPECT_EQ(test_data[i].expected_result, result.spec());
664 }
665}
666
[email protected]9b9fa672013-11-07 06:04:52667// Tests replacing currentPageUrl.
668TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48669 struct TestData {
[email protected]0085863a2013-12-06 21:19:03670 const base::string16 search_term;
[email protected]9b9fa672013-11-07 06:04:52671 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48672 const std::string url;
673 const std::string expected_result;
674 } test_data[] = {
675 { ASCIIToUTF16("foo"),
676 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52677 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48678 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
679 { ASCIIToUTF16("foo"),
680 "",
[email protected]9b9fa672013-11-07 06:04:52681 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48682 "http://www.google.com/?foo&" },
683 { ASCIIToUTF16("foo"),
684 "http://g.com/+-/*&=",
[email protected]9b9fa672013-11-07 06:04:52685 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48686 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
687 };
688 TemplateURLData data;
689 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27690 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]800569d2013-05-06 07:38:48691 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28692 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19693 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
694 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]800569d2013-05-06 07:38:48695 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52696 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19697 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
698 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48699 ASSERT_TRUE(result.is_valid());
700 EXPECT_EQ(test_data[i].expected_result, result.spec());
701 }
702}
703
[email protected]d82443b2009-01-15 19:54:56704TEST_F(TemplateURLTest, Suggestions) {
705 struct TestData {
706 const int accepted_suggestion;
[email protected]0085863a2013-12-06 21:19:03707 const base::string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56708 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27709 } test_data[] = {
[email protected]0085863a2013-12-06 21:19:03710 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
[email protected]29653892013-03-02 19:25:37711 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30712 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37713 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03714 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(),
[email protected]29653892013-03-02 19:25:37715 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30716 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37717 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03718 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
[email protected]29653892013-03-02 19:25:37719 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56720 };
[email protected]573889f22012-04-07 01:31:54721 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37722 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
723 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54724 data.input_encodings.push_back("UTF-8");
[email protected]168d08722014-06-18 07:13:28725 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19726 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
727 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
viettrungluu37a447b2014-10-16 18:23:27728 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04729 TemplateURLRef::SearchTermsArgs search_terms_args(
730 ASCIIToUTF16("foobar"));
731 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
732 search_terms_args.original_query =
733 test_data[i].original_query_for_suggestion;
[email protected]ce7ee5f2014-06-16 23:41:19734 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
735 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27736 ASSERT_TRUE(result.is_valid());
737 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56738 }
739}
740
[email protected]81f808de2009-09-25 01:36:34741TEST_F(TemplateURLTest, RLZ) {
[email protected]798baa8e2014-06-20 05:42:44742 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(false);
[email protected]d82443b2009-01-15 19:54:56743
[email protected]573889f22012-04-07 01:31:54744 TemplateURLData data;
745 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28746 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19747 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
748 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04749 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19750 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56751 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44752 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
753 result.spec());
[email protected]d82443b2009-01-15 19:54:56754}
755
[email protected]c8ccc41d2014-04-10 04:42:12756TEST_F(TemplateURLTest, RLZFromAppList) {
[email protected]798baa8e2014-06-20 05:42:44757 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(true);
[email protected]c8ccc41d2014-04-10 04:42:12758
759 TemplateURLData data;
760 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28761 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19762 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
763 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12764 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
765 args.from_app_list = true;
[email protected]ce7ee5f2014-06-16 23:41:19766 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12767 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44768 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
769 result.spec());
[email protected]c8ccc41d2014-04-10 04:42:12770}
[email protected]c8ccc41d2014-04-10 04:42:12771
[email protected]d82443b2009-01-15 19:54:56772TEST_F(TemplateURLTest, HostAndSearchTermKey) {
773 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19774 const std::string url;
[email protected]d82443b2009-01-15 19:54:56775 const std::string host;
776 const std::string path;
777 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54778 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50779 {"http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
780 {"http://blah/{searchTerms}", "blah", "", ""},
[email protected]d82443b2009-01-15 19:54:56781
vitbarf2a11562017-05-15 14:09:50782 // No term should result in empty values.
783 {"http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56784
vitbarf2a11562017-05-15 14:09:50785 // Multiple terms should result in empty values.
786 {"http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56787
vitbarf2a11562017-05-15 14:09:50788 // Term in the host shouldn't match.
789 {"http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56790
vitbarf2a11562017-05-15 14:09:50791 {"http://blah/?q={searchTerms}", "blah", "/", "q"},
792 {"https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56793
vitbarf2a11562017-05-15 14:09:50794 // Single term with extra chars in value should match.
795 {"http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56796 };
797
viettrungluu37a447b2014-10-16 18:23:27798 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54799 TemplateURLData data;
800 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28801 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19802 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
803 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
804 EXPECT_EQ(test_data[i].search_term_key,
805 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56806 }
807}
808
alexmos294849f2015-03-14 00:55:06809TEST_F(TemplateURLTest, SearchTermKeyLocation) {
810 struct TestData {
811 const std::string url;
812 const url::Parsed::ComponentType location;
813 const std::string path;
vitbarf2a11562017-05-15 14:09:50814 const std::string key;
815 const std::string value_prefix;
816 const std::string value_suffix;
alexmos294849f2015-03-14 00:55:06817 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50818 {"http://blah/{searchTerms}/", url::Parsed::PATH, "", "", "/", "/"},
819 {"http://blah/{searchTerms}", url::Parsed::PATH, "", "", "/", ""},
820 {"http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "", "",
821 "/begin/", "/end"},
822 {"http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY, "/", "q",
823 "", ""},
824 {"http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF, "/", "x",
825 "", ""},
826 {"http://www.example.com/?q=chromium-{searchTerms}@chromium.org/info",
827 url::Parsed::QUERY, "/", "q", "chromium-", "@chromium.org/info"},
alexmos294849f2015-03-14 00:55:06828
vitbarf2a11562017-05-15 14:09:50829 // searchTerms is a key, not a value, so this should result in an empty
830 // value.
831 {"http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x",
832 url::Parsed::QUERY, "", "", "", ""},
alexmos294849f2015-03-14 00:55:06833
vitbarf2a11562017-05-15 14:09:50834 // Multiple search terms should result in empty values.
835 {"http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY, "", "",
836 "", ""},
837 {"http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY, "", "",
838 "", ""},
839 {"http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY, "",
840 "", "", ""},
alexmos294849f2015-03-14 00:55:06841 };
842
843 for (size_t i = 0; i < arraysize(test_data); ++i) {
844 TemplateURLData data;
845 data.SetURL(test_data[i].url);
846 TemplateURL url(data);
847 EXPECT_EQ(test_data[i].location,
848 url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
849 EXPECT_EQ(test_data[i].path,
850 url.url_ref().GetPath(search_terms_data_));
vitbarf2a11562017-05-15 14:09:50851 EXPECT_EQ(test_data[i].key,
852 url.url_ref().GetSearchTermKey(search_terms_data_));
853 EXPECT_EQ(test_data[i].value_prefix,
854 url.url_ref().GetSearchTermValuePrefix(search_terms_data_));
855 EXPECT_EQ(test_data[i].value_suffix,
856 url.url_ref().GetSearchTermValueSuffix(search_terms_data_));
alexmos294849f2015-03-14 00:55:06857 }
858}
859
[email protected]d82443b2009-01-15 19:54:56860TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
861 static const struct {
[email protected]ddd231e2010-06-29 20:35:19862 const char* const base_url;
863 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56864 } data[] = {
[email protected]014010e2011-10-01 04:12:44865 { "http://google.com/", "http://google.com/complete/", },
866 { "http://www.google.com/", "http://www.google.com/complete/", },
867 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
868 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
869 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56870 };
871
viettrungluu37a447b2014-10-16 18:23:27872 for (size_t i = 0; i < arraysize(data); ++i)
[email protected]d82443b2009-01-15 19:54:56873 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
874}
875
[email protected]81c6ef62010-01-21 09:58:47876TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19877 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:54878 TemplateURLData data;
879 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28880 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47881 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:13882 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19883 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47884 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27885 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47886 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
887}
888
889TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:20890 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:54891 TemplateURLData data;
892 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28893 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47894 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:44895
896 // By default, TemplateURLRef should not consider itself prepopulated.
897 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:13898 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20899 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:44900 EXPECT_TRUE(replacements.empty());
901
902 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:20903 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:54904 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:28905 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:15906 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20907 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47908 EXPECT_TRUE(replacements.empty());
909}
910
911TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:28912 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:47913 TemplateURLRef::Replacements replacements;
914 bool valid = false;
[email protected]b37bdfe2012-03-16 20:53:27915 EXPECT_EQ(std::string(),
[email protected]93b29062013-07-12 03:09:09916 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47917 EXPECT_TRUE(replacements.empty());
918 EXPECT_TRUE(valid);
919}
920
921TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:54922 TemplateURLData data;
923 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:28924 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47925 TemplateURLRef::Replacements replacements;
926 bool valid = false;
[email protected]93b29062013-07-12 03:09:09927 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
928 &valid));
[email protected]81c6ef62010-01-21 09:58:47929 EXPECT_TRUE(replacements.empty());
930 EXPECT_FALSE(valid);
931}
932
933TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:54934 TemplateURLData data;
935 data.SetURL("{}");
[email protected]168d08722014-06-18 07:13:28936 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47937 TemplateURLRef::Replacements replacements;
938 bool valid = false;
[email protected]93b29062013-07-12 03:09:09939 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47940 EXPECT_TRUE(replacements.empty());
941 EXPECT_TRUE(valid);
942}
943
944TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:54945 TemplateURLData data;
946 data.SetURL("{}{{%s}}");
[email protected]168d08722014-06-18 07:13:28947 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47948 TemplateURLRef::Replacements replacements;
949 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19950 EXPECT_EQ("{}{}",
[email protected]93b29062013-07-12 03:09:09951 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
952 &valid));
[email protected]81c6ef62010-01-21 09:58:47953 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27954 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47955 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
956 EXPECT_TRUE(valid);
957}
958
959TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:54960 TemplateURLData data;
961 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:28962 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47963 TemplateURLRef::Replacements replacements;
964 bool valid = false;
[email protected]360ba052012-04-04 17:26:13965 EXPECT_EQ("{",
[email protected]93b29062013-07-12 03:09:09966 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
967 &valid));
[email protected]81c6ef62010-01-21 09:58:47968 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27969 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47970 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
971 EXPECT_TRUE(valid);
972}
[email protected]5b3078752012-10-09 18:54:16973
[email protected]fd6d8822012-12-08 06:56:11974TEST_F(TemplateURLTest, SearchClient) {
975 const std::string base_url_str("http://google.com/?");
976 const std::string terms_str("{searchTerms}&{google:searchClient}");
977 const std::string full_url_str = base_url_str + terms_str;
[email protected]0085863a2013-12-06 21:19:03978 const base::string16 terms(ASCIIToUTF16(terms_str));
[email protected]798baa8e2014-06-20 05:42:44979 search_terms_data_.set_google_base_url(base_url_str);
[email protected]fd6d8822012-12-08 06:56:11980
981 TemplateURLData data;
982 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:28983 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19984 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
985 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11986 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
987
988 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:19989 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
990 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11991 ASSERT_TRUE(result.is_valid());
992 EXPECT_EQ("http://google.com/?foobar&", result.spec());
993
994 // Check that the URL is correct when a client is present.
[email protected]798baa8e2014-06-20 05:42:44995 search_terms_data_.set_search_client("search_client");
[email protected]ce7ee5f2014-06-16 23:41:19996 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
997 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11998 ASSERT_TRUE(result_2.is_valid());
[email protected]798baa8e2014-06-20 05:42:44999 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
[email protected]fd6d8822012-12-08 06:56:111000}
[email protected]fd6d8822012-12-08 06:56:111001
[email protected]5b3078752012-10-09 18:54:161002TEST_F(TemplateURLTest, GetURLNoInstantURL) {
1003 TemplateURLData data;
1004 data.SetURL("http://google.com/?q={searchTerms}");
1005 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
1006 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
1007 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281008 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511009 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1010 ASSERT_EQ(3U, url_refs.size());
1011 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1012 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1013 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161014}
1015
1016TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
1017 TemplateURLData data;
1018 data.SetURL("http://google.com/?q={searchTerms}");
1019 data.instant_url = "http://google.com/instant#q={searchTerms}";
1020 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
1021 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281022 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511023 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1024 ASSERT_EQ(3U, url_refs.size());
1025 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1026 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1027 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161028}
1029
1030TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
1031 TemplateURLData data;
1032 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:281033 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511034 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1035 ASSERT_EQ(1U, url_refs.size());
1036 EXPECT_EQ("http://www.google.co.uk/", url_refs[0].GetURL());
[email protected]5b3078752012-10-09 18:54:161037}
1038
1039TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1040 TemplateURLData data;
1041 data.SetURL("http://google.com/?q={searchTerms}");
1042 data.instant_url = "http://google.com/instant#q={searchTerms}";
1043 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1044 data.alternate_urls.push_back(
1045 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281046 TemplateURL url(data);
[email protected]0085863a2013-12-06 21:19:031047 base::string16 result;
[email protected]5b3078752012-10-09 18:54:161048
1049 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191050 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191051 EXPECT_EQ(ASCIIToUTF16("something"), result);
1052
1053 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191054 GURL("http://google.com/?espv&q=something"),
1055 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191056 EXPECT_EQ(ASCIIToUTF16("something"), result);
1057
1058 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191059 GURL("http://google.com/?espv=1&q=something"),
1060 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191061 EXPECT_EQ(ASCIIToUTF16("something"), result);
1062
1063 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191064 GURL("http://google.com/?espv=0&q=something"),
1065 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191066 EXPECT_EQ(ASCIIToUTF16("something"), result);
1067
1068 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191069 GURL("http://google.com/alt/#q=something"),
1070 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191071 EXPECT_EQ(ASCIIToUTF16("something"), result);
1072
1073 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191074 GURL("http://google.com/alt/#espv&q=something"),
1075 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191076 EXPECT_EQ(ASCIIToUTF16("something"), result);
1077
1078 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191079 GURL("http://google.com/alt/#espv=1&q=something"),
1080 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191081 EXPECT_EQ(ASCIIToUTF16("something"), result);
1082
1083 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191084 GURL("http://google.com/alt/#espv=0&q=something"),
1085 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161086 EXPECT_EQ(ASCIIToUTF16("something"), result);
1087
1088 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191089 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371090 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161091
1092 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191093 GURL("http://google.ca/?q=something&q=anything"),
1094 search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371095 EXPECT_EQ(base::string16(), result);
[email protected]67d8b752013-04-03 17:33:271096
1097 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191098 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371099 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161100
[email protected]32e2d81b2012-10-16 19:03:251101 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191102 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
[email protected]32e2d81b2012-10-16 19:03:251103 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:161104
1105 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191106 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371107 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161108
1109 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191110 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161111 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1112
1113 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191114 GURL("http://google.com/alt/?q=123#q=456"),
1115 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161116 EXPECT_EQ(ASCIIToUTF16("456"), result);
1117
1118 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191119 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1120 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161121 EXPECT_EQ(ASCIIToUTF16("123"), result);
1122
1123 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191124 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1125 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161126 EXPECT_EQ(ASCIIToUTF16("789"), result);
1127
1128 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191129 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371130 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161131
1132 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191133 GURL("http://google.com/alt/?#q="), 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
1136 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191137 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371138 EXPECT_EQ(base::string16(), 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/alt/?q=123#q="), 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/alt/?q=#q=123"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161146 EXPECT_EQ(ASCIIToUTF16("123"), result);
1147}
[email protected]4076ea62013-01-09 01:47:191148
alexmos294849f2015-03-14 00:55:061149TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1150 TemplateURLData data;
1151 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1152 TemplateURL url(data);
1153 base::string16 result;
1154
1155 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1156 GURL("http://term-in-path.com/begin/something/end"),
1157 search_terms_data_, &result));
1158 EXPECT_EQ(ASCIIToUTF16("something"), result);
1159
1160 // "%20" must be converted to space.
1161 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1162 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1163 search_terms_data_, &result));
1164 EXPECT_EQ(ASCIIToUTF16("a b c"), result);
1165
1166 // Plus must not be converted to space.
1167 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1168 GURL("http://term-in-path.com/begin/1+2+3/end"),
1169 search_terms_data_, &result));
1170 EXPECT_EQ(ASCIIToUTF16("1+2+3"), result);
1171
1172 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1173 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
1174 EXPECT_EQ(base::string16(), result);
1175
1176 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1177 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
1178 EXPECT_EQ(base::string16(), result);
1179
1180 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1181 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
1182 EXPECT_EQ(base::string16(), result);
1183}
1184
vitbar27804d102015-04-15 10:54:411185// Checks that the ExtractSearchTermsFromURL function works correctly
1186// for urls containing non-latin characters in UTF8 encoding.
1187TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
1188 TemplateURLData data;
1189 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1190 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1191 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1192 TemplateURL url(data);
1193 base::string16 result;
1194
1195 // Russian text encoded with UTF-8.
1196 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551197 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1198 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
vitbar27804d102015-04-15 10:54:411199 search_terms_data_, &result));
1200 EXPECT_EQ(
1201 base::WideToUTF16(
1202 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1203 L"\x043C\x0438\x0440!"),
1204 result);
1205
1206 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551207 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1208 "%D0%B0"),
vitbar27804d102015-04-15 10:54:411209 search_terms_data_, &result));
1210 EXPECT_EQ(
1211 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1212 result);
1213
1214 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551215 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1216 "%D0%B8%20A"),
vitbar27804d102015-04-15 10:54:411217 search_terms_data_, &result));
1218 EXPECT_EQ(
1219 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1220 result);
1221}
1222
1223// Checks that the ExtractSearchTermsFromURL function works correctly
1224// for urls containing non-latin characters in non-UTF8 encoding.
1225TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) {
1226 TemplateURLData data;
1227 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1228 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1229 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1230 data.input_encodings.push_back("windows-1251");
1231 TemplateURL url(data);
1232 base::string16 result;
1233
1234 // Russian text encoded with Windows-1251.
1235 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1236 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1237 "%EC%E8%F0!"),
1238 search_terms_data_, &result));
1239 EXPECT_EQ(
1240 base::WideToUTF16(
1241 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1242 L"\x043C\x0438\x0440!"),
1243 result);
1244
1245 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1246 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1247 search_terms_data_, &result));
1248 EXPECT_EQ(
1249 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1250 result);
1251
1252 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1253 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1254 search_terms_data_, &result));
1255 EXPECT_EQ(
1256 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1257 result);
1258}
1259
jbroman50832022016-04-21 23:53:001260// Checks that the ExtractSearchTermsFromURL function strips constant
1261// prefix/suffix strings from the search terms param.
1262TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) {
1263 TemplateURLData data;
1264 data.alternate_urls.push_back(
1265 "http://www.example.com/?q=chromium-{searchTerms}@chromium.org");
1266 data.alternate_urls.push_back(
1267 "http://www.example.com/chromium-{searchTerms}@chromium.org/info");
1268 TemplateURL url(data);
1269 base::string16 result;
1270
1271 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1272 GURL("http://www.example.com/[email protected]"),
1273 search_terms_data_, &result));
1274 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1275
1276 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1277 GURL("http://www.example.com/[email protected]/info"),
1278 search_terms_data_, &result));
1279 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1280
1281 // Don't match if the prefix and suffix aren't there.
1282 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1283 GURL("http://www.example.com/?q=invalid"), search_terms_data_, &result));
jbromane8b8728d2017-04-12 17:29:391284
1285 // Don't match if the prefix and suffix overlap.
1286 TemplateURLData data_with_overlap;
1287 data.alternate_urls.push_back(
1288 "http://www.example.com/?q=goo{searchTerms}oogle");
1289 TemplateURL url_with_overlap(data);
1290 EXPECT_FALSE(url_with_overlap.ExtractSearchTermsFromURL(
1291 GURL("http://www.example.com/?q=google"), search_terms_data_, &result));
jbroman50832022016-04-21 23:53:001292}
1293
[email protected]4076ea62013-01-09 01:47:191294TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
1295 TemplateURLData data;
1296 data.SetURL("http://google.com/?q={searchTerms}");
1297 data.instant_url = "http://google.com/instant#q={searchTerms}";
1298 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1299 data.alternate_urls.push_back(
1300 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1301 data.search_terms_replacement_key = "espv";
[email protected]168d08722014-06-18 07:13:281302 TemplateURL url(data);
[email protected]4076ea62013-01-09 01:47:191303
1304 // Test with instant enabled required.
1305 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1306 GURL("http://google.com/")));
1307
1308 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1309 GURL("http://google.com/?espv")));
1310
1311 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1312 GURL("http://google.com/#espv")));
1313
1314 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1315 GURL("http://google.com/?q=something")));
1316
1317 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1318 GURL("http://google.com/?q=something&espv")));
1319
1320 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1321 GURL("http://google.com/?q=something&espv=1")));
1322
1323 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1324 GURL("http://google.com/?q=something&espv=0")));
1325
1326 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1327 GURL("http://google.com/?espv&q=something")));
1328
1329 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1330 GURL("http://google.com/?espv=1&q=something")));
1331
1332 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1333 GURL("http://google.com/?espv=0&q=something")));
1334
1335 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1336 GURL("http://google.com/alt/#q=something")));
1337
1338 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1339 GURL("http://google.com/alt/#q=something&espv")));
1340
1341 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1342 GURL("http://google.com/alt/#q=something&espv=1")));
1343
1344 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1345 GURL("http://google.com/alt/#q=something&espv=0")));
1346
1347 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1348 GURL("http://google.com/alt/#espv&q=something")));
1349
1350 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1351 GURL("http://google.com/alt/#espv=1&q=something")));
1352
1353 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1354 GURL("http://google.com/alt/#espv=0&q=something")));
1355
1356 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1357 GURL("http://google.com/?espv#q=something")));
1358
1359 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1360 GURL("http://google.com/?espv=1#q=something")));
1361
1362 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1363 GURL("http://google.com/?q=something#espv")));
1364
1365 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1366 GURL("http://google.com/?q=something#espv=1")));
1367
1368 // This does not ensure the domain matches.
1369 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1370 GURL("http://bing.com/?espv")));
1371
1372 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1373 GURL("http://bing.com/#espv")));
1374}
[email protected]f62e30f52013-03-23 03:45:151375
1376TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1377 TemplateURLData data;
1378 data.SetURL("http://google.com/?q={searchTerms}");
1379 data.instant_url = "http://google.com/instant#q={searchTerms}";
1380 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1381 data.alternate_urls.push_back(
1382 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281383 TemplateURL url(data);
[email protected]f62e30f52013-03-23 03:45:151384 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1385 GURL result;
1386
1387 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191388 GURL("http://google.com/?q=something"), search_terms,
1389 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101390 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151391
1392 result = GURL("http://should.not.change.com");
1393 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191394 GURL("http://google.ca/?q=something"), search_terms,
1395 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151396 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1397
1398 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191399 GURL("http://google.com/foo/?q=foo"), search_terms,
1400 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151401
1402 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191403 GURL("https://google.com/?q=foo"), search_terms,
1404 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101405 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151406
1407 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191408 GURL("http://google.com:8080/?q=foo"), search_terms,
1409 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151410
1411 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191412 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1413 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101414 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result);
[email protected]f62e30f52013-03-23 03:45:151415
1416 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1417 // template_url.cc for details.
1418 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191419 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1420 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101421 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151422
1423 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1424 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191425 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101426 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
[email protected]f62e30f52013-03-23 03:45:151427 result);
1428
1429 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1430 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191431 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151432 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
vitbarf298455d2015-04-21 12:58:101433 "#j=abc&q=Bob+Morane&h=def9"), result);
[email protected]f62e30f52013-03-23 03:45:151434
1435 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191436 GURL("http://google.com/alt/?q="), search_terms,
1437 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151438
1439 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191440 GURL("http://google.com/alt/?#q="), search_terms,
1441 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151442
1443 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191444 GURL("http://google.com/alt/?q=#q="), search_terms,
1445 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151446
1447 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191448 GURL("http://google.com/alt/?q=123#q="), search_terms,
1449 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151450
1451 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191452 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1453 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101454 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151455}
[email protected]56fa29592013-07-02 20:25:531456
alexmos294849f2015-03-14 00:55:061457TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1458 TemplateURLData data;
1459 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1460 TemplateURL url(data);
1461 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1462 GURL result;
1463
1464 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1465 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1466 search_terms_data_, &result));
1467 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1468
1469 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1470 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1471 search_terms_data_, &result));
1472 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1473
1474 result = GURL("http://should.not.change.com");
1475 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1476 GURL("http://term-in-path.com/about"), search_terms,
1477 search_terms_data_, &result));
1478 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1479}
1480
vitbarf298455d2015-04-21 12:58:101481// Checks that the ReplaceSearchTermsInURL function works correctly
1482// for search terms containing non-latin characters for a search engine
1483// using UTF-8 input encoding.
1484TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) {
1485 TemplateURLData data;
1486 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1487 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1488 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1489 TemplateURL url(data);
1490
1491 // Russian text which will be encoded with UTF-8.
1492 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1493 L"\x0442\x0435\x043A\x0441\x0442"));
1494 GURL result;
1495
1496 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1497 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_,
1498 &result));
1499 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1500 result);
1501
1502 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1503 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_,
1504 &result));
1505 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1506 result);
1507
1508 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1509 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_,
1510 &result));
1511 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1512 result);
1513}
1514
1515// Checks that the ReplaceSearchTermsInURL function works correctly
1516// for search terms containing non-latin characters for a search engine
1517// using non UTF-8 input encoding.
1518TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) {
1519 TemplateURLData data;
1520 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1521 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1522 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1523 data.input_encodings.push_back("windows-1251");
1524 TemplateURL url(data);
1525
1526 // Russian text which will be encoded with Windows-1251.
1527 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1528 L"\x0442\x0435\x043A\x0441\x0442"));
1529 GURL result;
1530
1531 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1532 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_,
1533 &result));
1534 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1535 result);
1536
1537 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1538 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_,
1539 &result));
1540 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1541 result);
1542
1543 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1544 GURL("http://windows-1251.ru/path/a%20b"), search_terms,
1545 search_terms_data_, &result));
1546 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1547 result);
1548}
1549
[email protected]621ade062013-10-28 06:27:431550// Test the |suggest_query_params| field of SearchTermsArgs.
1551TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431552 TemplateURLData data;
1553 // Pick a URL with replacements before, during, and after the query, to ensure
1554 // we don't goof up any of them.
1555 data.SetURL("{google:baseURL}search?q={searchTerms}"
1556 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281557 TemplateURL url(data);
[email protected]621ade062013-10-28 06:27:431558
1559 // Baseline: no |suggest_query_params| field.
1560 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1561 search_terms.original_query = ASCIIToUTF16("def");
1562 search_terms.accepted_suggestion = 0;
1563 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191564 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431565
1566 // Set the suggest_query_params.
1567 search_terms.suggest_query_params = "pq=xyz";
1568 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191569 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431570
1571 // Add extra_query_params in the mix, and ensure it works.
1572 search_terms.append_extra_query_params = true;
avi1772c1a2014-12-22 22:42:331573 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]621ade062013-10-28 06:27:431574 switches::kExtraSearchQueryParams, "a=b");
1575 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191576 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431577}
1578
[email protected]56fa29592013-07-02 20:25:531579// Test the |append_extra_query_params| field of SearchTermsArgs.
1580TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531581 TemplateURLData data;
1582 // Pick a URL with replacements before, during, and after the query, to ensure
1583 // we don't goof up any of them.
1584 data.SetURL("{google:baseURL}search?q={searchTerms}"
1585 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281586 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531587
1588 // Baseline: no command-line args, no |append_extra_query_params| flag.
1589 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1590 search_terms.original_query = ASCIIToUTF16("def");
1591 search_terms.accepted_suggestion = 0;
1592 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191593 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531594
1595 // Set the flag. Since there are no command-line args, this should have no
1596 // effect.
1597 search_terms.append_extra_query_params = true;
1598 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191599 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531600
1601 // Now append the command-line arg. This should be inserted into the query.
avi1772c1a2014-12-22 22:42:331602 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]56fa29592013-07-02 20:25:531603 switches::kExtraSearchQueryParams, "a=b");
1604 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191605 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531606
1607 // Turn off the flag. Now the command-line arg should be ignored again.
1608 search_terms.append_extra_query_params = false;
1609 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191610 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531611}
[email protected]d5015ca2013-08-08 22:04:181612
1613// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171614TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181615 TemplateURLData data;
1616 data.input_encodings.push_back("UTF-8");
1617 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281618 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191619 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1620 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]d5015ca2013-08-08 22:04:181621 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1622
[email protected]ce7ee5f2014-06-16 23:41:191623 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1624 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181625 EXPECT_EQ("http://www.google.com/?q=foo", result);
1626
[email protected]332d17d22014-06-20 16:56:031627 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191628 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1629 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181630 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1631
1632 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031633 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191634 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1635 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181636 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1637}
[email protected]2328ee22013-08-08 23:00:191638
1639// Test the IsSearchResults function.
1640TEST_F(TemplateURLTest, IsSearchResults) {
1641 TemplateURLData data;
1642 data.SetURL("http://bar/search?q={searchTerms}");
1643 data.instant_url = "http://bar/instant#q={searchTerms}";
[email protected]2767c0fd2013-08-16 17:44:161644 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191645 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1646 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1647 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1648 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281649 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191650
1651 const struct {
1652 const char* const url;
1653 bool result;
1654 } url_data[] = {
1655 { "http://bar/search?q=foo&oq=foo", true, },
1656 { "http://bar/?q=foo&oq=foo", true, },
1657 { "http://bar/#output=search&q=foo&oq=foo", true, },
1658 { "http://bar/webhp#q=foo&oq=foo", true, },
1659 { "http://bar/#q=foo&oq=foo", true, },
1660 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1661 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1662 { "http://bar/", false, },
1663 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161664 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191665 };
1666
viettrungluu37a447b2014-10-16 18:23:271667 for (size_t i = 0; i < arraysize(url_data); ++i) {
[email protected]2328ee22013-08-08 23:00:191668 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191669 search_provider.IsSearchURL(GURL(url_data[i].url),
1670 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191671 }
1672}
[email protected]a048de8a2013-10-02 18:30:061673
jdonnelly41c5b46a2015-07-10 21:24:381674TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) {
[email protected]9d70de12014-05-10 02:15:311675 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441676 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311677 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1678
[email protected]168d08722014-06-18 07:13:281679 TemplateURL url(data);
[email protected]9d70de12014-05-10 02:15:311680 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191681 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1682 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311683 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1684}
[email protected]20184242014-05-14 02:57:421685
1686TEST_F(TemplateURLTest, SessionToken) {
1687 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441688 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421689 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1690
[email protected]168d08722014-06-18 07:13:281691 TemplateURL url(data);
[email protected]20184242014-05-14 02:57:421692 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1693 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191694 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1695 search_terms_data_);
[email protected]20184242014-05-14 02:57:421696 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1697
[email protected]168d08722014-06-18 07:13:281698 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421699 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191700 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1701 search_terms_data_);
[email protected]20184242014-05-14 02:57:421702 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1703}
[email protected]448b17f52014-06-13 01:08:031704
1705TEST_F(TemplateURLTest, ContextualSearchParameters) {
1706 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441707 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031708 data.SetURL("http://bar/_/contextualsearch?"
1709 "{google:contextualSearchVersion}"
1710 "{google:contextualSearchContextData}");
1711
[email protected]168d08722014-06-18 07:13:281712 TemplateURL url(data);
[email protected]448b17f52014-06-13 01:08:031713 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191714 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1715 search_terms_data_);
donndbb98274e2016-11-01 21:04:401716 EXPECT_EQ("http://bar/_/contextualsearch?", result);
[email protected]448b17f52014-06-13 01:08:031717
donnd0bc73292017-01-17 22:07:561718 // Test the current common case, which uses no home country.
1719 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(2, 1,
1720 std::string());
[email protected]448b17f52014-06-13 01:08:031721 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191722 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1723 search_terms_data_);
donndd9fc4d92016-09-16 00:30:491724 EXPECT_EQ(
1725 "http://bar/_/contextualsearch?"
donnd0bc73292017-01-17 22:07:561726 "ctxs=2&"
donndd9fc4d92016-09-16 00:30:491727 "ctxsl_coca=1",
1728 result);
donndbb98274e2016-11-01 21:04:401729
donnd0bc73292017-01-17 22:07:561730 // Test the home country case.
donndbb98274e2016-11-01 21:04:401731 search_terms_args.contextual_search_params =
donnd0bc73292017-01-17 22:07:561732 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(2, 2, "CH");
donndbb98274e2016-11-01 21:04:401733 result =
1734 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1735
1736 EXPECT_EQ(
1737 "http://bar/_/contextualsearch?"
1738 "ctxs=2&"
donnd0bc73292017-01-17 22:07:561739 "ctxsl_coca=2&"
1740 "ctxs_hc=CH",
donndbb98274e2016-11-01 21:04:401741 result);
[email protected]448b17f52014-06-13 01:08:031742}
[email protected]44ccc9f2014-06-20 17:36:211743
1744TEST_F(TemplateURLTest, GenerateKeyword) {
1745 ASSERT_EQ(ASCIIToUTF16("foo"),
jshin1fb76462016-04-05 22:13:031746 TemplateURL::GenerateKeyword(GURL("http://foo")));
[email protected]44ccc9f2014-06-20 17:36:211747 // www. should be stripped.
jshin1fb76462016-04-05 22:13:031748 ASSERT_EQ(ASCIIToUTF16("foo"),
1749 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
[email protected]44ccc9f2014-06-20 17:36:211750 // Make sure we don't get a trailing '/'.
jshin1fb76462016-04-05 22:13:031751 ASSERT_EQ(ASCIIToUTF16("blah"),
1752 TemplateURL::GenerateKeyword(GURL("http://blah/")));
[email protected]44ccc9f2014-06-20 17:36:211753 // Don't generate the empty string.
jshin1fb76462016-04-05 22:13:031754 ASSERT_EQ(ASCIIToUTF16("www"),
1755 TemplateURL::GenerateKeyword(GURL("http://www.")));
alshabaline6212ac2015-07-23 08:03:001756 ASSERT_EQ(
1757 base::UTF8ToUTF16("\xd0\xb0\xd0\xb1\xd0\xb2"),
jshin1fb76462016-04-05 22:13:031758 TemplateURL::GenerateKeyword(GURL("http://xn--80acd")));
a-v-y81695d0d2017-04-20 08:22:221759
1760 // Generated keywords must always be in lowercase, because TemplateURLs always
1761 // converts keywords to lowercase in its constructor and TemplateURLService
1762 // stores TemplateURLs in maps using keyword as key.
1763 EXPECT_TRUE(IsLowerCase(TemplateURL::GenerateKeyword(GURL("http://BLAH/"))));
1764 EXPECT_TRUE(IsLowerCase(
1765 TemplateURL::GenerateKeyword(GURL("http://embeddedhtml.<head>/"))));
[email protected]44ccc9f2014-06-20 17:36:211766}
1767
1768TEST_F(TemplateURLTest, GenerateSearchURL) {
1769 struct GenerateSearchURLCase {
1770 const char* test_name;
1771 const char* url;
1772 const char* expected;
1773 } generate_url_cases[] = {
1774 { "invalid URL", "foo{searchTerms}", "" },
1775 { "URL with no replacements", "http://foo/", "http://foo/" },
1776 { "basic functionality", "http://foo/{searchTerms}",
1777 "http://foo/blah.blah.blah.blah.blah" }
1778 };
1779
viettrungluu37a447b2014-10-16 18:23:271780 for (size_t i = 0; i < arraysize(generate_url_cases); ++i) {
[email protected]44ccc9f2014-06-20 17:36:211781 TemplateURLData data;
1782 data.SetURL(generate_url_cases[i].url);
1783 TemplateURL t_url(data);
1784 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1785 generate_url_cases[i].expected)
1786 << generate_url_cases[i].test_name << " failed.";
1787 }
1788}
[email protected]9e9130ce2014-06-23 23:20:511789
1790TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1791 TemplateURLData data;
1792 search_terms_data_.set_google_base_url("http://bar/");
1793 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1794
1795 TemplateURL url(data);
1796 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1797 search_terms_args.prefetch_query = "full query text";
1798 search_terms_args.prefetch_query_type = "2338";
1799 std::string result =
1800 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1801 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1802 result);
1803
1804 TemplateURL url2(data);
1805 search_terms_args.prefetch_query.clear();
1806 search_terms_args.prefetch_query_type.clear();
1807 result =
1808 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1809 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1810}
vitbar5bd8c252016-01-29 18:44:511811
1812// Tests that TemplateURL works correctly after changing the Google base URL
1813// and invalidating cached values.
1814TEST_F(TemplateURLTest, InvalidateCachedValues) {
1815 TemplateURLData data;
1816 data.SetURL("{google:baseURL}search?q={searchTerms}");
1817 data.suggestions_url = "{google:baseSuggestURL}search?q={searchTerms}";
1818 data.instant_url = "{google:baseURL}webhp";
1819 data.image_url = "{google:baseURL}searchbyimage/upload";
1820 data.new_tab_url = "{google:baseURL}_/chrome/newtab";
1821 data.contextual_search_url = "{google:baseURL}_/contextualsearch";
1822 data.alternate_urls.push_back("{google:baseURL}s#q={searchTerms}");
1823 TemplateURL url(data);
1824 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("X"));
1825 base::string16 search_terms;
1826
1827 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1828 EXPECT_EQ("http://www.google.com/search?q=X",
1829 url.url_ref().ReplaceSearchTerms(search_terms_args,
1830 search_terms_data_));
1831 EXPECT_EQ("http://www.google.com/s#q=X",
1832 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1833 search_terms_data_));
1834 EXPECT_EQ("http://www.google.com/search?q=X",
1835 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1836 search_terms_data_));
1837 EXPECT_EQ("http://www.google.com/complete/search?q=X",
1838 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1839 search_terms_data_));
1840 EXPECT_EQ("http://www.google.com/webhp",
1841 url.instant_url_ref().ReplaceSearchTerms(search_terms_args,
1842 search_terms_data_));
1843 EXPECT_EQ("http://www.google.com/searchbyimage/upload",
1844 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1845 search_terms_data_));
1846 EXPECT_EQ("http://www.google.com/_/chrome/newtab",
1847 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1848 search_terms_data_));
1849 EXPECT_EQ("http://www.google.com/_/contextualsearch",
1850 url.contextual_search_url_ref().ReplaceSearchTerms(
1851 search_terms_args, search_terms_data_));
1852
1853 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1854 GURL("http://www.google.com/search?q=Y+Z"),
1855 search_terms_data_, &search_terms));
1856 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
1857 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1858 GURL("http://www.google.com/s#q=123"),
1859 search_terms_data_, &search_terms));
1860 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
1861
1862 search_terms_data_.set_google_base_url("https://www.foo.org/");
1863 url.InvalidateCachedValues();
1864
1865 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1866 EXPECT_EQ("https://www.foo.org/search?q=X",
1867 url.url_ref().ReplaceSearchTerms(search_terms_args,
1868 search_terms_data_));
1869 EXPECT_EQ("https://www.foo.org/s#q=X",
1870 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1871 search_terms_data_));
1872 EXPECT_EQ("https://www.foo.org/search?q=X",
1873 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1874 search_terms_data_));
1875 EXPECT_EQ("https://www.foo.org/complete/search?q=X",
1876 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1877 search_terms_data_));
1878 EXPECT_EQ("https://www.foo.org/webhp",
1879 url.instant_url_ref().ReplaceSearchTerms(search_terms_args,
1880 search_terms_data_));
1881 EXPECT_EQ("https://www.foo.org/searchbyimage/upload",
1882 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1883 search_terms_data_));
1884 EXPECT_EQ("https://www.foo.org/_/chrome/newtab",
1885 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1886 search_terms_data_));
1887 EXPECT_EQ("https://www.foo.org/_/contextualsearch",
1888 url.contextual_search_url_ref().ReplaceSearchTerms(
1889 search_terms_args, search_terms_data_));
1890
1891 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1892 GURL("https://www.foo.org/search?q=Y+Z"),
1893 search_terms_data_, &search_terms));
1894 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
1895 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1896 GURL("https://www.foo.org/s#q=123"),
1897 search_terms_data_, &search_terms));
1898 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
1899
1900 search_terms_data_.set_google_base_url("http://www.google.com/");
1901}
a-v-y02d3f50b2017-02-16 12:03:551902
1903// search_terms_replacement_key param of TemplateURLData with value of
1904// "{google:instantExtendedEnabledKey}" is replaced inside TemplateUrl
1905// constructor so must be handled specially inside MatchesData.
1906// Test that TemplateURL object created with such param matches correctly its
1907// TemplateURLData.
1908TEST_F(TemplateURLTest, MatchesData) {
1909 TemplateURLData data;
1910 data.search_terms_replacement_key =
1911 google_util::kGoogleInstantExtendedEnabledKeyFull;
1912 TemplateURL url(data);
1913 EXPECT_NE(google_util::kGoogleInstantExtendedEnabledKeyFull,
1914 url.search_terms_replacement_key());
1915 EXPECT_TRUE(TemplateURL::MatchesData(&url, &data, search_terms_data_));
1916}
1917
1918// Test for correct replacement of GoogleInstantExtendedEnabledKey param.
1919TEST_F(TemplateURLTest, GoogleInstantExtendedEnabledReplacement) {
1920 TemplateURLData data;
1921 data.SetURL(std::string("https://www.google.com?") +
1922 google_util::kGoogleInstantExtendedEnabledKeyFull +
1923 "&q={searchTerms}");
1924 data.SetShortName(ASCIIToUTF16("Google"));
1925 data.SetKeyword(ASCIIToUTF16("google.com"));
1926 data.search_terms_replacement_key =
1927 google_util::kGoogleInstantExtendedEnabledKeyFull;
1928 TemplateURL turl(data);
1929 EXPECT_TRUE(TemplateURL::MatchesData(&turl, &data, search_terms_data_));
1930 // Expect that replacement of search_terms_replacement_key in TemplateURL
1931 // constructor is correct.
1932 EXPECT_EQ(google_util::kInstantExtendedAPIParam,
1933 turl.search_terms_replacement_key());
1934 // Expect that replacement of {google:instantExtendedEnabledKey} in search url
1935 // is correct.
1936 GURL search_generated = turl.GenerateSearchURL(search_terms_data_);
1937 EXPECT_TRUE(turl.HasSearchTermsReplacementKey(search_generated));
1938 net::QueryIterator it(search_generated);
1939 ASSERT_FALSE(it.IsAtEnd());
1940 EXPECT_EQ(google_util::kInstantExtendedAPIParam, it.GetKey());
1941}