blob: d6bf5057a863b0c9947935ca44fab5b8b3759ba1 [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]d82443b2009-01-15 19:54:5610#include "chrome/browser/browser_process.h"
11#include "chrome/browser/rlz/rlz.h"
[email protected]375bd7312010-08-30 22:18:1312#include "chrome/browser/search_engines/search_terms_data.h"
[email protected]d54e03a52009-01-16 00:31:0413#include "chrome/browser/search_engines/template_url.h"
[email protected]56fa29592013-07-02 20:25:5314#include "chrome/common/chrome_switches.h"
[email protected]93b29062013-07-12 03:09:0915#include "chrome/common/chrome_version_info.h"
[email protected]d82443b2009-01-15 19:54:5616#include "testing/gtest/include/gtest/gtest.h"
17
[email protected]81d9b72d2012-03-26 22:29:1718#if defined(ENABLE_RLZ)
19#include "chrome/browser/google/google_util.h"
20#endif
21
[email protected]fd6d8822012-12-08 06:56:1122#if defined(OS_ANDROID)
23#include "chrome/browser/search_engines/search_terms_data_android.h"
24#endif
25
[email protected]f911df52013-12-24 23:24:2326using base::ASCIIToUTF16;
27
[email protected]b37bdfe2012-03-16 20:53:2728// TestSearchTermsData --------------------------------------------------------
29
[email protected]375bd7312010-08-30 22:18:1330// Simple implementation of SearchTermsData.
31class TestSearchTermsData : public SearchTermsData {
32 public:
[email protected]b37bdfe2012-03-16 20:53:2733 explicit TestSearchTermsData(const std::string& google_base_url);
[email protected]375bd7312010-08-30 22:18:1334
[email protected]b37bdfe2012-03-16 20:53:2735 virtual std::string GoogleBaseURLValue() const OVERRIDE;
[email protected]375bd7312010-08-30 22:18:1336
37 private:
38 std::string google_base_url_;
39
40 DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData);
41};
42
[email protected]b37bdfe2012-03-16 20:53:2743TestSearchTermsData::TestSearchTermsData(const std::string& google_base_url)
[email protected]2767c0fd2013-08-16 17:44:1644 : google_base_url_(google_base_url) {
[email protected]b37bdfe2012-03-16 20:53:2745}
46
47std::string TestSearchTermsData::GoogleBaseURLValue() const {
48 return google_base_url_;
49}
50
[email protected]b37bdfe2012-03-16 20:53:2751// TemplateURLTest ------------------------------------------------------------
52
[email protected]583844c2011-08-27 00:38:3553class TemplateURLTest : public testing::Test {
[email protected]d82443b2009-01-15 19:54:5654 public:
[email protected]b37bdfe2012-03-16 20:53:2755 void CheckSuggestBaseURL(const std::string& base_url,
56 const std::string& base_suggest_url) const;
[email protected]d82443b2009-01-15 19:54:5657};
58
[email protected]b37bdfe2012-03-16 20:53:2759void TemplateURLTest::CheckSuggestBaseURL(
60 const std::string& base_url,
61 const std::string& base_suggest_url) const {
62 TestSearchTermsData search_terms_data(base_url);
63 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue());
64}
65
66
67// Actual tests ---------------------------------------------------------------
68
[email protected]d82443b2009-01-15 19:54:5669TEST_F(TemplateURLTest, Defaults) {
[email protected]573889f22012-04-07 01:31:5470 TemplateURLData data;
71 EXPECT_FALSE(data.show_in_default_list);
72 EXPECT_FALSE(data.safe_for_autoreplace);
73 EXPECT_EQ(0, data.prepopulate_id);
[email protected]d82443b2009-01-15 19:54:5674}
75
76TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]573889f22012-04-07 01:31:5477 TemplateURLData data;
78 data.SetURL("{searchTerms}");
[email protected]16fca9b82012-04-23 18:40:2679 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:1380 EXPECT_TRUE(url.url_ref().IsValid());
[email protected]d82443b2009-01-15 19:54:5681}
82
83TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1984 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1985 const char* url;
[email protected]0085863a2013-12-06 21:19:0386 const base::string16 terms;
[email protected]34e24852012-01-31 18:43:5887 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:1988 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:3089 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1690 "http://foosea%20rch/bar" },
[email protected]400b133f2011-01-19 18:32:3091 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1692 "http://foosea%20rch/bar?boo=abc" },
[email protected]400b133f2011-01-19 18:32:3093 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1694 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]400b133f2011-01-19 18:32:3095 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
[email protected]c31a979c2012-05-31 23:57:1696 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:1997 };
98 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
99 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54100 TemplateURLData data;
101 data.SetURL(value.url);
[email protected]16fca9b82012-04-23 18:40:26102 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13103 EXPECT_TRUE(url.url_ref().IsValid());
104 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04105 GURL result(url.url_ref().ReplaceSearchTerms(
106 TemplateURLRef::SearchTermsArgs(value.terms)));
[email protected]c31a979c2012-05-31 23:57:16107 ASSERT_TRUE(result.is_valid());
108 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:19109 }
[email protected]d82443b2009-01-15 19:54:56110}
111
112TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:54113 TemplateURLData data;
114 data.SetURL("http://foo{searchTerms}{count?}");
[email protected]16fca9b82012-04-23 18:40:26115 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13116 EXPECT_TRUE(url.url_ref().IsValid());
117 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04118 GURL result(url.url_ref().ReplaceSearchTerms(
119 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56120 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27121 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:56122}
123
124TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:54125 TemplateURLData data;
126 data.SetURL("http://foo{searchTerms}{count}");
[email protected]16fca9b82012-04-23 18:40:26127 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13128 EXPECT_TRUE(url.url_ref().IsValid());
129 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04130 GURL result(url.url_ref().ReplaceSearchTerms(
131 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56132 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27133 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56134}
135
136TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54137 TemplateURLData data;
138 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]16fca9b82012-04-23 18:40:26139 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13140 EXPECT_TRUE(url.url_ref().IsValid());
141 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04142 GURL result(url.url_ref().ReplaceSearchTerms(
143 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56144 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27145 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56146}
147
148TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54149 TemplateURLData data;
150 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[email protected]16fca9b82012-04-23 18:40:26151 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13152 EXPECT_TRUE(url.url_ref().IsValid());
153 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04154 GURL result(url.url_ref().ReplaceSearchTerms(
155 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56156 ASSERT_TRUE(result.is_valid());
[email protected]405aae22012-03-29 20:36:13157 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56158}
159
160TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54161 TemplateURLData data;
162 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[email protected]16fca9b82012-04-23 18:40:26163 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13164 EXPECT_TRUE(url.url_ref().IsValid());
165 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04166 GURL result(url.url_ref().ReplaceSearchTerms(
167 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56168 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27169 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56170}
171
[email protected]93b29062013-07-12 03:09:09172TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
173 const char kInvalidPostParamsString[] =
174 "unknown_template={UnknownTemplate},bad_value=bad{value},"
175 "{google:sbiSource}";
176 // List all accpectable parameter format in valid_post_params_string. it is
177 // expected like: "name0=,name1=value1,name2={template1}"
178 const char kValidPostParamsString[] =
179 "image_content={google:imageThumbnail},image_url={google:imageURL},"
180 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
[email protected]2f3bc6512013-08-28 03:56:27181 "constant_param=constant,width={google:imageOriginalWidth}";
[email protected]93b29062013-07-12 03:09:09182 const char KImageSearchURL[] = "http://foo.com/sbi";
183
184 TemplateURLData data;
185 data.image_url = KImageSearchURL;
186
187 // Try to parse invalid post parameters.
188 data.image_url_post_params = kInvalidPostParamsString;
189 TemplateURL url_bad(NULL, data);
190 ASSERT_FALSE(url_bad.image_url_ref().IsValid());
191 const TemplateURLRef::PostParams& bad_post_params =
192 url_bad.image_url_ref().post_params_;
193 ASSERT_EQ(2U, bad_post_params.size());
194 EXPECT_EQ("unknown_template", bad_post_params[0].first);
195 EXPECT_EQ("{UnknownTemplate}", bad_post_params[0].second);
196 EXPECT_EQ("bad_value", bad_post_params[1].first);
197 EXPECT_EQ("bad{value}", bad_post_params[1].second);
198
199 // Try to parse valid post parameters.
200 data.image_url_post_params = kValidPostParamsString;
201 TemplateURL url(NULL, data);
202 ASSERT_TRUE(url.image_url_ref().IsValid());
203 ASSERT_FALSE(url.image_url_ref().SupportsReplacement());
204
205 // Check term replacement.
206 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
207 search_args.image_thumbnail_content = "dummy-image-thumbnail";
208 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
[email protected]2f3bc6512013-08-28 03:56:27209 search_args.image_original_size = gfx::Size(10, 10);
[email protected]93b29062013-07-12 03:09:09210 // Replacement operation with no post_data buffer should still return
211 // the parsed URL.
212 GURL result(url.image_url_ref().ReplaceSearchTerms(search_args));
213 ASSERT_TRUE(result.is_valid());
214 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48215 TemplateURLRef::PostContent post_content;
[email protected]93b29062013-07-12 03:09:09216 TestSearchTermsData search_terms_data("http://X");
217 result = GURL(url.image_url_ref().ReplaceSearchTermsUsingTermsData(
[email protected]7c2bcc42013-08-01 04:03:48218 search_args, search_terms_data, &post_content));
[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 ASSERT_FALSE(post_content.first.empty());
222 ASSERT_FALSE(post_content.second.empty());
[email protected]93b29062013-07-12 03:09:09223
224 // Check parsed result of post parameters.
225 const TemplateURLRef::Replacements& replacements =
226 url.image_url_ref().replacements_;
227 const TemplateURLRef::PostParams& post_params =
228 url.image_url_ref().post_params_;
[email protected]2f3bc6512013-08-28 03:56:27229 EXPECT_EQ(7U, post_params.size());
[email protected]93b29062013-07-12 03:09:09230 for (TemplateURLRef::PostParams::const_iterator i = post_params.begin();
231 i != post_params.end(); ++i) {
232 TemplateURLRef::Replacements::const_iterator j = replacements.begin();
233 for (; j != replacements.end(); ++j) {
234 if (j->is_post_param && j->index ==
235 static_cast<size_t>(i - post_params.begin())) {
236 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27237 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
238 EXPECT_EQ("width", i->first);
239 EXPECT_EQ(
240 base::IntToString(search_args.image_original_size.width()),
241 i->second);
242 break;
[email protected]93b29062013-07-12 03:09:09243 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
244 EXPECT_EQ("image_content", i->first);
245 EXPECT_EQ(search_args.image_thumbnail_content, i->second);
246 break;
247 case TemplateURLRef::GOOGLE_IMAGE_URL:
248 EXPECT_EQ("image_url", i->first);
249 EXPECT_EQ(search_args.image_url.spec(), i->second);
250 break;
251 case TemplateURLRef::LANGUAGE:
252 EXPECT_EQ("language", i->first);
253 EXPECT_EQ("en", i->second);
254 break;
255 default:
256 ADD_FAILURE(); // Should never go here.
257 }
258 break;
259 }
260 }
261 if (j != replacements.end())
262 continue;
263 if (i->first == "empty_param") {
264 EXPECT_TRUE(i->second.empty());
265 } else if (i->first == "sbisrc") {
[email protected]7c2bcc42013-08-01 04:03:48266 EXPECT_FALSE(i->second.empty());
[email protected]93b29062013-07-12 03:09:09267 } else {
268 EXPECT_EQ("constant_param", i->first);
269 EXPECT_EQ("constant", i->second);
270 }
271 }
272}
273
[email protected]d88cb202011-08-17 20:03:01274// Test that setting the prepopulate ID from TemplateURL causes the stored
275// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44276TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54277 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20278 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]16fca9b82012-04-23 18:40:26279 TemplateURL url(NULL, data);
[email protected]1a257262011-06-28 22:15:44280 TemplateURLRef::Replacements replacements;
281 bool valid = false;
[email protected]243abf32012-05-15 18:28:20282 EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL(
[email protected]93b29062013-07-12 03:09:09283 "http://foo{fhqwhgads}bar", &replacements, NULL, &valid));
[email protected]1a257262011-06-28 22:15:44284 EXPECT_TRUE(replacements.empty());
285 EXPECT_TRUE(valid);
286
[email protected]573889f22012-04-07 01:31:54287 data.prepopulate_id = 123;
[email protected]16fca9b82012-04-23 18:40:26288 TemplateURL url2(NULL, data);
[email protected]243abf32012-05-15 18:28:20289 EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar",
[email protected]93b29062013-07-12 03:09:09290 &replacements, NULL,
291 &valid));
[email protected]1a257262011-06-28 22:15:44292 EXPECT_TRUE(replacements.empty());
293 EXPECT_TRUE(valid);
294}
295
[email protected]d82443b2009-01-15 19:54:56296TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54297 TemplateURLData data;
298 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]16fca9b82012-04-23 18:40:26299 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13300 EXPECT_TRUE(url.url_ref().IsValid());
301 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04302 GURL result(url.url_ref().ReplaceSearchTerms(
303 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56304 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27305 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56306}
307
308TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54309 TemplateURLData data;
310 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]16fca9b82012-04-23 18:40:26311 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13312 EXPECT_TRUE(url.url_ref().IsValid());
313 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04314 GURL result(url.url_ref().ReplaceSearchTerms(
315 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56316 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27317 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56318}
319
[email protected]375bd7312010-08-30 22:18:13320TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
321 struct SearchTermsCase {
322 const char* url;
[email protected]0085863a2013-12-06 21:19:03323 const base::string16 terms;
[email protected]375bd7312010-08-30 22:18:13324 const char* output;
325 } search_term_cases[] = {
[email protected]0085863a2013-12-06 21:19:03326 { "{google:baseURL}{language}{searchTerms}", base::string16(),
[email protected]b37bdfe2012-03-16 20:53:27327 "http://example.com/e/en" },
[email protected]0085863a2013-12-06 21:19:03328 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
[email protected]014010e2011-10-01 04:12:44329 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13330 };
331
332 TestSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54333 TemplateURLData data;
[email protected]375bd7312010-08-30 22:18:13334 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
335 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54336 data.SetURL(value.url);
[email protected]16fca9b82012-04-23 18:40:26337 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13338 EXPECT_TRUE(url.url_ref().IsValid());
339 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04340 GURL result(url.url_ref().ReplaceSearchTermsUsingTermsData(
[email protected]93b29062013-07-12 03:09:09341 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL));
[email protected]375bd7312010-08-30 22:18:13342 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27343 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13344 }
345}
346
[email protected]d82443b2009-01-15 19:54:56347TEST_F(TemplateURLTest, URLRefTermToWide) {
348 struct ToWideCase {
349 const char* encoded_search_term;
[email protected]0085863a2013-12-06 21:19:03350 const base::string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56351 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30352 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56353 // Test some big-5 input.
[email protected]f911df52013-12-24 23:24:23354 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56355 // Test some UTF-8 input. We should fall back to this when the encoding
356 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
357 // Big-5 trailing byte.
[email protected]f911df52013-12-24 23:24:23358 {"%e4%bd%a05%e5%a5%bd+to+you",
359 base::WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56360 // Undecodable input should stay escaped.
[email protected]f911df52013-12-24 23:24:23361 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44362 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30363 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44364 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30365 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56366 };
367
[email protected]d82443b2009-01-15 19:54:56368 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54369 TemplateURLData data;
370 data.SetURL("http://foo?q={searchTerms}");
371 data.input_encodings.push_back("big-5");
[email protected]16fca9b82012-04-23 18:40:26372 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13373 EXPECT_TRUE(url.url_ref().IsValid());
374 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]f63ae312009-02-04 17:58:46375 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07376 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13377 url.url_ref().SearchTermToString16(
378 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56379 }
380}
381
[email protected]d82443b2009-01-15 19:54:56382TEST_F(TemplateURLTest, DisplayURLToURLRef) {
383 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19384 const std::string url;
[email protected]0085863a2013-12-06 21:19:03385 const base::string16 expected_result;
[email protected]b37bdfe2012-03-16 20:53:27386 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19387 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30388 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19389 { "http://X",
[email protected]400b133f2011-01-19 18:32:30390 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19391 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30392 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19393 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30394 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56395 };
[email protected]573889f22012-04-07 01:31:54396 TemplateURLData data;
[email protected]b37bdfe2012-03-16 20:53:27397 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54398 data.SetURL(test_data[i].url);
[email protected]16fca9b82012-04-23 18:40:26399 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13400 EXPECT_EQ(test_data[i].expected_result, url.url_ref().DisplayURL());
[email protected]b37bdfe2012-03-16 20:53:27401 EXPECT_EQ(test_data[i].url,
[email protected]360ba052012-04-04 17:26:13402 TemplateURLRef::DisplayURLToURLRef(url.url_ref().DisplayURL()));
[email protected]d82443b2009-01-15 19:54:56403 }
404}
405
406TEST_F(TemplateURLTest, ReplaceSearchTerms) {
407 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19408 const std::string url;
[email protected]d82443b2009-01-15 19:54:56409 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27410 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19411 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56412 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19413 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56414 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19415 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56416 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19417 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56418 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19419 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56420 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19421 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56422 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19423 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56424 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19425 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56426 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19427 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56428 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19429 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56430 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19431 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56432 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19433 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56434 "http://foo/UTF-8a{language}aXa" },
435 };
[email protected]573889f22012-04-07 01:31:54436 TemplateURLData data;
437 data.input_encodings.push_back("UTF-8");
[email protected]b37bdfe2012-03-16 20:53:27438 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54439 data.SetURL(test_data[i].url);
[email protected]16fca9b82012-04-23 18:40:26440 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13441 EXPECT_TRUE(url.url_ref().IsValid());
[email protected]3954c3a2012-04-10 20:17:55442 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]b37bdfe2012-03-16 20:53:27443 std::string expected_result = test_data[i].expected_result;
[email protected]d82443b2009-01-15 19:54:56444 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}",
[email protected]d70539de2009-06-24 22:17:06445 g_browser_process->GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04446 GURL result(url.url_ref().ReplaceSearchTerms(
447 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]b37bdfe2012-03-16 20:53:27448 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56449 EXPECT_EQ(expected_result, result.spec());
450 }
451}
452
453
454// Tests replacing search terms in various encodings and making sure the
455// generated URL matches the expected value.
456TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
457 struct TestData {
458 const std::string encoding;
[email protected]0085863a2013-12-06 21:19:03459 const base::string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19460 const std::string url;
[email protected]d82443b2009-01-15 19:54:56461 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27462 } test_data[] = {
[email protected]f911df52013-12-24 23:24:23463 { "BIG5", base::WideToUTF16(L"\x60BD"),
[email protected]400b133f2011-01-19 18:32:30464 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17465 "http://foo/?%B1~BIG5" },
[email protected]400b133f2011-01-19 18:32:30466 { "UTF-8", ASCIIToUTF16("blah"),
467 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17468 "http://foo/?blahUTF-8" },
[email protected]f911df52013-12-24 23:24:23469 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"),
[email protected]34e24852012-01-31 18:43:58470 "http://foo/{searchTerms}/bar",
471 "http://foo/%82%A0/bar"},
[email protected]f911df52013-12-24 23:24:23472 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
[email protected]34e24852012-01-31 18:43:58473 "http://foo/{searchTerms}/bar",
474 "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56475 };
[email protected]573889f22012-04-07 01:31:54476 TemplateURLData data;
[email protected]b37bdfe2012-03-16 20:53:27477 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54478 data.SetURL(test_data[i].url);
479 data.input_encodings.clear();
480 data.input_encodings.push_back(test_data[i].encoding);
[email protected]16fca9b82012-04-23 18:40:26481 TemplateURL url(NULL, data);
[email protected]3954c3a2012-04-10 20:17:55482 EXPECT_TRUE(url.url_ref().IsValid());
483 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04484 GURL result(url.url_ref().ReplaceSearchTerms(
485 TemplateURLRef::SearchTermsArgs(test_data[i].search_term)));
486 ASSERT_TRUE(result.is_valid());
487 EXPECT_EQ(test_data[i].expected_result, result.spec());
488 }
489}
490
491// Tests replacing assisted query stats (AQS) in various scenarios.
492TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
493 struct TestData {
[email protected]0085863a2013-12-06 21:19:03494 const base::string16 search_term;
[email protected]bca359b2012-06-24 07:53:04495 const std::string aqs;
496 const std::string base_url;
497 const std::string url;
498 const std::string expected_result;
499 } test_data[] = {
500 // No HTTPS, no AQS.
501 { ASCIIToUTF16("foo"),
502 "chrome.0.0l6",
503 "http://foo/",
504 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
505 "http://foo/?foo" },
506 // HTTPS available, AQS should be replaced.
507 { ASCIIToUTF16("foo"),
508 "chrome.0.0l6",
509 "https://foo/",
510 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
511 "https://foo/?fooaqs=chrome.0.0l6&" },
512 // HTTPS available, however AQS is empty.
513 { ASCIIToUTF16("foo"),
514 "",
515 "https://foo/",
516 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
517 "https://foo/?foo" },
518 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
519 { ASCIIToUTF16("foo"),
520 "chrome.0.0l6",
521 "",
522 "http://foo?{searchTerms}{google:assistedQueryStats}",
523 "http://foo/?foo" },
524 // A non-Google search provider with HTTPS should allow AQS.
525 { ASCIIToUTF16("foo"),
526 "chrome.0.0l6",
527 "",
528 "https://foo?{searchTerms}{google:assistedQueryStats}",
529 "https://foo/?fooaqs=chrome.0.0l6&" },
530 };
531 TemplateURLData data;
532 data.input_encodings.push_back("UTF-8");
533 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
534 data.SetURL(test_data[i].url);
535 TemplateURL url(NULL, data);
536 EXPECT_TRUE(url.url_ref().IsValid());
537 ASSERT_TRUE(url.url_ref().SupportsReplacement());
538 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
539 search_terms_args.assisted_query_stats = test_data[i].aqs;
540 UIThreadSearchTermsData::SetGoogleBaseURL(test_data[i].base_url);
541 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
[email protected]b37bdfe2012-03-16 20:53:27542 ASSERT_TRUE(result.is_valid());
543 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56544 }
545}
546
[email protected]00790562012-12-14 09:57:16547// Tests replacing cursor position.
548TEST_F(TemplateURLTest, ReplaceCursorPosition) {
549 struct TestData {
[email protected]0085863a2013-12-06 21:19:03550 const base::string16 search_term;
[email protected]00790562012-12-14 09:57:16551 size_t cursor_position;
552 const std::string url;
553 const std::string expected_result;
554 } test_data[] = {
555 { ASCIIToUTF16("foo"),
[email protected]0085863a2013-12-06 21:19:03556 base::string16::npos,
[email protected]00790562012-12-14 09:57:16557 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
558 "http://www.google.com/?foo&" },
559 { ASCIIToUTF16("foo"),
560 2,
561 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
562 "http://www.google.com/?foo&cp=2&" },
563 { ASCIIToUTF16("foo"),
564 15,
565 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
566 "http://www.google.com/?foo&cp=15&" },
567 };
[email protected]30e91c72014-05-03 00:02:00568 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
[email protected]00790562012-12-14 09:57:16569 TemplateURLData data;
570 data.input_encodings.push_back("UTF-8");
571 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
572 data.SetURL(test_data[i].url);
573 TemplateURL url(NULL, data);
574 EXPECT_TRUE(url.url_ref().IsValid());
575 ASSERT_TRUE(url.url_ref().SupportsReplacement());
576 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
577 search_terms_args.cursor_position = test_data[i].cursor_position;
578 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
579 ASSERT_TRUE(result.is_valid());
580 EXPECT_EQ(test_data[i].expected_result, result.spec());
581 }
582}
583
[email protected]9b9fa672013-11-07 06:04:52584// Tests replacing currentPageUrl.
585TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48586 struct TestData {
[email protected]0085863a2013-12-06 21:19:03587 const base::string16 search_term;
[email protected]9b9fa672013-11-07 06:04:52588 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48589 const std::string url;
590 const std::string expected_result;
591 } test_data[] = {
592 { ASCIIToUTF16("foo"),
593 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52594 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48595 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
596 { ASCIIToUTF16("foo"),
597 "",
[email protected]9b9fa672013-11-07 06:04:52598 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48599 "http://www.google.com/?foo&" },
600 { ASCIIToUTF16("foo"),
601 "http://g.com/+-/*&=",
[email protected]9b9fa672013-11-07 06:04:52602 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48603 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
604 };
[email protected]30e91c72014-05-03 00:02:00605 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
[email protected]800569d2013-05-06 07:38:48606 TemplateURLData data;
607 data.input_encodings.push_back("UTF-8");
608 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
609 data.SetURL(test_data[i].url);
610 TemplateURL url(NULL, data);
611 EXPECT_TRUE(url.url_ref().IsValid());
612 ASSERT_TRUE(url.url_ref().SupportsReplacement());
613 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52614 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]800569d2013-05-06 07:38:48615 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
616 ASSERT_TRUE(result.is_valid());
617 EXPECT_EQ(test_data[i].expected_result, result.spec());
618 }
619}
620
[email protected]d82443b2009-01-15 19:54:56621TEST_F(TemplateURLTest, Suggestions) {
622 struct TestData {
623 const int accepted_suggestion;
[email protected]0085863a2013-12-06 21:19:03624 const base::string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56625 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27626 } test_data[] = {
[email protected]0085863a2013-12-06 21:19:03627 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
[email protected]29653892013-03-02 19:25:37628 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30629 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37630 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03631 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(),
[email protected]29653892013-03-02 19:25:37632 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30633 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37634 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03635 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
[email protected]29653892013-03-02 19:25:37636 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56637 };
[email protected]573889f22012-04-07 01:31:54638 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37639 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
640 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54641 data.input_encodings.push_back("UTF-8");
[email protected]16fca9b82012-04-23 18:40:26642 TemplateURL url(NULL, data);
[email protected]3954c3a2012-04-10 20:17:55643 EXPECT_TRUE(url.url_ref().IsValid());
[email protected]360ba052012-04-04 17:26:13644 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]b37bdfe2012-03-16 20:53:27645 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04646 TemplateURLRef::SearchTermsArgs search_terms_args(
647 ASCIIToUTF16("foobar"));
648 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
649 search_terms_args.original_query =
650 test_data[i].original_query_for_suggestion;
651 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
[email protected]b37bdfe2012-03-16 20:53:27652 ASSERT_TRUE(result.is_valid());
653 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56654 }
655}
656
[email protected]81f808de2009-09-25 01:36:34657TEST_F(TemplateURLTest, RLZ) {
[email protected]0085863a2013-12-06 21:19:03658 base::string16 rlz_string;
[email protected]81d9b72d2012-03-26 22:29:17659#if defined(ENABLE_RLZ)
660 std::string brand;
661 if (google_util::GetBrand(&brand) && !brand.empty() &&
662 !google_util::IsOrganic(brand)) {
[email protected]7e6f4ed5c2012-10-31 21:10:34663 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz_string);
[email protected]81d9b72d2012-03-26 22:29:17664 }
[email protected]fd6d8822012-12-08 06:56:11665#elif defined(OS_ANDROID)
666 SearchTermsDataAndroid::rlz_parameter_value_.Get() =
667 ASCIIToUTF16("android_test");
668 rlz_string = SearchTermsDataAndroid::rlz_parameter_value_.Get();
[email protected]58b64332010-08-13 16:09:39669#endif
[email protected]d82443b2009-01-15 19:54:56670
[email protected]573889f22012-04-07 01:31:54671 TemplateURLData data;
672 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]16fca9b82012-04-23 18:40:26673 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13674 EXPECT_TRUE(url.url_ref().IsValid());
675 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04676 GURL result(url.url_ref().ReplaceSearchTerms(
677 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x"))));
[email protected]d82443b2009-01-15 19:54:56678 ASSERT_TRUE(result.is_valid());
[email protected]12bd05872009-03-17 19:25:06679 std::string expected_url = "http://bar/?";
[email protected]b37bdfe2012-03-16 20:53:27680 if (!rlz_string.empty())
[email protected]f911df52013-12-24 23:24:23681 expected_url += "rlz=" + base::UTF16ToUTF8(rlz_string) + "&";
[email protected]12bd05872009-03-17 19:25:06682 expected_url += "x";
[email protected]b37bdfe2012-03-16 20:53:27683 EXPECT_EQ(expected_url, result.spec());
[email protected]d82443b2009-01-15 19:54:56684}
685
[email protected]c8ccc41d2014-04-10 04:42:12686#if !defined(OS_ANDROID) && !defined(OS_IOS)
687TEST_F(TemplateURLTest, RLZFromAppList) {
688 base::string16 rlz_string;
689#if defined(ENABLE_RLZ)
690 std::string brand;
691 if (google_util::GetBrand(&brand) && !brand.empty() &&
692 !google_util::IsOrganic(brand)) {
693 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_APP_LIST, &rlz_string);
694 }
695#endif
696
697 TemplateURLData data;
698 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
699 TemplateURL url(NULL, data);
700 EXPECT_TRUE(url.url_ref().IsValid());
701 ASSERT_TRUE(url.url_ref().SupportsReplacement());
702 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x"));
703 args.from_app_list = true;
704 GURL result(url.url_ref().ReplaceSearchTerms(args));
705 ASSERT_TRUE(result.is_valid());
706 std::string expected_url = "http://bar/?";
707 if (!rlz_string.empty())
708 expected_url += "rlz=" + base::UTF16ToUTF8(rlz_string) + "&";
709 expected_url += "x";
710 EXPECT_EQ(expected_url, result.spec());
711}
712#endif
713
[email protected]d82443b2009-01-15 19:54:56714TEST_F(TemplateURLTest, HostAndSearchTermKey) {
715 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19716 const std::string url;
[email protected]d82443b2009-01-15 19:54:56717 const std::string host;
718 const std::string path;
719 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54720 } test_data[] = {
[email protected]7c60f5042013-02-14 03:39:32721 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56722
723 // No query key should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32724 { "http://blah/{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56725
726 // No term should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32727 { "http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56728
729 // Multiple terms should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32730 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56731
732 // Term in the host shouldn't match.
[email protected]7c60f5042013-02-14 03:39:32733 { "http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56734
[email protected]7c60f5042013-02-14 03:39:32735 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
736 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56737
738 // Single term with extra chars in value should match.
[email protected]7c60f5042013-02-14 03:39:32739 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56740 };
741
[email protected]573889f22012-04-07 01:31:54742 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
743 TemplateURLData data;
744 data.SetURL(test_data[i].url);
[email protected]16fca9b82012-04-23 18:40:26745 TemplateURL url(NULL, data);
[email protected]573889f22012-04-07 01:31:54746 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost());
747 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath());
748 EXPECT_EQ(test_data[i].search_term_key, url.url_ref().GetSearchTermKey());
[email protected]d82443b2009-01-15 19:54:56749 }
750}
751
752TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
753 static const struct {
[email protected]ddd231e2010-06-29 20:35:19754 const char* const base_url;
755 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56756 } data[] = {
[email protected]014010e2011-10-01 04:12:44757 { "http://google.com/", "http://google.com/complete/", },
758 { "http://www.google.com/", "http://www.google.com/complete/", },
759 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
760 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
761 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56762 };
763
[email protected]f63ae312009-02-04 17:58:46764 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i)
[email protected]d82443b2009-01-15 19:54:56765 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
766}
767
[email protected]81c6ef62010-01-21 09:58:47768TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19769 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:54770 TemplateURLData data;
771 data.SetURL(parsed_url);
[email protected]16fca9b82012-04-23 18:40:26772 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47773 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:13774 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19775 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47776 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27777 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47778 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
779}
780
781TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:20782 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:54783 TemplateURLData data;
784 data.SetURL(parsed_url);
[email protected]16fca9b82012-04-23 18:40:26785 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47786 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:44787
788 // By default, TemplateURLRef should not consider itself prepopulated.
789 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:13790 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20791 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:44792 EXPECT_TRUE(replacements.empty());
793
794 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:20795 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:54796 data.prepopulate_id = 1;
[email protected]16fca9b82012-04-23 18:40:26797 TemplateURL url2(NULL, data);
[email protected]495c30b2012-05-15 18:48:15798 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20799 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47800 EXPECT_TRUE(replacements.empty());
801}
802
803TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]16fca9b82012-04-23 18:40:26804 TemplateURL url(NULL, TemplateURLData());
[email protected]81c6ef62010-01-21 09:58:47805 TemplateURLRef::Replacements replacements;
806 bool valid = false;
[email protected]b37bdfe2012-03-16 20:53:27807 EXPECT_EQ(std::string(),
[email protected]93b29062013-07-12 03:09:09808 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47809 EXPECT_TRUE(replacements.empty());
810 EXPECT_TRUE(valid);
811}
812
813TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:54814 TemplateURLData data;
815 data.SetURL("{");
[email protected]16fca9b82012-04-23 18:40:26816 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47817 TemplateURLRef::Replacements replacements;
818 bool valid = false;
[email protected]93b29062013-07-12 03:09:09819 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
820 &valid));
[email protected]81c6ef62010-01-21 09:58:47821 EXPECT_TRUE(replacements.empty());
822 EXPECT_FALSE(valid);
823}
824
825TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:54826 TemplateURLData data;
827 data.SetURL("{}");
[email protected]16fca9b82012-04-23 18:40:26828 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47829 TemplateURLRef::Replacements replacements;
830 bool valid = false;
[email protected]93b29062013-07-12 03:09:09831 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47832 EXPECT_TRUE(replacements.empty());
833 EXPECT_TRUE(valid);
834}
835
836TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:54837 TemplateURLData data;
838 data.SetURL("{}{{%s}}");
[email protected]16fca9b82012-04-23 18:40:26839 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47840 TemplateURLRef::Replacements replacements;
841 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19842 EXPECT_EQ("{}{}",
[email protected]93b29062013-07-12 03:09:09843 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
844 &valid));
[email protected]81c6ef62010-01-21 09:58:47845 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27846 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47847 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
848 EXPECT_TRUE(valid);
849}
850
851TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:54852 TemplateURLData data;
853 data.SetURL("{%s");
[email protected]16fca9b82012-04-23 18:40:26854 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47855 TemplateURLRef::Replacements replacements;
856 bool valid = false;
[email protected]360ba052012-04-04 17:26:13857 EXPECT_EQ("{",
[email protected]93b29062013-07-12 03:09:09858 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
859 &valid));
[email protected]81c6ef62010-01-21 09:58:47860 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27861 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47862 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
863 EXPECT_TRUE(valid);
864}
[email protected]5b3078752012-10-09 18:54:16865
[email protected]fd6d8822012-12-08 06:56:11866#if defined(OS_ANDROID)
867TEST_F(TemplateURLTest, SearchClient) {
868 const std::string base_url_str("http://google.com/?");
869 const std::string terms_str("{searchTerms}&{google:searchClient}");
870 const std::string full_url_str = base_url_str + terms_str;
[email protected]0085863a2013-12-06 21:19:03871 const base::string16 terms(ASCIIToUTF16(terms_str));
[email protected]fd6d8822012-12-08 06:56:11872 UIThreadSearchTermsData::SetGoogleBaseURL(base_url_str);
873
874 TemplateURLData data;
875 data.SetURL(full_url_str);
876 TemplateURL url(NULL, data);
877 EXPECT_TRUE(url.url_ref().IsValid());
878 ASSERT_TRUE(url.url_ref().SupportsReplacement());
879 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
880
881 // Check that the URL is correct when a client is not present.
882 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
883 ASSERT_TRUE(result.is_valid());
884 EXPECT_EQ("http://google.com/?foobar&", result.spec());
885
886 // Check that the URL is correct when a client is present.
887 SearchTermsDataAndroid::search_client_.Get() = "android_test";
888 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args));
889 ASSERT_TRUE(result_2.is_valid());
890 EXPECT_EQ("http://google.com/?foobar&client=android_test&", result_2.spec());
891}
892#endif
893
[email protected]5b3078752012-10-09 18:54:16894TEST_F(TemplateURLTest, GetURLNoInstantURL) {
895 TemplateURLData data;
896 data.SetURL("http://google.com/?q={searchTerms}");
897 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
898 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
899 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
900 TemplateURL url(NULL, data);
901 ASSERT_EQ(3U, url.URLCount());
902 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
903 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
904 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
905}
906
907TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
908 TemplateURLData data;
909 data.SetURL("http://google.com/?q={searchTerms}");
910 data.instant_url = "http://google.com/instant#q={searchTerms}";
911 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
912 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
913 TemplateURL url(NULL, data);
914 ASSERT_EQ(3U, url.URLCount());
915 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
916 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
917 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
918}
919
920TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
921 TemplateURLData data;
922 data.SetURL("http://www.google.co.uk/");
923 TemplateURL url(NULL, data);
924 ASSERT_EQ(1U, url.URLCount());
925 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0));
926}
927
928TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
929 TemplateURLData data;
930 data.SetURL("http://google.com/?q={searchTerms}");
931 data.instant_url = "http://google.com/instant#q={searchTerms}";
932 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
933 data.alternate_urls.push_back(
934 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
935 TemplateURL url(NULL, data);
[email protected]0085863a2013-12-06 21:19:03936 base::string16 result;
[email protected]5b3078752012-10-09 18:54:16937
938 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
939 GURL("http://google.com/?q=something"), &result));
[email protected]4076ea62013-01-09 01:47:19940 EXPECT_EQ(ASCIIToUTF16("something"), result);
941
942 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
943 GURL("http://google.com/?espv&q=something"), &result));
944 EXPECT_EQ(ASCIIToUTF16("something"), result);
945
946 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
947 GURL("http://google.com/?espv=1&q=something"), &result));
948 EXPECT_EQ(ASCIIToUTF16("something"), result);
949
950 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
951 GURL("http://google.com/?espv=0&q=something"), &result));
952 EXPECT_EQ(ASCIIToUTF16("something"), result);
953
954 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
955 GURL("http://google.com/alt/#q=something"), &result));
956 EXPECT_EQ(ASCIIToUTF16("something"), result);
957
958 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
959 GURL("http://google.com/alt/#espv&q=something"), &result));
960 EXPECT_EQ(ASCIIToUTF16("something"), result);
961
962 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
963 GURL("http://google.com/alt/#espv=1&q=something"), &result));
964 EXPECT_EQ(ASCIIToUTF16("something"), result);
965
966 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
967 GURL("http://google.com/alt/#espv=0&q=something"), &result));
[email protected]5b3078752012-10-09 18:54:16968 EXPECT_EQ(ASCIIToUTF16("something"), result);
969
970 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
971 GURL("http://google.ca/?q=something"), &result));
[email protected]b959d7d42013-12-13 17:26:37972 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:16973
974 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]67d8b752013-04-03 17:33:27975 GURL("http://google.ca/?q=something&q=anything"), &result));
[email protected]b959d7d42013-12-13 17:26:37976 EXPECT_EQ(base::string16(), result);
[email protected]67d8b752013-04-03 17:33:27977
978 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]5b3078752012-10-09 18:54:16979 GURL("http://google.com/foo/?q=foo"), &result));
[email protected]b959d7d42013-12-13 17:26:37980 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:16981
[email protected]32e2d81b2012-10-16 19:03:25982 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]5b3078752012-10-09 18:54:16983 GURL("https://google.com/?q=foo"), &result));
[email protected]32e2d81b2012-10-16 19:03:25984 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:16985
986 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
987 GURL("http://google.com:8080/?q=foo"), &result));
[email protected]b959d7d42013-12-13 17:26:37988 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:16989
990 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
991 GURL("http://google.com/?q=1+2+3&b=456"), &result));
992 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
993
994 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
995 GURL("http://google.com/alt/?q=123#q=456"), &result));
996 EXPECT_EQ(ASCIIToUTF16("456"), result);
997
998 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
999 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), &result));
1000 EXPECT_EQ(ASCIIToUTF16("123"), result);
1001
1002 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
1003 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), &result));
1004 EXPECT_EQ(ASCIIToUTF16("789"), result);
1005
1006 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1007 GURL("http://google.com/alt/?q="), &result));
[email protected]b959d7d42013-12-13 17:26:371008 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161009
1010 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1011 GURL("http://google.com/alt/?#q="), &result));
[email protected]b959d7d42013-12-13 17:26:371012 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161013
1014 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1015 GURL("http://google.com/alt/?q=#q="), &result));
[email protected]b959d7d42013-12-13 17:26:371016 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161017
1018 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
1019 GURL("http://google.com/alt/?q=123#q="), &result));
[email protected]b959d7d42013-12-13 17:26:371020 EXPECT_EQ(base::string16(), result);
[email protected]5b3078752012-10-09 18:54:161021
1022 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
1023 GURL("http://google.com/alt/?q=#q=123"), &result));
1024 EXPECT_EQ(ASCIIToUTF16("123"), result);
1025}
[email protected]4076ea62013-01-09 01:47:191026
1027TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
1028 TemplateURLData data;
1029 data.SetURL("http://google.com/?q={searchTerms}");
1030 data.instant_url = "http://google.com/instant#q={searchTerms}";
1031 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1032 data.alternate_urls.push_back(
1033 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1034 data.search_terms_replacement_key = "espv";
1035 TemplateURL url(NULL, data);
1036
1037 // Test with instant enabled required.
1038 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1039 GURL("http://google.com/")));
1040
1041 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1042 GURL("http://google.com/?espv")));
1043
1044 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1045 GURL("http://google.com/#espv")));
1046
1047 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1048 GURL("http://google.com/?q=something")));
1049
1050 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1051 GURL("http://google.com/?q=something&espv")));
1052
1053 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1054 GURL("http://google.com/?q=something&espv=1")));
1055
1056 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1057 GURL("http://google.com/?q=something&espv=0")));
1058
1059 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1060 GURL("http://google.com/?espv&q=something")));
1061
1062 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1063 GURL("http://google.com/?espv=1&q=something")));
1064
1065 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1066 GURL("http://google.com/?espv=0&q=something")));
1067
1068 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1069 GURL("http://google.com/alt/#q=something")));
1070
1071 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1072 GURL("http://google.com/alt/#q=something&espv")));
1073
1074 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1075 GURL("http://google.com/alt/#q=something&espv=1")));
1076
1077 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1078 GURL("http://google.com/alt/#q=something&espv=0")));
1079
1080 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1081 GURL("http://google.com/alt/#espv&q=something")));
1082
1083 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1084 GURL("http://google.com/alt/#espv=1&q=something")));
1085
1086 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1087 GURL("http://google.com/alt/#espv=0&q=something")));
1088
1089 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1090 GURL("http://google.com/?espv#q=something")));
1091
1092 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1093 GURL("http://google.com/?espv=1#q=something")));
1094
1095 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1096 GURL("http://google.com/?q=something#espv")));
1097
1098 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1099 GURL("http://google.com/?q=something#espv=1")));
1100
1101 // This does not ensure the domain matches.
1102 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1103 GURL("http://bing.com/?espv")));
1104
1105 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1106 GURL("http://bing.com/#espv")));
1107}
[email protected]f62e30f52013-03-23 03:45:151108
1109TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1110 TemplateURLData data;
1111 data.SetURL("http://google.com/?q={searchTerms}");
1112 data.instant_url = "http://google.com/instant#q={searchTerms}";
1113 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1114 data.alternate_urls.push_back(
1115 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1116 TemplateURL url(NULL, data);
1117 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1118 GURL result;
1119
1120 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1121 GURL("http://google.com/?q=something"), search_terms, &result));
1122 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result);
1123
1124 result = GURL("http://should.not.change.com");
1125 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1126 GURL("http://google.ca/?q=something"), search_terms, &result));
1127 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1128
1129 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1130 GURL("http://google.com/foo/?q=foo"), search_terms, &result));
1131
1132 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1133 GURL("https://google.com/?q=foo"), search_terms, &result));
1134 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result);
1135
1136 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1137 GURL("http://google.com:8080/?q=foo"), search_terms, &result));
1138
1139 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1140 GURL("http://google.com/?q=1+2+3&b=456"), search_terms, &result));
1141 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result);
1142
1143 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1144 // template_url.cc for details.
1145 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1146 GURL("http://google.com/alt/?q=123#q=456"), search_terms, &result));
1147 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result);
1148
1149 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1150 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
1151 &result));
1152 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"),
1153 result);
1154
1155 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1156 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1157 search_terms, &result));
1158 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1159 "#j=abc&q=Bob Morane&h=def9"), result);
1160
1161 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1162 GURL("http://google.com/alt/?q="), search_terms, &result));
1163
1164 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1165 GURL("http://google.com/alt/?#q="), search_terms, &result));
1166
1167 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1168 GURL("http://google.com/alt/?q=#q="), search_terms, &result));
1169
1170 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1171 GURL("http://google.com/alt/?q=123#q="), search_terms, &result));
1172
1173 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1174 GURL("http://google.com/alt/?q=#q=123"), search_terms, &result));
1175 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result);
1176}
[email protected]56fa29592013-07-02 20:25:531177
[email protected]621ade062013-10-28 06:27:431178// Test the |suggest_query_params| field of SearchTermsArgs.
1179TEST_F(TemplateURLTest, SuggestQueryParams) {
1180 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1181 TemplateURLData data;
1182 // Pick a URL with replacements before, during, and after the query, to ensure
1183 // we don't goof up any of them.
1184 data.SetURL("{google:baseURL}search?q={searchTerms}"
1185 "#{google:originalQueryForSuggestion}x");
1186 TemplateURL url(NULL, data);
1187
1188 // Baseline: no |suggest_query_params| field.
1189 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1190 search_terms.original_query = ASCIIToUTF16("def");
1191 search_terms.accepted_suggestion = 0;
1192 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1193 url.url_ref().ReplaceSearchTerms(search_terms));
1194
1195 // Set the suggest_query_params.
1196 search_terms.suggest_query_params = "pq=xyz";
1197 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
1198 url.url_ref().ReplaceSearchTerms(search_terms));
1199
1200 // Add extra_query_params in the mix, and ensure it works.
1201 search_terms.append_extra_query_params = true;
1202 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1203 switches::kExtraSearchQueryParams, "a=b");
1204 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
1205 url.url_ref().ReplaceSearchTerms(search_terms));
1206}
1207
[email protected]56fa29592013-07-02 20:25:531208// Test the |append_extra_query_params| field of SearchTermsArgs.
1209TEST_F(TemplateURLTest, ExtraQueryParams) {
1210 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1211 TemplateURLData data;
1212 // Pick a URL with replacements before, during, and after the query, to ensure
1213 // we don't goof up any of them.
1214 data.SetURL("{google:baseURL}search?q={searchTerms}"
1215 "#{google:originalQueryForSuggestion}x");
1216 TemplateURL url(NULL, data);
1217
1218 // Baseline: no command-line args, no |append_extra_query_params| flag.
1219 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1220 search_terms.original_query = ASCIIToUTF16("def");
1221 search_terms.accepted_suggestion = 0;
1222 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1223 url.url_ref().ReplaceSearchTerms(search_terms));
1224
1225 // Set the flag. Since there are no command-line args, this should have no
1226 // effect.
1227 search_terms.append_extra_query_params = true;
1228 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1229 url.url_ref().ReplaceSearchTerms(search_terms));
1230
1231 // Now append the command-line arg. This should be inserted into the query.
1232 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1233 switches::kExtraSearchQueryParams, "a=b");
1234 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
1235 url.url_ref().ReplaceSearchTerms(search_terms));
1236
1237 // Turn off the flag. Now the command-line arg should be ignored again.
1238 search_terms.append_extra_query_params = false;
1239 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1240 url.url_ref().ReplaceSearchTerms(search_terms));
1241}
[email protected]d5015ca2013-08-08 22:04:181242
1243// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171244TEST_F(TemplateURLTest, ReplacePageClassification) {
1245 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
[email protected]d5015ca2013-08-08 22:04:181246 TemplateURLData data;
1247 data.input_encodings.push_back("UTF-8");
1248 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
1249 TemplateURL url(NULL, data);
1250 EXPECT_TRUE(url.url_ref().IsValid());
1251 ASSERT_TRUE(url.url_ref().SupportsReplacement());
1252 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1253
1254 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1255 EXPECT_EQ("http://www.google.com/?q=foo", result);
1256
[email protected]12b5cd052013-11-08 20:59:371257 search_terms_args.page_classification = AutocompleteInput::NTP;
[email protected]d5015ca2013-08-08 22:04:181258 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1259 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1260
1261 search_terms_args.page_classification =
[email protected]0b1a1112013-08-28 05:23:311262 AutocompleteInput::HOME_PAGE;
[email protected]d5015ca2013-08-08 22:04:181263 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1264 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1265}
[email protected]2328ee22013-08-08 23:00:191266
1267// Test the IsSearchResults function.
1268TEST_F(TemplateURLTest, IsSearchResults) {
1269 TemplateURLData data;
1270 data.SetURL("http://bar/search?q={searchTerms}");
1271 data.instant_url = "http://bar/instant#q={searchTerms}";
[email protected]2767c0fd2013-08-16 17:44:161272 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191273 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1274 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1275 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1276 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
1277 TemplateURL search_provider(NULL, data);
1278
1279 const struct {
1280 const char* const url;
1281 bool result;
1282 } url_data[] = {
1283 { "http://bar/search?q=foo&oq=foo", true, },
1284 { "http://bar/?q=foo&oq=foo", true, },
1285 { "http://bar/#output=search&q=foo&oq=foo", true, },
1286 { "http://bar/webhp#q=foo&oq=foo", true, },
1287 { "http://bar/#q=foo&oq=foo", true, },
1288 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1289 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1290 { "http://bar/", false, },
1291 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161292 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191293 };
1294
1295 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_data); ++i) {
1296 EXPECT_EQ(url_data[i].result,
1297 search_provider.IsSearchURL(GURL(url_data[i].url)));
1298 }
1299}
[email protected]a048de8a2013-10-02 18:30:061300
1301TEST_F(TemplateURLTest, ReflectsBookmarkBarPinned) {
[email protected]30e91c72014-05-03 00:02:001302 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
[email protected]a048de8a2013-10-02 18:30:061303 TemplateURLData data;
1304 data.input_encodings.push_back("UTF-8");
1305 data.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
1306 TemplateURL url(NULL, data);
1307 EXPECT_TRUE(url.url_ref().IsValid());
1308 ASSERT_TRUE(url.url_ref().SupportsReplacement());
1309 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1310
1311 // Do not add the param when InstantExtended is suppressed on SRPs.
1312 url.url_ref_.showing_search_terms_ = false;
1313 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1314 EXPECT_EQ("http://www.google.com/?q=foo", result);
1315
1316 // Add the param when InstantExtended is not suppressed on SRPs.
1317 url.url_ref_.showing_search_terms_ = true;
1318 search_terms_args.bookmark_bar_pinned = false;
1319 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1320 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result);
1321
1322 url.url_ref_.showing_search_terms_ = true;
1323 search_terms_args.bookmark_bar_pinned = true;
1324 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1325 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result);
1326}
[email protected]9d70de12014-05-10 02:15:311327
1328#if defined(OS_ANDROID)
1329TEST_F(TemplateURLTest, AnswersHasVersion) {
1330 TemplateURLData data;
1331 UIThreadSearchTermsData::SetGoogleBaseURL("http://bar/");
1332 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t");
1333
1334 TemplateURL url(NULL, data);
1335 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1336 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1337 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result);
1338
1339 CommandLine::ForCurrentProcess()->AppendSwitch(
1340 switches::kEnableAnswersInSuggest);
1341 TemplateURL url2(NULL, data);
1342 result = url2.url_ref().ReplaceSearchTerms(search_terms_args);
1343 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result);
1344}
1345#endif