blob: 6103835a45f678ba3ba8dc336016f85217edb943 [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"
avif57136c12015-12-25 23:27:459#include "base/macros.h"
[email protected]2f3bc6512013-08-28 03:56:2710#include "base/strings/string_number_conversions.h"
[email protected]24a555b62013-06-10 22:01:1711#include "base/strings/string_util.h"
[email protected]e309f312013-06-07 21:50:0812#include "base/strings/utf_string_conversions.h"
[email protected]332d17d22014-06-20 16:56:0313#include "components/metrics/proto/omnibox_event.pb.h"
[email protected]420472b22014-06-10 13:34:4314#include "components/metrics/proto/omnibox_input_type.pb.h"
[email protected]5c3c6e482014-06-23 09:47:1815#include "components/search_engines/search_engines_switches.h"
[email protected]798baa8e2014-06-20 05:42:4416#include "components/search_engines/search_terms_data.h"
[email protected]d550cb02014-06-25 06:48:1117#include "components/search_engines/template_url.h"
hashimoto3c831812014-08-25 07:40:2318#include "components/search_engines/testing_search_terms_data.h"
[email protected]d82443b2009-01-15 19:54:5619#include "testing/gtest/include/gtest/gtest.h"
20
[email protected]f911df52013-12-24 23:24:2321using base::ASCIIToUTF16;
22
[email protected]583844c2011-08-27 00:38:3523class TemplateURLTest : public testing::Test {
[email protected]d82443b2009-01-15 19:54:5624 public:
[email protected]798baa8e2014-06-20 05:42:4425 TemplateURLTest() : search_terms_data_("http://www.google.com/") {}
[email protected]b37bdfe2012-03-16 20:53:2726 void CheckSuggestBaseURL(const std::string& base_url,
27 const std::string& base_suggest_url) const;
[email protected]ce7ee5f2014-06-16 23:41:1928
vitbardeb285392015-02-20 14:02:5529 static void ExpectPostParamIs(
30 const TemplateURLRef::PostParam& param,
31 const std::string& name,
32 const std::string& value,
33 const std::string& content_type = std::string());
34
hashimoto3c831812014-08-25 07:40:2335 TestingSearchTermsData search_terms_data_;
[email protected]d82443b2009-01-15 19:54:5636};
37
[email protected]b37bdfe2012-03-16 20:53:2738void TemplateURLTest::CheckSuggestBaseURL(
39 const std::string& base_url,
40 const std::string& base_suggest_url) const {
hashimoto3c831812014-08-25 07:40:2341 TestingSearchTermsData search_terms_data(base_url);
[email protected]b37bdfe2012-03-16 20:53:2742 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue());
43}
44
vitbardeb285392015-02-20 14:02:5545// static
46void TemplateURLTest::ExpectPostParamIs(const TemplateURLRef::PostParam& param,
47 const std::string& name,
48 const std::string& value,
49 const std::string& content_type) {
50 EXPECT_EQ(name, param.name);
51 EXPECT_EQ(value, param.value);
52 EXPECT_EQ(content_type, param.content_type);
53}
54
[email protected]d82443b2009-01-15 19:54:5655TEST_F(TemplateURLTest, Defaults) {
[email protected]573889f22012-04-07 01:31:5456 TemplateURLData data;
57 EXPECT_FALSE(data.show_in_default_list);
58 EXPECT_FALSE(data.safe_for_autoreplace);
59 EXPECT_EQ(0, data.prepopulate_id);
[email protected]d82443b2009-01-15 19:54:5660}
61
62TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]573889f22012-04-07 01:31:5463 TemplateURLData data;
64 data.SetURL("{searchTerms}");
[email protected]168d08722014-06-18 07:13:2865 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1966 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:5667}
68
69TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1970 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1971 const char* url;
[email protected]0085863a2013-12-06 21:19:0372 const base::string16 terms;
[email protected]34e24852012-01-31 18:43:5873 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:1974 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:3075 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1676 "http://foosea%20rch/bar" },
[email protected]400b133f2011-01-19 18:32:3077 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1678 "http://foosea%20rch/bar?boo=abc" },
[email protected]400b133f2011-01-19 18:32:3079 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1680 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]400b133f2011-01-19 18:32:3081 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
[email protected]c31a979c2012-05-31 23:57:1682 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:1983 };
viettrungluu37a447b2014-10-16 18:23:2784 for (size_t i = 0; i < arraysize(search_term_cases); ++i) {
[email protected]0d2e6a62010-01-15 20:09:1985 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:5486 TemplateURLData data;
87 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:2888 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1989 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
90 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:0491 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:1992 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data_));
[email protected]c31a979c2012-05-31 23:57:1693 ASSERT_TRUE(result.is_valid());
94 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:1995 }
[email protected]d82443b2009-01-15 19:54:5696}
97
98TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:5499 TemplateURLData data;
100 data.SetURL("http://foo{searchTerms}{count?}");
[email protected]168d08722014-06-18 07:13:28101 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19102 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
103 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04104 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19105 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56106 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27107 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:56108}
109
110TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:54111 TemplateURLData data;
112 data.SetURL("http://foo{searchTerms}{count}");
[email protected]168d08722014-06-18 07:13:28113 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19114 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
115 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04116 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19117 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56118 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27119 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56120}
121
122TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54123 TemplateURLData data;
124 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]168d08722014-06-18 07:13:28125 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19126 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
127 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04128 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19129 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56130 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27131 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56132}
133
134TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54135 TemplateURLData data;
136 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[email protected]168d08722014-06-18 07:13:28137 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19138 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
139 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04140 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19141 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56142 ASSERT_TRUE(result.is_valid());
[email protected]405aae22012-03-29 20:36:13143 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56144}
145
146TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54147 TemplateURLData data;
148 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[email protected]168d08722014-06-18 07:13:28149 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19150 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
151 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04152 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19153 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56154 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27155 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56156}
157
[email protected]93b29062013-07-12 03:09:09158TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
159 const char kInvalidPostParamsString[] =
160 "unknown_template={UnknownTemplate},bad_value=bad{value},"
161 "{google:sbiSource}";
162 // List all accpectable parameter format in valid_post_params_string. it is
163 // expected like: "name0=,name1=value1,name2={template1}"
164 const char kValidPostParamsString[] =
165 "image_content={google:imageThumbnail},image_url={google:imageURL},"
166 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
[email protected]2f3bc6512013-08-28 03:56:27167 "constant_param=constant,width={google:imageOriginalWidth}";
[email protected]93b29062013-07-12 03:09:09168 const char KImageSearchURL[] = "http://foo.com/sbi";
169
170 TemplateURLData data;
171 data.image_url = KImageSearchURL;
172
173 // Try to parse invalid post parameters.
174 data.image_url_post_params = kInvalidPostParamsString;
[email protected]168d08722014-06-18 07:13:28175 TemplateURL url_bad(data);
[email protected]ce7ee5f2014-06-16 23:41:19176 ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09177 const TemplateURLRef::PostParams& bad_post_params =
178 url_bad.image_url_ref().post_params_;
179 ASSERT_EQ(2U, bad_post_params.size());
vitbardeb285392015-02-20 14:02:55180 ExpectPostParamIs(bad_post_params[0], "unknown_template",
181 "{UnknownTemplate}");
182 ExpectPostParamIs(bad_post_params[1], "bad_value", "bad{value}");
[email protected]93b29062013-07-12 03:09:09183
184 // Try to parse valid post parameters.
185 data.image_url_post_params = kValidPostParamsString;
[email protected]168d08722014-06-18 07:13:28186 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19187 ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_));
188 ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09189
190 // Check term replacement.
191 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
192 search_args.image_thumbnail_content = "dummy-image-thumbnail";
193 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
[email protected]2f3bc6512013-08-28 03:56:27194 search_args.image_original_size = gfx::Size(10, 10);
[email protected]93b29062013-07-12 03:09:09195 // Replacement operation with no post_data buffer should still return
196 // the parsed URL.
hashimoto3c831812014-08-25 07:40:23197 TestingSearchTermsData search_terms_data("http://X");
[email protected]ce7ee5f2014-06-16 23:41:19198 GURL result(url.image_url_ref().ReplaceSearchTerms(
199 search_args, search_terms_data));
[email protected]93b29062013-07-12 03:09:09200 ASSERT_TRUE(result.is_valid());
201 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48202 TemplateURLRef::PostContent post_content;
[email protected]ce7ee5f2014-06-16 23:41:19203 result = GURL(url.image_url_ref().ReplaceSearchTerms(
[email protected]7c2bcc42013-08-01 04:03:48204 search_args, search_terms_data, &post_content));
[email protected]93b29062013-07-12 03:09:09205 ASSERT_TRUE(result.is_valid());
206 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48207 ASSERT_FALSE(post_content.first.empty());
208 ASSERT_FALSE(post_content.second.empty());
[email protected]93b29062013-07-12 03:09:09209
210 // Check parsed result of post parameters.
211 const TemplateURLRef::Replacements& replacements =
212 url.image_url_ref().replacements_;
213 const TemplateURLRef::PostParams& post_params =
214 url.image_url_ref().post_params_;
[email protected]2f3bc6512013-08-28 03:56:27215 EXPECT_EQ(7U, post_params.size());
[email protected]93b29062013-07-12 03:09:09216 for (TemplateURLRef::PostParams::const_iterator i = post_params.begin();
217 i != post_params.end(); ++i) {
218 TemplateURLRef::Replacements::const_iterator j = replacements.begin();
219 for (; j != replacements.end(); ++j) {
220 if (j->is_post_param && j->index ==
221 static_cast<size_t>(i - post_params.begin())) {
222 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27223 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
vitbardeb285392015-02-20 14:02:55224 ExpectPostParamIs(*i, "width",
225 base::IntToString(
226 search_args.image_original_size.width()));
[email protected]2f3bc6512013-08-28 03:56:27227 break;
[email protected]1020fead2014-06-20 13:40:28228 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE:
vitbardeb285392015-02-20 14:02:55229 ExpectPostParamIs(*i, "sbisrc",
230 search_terms_data.GoogleImageSearchSource());
[email protected]1020fead2014-06-20 13:40:28231 break;
[email protected]93b29062013-07-12 03:09:09232 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
vitbardeb285392015-02-20 14:02:55233 ExpectPostParamIs(*i, "image_content",
234 search_args.image_thumbnail_content,
235 "image/jpeg");
[email protected]93b29062013-07-12 03:09:09236 break;
237 case TemplateURLRef::GOOGLE_IMAGE_URL:
vitbardeb285392015-02-20 14:02:55238 ExpectPostParamIs(*i, "image_url", search_args.image_url.spec());
[email protected]93b29062013-07-12 03:09:09239 break;
240 case TemplateURLRef::LANGUAGE:
vitbardeb285392015-02-20 14:02:55241 ExpectPostParamIs(*i, "language", "en");
[email protected]93b29062013-07-12 03:09:09242 break;
243 default:
244 ADD_FAILURE(); // Should never go here.
245 }
246 break;
247 }
248 }
249 if (j != replacements.end())
250 continue;
vitbardeb285392015-02-20 14:02:55251 if (i->name == "empty_param")
252 ExpectPostParamIs(*i, "empty_param", std::string());
253 else
254 ExpectPostParamIs(*i, "constant_param", "constant");
[email protected]93b29062013-07-12 03:09:09255 }
256}
257
[email protected]d88cb202011-08-17 20:03:01258// Test that setting the prepopulate ID from TemplateURL causes the stored
259// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44260TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54261 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20262 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]168d08722014-06-18 07:13:28263 TemplateURL url(data);
[email protected]1a257262011-06-28 22:15:44264 TemplateURLRef::Replacements replacements;
265 bool valid = false;
[email protected]243abf32012-05-15 18:28:20266 EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL(
[email protected]93b29062013-07-12 03:09:09267 "http://foo{fhqwhgads}bar", &replacements, NULL, &valid));
[email protected]1a257262011-06-28 22:15:44268 EXPECT_TRUE(replacements.empty());
269 EXPECT_TRUE(valid);
270
[email protected]573889f22012-04-07 01:31:54271 data.prepopulate_id = 123;
[email protected]168d08722014-06-18 07:13:28272 TemplateURL url2(data);
[email protected]243abf32012-05-15 18:28:20273 EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar",
[email protected]93b29062013-07-12 03:09:09274 &replacements, NULL,
275 &valid));
[email protected]1a257262011-06-28 22:15:44276 EXPECT_TRUE(replacements.empty());
277 EXPECT_TRUE(valid);
278}
279
vitbar90902c42016-05-11 06:16:55280// Test that setting the prepopulate ID from TemplateURL causes the stored
281// TemplateURLRef to handle parsing the URL parameters differently.
282TEST_F(TemplateURLTest, SetPrepopulatedAndReplace) {
283 TemplateURLData data;
284 data.SetURL("http://foo{fhqwhgads}search/?q={searchTerms}");
285 data.suggestions_url = "http://foo{fhqwhgads}suggest/?q={searchTerms}";
286 data.instant_url = "http://foo{fhqwhgads}instant/";
287 data.image_url = "http://foo{fhqwhgads}image/";
288 data.new_tab_url = "http://foo{fhqwhgads}newtab/";
289 data.contextual_search_url = "http://foo{fhqwhgads}context/";
290 data.alternate_urls.push_back(
291 "http://foo{fhqwhgads}alternate/?q={searchTerms}");
292
293 TemplateURLRef::SearchTermsArgs args(base::ASCIIToUTF16("X"));
294 const SearchTermsData& stdata = search_terms_data_;
295
296 TemplateURL url(data);
297 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
298 url.url_ref().ReplaceSearchTerms(args, stdata));
299 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dalternate/?q=X",
300 url.url_refs()[0].ReplaceSearchTerms(args, stdata));
301 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X",
302 url.url_refs()[1].ReplaceSearchTerms(args, stdata));
303 EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsuggest/?q=X",
304 url.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
305 EXPECT_EQ("http://foo{fhqwhgads}instant/",
306 url.instant_url_ref().ReplaceSearchTerms(args, stdata));
307 EXPECT_EQ("http://foo{fhqwhgads}image/",
308 url.image_url_ref().ReplaceSearchTerms(args, stdata));
309 EXPECT_EQ("http://foo{fhqwhgads}newtab/",
310 url.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
311 EXPECT_EQ("http://foo{fhqwhgads}context/",
312 url.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
313
314 data.prepopulate_id = 123;
315 TemplateURL url2(data);
316 EXPECT_EQ("http://foosearch/?q=X",
317 url2.url_ref().ReplaceSearchTerms(args, stdata));
318 EXPECT_EQ("http://fooalternate/?q=X",
319 url2.url_refs()[0].ReplaceSearchTerms(args, stdata));
320 EXPECT_EQ("http://foosearch/?q=X",
321 url2.url_refs()[1].ReplaceSearchTerms(args, stdata));
322 EXPECT_EQ("http://foosuggest/?q=X",
323 url2.suggestions_url_ref().ReplaceSearchTerms(args, stdata));
324 EXPECT_EQ("http://fooinstant/",
325 url2.instant_url_ref().ReplaceSearchTerms(args, stdata));
326 EXPECT_EQ("http://fooimage/",
327 url2.image_url_ref().ReplaceSearchTerms(args, stdata));
328 EXPECT_EQ("http://foonewtab/",
329 url2.new_tab_url_ref().ReplaceSearchTerms(args, stdata));
330 EXPECT_EQ("http://foocontext/",
331 url2.contextual_search_url_ref().ReplaceSearchTerms(args, stdata));
332}
333
[email protected]d82443b2009-01-15 19:54:56334TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54335 TemplateURLData data;
336 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]168d08722014-06-18 07:13:28337 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19338 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
339 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04340 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19341 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56342 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27343 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56344}
345
346TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54347 TemplateURLData data;
348 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]168d08722014-06-18 07:13:28349 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19350 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
351 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04352 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19353 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56354 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27355 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56356}
357
[email protected]375bd7312010-08-30 22:18:13358TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
359 struct SearchTermsCase {
360 const char* url;
[email protected]0085863a2013-12-06 21:19:03361 const base::string16 terms;
[email protected]375bd7312010-08-30 22:18:13362 const char* output;
363 } search_term_cases[] = {
[email protected]0085863a2013-12-06 21:19:03364 { "{google:baseURL}{language}{searchTerms}", base::string16(),
[email protected]b37bdfe2012-03-16 20:53:27365 "http://example.com/e/en" },
[email protected]0085863a2013-12-06 21:19:03366 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
[email protected]014010e2011-10-01 04:12:44367 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13368 };
369
hashimoto3c831812014-08-25 07:40:23370 TestingSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54371 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27372 for (size_t i = 0; i < arraysize(search_term_cases); ++i) {
[email protected]375bd7312010-08-30 22:18:13373 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54374 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:28375 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19376 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data));
377 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data));
378 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]93b29062013-07-12 03:09:09379 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL));
[email protected]375bd7312010-08-30 22:18:13380 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27381 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13382 }
383}
384
[email protected]d82443b2009-01-15 19:54:56385TEST_F(TemplateURLTest, URLRefTermToWide) {
386 struct ToWideCase {
387 const char* encoded_search_term;
[email protected]0085863a2013-12-06 21:19:03388 const base::string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56389 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30390 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56391 // Test some big-5 input.
[email protected]f911df52013-12-24 23:24:23392 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56393 // Test some UTF-8 input. We should fall back to this when the encoding
394 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
395 // Big-5 trailing byte.
[email protected]f911df52013-12-24 23:24:23396 {"%e4%bd%a05%e5%a5%bd+to+you",
397 base::WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56398 // Undecodable input should stay escaped.
[email protected]f911df52013-12-24 23:24:23399 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44400 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30401 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44402 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30403 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56404 };
405
[email protected]d82443b2009-01-15 19:54:56406 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54407 TemplateURLData data;
408 data.SetURL("http://foo?q={searchTerms}");
409 data.input_encodings.push_back("big-5");
[email protected]168d08722014-06-18 07:13:28410 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19411 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
412 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
viettrungluu37a447b2014-10-16 18:23:27413 for (size_t i = 0; i < arraysize(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07414 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13415 url.url_ref().SearchTermToString16(
416 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56417 }
418}
419
[email protected]d82443b2009-01-15 19:54:56420TEST_F(TemplateURLTest, DisplayURLToURLRef) {
421 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19422 const std::string url;
[email protected]0085863a2013-12-06 21:19:03423 const base::string16 expected_result;
[email protected]b37bdfe2012-03-16 20:53:27424 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19425 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30426 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19427 { "http://X",
[email protected]400b133f2011-01-19 18:32:30428 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19429 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30430 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19431 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30432 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56433 };
[email protected]573889f22012-04-07 01:31:54434 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27435 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54436 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28437 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19438 EXPECT_EQ(test_data[i].expected_result,
439 url.url_ref().DisplayURL(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27440 EXPECT_EQ(test_data[i].url,
[email protected]ce7ee5f2014-06-16 23:41:19441 TemplateURLRef::DisplayURLToURLRef(
442 url.url_ref().DisplayURL(search_terms_data_)));
[email protected]d82443b2009-01-15 19:54:56443 }
444}
445
446TEST_F(TemplateURLTest, ReplaceSearchTerms) {
447 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19448 const std::string url;
[email protected]d82443b2009-01-15 19:54:56449 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27450 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19451 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56452 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19453 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56454 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19455 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56456 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19457 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56458 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19459 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56460 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19461 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56462 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19463 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56464 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19465 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56466 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19467 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56468 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19469 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56470 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19471 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56472 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19473 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56474 "http://foo/UTF-8a{language}aXa" },
475 };
[email protected]573889f22012-04-07 01:31:54476 TemplateURLData data;
477 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27478 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54479 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28480 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19481 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
482 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27483 std::string expected_result = test_data[i].expected_result;
brettwe6dae462015-06-24 20:54:45484 base::ReplaceSubstringsAfterOffset(
485 &expected_result, 0, "{language}",
486 search_terms_data_.GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04487 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19488 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
489 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27490 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56491 EXPECT_EQ(expected_result, result.spec());
492 }
493}
494
495
496// Tests replacing search terms in various encodings and making sure the
497// generated URL matches the expected value.
498TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
499 struct TestData {
500 const std::string encoding;
[email protected]0085863a2013-12-06 21:19:03501 const base::string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19502 const std::string url;
[email protected]d82443b2009-01-15 19:54:56503 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27504 } test_data[] = {
[email protected]f911df52013-12-24 23:24:23505 { "BIG5", base::WideToUTF16(L"\x60BD"),
[email protected]400b133f2011-01-19 18:32:30506 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17507 "http://foo/?%B1~BIG5" },
[email protected]400b133f2011-01-19 18:32:30508 { "UTF-8", ASCIIToUTF16("blah"),
509 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17510 "http://foo/?blahUTF-8" },
[email protected]f911df52013-12-24 23:24:23511 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
[email protected]34e24852012-01-31 18:43:58512 "http://foo/{searchTerms}/bar",
513 "http://foo/%82%A0/bar"},
[email protected]f911df52013-12-24 23:24:23514 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
[email protected]34e24852012-01-31 18:43:58515 "http://foo/{searchTerms}/bar",
516 "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56517 };
[email protected]573889f22012-04-07 01:31:54518 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27519 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54520 data.SetURL(test_data[i].url);
521 data.input_encodings.clear();
522 data.input_encodings.push_back(test_data[i].encoding);
[email protected]168d08722014-06-18 07:13:28523 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19524 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
525 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04526 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19527 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
528 search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04529 ASSERT_TRUE(result.is_valid());
530 EXPECT_EQ(test_data[i].expected_result, result.spec());
531 }
532}
533
534// Tests replacing assisted query stats (AQS) in various scenarios.
535TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
536 struct TestData {
[email protected]0085863a2013-12-06 21:19:03537 const base::string16 search_term;
[email protected]bca359b2012-06-24 07:53:04538 const std::string aqs;
539 const std::string base_url;
540 const std::string url;
541 const std::string expected_result;
542 } test_data[] = {
543 // No HTTPS, no AQS.
544 { ASCIIToUTF16("foo"),
545 "chrome.0.0l6",
546 "http://foo/",
547 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
548 "http://foo/?foo" },
549 // HTTPS available, AQS should be replaced.
550 { ASCIIToUTF16("foo"),
551 "chrome.0.0l6",
552 "https://foo/",
553 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
554 "https://foo/?fooaqs=chrome.0.0l6&" },
555 // HTTPS available, however AQS is empty.
556 { ASCIIToUTF16("foo"),
557 "",
558 "https://foo/",
559 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
560 "https://foo/?foo" },
561 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
562 { ASCIIToUTF16("foo"),
563 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44564 "http://www.google.com",
[email protected]bca359b2012-06-24 07:53:04565 "http://foo?{searchTerms}{google:assistedQueryStats}",
566 "http://foo/?foo" },
567 // A non-Google search provider with HTTPS should allow AQS.
568 { ASCIIToUTF16("foo"),
569 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44570 "https://www.google.com",
[email protected]bca359b2012-06-24 07:53:04571 "https://foo?{searchTerms}{google:assistedQueryStats}",
572 "https://foo/?fooaqs=chrome.0.0l6&" },
573 };
574 TemplateURLData data;
575 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27576 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04577 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28578 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19579 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
580 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04581 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
582 search_terms_args.assisted_query_stats = test_data[i].aqs;
[email protected]798baa8e2014-06-20 05:42:44583 search_terms_data_.set_google_base_url(test_data[i].base_url);
[email protected]ce7ee5f2014-06-16 23:41:19584 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
585 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27586 ASSERT_TRUE(result.is_valid());
587 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56588 }
589}
590
[email protected]00790562012-12-14 09:57:16591// Tests replacing cursor position.
592TEST_F(TemplateURLTest, ReplaceCursorPosition) {
593 struct TestData {
[email protected]0085863a2013-12-06 21:19:03594 const base::string16 search_term;
[email protected]00790562012-12-14 09:57:16595 size_t cursor_position;
596 const std::string url;
597 const std::string expected_result;
598 } test_data[] = {
599 { ASCIIToUTF16("foo"),
[email protected]0085863a2013-12-06 21:19:03600 base::string16::npos,
[email protected]00790562012-12-14 09:57:16601 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
602 "http://www.google.com/?foo&" },
603 { ASCIIToUTF16("foo"),
604 2,
605 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
606 "http://www.google.com/?foo&cp=2&" },
607 { ASCIIToUTF16("foo"),
608 15,
609 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
610 "http://www.google.com/?foo&cp=15&" },
611 };
612 TemplateURLData data;
613 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27614 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]00790562012-12-14 09:57:16615 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28616 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19617 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
618 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]00790562012-12-14 09:57:16619 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
620 search_terms_args.cursor_position = test_data[i].cursor_position;
[email protected]ce7ee5f2014-06-16 23:41:19621 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
622 search_terms_data_));
[email protected]00790562012-12-14 09:57:16623 ASSERT_TRUE(result.is_valid());
624 EXPECT_EQ(test_data[i].expected_result, result.spec());
625 }
626}
627
[email protected]420472b22014-06-10 13:34:43628// Tests replacing input type (&oit=).
629TEST_F(TemplateURLTest, ReplaceInputType) {
630 struct TestData {
631 const base::string16 search_term;
[email protected]332d17d22014-06-20 16:56:03632 metrics::OmniboxInputType::Type input_type;
[email protected]420472b22014-06-10 13:34:43633 const std::string url;
634 const std::string expected_result;
635 } test_data[] = {
636 { ASCIIToUTF16("foo"),
637 metrics::OmniboxInputType::UNKNOWN,
638 "{google:baseURL}?{searchTerms}&{google:inputType}",
639 "http://www.google.com/?foo&oit=1&" },
640 { ASCIIToUTF16("foo"),
641 metrics::OmniboxInputType::URL,
642 "{google:baseURL}?{searchTerms}&{google:inputType}",
643 "http://www.google.com/?foo&oit=3&" },
644 { ASCIIToUTF16("foo"),
645 metrics::OmniboxInputType::FORCED_QUERY,
646 "{google:baseURL}?{searchTerms}&{google:inputType}",
647 "http://www.google.com/?foo&oit=5&" },
648 };
[email protected]420472b22014-06-10 13:34:43649 TemplateURLData data;
650 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27651 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]420472b22014-06-10 13:34:43652 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28653 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19654 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
655 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43656 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
657 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19658 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
659 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43660 ASSERT_TRUE(result.is_valid());
661 EXPECT_EQ(test_data[i].expected_result, result.spec());
662 }
663}
664
[email protected]9b9fa672013-11-07 06:04:52665// Tests replacing currentPageUrl.
666TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48667 struct TestData {
[email protected]0085863a2013-12-06 21:19:03668 const base::string16 search_term;
[email protected]9b9fa672013-11-07 06:04:52669 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48670 const std::string url;
671 const std::string expected_result;
672 } test_data[] = {
673 { ASCIIToUTF16("foo"),
674 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52675 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48676 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
677 { ASCIIToUTF16("foo"),
678 "",
[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&" },
681 { ASCIIToUTF16("foo"),
682 "http://g.com/+-/*&=",
[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&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
685 };
686 TemplateURLData data;
687 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27688 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]800569d2013-05-06 07:38:48689 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28690 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19691 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
692 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]800569d2013-05-06 07:38:48693 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52694 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19695 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
696 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48697 ASSERT_TRUE(result.is_valid());
698 EXPECT_EQ(test_data[i].expected_result, result.spec());
699 }
700}
701
[email protected]d82443b2009-01-15 19:54:56702TEST_F(TemplateURLTest, Suggestions) {
703 struct TestData {
704 const int accepted_suggestion;
[email protected]0085863a2013-12-06 21:19:03705 const base::string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56706 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27707 } test_data[] = {
[email protected]0085863a2013-12-06 21:19:03708 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
[email protected]29653892013-03-02 19:25:37709 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30710 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37711 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03712 { TemplateURLRef::NO_SUGGESTION_CHOSEN, 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_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37715 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03716 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
[email protected]29653892013-03-02 19:25:37717 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56718 };
[email protected]573889f22012-04-07 01:31:54719 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37720 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
721 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54722 data.input_encodings.push_back("UTF-8");
[email protected]168d08722014-06-18 07:13:28723 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19724 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
725 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
viettrungluu37a447b2014-10-16 18:23:27726 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04727 TemplateURLRef::SearchTermsArgs search_terms_args(
728 ASCIIToUTF16("foobar"));
729 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
730 search_terms_args.original_query =
731 test_data[i].original_query_for_suggestion;
[email protected]ce7ee5f2014-06-16 23:41:19732 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
733 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27734 ASSERT_TRUE(result.is_valid());
735 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56736 }
737}
738
[email protected]81f808de2009-09-25 01:36:34739TEST_F(TemplateURLTest, RLZ) {
[email protected]798baa8e2014-06-20 05:42:44740 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(false);
[email protected]d82443b2009-01-15 19:54:56741
[email protected]573889f22012-04-07 01:31:54742 TemplateURLData data;
743 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28744 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19745 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
746 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04747 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19748 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56749 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44750 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
751 result.spec());
[email protected]d82443b2009-01-15 19:54:56752}
753
[email protected]c8ccc41d2014-04-10 04:42:12754TEST_F(TemplateURLTest, RLZFromAppList) {
[email protected]798baa8e2014-06-20 05:42:44755 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(true);
[email protected]c8ccc41d2014-04-10 04:42:12756
757 TemplateURLData data;
758 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28759 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19760 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
761 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12762 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
763 args.from_app_list = true;
[email protected]ce7ee5f2014-06-16 23:41:19764 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12765 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44766 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
767 result.spec());
[email protected]c8ccc41d2014-04-10 04:42:12768}
[email protected]c8ccc41d2014-04-10 04:42:12769
[email protected]d82443b2009-01-15 19:54:56770TEST_F(TemplateURLTest, HostAndSearchTermKey) {
771 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19772 const std::string url;
[email protected]d82443b2009-01-15 19:54:56773 const std::string host;
774 const std::string path;
775 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54776 } test_data[] = {
[email protected]7c60f5042013-02-14 03:39:32777 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
alexmos294849f2015-03-14 00:55:06778 { "http://blah/{searchTerms}", "blah", "/", ""},
[email protected]d82443b2009-01-15 19:54:56779
780 // No term should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32781 { "http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56782
783 // Multiple terms should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32784 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56785
786 // Term in the host shouldn't match.
[email protected]7c60f5042013-02-14 03:39:32787 { "http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56788
[email protected]7c60f5042013-02-14 03:39:32789 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
790 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56791
792 // Single term with extra chars in value should match.
[email protected]7c60f5042013-02-14 03:39:32793 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56794 };
795
viettrungluu37a447b2014-10-16 18:23:27796 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54797 TemplateURLData data;
798 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28799 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19800 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
801 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
802 EXPECT_EQ(test_data[i].search_term_key,
803 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56804 }
805}
806
alexmos294849f2015-03-14 00:55:06807TEST_F(TemplateURLTest, SearchTermKeyLocation) {
808 struct TestData {
809 const std::string url;
810 const url::Parsed::ComponentType location;
811 const std::string path;
812 size_t position_in_path;
813 } test_data[] = {
814 { "http://blah/{searchTerms}/", url::Parsed::PATH, "//", 1 },
815 { "http://blah/{searchTerms}", url::Parsed::PATH, "/", 1 },
816 { "http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "/begin//end", 7 },
817
818 { "http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY,
819 "/", std::string::npos },
820 { "http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF,
821 "/", std::string::npos },
amistrybf7ec54f2015-06-04 06:15:41822 // searchTerms is a key, not a value, so this should result in an empty
823 // value.
824 { "http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x",
825 url::Parsed::QUERY, std::string(), std::string::npos },
alexmos294849f2015-03-14 00:55:06826
827 // Multiple search terms should result in empty values.
828 { "http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY,
829 "", std::string::npos },
830 { "http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY,
831 "", std::string::npos },
832 { "http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY,
833 "", std::string::npos },
834 };
835
836 for (size_t i = 0; i < arraysize(test_data); ++i) {
837 TemplateURLData data;
838 data.SetURL(test_data[i].url);
839 TemplateURL url(data);
840 EXPECT_EQ(test_data[i].location,
841 url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
842 EXPECT_EQ(test_data[i].path,
843 url.url_ref().GetPath(search_terms_data_));
844 EXPECT_EQ(test_data[i].position_in_path,
845 url.url_ref().GetSearchTermPositionInPath(search_terms_data_));
846 }
847}
848
[email protected]d82443b2009-01-15 19:54:56849TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
850 static const struct {
[email protected]ddd231e2010-06-29 20:35:19851 const char* const base_url;
852 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56853 } data[] = {
[email protected]014010e2011-10-01 04:12:44854 { "http://google.com/", "http://google.com/complete/", },
855 { "http://www.google.com/", "http://www.google.com/complete/", },
856 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
857 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
858 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56859 };
860
viettrungluu37a447b2014-10-16 18:23:27861 for (size_t i = 0; i < arraysize(data); ++i)
[email protected]d82443b2009-01-15 19:54:56862 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
863}
864
[email protected]81c6ef62010-01-21 09:58:47865TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19866 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:54867 TemplateURLData data;
868 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28869 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47870 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:13871 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19872 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47873 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27874 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47875 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
876}
877
878TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:20879 std::string parsed_url("{fhqwhgads}abc");
[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]1a257262011-06-28 22:15:44884
885 // By default, TemplateURLRef should not consider itself prepopulated.
886 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:13887 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20888 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:44889 EXPECT_TRUE(replacements.empty());
890
891 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:20892 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:54893 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:28894 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:15895 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20896 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47897 EXPECT_TRUE(replacements.empty());
898}
899
900TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:28901 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:47902 TemplateURLRef::Replacements replacements;
903 bool valid = false;
[email protected]b37bdfe2012-03-16 20:53:27904 EXPECT_EQ(std::string(),
[email protected]93b29062013-07-12 03:09:09905 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47906 EXPECT_TRUE(replacements.empty());
907 EXPECT_TRUE(valid);
908}
909
910TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:54911 TemplateURLData data;
912 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:28913 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47914 TemplateURLRef::Replacements replacements;
915 bool valid = false;
[email protected]93b29062013-07-12 03:09:09916 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
917 &valid));
[email protected]81c6ef62010-01-21 09:58:47918 EXPECT_TRUE(replacements.empty());
919 EXPECT_FALSE(valid);
920}
921
922TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:54923 TemplateURLData data;
924 data.SetURL("{}");
[email protected]168d08722014-06-18 07:13:28925 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47926 TemplateURLRef::Replacements replacements;
927 bool valid = false;
[email protected]93b29062013-07-12 03:09:09928 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47929 EXPECT_TRUE(replacements.empty());
930 EXPECT_TRUE(valid);
931}
932
933TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:54934 TemplateURLData data;
935 data.SetURL("{}{{%s}}");
[email protected]168d08722014-06-18 07:13:28936 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47937 TemplateURLRef::Replacements replacements;
938 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19939 EXPECT_EQ("{}{}",
[email protected]93b29062013-07-12 03:09:09940 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
941 &valid));
[email protected]81c6ef62010-01-21 09:58:47942 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27943 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47944 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
945 EXPECT_TRUE(valid);
946}
947
948TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:54949 TemplateURLData data;
950 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:28951 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47952 TemplateURLRef::Replacements replacements;
953 bool valid = false;
[email protected]360ba052012-04-04 17:26:13954 EXPECT_EQ("{",
[email protected]93b29062013-07-12 03:09:09955 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
956 &valid));
[email protected]81c6ef62010-01-21 09:58:47957 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27958 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47959 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
960 EXPECT_TRUE(valid);
961}
[email protected]5b3078752012-10-09 18:54:16962
[email protected]fd6d8822012-12-08 06:56:11963TEST_F(TemplateURLTest, SearchClient) {
964 const std::string base_url_str("http://google.com/?");
965 const std::string terms_str("{searchTerms}&{google:searchClient}");
966 const std::string full_url_str = base_url_str + terms_str;
[email protected]0085863a2013-12-06 21:19:03967 const base::string16 terms(ASCIIToUTF16(terms_str));
[email protected]798baa8e2014-06-20 05:42:44968 search_terms_data_.set_google_base_url(base_url_str);
[email protected]fd6d8822012-12-08 06:56:11969
970 TemplateURLData data;
971 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:28972 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19973 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
974 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11975 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
976
977 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:19978 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
979 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11980 ASSERT_TRUE(result.is_valid());
981 EXPECT_EQ("http://google.com/?foobar&", result.spec());
982
983 // Check that the URL is correct when a client is present.
[email protected]798baa8e2014-06-20 05:42:44984 search_terms_data_.set_search_client("search_client");
[email protected]ce7ee5f2014-06-16 23:41:19985 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
986 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11987 ASSERT_TRUE(result_2.is_valid());
[email protected]798baa8e2014-06-20 05:42:44988 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
[email protected]fd6d8822012-12-08 06:56:11989}
[email protected]fd6d8822012-12-08 06:56:11990
[email protected]5b3078752012-10-09 18:54:16991TEST_F(TemplateURLTest, GetURLNoInstantURL) {
992 TemplateURLData data;
993 data.SetURL("http://google.com/?q={searchTerms}");
994 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
995 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
996 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:28997 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:51998 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
999 ASSERT_EQ(3U, url_refs.size());
1000 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1001 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1002 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161003}
1004
1005TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
1006 TemplateURLData data;
1007 data.SetURL("http://google.com/?q={searchTerms}");
1008 data.instant_url = "http://google.com/instant#q={searchTerms}";
1009 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
1010 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281011 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511012 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1013 ASSERT_EQ(3U, url_refs.size());
1014 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL());
1015 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL());
1016 EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL());
[email protected]5b3078752012-10-09 18:54:161017}
1018
1019TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
1020 TemplateURLData data;
1021 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:281022 TemplateURL url(data);
vitbar5bd8c252016-01-29 18:44:511023 const std::vector<TemplateURLRef>& url_refs = url.url_refs();
1024 ASSERT_EQ(1U, url_refs.size());
1025 EXPECT_EQ("http://www.google.co.uk/", url_refs[0].GetURL());
[email protected]5b3078752012-10-09 18:54:161026}
1027
1028TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1029 TemplateURLData data;
1030 data.SetURL("http://google.com/?q={searchTerms}");
1031 data.instant_url = "http://google.com/instant#q={searchTerms}";
1032 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1033 data.alternate_urls.push_back(
1034 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281035 TemplateURL url(data);
[email protected]0085863a2013-12-06 21:19:031036 base::string16 result;
[email protected]5b3078752012-10-09 18:54:161037
1038 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191039 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191040 EXPECT_EQ(ASCIIToUTF16("something"), result);
1041
1042 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191043 GURL("http://google.com/?espv&q=something"),
1044 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191045 EXPECT_EQ(ASCIIToUTF16("something"), result);
1046
1047 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191048 GURL("http://google.com/?espv=1&q=something"),
1049 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191050 EXPECT_EQ(ASCIIToUTF16("something"), result);
1051
1052 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191053 GURL("http://google.com/?espv=0&q=something"),
1054 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191055 EXPECT_EQ(ASCIIToUTF16("something"), result);
1056
1057 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191058 GURL("http://google.com/alt/#q=something"),
1059 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191060 EXPECT_EQ(ASCIIToUTF16("something"), result);
1061
1062 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191063 GURL("http://google.com/alt/#espv&q=something"),
1064 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191065 EXPECT_EQ(ASCIIToUTF16("something"), result);
1066
1067 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191068 GURL("http://google.com/alt/#espv=1&q=something"),
1069 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191070 EXPECT_EQ(ASCIIToUTF16("something"), result);
1071
1072 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191073 GURL("http://google.com/alt/#espv=0&q=something"),
1074 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161075 EXPECT_EQ(ASCIIToUTF16("something"), result);
1076
1077 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191078 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371079 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161080
1081 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191082 GURL("http://google.ca/?q=something&q=anything"),
1083 search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371084 EXPECT_EQ(base::string16(), result);
[email protected]67d8b752013-04-03 17:33:271085
1086 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191087 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371088 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161089
[email protected]32e2d81b2012-10-16 19:03:251090 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191091 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
[email protected]32e2d81b2012-10-16 19:03:251092 EXPECT_EQ(ASCIIToUTF16("foo"), 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.com:8080/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371096 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161097
1098 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191099 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161100 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1101
1102 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191103 GURL("http://google.com/alt/?q=123#q=456"),
1104 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161105 EXPECT_EQ(ASCIIToUTF16("456"), result);
1106
1107 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191108 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1109 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161110 EXPECT_EQ(ASCIIToUTF16("123"), result);
1111
1112 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191113 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1114 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161115 EXPECT_EQ(ASCIIToUTF16("789"), result);
1116
1117 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191118 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371119 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161120
1121 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191122 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371123 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161124
1125 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191126 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371127 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161128
1129 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191130 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371131 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161132
1133 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191134 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161135 EXPECT_EQ(ASCIIToUTF16("123"), result);
1136}
[email protected]4076ea62013-01-09 01:47:191137
alexmos294849f2015-03-14 00:55:061138TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1139 TemplateURLData data;
1140 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1141 TemplateURL url(data);
1142 base::string16 result;
1143
1144 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1145 GURL("http://term-in-path.com/begin/something/end"),
1146 search_terms_data_, &result));
1147 EXPECT_EQ(ASCIIToUTF16("something"), result);
1148
1149 // "%20" must be converted to space.
1150 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1151 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1152 search_terms_data_, &result));
1153 EXPECT_EQ(ASCIIToUTF16("a b c"), result);
1154
1155 // Plus must not be converted to space.
1156 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1157 GURL("http://term-in-path.com/begin/1+2+3/end"),
1158 search_terms_data_, &result));
1159 EXPECT_EQ(ASCIIToUTF16("1+2+3"), result);
1160
1161 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1162 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
1163 EXPECT_EQ(base::string16(), result);
1164
1165 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1166 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
1167 EXPECT_EQ(base::string16(), result);
1168
1169 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1170 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
1171 EXPECT_EQ(base::string16(), result);
1172}
1173
vitbar27804d102015-04-15 10:54:411174// Checks that the ExtractSearchTermsFromURL function works correctly
1175// for urls containing non-latin characters in UTF8 encoding.
1176TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
1177 TemplateURLData data;
1178 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1179 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1180 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1181 TemplateURL url(data);
1182 base::string16 result;
1183
1184 // Russian text encoded with UTF-8.
1185 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551186 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1187 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
vitbar27804d102015-04-15 10:54:411188 search_terms_data_, &result));
1189 EXPECT_EQ(
1190 base::WideToUTF16(
1191 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1192 L"\x043C\x0438\x0440!"),
1193 result);
1194
1195 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551196 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1197 "%D0%B0"),
vitbar27804d102015-04-15 10:54:411198 search_terms_data_, &result));
1199 EXPECT_EQ(
1200 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1201 result);
1202
1203 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551204 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1205 "%D0%B8%20A"),
vitbar27804d102015-04-15 10:54:411206 search_terms_data_, &result));
1207 EXPECT_EQ(
1208 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1209 result);
1210}
1211
1212// Checks that the ExtractSearchTermsFromURL function works correctly
1213// for urls containing non-latin characters in non-UTF8 encoding.
1214TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) {
1215 TemplateURLData data;
1216 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1217 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1218 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1219 data.input_encodings.push_back("windows-1251");
1220 TemplateURL url(data);
1221 base::string16 result;
1222
1223 // Russian text encoded with Windows-1251.
1224 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1225 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1226 "%EC%E8%F0!"),
1227 search_terms_data_, &result));
1228 EXPECT_EQ(
1229 base::WideToUTF16(
1230 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1231 L"\x043C\x0438\x0440!"),
1232 result);
1233
1234 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1235 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1236 search_terms_data_, &result));
1237 EXPECT_EQ(
1238 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1239 result);
1240
1241 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1242 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1243 search_terms_data_, &result));
1244 EXPECT_EQ(
1245 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1246 result);
1247}
1248
jbroman50832022016-04-21 23:53:001249// Checks that the ExtractSearchTermsFromURL function strips constant
1250// prefix/suffix strings from the search terms param.
1251TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) {
1252 TemplateURLData data;
1253 data.alternate_urls.push_back(
1254 "http://www.example.com/?q=chromium-{searchTerms}@chromium.org");
1255 data.alternate_urls.push_back(
1256 "http://www.example.com/chromium-{searchTerms}@chromium.org/info");
1257 TemplateURL url(data);
1258 base::string16 result;
1259
1260 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1261 GURL("http://www.example.com/[email protected]"),
1262 search_terms_data_, &result));
1263 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1264
1265 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1266 GURL("http://www.example.com/[email protected]/info"),
1267 search_terms_data_, &result));
1268 EXPECT_EQ(ASCIIToUTF16("dev"), result);
1269
1270 // Don't match if the prefix and suffix aren't there.
1271 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1272 GURL("http://www.example.com/?q=invalid"), search_terms_data_, &result));
1273}
1274
[email protected]4076ea62013-01-09 01:47:191275TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
1276 TemplateURLData data;
1277 data.SetURL("http://google.com/?q={searchTerms}");
1278 data.instant_url = "http://google.com/instant#q={searchTerms}";
1279 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1280 data.alternate_urls.push_back(
1281 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1282 data.search_terms_replacement_key = "espv";
[email protected]168d08722014-06-18 07:13:281283 TemplateURL url(data);
[email protected]4076ea62013-01-09 01:47:191284
1285 // Test with instant enabled required.
1286 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1287 GURL("http://google.com/")));
1288
1289 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1290 GURL("http://google.com/?espv")));
1291
1292 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1293 GURL("http://google.com/#espv")));
1294
1295 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1296 GURL("http://google.com/?q=something")));
1297
1298 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1299 GURL("http://google.com/?q=something&espv")));
1300
1301 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1302 GURL("http://google.com/?q=something&espv=1")));
1303
1304 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1305 GURL("http://google.com/?q=something&espv=0")));
1306
1307 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1308 GURL("http://google.com/?espv&q=something")));
1309
1310 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1311 GURL("http://google.com/?espv=1&q=something")));
1312
1313 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1314 GURL("http://google.com/?espv=0&q=something")));
1315
1316 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1317 GURL("http://google.com/alt/#q=something")));
1318
1319 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1320 GURL("http://google.com/alt/#q=something&espv")));
1321
1322 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1323 GURL("http://google.com/alt/#q=something&espv=1")));
1324
1325 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1326 GURL("http://google.com/alt/#q=something&espv=0")));
1327
1328 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1329 GURL("http://google.com/alt/#espv&q=something")));
1330
1331 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1332 GURL("http://google.com/alt/#espv=1&q=something")));
1333
1334 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1335 GURL("http://google.com/alt/#espv=0&q=something")));
1336
1337 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1338 GURL("http://google.com/?espv#q=something")));
1339
1340 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1341 GURL("http://google.com/?espv=1#q=something")));
1342
1343 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1344 GURL("http://google.com/?q=something#espv")));
1345
1346 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1347 GURL("http://google.com/?q=something#espv=1")));
1348
1349 // This does not ensure the domain matches.
1350 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1351 GURL("http://bing.com/?espv")));
1352
1353 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1354 GURL("http://bing.com/#espv")));
1355}
[email protected]f62e30f52013-03-23 03:45:151356
1357TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1358 TemplateURLData data;
1359 data.SetURL("http://google.com/?q={searchTerms}");
1360 data.instant_url = "http://google.com/instant#q={searchTerms}";
1361 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1362 data.alternate_urls.push_back(
1363 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281364 TemplateURL url(data);
[email protected]f62e30f52013-03-23 03:45:151365 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1366 GURL result;
1367
1368 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191369 GURL("http://google.com/?q=something"), search_terms,
1370 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101371 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151372
1373 result = GURL("http://should.not.change.com");
1374 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191375 GURL("http://google.ca/?q=something"), search_terms,
1376 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151377 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1378
1379 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191380 GURL("http://google.com/foo/?q=foo"), search_terms,
1381 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151382
1383 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191384 GURL("https://google.com/?q=foo"), search_terms,
1385 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101386 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151387
1388 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191389 GURL("http://google.com:8080/?q=foo"), search_terms,
1390 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151391
1392 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191393 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1394 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101395 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result);
[email protected]f62e30f52013-03-23 03:45:151396
1397 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1398 // template_url.cc for details.
1399 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191400 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1401 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101402 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151403
1404 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1405 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191406 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101407 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
[email protected]f62e30f52013-03-23 03:45:151408 result);
1409
1410 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1411 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191412 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151413 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
vitbarf298455d2015-04-21 12:58:101414 "#j=abc&q=Bob+Morane&h=def9"), result);
[email protected]f62e30f52013-03-23 03:45:151415
1416 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191417 GURL("http://google.com/alt/?q="), search_terms,
1418 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151419
1420 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191421 GURL("http://google.com/alt/?#q="), search_terms,
1422 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151423
1424 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191425 GURL("http://google.com/alt/?q=#q="), search_terms,
1426 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151427
1428 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191429 GURL("http://google.com/alt/?q=123#q="), search_terms,
1430 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151431
1432 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191433 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1434 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101435 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151436}
[email protected]56fa29592013-07-02 20:25:531437
alexmos294849f2015-03-14 00:55:061438TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1439 TemplateURLData data;
1440 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1441 TemplateURL url(data);
1442 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1443 GURL result;
1444
1445 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1446 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1447 search_terms_data_, &result));
1448 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1449
1450 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1451 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1452 search_terms_data_, &result));
1453 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1454
1455 result = GURL("http://should.not.change.com");
1456 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1457 GURL("http://term-in-path.com/about"), search_terms,
1458 search_terms_data_, &result));
1459 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1460}
1461
vitbarf298455d2015-04-21 12:58:101462// Checks that the ReplaceSearchTermsInURL function works correctly
1463// for search terms containing non-latin characters for a search engine
1464// using UTF-8 input encoding.
1465TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) {
1466 TemplateURLData data;
1467 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1468 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1469 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1470 TemplateURL url(data);
1471
1472 // Russian text which will be encoded with UTF-8.
1473 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1474 L"\x0442\x0435\x043A\x0441\x0442"));
1475 GURL result;
1476
1477 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1478 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_,
1479 &result));
1480 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1481 result);
1482
1483 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1484 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_,
1485 &result));
1486 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1487 result);
1488
1489 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1490 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_,
1491 &result));
1492 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1493 result);
1494}
1495
1496// Checks that the ReplaceSearchTermsInURL function works correctly
1497// for search terms containing non-latin characters for a search engine
1498// using non UTF-8 input encoding.
1499TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) {
1500 TemplateURLData data;
1501 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1502 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1503 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1504 data.input_encodings.push_back("windows-1251");
1505 TemplateURL url(data);
1506
1507 // Russian text which will be encoded with Windows-1251.
1508 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1509 L"\x0442\x0435\x043A\x0441\x0442"));
1510 GURL result;
1511
1512 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1513 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_,
1514 &result));
1515 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1516 result);
1517
1518 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1519 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_,
1520 &result));
1521 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1522 result);
1523
1524 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1525 GURL("http://windows-1251.ru/path/a%20b"), search_terms,
1526 search_terms_data_, &result));
1527 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1528 result);
1529}
1530
[email protected]621ade062013-10-28 06:27:431531// Test the |suggest_query_params| field of SearchTermsArgs.
1532TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431533 TemplateURLData data;
1534 // Pick a URL with replacements before, during, and after the query, to ensure
1535 // we don't goof up any of them.
1536 data.SetURL("{google:baseURL}search?q={searchTerms}"
1537 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281538 TemplateURL url(data);
[email protected]621ade062013-10-28 06:27:431539
1540 // Baseline: no |suggest_query_params| field.
1541 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1542 search_terms.original_query = ASCIIToUTF16("def");
1543 search_terms.accepted_suggestion = 0;
1544 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191545 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431546
1547 // Set the suggest_query_params.
1548 search_terms.suggest_query_params = "pq=xyz";
1549 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191550 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431551
1552 // Add extra_query_params in the mix, and ensure it works.
1553 search_terms.append_extra_query_params = true;
avi1772c1a2014-12-22 22:42:331554 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]621ade062013-10-28 06:27:431555 switches::kExtraSearchQueryParams, "a=b");
1556 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191557 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431558}
1559
[email protected]56fa29592013-07-02 20:25:531560// Test the |append_extra_query_params| field of SearchTermsArgs.
1561TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531562 TemplateURLData data;
1563 // Pick a URL with replacements before, during, and after the query, to ensure
1564 // we don't goof up any of them.
1565 data.SetURL("{google:baseURL}search?q={searchTerms}"
1566 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281567 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531568
1569 // Baseline: no command-line args, no |append_extra_query_params| flag.
1570 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1571 search_terms.original_query = ASCIIToUTF16("def");
1572 search_terms.accepted_suggestion = 0;
1573 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191574 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531575
1576 // Set the flag. Since there are no command-line args, this should have no
1577 // effect.
1578 search_terms.append_extra_query_params = true;
1579 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191580 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531581
1582 // Now append the command-line arg. This should be inserted into the query.
avi1772c1a2014-12-22 22:42:331583 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]56fa29592013-07-02 20:25:531584 switches::kExtraSearchQueryParams, "a=b");
1585 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191586 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531587
1588 // Turn off the flag. Now the command-line arg should be ignored again.
1589 search_terms.append_extra_query_params = false;
1590 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191591 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531592}
[email protected]d5015ca2013-08-08 22:04:181593
1594// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171595TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181596 TemplateURLData data;
1597 data.input_encodings.push_back("UTF-8");
1598 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281599 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191600 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1601 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]d5015ca2013-08-08 22:04:181602 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1603
[email protected]ce7ee5f2014-06-16 23:41:191604 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1605 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181606 EXPECT_EQ("http://www.google.com/?q=foo", result);
1607
[email protected]332d17d22014-06-20 16:56:031608 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191609 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1610 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181611 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1612
1613 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031614 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191615 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1616 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181617 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1618}
[email protected]2328ee22013-08-08 23:00:191619
1620// Test the IsSearchResults function.
1621TEST_F(TemplateURLTest, IsSearchResults) {
1622 TemplateURLData data;
1623 data.SetURL("http://bar/search?q={searchTerms}");
1624 data.instant_url = "http://bar/instant#q={searchTerms}";
[email protected]2767c0fd2013-08-16 17:44:161625 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191626 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1627 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1628 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1629 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281630 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191631
1632 const struct {
1633 const char* const url;
1634 bool result;
1635 } url_data[] = {
1636 { "http://bar/search?q=foo&oq=foo", true, },
1637 { "http://bar/?q=foo&oq=foo", true, },
1638 { "http://bar/#output=search&q=foo&oq=foo", true, },
1639 { "http://bar/webhp#q=foo&oq=foo", true, },
1640 { "http://bar/#q=foo&oq=foo", true, },
1641 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1642 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1643 { "http://bar/", false, },
1644 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161645 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191646 };
1647
viettrungluu37a447b2014-10-16 18:23:271648 for (size_t i = 0; i < arraysize(url_data); ++i) {
[email protected]2328ee22013-08-08 23:00:191649 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191650 search_provider.IsSearchURL(GURL(url_data[i].url),
1651 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191652 }
1653}
[email protected]a048de8a2013-10-02 18:30:061654
jdonnelly41c5b46a2015-07-10 21:24:381655TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) {
[email protected]9d70de12014-05-10 02:15:311656 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441657 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311658 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1659
[email protected]168d08722014-06-18 07:13:281660 TemplateURL url(data);
[email protected]9d70de12014-05-10 02:15:311661 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191662 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1663 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311664 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1665}
[email protected]20184242014-05-14 02:57:421666
1667TEST_F(TemplateURLTest, SessionToken) {
1668 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441669 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421670 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1671
[email protected]168d08722014-06-18 07:13:281672 TemplateURL url(data);
[email protected]20184242014-05-14 02:57:421673 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1674 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191675 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1676 search_terms_data_);
[email protected]20184242014-05-14 02:57:421677 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1678
[email protected]168d08722014-06-18 07:13:281679 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421680 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191681 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1682 search_terms_data_);
[email protected]20184242014-05-14 02:57:421683 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1684}
[email protected]448b17f52014-06-13 01:08:031685
1686TEST_F(TemplateURLTest, ContextualSearchParameters) {
1687 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441688 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031689 data.SetURL("http://bar/_/contextualsearch?"
1690 "{google:contextualSearchVersion}"
1691 "{google:contextualSearchContextData}");
1692
[email protected]168d08722014-06-18 07:13:281693 TemplateURL url(data);
[email protected]448b17f52014-06-13 01:08:031694 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191695 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1696 search_terms_data_);
[email protected]52e02aa2014-08-06 05:36:091697 EXPECT_EQ("http://bar/_/contextualsearch?ctxsl_resolve=1", result);
[email protected]448b17f52014-06-13 01:08:031698
1699 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
1700 1, 6, 11, "allen", "woody+allen+movies", "www.wikipedia.org",
[email protected]52e02aa2014-08-06 05:36:091701 "utf-8", true);
[email protected]448b17f52014-06-13 01:08:031702 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191703 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1704 search_terms_data_);
[email protected]448b17f52014-06-13 01:08:031705 EXPECT_EQ("http://bar/_/contextualsearch?"
1706 "ctxs=1&"
1707 "ctxs_start=6&"
1708 "ctxs_end=11&"
1709 "q=allen&"
1710 "ctxs_content=woody+allen+movies&"
[email protected]52e02aa2014-08-06 05:36:091711 "ctxsl_url=www.wikipedia.org&"
1712 "ctxs_encoding=utf-8&"
1713 "ctxsl_resolve=1",
1714 result);
[email protected]448b17f52014-06-13 01:08:031715}
[email protected]44ccc9f2014-06-20 17:36:211716
1717TEST_F(TemplateURLTest, GenerateKeyword) {
[email protected]44ccc9f2014-06-20 17:36:211718 ASSERT_EQ(ASCIIToUTF16("foo"),
jshin1fb76462016-04-05 22:13:031719 TemplateURL::GenerateKeyword(GURL("http://foo")));
[email protected]44ccc9f2014-06-20 17:36:211720 // www. should be stripped.
jshin1fb76462016-04-05 22:13:031721 ASSERT_EQ(ASCIIToUTF16("foo"),
1722 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
[email protected]44ccc9f2014-06-20 17:36:211723 // Make sure we don't get a trailing '/'.
jshin1fb76462016-04-05 22:13:031724 ASSERT_EQ(ASCIIToUTF16("blah"),
1725 TemplateURL::GenerateKeyword(GURL("http://blah/")));
[email protected]44ccc9f2014-06-20 17:36:211726 // Don't generate the empty string.
jshin1fb76462016-04-05 22:13:031727 ASSERT_EQ(ASCIIToUTF16("www"),
1728 TemplateURL::GenerateKeyword(GURL("http://www.")));
alshabaline6212ac2015-07-23 08:03:001729 ASSERT_EQ(
1730 base::UTF8ToUTF16("\xd0\xb0\xd0\xb1\xd0\xb2"),
jshin1fb76462016-04-05 22:13:031731 TemplateURL::GenerateKeyword(GURL("http://xn--80acd")));
[email protected]44ccc9f2014-06-20 17:36:211732}
1733
1734TEST_F(TemplateURLTest, GenerateSearchURL) {
1735 struct GenerateSearchURLCase {
1736 const char* test_name;
1737 const char* url;
1738 const char* expected;
1739 } generate_url_cases[] = {
1740 { "invalid URL", "foo{searchTerms}", "" },
1741 { "URL with no replacements", "http://foo/", "http://foo/" },
1742 { "basic functionality", "http://foo/{searchTerms}",
1743 "http://foo/blah.blah.blah.blah.blah" }
1744 };
1745
viettrungluu37a447b2014-10-16 18:23:271746 for (size_t i = 0; i < arraysize(generate_url_cases); ++i) {
[email protected]44ccc9f2014-06-20 17:36:211747 TemplateURLData data;
1748 data.SetURL(generate_url_cases[i].url);
1749 TemplateURL t_url(data);
1750 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1751 generate_url_cases[i].expected)
1752 << generate_url_cases[i].test_name << " failed.";
1753 }
1754}
[email protected]9e9130ce2014-06-23 23:20:511755
1756TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1757 TemplateURLData data;
1758 search_terms_data_.set_google_base_url("http://bar/");
1759 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1760
1761 TemplateURL url(data);
1762 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1763 search_terms_args.prefetch_query = "full query text";
1764 search_terms_args.prefetch_query_type = "2338";
1765 std::string result =
1766 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1767 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1768 result);
1769
1770 TemplateURL url2(data);
1771 search_terms_args.prefetch_query.clear();
1772 search_terms_args.prefetch_query_type.clear();
1773 result =
1774 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1775 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1776}
vitbar5bd8c252016-01-29 18:44:511777
1778// Tests that TemplateURL works correctly after changing the Google base URL
1779// and invalidating cached values.
1780TEST_F(TemplateURLTest, InvalidateCachedValues) {
1781 TemplateURLData data;
1782 data.SetURL("{google:baseURL}search?q={searchTerms}");
1783 data.suggestions_url = "{google:baseSuggestURL}search?q={searchTerms}";
1784 data.instant_url = "{google:baseURL}webhp";
1785 data.image_url = "{google:baseURL}searchbyimage/upload";
1786 data.new_tab_url = "{google:baseURL}_/chrome/newtab";
1787 data.contextual_search_url = "{google:baseURL}_/contextualsearch";
1788 data.alternate_urls.push_back("{google:baseURL}s#q={searchTerms}");
1789 TemplateURL url(data);
1790 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("X"));
1791 base::string16 search_terms;
1792
1793 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1794 EXPECT_EQ("http://www.google.com/search?q=X",
1795 url.url_ref().ReplaceSearchTerms(search_terms_args,
1796 search_terms_data_));
1797 EXPECT_EQ("http://www.google.com/s#q=X",
1798 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1799 search_terms_data_));
1800 EXPECT_EQ("http://www.google.com/search?q=X",
1801 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1802 search_terms_data_));
1803 EXPECT_EQ("http://www.google.com/complete/search?q=X",
1804 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1805 search_terms_data_));
1806 EXPECT_EQ("http://www.google.com/webhp",
1807 url.instant_url_ref().ReplaceSearchTerms(search_terms_args,
1808 search_terms_data_));
1809 EXPECT_EQ("http://www.google.com/searchbyimage/upload",
1810 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1811 search_terms_data_));
1812 EXPECT_EQ("http://www.google.com/_/chrome/newtab",
1813 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1814 search_terms_data_));
1815 EXPECT_EQ("http://www.google.com/_/contextualsearch",
1816 url.contextual_search_url_ref().ReplaceSearchTerms(
1817 search_terms_args, search_terms_data_));
1818
1819 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1820 GURL("http://www.google.com/search?q=Y+Z"),
1821 search_terms_data_, &search_terms));
1822 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
1823 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1824 GURL("http://www.google.com/s#q=123"),
1825 search_terms_data_, &search_terms));
1826 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
1827
1828 search_terms_data_.set_google_base_url("https://www.foo.org/");
1829 url.InvalidateCachedValues();
1830
1831 EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_));
1832 EXPECT_EQ("https://www.foo.org/search?q=X",
1833 url.url_ref().ReplaceSearchTerms(search_terms_args,
1834 search_terms_data_));
1835 EXPECT_EQ("https://www.foo.org/s#q=X",
1836 url.url_refs()[0].ReplaceSearchTerms(search_terms_args,
1837 search_terms_data_));
1838 EXPECT_EQ("https://www.foo.org/search?q=X",
1839 url.url_refs()[1].ReplaceSearchTerms(search_terms_args,
1840 search_terms_data_));
1841 EXPECT_EQ("https://www.foo.org/complete/search?q=X",
1842 url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args,
1843 search_terms_data_));
1844 EXPECT_EQ("https://www.foo.org/webhp",
1845 url.instant_url_ref().ReplaceSearchTerms(search_terms_args,
1846 search_terms_data_));
1847 EXPECT_EQ("https://www.foo.org/searchbyimage/upload",
1848 url.image_url_ref().ReplaceSearchTerms(search_terms_args,
1849 search_terms_data_));
1850 EXPECT_EQ("https://www.foo.org/_/chrome/newtab",
1851 url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args,
1852 search_terms_data_));
1853 EXPECT_EQ("https://www.foo.org/_/contextualsearch",
1854 url.contextual_search_url_ref().ReplaceSearchTerms(
1855 search_terms_args, search_terms_data_));
1856
1857 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1858 GURL("https://www.foo.org/search?q=Y+Z"),
1859 search_terms_data_, &search_terms));
1860 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms);
1861 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1862 GURL("https://www.foo.org/s#q=123"),
1863 search_terms_data_, &search_terms));
1864 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms);
1865
1866 search_terms_data_.set_google_base_url("http://www.google.com/");
1867}