blob: 555783289da16f74ac7686c37a0999b0143d68ce [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
hashimoto3c831812014-08-25 07:40:2326 TestingSearchTermsData search_terms_data_;
[email protected]d82443b2009-01-15 19:54:5627};
28
[email protected]b37bdfe2012-03-16 20:53:2729void TemplateURLTest::CheckSuggestBaseURL(
30 const std::string& base_url,
31 const std::string& base_suggest_url) const {
hashimoto3c831812014-08-25 07:40:2332 TestingSearchTermsData search_terms_data(base_url);
[email protected]b37bdfe2012-03-16 20:53:2733 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue());
34}
35
[email protected]d82443b2009-01-15 19:54:5636TEST_F(TemplateURLTest, Defaults) {
[email protected]573889f22012-04-07 01:31:5437 TemplateURLData data;
38 EXPECT_FALSE(data.show_in_default_list);
39 EXPECT_FALSE(data.safe_for_autoreplace);
40 EXPECT_EQ(0, data.prepopulate_id);
[email protected]d82443b2009-01-15 19:54:5641}
42
43TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]573889f22012-04-07 01:31:5444 TemplateURLData data;
45 data.SetURL("{searchTerms}");
[email protected]168d08722014-06-18 07:13:2846 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1947 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:5648}
49
50TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1951 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1952 const char* url;
[email protected]0085863a2013-12-06 21:19:0353 const base::string16 terms;
[email protected]34e24852012-01-31 18:43:5854 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:1955 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:3056 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1657 "http://foosea%20rch/bar" },
[email protected]400b133f2011-01-19 18:32:3058 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1659 "http://foosea%20rch/bar?boo=abc" },
[email protected]400b133f2011-01-19 18:32:3060 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1661 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]400b133f2011-01-19 18:32:3062 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
[email protected]c31a979c2012-05-31 23:57:1663 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:1964 };
viettrungluu37a447b2014-10-16 18:23:2765 for (size_t i = 0; i < arraysize(search_term_cases); ++i) {
[email protected]0d2e6a62010-01-15 20:09:1966 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:5467 TemplateURLData data;
68 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:2869 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1970 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
71 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:0472 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:1973 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data_));
[email protected]c31a979c2012-05-31 23:57:1674 ASSERT_TRUE(result.is_valid());
75 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:1976 }
[email protected]d82443b2009-01-15 19:54:5677}
78
79TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:5480 TemplateURLData data;
81 data.SetURL("http://foo{searchTerms}{count?}");
[email protected]168d08722014-06-18 07:13:2882 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1983 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
84 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:0485 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:1986 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:5687 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:2788 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:5689}
90
91TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:5492 TemplateURLData data;
93 data.SetURL("http://foo{searchTerms}{count}");
[email protected]168d08722014-06-18 07:13:2894 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1995 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
96 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:0497 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:1998 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:5699 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27100 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56101}
102
103TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54104 TemplateURLData data;
105 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]168d08722014-06-18 07:13:28106 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19107 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
108 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04109 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19110 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56111 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27112 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56113}
114
115TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54116 TemplateURLData data;
117 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[email protected]168d08722014-06-18 07:13:28118 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19119 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
120 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04121 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19122 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56123 ASSERT_TRUE(result.is_valid());
[email protected]405aae22012-03-29 20:36:13124 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56125}
126
127TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54128 TemplateURLData data;
129 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[email protected]168d08722014-06-18 07:13:28130 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19131 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
132 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04133 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19134 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56135 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27136 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56137}
138
[email protected]93b29062013-07-12 03:09:09139TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
140 const char kInvalidPostParamsString[] =
141 "unknown_template={UnknownTemplate},bad_value=bad{value},"
142 "{google:sbiSource}";
143 // List all accpectable parameter format in valid_post_params_string. it is
144 // expected like: "name0=,name1=value1,name2={template1}"
145 const char kValidPostParamsString[] =
146 "image_content={google:imageThumbnail},image_url={google:imageURL},"
147 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
[email protected]2f3bc6512013-08-28 03:56:27148 "constant_param=constant,width={google:imageOriginalWidth}";
[email protected]93b29062013-07-12 03:09:09149 const char KImageSearchURL[] = "http://foo.com/sbi";
150
151 TemplateURLData data;
152 data.image_url = KImageSearchURL;
153
154 // Try to parse invalid post parameters.
155 data.image_url_post_params = kInvalidPostParamsString;
[email protected]168d08722014-06-18 07:13:28156 TemplateURL url_bad(data);
[email protected]ce7ee5f2014-06-16 23:41:19157 ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09158 const TemplateURLRef::PostParams& bad_post_params =
159 url_bad.image_url_ref().post_params_;
160 ASSERT_EQ(2U, bad_post_params.size());
161 EXPECT_EQ("unknown_template", bad_post_params[0].first);
162 EXPECT_EQ("{UnknownTemplate}", bad_post_params[0].second);
163 EXPECT_EQ("bad_value", bad_post_params[1].first);
164 EXPECT_EQ("bad{value}", bad_post_params[1].second);
165
166 // Try to parse valid post parameters.
167 data.image_url_post_params = kValidPostParamsString;
[email protected]168d08722014-06-18 07:13:28168 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19169 ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_));
170 ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09171
172 // Check term replacement.
173 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
174 search_args.image_thumbnail_content = "dummy-image-thumbnail";
175 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
[email protected]2f3bc6512013-08-28 03:56:27176 search_args.image_original_size = gfx::Size(10, 10);
[email protected]93b29062013-07-12 03:09:09177 // Replacement operation with no post_data buffer should still return
178 // the parsed URL.
hashimoto3c831812014-08-25 07:40:23179 TestingSearchTermsData search_terms_data("http://X");
[email protected]ce7ee5f2014-06-16 23:41:19180 GURL result(url.image_url_ref().ReplaceSearchTerms(
181 search_args, search_terms_data));
[email protected]93b29062013-07-12 03:09:09182 ASSERT_TRUE(result.is_valid());
183 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48184 TemplateURLRef::PostContent post_content;
[email protected]ce7ee5f2014-06-16 23:41:19185 result = GURL(url.image_url_ref().ReplaceSearchTerms(
[email protected]7c2bcc42013-08-01 04:03:48186 search_args, search_terms_data, &post_content));
[email protected]93b29062013-07-12 03:09:09187 ASSERT_TRUE(result.is_valid());
188 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48189 ASSERT_FALSE(post_content.first.empty());
190 ASSERT_FALSE(post_content.second.empty());
[email protected]93b29062013-07-12 03:09:09191
192 // Check parsed result of post parameters.
193 const TemplateURLRef::Replacements& replacements =
194 url.image_url_ref().replacements_;
195 const TemplateURLRef::PostParams& post_params =
196 url.image_url_ref().post_params_;
[email protected]2f3bc6512013-08-28 03:56:27197 EXPECT_EQ(7U, post_params.size());
[email protected]93b29062013-07-12 03:09:09198 for (TemplateURLRef::PostParams::const_iterator i = post_params.begin();
199 i != post_params.end(); ++i) {
200 TemplateURLRef::Replacements::const_iterator j = replacements.begin();
201 for (; j != replacements.end(); ++j) {
202 if (j->is_post_param && j->index ==
203 static_cast<size_t>(i - post_params.begin())) {
204 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27205 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
206 EXPECT_EQ("width", i->first);
207 EXPECT_EQ(
208 base::IntToString(search_args.image_original_size.width()),
209 i->second);
210 break;
[email protected]1020fead2014-06-20 13:40:28211 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE:
212 EXPECT_EQ("sbisrc", i->first);
213 EXPECT_EQ(search_terms_data.GoogleImageSearchSource(), i->second);
214 break;
[email protected]93b29062013-07-12 03:09:09215 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
216 EXPECT_EQ("image_content", i->first);
217 EXPECT_EQ(search_args.image_thumbnail_content, i->second);
218 break;
219 case TemplateURLRef::GOOGLE_IMAGE_URL:
220 EXPECT_EQ("image_url", i->first);
221 EXPECT_EQ(search_args.image_url.spec(), i->second);
222 break;
223 case TemplateURLRef::LANGUAGE:
224 EXPECT_EQ("language", i->first);
225 EXPECT_EQ("en", i->second);
226 break;
227 default:
228 ADD_FAILURE(); // Should never go here.
229 }
230 break;
231 }
232 }
233 if (j != replacements.end())
234 continue;
235 if (i->first == "empty_param") {
236 EXPECT_TRUE(i->second.empty());
237 } else if (i->first == "sbisrc") {
[email protected]7c2bcc42013-08-01 04:03:48238 EXPECT_FALSE(i->second.empty());
[email protected]93b29062013-07-12 03:09:09239 } else {
240 EXPECT_EQ("constant_param", i->first);
241 EXPECT_EQ("constant", i->second);
242 }
243 }
244}
245
[email protected]d88cb202011-08-17 20:03:01246// Test that setting the prepopulate ID from TemplateURL causes the stored
247// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44248TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54249 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20250 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]168d08722014-06-18 07:13:28251 TemplateURL url(data);
[email protected]1a257262011-06-28 22:15:44252 TemplateURLRef::Replacements replacements;
253 bool valid = false;
[email protected]243abf32012-05-15 18:28:20254 EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL(
[email protected]93b29062013-07-12 03:09:09255 "http://foo{fhqwhgads}bar", &replacements, NULL, &valid));
[email protected]1a257262011-06-28 22:15:44256 EXPECT_TRUE(replacements.empty());
257 EXPECT_TRUE(valid);
258
[email protected]573889f22012-04-07 01:31:54259 data.prepopulate_id = 123;
[email protected]168d08722014-06-18 07:13:28260 TemplateURL url2(data);
[email protected]243abf32012-05-15 18:28:20261 EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar",
[email protected]93b29062013-07-12 03:09:09262 &replacements, NULL,
263 &valid));
[email protected]1a257262011-06-28 22:15:44264 EXPECT_TRUE(replacements.empty());
265 EXPECT_TRUE(valid);
266}
267
[email protected]d82443b2009-01-15 19:54:56268TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54269 TemplateURLData data;
270 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]168d08722014-06-18 07:13:28271 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19272 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
273 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04274 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19275 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56276 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27277 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56278}
279
280TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54281 TemplateURLData data;
282 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]168d08722014-06-18 07:13:28283 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19284 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
285 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04286 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19287 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56288 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27289 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56290}
291
[email protected]375bd7312010-08-30 22:18:13292TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
293 struct SearchTermsCase {
294 const char* url;
[email protected]0085863a2013-12-06 21:19:03295 const base::string16 terms;
[email protected]375bd7312010-08-30 22:18:13296 const char* output;
297 } search_term_cases[] = {
[email protected]0085863a2013-12-06 21:19:03298 { "{google:baseURL}{language}{searchTerms}", base::string16(),
[email protected]b37bdfe2012-03-16 20:53:27299 "http://example.com/e/en" },
[email protected]0085863a2013-12-06 21:19:03300 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
[email protected]014010e2011-10-01 04:12:44301 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13302 };
303
hashimoto3c831812014-08-25 07:40:23304 TestingSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54305 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27306 for (size_t i = 0; i < arraysize(search_term_cases); ++i) {
[email protected]375bd7312010-08-30 22:18:13307 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54308 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:28309 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19310 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data));
311 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data));
312 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]93b29062013-07-12 03:09:09313 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL));
[email protected]375bd7312010-08-30 22:18:13314 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27315 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13316 }
317}
318
[email protected]d82443b2009-01-15 19:54:56319TEST_F(TemplateURLTest, URLRefTermToWide) {
320 struct ToWideCase {
321 const char* encoded_search_term;
[email protected]0085863a2013-12-06 21:19:03322 const base::string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56323 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30324 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56325 // Test some big-5 input.
[email protected]f911df52013-12-24 23:24:23326 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56327 // Test some UTF-8 input. We should fall back to this when the encoding
328 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
329 // Big-5 trailing byte.
[email protected]f911df52013-12-24 23:24:23330 {"%e4%bd%a05%e5%a5%bd+to+you",
331 base::WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56332 // Undecodable input should stay escaped.
[email protected]f911df52013-12-24 23:24:23333 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44334 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30335 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44336 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30337 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56338 };
339
[email protected]d82443b2009-01-15 19:54:56340 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54341 TemplateURLData data;
342 data.SetURL("http://foo?q={searchTerms}");
343 data.input_encodings.push_back("big-5");
[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_));
viettrungluu37a447b2014-10-16 18:23:27347 for (size_t i = 0; i < arraysize(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07348 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13349 url.url_ref().SearchTermToString16(
350 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56351 }
352}
353
[email protected]d82443b2009-01-15 19:54:56354TEST_F(TemplateURLTest, DisplayURLToURLRef) {
355 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19356 const std::string url;
[email protected]0085863a2013-12-06 21:19:03357 const base::string16 expected_result;
[email protected]b37bdfe2012-03-16 20:53:27358 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19359 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30360 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19361 { "http://X",
[email protected]400b133f2011-01-19 18:32:30362 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19363 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30364 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19365 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30366 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56367 };
[email protected]573889f22012-04-07 01:31:54368 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27369 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54370 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28371 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19372 EXPECT_EQ(test_data[i].expected_result,
373 url.url_ref().DisplayURL(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27374 EXPECT_EQ(test_data[i].url,
[email protected]ce7ee5f2014-06-16 23:41:19375 TemplateURLRef::DisplayURLToURLRef(
376 url.url_ref().DisplayURL(search_terms_data_)));
[email protected]d82443b2009-01-15 19:54:56377 }
378}
379
380TEST_F(TemplateURLTest, ReplaceSearchTerms) {
381 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19382 const std::string url;
[email protected]d82443b2009-01-15 19:54:56383 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27384 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19385 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56386 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19387 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56388 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19389 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56390 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19391 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56392 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19393 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56394 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19395 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56396 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19397 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56398 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19399 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56400 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19401 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56402 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19403 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56404 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19405 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56406 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19407 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56408 "http://foo/UTF-8a{language}aXa" },
409 };
[email protected]573889f22012-04-07 01:31:54410 TemplateURLData data;
411 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27412 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54413 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28414 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19415 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
416 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27417 std::string expected_result = test_data[i].expected_result;
[email protected]d82443b2009-01-15 19:54:56418 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}",
[email protected]798baa8e2014-06-20 05:42:44419 search_terms_data_.GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04420 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19421 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
422 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27423 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56424 EXPECT_EQ(expected_result, result.spec());
425 }
426}
427
428
429// Tests replacing search terms in various encodings and making sure the
430// generated URL matches the expected value.
431TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
432 struct TestData {
433 const std::string encoding;
[email protected]0085863a2013-12-06 21:19:03434 const base::string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19435 const std::string url;
[email protected]d82443b2009-01-15 19:54:56436 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27437 } test_data[] = {
[email protected]f911df52013-12-24 23:24:23438 { "BIG5", base::WideToUTF16(L"\x60BD"),
[email protected]400b133f2011-01-19 18:32:30439 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17440 "http://foo/?%B1~BIG5" },
[email protected]400b133f2011-01-19 18:32:30441 { "UTF-8", ASCIIToUTF16("blah"),
442 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17443 "http://foo/?blahUTF-8" },
[email protected]f911df52013-12-24 23:24:23444 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
[email protected]34e24852012-01-31 18:43:58445 "http://foo/{searchTerms}/bar",
446 "http://foo/%82%A0/bar"},
[email protected]f911df52013-12-24 23:24:23447 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
[email protected]34e24852012-01-31 18:43:58448 "http://foo/{searchTerms}/bar",
449 "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56450 };
[email protected]573889f22012-04-07 01:31:54451 TemplateURLData data;
viettrungluu37a447b2014-10-16 18:23:27452 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54453 data.SetURL(test_data[i].url);
454 data.input_encodings.clear();
455 data.input_encodings.push_back(test_data[i].encoding);
[email protected]168d08722014-06-18 07:13:28456 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19457 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
458 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04459 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19460 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
461 search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04462 ASSERT_TRUE(result.is_valid());
463 EXPECT_EQ(test_data[i].expected_result, result.spec());
464 }
465}
466
467// Tests replacing assisted query stats (AQS) in various scenarios.
468TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
469 struct TestData {
[email protected]0085863a2013-12-06 21:19:03470 const base::string16 search_term;
[email protected]bca359b2012-06-24 07:53:04471 const std::string aqs;
472 const std::string base_url;
473 const std::string url;
474 const std::string expected_result;
475 } test_data[] = {
476 // No HTTPS, no AQS.
477 { ASCIIToUTF16("foo"),
478 "chrome.0.0l6",
479 "http://foo/",
480 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
481 "http://foo/?foo" },
482 // HTTPS available, AQS should be replaced.
483 { ASCIIToUTF16("foo"),
484 "chrome.0.0l6",
485 "https://foo/",
486 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
487 "https://foo/?fooaqs=chrome.0.0l6&" },
488 // HTTPS available, however AQS is empty.
489 { ASCIIToUTF16("foo"),
490 "",
491 "https://foo/",
492 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
493 "https://foo/?foo" },
494 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
495 { ASCIIToUTF16("foo"),
496 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44497 "http://www.google.com",
[email protected]bca359b2012-06-24 07:53:04498 "http://foo?{searchTerms}{google:assistedQueryStats}",
499 "http://foo/?foo" },
500 // A non-Google search provider with HTTPS should allow AQS.
501 { ASCIIToUTF16("foo"),
502 "chrome.0.0l6",
[email protected]798baa8e2014-06-20 05:42:44503 "https://www.google.com",
[email protected]bca359b2012-06-24 07:53:04504 "https://foo?{searchTerms}{google:assistedQueryStats}",
505 "https://foo/?fooaqs=chrome.0.0l6&" },
506 };
507 TemplateURLData data;
508 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27509 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04510 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28511 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19512 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
513 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04514 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
515 search_terms_args.assisted_query_stats = test_data[i].aqs;
[email protected]798baa8e2014-06-20 05:42:44516 search_terms_data_.set_google_base_url(test_data[i].base_url);
[email protected]ce7ee5f2014-06-16 23:41:19517 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
518 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27519 ASSERT_TRUE(result.is_valid());
520 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56521 }
522}
523
[email protected]00790562012-12-14 09:57:16524// Tests replacing cursor position.
525TEST_F(TemplateURLTest, ReplaceCursorPosition) {
526 struct TestData {
[email protected]0085863a2013-12-06 21:19:03527 const base::string16 search_term;
[email protected]00790562012-12-14 09:57:16528 size_t cursor_position;
529 const std::string url;
530 const std::string expected_result;
531 } test_data[] = {
532 { ASCIIToUTF16("foo"),
[email protected]0085863a2013-12-06 21:19:03533 base::string16::npos,
[email protected]00790562012-12-14 09:57:16534 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
535 "http://www.google.com/?foo&" },
536 { ASCIIToUTF16("foo"),
537 2,
538 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
539 "http://www.google.com/?foo&cp=2&" },
540 { ASCIIToUTF16("foo"),
541 15,
542 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
543 "http://www.google.com/?foo&cp=15&" },
544 };
545 TemplateURLData data;
546 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27547 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]00790562012-12-14 09:57:16548 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28549 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19550 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
551 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]00790562012-12-14 09:57:16552 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
553 search_terms_args.cursor_position = test_data[i].cursor_position;
[email protected]ce7ee5f2014-06-16 23:41:19554 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
555 search_terms_data_));
[email protected]00790562012-12-14 09:57:16556 ASSERT_TRUE(result.is_valid());
557 EXPECT_EQ(test_data[i].expected_result, result.spec());
558 }
559}
560
[email protected]420472b22014-06-10 13:34:43561// Tests replacing input type (&oit=).
562TEST_F(TemplateURLTest, ReplaceInputType) {
563 struct TestData {
564 const base::string16 search_term;
[email protected]332d17d22014-06-20 16:56:03565 metrics::OmniboxInputType::Type input_type;
[email protected]420472b22014-06-10 13:34:43566 const std::string url;
567 const std::string expected_result;
568 } test_data[] = {
569 { ASCIIToUTF16("foo"),
570 metrics::OmniboxInputType::UNKNOWN,
571 "{google:baseURL}?{searchTerms}&{google:inputType}",
572 "http://www.google.com/?foo&oit=1&" },
573 { ASCIIToUTF16("foo"),
574 metrics::OmniboxInputType::URL,
575 "{google:baseURL}?{searchTerms}&{google:inputType}",
576 "http://www.google.com/?foo&oit=3&" },
577 { ASCIIToUTF16("foo"),
578 metrics::OmniboxInputType::FORCED_QUERY,
579 "{google:baseURL}?{searchTerms}&{google:inputType}",
580 "http://www.google.com/?foo&oit=5&" },
581 };
[email protected]420472b22014-06-10 13:34:43582 TemplateURLData data;
583 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27584 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]420472b22014-06-10 13:34:43585 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28586 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19587 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
588 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43589 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
590 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19591 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
592 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43593 ASSERT_TRUE(result.is_valid());
594 EXPECT_EQ(test_data[i].expected_result, result.spec());
595 }
596}
597
[email protected]9b9fa672013-11-07 06:04:52598// Tests replacing currentPageUrl.
599TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48600 struct TestData {
[email protected]0085863a2013-12-06 21:19:03601 const base::string16 search_term;
[email protected]9b9fa672013-11-07 06:04:52602 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48603 const std::string url;
604 const std::string expected_result;
605 } test_data[] = {
606 { ASCIIToUTF16("foo"),
607 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52608 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48609 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
610 { ASCIIToUTF16("foo"),
611 "",
[email protected]9b9fa672013-11-07 06:04:52612 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48613 "http://www.google.com/?foo&" },
614 { ASCIIToUTF16("foo"),
615 "http://g.com/+-/*&=",
[email protected]9b9fa672013-11-07 06:04:52616 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48617 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
618 };
619 TemplateURLData data;
620 data.input_encodings.push_back("UTF-8");
viettrungluu37a447b2014-10-16 18:23:27621 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]800569d2013-05-06 07:38:48622 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28623 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19624 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
625 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]800569d2013-05-06 07:38:48626 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52627 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19628 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
629 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48630 ASSERT_TRUE(result.is_valid());
631 EXPECT_EQ(test_data[i].expected_result, result.spec());
632 }
633}
634
[email protected]1d7727e2014-07-23 07:04:43635TEST_F(TemplateURLTest, OmniboxStartmargin) {
636 struct TestData {
637 const bool enable_omnibox_start_margin;
638 const int omnibox_start_margin;
639 const std::string expected_result;
640 } test_data[] = {
641 { false,
642 0,
643 "http://bar/foo?q=foobar" },
644 { true,
645 0,
646 "http://bar/foo?es_sm=0&q=foobar" },
647 { true,
648 42,
649 "http://bar/foo?es_sm=42&q=foobar" },
650 };
651 TemplateURLData data;
652 data.SetURL("http://bar/foo?{google:omniboxStartMarginParameter}"
653 "q={searchTerms}");
654 data.input_encodings.push_back("UTF-8");
655 TemplateURL url(data);
656 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
657 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
viettrungluu37a447b2014-10-16 18:23:27658 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]1d7727e2014-07-23 07:04:43659 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
660 search_terms_args.enable_omnibox_start_margin =
661 test_data[i].enable_omnibox_start_margin;
662 search_terms_data_.set_omnibox_start_margin(
663 test_data[i].omnibox_start_margin);
664 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
665 search_terms_data_));
666 ASSERT_TRUE(result.is_valid());
667 EXPECT_EQ(test_data[i].expected_result, result.spec());
668 }
669}
670
[email protected]d82443b2009-01-15 19:54:56671TEST_F(TemplateURLTest, Suggestions) {
672 struct TestData {
673 const int accepted_suggestion;
[email protected]0085863a2013-12-06 21:19:03674 const base::string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56675 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27676 } test_data[] = {
[email protected]0085863a2013-12-06 21:19:03677 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
[email protected]29653892013-03-02 19:25:37678 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30679 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37680 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03681 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(),
[email protected]29653892013-03-02 19:25:37682 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30683 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37684 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03685 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
[email protected]29653892013-03-02 19:25:37686 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56687 };
[email protected]573889f22012-04-07 01:31:54688 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37689 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
690 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54691 data.input_encodings.push_back("UTF-8");
[email protected]168d08722014-06-18 07:13:28692 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19693 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
694 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
viettrungluu37a447b2014-10-16 18:23:27695 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04696 TemplateURLRef::SearchTermsArgs search_terms_args(
697 ASCIIToUTF16("foobar"));
698 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
699 search_terms_args.original_query =
700 test_data[i].original_query_for_suggestion;
[email protected]ce7ee5f2014-06-16 23:41:19701 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
702 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27703 ASSERT_TRUE(result.is_valid());
704 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56705 }
706}
707
[email protected]81f808de2009-09-25 01:36:34708TEST_F(TemplateURLTest, RLZ) {
[email protected]798baa8e2014-06-20 05:42:44709 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(false);
[email protected]d82443b2009-01-15 19:54:56710
[email protected]573889f22012-04-07 01:31:54711 TemplateURLData data;
712 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28713 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19714 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
715 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04716 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19717 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56718 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44719 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
720 result.spec());
[email protected]d82443b2009-01-15 19:54:56721}
722
[email protected]c8ccc41d2014-04-10 04:42:12723TEST_F(TemplateURLTest, RLZFromAppList) {
[email protected]798baa8e2014-06-20 05:42:44724 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(true);
[email protected]c8ccc41d2014-04-10 04:42:12725
726 TemplateURLData data;
727 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28728 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19729 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
730 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12731 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
732 args.from_app_list = true;
[email protected]ce7ee5f2014-06-16 23:41:19733 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12734 ASSERT_TRUE(result.is_valid());
[email protected]798baa8e2014-06-20 05:42:44735 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x",
736 result.spec());
[email protected]c8ccc41d2014-04-10 04:42:12737}
[email protected]c8ccc41d2014-04-10 04:42:12738
[email protected]d82443b2009-01-15 19:54:56739TEST_F(TemplateURLTest, HostAndSearchTermKey) {
740 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19741 const std::string url;
[email protected]d82443b2009-01-15 19:54:56742 const std::string host;
743 const std::string path;
744 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54745 } test_data[] = {
[email protected]7c60f5042013-02-14 03:39:32746 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56747
748 // No query key should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32749 { "http://blah/{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56750
751 // No term should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32752 { "http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56753
754 // Multiple terms should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32755 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56756
757 // Term in the host shouldn't match.
[email protected]7c60f5042013-02-14 03:39:32758 { "http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56759
[email protected]7c60f5042013-02-14 03:39:32760 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
761 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56762
763 // Single term with extra chars in value should match.
[email protected]7c60f5042013-02-14 03:39:32764 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56765 };
766
viettrungluu37a447b2014-10-16 18:23:27767 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54768 TemplateURLData data;
769 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28770 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19771 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
772 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
773 EXPECT_EQ(test_data[i].search_term_key,
774 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56775 }
776}
777
778TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
779 static const struct {
[email protected]ddd231e2010-06-29 20:35:19780 const char* const base_url;
781 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56782 } data[] = {
[email protected]014010e2011-10-01 04:12:44783 { "http://google.com/", "http://google.com/complete/", },
784 { "http://www.google.com/", "http://www.google.com/complete/", },
785 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
786 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
787 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56788 };
789
viettrungluu37a447b2014-10-16 18:23:27790 for (size_t i = 0; i < arraysize(data); ++i)
[email protected]d82443b2009-01-15 19:54:56791 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
792}
793
[email protected]81c6ef62010-01-21 09:58:47794TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19795 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:54796 TemplateURLData data;
797 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28798 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47799 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:13800 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19801 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47802 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27803 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47804 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
805}
806
807TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:20808 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:54809 TemplateURLData data;
810 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28811 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47812 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:44813
814 // By default, TemplateURLRef should not consider itself prepopulated.
815 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:13816 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20817 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:44818 EXPECT_TRUE(replacements.empty());
819
820 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:20821 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:54822 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:28823 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:15824 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20825 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47826 EXPECT_TRUE(replacements.empty());
827}
828
829TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:28830 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:47831 TemplateURLRef::Replacements replacements;
832 bool valid = false;
[email protected]b37bdfe2012-03-16 20:53:27833 EXPECT_EQ(std::string(),
[email protected]93b29062013-07-12 03:09:09834 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47835 EXPECT_TRUE(replacements.empty());
836 EXPECT_TRUE(valid);
837}
838
839TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:54840 TemplateURLData data;
841 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:28842 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47843 TemplateURLRef::Replacements replacements;
844 bool valid = false;
[email protected]93b29062013-07-12 03:09:09845 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
846 &valid));
[email protected]81c6ef62010-01-21 09:58:47847 EXPECT_TRUE(replacements.empty());
848 EXPECT_FALSE(valid);
849}
850
851TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:54852 TemplateURLData data;
853 data.SetURL("{}");
[email protected]168d08722014-06-18 07:13:28854 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47855 TemplateURLRef::Replacements replacements;
856 bool valid = false;
[email protected]93b29062013-07-12 03:09:09857 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47858 EXPECT_TRUE(replacements.empty());
859 EXPECT_TRUE(valid);
860}
861
862TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:54863 TemplateURLData data;
864 data.SetURL("{}{{%s}}");
[email protected]168d08722014-06-18 07:13:28865 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47866 TemplateURLRef::Replacements replacements;
867 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19868 EXPECT_EQ("{}{}",
[email protected]93b29062013-07-12 03:09:09869 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
870 &valid));
[email protected]81c6ef62010-01-21 09:58:47871 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27872 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47873 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
874 EXPECT_TRUE(valid);
875}
876
877TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:54878 TemplateURLData data;
879 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:28880 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47881 TemplateURLRef::Replacements replacements;
882 bool valid = false;
[email protected]360ba052012-04-04 17:26:13883 EXPECT_EQ("{",
[email protected]93b29062013-07-12 03:09:09884 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
885 &valid));
[email protected]81c6ef62010-01-21 09:58:47886 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27887 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47888 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
889 EXPECT_TRUE(valid);
890}
[email protected]5b3078752012-10-09 18:54:16891
[email protected]fd6d8822012-12-08 06:56:11892TEST_F(TemplateURLTest, SearchClient) {
893 const std::string base_url_str("http://google.com/?");
894 const std::string terms_str("{searchTerms}&{google:searchClient}");
895 const std::string full_url_str = base_url_str + terms_str;
[email protected]0085863a2013-12-06 21:19:03896 const base::string16 terms(ASCIIToUTF16(terms_str));
[email protected]798baa8e2014-06-20 05:42:44897 search_terms_data_.set_google_base_url(base_url_str);
[email protected]fd6d8822012-12-08 06:56:11898
899 TemplateURLData data;
900 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:28901 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19902 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
903 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11904 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
905
906 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:19907 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
908 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11909 ASSERT_TRUE(result.is_valid());
910 EXPECT_EQ("http://google.com/?foobar&", result.spec());
911
912 // Check that the URL is correct when a client is present.
[email protected]798baa8e2014-06-20 05:42:44913 search_terms_data_.set_search_client("search_client");
[email protected]ce7ee5f2014-06-16 23:41:19914 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
915 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11916 ASSERT_TRUE(result_2.is_valid());
[email protected]798baa8e2014-06-20 05:42:44917 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec());
[email protected]fd6d8822012-12-08 06:56:11918}
[email protected]fd6d8822012-12-08 06:56:11919
[email protected]5b3078752012-10-09 18:54:16920TEST_F(TemplateURLTest, GetURLNoInstantURL) {
921 TemplateURLData data;
922 data.SetURL("http://google.com/?q={searchTerms}");
923 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
924 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
925 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:28926 TemplateURL url(data);
[email protected]5b3078752012-10-09 18:54:16927 ASSERT_EQ(3U, url.URLCount());
928 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
929 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
930 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
931}
932
933TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
934 TemplateURLData data;
935 data.SetURL("http://google.com/?q={searchTerms}");
936 data.instant_url = "http://google.com/instant#q={searchTerms}";
937 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
938 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:28939 TemplateURL url(data);
[email protected]5b3078752012-10-09 18:54:16940 ASSERT_EQ(3U, url.URLCount());
941 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
942 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
943 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
944}
945
946TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
947 TemplateURLData data;
948 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:28949 TemplateURL url(data);
[email protected]5b3078752012-10-09 18:54:16950 ASSERT_EQ(1U, url.URLCount());
951 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0));
952}
953
954TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
955 TemplateURLData data;
956 data.SetURL("http://google.com/?q={searchTerms}");
957 data.instant_url = "http://google.com/instant#q={searchTerms}";
958 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
959 data.alternate_urls.push_back(
960 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:28961 TemplateURL url(data);
[email protected]0085863a2013-12-06 21:19:03962 base::string16 result;
[email protected]5b3078752012-10-09 18:54:16963
964 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:19965 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:19966 EXPECT_EQ(ASCIIToUTF16("something"), result);
967
968 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:19969 GURL("http://google.com/?espv&q=something"),
970 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:19971 EXPECT_EQ(ASCIIToUTF16("something"), result);
972
973 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:19974 GURL("http://google.com/?espv=1&q=something"),
975 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:19976 EXPECT_EQ(ASCIIToUTF16("something"), result);
977
978 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:19979 GURL("http://google.com/?espv=0&q=something"),
980 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:19981 EXPECT_EQ(ASCIIToUTF16("something"), result);
982
983 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:19984 GURL("http://google.com/alt/#q=something"),
985 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:19986 EXPECT_EQ(ASCIIToUTF16("something"), result);
987
988 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:19989 GURL("http://google.com/alt/#espv&q=something"),
990 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:19991 EXPECT_EQ(ASCIIToUTF16("something"), result);
992
993 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:19994 GURL("http://google.com/alt/#espv=1&q=something"),
995 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:19996 EXPECT_EQ(ASCIIToUTF16("something"), result);
997
998 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:19999 GURL("http://google.com/alt/#espv=0&q=something"),
1000 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161001 EXPECT_EQ(ASCIIToUTF16("something"), result);
1002
1003 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191004 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371005 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161006
1007 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191008 GURL("http://google.ca/?q=something&q=anything"),
1009 search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371010 EXPECT_EQ(base::string16(), result);
[email protected]67d8b752013-04-03 17:33:271011
1012 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191013 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371014 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161015
[email protected]32e2d81b2012-10-16 19:03:251016 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191017 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
[email protected]32e2d81b2012-10-16 19:03:251018 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:161019
1020 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191021 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371022 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161023
1024 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191025 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161026 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1027
1028 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191029 GURL("http://google.com/alt/?q=123#q=456"),
1030 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161031 EXPECT_EQ(ASCIIToUTF16("456"), result);
1032
1033 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191034 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1035 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161036 EXPECT_EQ(ASCIIToUTF16("123"), result);
1037
1038 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191039 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1040 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161041 EXPECT_EQ(ASCIIToUTF16("789"), result);
1042
1043 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191044 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371045 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161046
1047 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191048 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371049 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161050
1051 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191052 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371053 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161054
1055 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191056 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371057 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161058
1059 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191060 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161061 EXPECT_EQ(ASCIIToUTF16("123"), result);
1062}
[email protected]4076ea62013-01-09 01:47:191063
1064TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
1065 TemplateURLData data;
1066 data.SetURL("http://google.com/?q={searchTerms}");
1067 data.instant_url = "http://google.com/instant#q={searchTerms}";
1068 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1069 data.alternate_urls.push_back(
1070 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1071 data.search_terms_replacement_key = "espv";
[email protected]168d08722014-06-18 07:13:281072 TemplateURL url(data);
[email protected]4076ea62013-01-09 01:47:191073
1074 // Test with instant enabled required.
1075 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1076 GURL("http://google.com/")));
1077
1078 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1079 GURL("http://google.com/?espv")));
1080
1081 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1082 GURL("http://google.com/#espv")));
1083
1084 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1085 GURL("http://google.com/?q=something")));
1086
1087 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1088 GURL("http://google.com/?q=something&espv")));
1089
1090 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1091 GURL("http://google.com/?q=something&espv=1")));
1092
1093 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1094 GURL("http://google.com/?q=something&espv=0")));
1095
1096 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1097 GURL("http://google.com/?espv&q=something")));
1098
1099 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1100 GURL("http://google.com/?espv=1&q=something")));
1101
1102 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1103 GURL("http://google.com/?espv=0&q=something")));
1104
1105 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1106 GURL("http://google.com/alt/#q=something")));
1107
1108 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1109 GURL("http://google.com/alt/#q=something&espv")));
1110
1111 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1112 GURL("http://google.com/alt/#q=something&espv=1")));
1113
1114 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1115 GURL("http://google.com/alt/#q=something&espv=0")));
1116
1117 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1118 GURL("http://google.com/alt/#espv&q=something")));
1119
1120 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1121 GURL("http://google.com/alt/#espv=1&q=something")));
1122
1123 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1124 GURL("http://google.com/alt/#espv=0&q=something")));
1125
1126 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1127 GURL("http://google.com/?espv#q=something")));
1128
1129 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1130 GURL("http://google.com/?espv=1#q=something")));
1131
1132 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1133 GURL("http://google.com/?q=something#espv")));
1134
1135 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1136 GURL("http://google.com/?q=something#espv=1")));
1137
1138 // This does not ensure the domain matches.
1139 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1140 GURL("http://bing.com/?espv")));
1141
1142 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1143 GURL("http://bing.com/#espv")));
1144}
[email protected]f62e30f52013-03-23 03:45:151145
1146TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1147 TemplateURLData data;
1148 data.SetURL("http://google.com/?q={searchTerms}");
1149 data.instant_url = "http://google.com/instant#q={searchTerms}";
1150 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1151 data.alternate_urls.push_back(
1152 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281153 TemplateURL url(data);
[email protected]f62e30f52013-03-23 03:45:151154 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1155 GURL result;
1156
1157 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191158 GURL("http://google.com/?q=something"), search_terms,
1159 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151160 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result);
1161
1162 result = GURL("http://should.not.change.com");
1163 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191164 GURL("http://google.ca/?q=something"), search_terms,
1165 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151166 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1167
1168 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191169 GURL("http://google.com/foo/?q=foo"), search_terms,
1170 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151171
1172 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191173 GURL("https://google.com/?q=foo"), search_terms,
1174 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151175 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result);
1176
1177 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191178 GURL("http://google.com:8080/?q=foo"), search_terms,
1179 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151180
1181 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191182 GURL("http://google.com/?q=1+2+3&b=456"), search_terms,
1183 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151184 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result);
1185
1186 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1187 // template_url.cc for details.
1188 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191189 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1190 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151191 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result);
1192
1193 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1194 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191195 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151196 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"),
1197 result);
1198
1199 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1200 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191201 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151202 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1203 "#j=abc&q=Bob Morane&h=def9"), result);
1204
1205 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191206 GURL("http://google.com/alt/?q="), search_terms,
1207 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151208
1209 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191210 GURL("http://google.com/alt/?#q="), search_terms,
1211 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151212
1213 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191214 GURL("http://google.com/alt/?q=#q="), search_terms,
1215 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151216
1217 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191218 GURL("http://google.com/alt/?q=123#q="), search_terms,
1219 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151220
1221 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191222 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1223 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151224 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result);
1225}
[email protected]56fa29592013-07-02 20:25:531226
[email protected]621ade062013-10-28 06:27:431227// Test the |suggest_query_params| field of SearchTermsArgs.
1228TEST_F(TemplateURLTest, SuggestQueryParams) {
[email protected]621ade062013-10-28 06:27:431229 TemplateURLData data;
1230 // Pick a URL with replacements before, during, and after the query, to ensure
1231 // we don't goof up any of them.
1232 data.SetURL("{google:baseURL}search?q={searchTerms}"
1233 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281234 TemplateURL url(data);
[email protected]621ade062013-10-28 06:27:431235
1236 // Baseline: no |suggest_query_params| field.
1237 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1238 search_terms.original_query = ASCIIToUTF16("def");
1239 search_terms.accepted_suggestion = 0;
1240 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191241 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431242
1243 // Set the suggest_query_params.
1244 search_terms.suggest_query_params = "pq=xyz";
1245 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191246 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431247
1248 // Add extra_query_params in the mix, and ensure it works.
1249 search_terms.append_extra_query_params = true;
1250 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1251 switches::kExtraSearchQueryParams, "a=b");
1252 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191253 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431254}
1255
[email protected]56fa29592013-07-02 20:25:531256// Test the |append_extra_query_params| field of SearchTermsArgs.
1257TEST_F(TemplateURLTest, ExtraQueryParams) {
[email protected]56fa29592013-07-02 20:25:531258 TemplateURLData data;
1259 // Pick a URL with replacements before, during, and after the query, to ensure
1260 // we don't goof up any of them.
1261 data.SetURL("{google:baseURL}search?q={searchTerms}"
1262 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281263 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531264
1265 // Baseline: no command-line args, no |append_extra_query_params| flag.
1266 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1267 search_terms.original_query = ASCIIToUTF16("def");
1268 search_terms.accepted_suggestion = 0;
1269 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191270 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531271
1272 // Set the flag. Since there are no command-line args, this should have no
1273 // effect.
1274 search_terms.append_extra_query_params = true;
1275 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191276 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531277
1278 // Now append the command-line arg. This should be inserted into the query.
1279 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1280 switches::kExtraSearchQueryParams, "a=b");
1281 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191282 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531283
1284 // Turn off the flag. Now the command-line arg should be ignored again.
1285 search_terms.append_extra_query_params = false;
1286 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191287 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531288}
[email protected]d5015ca2013-08-08 22:04:181289
1290// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171291TEST_F(TemplateURLTest, ReplacePageClassification) {
[email protected]d5015ca2013-08-08 22:04:181292 TemplateURLData data;
1293 data.input_encodings.push_back("UTF-8");
1294 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281295 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191296 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1297 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]d5015ca2013-08-08 22:04:181298 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1299
[email protected]ce7ee5f2014-06-16 23:41:191300 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1301 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181302 EXPECT_EQ("http://www.google.com/?q=foo", result);
1303
[email protected]332d17d22014-06-20 16:56:031304 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191305 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1306 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181307 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1308
1309 search_terms_args.page_classification =
[email protected]332d17d22014-06-20 16:56:031310 metrics::OmniboxEventProto::HOME_PAGE;
[email protected]ce7ee5f2014-06-16 23:41:191311 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1312 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181313 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1314}
[email protected]2328ee22013-08-08 23:00:191315
1316// Test the IsSearchResults function.
1317TEST_F(TemplateURLTest, IsSearchResults) {
1318 TemplateURLData data;
1319 data.SetURL("http://bar/search?q={searchTerms}");
1320 data.instant_url = "http://bar/instant#q={searchTerms}";
[email protected]2767c0fd2013-08-16 17:44:161321 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191322 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1323 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1324 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1325 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281326 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191327
1328 const struct {
1329 const char* const url;
1330 bool result;
1331 } url_data[] = {
1332 { "http://bar/search?q=foo&oq=foo", true, },
1333 { "http://bar/?q=foo&oq=foo", true, },
1334 { "http://bar/#output=search&q=foo&oq=foo", true, },
1335 { "http://bar/webhp#q=foo&oq=foo", true, },
1336 { "http://bar/#q=foo&oq=foo", true, },
1337 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1338 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1339 { "http://bar/", false, },
1340 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161341 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191342 };
1343
viettrungluu37a447b2014-10-16 18:23:271344 for (size_t i = 0; i < arraysize(url_data); ++i) {
[email protected]2328ee22013-08-08 23:00:191345 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191346 search_provider.IsSearchURL(GURL(url_data[i].url),
1347 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191348 }
1349}
[email protected]a048de8a2013-10-02 18:30:061350
1351TEST_F(TemplateURLTest, ReflectsBookmarkBarPinned) {
1352 TemplateURLData data;
1353 data.input_encodings.push_back("UTF-8");
1354 data.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281355 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191356 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1357 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]a048de8a2013-10-02 18:30:061358 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1359
1360 // Do not add the param when InstantExtended is suppressed on SRPs.
[email protected]5c3c6e482014-06-23 09:47:181361 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(false);
[email protected]ce7ee5f2014-06-16 23:41:191362 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1363 search_terms_data_);
[email protected]a048de8a2013-10-02 18:30:061364 EXPECT_EQ("http://www.google.com/?q=foo", result);
1365
1366 // Add the param when InstantExtended is not suppressed on SRPs.
[email protected]5c3c6e482014-06-23 09:47:181367 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true);
[email protected]a048de8a2013-10-02 18:30:061368 search_terms_args.bookmark_bar_pinned = false;
[email protected]ce7ee5f2014-06-16 23:41:191369 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1370 search_terms_data_);
[email protected]a048de8a2013-10-02 18:30:061371 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result);
1372
[email protected]5c3c6e482014-06-23 09:47:181373 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true);
[email protected]a048de8a2013-10-02 18:30:061374 search_terms_args.bookmark_bar_pinned = true;
[email protected]ce7ee5f2014-06-16 23:41:191375 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1376 search_terms_data_);
[email protected]a048de8a2013-10-02 18:30:061377 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result);
1378}
[email protected]9d70de12014-05-10 02:15:311379
[email protected]9d70de12014-05-10 02:15:311380TEST_F(TemplateURLTest, AnswersHasVersion) {
1381 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441382 search_terms_data_.set_google_base_url("http://bar/");
[email protected]9d70de12014-05-10 02:15:311383 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1384
[email protected]168d08722014-06-18 07:13:281385 TemplateURL url(data);
[email protected]9d70de12014-05-10 02:15:311386 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191387 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1388 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311389 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1390
[email protected]5c3c6e482014-06-23 09:47:181391 search_terms_data_.set_enable_answers_in_suggest(true);
[email protected]168d08722014-06-18 07:13:281392 TemplateURL url2(data);
[email protected]ce7ee5f2014-06-16 23:41:191393 result = url2.url_ref().ReplaceSearchTerms(search_terms_args,
1394 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311395 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1396}
[email protected]20184242014-05-14 02:57:421397
1398TEST_F(TemplateURLTest, SessionToken) {
1399 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441400 search_terms_data_.set_google_base_url("http://bar/");
[email protected]20184242014-05-14 02:57:421401 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1402
[email protected]168d08722014-06-18 07:13:281403 TemplateURL url(data);
[email protected]20184242014-05-14 02:57:421404 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1405 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191406 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1407 search_terms_data_);
[email protected]20184242014-05-14 02:57:421408 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1409
[email protected]168d08722014-06-18 07:13:281410 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421411 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191412 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1413 search_terms_data_);
[email protected]20184242014-05-14 02:57:421414 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1415}
[email protected]448b17f52014-06-13 01:08:031416
1417TEST_F(TemplateURLTest, ContextualSearchParameters) {
1418 TemplateURLData data;
[email protected]798baa8e2014-06-20 05:42:441419 search_terms_data_.set_google_base_url("http://bar/");
[email protected]448b17f52014-06-13 01:08:031420 data.SetURL("http://bar/_/contextualsearch?"
1421 "{google:contextualSearchVersion}"
1422 "{google:contextualSearchContextData}");
1423
[email protected]168d08722014-06-18 07:13:281424 TemplateURL url(data);
[email protected]448b17f52014-06-13 01:08:031425 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191426 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1427 search_terms_data_);
[email protected]52e02aa2014-08-06 05:36:091428 EXPECT_EQ("http://bar/_/contextualsearch?ctxsl_resolve=1", result);
[email protected]448b17f52014-06-13 01:08:031429
1430 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
1431 1, 6, 11, "allen", "woody+allen+movies", "www.wikipedia.org",
[email protected]52e02aa2014-08-06 05:36:091432 "utf-8", true);
[email protected]448b17f52014-06-13 01:08:031433 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191434 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1435 search_terms_data_);
[email protected]448b17f52014-06-13 01:08:031436 EXPECT_EQ("http://bar/_/contextualsearch?"
1437 "ctxs=1&"
1438 "ctxs_start=6&"
1439 "ctxs_end=11&"
1440 "q=allen&"
1441 "ctxs_content=woody+allen+movies&"
[email protected]52e02aa2014-08-06 05:36:091442 "ctxsl_url=www.wikipedia.org&"
1443 "ctxs_encoding=utf-8&"
1444 "ctxsl_resolve=1",
1445 result);
[email protected]448b17f52014-06-13 01:08:031446}
[email protected]44ccc9f2014-06-20 17:36:211447
1448TEST_F(TemplateURLTest, GenerateKeyword) {
1449 ASSERT_EQ(ASCIIToUTF16("foo"),
1450 TemplateURL::GenerateKeyword(GURL("http://foo")));
1451 // www. should be stripped.
1452 ASSERT_EQ(ASCIIToUTF16("foo"),
1453 TemplateURL::GenerateKeyword(GURL("http://www.foo")));
1454 // Make sure we don't get a trailing '/'.
1455 ASSERT_EQ(ASCIIToUTF16("blah"),
1456 TemplateURL::GenerateKeyword(GURL("http://blah/")));
1457 // Don't generate the empty string.
1458 ASSERT_EQ(ASCIIToUTF16("www"),
1459 TemplateURL::GenerateKeyword(GURL("http://www.")));
1460}
1461
1462TEST_F(TemplateURLTest, GenerateSearchURL) {
1463 struct GenerateSearchURLCase {
1464 const char* test_name;
1465 const char* url;
1466 const char* expected;
1467 } generate_url_cases[] = {
1468 { "invalid URL", "foo{searchTerms}", "" },
1469 { "URL with no replacements", "http://foo/", "http://foo/" },
1470 { "basic functionality", "http://foo/{searchTerms}",
1471 "http://foo/blah.blah.blah.blah.blah" }
1472 };
1473
viettrungluu37a447b2014-10-16 18:23:271474 for (size_t i = 0; i < arraysize(generate_url_cases); ++i) {
[email protected]44ccc9f2014-06-20 17:36:211475 TemplateURLData data;
1476 data.SetURL(generate_url_cases[i].url);
1477 TemplateURL t_url(data);
1478 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(),
1479 generate_url_cases[i].expected)
1480 << generate_url_cases[i].test_name << " failed.";
1481 }
1482}
[email protected]9e9130ce2014-06-23 23:20:511483
1484TEST_F(TemplateURLTest, PrefetchQueryParameters) {
1485 TemplateURLData data;
1486 search_terms_data_.set_google_base_url("http://bar/");
1487 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t");
1488
1489 TemplateURL url(data);
1490 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1491 search_terms_args.prefetch_query = "full query text";
1492 search_terms_args.prefetch_query_type = "2338";
1493 std::string result =
1494 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1495 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t",
1496 result);
1497
1498 TemplateURL url2(data);
1499 search_terms_args.prefetch_query.clear();
1500 search_terms_args.prefetch_query_type.clear();
1501 result =
1502 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_);
1503 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1504}