blob: f269e0f22549d42c529625da7d4498a55a84a10e [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;
[email protected]332d17d22014-06-20 16:56:03640 metrics::OmniboxInputType::Type input_type;
[email protected]420472b22014-06-10 13:34:43641 const std::string url;
642 const std::string expected_result;
643 } test_data[] = {
644 { ASCIIToUTF16("foo"),
645 metrics::OmniboxInputType::UNKNOWN,
646 "{google:baseURL}?{searchTerms}&{google:inputType}",
647 "http://www.google.com/?foo&oit=1&" },
648 { ASCIIToUTF16("foo"),
649 metrics::OmniboxInputType::URL,
650 "{google:baseURL}?{searchTerms}&{google:inputType}",
651 "http://www.google.com/?foo&oit=3&" },
[email protected]420472b22014-06-10 13:34:43652 };
[email protected]420472b22014-06-10 13:34:43653 TemplateURLData data;
654 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27655 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]420472b22014-06-10 13:34:43656 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28657 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19658 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
659 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43660 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
661 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19662 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
663 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43664 ASSERT_TRUE(result.is_valid());
665 EXPECT_EQ(test_data[i].expected_result, result.spec());
666 }
667}
668
[email protected]9b9fa672013-11-07 06:04:52669// Tests replacing currentPageUrl.
670TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48671 struct TestData {
[email protected]0085863a2013-12-06 21:19:03672 const base::string16 search_term;
[email protected]9b9fa672013-11-07 06:04:52673 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48674 const std::string url;
675 const std::string expected_result;
676 } test_data[] = {
677 { ASCIIToUTF16("foo"),
678 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52679 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48680 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
681 { ASCIIToUTF16("foo"),
682 "",
[email protected]9b9fa672013-11-07 06:04:52683 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48684 "http://www.google.com/?foo&" },
685 { ASCIIToUTF16("foo"),
686 "http://g.com/+-/*&=",
[email protected]9b9fa672013-11-07 06:04:52687 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48688 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
689 };
690 TemplateURLData data;
691 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27692 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]800569d2013-05-06 07:38:48693 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28694 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19695 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
696 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]800569d2013-05-06 07:38:48697 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52698 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19699 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
700 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48701 ASSERT_TRUE(result.is_valid());
702 EXPECT_EQ(test_data[i].expected_result, result.spec());
703 }
704}
705
[email protected]d82443b2009-01-15 19:54:56706TEST_F(TemplateURLTest, Suggestions) {
707 struct TestData {
708 const int accepted_suggestion;
[email protected]0085863a2013-12-06 21:19:03709 const base::string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56710 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27711 } test_data[] = {
[email protected]0085863a2013-12-06 21:19:03712 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
[email protected]29653892013-03-02 19:25:37713 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30714 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37715 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03716 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(),
[email protected]29653892013-03-02 19:25:37717 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30718 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37719 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03720 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
[email protected]29653892013-03-02 19:25:37721 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56722 };
[email protected]573889f22012-04-07 01:31:54723 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37724 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
725 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54726 data.input_encodings.push_back("UTF-8");
[email protected]168d08722014-06-18 07:13:28727 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19728 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
729 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
viettrungluu37a447b2014-10-16 18:23:27730 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04731 TemplateURLRef::SearchTermsArgs search_terms_args(
732 ASCIIToUTF16("foobar"));
733 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
734 search_terms_args.original_query =
735 test_data[i].original_query_for_suggestion;
[email protected]ce7ee5f2014-06-16 23:41:19736 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
737 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27738 ASSERT_TRUE(result.is_valid());
739 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56740 }
741}
742
[email protected]81f808de2009-09-25 01:36:34743TEST_F(TemplateURLTest, RLZ) {
[email protected]798baa8e2014-06-20 05:42:44744 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(false);
[email protected]d82443b2009-01-15 19:54:56745
[email protected]573889f22012-04-07 01:31:54746 TemplateURLData data;
747 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28748 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19749 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
750 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04751 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19752 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56753 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44754 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
755 result.spec());
[email protected]d82443b2009-01-15 19:54:56756}
757
[email protected]c8ccc41d2014-04-10 04:42:12758TEST_F(TemplateURLTest, RLZFromAppList) {
[email protected]798baa8e2014-06-20 05:42:44759 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(true);
[email protected]c8ccc41d2014-04-10 04:42:12760
761 TemplateURLData data;
762 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28763 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19764 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
765 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12766 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
767 args.from_app_list = true;
[email protected]ce7ee5f2014-06-16 23:41:19768 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12769 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44770 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
771 result.spec());
[email protected]c8ccc41d2014-04-10 04:42:12772}
[email protected]c8ccc41d2014-04-10 04:42:12773
[email protected]d82443b2009-01-15 19:54:56774TEST_F(TemplateURLTest, HostAndSearchTermKey) {
775 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19776 const std::string url;
[email protected]d82443b2009-01-15 19:54:56777 const std::string host;
778 const std::string path;
779 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54780 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50781 {"http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
782 {"http://blah/{searchTerms}", "blah", "", ""},
[email protected]d82443b2009-01-15 19:54:56783
vitbarf2a11562017-05-15 14:09:50784 // No term should result in empty values.
785 {"http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56786
vitbarf2a11562017-05-15 14:09:50787 // Multiple terms should result in empty values.
788 {"http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56789
vitbarf2a11562017-05-15 14:09:50790 // Term in the host shouldn't match.
791 {"http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56792
vitbarf2a11562017-05-15 14:09:50793 {"http://blah/?q={searchTerms}", "blah", "/", "q"},
794 {"https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56795
vitbarf2a11562017-05-15 14:09:50796 // Single term with extra chars in value should match.
797 {"http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56798 };
799
viettrungluu37a447b2014-10-16 18:23:27800 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54801 TemplateURLData data;
802 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28803 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19804 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
805 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
806 EXPECT_EQ(test_data[i].search_term_key,
807 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56808 }
809}
810
alexmos294849f2015-03-14 00:55:06811TEST_F(TemplateURLTest, SearchTermKeyLocation) {
812 struct TestData {
813 const std::string url;
814 const url::Parsed::ComponentType location;
815 const std::string path;
vitbarf2a11562017-05-15 14:09:50816 const std::string key;
817 const std::string value_prefix;
818 const std::string value_suffix;
alexmos294849f2015-03-14 00:55:06819 } test_data[] = {
vitbarf2a11562017-05-15 14:09:50820 {"http://blah/{searchTerms}/", url::Parsed::PATH, "", "", "/", "/"},
821 {"http://blah/{searchTerms}", url::Parsed::PATH, "", "", "/", ""},
822 {"http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "", "",
823 "/begin/", "/end"},
824 {"http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY, "/", "q",
825 "", ""},
826 {"http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF, "/", "x",
827 "", ""},
828 {"http://www.example.com/?q=chromium-{searchTerms}@chromium.org/info",
829 url::Parsed::QUERY, "/", "q", "chromium-", "@chromium.org/info"},
alexmos294849f2015-03-14 00:55:06830
vitbarf2a11562017-05-15 14:09:50831 // searchTerms is a key, not a value, so this should result in an empty
832 // value.
833 {"http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x",
834 url::Parsed::QUERY, "", "", "", ""},
alexmos294849f2015-03-14 00:55:06835
vitbarf2a11562017-05-15 14:09:50836 // Multiple search terms should result in empty values.
837 {"http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY, "", "",
838 "", ""},
839 {"http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY, "", "",
840 "", ""},
841 {"http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY, "",
842 "", "", ""},
alexmos294849f2015-03-14 00:55:06843 };
844
845 for (size_t i = 0; i < arraysize(test_data); ++i) {
846 TemplateURLData data;
847 data.SetURL(test_data[i].url);
848 TemplateURL url(data);
849 EXPECT_EQ(test_data[i].location,
850 url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
851 EXPECT_EQ(test_data[i].path,
852 url.url_ref().GetPath(search_terms_data_));
vitbarf2a11562017-05-15 14:09:50853 EXPECT_EQ(test_data[i].key,
854 url.url_ref().GetSearchTermKey(search_terms_data_));
855 EXPECT_EQ(test_data[i].value_prefix,
856 url.url_ref().GetSearchTermValuePrefix(search_terms_data_));
857 EXPECT_EQ(test_data[i].value_suffix,
858 url.url_ref().GetSearchTermValueSuffix(search_terms_data_));
alexmos294849f2015-03-14 00:55:06859 }
860}
861
[email protected]d82443b2009-01-15 19:54:56862TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
863 static const struct {
[email protected]ddd231e2010-06-29 20:35:19864 const char* const base_url;
865 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56866 } data[] = {
[email protected]014010e2011-10-01 04:12:44867 { "http://google.com/", "http://google.com/complete/", },
868 { "http://www.google.com/", "http://www.google.com/complete/", },
869 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
870 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
871 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56872 };
873
viettrungluu37a447b2014-10-16 18:23:27874 for (size_t i = 0; i < arraysize(data); ++i)
[email protected]d82443b2009-01-15 19:54:56875 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
876}
877
[email protected]81c6ef62010-01-21 09:58:47878TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19879 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:54880 TemplateURLData data;
881 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28882 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47883 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:13884 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19885 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47886 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27887 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47888 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
889}
890
891TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:20892 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:54893 TemplateURLData data;
894 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28895 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47896 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:44897
898 // By default, TemplateURLRef should not consider itself prepopulated.
899 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:13900 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20901 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:44902 EXPECT_TRUE(replacements.empty());
903
904 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:20905 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:54906 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:28907 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:15908 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20909 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47910 EXPECT_TRUE(replacements.empty());
911}
912
913TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:28914 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:47915 TemplateURLRef::Replacements replacements;
916 bool valid = false;
[email protected]b37bdfe2012-03-16 20:53:27917 EXPECT_EQ(std::string(),
[email protected]93b29062013-07-12 03:09:09918 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47919 EXPECT_TRUE(replacements.empty());
920 EXPECT_TRUE(valid);
921}
922
923TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:54924 TemplateURLData data;
925 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:28926 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47927 TemplateURLRef::Replacements replacements;
928 bool valid = false;
[email protected]93b29062013-07-12 03:09:09929 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
930 &valid));
[email protected]81c6ef62010-01-21 09:58:47931 EXPECT_TRUE(replacements.empty());
932 EXPECT_FALSE(valid);
933}
934
935TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:54936 TemplateURLData data;
937 data.SetURL("{}");
[email protected]168d08722014-06-18 07:13:28938 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47939 TemplateURLRef::Replacements replacements;
940 bool valid = false;
[email protected]93b29062013-07-12 03:09:09941 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47942 EXPECT_TRUE(replacements.empty());
943 EXPECT_TRUE(valid);
944}
945
946TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:54947 TemplateURLData data;
948 data.SetURL("{}{{%s}}");
[email protected]168d08722014-06-18 07:13:28949 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47950 TemplateURLRef::Replacements replacements;
951 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19952 EXPECT_EQ("{}{}",
[email protected]93b29062013-07-12 03:09:09953 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
954 &valid));
[email protected]81c6ef62010-01-21 09:58:47955 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27956 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47957 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
958 EXPECT_TRUE(valid);
959}
960
961TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:54962 TemplateURLData data;
963 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:28964 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47965 TemplateURLRef::Replacements replacements;
966 bool valid = false;
[email protected]360ba052012-04-04 17:26:13967 EXPECT_EQ("{",
[email protected]93b29062013-07-12 03:09:09968 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
969 &valid));
[email protected]81c6ef62010-01-21 09:58:47970 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27971 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47972 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
973 EXPECT_TRUE(valid);
974}
[email protected]5b3078752012-10-09 18:54:16975
[email protected]fd6d8822012-12-08 06:56:11976TEST_F(TemplateURLTest, SearchClient) {
977 const std::string base_url_str("http://google.com/?");
978 const std::string terms_str("{searchTerms}&{google:searchClient}");
979 const std::string full_url_str = base_url_str + terms_str;
[email protected]0085863a2013-12-06 21:19:03980 const base::string16 terms(ASCIIToUTF16(terms_str));
[email protected]798baa8e2014-06-20 05:42:44981 search_terms_data_.set_google_base_url(base_url_str);
[email protected]fd6d8822012-12-08 06:56:11982
983 TemplateURLData data;
984 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:28985 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19986 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
987 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11988 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
989
990 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:19991 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
992 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11993 ASSERT_TRUE(result.is_valid());
994 EXPECT_EQ("http://google.com/?foobar&", result.spec());
995
996 // Check that the URL is correct when a client is present.
[email protected]798baa8e2014-06-20 05:42:44997 search_terms_data_.set_search_client("search_client");
[email protected]ce7ee5f2014-06-16 23:41:19998 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
999 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:111000 ASSERT_TRUE(result_2.is_valid());
[email protected]798baa8e2014-06-20 05:42:441001 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
[email protected]fd6d8822012-12-08 06:56:111002}
[email protected]fd6d8822012-12-08 06:56:111003
[email protected]5b3078752012-10-09 18:54:161004TEST_F(TemplateURLTest, GetURLNoInstantURL) {
1005 TemplateURLData data;
1006 data.SetURL("http://google.com/?q={searchTerms}");
1007 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
1008 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
1009 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281010 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511011 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1012 ASSERT_EQ(3U, url_refs.size());
1013 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1014 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1015 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161016}
1017
1018TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
1019 TemplateURLData data;
1020 data.SetURL("http://google.com/?q={searchTerms}");
1021 data.instant_url = "http://google.com/instant#q={searchTerms}";
1022 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
1023 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281024 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511025 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1026 ASSERT_EQ(3U, url_refs.size());
1027 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1028 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1029 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161030}
1031
1032TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
1033 TemplateURLData data;
1034 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:281035 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511036 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1037 ASSERT_EQ(1U, url_refs.size());
1038 EXPECT_EQ("http://www.google.co.uk/", url_refs[0].GetURL());
[email protected]5b3078752012-10-09 18:54:161039}
1040
1041TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1042 TemplateURLData data;
1043 data.SetURL("http://google.com/?q={searchTerms}");
1044 data.instant_url = "http://google.com/instant#q={searchTerms}";
1045 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1046 data.alternate_urls.push_back(
1047 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281048 TemplateURL url(data);
[email protected]0085863a2013-12-06 21:19:031049 base::string16 result;
[email protected]5b3078752012-10-09 18:54:161050
1051 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191052 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191053 EXPECT_EQ(ASCIIToUTF16("something"), result);
1054
1055 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191056 GURL("http://google.com/?espv&q=something"),
1057 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191058 EXPECT_EQ(ASCIIToUTF16("something"), result);
1059
1060 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191061 GURL("http://google.com/?espv=1&q=something"),
1062 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191063 EXPECT_EQ(ASCIIToUTF16("something"), result);
1064
1065 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191066 GURL("http://google.com/?espv=0&q=something"),
1067 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191068 EXPECT_EQ(ASCIIToUTF16("something"), result);
1069
1070 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191071 GURL("http://google.com/alt/#q=something"),
1072 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191073 EXPECT_EQ(ASCIIToUTF16("something"), result);
1074
1075 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191076 GURL("http://google.com/alt/#espv&q=something"),
1077 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191078 EXPECT_EQ(ASCIIToUTF16("something"), result);
1079
1080 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191081 GURL("http://google.com/alt/#espv=1&q=something"),
1082 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191083 EXPECT_EQ(ASCIIToUTF16("something"), result);
1084
1085 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191086 GURL("http://google.com/alt/#espv=0&q=something"),
1087 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161088 EXPECT_EQ(ASCIIToUTF16("something"), result);
1089
1090 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191091 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371092 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161093
1094 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191095 GURL("http://google.ca/?q=something&q=anything"),
1096 search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371097 EXPECT_EQ(base::string16(), result);
[email protected]67d8b752013-04-03 17:33:271098
1099 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191100 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371101 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161102
[email protected]32e2d81b2012-10-16 19:03:251103 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191104 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
[email protected]32e2d81b2012-10-16 19:03:251105 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:161106
1107 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191108 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371109 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161110
1111 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191112 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161113 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1114
1115 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191116 GURL("http://google.com/alt/?q=123#q=456"),
1117 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161118 EXPECT_EQ(ASCIIToUTF16("456"), result);
1119
1120 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191121 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1122 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161123 EXPECT_EQ(ASCIIToUTF16("123"), result);
1124
1125 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191126 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1127 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161128 EXPECT_EQ(ASCIIToUTF16("789"), result);
1129
1130 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191131 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371132 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161133
1134 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191135 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371136 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161137
1138 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191139 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371140 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161141
1142 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191143 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371144 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161145
1146 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191147 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161148 EXPECT_EQ(ASCIIToUTF16("123"), result);
1149}
[email protected]4076ea62013-01-09 01:47:191150
alexmos294849f2015-03-14 00:55:061151TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1152 TemplateURLData data;
1153 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1154 TemplateURL url(data);
1155 base::string16 result;
1156
1157 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1158 GURL("http://term-in-path.com/begin/something/end"),
1159 search_terms_data_, &result));
1160 EXPECT_EQ(ASCIIToUTF16("something"), result);
1161
1162 // "%20" must be converted to space.
1163 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1164 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1165 search_terms_data_, &result));
1166 EXPECT_EQ(ASCIIToUTF16("a b c"), result);
1167
1168 // Plus must not be converted to space.
1169 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1170 GURL("http://term-in-path.com/begin/1+2+3/end"),
1171 search_terms_data_, &result));
1172 EXPECT_EQ(ASCIIToUTF16("1+2+3"), result);
1173
1174 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1175 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
1176 EXPECT_EQ(base::string16(), result);
1177
1178 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1179 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
1180 EXPECT_EQ(base::string16(), result);
1181
1182 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1183 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
1184 EXPECT_EQ(base::string16(), result);
1185}
1186
vitbar27804d102015-04-15 10:54:411187// Checks that the ExtractSearchTermsFromURL function works correctly
1188// for urls containing non-latin characters in UTF8 encoding.
1189TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
1190 TemplateURLData data;
1191 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1192 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1193 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1194 TemplateURL url(data);
1195 base::string16 result;
1196
1197 // Russian text encoded with UTF-8.
1198 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551199 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1200 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
vitbar27804d102015-04-15 10:54:411201 search_terms_data_, &result));
1202 EXPECT_EQ(
1203 base::WideToUTF16(
1204 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1205 L"\x043C\x0438\x0440!"),
1206 result);
1207
1208 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551209 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1210 "%D0%B0"),
vitbar27804d102015-04-15 10:54:411211 search_terms_data_, &result));
1212 EXPECT_EQ(
1213 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1214 result);
1215
1216 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551217 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1218 "%D0%B8%20A"),
vitbar27804d102015-04-15 10:54:411219 search_terms_data_, &result));
1220 EXPECT_EQ(
1221 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1222 result);
1223}
1224
1225// Checks that the ExtractSearchTermsFromURL function works correctly
1226// for urls containing non-latin characters in non-UTF8 encoding.
1227TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) {
1228 TemplateURLData data;
1229 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1230 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1231 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1232 data.input_encodings.push_back("windows-1251");
1233 TemplateURL url(data);
1234 base::string16 result;
1235
1236 // Russian text encoded with Windows-1251.
1237 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1238 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1239 "%EC%E8%F0!"),
1240 search_terms_data_, &result));
1241 EXPECT_EQ(
1242 base::WideToUTF16(
1243 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1244 L"\x043C\x0438\x0440!"),
1245 result);
1246
1247 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1248 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1249 search_terms_data_, &result));
1250 EXPECT_EQ(
1251 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1252 result);
1253
1254 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1255 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1256 search_terms_data_, &result));
1257 EXPECT_EQ(
1258 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1259 result);
1260}
1261
jbroman50832022016-04-21 23:53:001262// Checks that the ExtractSearchTermsFromURL function strips constant
1263// prefix/suffix strings from the search terms param.
1264TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) {
1265 TemplateURLData data;
1266 data.alternate_urls.push_back(
1267 "http://www.example.com/?q=chromium-{searchTerms}@chromium.org");
1268 data.alternate_urls.push_back(
1269 "http://www.example.com/chromium-{searchTerms}@chromium.org/info");
1270 TemplateURL url(data);
1271 base::string16 result;
1272
1273 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1274 GURL("http://www.example.com/[email protected]"),
1275 search_terms_data_, &result));
1276 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1277
1278 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1279 GURL("http://www.example.com/[email protected]/info"),
1280 search_terms_data_, &result));
1281 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1282
1283 // Don't match if the prefix and suffix aren't there.
1284 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1285 GURL("http://www.example.com/?q=invalid"), search_terms_data_, &result));
jbromane8b8728d2017-04-12 17:29:391286
1287 // Don't match if the prefix and suffix overlap.
1288 TemplateURLData data_with_overlap;
1289 data.alternate_urls.push_back(
1290 "http://www.example.com/?q=goo{searchTerms}oogle");
1291 TemplateURL url_with_overlap(data);
1292 EXPECT_FALSE(url_with_overlap.ExtractSearchTermsFromURL(
1293 GURL("http://www.example.com/?q=google"), search_terms_data_, &result));
jbroman50832022016-04-21 23:53:001294}
1295
[email protected]4076ea62013-01-09 01:47:191296TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
1297 TemplateURLData data;
1298 data.SetURL("http://google.com/?q={searchTerms}");
1299 data.instant_url = "http://google.com/instant#q={searchTerms}";
1300 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1301 data.alternate_urls.push_back(
1302 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1303 data.search_terms_replacement_key = "espv";
[email protected]168d08722014-06-18 07:13:281304 TemplateURL url(data);
[email protected]4076ea62013-01-09 01:47:191305
1306 // Test with instant enabled required.
1307 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1308 GURL("http://google.com/")));
1309
1310 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1311 GURL("http://google.com/?espv")));
1312
1313 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1314 GURL("http://google.com/#espv")));
1315
1316 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1317 GURL("http://google.com/?q=something")));
1318
1319 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1320 GURL("http://google.com/?q=something&espv")));
1321
1322 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1323 GURL("http://google.com/?q=something&espv=1")));
1324
1325 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1326 GURL("http://google.com/?q=something&espv=0")));
1327
1328 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1329 GURL("http://google.com/?espv&q=something")));
1330
1331 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1332 GURL("http://google.com/?espv=1&q=something")));
1333
1334 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1335 GURL("http://google.com/?espv=0&q=something")));
1336
1337 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1338 GURL("http://google.com/alt/#q=something")));
1339
1340 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1341 GURL("http://google.com/alt/#q=something&espv")));
1342
1343 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1344 GURL("http://google.com/alt/#q=something&espv=1")));
1345
1346 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1347 GURL("http://google.com/alt/#q=something&espv=0")));
1348
1349 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1350 GURL("http://google.com/alt/#espv&q=something")));
1351
1352 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1353 GURL("http://google.com/alt/#espv=1&q=something")));
1354
1355 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1356 GURL("http://google.com/alt/#espv=0&q=something")));
1357
1358 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1359 GURL("http://google.com/?espv#q=something")));
1360
1361 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1362 GURL("http://google.com/?espv=1#q=something")));
1363
1364 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1365 GURL("http://google.com/?q=something#espv")));
1366
1367 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1368 GURL("http://google.com/?q=something#espv=1")));
1369
1370 // This does not ensure the domain matches.
1371 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1372 GURL("http://bing.com/?espv")));
1373
1374 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1375 GURL("http://bing.com/#espv")));
1376}
[email protected]f62e30f52013-03-23 03:45:151377
1378TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1379 TemplateURLData data;
1380 data.SetURL("http://google.com/?q={searchTerms}");
1381 data.instant_url = "http://google.com/instant#q={searchTerms}";
1382 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1383 data.alternate_urls.push_back(
1384 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281385 TemplateURL url(data);
[email protected]f62e30f52013-03-23 03:45:151386 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1387 GURL result;
1388
1389 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191390 GURL("http://google.com/?q=something"), search_terms,
1391 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101392 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151393
1394 result = GURL("http://should.not.change.com");
1395 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191396 GURL("http://google.ca/?q=something"), search_terms,
1397 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151398 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1399
1400 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191401 GURL("http://google.com/foo/?q=foo"), search_terms,
1402 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151403
1404 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191405 GURL("https://google.com/?q=foo"), search_terms,
1406 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101407 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151408
1409 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191410 GURL("http://google.com:8080/?q=foo"), search_terms,
1411 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151412
1413 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191414 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1415 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101416 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result);
[email protected]f62e30f52013-03-23 03:45:151417
1418 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1419 // template_url.cc for details.
1420 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191421 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1422 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101423 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151424
1425 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1426 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191427 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101428 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
[email protected]f62e30f52013-03-23 03:45:151429 result);
1430
1431 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1432 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191433 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151434 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
vitbarf298455d2015-04-21 12:58:101435 "#j=abc&q=Bob+Morane&h=def9"), result);
[email protected]f62e30f52013-03-23 03:45:151436
1437 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191438 GURL("http://google.com/alt/?q="), search_terms,
1439 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151440
1441 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191442 GURL("http://google.com/alt/?#q="), search_terms,
1443 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151444
1445 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191446 GURL("http://google.com/alt/?q=#q="), search_terms,
1447 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151448
1449 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191450 GURL("http://google.com/alt/?q=123#q="), search_terms,
1451 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151452
1453 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191454 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1455 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101456 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151457}
[email protected]56fa29592013-07-02 20:25:531458
alexmos294849f2015-03-14 00:55:061459TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1460 TemplateURLData data;
1461 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1462 TemplateURL url(data);
1463 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1464 GURL result;
1465
1466 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1467 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1468 search_terms_data_, &result));
1469 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1470
1471 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1472 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1473 search_terms_data_, &result));
1474 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1475
1476 result = GURL("http://should.not.change.com");
1477 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1478 GURL("http://term-in-path.com/about"), search_terms,
1479 search_terms_data_, &result));
1480 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1481}
1482
vitbarf298455d2015-04-21 12:58:101483// Checks that the ReplaceSearchTermsInURL function works correctly
1484// for search terms containing non-latin characters for a search engine
1485// using UTF-8 input encoding.
1486TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) {
1487 TemplateURLData data;
1488 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1489 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1490 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1491 TemplateURL url(data);
1492
1493 // Russian text which will be encoded with UTF-8.
1494 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1495 L"\x0442\x0435\x043A\x0441\x0442"));
1496 GURL result;
1497
1498 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1499 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_,
1500 &result));
1501 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1502 result);
1503
1504 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1505 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_,
1506 &result));
1507 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1508 result);
1509
1510 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1511 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_,
1512 &result));
1513 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1514 result);
1515}
1516
1517// Checks that the ReplaceSearchTermsInURL function works correctly
1518// for search terms containing non-latin characters for a search engine
1519// using non UTF-8 input encoding.
1520TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) {
1521 TemplateURLData data;
1522 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1523 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1524 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1525 data.input_encodings.push_back("windows-1251");
1526 TemplateURL url(data);
1527
1528 // Russian text which will be encoded with Windows-1251.
1529 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1530 L"\x0442\x0435\x043A\x0441\x0442"));
1531 GURL result;
1532
1533 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1534 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_,
1535 &result));
1536 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1537 result);
1538
1539 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1540 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_,
1541 &result));
1542 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1543 result);
1544
1545 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1546 GURL("http://windows-1251.ru/path/a%20b"), search_terms,
1547 search_terms_data_, &result));
1548 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1549 result);
1550}
1551
[email protected]621ade062013-10-28 06:27:431552// Test the |suggest_query_params| field of SearchTermsArgs.
1553TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431554 TemplateURLData data;
1555 // Pick a URL with replacements before, during, and after the query, to ensure
1556 // we don't goof up any of them.
1557 data.SetURL("{google:baseURL}search?q={searchTerms}"
1558 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281559 TemplateURL url(data);
[email protected]621ade062013-10-28 06:27:431560
1561 // Baseline: no |suggest_query_params| field.
1562 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1563 search_terms.original_query = ASCIIToUTF16("def");
1564 search_terms.accepted_suggestion = 0;
1565 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191566 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431567
1568 // Set the suggest_query_params.
1569 search_terms.suggest_query_params = "pq=xyz";
1570 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191571 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431572
1573 // Add extra_query_params in the mix, and ensure it works.
1574 search_terms.append_extra_query_params = true;
avi1772c1a2014-12-22 22:42:331575 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]621ade062013-10-28 06:27:431576 switches::kExtraSearchQueryParams, "a=b");
1577 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191578 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431579}
1580
[email protected]56fa29592013-07-02 20:25:531581// Test the |append_extra_query_params| field of SearchTermsArgs.
1582TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531583 TemplateURLData data;
1584 // Pick a URL with replacements before, during, and after the query, to ensure
1585 // we don't goof up any of them.
1586 data.SetURL("{google:baseURL}search?q={searchTerms}"
1587 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281588 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531589
1590 // Baseline: no command-line args, no |append_extra_query_params| flag.
1591 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1592 search_terms.original_query = ASCIIToUTF16("def");
1593 search_terms.accepted_suggestion = 0;
1594 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191595 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531596
1597 // Set the flag. Since there are no command-line args, this should have no
1598 // effect.
1599 search_terms.append_extra_query_params = true;
1600 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191601 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531602
1603 // Now append the command-line arg. This should be inserted into the query.
avi1772c1a2014-12-22 22:42:331604 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]56fa29592013-07-02 20:25:531605 switches::kExtraSearchQueryParams, "a=b");
1606 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191607 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531608
1609 // Turn off the flag. Now the command-line arg should be ignored again.
1610 search_terms.append_extra_query_params = false;
1611 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191612 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531613}
[email protected]d5015ca2013-08-08 22:04:181614
1615// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171616TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181617 TemplateURLData data;
1618 data.input_encodings.push_back("UTF-8");
1619 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281620 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191621 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1622 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]d5015ca2013-08-08 22:04:181623 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1624
[email protected]ce7ee5f2014-06-16 23:41:191625 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1626 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181627 EXPECT_EQ("http://www.google.com/?q=foo", result);
1628
[email protected]332d17d22014-06-20 16:56:031629 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191630 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1631 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181632 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1633
1634 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031635 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191636 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1637 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181638 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1639}
[email protected]2328ee22013-08-08 23:00:191640
1641// Test the IsSearchResults function.
1642TEST_F(TemplateURLTest, IsSearchResults) {
1643 TemplateURLData data;
1644 data.SetURL("http://bar/search?q={searchTerms}");
1645 data.instant_url = "http://bar/instant#q={searchTerms}";
[email protected]2767c0fd2013-08-16 17:44:161646 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191647 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1648 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1649 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1650 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281651 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191652
1653 const struct {
1654 const char* const url;
1655 bool result;
1656 } url_data[] = {
1657 { "http://bar/search?q=foo&oq=foo", true, },
1658 { "http://bar/?q=foo&oq=foo", true, },
1659 { "http://bar/#output=search&q=foo&oq=foo", true, },
1660 { "http://bar/webhp#q=foo&oq=foo", true, },
1661 { "http://bar/#q=foo&oq=foo", true, },
1662 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1663 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1664 { "http://bar/", false, },
1665 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161666 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191667 };
1668
viettrungluu37a447b2014-10-16 18:23:271669 for (size_t i = 0; i < arraysize(url_data); ++i) {
[email protected]2328ee22013-08-08 23:00:191670 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191671 search_provider.IsSearchURL(GURL(url_data[i].url),
1672 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191673 }
1674}
[email protected]a048de8a2013-10-02 18:30:061675
jdonnelly41c5b46a2015-07-10 21:24:381676TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) {
[email protected]9d70de12014-05-10 02:15:311677 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441678 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311679 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1680
[email protected]168d08722014-06-18 07:13:281681 TemplateURL url(data);
[email protected]9d70de12014-05-10 02:15:311682 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191683 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1684 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311685 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1686}
[email protected]20184242014-05-14 02:57:421687
1688TEST_F(TemplateURLTest, SessionToken) {
1689 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441690 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421691 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1692
[email protected]168d08722014-06-18 07:13:281693 TemplateURL url(data);
[email protected]20184242014-05-14 02:57:421694 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1695 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191696 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1697 search_terms_data_);
[email protected]20184242014-05-14 02:57:421698 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1699
[email protected]168d08722014-06-18 07:13:281700 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421701 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191702 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1703 search_terms_data_);
[email protected]20184242014-05-14 02:57:421704 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1705}
[email protected]448b17f52014-06-13 01:08:031706
1707TEST_F(TemplateURLTest, ContextualSearchParameters) {
1708 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441709 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031710 data.SetURL("http://bar/_/contextualsearch?"
1711 "{google:contextualSearchVersion}"
1712 "{google:contextualSearchContextData}");
1713
[email protected]168d08722014-06-18 07:13:281714 TemplateURL url(data);
[email protected]448b17f52014-06-13 01:08:031715 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191716 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1717 search_terms_data_);
donndbb98274e2016-11-01 21:04:401718 EXPECT_EQ("http://bar/_/contextualsearch?", result);
[email protected]448b17f52014-06-13 01:08:031719
donnd0bc73292017-01-17 22:07:561720 // Test the current common case, which uses no home country.
1721 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(2, 1,
1722 std::string());
[email protected]448b17f52014-06-13 01:08:031723 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191724 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1725 search_terms_data_);
donndd9fc4d92016-09-16 00:30:491726 EXPECT_EQ(
1727 "http://bar/_/contextualsearch?"
donnd0bc73292017-01-17 22:07:561728 "ctxs=2&"
donndd9fc4d92016-09-16 00:30:491729 "ctxsl_coca=1",
1730 result);
donndbb98274e2016-11-01 21:04:401731
donnd0bc73292017-01-17 22:07:561732 // Test the home country case.
donndbb98274e2016-11-01 21:04:401733 search_terms_args.contextual_search_params =
donnd0bc73292017-01-17 22:07:561734 TemplateURLRef::SearchTermsArgs::ContextualSearchParams(2, 2, "CH");
donndbb98274e2016-11-01 21:04:401735 result =
1736 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1737
1738 EXPECT_EQ(
1739 "http://bar/_/contextualsearch?"
1740 "ctxs=2&"
donnd0bc73292017-01-17 22:07:561741 "ctxsl_coca=2&"
1742 "ctxs_hc=CH",
donndbb98274e2016-11-01 21:04:401743 result);
[email protected]448b17f52014-06-13 01:08:031744}
[email protected]44ccc9f2014-06-20 17:36:211745
1746TEST_F(TemplateURLTest, GenerateKeyword) {
1747 ASSERT_EQ(ASCIIToUTF16("foo"),
jshin1fb76462016-04-05 22:13:031748 TemplateURL::GenerateKeyword(GURL("http://foo")));
[email protected]44ccc9f2014-06-20 17:36:211749 // www. should be stripped.
jshin1fb76462016-04-05 22:13:031750 ASSERT_EQ(ASCIIToUTF16("foo"),
1751 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
[email protected]44ccc9f2014-06-20 17:36:211752 // Make sure we don't get a trailing '/'.
jshin1fb76462016-04-05 22:13:031753 ASSERT_EQ(ASCIIToUTF16("blah"),
1754 TemplateURL::GenerateKeyword(GURL("http://blah/")));
[email protected]44ccc9f2014-06-20 17:36:211755 // Don't generate the empty string.
jshin1fb76462016-04-05 22:13:031756 ASSERT_EQ(ASCIIToUTF16("www"),
1757 TemplateURL::GenerateKeyword(GURL("http://www.")));
alshabaline6212ac2015-07-23 08:03:001758 ASSERT_EQ(
1759 base::UTF8ToUTF16("\xd0\xb0\xd0\xb1\xd0\xb2"),
jshin1fb76462016-04-05 22:13:031760 TemplateURL::GenerateKeyword(GURL("http://xn--80acd")));
a-v-y81695d0d2017-04-20 08:22:221761
1762 // Generated keywords must always be in lowercase, because TemplateURLs always
1763 // converts keywords to lowercase in its constructor and TemplateURLService
1764 // stores TemplateURLs in maps using keyword as key.
1765 EXPECT_TRUE(IsLowerCase(TemplateURL::GenerateKeyword(GURL("http://BLAH/"))));
1766 EXPECT_TRUE(IsLowerCase(
1767 TemplateURL::GenerateKeyword(GURL("http://embeddedhtml.<head>/"))));
[email protected]44ccc9f2014-06-20 17:36:211768}
1769
1770TEST_F(TemplateURLTest, GenerateSearchURL) {
1771 struct GenerateSearchURLCase {
1772 const char* test_name;
1773 const char* url;
1774 const char* expected;
1775 } generate_url_cases[] = {
1776 { "invalid URL", "foo{searchTerms}", "" },
1777 { "URL with no replacements", "http://foo/", "http://foo/" },
1778 { "basic functionality", "http://foo/{searchTerms}",
1779 "http://foo/blah.blah.blah.blah.blah" }
1780 };
1781
viettrungluu37a447b2014-10-16 18:23:271782 for (size_t i = 0; i < arraysize(generate_url_cases); ++i) {
[email protected]44ccc9f2014-06-20 17:36:211783 TemplateURLData data;
1784 data.SetURL(generate_url_cases[i].url);
1785 TemplateURL t_url(data);
1786 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1787 generate_url_cases[i].expected)
1788 << generate_url_cases[i].test_name << " failed.";
1789 }
1790}
[email protected]9e9130ce2014-06-23 23:20:511791
1792TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1793 TemplateURLData data;
1794 search_terms_data_.set_google_base_url("http://bar/");
1795 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1796
1797 TemplateURL url(data);
1798 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1799 search_terms_args.prefetch_query = "full query text";
1800 search_terms_args.prefetch_query_type = "2338";
1801 std::string result =
1802 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1803 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1804 result);
1805
1806 TemplateURL url2(data);
1807 search_terms_args.prefetch_query.clear();
1808 search_terms_args.prefetch_query_type.clear();
1809 result =
1810 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1811 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1812}
vitbar5bd8c252016-01-29 18:44:511813
1814// Tests that TemplateURL works correctly after changing the Google base URL
1815// and invalidating cached values.
1816TEST_F(TemplateURLTest, InvalidateCachedValues) {
1817 TemplateURLData data;
1818 data.SetURL("{google:baseURL}search?q={searchTerms}");
1819 data.suggestions_url = "{google:baseSuggestURL}search?q={searchTerms}";
1820 data.instant_url = "{google:baseURL}webhp";
1821 data.image_url = "{google:baseURL}searchbyimage/upload";
1822 data.new_tab_url = "{google:baseURL}_/chrome/newtab";
1823 data.contextual_search_url = "{google:baseURL}_/contextualsearch";
1824 data.alternate_urls.push_back("{google:baseURL}s#q={searchTerms}");
1825 TemplateURL url(data);
1826 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("X"));
1827 base::string16 search_terms;
1828
1829 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1830 EXPECT_EQ("http://www.google.com/search?q=X",
1831 url.url_ref().ReplaceSearchTerms(search_terms_args,
1832 search_terms_data_));
1833 EXPECT_EQ("http://www.google.com/s#q=X",
1834 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1835 search_terms_data_));
1836 EXPECT_EQ("http://www.google.com/search?q=X",
1837 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1838 search_terms_data_));
1839 EXPECT_EQ("http://www.google.com/complete/search?q=X",
1840 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1841 search_terms_data_));
1842 EXPECT_EQ("http://www.google.com/webhp",
1843 url.instant_url_ref().ReplaceSearchTerms(search_terms_args,
1844 search_terms_data_));
1845 EXPECT_EQ("http://www.google.com/searchbyimage/upload",
1846 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1847 search_terms_data_));
1848 EXPECT_EQ("http://www.google.com/_/chrome/newtab",
1849 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1850 search_terms_data_));
1851 EXPECT_EQ("http://www.google.com/_/contextualsearch",
1852 url.contextual_search_url_ref().ReplaceSearchTerms(
1853 search_terms_args, search_terms_data_));
1854
1855 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1856 GURL("http://www.google.com/search?q=Y+Z"),
1857 search_terms_data_, &search_terms));
1858 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
1859 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1860 GURL("http://www.google.com/s#q=123"),
1861 search_terms_data_, &search_terms));
1862 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
1863
1864 search_terms_data_.set_google_base_url("https://www.foo.org/");
1865 url.InvalidateCachedValues();
1866
1867 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1868 EXPECT_EQ("https://www.foo.org/search?q=X",
1869 url.url_ref().ReplaceSearchTerms(search_terms_args,
1870 search_terms_data_));
1871 EXPECT_EQ("https://www.foo.org/s#q=X",
1872 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1873 search_terms_data_));
1874 EXPECT_EQ("https://www.foo.org/search?q=X",
1875 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1876 search_terms_data_));
1877 EXPECT_EQ("https://www.foo.org/complete/search?q=X",
1878 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1879 search_terms_data_));
1880 EXPECT_EQ("https://www.foo.org/webhp",
1881 url.instant_url_ref().ReplaceSearchTerms(search_terms_args,
1882 search_terms_data_));
1883 EXPECT_EQ("https://www.foo.org/searchbyimage/upload",
1884 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1885 search_terms_data_));
1886 EXPECT_EQ("https://www.foo.org/_/chrome/newtab",
1887 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1888 search_terms_data_));
1889 EXPECT_EQ("https://www.foo.org/_/contextualsearch",
1890 url.contextual_search_url_ref().ReplaceSearchTerms(
1891 search_terms_args, search_terms_data_));
1892
1893 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1894 GURL("https://www.foo.org/search?q=Y+Z"),
1895 search_terms_data_, &search_terms));
1896 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
1897 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1898 GURL("https://www.foo.org/s#q=123"),
1899 search_terms_data_, &search_terms));
1900 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
1901
1902 search_terms_data_.set_google_base_url("http://www.google.com/");
1903}
a-v-y02d3f50b2017-02-16 12:03:551904
1905// search_terms_replacement_key param of TemplateURLData with value of
1906// "{google:instantExtendedEnabledKey}" is replaced inside TemplateUrl
1907// constructor so must be handled specially inside MatchesData.
1908// Test that TemplateURL object created with such param matches correctly its
1909// TemplateURLData.
1910TEST_F(TemplateURLTest, MatchesData) {
1911 TemplateURLData data;
1912 data.search_terms_replacement_key =
1913 google_util::kGoogleInstantExtendedEnabledKeyFull;
1914 TemplateURL url(data);
1915 EXPECT_NE(google_util::kGoogleInstantExtendedEnabledKeyFull,
1916 url.search_terms_replacement_key());
1917 EXPECT_TRUE(TemplateURL::MatchesData(&url, &data, search_terms_data_));
1918}
1919
1920// Test for correct replacement of GoogleInstantExtendedEnabledKey param.
1921TEST_F(TemplateURLTest, GoogleInstantExtendedEnabledReplacement) {
1922 TemplateURLData data;
1923 data.SetURL(std::string("https://www.google.com?") +
1924 google_util::kGoogleInstantExtendedEnabledKeyFull +
1925 "&q={searchTerms}");
1926 data.SetShortName(ASCIIToUTF16("Google"));
1927 data.SetKeyword(ASCIIToUTF16("google.com"));
1928 data.search_terms_replacement_key =
1929 google_util::kGoogleInstantExtendedEnabledKeyFull;
1930 TemplateURL turl(data);
1931 EXPECT_TRUE(TemplateURL::MatchesData(&turl, &data, search_terms_data_));
1932 // Expect that replacement of search_terms_replacement_key in TemplateURL
1933 // constructor is correct.
1934 EXPECT_EQ(google_util::kInstantExtendedAPIParam,
1935 turl.search_terms_replacement_key());
1936 // Expect that replacement of {google:instantExtendedEnabledKey} in search url
1937 // is correct.
1938 GURL search_generated = turl.GenerateSearchURL(search_terms_data_);
1939 EXPECT_TRUE(turl.HasSearchTermsReplacementKey(search_generated));
1940 net::QueryIterator it(search_generated);
1941 ASSERT_FALSE(it.IsAtEnd());
1942 EXPECT_EQ(google_util::kInstantExtendedAPIParam, it.GetKey());
1943}