blob: 4d71d0fa897f057fb94303f3efea04dd7c08c9e2 [file] [log] [blame]
[email protected]34e24852012-01-31 18:43:581// Copyright (c) 2012 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]d54e03a52009-01-16 00:31:0410#include "chrome/browser/search_engines/template_url.h"
[email protected]332d17d22014-06-20 16:56:0311#include "components/metrics/proto/omnibox_event.pb.h"
[email protected]420472b22014-06-10 13:34:4312#include "components/metrics/proto/omnibox_input_type.pb.h"
[email protected]5c3c6e482014-06-23 09:47:1813#include "components/search_engines/search_engines_switches.h"
[email protected]798baa8e2014-06-20 05:42:4414#include "components/search_engines/search_terms_data.h"
[email protected]d82443b2009-01-15 19:54:5615#include "testing/gtest/include/gtest/gtest.h"
16
[email protected]f911df52013-12-24 23:24:2317using base::ASCIIToUTF16;
18
[email protected]b37bdfe2012-03-16 20:53:2719// TestSearchTermsData --------------------------------------------------------
20
[email protected]375bd7312010-08-30 22:18:1321// Simple implementation of SearchTermsData.
22class TestSearchTermsData : public SearchTermsData {
23 public:
[email protected]b37bdfe2012-03-16 20:53:2724 explicit TestSearchTermsData(const std::string& google_base_url);
[email protected]375bd7312010-08-30 22:18:1325
[email protected]b37bdfe2012-03-16 20:53:2726 virtual std::string GoogleBaseURLValue() const OVERRIDE;
[email protected]798baa8e2014-06-20 05:42:4427 virtual base::string16 GetRlzParameterValue(
28 bool from_app_list) const OVERRIDE;
29 virtual std::string GetSearchClient() const OVERRIDE;
[email protected]1020fead2014-06-20 13:40:2830 virtual std::string GoogleImageSearchSource() const OVERRIDE;
[email protected]5c3c6e482014-06-23 09:47:1831 virtual bool EnableAnswersInSuggest() const OVERRIDE;
32 virtual bool IsShowingSearchTermsOnSearchResultsPages() const OVERRIDE;
[email protected]798baa8e2014-06-20 05:42:4433
34 void set_google_base_url(const std::string& google_base_url) {
35 google_base_url_ = google_base_url;
36 }
37 void set_search_client(const std::string& search_client) {
38 search_client_ = search_client;
39 }
[email protected]5c3c6e482014-06-23 09:47:1840 void set_enable_answers_in_suggest(bool enable_answers_in_suggest) {
41 enable_answers_in_suggest_ = enable_answers_in_suggest;
42 }
43 void set_is_showing_search_terms_on_search_results_pages(bool value) {
44 is_showing_search_terms_on_search_results_pages_ = value;
45 }
[email protected]375bd7312010-08-30 22:18:1346
47 private:
48 std::string google_base_url_;
[email protected]798baa8e2014-06-20 05:42:4449 std::string search_client_;
[email protected]5c3c6e482014-06-23 09:47:1850 bool enable_answers_in_suggest_;
51 bool is_showing_search_terms_on_search_results_pages_;
[email protected]375bd7312010-08-30 22:18:1352
53 DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData);
54};
55
[email protected]b37bdfe2012-03-16 20:53:2756TestSearchTermsData::TestSearchTermsData(const std::string& google_base_url)
[email protected]5c3c6e482014-06-23 09:47:1857 : google_base_url_(google_base_url),
58 enable_answers_in_suggest_(false),
59 is_showing_search_terms_on_search_results_pages_(false) {
[email protected]b37bdfe2012-03-16 20:53:2760}
61
62std::string TestSearchTermsData::GoogleBaseURLValue() const {
63 return google_base_url_;
64}
65
[email protected]798baa8e2014-06-20 05:42:4466base::string16 TestSearchTermsData::GetRlzParameterValue(
67 bool from_app_list) const {
68 return ASCIIToUTF16(
69 from_app_list ? "rlz_parameter_from_app_list" : "rlz_parameter");
70}
71
72std::string TestSearchTermsData::GetSearchClient() const {
73 return search_client_;
74}
75
[email protected]1020fead2014-06-20 13:40:2876std::string TestSearchTermsData::GoogleImageSearchSource() const {
77 return "google_image_search_source";
78}
79
[email protected]5c3c6e482014-06-23 09:47:1880bool TestSearchTermsData::EnableAnswersInSuggest() const {
81 return enable_answers_in_suggest_;
82}
83
84bool TestSearchTermsData::IsShowingSearchTermsOnSearchResultsPages() const {
85 return is_showing_search_terms_on_search_results_pages_;
86}
87
[email protected]b37bdfe2012-03-16 20:53:2788// TemplateURLTest ------------------------------------------------------------
89
[email protected]583844c2011-08-27 00:38:3590class TemplateURLTest : public testing::Test {
[email protected]d82443b2009-01-15 19:54:5691 public:
[email protected]798baa8e2014-06-20 05:42:4492 TemplateURLTest() : search_terms_data_("http://www.google.com/") {}
[email protected]b37bdfe2012-03-16 20:53:2793 void CheckSuggestBaseURL(const std::string& base_url,
94 const std::string& base_suggest_url) const;
[email protected]ce7ee5f2014-06-16 23:41:1995
[email protected]798baa8e2014-06-20 05:42:4496 TestSearchTermsData search_terms_data_;
[email protected]d82443b2009-01-15 19:54:5697};
98
[email protected]b37bdfe2012-03-16 20:53:2799void TemplateURLTest::CheckSuggestBaseURL(
100 const std::string& base_url,
101 const std::string& base_suggest_url) const {
102 TestSearchTermsData search_terms_data(base_url);
103 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue());
104}
105
106
107// Actual tests ---------------------------------------------------------------
108
[email protected]d82443b2009-01-15 19:54:56109TEST_F(TemplateURLTest, Defaults) {
[email protected]573889f22012-04-07 01:31:54110 TemplateURLData data;
111 EXPECT_FALSE(data.show_in_default_list);
112 EXPECT_FALSE(data.safe_for_autoreplace);
113 EXPECT_EQ(0, data.prepopulate_id);
[email protected]d82443b2009-01-15 19:54:56114}
115
116TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]573889f22012-04-07 01:31:54117 TemplateURLData data;
118 data.SetURL("{searchTerms}");
[email protected]168d08722014-06-18 07:13:28119 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19120 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56121}
122
123TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:19124 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:19125 const char* url;
[email protected]0085863a2013-12-06 21:19:03126 const base::string16 terms;
[email protected]34e24852012-01-31 18:43:58127 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:19128 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:30129 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:16130 "http://foosea%20rch/bar" },
[email protected]400b133f2011-01-19 18:32:30131 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:16132 "http://foosea%20rch/bar?boo=abc" },
[email protected]400b133f2011-01-19 18:32:30133 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:16134 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]400b133f2011-01-19 18:32:30135 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
[email protected]c31a979c2012-05-31 23:57:16136 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:19137 };
138 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
139 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54140 TemplateURLData data;
141 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:28142 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19143 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
144 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04145 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19146 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data_));
[email protected]c31a979c2012-05-31 23:57:16147 ASSERT_TRUE(result.is_valid());
148 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:19149 }
[email protected]d82443b2009-01-15 19:54:56150}
151
152TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:54153 TemplateURLData data;
154 data.SetURL("http://foo{searchTerms}{count?}");
[email protected]168d08722014-06-18 07:13:28155 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19156 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
157 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04158 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19159 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56160 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27161 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:56162}
163
164TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:54165 TemplateURLData data;
166 data.SetURL("http://foo{searchTerms}{count}");
[email protected]168d08722014-06-18 07:13:28167 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19168 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
169 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04170 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19171 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56172 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27173 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56174}
175
176TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54177 TemplateURLData data;
178 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]168d08722014-06-18 07:13:28179 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19180 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
181 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04182 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19183 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56184 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27185 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56186}
187
188TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54189 TemplateURLData data;
190 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[email protected]168d08722014-06-18 07:13:28191 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19192 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
193 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04194 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19195 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56196 ASSERT_TRUE(result.is_valid());
[email protected]405aae22012-03-29 20:36:13197 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56198}
199
200TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54201 TemplateURLData data;
202 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[email protected]168d08722014-06-18 07:13:28203 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19204 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
205 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04206 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19207 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56208 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27209 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56210}
211
[email protected]93b29062013-07-12 03:09:09212TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
213 const char kInvalidPostParamsString[] =
214 "unknown_template={UnknownTemplate},bad_value=bad{value},"
215 "{google:sbiSource}";
216 // List all accpectable parameter format in valid_post_params_string. it is
217 // expected like: "name0=,name1=value1,name2={template1}"
218 const char kValidPostParamsString[] =
219 "image_content={google:imageThumbnail},image_url={google:imageURL},"
220 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
[email protected]2f3bc6512013-08-28 03:56:27221 "constant_param=constant,width={google:imageOriginalWidth}";
[email protected]93b29062013-07-12 03:09:09222 const char KImageSearchURL[] = "http://foo.com/sbi";
223
224 TemplateURLData data;
225 data.image_url = KImageSearchURL;
226
227 // Try to parse invalid post parameters.
228 data.image_url_post_params = kInvalidPostParamsString;
[email protected]168d08722014-06-18 07:13:28229 TemplateURL url_bad(data);
[email protected]ce7ee5f2014-06-16 23:41:19230 ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09231 const TemplateURLRef::PostParams& bad_post_params =
232 url_bad.image_url_ref().post_params_;
233 ASSERT_EQ(2U, bad_post_params.size());
234 EXPECT_EQ("unknown_template", bad_post_params[0].first);
235 EXPECT_EQ("{UnknownTemplate}", bad_post_params[0].second);
236 EXPECT_EQ("bad_value", bad_post_params[1].first);
237 EXPECT_EQ("bad{value}", bad_post_params[1].second);
238
239 // Try to parse valid post parameters.
240 data.image_url_post_params = kValidPostParamsString;
[email protected]168d08722014-06-18 07:13:28241 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19242 ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_));
243 ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09244
245 // Check term replacement.
246 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
247 search_args.image_thumbnail_content = "dummy-image-thumbnail";
248 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
[email protected]2f3bc6512013-08-28 03:56:27249 search_args.image_original_size = gfx::Size(10, 10);
[email protected]93b29062013-07-12 03:09:09250 // Replacement operation with no post_data buffer should still return
251 // the parsed URL.
[email protected]ce7ee5f2014-06-16 23:41:19252 TestSearchTermsData search_terms_data("http://X");
253 GURL result(url.image_url_ref().ReplaceSearchTerms(
254 search_args, search_terms_data));
[email protected]93b29062013-07-12 03:09:09255 ASSERT_TRUE(result.is_valid());
256 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48257 TemplateURLRef::PostContent post_content;
[email protected]ce7ee5f2014-06-16 23:41:19258 result = GURL(url.image_url_ref().ReplaceSearchTerms(
[email protected]7c2bcc42013-08-01 04:03:48259 search_args, search_terms_data, &post_content));
[email protected]93b29062013-07-12 03:09:09260 ASSERT_TRUE(result.is_valid());
261 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48262 ASSERT_FALSE(post_content.first.empty());
263 ASSERT_FALSE(post_content.second.empty());
[email protected]93b29062013-07-12 03:09:09264
265 // Check parsed result of post parameters.
266 const TemplateURLRef::Replacements& replacements =
267 url.image_url_ref().replacements_;
268 const TemplateURLRef::PostParams& post_params =
269 url.image_url_ref().post_params_;
[email protected]2f3bc6512013-08-28 03:56:27270 EXPECT_EQ(7U, post_params.size());
[email protected]93b29062013-07-12 03:09:09271 for (TemplateURLRef::PostParams::const_iterator i = post_params.begin();
272 i != post_params.end(); ++i) {
273 TemplateURLRef::Replacements::const_iterator j = replacements.begin();
274 for (; j != replacements.end(); ++j) {
275 if (j->is_post_param && j->index ==
276 static_cast<size_t>(i - post_params.begin())) {
277 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27278 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
279 EXPECT_EQ("width", i->first);
280 EXPECT_EQ(
281 base::IntToString(search_args.image_original_size.width()),
282 i->second);
283 break;
[email protected]1020fead2014-06-20 13:40:28284 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE:
285 EXPECT_EQ("sbisrc", i->first);
286 EXPECT_EQ(search_terms_data.GoogleImageSearchSource(), i->second);
287 break;
[email protected]93b29062013-07-12 03:09:09288 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
289 EXPECT_EQ("image_content", i->first);
290 EXPECT_EQ(search_args.image_thumbnail_content, i->second);
291 break;
292 case TemplateURLRef::GOOGLE_IMAGE_URL:
293 EXPECT_EQ("image_url", i->first);
294 EXPECT_EQ(search_args.image_url.spec(), i->second);
295 break;
296 case TemplateURLRef::LANGUAGE:
297 EXPECT_EQ("language", i->first);
298 EXPECT_EQ("en", i->second);
299 break;
300 default:
301 ADD_FAILURE(); // Should never go here.
302 }
303 break;
304 }
305 }
306 if (j != replacements.end())
307 continue;
308 if (i->first == "empty_param") {
309 EXPECT_TRUE(i->second.empty());
310 } else if (i->first == "sbisrc") {
[email protected]7c2bcc42013-08-01 04:03:48311 EXPECT_FALSE(i->second.empty());
[email protected]93b29062013-07-12 03:09:09312 } else {
313 EXPECT_EQ("constant_param", i->first);
314 EXPECT_EQ("constant", i->second);
315 }
316 }
317}
318
[email protected]d88cb202011-08-17 20:03:01319// Test that setting the prepopulate ID from TemplateURL causes the stored
320// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44321TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54322 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20323 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]168d08722014-06-18 07:13:28324 TemplateURL url(data);
[email protected]1a257262011-06-28 22:15:44325 TemplateURLRef::Replacements replacements;
326 bool valid = false;
[email protected]243abf32012-05-15 18:28:20327 EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL(
[email protected]93b29062013-07-12 03:09:09328 "http://foo{fhqwhgads}bar", &replacements, NULL, &valid));
[email protected]1a257262011-06-28 22:15:44329 EXPECT_TRUE(replacements.empty());
330 EXPECT_TRUE(valid);
331
[email protected]573889f22012-04-07 01:31:54332 data.prepopulate_id = 123;
[email protected]168d08722014-06-18 07:13:28333 TemplateURL url2(data);
[email protected]243abf32012-05-15 18:28:20334 EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar",
[email protected]93b29062013-07-12 03:09:09335 &replacements, NULL,
336 &valid));
[email protected]1a257262011-06-28 22:15:44337 EXPECT_TRUE(replacements.empty());
338 EXPECT_TRUE(valid);
339}
340
[email protected]d82443b2009-01-15 19:54:56341TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54342 TemplateURLData data;
343 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]168d08722014-06-18 07:13:28344 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19345 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
346 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04347 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19348 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56349 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27350 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56351}
352
353TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54354 TemplateURLData data;
355 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]168d08722014-06-18 07:13:28356 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19357 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
358 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04359 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19360 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56361 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27362 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56363}
364
[email protected]375bd7312010-08-30 22:18:13365TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
366 struct SearchTermsCase {
367 const char* url;
[email protected]0085863a2013-12-06 21:19:03368 const base::string16 terms;
[email protected]375bd7312010-08-30 22:18:13369 const char* output;
370 } search_term_cases[] = {
[email protected]0085863a2013-12-06 21:19:03371 { "{google:baseURL}{language}{searchTerms}", base::string16(),
[email protected]b37bdfe2012-03-16 20:53:27372 "http://example.com/e/en" },
[email protected]0085863a2013-12-06 21:19:03373 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
[email protected]014010e2011-10-01 04:12:44374 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13375 };
376
377 TestSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54378 TemplateURLData data;
[email protected]375bd7312010-08-30 22:18:13379 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
380 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54381 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:28382 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19383 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data));
384 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data));
385 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]93b29062013-07-12 03:09:09386 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL));
[email protected]375bd7312010-08-30 22:18:13387 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27388 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13389 }
390}
391
[email protected]d82443b2009-01-15 19:54:56392TEST_F(TemplateURLTest, URLRefTermToWide) {
393 struct ToWideCase {
394 const char* encoded_search_term;
[email protected]0085863a2013-12-06 21:19:03395 const base::string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56396 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30397 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56398 // Test some big-5 input.
[email protected]f911df52013-12-24 23:24:23399 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56400 // Test some UTF-8 input. We should fall back to this when the encoding
401 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
402 // Big-5 trailing byte.
[email protected]f911df52013-12-24 23:24:23403 {"%e4%bd%a05%e5%a5%bd+to+you",
404 base::WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56405 // Undecodable input should stay escaped.
[email protected]f911df52013-12-24 23:24:23406 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44407 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30408 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44409 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30410 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56411 };
412
[email protected]d82443b2009-01-15 19:54:56413 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54414 TemplateURLData data;
415 data.SetURL("http://foo?q={searchTerms}");
416 data.input_encodings.push_back("big-5");
[email protected]168d08722014-06-18 07:13:28417 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19418 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
419 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]f63ae312009-02-04 17:58:46420 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07421 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13422 url.url_ref().SearchTermToString16(
423 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56424 }
425}
426
[email protected]d82443b2009-01-15 19:54:56427TEST_F(TemplateURLTest, DisplayURLToURLRef) {
428 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19429 const std::string url;
[email protected]0085863a2013-12-06 21:19:03430 const base::string16 expected_result;
[email protected]b37bdfe2012-03-16 20:53:27431 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19432 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30433 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19434 { "http://X",
[email protected]400b133f2011-01-19 18:32:30435 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19436 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30437 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19438 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30439 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56440 };
[email protected]573889f22012-04-07 01:31:54441 TemplateURLData data;
[email protected]b37bdfe2012-03-16 20:53:27442 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54443 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28444 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19445 EXPECT_EQ(test_data[i].expected_result,
446 url.url_ref().DisplayURL(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27447 EXPECT_EQ(test_data[i].url,
[email protected]ce7ee5f2014-06-16 23:41:19448 TemplateURLRef::DisplayURLToURLRef(
449 url.url_ref().DisplayURL(search_terms_data_)));
[email protected]d82443b2009-01-15 19:54:56450 }
451}
452
453TEST_F(TemplateURLTest, ReplaceSearchTerms) {
454 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19455 const std::string url;
[email protected]d82443b2009-01-15 19:54:56456 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27457 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19458 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56459 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19460 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56461 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19462 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56463 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19464 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56465 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19466 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56467 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19468 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56469 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19470 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56471 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19472 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56473 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19474 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56475 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19476 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56477 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19478 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56479 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19480 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56481 "http://foo/UTF-8a{language}aXa" },
482 };
[email protected]573889f22012-04-07 01:31:54483 TemplateURLData data;
484 data.input_encodings.push_back("UTF-8");
[email protected]b37bdfe2012-03-16 20:53:27485 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54486 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28487 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19488 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
489 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27490 std::string expected_result = test_data[i].expected_result;
[email protected]d82443b2009-01-15 19:54:56491 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}",
[email protected]798baa8e2014-06-20 05:42:44492 search_terms_data_.GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04493 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19494 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
495 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27496 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56497 EXPECT_EQ(expected_result, result.spec());
498 }
499}
500
501
502// Tests replacing search terms in various encodings and making sure the
503// generated URL matches the expected value.
504TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
505 struct TestData {
506 const std::string encoding;
[email protected]0085863a2013-12-06 21:19:03507 const base::string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19508 const std::string url;
[email protected]d82443b2009-01-15 19:54:56509 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27510 } test_data[] = {
[email protected]f911df52013-12-24 23:24:23511 { "BIG5", base::WideToUTF16(L"\x60BD"),
[email protected]400b133f2011-01-19 18:32:30512 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17513 "http://foo/?%B1~BIG5" },
[email protected]400b133f2011-01-19 18:32:30514 { "UTF-8", ASCIIToUTF16("blah"),
515 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17516 "http://foo/?blahUTF-8" },
[email protected]f911df52013-12-24 23:24:23517 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
[email protected]34e24852012-01-31 18:43:58518 "http://foo/{searchTerms}/bar",
519 "http://foo/%82%A0/bar"},
[email protected]f911df52013-12-24 23:24:23520 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
[email protected]34e24852012-01-31 18:43:58521 "http://foo/{searchTerms}/bar",
522 "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56523 };
[email protected]573889f22012-04-07 01:31:54524 TemplateURLData data;
[email protected]b37bdfe2012-03-16 20:53:27525 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54526 data.SetURL(test_data[i].url);
527 data.input_encodings.clear();
528 data.input_encodings.push_back(test_data[i].encoding);
[email protected]168d08722014-06-18 07:13:28529 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19530 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
531 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04532 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19533 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
534 search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04535 ASSERT_TRUE(result.is_valid());
536 EXPECT_EQ(test_data[i].expected_result, result.spec());
537 }
538}
539
540// Tests replacing assisted query stats (AQS) in various scenarios.
541TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
542 struct TestData {
[email protected]0085863a2013-12-06 21:19:03543 const base::string16 search_term;
[email protected]bca359b2012-06-24 07:53:04544 const std::string aqs;
545 const std::string base_url;
546 const std::string url;
547 const std::string expected_result;
548 } test_data[] = {
549 // No HTTPS, no AQS.
550 { ASCIIToUTF16("foo"),
551 "chrome.0.0l6",
552 "http://foo/",
553 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
554 "http://foo/?foo" },
555 // HTTPS available, AQS should be replaced.
556 { ASCIIToUTF16("foo"),
557 "chrome.0.0l6",
558 "https://foo/",
559 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
560 "https://foo/?fooaqs=chrome.0.0l6&" },
561 // HTTPS available, however AQS is empty.
562 { ASCIIToUTF16("foo"),
563 "",
564 "https://foo/",
565 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
566 "https://foo/?foo" },
567 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
568 { ASCIIToUTF16("foo"),
569 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44570 "http://www.google.com",
[email protected]bca359b2012-06-24 07:53:04571 "http://foo?{searchTerms}{google:assistedQueryStats}",
572 "http://foo/?foo" },
573 // A non-Google search provider with HTTPS should allow AQS.
574 { ASCIIToUTF16("foo"),
575 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44576 "https://www.google.com",
[email protected]bca359b2012-06-24 07:53:04577 "https://foo?{searchTerms}{google:assistedQueryStats}",
578 "https://foo/?fooaqs=chrome.0.0l6&" },
579 };
580 TemplateURLData data;
581 data.input_encodings.push_back("UTF-8");
582 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
583 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28584 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19585 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
586 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04587 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
588 search_terms_args.assisted_query_stats = test_data[i].aqs;
[email protected]798baa8e2014-06-20 05:42:44589 search_terms_data_.set_google_base_url(test_data[i].base_url);
[email protected]ce7ee5f2014-06-16 23:41:19590 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
591 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27592 ASSERT_TRUE(result.is_valid());
593 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56594 }
595}
596
[email protected]00790562012-12-14 09:57:16597// Tests replacing cursor position.
598TEST_F(TemplateURLTest, ReplaceCursorPosition) {
599 struct TestData {
[email protected]0085863a2013-12-06 21:19:03600 const base::string16 search_term;
[email protected]00790562012-12-14 09:57:16601 size_t cursor_position;
602 const std::string url;
603 const std::string expected_result;
604 } test_data[] = {
605 { ASCIIToUTF16("foo"),
[email protected]0085863a2013-12-06 21:19:03606 base::string16::npos,
[email protected]00790562012-12-14 09:57:16607 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
608 "http://www.google.com/?foo&" },
609 { ASCIIToUTF16("foo"),
610 2,
611 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
612 "http://www.google.com/?foo&cp=2&" },
613 { ASCIIToUTF16("foo"),
614 15,
615 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
616 "http://www.google.com/?foo&cp=15&" },
617 };
[email protected]00790562012-12-14 09:57:16618 TemplateURLData data;
619 data.input_encodings.push_back("UTF-8");
620 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
621 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28622 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19623 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
624 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]00790562012-12-14 09:57:16625 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
626 search_terms_args.cursor_position = test_data[i].cursor_position;
[email protected]ce7ee5f2014-06-16 23:41:19627 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
628 search_terms_data_));
[email protected]00790562012-12-14 09:57:16629 ASSERT_TRUE(result.is_valid());
630 EXPECT_EQ(test_data[i].expected_result, result.spec());
631 }
632}
633
[email protected]420472b22014-06-10 13:34:43634// Tests replacing input type (&oit=).
635TEST_F(TemplateURLTest, ReplaceInputType) {
636 struct TestData {
637 const base::string16 search_term;
[email protected]332d17d22014-06-20 16:56:03638 metrics::OmniboxInputType::Type input_type;
[email protected]420472b22014-06-10 13:34:43639 const std::string url;
640 const std::string expected_result;
641 } test_data[] = {
642 { ASCIIToUTF16("foo"),
643 metrics::OmniboxInputType::UNKNOWN,
644 "{google:baseURL}?{searchTerms}&{google:inputType}",
645 "http://www.google.com/?foo&oit=1&" },
646 { ASCIIToUTF16("foo"),
647 metrics::OmniboxInputType::URL,
648 "{google:baseURL}?{searchTerms}&{google:inputType}",
649 "http://www.google.com/?foo&oit=3&" },
650 { ASCIIToUTF16("foo"),
651 metrics::OmniboxInputType::FORCED_QUERY,
652 "{google:baseURL}?{searchTerms}&{google:inputType}",
653 "http://www.google.com/?foo&oit=5&" },
654 };
[email protected]420472b22014-06-10 13:34:43655 TemplateURLData data;
656 data.input_encodings.push_back("UTF-8");
657 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
658 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28659 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19660 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
661 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43662 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
663 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19664 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
665 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43666 ASSERT_TRUE(result.is_valid());
667 EXPECT_EQ(test_data[i].expected_result, result.spec());
668 }
669}
670
[email protected]9b9fa672013-11-07 06:04:52671// Tests replacing currentPageUrl.
672TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48673 struct TestData {
[email protected]0085863a2013-12-06 21:19:03674 const base::string16 search_term;
[email protected]9b9fa672013-11-07 06:04:52675 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48676 const std::string url;
677 const std::string expected_result;
678 } test_data[] = {
679 { ASCIIToUTF16("foo"),
680 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52681 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48682 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
683 { ASCIIToUTF16("foo"),
684 "",
[email protected]9b9fa672013-11-07 06:04:52685 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48686 "http://www.google.com/?foo&" },
687 { ASCIIToUTF16("foo"),
688 "http://g.com/+-/*&=",
[email protected]9b9fa672013-11-07 06:04:52689 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48690 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
691 };
[email protected]800569d2013-05-06 07:38:48692 TemplateURLData data;
693 data.input_encodings.push_back("UTF-8");
694 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
695 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28696 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19697 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
698 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]800569d2013-05-06 07:38:48699 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52700 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19701 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
702 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48703 ASSERT_TRUE(result.is_valid());
704 EXPECT_EQ(test_data[i].expected_result, result.spec());
705 }
706}
707
[email protected]d82443b2009-01-15 19:54:56708TEST_F(TemplateURLTest, Suggestions) {
709 struct TestData {
710 const int accepted_suggestion;
[email protected]0085863a2013-12-06 21:19:03711 const base::string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56712 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27713 } test_data[] = {
[email protected]0085863a2013-12-06 21:19:03714 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
[email protected]29653892013-03-02 19:25:37715 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30716 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37717 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03718 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(),
[email protected]29653892013-03-02 19:25:37719 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30720 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37721 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03722 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
[email protected]29653892013-03-02 19:25:37723 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56724 };
[email protected]573889f22012-04-07 01:31:54725 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37726 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
727 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54728 data.input_encodings.push_back("UTF-8");
[email protected]168d08722014-06-18 07:13:28729 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19730 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
731 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27732 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04733 TemplateURLRef::SearchTermsArgs search_terms_args(
734 ASCIIToUTF16("foobar"));
735 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
736 search_terms_args.original_query =
737 test_data[i].original_query_for_suggestion;
[email protected]ce7ee5f2014-06-16 23:41:19738 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
739 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27740 ASSERT_TRUE(result.is_valid());
741 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56742 }
743}
744
[email protected]81f808de2009-09-25 01:36:34745TEST_F(TemplateURLTest, RLZ) {
[email protected]798baa8e2014-06-20 05:42:44746 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(false);
[email protected]d82443b2009-01-15 19:54:56747
[email protected]573889f22012-04-07 01:31:54748 TemplateURLData data;
749 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28750 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19751 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
752 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04753 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19754 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56755 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44756 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
757 result.spec());
[email protected]d82443b2009-01-15 19:54:56758}
759
[email protected]c8ccc41d2014-04-10 04:42:12760TEST_F(TemplateURLTest, RLZFromAppList) {
[email protected]798baa8e2014-06-20 05:42:44761 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(true);
[email protected]c8ccc41d2014-04-10 04:42:12762
763 TemplateURLData data;
764 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28765 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19766 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
767 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12768 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
769 args.from_app_list = true;
[email protected]ce7ee5f2014-06-16 23:41:19770 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12771 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44772 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
773 result.spec());
[email protected]c8ccc41d2014-04-10 04:42:12774}
[email protected]c8ccc41d2014-04-10 04:42:12775
[email protected]d82443b2009-01-15 19:54:56776TEST_F(TemplateURLTest, HostAndSearchTermKey) {
777 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19778 const std::string url;
[email protected]d82443b2009-01-15 19:54:56779 const std::string host;
780 const std::string path;
781 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54782 } test_data[] = {
[email protected]7c60f5042013-02-14 03:39:32783 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56784
785 // No query key should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32786 { "http://blah/{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56787
788 // No term should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32789 { "http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56790
791 // Multiple terms should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32792 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56793
794 // Term in the host shouldn't match.
[email protected]7c60f5042013-02-14 03:39:32795 { "http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56796
[email protected]7c60f5042013-02-14 03:39:32797 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
798 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56799
800 // Single term with extra chars in value should match.
[email protected]7c60f5042013-02-14 03:39:32801 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56802 };
803
[email protected]573889f22012-04-07 01:31:54804 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
805 TemplateURLData data;
806 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28807 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19808 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
809 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
810 EXPECT_EQ(test_data[i].search_term_key,
811 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56812 }
813}
814
815TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
816 static const struct {
[email protected]ddd231e2010-06-29 20:35:19817 const char* const base_url;
818 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56819 } data[] = {
[email protected]014010e2011-10-01 04:12:44820 { "http://google.com/", "http://google.com/complete/", },
821 { "http://www.google.com/", "http://www.google.com/complete/", },
822 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
823 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
824 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56825 };
826
[email protected]f63ae312009-02-04 17:58:46827 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i)
[email protected]d82443b2009-01-15 19:54:56828 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
829}
830
[email protected]81c6ef62010-01-21 09:58:47831TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19832 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:54833 TemplateURLData data;
834 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28835 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47836 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:13837 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19838 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47839 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27840 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47841 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
842}
843
844TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:20845 std::string parsed_url("{fhqwhgads}abc");
[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]1a257262011-06-28 22:15:44850
851 // By default, TemplateURLRef should not consider itself prepopulated.
852 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:13853 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20854 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:44855 EXPECT_TRUE(replacements.empty());
856
857 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:20858 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:54859 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:28860 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:15861 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20862 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47863 EXPECT_TRUE(replacements.empty());
864}
865
866TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:28867 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:47868 TemplateURLRef::Replacements replacements;
869 bool valid = false;
[email protected]b37bdfe2012-03-16 20:53:27870 EXPECT_EQ(std::string(),
[email protected]93b29062013-07-12 03:09:09871 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47872 EXPECT_TRUE(replacements.empty());
873 EXPECT_TRUE(valid);
874}
875
876TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:54877 TemplateURLData data;
878 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:28879 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47880 TemplateURLRef::Replacements replacements;
881 bool valid = false;
[email protected]93b29062013-07-12 03:09:09882 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
883 &valid));
[email protected]81c6ef62010-01-21 09:58:47884 EXPECT_TRUE(replacements.empty());
885 EXPECT_FALSE(valid);
886}
887
888TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:54889 TemplateURLData data;
890 data.SetURL("{}");
[email protected]168d08722014-06-18 07:13:28891 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47892 TemplateURLRef::Replacements replacements;
893 bool valid = false;
[email protected]93b29062013-07-12 03:09:09894 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47895 EXPECT_TRUE(replacements.empty());
896 EXPECT_TRUE(valid);
897}
898
899TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:54900 TemplateURLData data;
901 data.SetURL("{}{{%s}}");
[email protected]168d08722014-06-18 07:13:28902 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47903 TemplateURLRef::Replacements replacements;
904 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19905 EXPECT_EQ("{}{}",
[email protected]93b29062013-07-12 03:09:09906 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
907 &valid));
[email protected]81c6ef62010-01-21 09:58:47908 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27909 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47910 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
911 EXPECT_TRUE(valid);
912}
913
914TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:54915 TemplateURLData data;
916 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:28917 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47918 TemplateURLRef::Replacements replacements;
919 bool valid = false;
[email protected]360ba052012-04-04 17:26:13920 EXPECT_EQ("{",
[email protected]93b29062013-07-12 03:09:09921 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
922 &valid));
[email protected]81c6ef62010-01-21 09:58:47923 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27924 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47925 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
926 EXPECT_TRUE(valid);
927}
[email protected]5b3078752012-10-09 18:54:16928
[email protected]fd6d8822012-12-08 06:56:11929TEST_F(TemplateURLTest, SearchClient) {
930 const std::string base_url_str("http://google.com/?");
931 const std::string terms_str("{searchTerms}&{google:searchClient}");
932 const std::string full_url_str = base_url_str + terms_str;
[email protected]0085863a2013-12-06 21:19:03933 const base::string16 terms(ASCIIToUTF16(terms_str));
[email protected]798baa8e2014-06-20 05:42:44934 search_terms_data_.set_google_base_url(base_url_str);
[email protected]fd6d8822012-12-08 06:56:11935
936 TemplateURLData data;
937 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:28938 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19939 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
940 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11941 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
942
943 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:19944 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
945 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11946 ASSERT_TRUE(result.is_valid());
947 EXPECT_EQ("http://google.com/?foobar&", result.spec());
948
949 // Check that the URL is correct when a client is present.
[email protected]798baa8e2014-06-20 05:42:44950 search_terms_data_.set_search_client("search_client");
[email protected]ce7ee5f2014-06-16 23:41:19951 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
952 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11953 ASSERT_TRUE(result_2.is_valid());
[email protected]798baa8e2014-06-20 05:42:44954 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
[email protected]fd6d8822012-12-08 06:56:11955}
[email protected]fd6d8822012-12-08 06:56:11956
[email protected]5b3078752012-10-09 18:54:16957TEST_F(TemplateURLTest, GetURLNoInstantURL) {
958 TemplateURLData data;
959 data.SetURL("http://google.com/?q={searchTerms}");
960 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
961 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
962 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:28963 TemplateURL url(data);
[email protected]5b3078752012-10-09 18:54:16964 ASSERT_EQ(3U, url.URLCount());
965 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
966 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
967 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
968}
969
970TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
971 TemplateURLData data;
972 data.SetURL("http://google.com/?q={searchTerms}");
973 data.instant_url = "http://google.com/instant#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, GetURLOnlyOneURL) {
984 TemplateURLData data;
985 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:28986 TemplateURL url(data);
[email protected]5b3078752012-10-09 18:54:16987 ASSERT_EQ(1U, url.URLCount());
988 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0));
989}
990
991TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
992 TemplateURLData data;
993 data.SetURL("http://google.com/?q={searchTerms}");
994 data.instant_url = "http://google.com/instant#q={searchTerms}";
995 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
996 data.alternate_urls.push_back(
997 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:28998 TemplateURL url(data);
[email protected]0085863a2013-12-06 21:19:03999 base::string16 result;
[email protected]5b3078752012-10-09 18:54:161000
1001 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191002 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191003 EXPECT_EQ(ASCIIToUTF16("something"), result);
1004
1005 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191006 GURL("http://google.com/?espv&q=something"),
1007 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191008 EXPECT_EQ(ASCIIToUTF16("something"), result);
1009
1010 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191011 GURL("http://google.com/?espv=1&q=something"),
1012 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191013 EXPECT_EQ(ASCIIToUTF16("something"), result);
1014
1015 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191016 GURL("http://google.com/?espv=0&q=something"),
1017 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191018 EXPECT_EQ(ASCIIToUTF16("something"), result);
1019
1020 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191021 GURL("http://google.com/alt/#q=something"),
1022 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191023 EXPECT_EQ(ASCIIToUTF16("something"), result);
1024
1025 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191026 GURL("http://google.com/alt/#espv&q=something"),
1027 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191028 EXPECT_EQ(ASCIIToUTF16("something"), result);
1029
1030 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191031 GURL("http://google.com/alt/#espv=1&q=something"),
1032 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191033 EXPECT_EQ(ASCIIToUTF16("something"), result);
1034
1035 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191036 GURL("http://google.com/alt/#espv=0&q=something"),
1037 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161038 EXPECT_EQ(ASCIIToUTF16("something"), result);
1039
1040 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191041 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371042 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161043
1044 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191045 GURL("http://google.ca/?q=something&q=anything"),
1046 search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371047 EXPECT_EQ(base::string16(), result);
[email protected]67d8b752013-04-03 17:33:271048
1049 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191050 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371051 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161052
[email protected]32e2d81b2012-10-16 19:03:251053 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191054 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
[email protected]32e2d81b2012-10-16 19:03:251055 EXPECT_EQ(ASCIIToUTF16("foo"), 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.com:8080/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371059 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161060
1061 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191062 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161063 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1064
1065 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191066 GURL("http://google.com/alt/?q=123#q=456"),
1067 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161068 EXPECT_EQ(ASCIIToUTF16("456"), result);
1069
1070 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191071 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1072 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161073 EXPECT_EQ(ASCIIToUTF16("123"), result);
1074
1075 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191076 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1077 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161078 EXPECT_EQ(ASCIIToUTF16("789"), result);
1079
1080 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191081 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371082 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161083
1084 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191085 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371086 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161087
1088 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191089 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371090 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161091
1092 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191093 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371094 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161095
1096 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191097 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161098 EXPECT_EQ(ASCIIToUTF16("123"), result);
1099}
[email protected]4076ea62013-01-09 01:47:191100
1101TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
1102 TemplateURLData data;
1103 data.SetURL("http://google.com/?q={searchTerms}");
1104 data.instant_url = "http://google.com/instant#q={searchTerms}";
1105 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1106 data.alternate_urls.push_back(
1107 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1108 data.search_terms_replacement_key = "espv";
[email protected]168d08722014-06-18 07:13:281109 TemplateURL url(data);
[email protected]4076ea62013-01-09 01:47:191110
1111 // Test with instant enabled required.
1112 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1113 GURL("http://google.com/")));
1114
1115 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1116 GURL("http://google.com/?espv")));
1117
1118 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1119 GURL("http://google.com/#espv")));
1120
1121 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1122 GURL("http://google.com/?q=something")));
1123
1124 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1125 GURL("http://google.com/?q=something&espv")));
1126
1127 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1128 GURL("http://google.com/?q=something&espv=1")));
1129
1130 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1131 GURL("http://google.com/?q=something&espv=0")));
1132
1133 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1134 GURL("http://google.com/?espv&q=something")));
1135
1136 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1137 GURL("http://google.com/?espv=1&q=something")));
1138
1139 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1140 GURL("http://google.com/?espv=0&q=something")));
1141
1142 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1143 GURL("http://google.com/alt/#q=something")));
1144
1145 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1146 GURL("http://google.com/alt/#q=something&espv")));
1147
1148 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1149 GURL("http://google.com/alt/#q=something&espv=1")));
1150
1151 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1152 GURL("http://google.com/alt/#q=something&espv=0")));
1153
1154 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1155 GURL("http://google.com/alt/#espv&q=something")));
1156
1157 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1158 GURL("http://google.com/alt/#espv=1&q=something")));
1159
1160 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1161 GURL("http://google.com/alt/#espv=0&q=something")));
1162
1163 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1164 GURL("http://google.com/?espv#q=something")));
1165
1166 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1167 GURL("http://google.com/?espv=1#q=something")));
1168
1169 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1170 GURL("http://google.com/?q=something#espv")));
1171
1172 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1173 GURL("http://google.com/?q=something#espv=1")));
1174
1175 // This does not ensure the domain matches.
1176 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1177 GURL("http://bing.com/?espv")));
1178
1179 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1180 GURL("http://bing.com/#espv")));
1181}
[email protected]f62e30f52013-03-23 03:45:151182
1183TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1184 TemplateURLData data;
1185 data.SetURL("http://google.com/?q={searchTerms}");
1186 data.instant_url = "http://google.com/instant#q={searchTerms}";
1187 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1188 data.alternate_urls.push_back(
1189 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281190 TemplateURL url(data);
[email protected]f62e30f52013-03-23 03:45:151191 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1192 GURL result;
1193
1194 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191195 GURL("http://google.com/?q=something"), search_terms,
1196 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151197 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result);
1198
1199 result = GURL("http://should.not.change.com");
1200 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191201 GURL("http://google.ca/?q=something"), search_terms,
1202 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151203 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1204
1205 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191206 GURL("http://google.com/foo/?q=foo"), search_terms,
1207 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151208
1209 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191210 GURL("https://google.com/?q=foo"), search_terms,
1211 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151212 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result);
1213
1214 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191215 GURL("http://google.com:8080/?q=foo"), search_terms,
1216 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151217
1218 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191219 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1220 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151221 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result);
1222
1223 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1224 // template_url.cc for details.
1225 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191226 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1227 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151228 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result);
1229
1230 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1231 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191232 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151233 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"),
1234 result);
1235
1236 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1237 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191238 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151239 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1240 "#j=abc&q=Bob Morane&h=def9"), result);
1241
1242 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191243 GURL("http://google.com/alt/?q="), search_terms,
1244 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151245
1246 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191247 GURL("http://google.com/alt/?#q="), search_terms,
1248 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151249
1250 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191251 GURL("http://google.com/alt/?q=#q="), search_terms,
1252 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151253
1254 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191255 GURL("http://google.com/alt/?q=123#q="), search_terms,
1256 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151257
1258 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191259 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1260 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151261 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result);
1262}
[email protected]56fa29592013-07-02 20:25:531263
[email protected]621ade062013-10-28 06:27:431264// Test the |suggest_query_params| field of SearchTermsArgs.
1265TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431266 TemplateURLData data;
1267 // Pick a URL with replacements before, during, and after the query, to ensure
1268 // we don't goof up any of them.
1269 data.SetURL("{google:baseURL}search?q={searchTerms}"
1270 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281271 TemplateURL url(data);
[email protected]621ade062013-10-28 06:27:431272
1273 // Baseline: no |suggest_query_params| field.
1274 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1275 search_terms.original_query = ASCIIToUTF16("def");
1276 search_terms.accepted_suggestion = 0;
1277 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191278 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431279
1280 // Set the suggest_query_params.
1281 search_terms.suggest_query_params = "pq=xyz";
1282 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191283 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431284
1285 // Add extra_query_params in the mix, and ensure it works.
1286 search_terms.append_extra_query_params = true;
1287 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1288 switches::kExtraSearchQueryParams, "a=b");
1289 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191290 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431291}
1292
[email protected]56fa29592013-07-02 20:25:531293// Test the |append_extra_query_params| field of SearchTermsArgs.
1294TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531295 TemplateURLData data;
1296 // Pick a URL with replacements before, during, and after the query, to ensure
1297 // we don't goof up any of them.
1298 data.SetURL("{google:baseURL}search?q={searchTerms}"
1299 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281300 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531301
1302 // Baseline: no command-line args, no |append_extra_query_params| flag.
1303 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1304 search_terms.original_query = ASCIIToUTF16("def");
1305 search_terms.accepted_suggestion = 0;
1306 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191307 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531308
1309 // Set the flag. Since there are no command-line args, this should have no
1310 // effect.
1311 search_terms.append_extra_query_params = true;
1312 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191313 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531314
1315 // Now append the command-line arg. This should be inserted into the query.
1316 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1317 switches::kExtraSearchQueryParams, "a=b");
1318 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191319 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531320
1321 // Turn off the flag. Now the command-line arg should be ignored again.
1322 search_terms.append_extra_query_params = false;
1323 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191324 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531325}
[email protected]d5015ca2013-08-08 22:04:181326
1327// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171328TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181329 TemplateURLData data;
1330 data.input_encodings.push_back("UTF-8");
1331 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281332 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191333 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1334 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]d5015ca2013-08-08 22:04:181335 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1336
[email protected]ce7ee5f2014-06-16 23:41:191337 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1338 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181339 EXPECT_EQ("http://www.google.com/?q=foo", result);
1340
[email protected]332d17d22014-06-20 16:56:031341 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191342 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1343 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181344 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1345
1346 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031347 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191348 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1349 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181350 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1351}
[email protected]2328ee22013-08-08 23:00:191352
1353// Test the IsSearchResults function.
1354TEST_F(TemplateURLTest, IsSearchResults) {
1355 TemplateURLData data;
1356 data.SetURL("http://bar/search?q={searchTerms}");
1357 data.instant_url = "http://bar/instant#q={searchTerms}";
[email protected]2767c0fd2013-08-16 17:44:161358 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191359 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1360 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1361 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1362 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281363 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191364
1365 const struct {
1366 const char* const url;
1367 bool result;
1368 } url_data[] = {
1369 { "http://bar/search?q=foo&oq=foo", true, },
1370 { "http://bar/?q=foo&oq=foo", true, },
1371 { "http://bar/#output=search&q=foo&oq=foo", true, },
1372 { "http://bar/webhp#q=foo&oq=foo", true, },
1373 { "http://bar/#q=foo&oq=foo", true, },
1374 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1375 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1376 { "http://bar/", false, },
1377 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161378 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191379 };
1380
1381 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_data); ++i) {
1382 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191383 search_provider.IsSearchURL(GURL(url_data[i].url),
1384 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191385 }
1386}
[email protected]a048de8a2013-10-02 18:30:061387
1388TEST_F(TemplateURLTest, ReflectsBookmarkBarPinned) {
[email protected]a048de8a2013-10-02 18:30:061389 TemplateURLData data;
1390 data.input_encodings.push_back("UTF-8");
1391 data.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281392 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191393 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1394 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]a048de8a2013-10-02 18:30:061395 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1396
1397 // Do not add the param when InstantExtended is suppressed on SRPs.
[email protected]5c3c6e482014-06-23 09:47:181398 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(false);
[email protected]ce7ee5f2014-06-16 23:41:191399 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1400 search_terms_data_);
[email protected]a048de8a2013-10-02 18:30:061401 EXPECT_EQ("http://www.google.com/?q=foo", result);
1402
1403 // Add the param when InstantExtended is not suppressed on SRPs.
[email protected]5c3c6e482014-06-23 09:47:181404 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true);
[email protected]a048de8a2013-10-02 18:30:061405 search_terms_args.bookmark_bar_pinned = false;
[email protected]ce7ee5f2014-06-16 23:41:191406 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1407 search_terms_data_);
[email protected]a048de8a2013-10-02 18:30:061408 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result);
1409
[email protected]5c3c6e482014-06-23 09:47:181410 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true);
[email protected]a048de8a2013-10-02 18:30:061411 search_terms_args.bookmark_bar_pinned = true;
[email protected]ce7ee5f2014-06-16 23:41:191412 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1413 search_terms_data_);
[email protected]a048de8a2013-10-02 18:30:061414 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result);
1415}
[email protected]9d70de12014-05-10 02:15:311416
[email protected]9d70de12014-05-10 02:15:311417TEST_F(TemplateURLTest, AnswersHasVersion) {
1418 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441419 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311420 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1421
[email protected]168d08722014-06-18 07:13:281422 TemplateURL url(data);
[email protected]9d70de12014-05-10 02:15:311423 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191424 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1425 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311426 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1427
[email protected]5c3c6e482014-06-23 09:47:181428 search_terms_data_.set_enable_answers_in_suggest(true);
[email protected]168d08722014-06-18 07:13:281429 TemplateURL url2(data);
[email protected]ce7ee5f2014-06-16 23:41:191430 result = url2.url_ref().ReplaceSearchTerms(search_terms_args,
1431 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311432 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1433}
[email protected]20184242014-05-14 02:57:421434
1435TEST_F(TemplateURLTest, SessionToken) {
1436 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441437 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421438 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1439
[email protected]168d08722014-06-18 07:13:281440 TemplateURL url(data);
[email protected]20184242014-05-14 02:57:421441 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1442 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191443 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1444 search_terms_data_);
[email protected]20184242014-05-14 02:57:421445 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1446
[email protected]168d08722014-06-18 07:13:281447 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421448 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191449 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1450 search_terms_data_);
[email protected]20184242014-05-14 02:57:421451 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1452}
[email protected]448b17f52014-06-13 01:08:031453
1454TEST_F(TemplateURLTest, ContextualSearchParameters) {
1455 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441456 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031457 data.SetURL("http://bar/_/contextualsearch?"
1458 "{google:contextualSearchVersion}"
1459 "{google:contextualSearchContextData}");
1460
[email protected]168d08722014-06-18 07:13:281461 TemplateURL url(data);
[email protected]448b17f52014-06-13 01:08:031462 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191463 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1464 search_terms_data_);
[email protected]448b17f52014-06-13 01:08:031465 EXPECT_EQ("http://bar/_/contextualsearch?", result);
1466
1467 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
1468 1, 6, 11, "allen", "woody+allen+movies", "www.wikipedia.org",
1469 "utf-8");
1470 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191471 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1472 search_terms_data_);
[email protected]448b17f52014-06-13 01:08:031473 EXPECT_EQ("http://bar/_/contextualsearch?"
1474 "ctxs=1&"
1475 "ctxs_start=6&"
1476 "ctxs_end=11&"
1477 "q=allen&"
1478 "ctxs_content=woody+allen+movies&"
1479 "ctxs_url=www.wikipedia.org&"
1480 "ctxs_encoding=utf-8&", result);
1481}
[email protected]44ccc9f2014-06-20 17:36:211482
1483TEST_F(TemplateURLTest, GenerateKeyword) {
1484 ASSERT_EQ(ASCIIToUTF16("foo"),
1485 TemplateURL::GenerateKeyword(GURL("http://foo")));
1486 // www. should be stripped.
1487 ASSERT_EQ(ASCIIToUTF16("foo"),
1488 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
1489 // Make sure we don't get a trailing '/'.
1490 ASSERT_EQ(ASCIIToUTF16("blah"),
1491 TemplateURL::GenerateKeyword(GURL("http://blah/")));
1492 // Don't generate the empty string.
1493 ASSERT_EQ(ASCIIToUTF16("www"),
1494 TemplateURL::GenerateKeyword(GURL("http://www.")));
1495}
1496
1497TEST_F(TemplateURLTest, GenerateSearchURL) {
1498 struct GenerateSearchURLCase {
1499 const char* test_name;
1500 const char* url;
1501 const char* expected;
1502 } generate_url_cases[] = {
1503 { "invalid URL", "foo{searchTerms}", "" },
1504 { "URL with no replacements", "http://foo/", "http://foo/" },
1505 { "basic functionality", "http://foo/{searchTerms}",
1506 "http://foo/blah.blah.blah.blah.blah" }
1507 };
1508
1509 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generate_url_cases); ++i) {
1510 TemplateURLData data;
1511 data.SetURL(generate_url_cases[i].url);
1512 TemplateURL t_url(data);
1513 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1514 generate_url_cases[i].expected)
1515 << generate_url_cases[i].test_name << " failed.";
1516 }
1517}
[email protected]9e9130ce2014-06-23 23:20:511518
1519TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1520 TemplateURLData data;
1521 search_terms_data_.set_google_base_url("http://bar/");
1522 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1523
1524 TemplateURL url(data);
1525 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1526 search_terms_args.prefetch_query = "full query text";
1527 search_terms_args.prefetch_query_type = "2338";
1528 std::string result =
1529 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1530 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1531 result);
1532
1533 TemplateURL url2(data);
1534 search_terms_args.prefetch_query.clear();
1535 search_terms_args.prefetch_query_type.clear();
1536 result =
1537 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1538 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1539}