blob: 42b55ba210c4423a0749c42854b90baa1818c89b [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]420472b22014-06-10 13:34:4310#include "chrome/browser/autocomplete/autocomplete_input.h"
[email protected]d82443b2009-01-15 19:54:5611#include "chrome/browser/browser_process.h"
12#include "chrome/browser/rlz/rlz.h"
[email protected]d54e03a52009-01-16 00:31:0413#include "chrome/browser/search_engines/template_url.h"
[email protected]eb7c2e22014-06-12 16:26:0714#include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
[email protected]56fa29592013-07-02 20:25:5315#include "chrome/common/chrome_switches.h"
[email protected]420472b22014-06-10 13:34:4316#include "components/metrics/proto/omnibox_input_type.pb.h"
[email protected]d82443b2009-01-15 19:54:5617#include "testing/gtest/include/gtest/gtest.h"
18
[email protected]81d9b72d2012-03-26 22:29:1719#if defined(ENABLE_RLZ)
[email protected]dd3e528a2014-06-06 11:41:1220#include "chrome/browser/google/google_brand.h"
[email protected]81d9b72d2012-03-26 22:29:1721#endif
22
[email protected]fd6d8822012-12-08 06:56:1123#if defined(OS_ANDROID)
[email protected]eb7c2e22014-06-12 16:26:0724#include "chrome/browser/search_engines/ui_thread_search_terms_data_android.h"
[email protected]fd6d8822012-12-08 06:56:1125#endif
26
[email protected]f911df52013-12-24 23:24:2327using base::ASCIIToUTF16;
28
[email protected]b37bdfe2012-03-16 20:53:2729// TestSearchTermsData --------------------------------------------------------
30
[email protected]375bd7312010-08-30 22:18:1331// Simple implementation of SearchTermsData.
32class TestSearchTermsData : public SearchTermsData {
33 public:
[email protected]b37bdfe2012-03-16 20:53:2734 explicit TestSearchTermsData(const std::string& google_base_url);
[email protected]375bd7312010-08-30 22:18:1335
[email protected]b37bdfe2012-03-16 20:53:2736 virtual std::string GoogleBaseURLValue() const OVERRIDE;
[email protected]375bd7312010-08-30 22:18:1337
38 private:
39 std::string google_base_url_;
40
41 DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData);
42};
43
[email protected]b37bdfe2012-03-16 20:53:2744TestSearchTermsData::TestSearchTermsData(const std::string& google_base_url)
[email protected]2767c0fd2013-08-16 17:44:1645 : google_base_url_(google_base_url) {
[email protected]b37bdfe2012-03-16 20:53:2746}
47
48std::string TestSearchTermsData::GoogleBaseURLValue() const {
49 return google_base_url_;
50}
51
[email protected]b37bdfe2012-03-16 20:53:2752// TemplateURLTest ------------------------------------------------------------
53
[email protected]583844c2011-08-27 00:38:3554class TemplateURLTest : public testing::Test {
[email protected]d82443b2009-01-15 19:54:5655 public:
[email protected]ce7ee5f2014-06-16 23:41:1956 TemplateURLTest() : search_terms_data_(NULL) {}
[email protected]b37bdfe2012-03-16 20:53:2757 void CheckSuggestBaseURL(const std::string& base_url,
58 const std::string& base_suggest_url) const;
[email protected]ce7ee5f2014-06-16 23:41:1959
60 UIThreadSearchTermsData search_terms_data_;
[email protected]d82443b2009-01-15 19:54:5661};
62
[email protected]b37bdfe2012-03-16 20:53:2763void TemplateURLTest::CheckSuggestBaseURL(
64 const std::string& base_url,
65 const std::string& base_suggest_url) const {
66 TestSearchTermsData search_terms_data(base_url);
67 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue());
68}
69
70
71// Actual tests ---------------------------------------------------------------
72
[email protected]d82443b2009-01-15 19:54:5673TEST_F(TemplateURLTest, Defaults) {
[email protected]573889f22012-04-07 01:31:5474 TemplateURLData data;
75 EXPECT_FALSE(data.show_in_default_list);
76 EXPECT_FALSE(data.safe_for_autoreplace);
77 EXPECT_EQ(0, data.prepopulate_id);
[email protected]d82443b2009-01-15 19:54:5678}
79
80TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]573889f22012-04-07 01:31:5481 TemplateURLData data;
82 data.SetURL("{searchTerms}");
[email protected]168d08722014-06-18 07:13:2883 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:1984 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:5685}
86
87TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1988 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1989 const char* url;
[email protected]0085863a2013-12-06 21:19:0390 const base::string16 terms;
[email protected]34e24852012-01-31 18:43:5891 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:1992 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:3093 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1694 "http://foosea%20rch/bar" },
[email protected]400b133f2011-01-19 18:32:3095 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1696 "http://foosea%20rch/bar?boo=abc" },
[email protected]400b133f2011-01-19 18:32:3097 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1698 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]400b133f2011-01-19 18:32:3099 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
[email protected]c31a979c2012-05-31 23:57:16100 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:19101 };
102 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
103 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54104 TemplateURLData data;
105 data.SetURL(value.url);
[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(value.terms), search_terms_data_));
[email protected]c31a979c2012-05-31 23:57:16111 ASSERT_TRUE(result.is_valid());
112 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:19113 }
[email protected]d82443b2009-01-15 19:54:56114}
115
116TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:54117 TemplateURLData data;
118 data.SetURL("http://foo{searchTerms}{count?}");
[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_));
121 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04122 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19123 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56124 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27125 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:56126}
127
128TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:54129 TemplateURLData data;
130 data.SetURL("http://foo{searchTerms}{count}");
[email protected]168d08722014-06-18 07:13:28131 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19132 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
133 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04134 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19135 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56136 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27137 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56138}
139
140TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54141 TemplateURLData data;
142 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]168d08722014-06-18 07:13:28143 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19144 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
145 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04146 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19147 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56148 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27149 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56150}
151
152TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54153 TemplateURLData data;
154 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[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]405aae22012-03-29 20:36:13161 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56162}
163
164TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54165 TemplateURLData data;
166 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[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://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56174}
175
[email protected]93b29062013-07-12 03:09:09176TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
177 const char kInvalidPostParamsString[] =
178 "unknown_template={UnknownTemplate},bad_value=bad{value},"
179 "{google:sbiSource}";
180 // List all accpectable parameter format in valid_post_params_string. it is
181 // expected like: "name0=,name1=value1,name2={template1}"
182 const char kValidPostParamsString[] =
183 "image_content={google:imageThumbnail},image_url={google:imageURL},"
184 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
[email protected]2f3bc6512013-08-28 03:56:27185 "constant_param=constant,width={google:imageOriginalWidth}";
[email protected]93b29062013-07-12 03:09:09186 const char KImageSearchURL[] = "http://foo.com/sbi";
187
188 TemplateURLData data;
189 data.image_url = KImageSearchURL;
190
191 // Try to parse invalid post parameters.
192 data.image_url_post_params = kInvalidPostParamsString;
[email protected]168d08722014-06-18 07:13:28193 TemplateURL url_bad(data);
[email protected]ce7ee5f2014-06-16 23:41:19194 ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09195 const TemplateURLRef::PostParams& bad_post_params =
196 url_bad.image_url_ref().post_params_;
197 ASSERT_EQ(2U, bad_post_params.size());
198 EXPECT_EQ("unknown_template", bad_post_params[0].first);
199 EXPECT_EQ("{UnknownTemplate}", bad_post_params[0].second);
200 EXPECT_EQ("bad_value", bad_post_params[1].first);
201 EXPECT_EQ("bad{value}", bad_post_params[1].second);
202
203 // Try to parse valid post parameters.
204 data.image_url_post_params = kValidPostParamsString;
[email protected]168d08722014-06-18 07:13:28205 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19206 ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_));
207 ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_));
[email protected]93b29062013-07-12 03:09:09208
209 // Check term replacement.
210 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
211 search_args.image_thumbnail_content = "dummy-image-thumbnail";
212 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
[email protected]2f3bc6512013-08-28 03:56:27213 search_args.image_original_size = gfx::Size(10, 10);
[email protected]93b29062013-07-12 03:09:09214 // Replacement operation with no post_data buffer should still return
215 // the parsed URL.
[email protected]ce7ee5f2014-06-16 23:41:19216 TestSearchTermsData search_terms_data("http://X");
217 GURL result(url.image_url_ref().ReplaceSearchTerms(
218 search_args, search_terms_data));
[email protected]93b29062013-07-12 03:09:09219 ASSERT_TRUE(result.is_valid());
220 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48221 TemplateURLRef::PostContent post_content;
[email protected]ce7ee5f2014-06-16 23:41:19222 result = GURL(url.image_url_ref().ReplaceSearchTerms(
[email protected]7c2bcc42013-08-01 04:03:48223 search_args, search_terms_data, &post_content));
[email protected]93b29062013-07-12 03:09:09224 ASSERT_TRUE(result.is_valid());
225 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48226 ASSERT_FALSE(post_content.first.empty());
227 ASSERT_FALSE(post_content.second.empty());
[email protected]93b29062013-07-12 03:09:09228
229 // Check parsed result of post parameters.
230 const TemplateURLRef::Replacements& replacements =
231 url.image_url_ref().replacements_;
232 const TemplateURLRef::PostParams& post_params =
233 url.image_url_ref().post_params_;
[email protected]2f3bc6512013-08-28 03:56:27234 EXPECT_EQ(7U, post_params.size());
[email protected]93b29062013-07-12 03:09:09235 for (TemplateURLRef::PostParams::const_iterator i = post_params.begin();
236 i != post_params.end(); ++i) {
237 TemplateURLRef::Replacements::const_iterator j = replacements.begin();
238 for (; j != replacements.end(); ++j) {
239 if (j->is_post_param && j->index ==
240 static_cast<size_t>(i - post_params.begin())) {
241 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27242 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
243 EXPECT_EQ("width", i->first);
244 EXPECT_EQ(
245 base::IntToString(search_args.image_original_size.width()),
246 i->second);
247 break;
[email protected]93b29062013-07-12 03:09:09248 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
249 EXPECT_EQ("image_content", i->first);
250 EXPECT_EQ(search_args.image_thumbnail_content, i->second);
251 break;
252 case TemplateURLRef::GOOGLE_IMAGE_URL:
253 EXPECT_EQ("image_url", i->first);
254 EXPECT_EQ(search_args.image_url.spec(), i->second);
255 break;
256 case TemplateURLRef::LANGUAGE:
257 EXPECT_EQ("language", i->first);
258 EXPECT_EQ("en", i->second);
259 break;
260 default:
261 ADD_FAILURE(); // Should never go here.
262 }
263 break;
264 }
265 }
266 if (j != replacements.end())
267 continue;
268 if (i->first == "empty_param") {
269 EXPECT_TRUE(i->second.empty());
270 } else if (i->first == "sbisrc") {
[email protected]7c2bcc42013-08-01 04:03:48271 EXPECT_FALSE(i->second.empty());
[email protected]93b29062013-07-12 03:09:09272 } else {
273 EXPECT_EQ("constant_param", i->first);
274 EXPECT_EQ("constant", i->second);
275 }
276 }
277}
278
[email protected]d88cb202011-08-17 20:03:01279// Test that setting the prepopulate ID from TemplateURL causes the stored
280// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44281TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54282 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20283 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]168d08722014-06-18 07:13:28284 TemplateURL url(data);
[email protected]1a257262011-06-28 22:15:44285 TemplateURLRef::Replacements replacements;
286 bool valid = false;
[email protected]243abf32012-05-15 18:28:20287 EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL(
[email protected]93b29062013-07-12 03:09:09288 "http://foo{fhqwhgads}bar", &replacements, NULL, &valid));
[email protected]1a257262011-06-28 22:15:44289 EXPECT_TRUE(replacements.empty());
290 EXPECT_TRUE(valid);
291
[email protected]573889f22012-04-07 01:31:54292 data.prepopulate_id = 123;
[email protected]168d08722014-06-18 07:13:28293 TemplateURL url2(data);
[email protected]243abf32012-05-15 18:28:20294 EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar",
[email protected]93b29062013-07-12 03:09:09295 &replacements, NULL,
296 &valid));
[email protected]1a257262011-06-28 22:15:44297 EXPECT_TRUE(replacements.empty());
298 EXPECT_TRUE(valid);
299}
300
[email protected]d82443b2009-01-15 19:54:56301TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54302 TemplateURLData data;
303 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]168d08722014-06-18 07:13:28304 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19305 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
306 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04307 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19308 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56309 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27310 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56311}
312
313TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54314 TemplateURLData data;
315 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]168d08722014-06-18 07:13:28316 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19317 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
318 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04319 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19320 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56321 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27322 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56323}
324
[email protected]375bd7312010-08-30 22:18:13325TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
326 struct SearchTermsCase {
327 const char* url;
[email protected]0085863a2013-12-06 21:19:03328 const base::string16 terms;
[email protected]375bd7312010-08-30 22:18:13329 const char* output;
330 } search_term_cases[] = {
[email protected]0085863a2013-12-06 21:19:03331 { "{google:baseURL}{language}{searchTerms}", base::string16(),
[email protected]b37bdfe2012-03-16 20:53:27332 "http://example.com/e/en" },
[email protected]0085863a2013-12-06 21:19:03333 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
[email protected]014010e2011-10-01 04:12:44334 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13335 };
336
337 TestSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54338 TemplateURLData data;
[email protected]375bd7312010-08-30 22:18:13339 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
340 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54341 data.SetURL(value.url);
[email protected]168d08722014-06-18 07:13:28342 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19343 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data));
344 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data));
345 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]93b29062013-07-12 03:09:09346 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL));
[email protected]375bd7312010-08-30 22:18:13347 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27348 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13349 }
350}
351
[email protected]d82443b2009-01-15 19:54:56352TEST_F(TemplateURLTest, URLRefTermToWide) {
353 struct ToWideCase {
354 const char* encoded_search_term;
[email protected]0085863a2013-12-06 21:19:03355 const base::string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56356 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30357 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56358 // Test some big-5 input.
[email protected]f911df52013-12-24 23:24:23359 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56360 // Test some UTF-8 input. We should fall back to this when the encoding
361 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
362 // Big-5 trailing byte.
[email protected]f911df52013-12-24 23:24:23363 {"%e4%bd%a05%e5%a5%bd+to+you",
364 base::WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56365 // Undecodable input should stay escaped.
[email protected]f911df52013-12-24 23:24:23366 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44367 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30368 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44369 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30370 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56371 };
372
[email protected]d82443b2009-01-15 19:54:56373 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54374 TemplateURLData data;
375 data.SetURL("http://foo?q={searchTerms}");
376 data.input_encodings.push_back("big-5");
[email protected]168d08722014-06-18 07:13:28377 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19378 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
379 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]f63ae312009-02-04 17:58:46380 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07381 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13382 url.url_ref().SearchTermToString16(
383 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56384 }
385}
386
[email protected]d82443b2009-01-15 19:54:56387TEST_F(TemplateURLTest, DisplayURLToURLRef) {
388 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19389 const std::string url;
[email protected]0085863a2013-12-06 21:19:03390 const base::string16 expected_result;
[email protected]b37bdfe2012-03-16 20:53:27391 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19392 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30393 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19394 { "http://X",
[email protected]400b133f2011-01-19 18:32:30395 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19396 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30397 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19398 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30399 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56400 };
[email protected]573889f22012-04-07 01:31:54401 TemplateURLData data;
[email protected]b37bdfe2012-03-16 20:53:27402 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54403 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28404 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19405 EXPECT_EQ(test_data[i].expected_result,
406 url.url_ref().DisplayURL(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27407 EXPECT_EQ(test_data[i].url,
[email protected]ce7ee5f2014-06-16 23:41:19408 TemplateURLRef::DisplayURLToURLRef(
409 url.url_ref().DisplayURL(search_terms_data_)));
[email protected]d82443b2009-01-15 19:54:56410 }
411}
412
413TEST_F(TemplateURLTest, ReplaceSearchTerms) {
414 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19415 const std::string url;
[email protected]d82443b2009-01-15 19:54:56416 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27417 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19418 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56419 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19420 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56421 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19422 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56423 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19424 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56425 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19426 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56427 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19428 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56429 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19430 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56431 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19432 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56433 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19434 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56435 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19436 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56437 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19438 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56439 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19440 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56441 "http://foo/UTF-8a{language}aXa" },
442 };
[email protected]573889f22012-04-07 01:31:54443 TemplateURLData data;
444 data.input_encodings.push_back("UTF-8");
[email protected]b37bdfe2012-03-16 20:53:27445 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54446 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28447 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19448 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
449 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27450 std::string expected_result = test_data[i].expected_result;
[email protected]d82443b2009-01-15 19:54:56451 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}",
[email protected]d70539de2009-06-24 22:17:06452 g_browser_process->GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04453 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19454 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")),
455 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27456 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56457 EXPECT_EQ(expected_result, result.spec());
458 }
459}
460
461
462// Tests replacing search terms in various encodings and making sure the
463// generated URL matches the expected value.
464TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
465 struct TestData {
466 const std::string encoding;
[email protected]0085863a2013-12-06 21:19:03467 const base::string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19468 const std::string url;
[email protected]d82443b2009-01-15 19:54:56469 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27470 } test_data[] = {
[email protected]f911df52013-12-24 23:24:23471 { "BIG5", base::WideToUTF16(L"\x60BD"),
[email protected]400b133f2011-01-19 18:32:30472 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17473 "http://foo/?%B1~BIG5" },
[email protected]400b133f2011-01-19 18:32:30474 { "UTF-8", ASCIIToUTF16("blah"),
475 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17476 "http://foo/?blahUTF-8" },
[email protected]f911df52013-12-24 23:24:23477 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
[email protected]34e24852012-01-31 18:43:58478 "http://foo/{searchTerms}/bar",
479 "http://foo/%82%A0/bar"},
[email protected]f911df52013-12-24 23:24:23480 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
[email protected]34e24852012-01-31 18:43:58481 "http://foo/{searchTerms}/bar",
482 "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56483 };
[email protected]573889f22012-04-07 01:31:54484 TemplateURLData data;
[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);
487 data.input_encodings.clear();
488 data.input_encodings.push_back(test_data[i].encoding);
[email protected]168d08722014-06-18 07:13:28489 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19490 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
491 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04492 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19493 TemplateURLRef::SearchTermsArgs(test_data[i].search_term),
494 search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04495 ASSERT_TRUE(result.is_valid());
496 EXPECT_EQ(test_data[i].expected_result, result.spec());
497 }
498}
499
500// Tests replacing assisted query stats (AQS) in various scenarios.
501TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
502 struct TestData {
[email protected]0085863a2013-12-06 21:19:03503 const base::string16 search_term;
[email protected]bca359b2012-06-24 07:53:04504 const std::string aqs;
505 const std::string base_url;
506 const std::string url;
507 const std::string expected_result;
508 } test_data[] = {
509 // No HTTPS, no AQS.
510 { ASCIIToUTF16("foo"),
511 "chrome.0.0l6",
512 "http://foo/",
513 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
514 "http://foo/?foo" },
515 // HTTPS available, AQS should be replaced.
516 { ASCIIToUTF16("foo"),
517 "chrome.0.0l6",
518 "https://foo/",
519 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
520 "https://foo/?fooaqs=chrome.0.0l6&" },
521 // HTTPS available, however AQS is empty.
522 { ASCIIToUTF16("foo"),
523 "",
524 "https://foo/",
525 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
526 "https://foo/?foo" },
527 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
528 { ASCIIToUTF16("foo"),
529 "chrome.0.0l6",
530 "",
531 "http://foo?{searchTerms}{google:assistedQueryStats}",
532 "http://foo/?foo" },
533 // A non-Google search provider with HTTPS should allow AQS.
534 { ASCIIToUTF16("foo"),
535 "chrome.0.0l6",
536 "",
537 "https://foo?{searchTerms}{google:assistedQueryStats}",
538 "https://foo/?fooaqs=chrome.0.0l6&" },
539 };
540 TemplateURLData data;
541 data.input_encodings.push_back("UTF-8");
542 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
543 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28544 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19545 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
546 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04547 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
548 search_terms_args.assisted_query_stats = test_data[i].aqs;
549 UIThreadSearchTermsData::SetGoogleBaseURL(test_data[i].base_url);
[email protected]ce7ee5f2014-06-16 23:41:19550 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
551 search_terms_data_));
[email protected]b37bdfe2012-03-16 20:53:27552 ASSERT_TRUE(result.is_valid());
553 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56554 }
555}
556
[email protected]00790562012-12-14 09:57:16557// Tests replacing cursor position.
558TEST_F(TemplateURLTest, ReplaceCursorPosition) {
559 struct TestData {
[email protected]0085863a2013-12-06 21:19:03560 const base::string16 search_term;
[email protected]00790562012-12-14 09:57:16561 size_t cursor_position;
562 const std::string url;
563 const std::string expected_result;
564 } test_data[] = {
565 { ASCIIToUTF16("foo"),
[email protected]0085863a2013-12-06 21:19:03566 base::string16::npos,
[email protected]00790562012-12-14 09:57:16567 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
568 "http://www.google.com/?foo&" },
569 { ASCIIToUTF16("foo"),
570 2,
571 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
572 "http://www.google.com/?foo&cp=2&" },
573 { ASCIIToUTF16("foo"),
574 15,
575 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
576 "http://www.google.com/?foo&cp=15&" },
577 };
[email protected]30e91c72014-05-03 00:02:00578 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
[email protected]00790562012-12-14 09:57:16579 TemplateURLData data;
580 data.input_encodings.push_back("UTF-8");
581 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
582 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28583 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19584 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
585 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]00790562012-12-14 09:57:16586 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
587 search_terms_args.cursor_position = test_data[i].cursor_position;
[email protected]ce7ee5f2014-06-16 23:41:19588 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
589 search_terms_data_));
[email protected]00790562012-12-14 09:57:16590 ASSERT_TRUE(result.is_valid());
591 EXPECT_EQ(test_data[i].expected_result, result.spec());
592 }
593}
594
[email protected]420472b22014-06-10 13:34:43595// Tests replacing input type (&oit=).
596TEST_F(TemplateURLTest, ReplaceInputType) {
597 struct TestData {
598 const base::string16 search_term;
599 AutocompleteInput::Type input_type;
600 const std::string url;
601 const std::string expected_result;
602 } test_data[] = {
603 { ASCIIToUTF16("foo"),
604 metrics::OmniboxInputType::UNKNOWN,
605 "{google:baseURL}?{searchTerms}&{google:inputType}",
606 "http://www.google.com/?foo&oit=1&" },
607 { ASCIIToUTF16("foo"),
608 metrics::OmniboxInputType::URL,
609 "{google:baseURL}?{searchTerms}&{google:inputType}",
610 "http://www.google.com/?foo&oit=3&" },
611 { ASCIIToUTF16("foo"),
612 metrics::OmniboxInputType::FORCED_QUERY,
613 "{google:baseURL}?{searchTerms}&{google:inputType}",
614 "http://www.google.com/?foo&oit=5&" },
615 };
616 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
617 TemplateURLData data;
618 data.input_encodings.push_back("UTF-8");
619 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
620 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28621 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19622 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
623 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]420472b22014-06-10 13:34:43624 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
625 search_terms_args.input_type = test_data[i].input_type;
[email protected]ce7ee5f2014-06-16 23:41:19626 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
627 search_terms_data_));
[email protected]420472b22014-06-10 13:34:43628 ASSERT_TRUE(result.is_valid());
629 EXPECT_EQ(test_data[i].expected_result, result.spec());
630 }
631}
632
[email protected]9b9fa672013-11-07 06:04:52633// Tests replacing currentPageUrl.
634TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48635 struct TestData {
[email protected]0085863a2013-12-06 21:19:03636 const base::string16 search_term;
[email protected]9b9fa672013-11-07 06:04:52637 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48638 const std::string url;
639 const std::string expected_result;
640 } test_data[] = {
641 { ASCIIToUTF16("foo"),
642 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52643 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48644 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
645 { ASCIIToUTF16("foo"),
646 "",
[email protected]9b9fa672013-11-07 06:04:52647 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48648 "http://www.google.com/?foo&" },
649 { ASCIIToUTF16("foo"),
650 "http://g.com/+-/*&=",
[email protected]9b9fa672013-11-07 06:04:52651 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48652 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
653 };
[email protected]30e91c72014-05-03 00:02:00654 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
[email protected]800569d2013-05-06 07:38:48655 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]800569d2013-05-06 07:38:48662 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52663 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]ce7ee5f2014-06-16 23:41:19664 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
665 search_terms_data_));
[email protected]800569d2013-05-06 07:38:48666 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_));
[email protected]b37bdfe2012-03-16 20:53:27695 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(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]0085863a2013-12-06 21:19:03709 base::string16 rlz_string;
[email protected]81d9b72d2012-03-26 22:29:17710#if defined(ENABLE_RLZ)
711 std::string brand;
[email protected]dd3e528a2014-06-06 11:41:12712 if (google_brand::GetBrand(&brand) && !brand.empty() &&
713 !google_brand::IsOrganic(brand)) {
[email protected]e24bcb42014-05-22 06:47:42714 RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz_string);
[email protected]81d9b72d2012-03-26 22:29:17715 }
[email protected]fd6d8822012-12-08 06:56:11716#elif defined(OS_ANDROID)
717 SearchTermsDataAndroid::rlz_parameter_value_.Get() =
718 ASCIIToUTF16("android_test");
719 rlz_string = SearchTermsDataAndroid::rlz_parameter_value_.Get();
[email protected]58b64332010-08-13 16:09:39720#endif
[email protected]d82443b2009-01-15 19:54:56721
[email protected]573889f22012-04-07 01:31:54722 TemplateURLData data;
723 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]168d08722014-06-18 07:13:28724 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19725 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
726 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]bca359b2012-06-24 07:53:04727 GURL result(url.url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19728 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56729 ASSERT_TRUE(result.is_valid());
[email protected]12bd05872009-03-17 19:25:06730 std::string expected_url = "http://bar/?";
[email protected]b37bdfe2012-03-16 20:53:27731 if (!rlz_string.empty())
[email protected]f911df52013-12-24 23:24:23732 expected_url += "rlz=" + base::UTF16ToUTF8(rlz_string) + "&";
[email protected]12bd05872009-03-17 19:25:06733 expected_url += "x";
[email protected]b37bdfe2012-03-16 20:53:27734 EXPECT_EQ(expected_url, result.spec());
[email protected]d82443b2009-01-15 19:54:56735}
736
[email protected]c8ccc41d2014-04-10 04:42:12737#if !defined(OS_ANDROID) && !defined(OS_IOS)
738TEST_F(TemplateURLTest, RLZFromAppList) {
739 base::string16 rlz_string;
740#if defined(ENABLE_RLZ)
741 std::string brand;
[email protected]dd3e528a2014-06-06 11:41:12742 if (google_brand::GetBrand(&brand) && !brand.empty() &&
743 !google_brand::IsOrganic(brand)) {
[email protected]e24bcb42014-05-22 06:47:42744 RLZTracker::GetAccessPointRlz(RLZTracker::ChromeAppList(), &rlz_string);
[email protected]c8ccc41d2014-04-10 04:42:12745 }
746#endif
747
748 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]c8ccc41d2014-04-10 04:42:12753 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
754 args.from_app_list = true;
[email protected]ce7ee5f2014-06-16 23:41:19755 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_));
[email protected]c8ccc41d2014-04-10 04:42:12756 ASSERT_TRUE(result.is_valid());
757 std::string expected_url = "http://bar/?";
758 if (!rlz_string.empty())
759 expected_url += "rlz=" + base::UTF16ToUTF8(rlz_string) + "&";
760 expected_url += "x";
761 EXPECT_EQ(expected_url, result.spec());
762}
763#endif
764
[email protected]d82443b2009-01-15 19:54:56765TEST_F(TemplateURLTest, HostAndSearchTermKey) {
766 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19767 const std::string url;
[email protected]d82443b2009-01-15 19:54:56768 const std::string host;
769 const std::string path;
770 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54771 } test_data[] = {
[email protected]7c60f5042013-02-14 03:39:32772 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56773
774 // No query key should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32775 { "http://blah/{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56776
777 // No term should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32778 { "http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56779
780 // Multiple terms should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32781 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56782
783 // Term in the host shouldn't match.
[email protected]7c60f5042013-02-14 03:39:32784 { "http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56785
[email protected]7c60f5042013-02-14 03:39:32786 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
787 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56788
789 // Single term with extra chars in value should match.
[email protected]7c60f5042013-02-14 03:39:32790 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56791 };
792
[email protected]573889f22012-04-07 01:31:54793 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
794 TemplateURLData data;
795 data.SetURL(test_data[i].url);
[email protected]168d08722014-06-18 07:13:28796 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19797 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
798 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
799 EXPECT_EQ(test_data[i].search_term_key,
800 url.url_ref().GetSearchTermKey(search_terms_data_));
[email protected]d82443b2009-01-15 19:54:56801 }
802}
803
804TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
805 static const struct {
[email protected]ddd231e2010-06-29 20:35:19806 const char* const base_url;
807 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56808 } data[] = {
[email protected]014010e2011-10-01 04:12:44809 { "http://google.com/", "http://google.com/complete/", },
810 { "http://www.google.com/", "http://www.google.com/complete/", },
811 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
812 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
813 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56814 };
815
[email protected]f63ae312009-02-04 17:58:46816 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i)
[email protected]d82443b2009-01-15 19:54:56817 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
818}
819
[email protected]81c6ef62010-01-21 09:58:47820TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19821 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:54822 TemplateURLData data;
823 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28824 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47825 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:13826 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19827 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47828 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27829 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47830 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
831}
832
833TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:20834 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:54835 TemplateURLData data;
836 data.SetURL(parsed_url);
[email protected]168d08722014-06-18 07:13:28837 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47838 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:44839
840 // By default, TemplateURLRef should not consider itself prepopulated.
841 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:13842 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20843 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:44844 EXPECT_TRUE(replacements.empty());
845
846 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:20847 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:54848 data.prepopulate_id = 1;
[email protected]168d08722014-06-18 07:13:28849 TemplateURL url2(data);
[email protected]495c30b2012-05-15 18:48:15850 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20851 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47852 EXPECT_TRUE(replacements.empty());
853}
854
855TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]168d08722014-06-18 07:13:28856 TemplateURL url((TemplateURLData()));
[email protected]81c6ef62010-01-21 09:58:47857 TemplateURLRef::Replacements replacements;
858 bool valid = false;
[email protected]b37bdfe2012-03-16 20:53:27859 EXPECT_EQ(std::string(),
[email protected]93b29062013-07-12 03:09:09860 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47861 EXPECT_TRUE(replacements.empty());
862 EXPECT_TRUE(valid);
863}
864
865TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:54866 TemplateURLData data;
867 data.SetURL("{");
[email protected]168d08722014-06-18 07:13:28868 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47869 TemplateURLRef::Replacements replacements;
870 bool valid = false;
[email protected]93b29062013-07-12 03:09:09871 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
872 &valid));
[email protected]81c6ef62010-01-21 09:58:47873 EXPECT_TRUE(replacements.empty());
874 EXPECT_FALSE(valid);
875}
876
877TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:54878 TemplateURLData data;
879 data.SetURL("{}");
[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]93b29062013-07-12 03:09:09883 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47884 EXPECT_TRUE(replacements.empty());
885 EXPECT_TRUE(valid);
886}
887
888TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:54889 TemplateURLData data;
890 data.SetURL("{}{{%s}}");
[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]ddd231e2010-06-29 20:35:19894 EXPECT_EQ("{}{}",
[email protected]93b29062013-07-12 03:09:09895 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
896 &valid));
[email protected]81c6ef62010-01-21 09:58:47897 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27898 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47899 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
900 EXPECT_TRUE(valid);
901}
902
903TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:54904 TemplateURLData data;
905 data.SetURL("{%s");
[email protected]168d08722014-06-18 07:13:28906 TemplateURL url(data);
[email protected]81c6ef62010-01-21 09:58:47907 TemplateURLRef::Replacements replacements;
908 bool valid = false;
[email protected]360ba052012-04-04 17:26:13909 EXPECT_EQ("{",
[email protected]93b29062013-07-12 03:09:09910 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
911 &valid));
[email protected]81c6ef62010-01-21 09:58:47912 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27913 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47914 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
915 EXPECT_TRUE(valid);
916}
[email protected]5b3078752012-10-09 18:54:16917
[email protected]fd6d8822012-12-08 06:56:11918#if defined(OS_ANDROID)
919TEST_F(TemplateURLTest, SearchClient) {
920 const std::string base_url_str("http://google.com/?");
921 const std::string terms_str("{searchTerms}&{google:searchClient}");
922 const std::string full_url_str = base_url_str + terms_str;
[email protected]0085863a2013-12-06 21:19:03923 const base::string16 terms(ASCIIToUTF16(terms_str));
[email protected]fd6d8822012-12-08 06:56:11924 UIThreadSearchTermsData::SetGoogleBaseURL(base_url_str);
925
926 TemplateURLData data;
927 data.SetURL(full_url_str);
[email protected]168d08722014-06-18 07:13:28928 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:19929 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
930 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11931 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
932
933 // Check that the URL is correct when a client is not present.
[email protected]ce7ee5f2014-06-16 23:41:19934 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args,
935 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11936 ASSERT_TRUE(result.is_valid());
937 EXPECT_EQ("http://google.com/?foobar&", result.spec());
938
939 // Check that the URL is correct when a client is present.
940 SearchTermsDataAndroid::search_client_.Get() = "android_test";
[email protected]ce7ee5f2014-06-16 23:41:19941 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args,
942 search_terms_data_));
[email protected]fd6d8822012-12-08 06:56:11943 ASSERT_TRUE(result_2.is_valid());
944 EXPECT_EQ("http://google.com/?foobar&client=android_test&", result_2.spec());
945}
946#endif
947
[email protected]5b3078752012-10-09 18:54:16948TEST_F(TemplateURLTest, GetURLNoInstantURL) {
949 TemplateURLData data;
950 data.SetURL("http://google.com/?q={searchTerms}");
951 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
952 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
953 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:28954 TemplateURL url(data);
[email protected]5b3078752012-10-09 18:54:16955 ASSERT_EQ(3U, url.URLCount());
956 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
957 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
958 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
959}
960
961TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
962 TemplateURLData data;
963 data.SetURL("http://google.com/?q={searchTerms}");
964 data.instant_url = "http://google.com/instant#q={searchTerms}";
965 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
966 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:28967 TemplateURL url(data);
[email protected]5b3078752012-10-09 18:54:16968 ASSERT_EQ(3U, url.URLCount());
969 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
970 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
971 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
972}
973
974TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
975 TemplateURLData data;
976 data.SetURL("http://www.google.co.uk/");
[email protected]168d08722014-06-18 07:13:28977 TemplateURL url(data);
[email protected]5b3078752012-10-09 18:54:16978 ASSERT_EQ(1U, url.URLCount());
979 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0));
980}
981
982TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
983 TemplateURLData data;
984 data.SetURL("http://google.com/?q={searchTerms}");
985 data.instant_url = "http://google.com/instant#q={searchTerms}";
986 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
987 data.alternate_urls.push_back(
988 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:28989 TemplateURL url(data);
[email protected]0085863a2013-12-06 21:19:03990 base::string16 result;
[email protected]5b3078752012-10-09 18:54:16991
992 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:19993 GURL("http://google.com/?q=something"), search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:19994 EXPECT_EQ(ASCIIToUTF16("something"), result);
995
996 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:19997 GURL("http://google.com/?espv&q=something"),
998 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:19999 EXPECT_EQ(ASCIIToUTF16("something"), result);
1000
1001 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191002 GURL("http://google.com/?espv=1&q=something"),
1003 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191004 EXPECT_EQ(ASCIIToUTF16("something"), result);
1005
1006 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191007 GURL("http://google.com/?espv=0&q=something"),
1008 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191009 EXPECT_EQ(ASCIIToUTF16("something"), result);
1010
1011 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191012 GURL("http://google.com/alt/#q=something"),
1013 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191014 EXPECT_EQ(ASCIIToUTF16("something"), result);
1015
1016 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191017 GURL("http://google.com/alt/#espv&q=something"),
1018 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191019 EXPECT_EQ(ASCIIToUTF16("something"), result);
1020
1021 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191022 GURL("http://google.com/alt/#espv=1&q=something"),
1023 search_terms_data_, &result));
[email protected]4076ea62013-01-09 01:47:191024 EXPECT_EQ(ASCIIToUTF16("something"), result);
1025
1026 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191027 GURL("http://google.com/alt/#espv=0&q=something"),
1028 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161029 EXPECT_EQ(ASCIIToUTF16("something"), result);
1030
1031 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191032 GURL("http://google.ca/?q=something"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371033 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161034
1035 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191036 GURL("http://google.ca/?q=something&q=anything"),
1037 search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371038 EXPECT_EQ(base::string16(), result);
[email protected]67d8b752013-04-03 17:33:271039
1040 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191041 GURL("http://google.com/foo/?q=foo"), 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
[email protected]32e2d81b2012-10-16 19:03:251044 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191045 GURL("https://google.com/?q=foo"), search_terms_data_, &result));
[email protected]32e2d81b2012-10-16 19:03:251046 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:161047
1048 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191049 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371050 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161051
1052 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191053 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161054 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
1055
1056 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191057 GURL("http://google.com/alt/?q=123#q=456"),
1058 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161059 EXPECT_EQ(ASCIIToUTF16("456"), result);
1060
1061 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191062 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"),
1063 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161064 EXPECT_EQ(ASCIIToUTF16("123"), result);
1065
1066 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
[email protected]ce7ee5f2014-06-16 23:41:191067 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1068 search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161069 EXPECT_EQ(ASCIIToUTF16("789"), result);
1070
1071 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191072 GURL("http://google.com/alt/?q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371073 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161074
1075 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191076 GURL("http://google.com/alt/?#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371077 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161078
1079 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191080 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371081 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161082
1083 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191084 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result));
[email protected]b959d7d42013-12-13 17:26:371085 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161086
1087 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]ce7ee5f2014-06-16 23:41:191088 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result));
[email protected]5b3078752012-10-09 18:54:161089 EXPECT_EQ(ASCIIToUTF16("123"), result);
1090}
[email protected]4076ea62013-01-09 01:47:191091
1092TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
1093 TemplateURLData data;
1094 data.SetURL("http://google.com/?q={searchTerms}");
1095 data.instant_url = "http://google.com/instant#q={searchTerms}";
1096 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1097 data.alternate_urls.push_back(
1098 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1099 data.search_terms_replacement_key = "espv";
[email protected]168d08722014-06-18 07:13:281100 TemplateURL url(data);
[email protected]4076ea62013-01-09 01:47:191101
1102 // Test with instant enabled required.
1103 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1104 GURL("http://google.com/")));
1105
1106 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1107 GURL("http://google.com/?espv")));
1108
1109 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1110 GURL("http://google.com/#espv")));
1111
1112 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1113 GURL("http://google.com/?q=something")));
1114
1115 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1116 GURL("http://google.com/?q=something&espv")));
1117
1118 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1119 GURL("http://google.com/?q=something&espv=1")));
1120
1121 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1122 GURL("http://google.com/?q=something&espv=0")));
1123
1124 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1125 GURL("http://google.com/?espv&q=something")));
1126
1127 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1128 GURL("http://google.com/?espv=1&q=something")));
1129
1130 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1131 GURL("http://google.com/?espv=0&q=something")));
1132
1133 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1134 GURL("http://google.com/alt/#q=something")));
1135
1136 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1137 GURL("http://google.com/alt/#q=something&espv")));
1138
1139 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1140 GURL("http://google.com/alt/#q=something&espv=1")));
1141
1142 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1143 GURL("http://google.com/alt/#q=something&espv=0")));
1144
1145 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1146 GURL("http://google.com/alt/#espv&q=something")));
1147
1148 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1149 GURL("http://google.com/alt/#espv=1&q=something")));
1150
1151 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1152 GURL("http://google.com/alt/#espv=0&q=something")));
1153
1154 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1155 GURL("http://google.com/?espv#q=something")));
1156
1157 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1158 GURL("http://google.com/?espv=1#q=something")));
1159
1160 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1161 GURL("http://google.com/?q=something#espv")));
1162
1163 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1164 GURL("http://google.com/?q=something#espv=1")));
1165
1166 // This does not ensure the domain matches.
1167 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1168 GURL("http://bing.com/?espv")));
1169
1170 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1171 GURL("http://bing.com/#espv")));
1172}
[email protected]f62e30f52013-03-23 03:45:151173
1174TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1175 TemplateURLData data;
1176 data.SetURL("http://google.com/?q={searchTerms}");
1177 data.instant_url = "http://google.com/instant#q={searchTerms}";
1178 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1179 data.alternate_urls.push_back(
1180 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
[email protected]168d08722014-06-18 07:13:281181 TemplateURL url(data);
[email protected]f62e30f52013-03-23 03:45:151182 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1183 GURL result;
1184
1185 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191186 GURL("http://google.com/?q=something"), search_terms,
1187 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151188 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result);
1189
1190 result = GURL("http://should.not.change.com");
1191 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191192 GURL("http://google.ca/?q=something"), search_terms,
1193 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151194 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1195
1196 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191197 GURL("http://google.com/foo/?q=foo"), search_terms,
1198 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151199
1200 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191201 GURL("https://google.com/?q=foo"), search_terms,
1202 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151203 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result);
1204
1205 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191206 GURL("http://google.com:8080/?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("http://google.com/?q=1+2+3&b=456"), search_terms,
1211 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151212 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result);
1213
1214 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1215 // template_url.cc for details.
1216 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191217 GURL("http://google.com/alt/?q=123#q=456"), search_terms,
1218 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151219 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result);
1220
1221 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1222 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
[email protected]ce7ee5f2014-06-16 23:41:191223 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151224 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"),
1225 result);
1226
1227 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1228 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
[email protected]ce7ee5f2014-06-16 23:41:191229 search_terms, search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151230 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1231 "#j=abc&q=Bob Morane&h=def9"), result);
1232
1233 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191234 GURL("http://google.com/alt/?q="), search_terms,
1235 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151236
1237 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191238 GURL("http://google.com/alt/?#q="), search_terms,
1239 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151240
1241 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191242 GURL("http://google.com/alt/?q=#q="), search_terms,
1243 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151244
1245 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191246 GURL("http://google.com/alt/?q=123#q="), search_terms,
1247 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151248
1249 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
[email protected]ce7ee5f2014-06-16 23:41:191250 GURL("http://google.com/alt/?q=#q=123"), search_terms,
1251 search_terms_data_, &result));
[email protected]f62e30f52013-03-23 03:45:151252 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result);
1253}
[email protected]56fa29592013-07-02 20:25:531254
[email protected]621ade062013-10-28 06:27:431255// Test the |suggest_query_params| field of SearchTermsArgs.
1256TEST_F(TemplateURLTest, SuggestQueryParams) {
1257 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1258 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]621ade062013-10-28 06:27:431264
1265 // Baseline: no |suggest_query_params| field.
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]621ade062013-10-28 06:27:431271
1272 // Set the suggest_query_params.
1273 search_terms.suggest_query_params = "pq=xyz";
1274 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191275 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431276
1277 // Add extra_query_params in the mix, and ensure it works.
1278 search_terms.append_extra_query_params = true;
1279 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1280 switches::kExtraSearchQueryParams, "a=b");
1281 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191282 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]621ade062013-10-28 06:27:431283}
1284
[email protected]56fa29592013-07-02 20:25:531285// Test the |append_extra_query_params| field of SearchTermsArgs.
1286TEST_F(TemplateURLTest, ExtraQueryParams) {
1287 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1288 TemplateURLData data;
1289 // Pick a URL with replacements before, during, and after the query, to ensure
1290 // we don't goof up any of them.
1291 data.SetURL("{google:baseURL}search?q={searchTerms}"
1292 "#{google:originalQueryForSuggestion}x");
[email protected]168d08722014-06-18 07:13:281293 TemplateURL url(data);
[email protected]56fa29592013-07-02 20:25:531294
1295 // Baseline: no command-line args, no |append_extra_query_params| flag.
1296 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1297 search_terms.original_query = ASCIIToUTF16("def");
1298 search_terms.accepted_suggestion = 0;
1299 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191300 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531301
1302 // Set the flag. Since there are no command-line args, this should have no
1303 // effect.
1304 search_terms.append_extra_query_params = true;
1305 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191306 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531307
1308 // Now append the command-line arg. This should be inserted into the query.
1309 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1310 switches::kExtraSearchQueryParams, "a=b");
1311 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191312 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531313
1314 // Turn off the flag. Now the command-line arg should be ignored again.
1315 search_terms.append_extra_query_params = false;
1316 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
[email protected]ce7ee5f2014-06-16 23:41:191317 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_));
[email protected]56fa29592013-07-02 20:25:531318}
[email protected]d5015ca2013-08-08 22:04:181319
1320// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171321TEST_F(TemplateURLTest, ReplacePageClassification) {
1322 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
[email protected]d5015ca2013-08-08 22:04:181323 TemplateURLData data;
1324 data.input_encodings.push_back("UTF-8");
1325 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281326 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191327 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1328 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]d5015ca2013-08-08 22:04:181329 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1330
[email protected]ce7ee5f2014-06-16 23:41:191331 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1332 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181333 EXPECT_EQ("http://www.google.com/?q=foo", result);
1334
[email protected]12b5cd052013-11-08 20:59:371335 search_terms_args.page_classification = AutocompleteInput::NTP;
[email protected]ce7ee5f2014-06-16 23:41:191336 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1337 search_terms_data_);
[email protected]d5015ca2013-08-08 22:04:181338 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1339
1340 search_terms_args.page_classification =
[email protected]0b1a1112013-08-28 05:23:311341 AutocompleteInput::HOME_PAGE;
[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=3&q=foo", result);
1345}
[email protected]2328ee22013-08-08 23:00:191346
1347// Test the IsSearchResults function.
1348TEST_F(TemplateURLTest, IsSearchResults) {
1349 TemplateURLData data;
1350 data.SetURL("http://bar/search?q={searchTerms}");
1351 data.instant_url = "http://bar/instant#q={searchTerms}";
[email protected]2767c0fd2013-08-16 17:44:161352 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191353 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1354 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1355 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1356 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281357 TemplateURL search_provider(data);
[email protected]2328ee22013-08-08 23:00:191358
1359 const struct {
1360 const char* const url;
1361 bool result;
1362 } url_data[] = {
1363 { "http://bar/search?q=foo&oq=foo", true, },
1364 { "http://bar/?q=foo&oq=foo", true, },
1365 { "http://bar/#output=search&q=foo&oq=foo", true, },
1366 { "http://bar/webhp#q=foo&oq=foo", true, },
1367 { "http://bar/#q=foo&oq=foo", true, },
1368 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1369 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1370 { "http://bar/", false, },
1371 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161372 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191373 };
1374
1375 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_data); ++i) {
1376 EXPECT_EQ(url_data[i].result,
[email protected]ce7ee5f2014-06-16 23:41:191377 search_provider.IsSearchURL(GURL(url_data[i].url),
1378 search_terms_data_));
[email protected]2328ee22013-08-08 23:00:191379 }
1380}
[email protected]a048de8a2013-10-02 18:30:061381
1382TEST_F(TemplateURLTest, ReflectsBookmarkBarPinned) {
[email protected]30e91c72014-05-03 00:02:001383 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
[email protected]a048de8a2013-10-02 18:30:061384 TemplateURLData data;
1385 data.input_encodings.push_back("UTF-8");
1386 data.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
[email protected]168d08722014-06-18 07:13:281387 TemplateURL url(data);
[email protected]ce7ee5f2014-06-16 23:41:191388 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_));
1389 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_));
[email protected]a048de8a2013-10-02 18:30:061390 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1391
1392 // Do not add the param when InstantExtended is suppressed on SRPs.
1393 url.url_ref_.showing_search_terms_ = false;
[email protected]ce7ee5f2014-06-16 23:41:191394 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1395 search_terms_data_);
[email protected]a048de8a2013-10-02 18:30:061396 EXPECT_EQ("http://www.google.com/?q=foo", result);
1397
1398 // Add the param when InstantExtended is not suppressed on SRPs.
1399 url.url_ref_.showing_search_terms_ = true;
1400 search_terms_args.bookmark_bar_pinned = false;
[email protected]ce7ee5f2014-06-16 23:41:191401 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1402 search_terms_data_);
[email protected]a048de8a2013-10-02 18:30:061403 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result);
1404
1405 url.url_ref_.showing_search_terms_ = true;
1406 search_terms_args.bookmark_bar_pinned = true;
[email protected]ce7ee5f2014-06-16 23:41:191407 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1408 search_terms_data_);
[email protected]a048de8a2013-10-02 18:30:061409 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result);
1410}
[email protected]9d70de12014-05-10 02:15:311411
[email protected]9d70de12014-05-10 02:15:311412TEST_F(TemplateURLTest, AnswersHasVersion) {
1413 TemplateURLData data;
1414 UIThreadSearchTermsData::SetGoogleBaseURL("http://bar/");
1415 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1416
[email protected]168d08722014-06-18 07:13:281417 TemplateURL url(data);
[email protected]9d70de12014-05-10 02:15:311418 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191419 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1420 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311421 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1422
1423 CommandLine::ForCurrentProcess()->AppendSwitch(
1424 switches::kEnableAnswersInSuggest);
[email protected]168d08722014-06-18 07:13:281425 TemplateURL url2(data);
[email protected]ce7ee5f2014-06-16 23:41:191426 result = url2.url_ref().ReplaceSearchTerms(search_terms_args,
1427 search_terms_data_);
[email protected]9d70de12014-05-10 02:15:311428 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1429}
[email protected]20184242014-05-14 02:57:421430
1431TEST_F(TemplateURLTest, SessionToken) {
1432 TemplateURLData data;
1433 UIThreadSearchTermsData::SetGoogleBaseURL("http://bar/");
1434 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t");
1435
[email protected]168d08722014-06-18 07:13:281436 TemplateURL url(data);
[email protected]20184242014-05-14 02:57:421437 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1438 search_terms_args.session_token = "SESSIONTOKENGOESHERE";
[email protected]ce7ee5f2014-06-16 23:41:191439 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1440 search_terms_data_);
[email protected]20184242014-05-14 02:57:421441 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result);
1442
[email protected]168d08722014-06-18 07:13:281443 TemplateURL url2(data);
[email protected]20184242014-05-14 02:57:421444 search_terms_args.session_token = "";
[email protected]ce7ee5f2014-06-16 23:41:191445 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1446 search_terms_data_);
[email protected]20184242014-05-14 02:57:421447 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1448}
[email protected]448b17f52014-06-13 01:08:031449
1450TEST_F(TemplateURLTest, ContextualSearchParameters) {
1451 TemplateURLData data;
1452 UIThreadSearchTermsData::SetGoogleBaseURL("http://bar/");
1453 data.SetURL("http://bar/_/contextualsearch?"
1454 "{google:contextualSearchVersion}"
1455 "{google:contextualSearchContextData}");
1456
[email protected]168d08722014-06-18 07:13:281457 TemplateURL url(data);
[email protected]448b17f52014-06-13 01:08:031458 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
[email protected]ce7ee5f2014-06-16 23:41:191459 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1460 search_terms_data_);
[email protected]448b17f52014-06-13 01:08:031461 EXPECT_EQ("http://bar/_/contextualsearch?", result);
1462
1463 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params(
1464 1, 6, 11, "allen", "woody+allen+movies", "www.wikipedia.org",
1465 "utf-8");
1466 search_terms_args.contextual_search_params = params;
[email protected]ce7ee5f2014-06-16 23:41:191467 result = url.url_ref().ReplaceSearchTerms(search_terms_args,
1468 search_terms_data_);
[email protected]448b17f52014-06-13 01:08:031469 EXPECT_EQ("http://bar/_/contextualsearch?"
1470 "ctxs=1&"
1471 "ctxs_start=6&"
1472 "ctxs_end=11&"
1473 "q=allen&"
1474 "ctxs_content=woody+allen+movies&"
1475 "ctxs_url=www.wikipedia.org&"
1476 "ctxs_encoding=utf-8&", result);
1477}