blob: 2c941f7ed51e07213c8a94c6551398477cc40894 [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
[email protected]12bd05872009-03-17 19:25:065#include "base/base_paths.h"
[email protected]56fa29592013-07-02 20:25:536#include "base/command_line.h"
[email protected]2f3bc6512013-08-28 03:56:277#include "base/strings/string_number_conversions.h"
[email protected]24a555b62013-06-10 22:01:178#include "base/strings/string_util.h"
[email protected]e309f312013-06-07 21:50:089#include "base/strings/utf_string_conversions.h"
[email protected]332d17d22014-06-20 16:56:0310#include "components/metrics/proto/omnibox_event.pb.h"
[email protected]420472b22014-06-10 13:34:4311#include "components/metrics/proto/omnibox_input_type.pb.h"
[email protected]5c3c6e482014-06-23 09:47:1812#include "components/search_engines/search_engines_switches.h"
[email protected]798baa8e2014-06-20 05:42:4413#include "components/search_engines/search_terms_data.h"
[email protected]d550cb02014-06-25 06:48:1114#include "components/search_engines/template_url.h"
hashimoto3c831812014-08-25 07:40:2315#include "components/search_engines/testing_search_terms_data.h"
[email protected]d82443b2009-01-15 19:54:5616#include "testing/gtest/include/gtest/gtest.h"
17
[email protected]f911df52013-12-24 23:24:2318using base::ASCIIToUTF16;
19
[email protected]583844c2011-08-27 00:38:3520class TemplateURLTest : public testing::Test {
[email protected]d82443b2009-01-15 19:54:5621 public:
[email protected]798baa8e2014-06-20 05:42:4422 TemplateURLTest() : search_terms_data_("http://www.google.com/") {}
[email protected]b37bdfe2012-03-16 20:53:2723 void CheckSuggestBaseURL(const std::string& base_url,
24 const std::string& base_suggest_url) const;
[email protected]ce7ee5f2014-06-16 23:41:1925
vitbardeb285392015-02-20 14:02:5526 static void ExpectPostParamIs(
27 const TemplateURLRef::PostParam& param,
28 const std::string& name,
29 const std::string& value,
30 const std::string& content_type = std::string());
31
hashimoto3c831812014-08-25 07:40:2332 TestingSearchTermsData search_terms_data_;
[email protected]d82443b2009-01-15 19:54:5633};
34
[email protected]b37bdfe2012-03-16 20:53:2735void TemplateURLTest::CheckSuggestBaseURL(
36 const std::string& base_url,
37 const std::string& base_suggest_url) const {
hashimoto3c831812014-08-25 07:40:2338 TestingSearchTermsData search_terms_data(base_url);
[email protected]b37bdfe2012-03-16 20:53:2739 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue());
40}
41
vitbardeb285392015-02-20 14:02:5542// static
43void TemplateURLTest::ExpectPostParamIs(const TemplateURLRef::PostParam& param,
44 const std::string& name,
45 const std::string& value,
46 const std::string& content_type) {
47 EXPECT_EQ(name, param.name);
48 EXPECT_EQ(value, param.value);
49 EXPECT_EQ(content_type, param.content_type);
50}
51
[email protected]d82443b2009-01-15 19:54:5652TEST_F(TemplateURLTest, Defaults) {
[email protected]573889f22012-04-07 01:31:5453 TemplateURLData data;
54 EXPECT_FALSE(data.show_in_default_list);
55 EXPECT_FALSE(data.safe_for_autoreplace);
56 EXPECT_EQ(0, data.prepopulate_id);
[email protected]d82443b2009-01-15 19:54:5657}
58
59TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]573889f22012-04-07 01:31:5460 TemplateURLData data;
61 data.SetURL("{searchTerms}");
[email protected]168d08722014-06-18 07:13:2862 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1963 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:5664}
65
66TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1967 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1968 const char* url;
[email protected]0085863a2013-12-06 21:19:0369 const base::string16 terms;
[email protected]34e24852012-01-31 18:43:5870 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:1971 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:3072 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1673 "http://foosea%20rch/bar" },
[email protected]400b133f2011-01-19 18:32:3074 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1675 "http://foosea%20rch/bar?boo=abc" },
[email protected]400b133f2011-01-19 18:32:3076 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1677 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]400b133f2011-01-19 18:32:3078 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
[email protected]c31a979c2012-05-31 23:57:1679 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:1980 };
viettrungluu37a447b2014-10-16 18:23:2781 for (size_t i = 0; i < arraysize(search_term_cases); ++i) {
[email protected]0d2e6a62010-01-15 20:09:1982 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:5483 TemplateURLData data;
84 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:2885 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1986 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
87 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:0488 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:1989 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data_));
[email protected]c31a979c2012-05-31 23:57:1690 ASSERT_TRUE(result.is_valid());
91 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:1992 }
[email protected]d82443b2009-01-15 19:54:5693}
94
95TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:5496 TemplateURLData data;
97 data.SetURL("http://foo{searchTerms}{count?}");
[email protected]168d08722014-06-18 07:13:2898 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1999 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
100 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04101 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19102 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56103 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27104 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:56105}
106
107TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:54108 TemplateURLData data;
109 data.SetURL("http://foo{searchTerms}{count}");
[email protected]168d08722014-06-18 07:13:28110 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19111 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
112 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04113 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19114 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56115 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27116 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56117}
118
119TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54120 TemplateURLData data;
121 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]168d08722014-06-18 07:13:28122 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19123 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
124 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04125 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19126 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56127 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27128 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56129}
130
131TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54132 TemplateURLData data;
133 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[email protected]168d08722014-06-18 07:13:28134 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19135 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
136 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04137 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19138 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56139 ASSERT_TRUE(result.is_valid());
[email protected]405aae22012-03-29 20:36:13140 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56141}
142
143TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54144 TemplateURLData data;
145 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[email protected]168d08722014-06-18 07:13:28146 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19147 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
148 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04149 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19150 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56151 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27152 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56153}
154
[email protected]93b29062013-07-12 03:09:09155TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
156 const char kInvalidPostParamsString[] =
157 "unknown_template={UnknownTemplate},bad_value=bad{value},"
158 "{google:sbiSource}";
159 // List all accpectable parameter format in valid_post_params_string. it is
160 // expected like: "name0=,name1=value1,name2={template1}"
161 const char kValidPostParamsString[] =
162 "image_content={google:imageThumbnail},image_url={google:imageURL},"
163 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
[email protected]2f3bc6512013-08-28 03:56:27164 "constant_param=constant,width={google:imageOriginalWidth}";
[email protected]93b29062013-07-12 03:09:09165 const char KImageSearchURL[] = "http://foo.com/sbi";
166
167 TemplateURLData data;
168 data.image_url = KImageSearchURL;
169
170 // Try to parse invalid post parameters.
171 data.image_url_post_params = kInvalidPostParamsString;
[email protected]168d08722014-06-18 07:13:28172 TemplateURL url_bad(data);
[email protected]ce7ee5f2014-06-16 23:41:19173 ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09174 const TemplateURLRef::PostParams& bad_post_params =
175 url_bad.image_url_ref().post_params_;
176 ASSERT_EQ(2U, bad_post_params.size());
vitbardeb285392015-02-20 14:02:55177 ExpectPostParamIs(bad_post_params[0], "unknown_template",
178 "{UnknownTemplate}");
179 ExpectPostParamIs(bad_post_params[1], "bad_value", "bad{value}");
[email protected]93b29062013-07-12 03:09:09180
181 // Try to parse valid post parameters.
182 data.image_url_post_params = kValidPostParamsString;
[email protected]168d08722014-06-18 07:13:28183 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19184 ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_));
185 ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09186
187 // Check term replacement.
188 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
189 search_args.image_thumbnail_content = "dummy-image-thumbnail";
190 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
[email protected]2f3bc6512013-08-28 03:56:27191 search_args.image_original_size = gfx::Size(10, 10);
[email protected]93b29062013-07-12 03:09:09192 // Replacement operation with no post_data buffer should still return
193 // the parsed URL.
hashimoto3c831812014-08-25 07:40:23194 TestingSearchTermsData search_terms_data("http://X");
[email protected]ce7ee5f2014-06-16 23:41:19195 GURL result(url.image_url_ref().ReplaceSearchTerms(
196 search_args, search_terms_data));
[email protected]93b29062013-07-12 03:09:09197 ASSERT_TRUE(result.is_valid());
198 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48199 TemplateURLRef::PostContent post_content;
[email protected]ce7ee5f2014-06-16 23:41:19200 result = GURL(url.image_url_ref().ReplaceSearchTerms(
[email protected]7c2bcc42013-08-01 04:03:48201 search_args, search_terms_data, &post_content));
[email protected]93b29062013-07-12 03:09:09202 ASSERT_TRUE(result.is_valid());
203 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48204 ASSERT_FALSE(post_content.first.empty());
205 ASSERT_FALSE(post_content.second.empty());
[email protected]93b29062013-07-12 03:09:09206
207 // Check parsed result of post parameters.
208 const TemplateURLRef::Replacements& replacements =
209 url.image_url_ref().replacements_;
210 const TemplateURLRef::PostParams& post_params =
211 url.image_url_ref().post_params_;
[email protected]2f3bc6512013-08-28 03:56:27212 EXPECT_EQ(7U, post_params.size());
[email protected]93b29062013-07-12 03:09:09213 for (TemplateURLRef::PostParams::const_iterator i = post_params.begin();
214 i != post_params.end(); ++i) {
215 TemplateURLRef::Replacements::const_iterator j = replacements.begin();
216 for (; j != replacements.end(); ++j) {
217 if (j->is_post_param && j->index ==
218 static_cast<size_t>(i - post_params.begin())) {
219 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27220 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
vitbardeb285392015-02-20 14:02:55221 ExpectPostParamIs(*i, "width",
222 base::IntToString(
223 search_args.image_original_size.width()));
[email protected]2f3bc6512013-08-28 03:56:27224 break;
[email protected]1020fead2014-06-20 13:40:28225 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE:
vitbardeb285392015-02-20 14:02:55226 ExpectPostParamIs(*i, "sbisrc",
227 search_terms_data.GoogleImageSearchSource());
[email protected]1020fead2014-06-20 13:40:28228 break;
[email protected]93b29062013-07-12 03:09:09229 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
vitbardeb285392015-02-20 14:02:55230 ExpectPostParamIs(*i, "image_content",
231 search_args.image_thumbnail_content,
232 "image/jpeg");
[email protected]93b29062013-07-12 03:09:09233 break;
234 case TemplateURLRef::GOOGLE_IMAGE_URL:
vitbardeb285392015-02-20 14:02:55235 ExpectPostParamIs(*i, "image_url", search_args.image_url.spec());
[email protected]93b29062013-07-12 03:09:09236 break;
237 case TemplateURLRef::LANGUAGE:
vitbardeb285392015-02-20 14:02:55238 ExpectPostParamIs(*i, "language", "en");
[email protected]93b29062013-07-12 03:09:09239 break;
240 default:
241 ADD_FAILURE(); // Should never go here.
242 }
243 break;
244 }
245 }
246 if (j != replacements.end())
247 continue;
vitbardeb285392015-02-20 14:02:55248 if (i->name == "empty_param")
249 ExpectPostParamIs(*i, "empty_param", std::string());
250 else
251 ExpectPostParamIs(*i, "constant_param", "constant");
[email protected]93b29062013-07-12 03:09:09252 }
253}
254
[email protected]d88cb202011-08-17 20:03:01255// Test that setting the prepopulate ID from TemplateURL causes the stored
256// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44257TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54258 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20259 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]168d08722014-06-18 07:13:28260 TemplateURL url(data);
[email protected]1a257262011-06-28 22:15:44261 TemplateURLRef::Replacements replacements;
262 bool valid = false;
[email protected]243abf32012-05-15 18:28:20263 EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL(
[email protected]93b29062013-07-12 03:09:09264 "http://foo{fhqwhgads}bar", &replacements, NULL, &valid));
[email protected]1a257262011-06-28 22:15:44265 EXPECT_TRUE(replacements.empty());
266 EXPECT_TRUE(valid);
267
[email protected]573889f22012-04-07 01:31:54268 data.prepopulate_id = 123;
[email protected]168d08722014-06-18 07:13:28269 TemplateURL url2(data);
[email protected]243abf32012-05-15 18:28:20270 EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar",
[email protected]93b29062013-07-12 03:09:09271 &replacements, NULL,
272 &valid));
[email protected]1a257262011-06-28 22:15:44273 EXPECT_TRUE(replacements.empty());
274 EXPECT_TRUE(valid);
275}
276
[email protected]d82443b2009-01-15 19:54:56277TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54278 TemplateURLData data;
279 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]168d08722014-06-18 07:13:28280 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19281 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
282 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04283 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19284 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56285 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27286 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56287}
288
289TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54290 TemplateURLData data;
291 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]168d08722014-06-18 07:13:28292 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19293 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
294 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04295 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19296 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56297 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27298 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56299}
300
[email protected]375bd7312010-08-30 22:18:13301TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
302 struct SearchTermsCase {
303 const char* url;
[email protected]0085863a2013-12-06 21:19:03304 const base::string16 terms;
[email protected]375bd7312010-08-30 22:18:13305 const char* output;
306 } search_term_cases[] = {
[email protected]0085863a2013-12-06 21:19:03307 { "{google:baseURL}{language}{searchTerms}", base::string16(),
[email protected]b37bdfe2012-03-16 20:53:27308 "http://example.com/e/en" },
[email protected]0085863a2013-12-06 21:19:03309 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
[email protected]014010e2011-10-01 04:12:44310 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13311 };
312
hashimoto3c831812014-08-25 07:40:23313 TestingSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54314 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27315 for (size_t i = 0; i < arraysize(search_term_cases); ++i) {
[email protected]375bd7312010-08-30 22:18:13316 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54317 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:28318 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19319 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data));
320 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data));
321 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]93b29062013-07-12 03:09:09322 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL));
[email protected]375bd7312010-08-30 22:18:13323 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27324 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13325 }
326}
327
[email protected]d82443b2009-01-15 19:54:56328TEST_F(TemplateURLTest, URLRefTermToWide) {
329 struct ToWideCase {
330 const char* encoded_search_term;
[email protected]0085863a2013-12-06 21:19:03331 const base::string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56332 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30333 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56334 // Test some big-5 input.
[email protected]f911df52013-12-24 23:24:23335 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56336 // Test some UTF-8 input. We should fall back to this when the encoding
337 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
338 // Big-5 trailing byte.
[email protected]f911df52013-12-24 23:24:23339 {"%e4%bd%a05%e5%a5%bd+to+you",
340 base::WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56341 // Undecodable input should stay escaped.
[email protected]f911df52013-12-24 23:24:23342 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44343 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30344 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44345 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30346 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56347 };
348
[email protected]d82443b2009-01-15 19:54:56349 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54350 TemplateURLData data;
351 data.SetURL("http://foo?q={searchTerms}");
352 data.input_encodings.push_back("big-5");
[email protected]168d08722014-06-18 07:13:28353 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19354 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
355 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
viettrungluu37a447b2014-10-16 18:23:27356 for (size_t i = 0; i < arraysize(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07357 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13358 url.url_ref().SearchTermToString16(
359 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56360 }
361}
362
[email protected]d82443b2009-01-15 19:54:56363TEST_F(TemplateURLTest, DisplayURLToURLRef) {
364 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19365 const std::string url;
[email protected]0085863a2013-12-06 21:19:03366 const base::string16 expected_result;
[email protected]b37bdfe2012-03-16 20:53:27367 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19368 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30369 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19370 { "http://X",
[email protected]400b133f2011-01-19 18:32:30371 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19372 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30373 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19374 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30375 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56376 };
[email protected]573889f22012-04-07 01:31:54377 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27378 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54379 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28380 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19381 EXPECT_EQ(test_data[i].expected_result,
382 url.url_ref().DisplayURL(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27383 EXPECT_EQ(test_data[i].url,
[email protected]ce7ee5f2014-06-16 23:41:19384 TemplateURLRef::DisplayURLToURLRef(
385 url.url_ref().DisplayURL(search_terms_data_)));
[email protected]d82443b2009-01-15 19:54:56386 }
387}
388
389TEST_F(TemplateURLTest, ReplaceSearchTerms) {
390 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19391 const std::string url;
[email protected]d82443b2009-01-15 19:54:56392 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27393 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19394 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56395 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19396 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56397 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19398 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56399 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19400 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56401 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19402 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56403 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19404 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56405 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19406 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56407 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19408 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56409 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19410 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56411 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19412 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56413 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19414 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56415 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19416 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56417 "http://foo/UTF-8a{language}aXa" },
418 };
[email protected]573889f22012-04-07 01:31:54419 TemplateURLData data;
420 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27421 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54422 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28423 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19424 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
425 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27426 std::string expected_result = test_data[i].expected_result;
brettwe6dae462015-06-24 20:54:45427 base::ReplaceSubstringsAfterOffset(
428 &expected_result, 0, "{language}",
429 search_terms_data_.GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04430 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19431 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
432 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27433 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56434 EXPECT_EQ(expected_result, result.spec());
435 }
436}
437
438
439// Tests replacing search terms in various encodings and making sure the
440// generated URL matches the expected value.
441TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
442 struct TestData {
443 const std::string encoding;
[email protected]0085863a2013-12-06 21:19:03444 const base::string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19445 const std::string url;
[email protected]d82443b2009-01-15 19:54:56446 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27447 } test_data[] = {
[email protected]f911df52013-12-24 23:24:23448 { "BIG5", base::WideToUTF16(L"\x60BD"),
[email protected]400b133f2011-01-19 18:32:30449 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17450 "http://foo/?%B1~BIG5" },
[email protected]400b133f2011-01-19 18:32:30451 { "UTF-8", ASCIIToUTF16("blah"),
452 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17453 "http://foo/?blahUTF-8" },
[email protected]f911df52013-12-24 23:24:23454 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
[email protected]34e24852012-01-31 18:43:58455 "http://foo/{searchTerms}/bar",
456 "http://foo/%82%A0/bar"},
[email protected]f911df52013-12-24 23:24:23457 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
[email protected]34e24852012-01-31 18:43:58458 "http://foo/{searchTerms}/bar",
459 "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56460 };
[email protected]573889f22012-04-07 01:31:54461 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27462 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54463 data.SetURL(test_data[i].url);
464 data.input_encodings.clear();
465 data.input_encodings.push_back(test_data[i].encoding);
[email protected]168d08722014-06-18 07:13:28466 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19467 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
468 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04469 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19470 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
471 search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04472 ASSERT_TRUE(result.is_valid());
473 EXPECT_EQ(test_data[i].expected_result, result.spec());
474 }
475}
476
477// Tests replacing assisted query stats (AQS) in various scenarios.
478TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
479 struct TestData {
[email protected]0085863a2013-12-06 21:19:03480 const base::string16 search_term;
[email protected]bca359b2012-06-24 07:53:04481 const std::string aqs;
482 const std::string base_url;
483 const std::string url;
484 const std::string expected_result;
485 } test_data[] = {
486 // No HTTPS, no AQS.
487 { ASCIIToUTF16("foo"),
488 "chrome.0.0l6",
489 "http://foo/",
490 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
491 "http://foo/?foo" },
492 // HTTPS available, AQS should be replaced.
493 { ASCIIToUTF16("foo"),
494 "chrome.0.0l6",
495 "https://foo/",
496 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
497 "https://foo/?fooaqs=chrome.0.0l6&" },
498 // HTTPS available, however AQS is empty.
499 { ASCIIToUTF16("foo"),
500 "",
501 "https://foo/",
502 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
503 "https://foo/?foo" },
504 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
505 { ASCIIToUTF16("foo"),
506 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44507 "http://www.google.com",
[email protected]bca359b2012-06-24 07:53:04508 "http://foo?{searchTerms}{google:assistedQueryStats}",
509 "http://foo/?foo" },
510 // A non-Google search provider with HTTPS should allow AQS.
511 { ASCIIToUTF16("foo"),
512 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44513 "https://www.google.com",
[email protected]bca359b2012-06-24 07:53:04514 "https://foo?{searchTerms}{google:assistedQueryStats}",
515 "https://foo/?fooaqs=chrome.0.0l6&" },
516 };
517 TemplateURLData data;
518 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27519 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04520 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28521 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19522 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
523 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04524 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
525 search_terms_args.assisted_query_stats = test_data[i].aqs;
[email protected]798baa8e2014-06-20 05:42:44526 search_terms_data_.set_google_base_url(test_data[i].base_url);
[email protected]ce7ee5f2014-06-16 23:41:19527 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
528 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27529 ASSERT_TRUE(result.is_valid());
530 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56531 }
532}
533
[email protected]00790562012-12-14 09:57:16534// Tests replacing cursor position.
535TEST_F(TemplateURLTest, ReplaceCursorPosition) {
536 struct TestData {
[email protected]0085863a2013-12-06 21:19:03537 const base::string16 search_term;
[email protected]00790562012-12-14 09:57:16538 size_t cursor_position;
539 const std::string url;
540 const std::string expected_result;
541 } test_data[] = {
542 { ASCIIToUTF16("foo"),
[email protected]0085863a2013-12-06 21:19:03543 base::string16::npos,
[email protected]00790562012-12-14 09:57:16544 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
545 "http://www.google.com/?foo&" },
546 { ASCIIToUTF16("foo"),
547 2,
548 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
549 "http://www.google.com/?foo&cp=2&" },
550 { ASCIIToUTF16("foo"),
551 15,
552 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
553 "http://www.google.com/?foo&cp=15&" },
554 };
555 TemplateURLData data;
556 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27557 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]00790562012-12-14 09:57:16558 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28559 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19560 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
561 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]00790562012-12-14 09:57:16562 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
563 search_terms_args.cursor_position = test_data[i].cursor_position;
[email protected]ce7ee5f2014-06-16 23:41:19564 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
565 search_terms_data_));
[email protected]00790562012-12-14 09:57:16566 ASSERT_TRUE(result.is_valid());
567 EXPECT_EQ(test_data[i].expected_result, result.spec());
568 }
569}
570
[email protected]420472b22014-06-10 13:34:43571// Tests replacing input type (&oit=).
572TEST_F(TemplateURLTest, ReplaceInputType) {
573 struct TestData {
574 const base::string16 search_term;
[email protected]332d17d22014-06-20 16:56:03575 metrics::OmniboxInputType::Type input_type;
[email protected]420472b22014-06-10 13:34:43576 const std::string url;
577 const std::string expected_result;
578 } test_data[] = {
579 { ASCIIToUTF16("foo"),
580 metrics::OmniboxInputType::UNKNOWN,
581 "{google:baseURL}?{searchTerms}&{google:inputType}",
582 "http://www.google.com/?foo&oit=1&" },
583 { ASCIIToUTF16("foo"),
584 metrics::OmniboxInputType::URL,
585 "{google:baseURL}?{searchTerms}&{google:inputType}",
586 "http://www.google.com/?foo&oit=3&" },
587 { ASCIIToUTF16("foo"),
588 metrics::OmniboxInputType::FORCED_QUERY,
589 "{google:baseURL}?{searchTerms}&{google:inputType}",
590 "http://www.google.com/?foo&oit=5&" },
591 };
[email protected]420472b22014-06-10 13:34:43592 TemplateURLData data;
593 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27594 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]420472b22014-06-10 13:34:43595 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28596 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19597 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
598 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43599 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
600 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19601 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
602 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43603 ASSERT_TRUE(result.is_valid());
604 EXPECT_EQ(test_data[i].expected_result, result.spec());
605 }
606}
607
[email protected]9b9fa672013-11-07 06:04:52608// Tests replacing currentPageUrl.
609TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48610 struct TestData {
[email protected]0085863a2013-12-06 21:19:03611 const base::string16 search_term;
[email protected]9b9fa672013-11-07 06:04:52612 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48613 const std::string url;
614 const std::string expected_result;
615 } test_data[] = {
616 { ASCIIToUTF16("foo"),
617 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52618 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48619 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
620 { ASCIIToUTF16("foo"),
621 "",
[email protected]9b9fa672013-11-07 06:04:52622 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48623 "http://www.google.com/?foo&" },
624 { ASCIIToUTF16("foo"),
625 "http://g.com/+-/*&=",
[email protected]9b9fa672013-11-07 06:04:52626 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48627 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
628 };
629 TemplateURLData data;
630 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27631 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]800569d2013-05-06 07:38:48632 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28633 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19634 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
635 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]800569d2013-05-06 07:38:48636 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52637 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19638 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
639 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48640 ASSERT_TRUE(result.is_valid());
641 EXPECT_EQ(test_data[i].expected_result, result.spec());
642 }
643}
644
[email protected]1d7727e2014-07-23 07:04:43645TEST_F(TemplateURLTest, OmniboxStartmargin) {
646 struct TestData {
647 const bool enable_omnibox_start_margin;
648 const int omnibox_start_margin;
649 const std::string expected_result;
650 } test_data[] = {
651 { false,
652 0,
653 "http://bar/foo?q=foobar" },
654 { true,
655 0,
656 "http://bar/foo?es_sm=0&q=foobar" },
657 { true,
658 42,
659 "http://bar/foo?es_sm=42&q=foobar" },
660 };
661 TemplateURLData data;
662 data.SetURL("http://bar/foo?{google:omniboxStartMarginParameter}"
663 "q={searchTerms}");
664 data.input_encodings.push_back("UTF-8");
665 TemplateURL url(data);
666 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
667 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
viettrungluu37a447b2014-10-16 18:23:27668 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]1d7727e2014-07-23 07:04:43669 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
670 search_terms_args.enable_omnibox_start_margin =
671 test_data[i].enable_omnibox_start_margin;
672 search_terms_data_.set_omnibox_start_margin(
673 test_data[i].omnibox_start_margin);
674 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
675 search_terms_data_));
676 ASSERT_TRUE(result.is_valid());
677 EXPECT_EQ(test_data[i].expected_result, result.spec());
678 }
679}
680
[email protected]d82443b2009-01-15 19:54:56681TEST_F(TemplateURLTest, Suggestions) {
682 struct TestData {
683 const int accepted_suggestion;
[email protected]0085863a2013-12-06 21:19:03684 const base::string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56685 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27686 } test_data[] = {
[email protected]0085863a2013-12-06 21:19:03687 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
[email protected]29653892013-03-02 19:25:37688 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30689 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37690 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03691 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(),
[email protected]29653892013-03-02 19:25:37692 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30693 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37694 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03695 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
[email protected]29653892013-03-02 19:25:37696 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56697 };
[email protected]573889f22012-04-07 01:31:54698 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37699 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
700 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54701 data.input_encodings.push_back("UTF-8");
[email protected]168d08722014-06-18 07:13:28702 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19703 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
704 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
viettrungluu37a447b2014-10-16 18:23:27705 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04706 TemplateURLRef::SearchTermsArgs search_terms_args(
707 ASCIIToUTF16("foobar"));
708 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
709 search_terms_args.original_query =
710 test_data[i].original_query_for_suggestion;
[email protected]ce7ee5f2014-06-16 23:41:19711 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
712 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27713 ASSERT_TRUE(result.is_valid());
714 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56715 }
716}
717
[email protected]81f808de2009-09-25 01:36:34718TEST_F(TemplateURLTest, RLZ) {
[email protected]798baa8e2014-06-20 05:42:44719 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(false);
[email protected]d82443b2009-01-15 19:54:56720
[email protected]573889f22012-04-07 01:31:54721 TemplateURLData data;
722 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[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_));
[email protected]bca359b2012-06-24 07:53:04726 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19727 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56728 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44729 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
730 result.spec());
[email protected]d82443b2009-01-15 19:54:56731}
732
[email protected]c8ccc41d2014-04-10 04:42:12733TEST_F(TemplateURLTest, RLZFromAppList) {
[email protected]798baa8e2014-06-20 05:42:44734 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(true);
[email protected]c8ccc41d2014-04-10 04:42:12735
736 TemplateURLData data;
737 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28738 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19739 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
740 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12741 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
742 args.from_app_list = true;
[email protected]ce7ee5f2014-06-16 23:41:19743 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12744 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44745 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
746 result.spec());
[email protected]c8ccc41d2014-04-10 04:42:12747}
[email protected]c8ccc41d2014-04-10 04:42:12748
[email protected]d82443b2009-01-15 19:54:56749TEST_F(TemplateURLTest, HostAndSearchTermKey) {
750 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19751 const std::string url;
[email protected]d82443b2009-01-15 19:54:56752 const std::string host;
753 const std::string path;
754 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54755 } test_data[] = {
[email protected]7c60f5042013-02-14 03:39:32756 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
alexmos294849f2015-03-14 00:55:06757 { "http://blah/{searchTerms}", "blah", "/", ""},
[email protected]d82443b2009-01-15 19:54:56758
759 // No term should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32760 { "http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56761
762 // Multiple terms should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32763 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56764
765 // Term in the host shouldn't match.
[email protected]7c60f5042013-02-14 03:39:32766 { "http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56767
[email protected]7c60f5042013-02-14 03:39:32768 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
769 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56770
771 // Single term with extra chars in value should match.
[email protected]7c60f5042013-02-14 03:39:32772 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56773 };
774
viettrungluu37a447b2014-10-16 18:23:27775 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54776 TemplateURLData data;
777 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28778 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19779 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
780 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
781 EXPECT_EQ(test_data[i].search_term_key,
782 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56783 }
784}
785
alexmos294849f2015-03-14 00:55:06786TEST_F(TemplateURLTest, SearchTermKeyLocation) {
787 struct TestData {
788 const std::string url;
789 const url::Parsed::ComponentType location;
790 const std::string path;
791 size_t position_in_path;
792 } test_data[] = {
793 { "http://blah/{searchTerms}/", url::Parsed::PATH, "//", 1 },
794 { "http://blah/{searchTerms}", url::Parsed::PATH, "/", 1 },
795 { "http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "/begin//end", 7 },
796
797 { "http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY,
798 "/", std::string::npos },
799 { "http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF,
800 "/", std::string::npos },
amistrybf7ec54f2015-06-04 06:15:41801 // searchTerms is a key, not a value, so this should result in an empty
802 // value.
803 { "http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x",
804 url::Parsed::QUERY, std::string(), std::string::npos },
alexmos294849f2015-03-14 00:55:06805
806 // Multiple search terms should result in empty values.
807 { "http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY,
808 "", std::string::npos },
809 { "http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY,
810 "", std::string::npos },
811 { "http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY,
812 "", std::string::npos },
813 };
814
815 for (size_t i = 0; i < arraysize(test_data); ++i) {
816 TemplateURLData data;
817 data.SetURL(test_data[i].url);
818 TemplateURL url(data);
819 EXPECT_EQ(test_data[i].location,
820 url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
821 EXPECT_EQ(test_data[i].path,
822 url.url_ref().GetPath(search_terms_data_));
823 EXPECT_EQ(test_data[i].position_in_path,
824 url.url_ref().GetSearchTermPositionInPath(search_terms_data_));
825 }
826}
827
[email protected]d82443b2009-01-15 19:54:56828TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
829 static const struct {
[email protected]ddd231e2010-06-29 20:35:19830 const char* const base_url;
831 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56832 } data[] = {
[email protected]014010e2011-10-01 04:12:44833 { "http://google.com/", "http://google.com/complete/", },
834 { "http://www.google.com/", "http://www.google.com/complete/", },
835 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
836 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
837 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56838 };
839
viettrungluu37a447b2014-10-16 18:23:27840 for (size_t i = 0; i < arraysize(data); ++i)
[email protected]d82443b2009-01-15 19:54:56841 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
842}
843
[email protected]81c6ef62010-01-21 09:58:47844TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19845 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:54846 TemplateURLData data;
847 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28848 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47849 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:13850 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19851 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47852 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27853 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47854 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
855}
856
857TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:20858 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:54859 TemplateURLData data;
860 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28861 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47862 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:44863
864 // By default, TemplateURLRef should not consider itself prepopulated.
865 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:13866 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20867 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:44868 EXPECT_TRUE(replacements.empty());
869
870 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:20871 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:54872 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:28873 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:15874 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20875 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47876 EXPECT_TRUE(replacements.empty());
877}
878
879TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:28880 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:47881 TemplateURLRef::Replacements replacements;
882 bool valid = false;
[email protected]b37bdfe2012-03-16 20:53:27883 EXPECT_EQ(std::string(),
[email protected]93b29062013-07-12 03:09:09884 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47885 EXPECT_TRUE(replacements.empty());
886 EXPECT_TRUE(valid);
887}
888
889TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:54890 TemplateURLData data;
891 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:28892 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47893 TemplateURLRef::Replacements replacements;
894 bool valid = false;
[email protected]93b29062013-07-12 03:09:09895 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
896 &valid));
[email protected]81c6ef62010-01-21 09:58:47897 EXPECT_TRUE(replacements.empty());
898 EXPECT_FALSE(valid);
899}
900
901TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:54902 TemplateURLData data;
903 data.SetURL("{}");
[email protected]168d08722014-06-18 07:13:28904 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47905 TemplateURLRef::Replacements replacements;
906 bool valid = false;
[email protected]93b29062013-07-12 03:09:09907 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47908 EXPECT_TRUE(replacements.empty());
909 EXPECT_TRUE(valid);
910}
911
912TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:54913 TemplateURLData data;
914 data.SetURL("{}{{%s}}");
[email protected]168d08722014-06-18 07:13:28915 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47916 TemplateURLRef::Replacements replacements;
917 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19918 EXPECT_EQ("{}{}",
[email protected]93b29062013-07-12 03:09:09919 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
920 &valid));
[email protected]81c6ef62010-01-21 09:58:47921 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27922 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47923 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
924 EXPECT_TRUE(valid);
925}
926
927TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:54928 TemplateURLData data;
929 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:28930 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47931 TemplateURLRef::Replacements replacements;
932 bool valid = false;
[email protected]360ba052012-04-04 17:26:13933 EXPECT_EQ("{",
[email protected]93b29062013-07-12 03:09:09934 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
935 &valid));
[email protected]81c6ef62010-01-21 09:58:47936 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27937 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47938 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
939 EXPECT_TRUE(valid);
940}
[email protected]5b3078752012-10-09 18:54:16941
[email protected]fd6d8822012-12-08 06:56:11942TEST_F(TemplateURLTest, SearchClient) {
943 const std::string base_url_str("http://google.com/?");
944 const std::string terms_str("{searchTerms}&{google:searchClient}");
945 const std::string full_url_str = base_url_str + terms_str;
[email protected]0085863a2013-12-06 21:19:03946 const base::string16 terms(ASCIIToUTF16(terms_str));
[email protected]798baa8e2014-06-20 05:42:44947 search_terms_data_.set_google_base_url(base_url_str);
[email protected]fd6d8822012-12-08 06:56:11948
949 TemplateURLData data;
950 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:28951 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19952 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
953 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11954 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
955
956 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:19957 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
958 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11959 ASSERT_TRUE(result.is_valid());
960 EXPECT_EQ("http://google.com/?foobar&", result.spec());
961
962 // Check that the URL is correct when a client is present.
[email protected]798baa8e2014-06-20 05:42:44963 search_terms_data_.set_search_client("search_client");
[email protected]ce7ee5f2014-06-16 23:41:19964 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
965 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11966 ASSERT_TRUE(result_2.is_valid());
[email protected]798baa8e2014-06-20 05:42:44967 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
[email protected]fd6d8822012-12-08 06:56:11968}
[email protected]fd6d8822012-12-08 06:56:11969
[email protected]5b3078752012-10-09 18:54:16970TEST_F(TemplateURLTest, GetURLNoInstantURL) {
971 TemplateURLData data;
972 data.SetURL("http://google.com/?q={searchTerms}");
973 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
974 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
975 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:28976 TemplateURL url(data);
[email protected]5b3078752012-10-09 18:54:16977 ASSERT_EQ(3U, url.URLCount());
978 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
979 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
980 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
981}
982
983TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
984 TemplateURLData data;
985 data.SetURL("http://google.com/?q={searchTerms}");
986 data.instant_url = "http://google.com/instant#q={searchTerms}";
987 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
988 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:28989 TemplateURL url(data);
[email protected]5b3078752012-10-09 18:54:16990 ASSERT_EQ(3U, url.URLCount());
991 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
992 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
993 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
994}
995
996TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
997 TemplateURLData data;
998 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:28999 TemplateURL url(data);
[email protected]5b3078752012-10-09 18:54:161000 ASSERT_EQ(1U, url.URLCount());
1001 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0));
1002}
1003
1004TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
1005 TemplateURLData data;
1006 data.SetURL("http://google.com/?q={searchTerms}");
1007 data.instant_url = "http://google.com/instant#q={searchTerms}";
1008 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1009 data.alternate_urls.push_back(
1010 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281011 TemplateURL url(data);
[email protected]0085863a2013-12-06 21:19:031012 base::string16 result;
[email protected]5b3078752012-10-09 18:54:161013
1014 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191015 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191016 EXPECT_EQ(ASCIIToUTF16("something"), result);
1017
1018 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191019 GURL("http://google.com/?espv&q=something"),
1020 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191021 EXPECT_EQ(ASCIIToUTF16("something"), result);
1022
1023 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191024 GURL("http://google.com/?espv=1&q=something"),
1025 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191026 EXPECT_EQ(ASCIIToUTF16("something"), result);
1027
1028 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191029 GURL("http://google.com/?espv=0&q=something"),
1030 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191031 EXPECT_EQ(ASCIIToUTF16("something"), result);
1032
1033 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191034 GURL("http://google.com/alt/#q=something"),
1035 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191036 EXPECT_EQ(ASCIIToUTF16("something"), result);
1037
1038 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191039 GURL("http://google.com/alt/#espv&q=something"),
1040 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191041 EXPECT_EQ(ASCIIToUTF16("something"), result);
1042
1043 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191044 GURL("http://google.com/alt/#espv=1&q=something"),
1045 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191046 EXPECT_EQ(ASCIIToUTF16("something"), result);
1047
1048 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191049 GURL("http://google.com/alt/#espv=0&q=something"),
1050 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161051 EXPECT_EQ(ASCIIToUTF16("something"), result);
1052
1053 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191054 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371055 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161056
1057 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191058 GURL("http://google.ca/?q=something&q=anything"),
1059 search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371060 EXPECT_EQ(base::string16(), result);
[email protected]67d8b752013-04-03 17:33:271061
1062 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191063 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371064 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161065
[email protected]32e2d81b2012-10-16 19:03:251066 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191067 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
[email protected]32e2d81b2012-10-16 19:03:251068 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:161069
1070 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191071 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371072 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161073
1074 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191075 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161076 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1077
1078 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191079 GURL("http://google.com/alt/?q=123#q=456"),
1080 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161081 EXPECT_EQ(ASCIIToUTF16("456"), result);
1082
1083 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191084 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1085 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161086 EXPECT_EQ(ASCIIToUTF16("123"), result);
1087
1088 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191089 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1090 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161091 EXPECT_EQ(ASCIIToUTF16("789"), result);
1092
1093 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191094 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371095 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161096
1097 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191098 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371099 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161100
1101 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191102 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371103 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161104
1105 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191106 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371107 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161108
1109 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191110 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161111 EXPECT_EQ(ASCIIToUTF16("123"), result);
1112}
[email protected]4076ea62013-01-09 01:47:191113
alexmos294849f2015-03-14 00:55:061114TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
1115 TemplateURLData data;
1116 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1117 TemplateURL url(data);
1118 base::string16 result;
1119
1120 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1121 GURL("http://term-in-path.com/begin/something/end"),
1122 search_terms_data_, &result));
1123 EXPECT_EQ(ASCIIToUTF16("something"), result);
1124
1125 // "%20" must be converted to space.
1126 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1127 GURL("http://term-in-path.com/begin/a%20b%20c/end"),
1128 search_terms_data_, &result));
1129 EXPECT_EQ(ASCIIToUTF16("a b c"), result);
1130
1131 // Plus must not be converted to space.
1132 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1133 GURL("http://term-in-path.com/begin/1+2+3/end"),
1134 search_terms_data_, &result));
1135 EXPECT_EQ(ASCIIToUTF16("1+2+3"), result);
1136
1137 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1138 GURL("http://term-in-path.com/about"), search_terms_data_, &result));
1139 EXPECT_EQ(base::string16(), result);
1140
1141 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1142 GURL("http://term-in-path.com/begin"), search_terms_data_, &result));
1143 EXPECT_EQ(base::string16(), result);
1144
1145 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1146 GURL("http://term-in-path.com/end"), search_terms_data_, &result));
1147 EXPECT_EQ(base::string16(), result);
1148}
1149
vitbar27804d102015-04-15 10:54:411150// Checks that the ExtractSearchTermsFromURL function works correctly
1151// for urls containing non-latin characters in UTF8 encoding.
1152TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
1153 TemplateURLData data;
1154 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1155 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1156 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1157 TemplateURL url(data);
1158 base::string16 result;
1159
1160 // Russian text encoded with UTF-8.
1161 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551162 GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82"
1163 "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"),
vitbar27804d102015-04-15 10:54:411164 search_terms_data_, &result));
1165 EXPECT_EQ(
1166 base::WideToUTF16(
1167 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1168 L"\x043C\x0438\x0440!"),
1169 result);
1170
1171 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551172 GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2"
1173 "%D0%B0"),
vitbar27804d102015-04-15 10:54:411174 search_terms_data_, &result));
1175 EXPECT_EQ(
1176 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1177 result);
1178
1179 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
vitbar7060e23c2015-04-22 13:19:551180 GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20"
1181 "%D0%B8%20A"),
vitbar27804d102015-04-15 10:54:411182 search_terms_data_, &result));
1183 EXPECT_EQ(
1184 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1185 result);
1186}
1187
1188// Checks that the ExtractSearchTermsFromURL function works correctly
1189// for urls containing non-latin characters in non-UTF8 encoding.
1190TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) {
1191 TemplateURLData data;
1192 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1193 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1194 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1195 data.input_encodings.push_back("windows-1251");
1196 TemplateURL url(data);
1197 base::string16 result;
1198
1199 // Russian text encoded with Windows-1251.
1200 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1201 GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+"
1202 "%EC%E8%F0!"),
1203 search_terms_data_, &result));
1204 EXPECT_EQ(
1205 base::WideToUTF16(
1206 L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, "
1207 L"\x043C\x0438\x0440!"),
1208 result);
1209
1210 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1211 GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"),
1212 search_terms_data_, &result));
1213 EXPECT_EQ(
1214 base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"),
1215 result);
1216
1217 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1218 GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"),
1219 search_terms_data_, &result));
1220 EXPECT_EQ(
1221 base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"),
1222 result);
1223}
1224
[email protected]4076ea62013-01-09 01:47:191225TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
1226 TemplateURLData data;
1227 data.SetURL("http://google.com/?q={searchTerms}");
1228 data.instant_url = "http://google.com/instant#q={searchTerms}";
1229 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1230 data.alternate_urls.push_back(
1231 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1232 data.search_terms_replacement_key = "espv";
[email protected]168d08722014-06-18 07:13:281233 TemplateURL url(data);
[email protected]4076ea62013-01-09 01:47:191234
1235 // Test with instant enabled required.
1236 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1237 GURL("http://google.com/")));
1238
1239 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1240 GURL("http://google.com/?espv")));
1241
1242 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1243 GURL("http://google.com/#espv")));
1244
1245 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1246 GURL("http://google.com/?q=something")));
1247
1248 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1249 GURL("http://google.com/?q=something&espv")));
1250
1251 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1252 GURL("http://google.com/?q=something&espv=1")));
1253
1254 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1255 GURL("http://google.com/?q=something&espv=0")));
1256
1257 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1258 GURL("http://google.com/?espv&q=something")));
1259
1260 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1261 GURL("http://google.com/?espv=1&q=something")));
1262
1263 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1264 GURL("http://google.com/?espv=0&q=something")));
1265
1266 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1267 GURL("http://google.com/alt/#q=something")));
1268
1269 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1270 GURL("http://google.com/alt/#q=something&espv")));
1271
1272 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1273 GURL("http://google.com/alt/#q=something&espv=1")));
1274
1275 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1276 GURL("http://google.com/alt/#q=something&espv=0")));
1277
1278 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1279 GURL("http://google.com/alt/#espv&q=something")));
1280
1281 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1282 GURL("http://google.com/alt/#espv=1&q=something")));
1283
1284 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1285 GURL("http://google.com/alt/#espv=0&q=something")));
1286
1287 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1288 GURL("http://google.com/?espv#q=something")));
1289
1290 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1291 GURL("http://google.com/?espv=1#q=something")));
1292
1293 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1294 GURL("http://google.com/?q=something#espv")));
1295
1296 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1297 GURL("http://google.com/?q=something#espv=1")));
1298
1299 // This does not ensure the domain matches.
1300 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1301 GURL("http://bing.com/?espv")));
1302
1303 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1304 GURL("http://bing.com/#espv")));
1305}
[email protected]f62e30f52013-03-23 03:45:151306
1307TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1308 TemplateURLData data;
1309 data.SetURL("http://google.com/?q={searchTerms}");
1310 data.instant_url = "http://google.com/instant#q={searchTerms}";
1311 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1312 data.alternate_urls.push_back(
1313 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281314 TemplateURL url(data);
[email protected]f62e30f52013-03-23 03:45:151315 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1316 GURL result;
1317
1318 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191319 GURL("http://google.com/?q=something"), search_terms,
1320 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101321 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151322
1323 result = GURL("http://should.not.change.com");
1324 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191325 GURL("http://google.ca/?q=something"), search_terms,
1326 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151327 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1328
1329 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191330 GURL("http://google.com/foo/?q=foo"), search_terms,
1331 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151332
1333 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191334 GURL("https://google.com/?q=foo"), search_terms,
1335 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101336 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151337
1338 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191339 GURL("http://google.com:8080/?q=foo"), search_terms,
1340 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151341
1342 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191343 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1344 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101345 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result);
[email protected]f62e30f52013-03-23 03:45:151346
1347 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1348 // template_url.cc for details.
1349 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191350 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1351 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101352 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151353
1354 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1355 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191356 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101357 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"),
[email protected]f62e30f52013-03-23 03:45:151358 result);
1359
1360 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1361 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191362 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151363 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
vitbarf298455d2015-04-21 12:58:101364 "#j=abc&q=Bob+Morane&h=def9"), result);
[email protected]f62e30f52013-03-23 03:45:151365
1366 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191367 GURL("http://google.com/alt/?q="), search_terms,
1368 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151369
1370 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191371 GURL("http://google.com/alt/?#q="), search_terms,
1372 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151373
1374 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191375 GURL("http://google.com/alt/?q=#q="), search_terms,
1376 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151377
1378 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191379 GURL("http://google.com/alt/?q=123#q="), search_terms,
1380 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151381
1382 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191383 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1384 search_terms_data_, &result));
vitbarf298455d2015-04-21 12:58:101385 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result);
[email protected]f62e30f52013-03-23 03:45:151386}
[email protected]56fa29592013-07-02 20:25:531387
alexmos294849f2015-03-14 00:55:061388TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) {
1389 TemplateURLData data;
1390 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end");
1391 TemplateURL url(data);
1392 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1393 GURL result;
1394
1395 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1396 GURL("http://term-in-path.com/begin/something/end"), search_terms,
1397 search_terms_data_, &result));
1398 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1399
1400 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1401 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms,
1402 search_terms_data_, &result));
1403 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result);
1404
1405 result = GURL("http://should.not.change.com");
1406 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1407 GURL("http://term-in-path.com/about"), search_terms,
1408 search_terms_data_, &result));
1409 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1410}
1411
vitbarf298455d2015-04-21 12:58:101412// Checks that the ReplaceSearchTermsInURL function works correctly
1413// for search terms containing non-latin characters for a search engine
1414// using UTF-8 input encoding.
1415TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) {
1416 TemplateURLData data;
1417 data.SetURL("http://utf-8.ru/?q={searchTerms}");
1418 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}");
1419 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}");
1420 TemplateURL url(data);
1421
1422 // Russian text which will be encoded with UTF-8.
1423 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1424 L"\x0442\x0435\x043A\x0441\x0442"));
1425 GURL result;
1426
1427 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1428 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_,
1429 &result));
1430 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1431 result);
1432
1433 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1434 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_,
1435 &result));
1436 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1437 result);
1438
1439 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1440 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_,
1441 &result));
1442 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"),
1443 result);
1444}
1445
1446// Checks that the ReplaceSearchTermsInURL function works correctly
1447// for search terms containing non-latin characters for a search engine
1448// using non UTF-8 input encoding.
1449TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) {
1450 TemplateURLData data;
1451 data.SetURL("http://windows-1251.ru/?q={searchTerms}");
1452 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}");
1453 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}");
1454 data.input_encodings.push_back("windows-1251");
1455 TemplateURL url(data);
1456
1457 // Russian text which will be encoded with Windows-1251.
1458 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16(
1459 L"\x0442\x0435\x043A\x0441\x0442"));
1460 GURL result;
1461
1462 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1463 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_,
1464 &result));
1465 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"),
1466 result);
1467
1468 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1469 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_,
1470 &result));
1471 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"),
1472 result);
1473
1474 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1475 GURL("http://windows-1251.ru/path/a%20b"), search_terms,
1476 search_terms_data_, &result));
1477 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"),
1478 result);
1479}
1480
[email protected]621ade062013-10-28 06:27:431481// Test the |suggest_query_params| field of SearchTermsArgs.
1482TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431483 TemplateURLData data;
1484 // Pick a URL with replacements before, during, and after the query, to ensure
1485 // we don't goof up any of them.
1486 data.SetURL("{google:baseURL}search?q={searchTerms}"
1487 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281488 TemplateURL url(data);
[email protected]621ade062013-10-28 06:27:431489
1490 // Baseline: no |suggest_query_params| field.
1491 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1492 search_terms.original_query = ASCIIToUTF16("def");
1493 search_terms.accepted_suggestion = 0;
1494 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191495 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431496
1497 // Set the suggest_query_params.
1498 search_terms.suggest_query_params = "pq=xyz";
1499 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191500 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431501
1502 // Add extra_query_params in the mix, and ensure it works.
1503 search_terms.append_extra_query_params = true;
avi1772c1a2014-12-22 22:42:331504 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]621ade062013-10-28 06:27:431505 switches::kExtraSearchQueryParams, "a=b");
1506 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191507 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431508}
1509
[email protected]56fa29592013-07-02 20:25:531510// Test the |append_extra_query_params| field of SearchTermsArgs.
1511TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531512 TemplateURLData data;
1513 // Pick a URL with replacements before, during, and after the query, to ensure
1514 // we don't goof up any of them.
1515 data.SetURL("{google:baseURL}search?q={searchTerms}"
1516 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281517 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531518
1519 // Baseline: no command-line args, no |append_extra_query_params| flag.
1520 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1521 search_terms.original_query = ASCIIToUTF16("def");
1522 search_terms.accepted_suggestion = 0;
1523 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191524 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531525
1526 // Set the flag. Since there are no command-line args, this should have no
1527 // effect.
1528 search_terms.append_extra_query_params = true;
1529 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191530 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531531
1532 // Now append the command-line arg. This should be inserted into the query.
avi1772c1a2014-12-22 22:42:331533 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]56fa29592013-07-02 20:25:531534 switches::kExtraSearchQueryParams, "a=b");
1535 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191536 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531537
1538 // Turn off the flag. Now the command-line arg should be ignored again.
1539 search_terms.append_extra_query_params = false;
1540 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191541 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531542}
[email protected]d5015ca2013-08-08 22:04:181543
1544// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171545TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181546 TemplateURLData data;
1547 data.input_encodings.push_back("UTF-8");
1548 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281549 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191550 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1551 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]d5015ca2013-08-08 22:04:181552 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1553
[email protected]ce7ee5f2014-06-16 23:41:191554 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1555 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181556 EXPECT_EQ("http://www.google.com/?q=foo", result);
1557
[email protected]332d17d22014-06-20 16:56:031558 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191559 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1560 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181561 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1562
1563 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031564 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191565 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1566 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181567 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1568}
[email protected]2328ee22013-08-08 23:00:191569
1570// Test the IsSearchResults function.
1571TEST_F(TemplateURLTest, IsSearchResults) {
1572 TemplateURLData data;
1573 data.SetURL("http://bar/search?q={searchTerms}");
1574 data.instant_url = "http://bar/instant#q={searchTerms}";
[email protected]2767c0fd2013-08-16 17:44:161575 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191576 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1577 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1578 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1579 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281580 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191581
1582 const struct {
1583 const char* const url;
1584 bool result;
1585 } url_data[] = {
1586 { "http://bar/search?q=foo&oq=foo", true, },
1587 { "http://bar/?q=foo&oq=foo", true, },
1588 { "http://bar/#output=search&q=foo&oq=foo", true, },
1589 { "http://bar/webhp#q=foo&oq=foo", true, },
1590 { "http://bar/#q=foo&oq=foo", true, },
1591 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1592 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1593 { "http://bar/", false, },
1594 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161595 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191596 };
1597
viettrungluu37a447b2014-10-16 18:23:271598 for (size_t i = 0; i < arraysize(url_data); ++i) {
[email protected]2328ee22013-08-08 23:00:191599 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191600 search_provider.IsSearchURL(GURL(url_data[i].url),
1601 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191602 }
1603}
[email protected]a048de8a2013-10-02 18:30:061604
1605TEST_F(TemplateURLTest, ReflectsBookmarkBarPinned) {
1606 TemplateURLData data;
1607 data.input_encodings.push_back("UTF-8");
1608 data.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281609 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191610 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1611 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]a048de8a2013-10-02 18:30:061612 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1613
1614 // Do not add the param when InstantExtended is suppressed on SRPs.
[email protected]5c3c6e482014-06-23 09:47:181615 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(false);
[email protected]ce7ee5f2014-06-16 23:41:191616 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1617 search_terms_data_);
[email protected]a048de8a2013-10-02 18:30:061618 EXPECT_EQ("http://www.google.com/?q=foo", result);
1619
1620 // Add the param when InstantExtended is not suppressed on SRPs.
[email protected]5c3c6e482014-06-23 09:47:181621 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true);
[email protected]a048de8a2013-10-02 18:30:061622 search_terms_args.bookmark_bar_pinned = false;
[email protected]ce7ee5f2014-06-16 23:41:191623 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1624 search_terms_data_);
[email protected]a048de8a2013-10-02 18:30:061625 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result);
1626
[email protected]5c3c6e482014-06-23 09:47:181627 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true);
[email protected]a048de8a2013-10-02 18:30:061628 search_terms_args.bookmark_bar_pinned = true;
[email protected]ce7ee5f2014-06-16 23:41:191629 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1630 search_terms_data_);
[email protected]a048de8a2013-10-02 18:30:061631 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result);
1632}
[email protected]9d70de12014-05-10 02:15:311633
jdonnelly41c5b46a2015-07-10 21:24:381634TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) {
[email protected]9d70de12014-05-10 02:15:311635 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441636 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311637 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1638
[email protected]168d08722014-06-18 07:13:281639 TemplateURL url(data);
[email protected]9d70de12014-05-10 02:15:311640 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191641 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1642 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311643 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1644}
[email protected]20184242014-05-14 02:57:421645
1646TEST_F(TemplateURLTest, SessionToken) {
1647 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441648 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421649 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1650
[email protected]168d08722014-06-18 07:13:281651 TemplateURL url(data);
[email protected]20184242014-05-14 02:57:421652 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1653 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191654 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1655 search_terms_data_);
[email protected]20184242014-05-14 02:57:421656 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1657
[email protected]168d08722014-06-18 07:13:281658 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421659 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191660 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1661 search_terms_data_);
[email protected]20184242014-05-14 02:57:421662 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1663}
[email protected]448b17f52014-06-13 01:08:031664
1665TEST_F(TemplateURLTest, ContextualSearchParameters) {
1666 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441667 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031668 data.SetURL("http://bar/_/contextualsearch?"
1669 "{google:contextualSearchVersion}"
1670 "{google:contextualSearchContextData}");
1671
[email protected]168d08722014-06-18 07:13:281672 TemplateURL url(data);
[email protected]448b17f52014-06-13 01:08:031673 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191674 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1675 search_terms_data_);
[email protected]52e02aa2014-08-06 05:36:091676 EXPECT_EQ("http://bar/_/contextualsearch?ctxsl_resolve=1", result);
[email protected]448b17f52014-06-13 01:08:031677
1678 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
1679 1, 6, 11, "allen", "woody+allen+movies", "www.wikipedia.org",
[email protected]52e02aa2014-08-06 05:36:091680 "utf-8", true);
[email protected]448b17f52014-06-13 01:08:031681 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191682 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1683 search_terms_data_);
[email protected]448b17f52014-06-13 01:08:031684 EXPECT_EQ("http://bar/_/contextualsearch?"
1685 "ctxs=1&"
1686 "ctxs_start=6&"
1687 "ctxs_end=11&"
1688 "q=allen&"
1689 "ctxs_content=woody+allen+movies&"
[email protected]52e02aa2014-08-06 05:36:091690 "ctxsl_url=www.wikipedia.org&"
1691 "ctxs_encoding=utf-8&"
1692 "ctxsl_resolve=1",
1693 result);
[email protected]448b17f52014-06-13 01:08:031694}
[email protected]44ccc9f2014-06-20 17:36:211695
1696TEST_F(TemplateURLTest, GenerateKeyword) {
alshabaline6212ac2015-07-23 08:03:001697 std::string accept_languages = "en,ru";
[email protected]44ccc9f2014-06-20 17:36:211698 ASSERT_EQ(ASCIIToUTF16("foo"),
alshabaline6212ac2015-07-23 08:03:001699 TemplateURL::GenerateKeyword(GURL("http://foo"), accept_languages));
[email protected]44ccc9f2014-06-20 17:36:211700 // www. should be stripped.
alshabaline6212ac2015-07-23 08:03:001701 ASSERT_EQ(ASCIIToUTF16("foo"), TemplateURL::GenerateKeyword(
1702 GURL("http://www.foo"), accept_languages));
[email protected]44ccc9f2014-06-20 17:36:211703 // Make sure we don't get a trailing '/'.
alshabaline6212ac2015-07-23 08:03:001704 ASSERT_EQ(ASCIIToUTF16("blah"), TemplateURL::GenerateKeyword(
1705 GURL("http://blah/"), accept_languages));
[email protected]44ccc9f2014-06-20 17:36:211706 // Don't generate the empty string.
alshabaline6212ac2015-07-23 08:03:001707 ASSERT_EQ(ASCIIToUTF16("www"), TemplateURL::GenerateKeyword(
1708 GURL("http://www."), accept_languages));
1709 ASSERT_EQ(
1710 base::UTF8ToUTF16("\xd0\xb0\xd0\xb1\xd0\xb2"),
1711 TemplateURL::GenerateKeyword(GURL("http://xn--80acd"), accept_languages));
[email protected]44ccc9f2014-06-20 17:36:211712}
1713
1714TEST_F(TemplateURLTest, GenerateSearchURL) {
1715 struct GenerateSearchURLCase {
1716 const char* test_name;
1717 const char* url;
1718 const char* expected;
1719 } generate_url_cases[] = {
1720 { "invalid URL", "foo{searchTerms}", "" },
1721 { "URL with no replacements", "http://foo/", "http://foo/" },
1722 { "basic functionality", "http://foo/{searchTerms}",
1723 "http://foo/blah.blah.blah.blah.blah" }
1724 };
1725
viettrungluu37a447b2014-10-16 18:23:271726 for (size_t i = 0; i < arraysize(generate_url_cases); ++i) {
[email protected]44ccc9f2014-06-20 17:36:211727 TemplateURLData data;
1728 data.SetURL(generate_url_cases[i].url);
1729 TemplateURL t_url(data);
1730 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1731 generate_url_cases[i].expected)
1732 << generate_url_cases[i].test_name << " failed.";
1733 }
1734}
[email protected]9e9130ce2014-06-23 23:20:511735
1736TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1737 TemplateURLData data;
1738 search_terms_data_.set_google_base_url("http://bar/");
1739 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1740
1741 TemplateURL url(data);
1742 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1743 search_terms_args.prefetch_query = "full query text";
1744 search_terms_args.prefetch_query_type = "2338";
1745 std::string result =
1746 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1747 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1748 result);
1749
1750 TemplateURL url2(data);
1751 search_terms_args.prefetch_query.clear();
1752 search_terms_args.prefetch_query_type.clear();
1753 result =
1754 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1755 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1756}