blob: 4329113e608a029c64d43992e09ebf5fb207c52e [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]b37bdfe2012-03-16 20:53:2726// TestSearchTermsData --------------------------------------------------------
27
[email protected]375bd7312010-08-30 22:18:1328// Simple implementation of SearchTermsData.
29class TestSearchTermsData : public SearchTermsData {
30 public:
[email protected]b37bdfe2012-03-16 20:53:2731 explicit TestSearchTermsData(const std::string& google_base_url);
[email protected]375bd7312010-08-30 22:18:1332
[email protected]b37bdfe2012-03-16 20:53:2733 virtual std::string GoogleBaseURLValue() const OVERRIDE;
[email protected]375bd7312010-08-30 22:18:1334
35 private:
36 std::string google_base_url_;
37
38 DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData);
39};
40
[email protected]b37bdfe2012-03-16 20:53:2741TestSearchTermsData::TestSearchTermsData(const std::string& google_base_url)
[email protected]2767c0fd2013-08-16 17:44:1642 : google_base_url_(google_base_url) {
[email protected]b37bdfe2012-03-16 20:53:2743}
44
45std::string TestSearchTermsData::GoogleBaseURLValue() const {
46 return google_base_url_;
47}
48
[email protected]b37bdfe2012-03-16 20:53:2749// TemplateURLTest ------------------------------------------------------------
50
[email protected]583844c2011-08-27 00:38:3551class TemplateURLTest : public testing::Test {
[email protected]d82443b2009-01-15 19:54:5652 public:
[email protected]b37bdfe2012-03-16 20:53:2753 void CheckSuggestBaseURL(const std::string& base_url,
54 const std::string& base_suggest_url) const;
[email protected]d82443b2009-01-15 19:54:5655};
56
[email protected]b37bdfe2012-03-16 20:53:2757void TemplateURLTest::CheckSuggestBaseURL(
58 const std::string& base_url,
59 const std::string& base_suggest_url) const {
60 TestSearchTermsData search_terms_data(base_url);
61 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue());
62}
63
64
65// Actual tests ---------------------------------------------------------------
66
[email protected]d82443b2009-01-15 19:54:5667TEST_F(TemplateURLTest, Defaults) {
[email protected]573889f22012-04-07 01:31:5468 TemplateURLData data;
69 EXPECT_FALSE(data.show_in_default_list);
70 EXPECT_FALSE(data.safe_for_autoreplace);
71 EXPECT_EQ(0, data.prepopulate_id);
[email protected]d82443b2009-01-15 19:54:5672}
73
74TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]573889f22012-04-07 01:31:5475 TemplateURLData data;
76 data.SetURL("{searchTerms}");
[email protected]16fca9b82012-04-23 18:40:2677 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:1378 EXPECT_TRUE(url.url_ref().IsValid());
[email protected]d82443b2009-01-15 19:54:5679}
80
81TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1982 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1983 const char* url;
[email protected]0085863a2013-12-06 21:19:0384 const base::string16 terms;
[email protected]34e24852012-01-31 18:43:5885 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:1986 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:3087 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1688 "http://foosea%20rch/bar" },
[email protected]400b133f2011-01-19 18:32:3089 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1690 "http://foosea%20rch/bar?boo=abc" },
[email protected]400b133f2011-01-19 18:32:3091 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1692 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]400b133f2011-01-19 18:32:3093 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
[email protected]c31a979c2012-05-31 23:57:1694 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:1995 };
96 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
97 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:5498 TemplateURLData data;
99 data.SetURL(value.url);
[email protected]16fca9b82012-04-23 18:40:26100 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13101 EXPECT_TRUE(url.url_ref().IsValid());
102 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04103 GURL result(url.url_ref().ReplaceSearchTerms(
104 TemplateURLRef::SearchTermsArgs(value.terms)));
[email protected]c31a979c2012-05-31 23:57:16105 ASSERT_TRUE(result.is_valid());
106 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:19107 }
[email protected]d82443b2009-01-15 19:54:56108}
109
110TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:54111 TemplateURLData data;
112 data.SetURL("http://foo{searchTerms}{count?}");
[email protected]16fca9b82012-04-23 18:40:26113 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13114 EXPECT_TRUE(url.url_ref().IsValid());
115 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04116 GURL result(url.url_ref().ReplaceSearchTerms(
117 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56118 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27119 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:56120}
121
122TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:54123 TemplateURLData data;
124 data.SetURL("http://foo{searchTerms}{count}");
[email protected]16fca9b82012-04-23 18:40:26125 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13126 EXPECT_TRUE(url.url_ref().IsValid());
127 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04128 GURL result(url.url_ref().ReplaceSearchTerms(
129 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56130 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27131 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56132}
133
134TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54135 TemplateURLData data;
136 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]16fca9b82012-04-23 18:40:26137 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13138 EXPECT_TRUE(url.url_ref().IsValid());
139 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04140 GURL result(url.url_ref().ReplaceSearchTerms(
141 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56142 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27143 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56144}
145
146TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54147 TemplateURLData data;
148 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[email protected]16fca9b82012-04-23 18:40:26149 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13150 EXPECT_TRUE(url.url_ref().IsValid());
151 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04152 GURL result(url.url_ref().ReplaceSearchTerms(
153 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56154 ASSERT_TRUE(result.is_valid());
[email protected]405aae22012-03-29 20:36:13155 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56156}
157
158TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54159 TemplateURLData data;
160 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[email protected]16fca9b82012-04-23 18:40:26161 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13162 EXPECT_TRUE(url.url_ref().IsValid());
163 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04164 GURL result(url.url_ref().ReplaceSearchTerms(
165 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56166 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27167 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56168}
169
[email protected]93b29062013-07-12 03:09:09170TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
171 const char kInvalidPostParamsString[] =
172 "unknown_template={UnknownTemplate},bad_value=bad{value},"
173 "{google:sbiSource}";
174 // List all accpectable parameter format in valid_post_params_string. it is
175 // expected like: "name0=,name1=value1,name2={template1}"
176 const char kValidPostParamsString[] =
177 "image_content={google:imageThumbnail},image_url={google:imageURL},"
178 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
[email protected]2f3bc6512013-08-28 03:56:27179 "constant_param=constant,width={google:imageOriginalWidth}";
[email protected]93b29062013-07-12 03:09:09180 const char KImageSearchURL[] = "http://foo.com/sbi";
181
182 TemplateURLData data;
183 data.image_url = KImageSearchURL;
184
185 // Try to parse invalid post parameters.
186 data.image_url_post_params = kInvalidPostParamsString;
187 TemplateURL url_bad(NULL, data);
188 ASSERT_FALSE(url_bad.image_url_ref().IsValid());
189 const TemplateURLRef::PostParams& bad_post_params =
190 url_bad.image_url_ref().post_params_;
191 ASSERT_EQ(2U, bad_post_params.size());
192 EXPECT_EQ("unknown_template", bad_post_params[0].first);
193 EXPECT_EQ("{UnknownTemplate}", bad_post_params[0].second);
194 EXPECT_EQ("bad_value", bad_post_params[1].first);
195 EXPECT_EQ("bad{value}", bad_post_params[1].second);
196
197 // Try to parse valid post parameters.
198 data.image_url_post_params = kValidPostParamsString;
199 TemplateURL url(NULL, data);
200 ASSERT_TRUE(url.image_url_ref().IsValid());
201 ASSERT_FALSE(url.image_url_ref().SupportsReplacement());
202
203 // Check term replacement.
204 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
205 search_args.image_thumbnail_content = "dummy-image-thumbnail";
206 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
[email protected]2f3bc6512013-08-28 03:56:27207 search_args.image_original_size = gfx::Size(10, 10);
[email protected]93b29062013-07-12 03:09:09208 // Replacement operation with no post_data buffer should still return
209 // the parsed URL.
210 GURL result(url.image_url_ref().ReplaceSearchTerms(search_args));
211 ASSERT_TRUE(result.is_valid());
212 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48213 TemplateURLRef::PostContent post_content;
[email protected]93b29062013-07-12 03:09:09214 TestSearchTermsData search_terms_data("http://X");
215 result = GURL(url.image_url_ref().ReplaceSearchTermsUsingTermsData(
[email protected]7c2bcc42013-08-01 04:03:48216 search_args, search_terms_data, &post_content));
[email protected]93b29062013-07-12 03:09:09217 ASSERT_TRUE(result.is_valid());
218 EXPECT_EQ(KImageSearchURL, result.spec());
[email protected]7c2bcc42013-08-01 04:03:48219 ASSERT_FALSE(post_content.first.empty());
220 ASSERT_FALSE(post_content.second.empty());
[email protected]93b29062013-07-12 03:09:09221
222 // Check parsed result of post parameters.
223 const TemplateURLRef::Replacements& replacements =
224 url.image_url_ref().replacements_;
225 const TemplateURLRef::PostParams& post_params =
226 url.image_url_ref().post_params_;
[email protected]2f3bc6512013-08-28 03:56:27227 EXPECT_EQ(7U, post_params.size());
[email protected]93b29062013-07-12 03:09:09228 for (TemplateURLRef::PostParams::const_iterator i = post_params.begin();
229 i != post_params.end(); ++i) {
230 TemplateURLRef::Replacements::const_iterator j = replacements.begin();
231 for (; j != replacements.end(); ++j) {
232 if (j->is_post_param && j->index ==
233 static_cast<size_t>(i - post_params.begin())) {
234 switch (j->type) {
[email protected]2f3bc6512013-08-28 03:56:27235 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH:
236 EXPECT_EQ("width", i->first);
237 EXPECT_EQ(
238 base::IntToString(search_args.image_original_size.width()),
239 i->second);
240 break;
[email protected]93b29062013-07-12 03:09:09241 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
242 EXPECT_EQ("image_content", i->first);
243 EXPECT_EQ(search_args.image_thumbnail_content, i->second);
244 break;
245 case TemplateURLRef::GOOGLE_IMAGE_URL:
246 EXPECT_EQ("image_url", i->first);
247 EXPECT_EQ(search_args.image_url.spec(), i->second);
248 break;
249 case TemplateURLRef::LANGUAGE:
250 EXPECT_EQ("language", i->first);
251 EXPECT_EQ("en", i->second);
252 break;
253 default:
254 ADD_FAILURE(); // Should never go here.
255 }
256 break;
257 }
258 }
259 if (j != replacements.end())
260 continue;
261 if (i->first == "empty_param") {
262 EXPECT_TRUE(i->second.empty());
263 } else if (i->first == "sbisrc") {
[email protected]7c2bcc42013-08-01 04:03:48264 EXPECT_FALSE(i->second.empty());
[email protected]93b29062013-07-12 03:09:09265 } else {
266 EXPECT_EQ("constant_param", i->first);
267 EXPECT_EQ("constant", i->second);
268 }
269 }
270}
271
[email protected]d88cb202011-08-17 20:03:01272// Test that setting the prepopulate ID from TemplateURL causes the stored
273// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44274TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54275 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20276 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]16fca9b82012-04-23 18:40:26277 TemplateURL url(NULL, data);
[email protected]1a257262011-06-28 22:15:44278 TemplateURLRef::Replacements replacements;
279 bool valid = false;
[email protected]243abf32012-05-15 18:28:20280 EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL(
[email protected]93b29062013-07-12 03:09:09281 "http://foo{fhqwhgads}bar", &replacements, NULL, &valid));
[email protected]1a257262011-06-28 22:15:44282 EXPECT_TRUE(replacements.empty());
283 EXPECT_TRUE(valid);
284
[email protected]573889f22012-04-07 01:31:54285 data.prepopulate_id = 123;
[email protected]16fca9b82012-04-23 18:40:26286 TemplateURL url2(NULL, data);
[email protected]243abf32012-05-15 18:28:20287 EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar",
[email protected]93b29062013-07-12 03:09:09288 &replacements, NULL,
289 &valid));
[email protected]1a257262011-06-28 22:15:44290 EXPECT_TRUE(replacements.empty());
291 EXPECT_TRUE(valid);
292}
293
[email protected]d82443b2009-01-15 19:54:56294TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54295 TemplateURLData data;
296 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]16fca9b82012-04-23 18:40:26297 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13298 EXPECT_TRUE(url.url_ref().IsValid());
299 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04300 GURL result(url.url_ref().ReplaceSearchTerms(
301 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56302 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27303 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56304}
305
306TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54307 TemplateURLData data;
308 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]16fca9b82012-04-23 18:40:26309 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13310 EXPECT_TRUE(url.url_ref().IsValid());
311 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04312 GURL result(url.url_ref().ReplaceSearchTerms(
313 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56314 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27315 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56316}
317
[email protected]375bd7312010-08-30 22:18:13318TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
319 struct SearchTermsCase {
320 const char* url;
[email protected]0085863a2013-12-06 21:19:03321 const base::string16 terms;
[email protected]375bd7312010-08-30 22:18:13322 const char* output;
323 } search_term_cases[] = {
[email protected]0085863a2013-12-06 21:19:03324 { "{google:baseURL}{language}{searchTerms}", base::string16(),
[email protected]b37bdfe2012-03-16 20:53:27325 "http://example.com/e/en" },
[email protected]0085863a2013-12-06 21:19:03326 { "{google:baseSuggestURL}{searchTerms}", base::string16(),
[email protected]014010e2011-10-01 04:12:44327 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13328 };
329
330 TestSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54331 TemplateURLData data;
[email protected]375bd7312010-08-30 22:18:13332 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
333 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54334 data.SetURL(value.url);
[email protected]16fca9b82012-04-23 18:40:26335 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13336 EXPECT_TRUE(url.url_ref().IsValid());
337 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04338 GURL result(url.url_ref().ReplaceSearchTermsUsingTermsData(
[email protected]93b29062013-07-12 03:09:09339 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL));
[email protected]375bd7312010-08-30 22:18:13340 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27341 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13342 }
343}
344
[email protected]d82443b2009-01-15 19:54:56345TEST_F(TemplateURLTest, URLRefTermToWide) {
346 struct ToWideCase {
347 const char* encoded_search_term;
[email protected]0085863a2013-12-06 21:19:03348 const base::string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56349 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30350 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56351 // Test some big-5 input.
[email protected]47e870b2013-02-24 21:14:53352 {"%a7A%A6%6e+to+you", WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56353 // Test some UTF-8 input. We should fall back to this when the encoding
354 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
355 // Big-5 trailing byte.
[email protected]47e870b2013-02-24 21:14:53356 {"%e4%bd%a05%e5%a5%bd+to+you", WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56357 // Undecodable input should stay escaped.
[email protected]47e870b2013-02-24 21:14:53358 {"%91%01+abcd", WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44359 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30360 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44361 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30362 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56363 };
364
[email protected]d82443b2009-01-15 19:54:56365 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54366 TemplateURLData data;
367 data.SetURL("http://foo?q={searchTerms}");
368 data.input_encodings.push_back("big-5");
[email protected]16fca9b82012-04-23 18:40:26369 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13370 EXPECT_TRUE(url.url_ref().IsValid());
371 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]f63ae312009-02-04 17:58:46372 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07373 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13374 url.url_ref().SearchTermToString16(
375 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56376 }
377}
378
[email protected]d82443b2009-01-15 19:54:56379TEST_F(TemplateURLTest, DisplayURLToURLRef) {
380 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19381 const std::string url;
[email protected]0085863a2013-12-06 21:19:03382 const base::string16 expected_result;
[email protected]b37bdfe2012-03-16 20:53:27383 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19384 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30385 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19386 { "http://X",
[email protected]400b133f2011-01-19 18:32:30387 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19388 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30389 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19390 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30391 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56392 };
[email protected]573889f22012-04-07 01:31:54393 TemplateURLData data;
[email protected]b37bdfe2012-03-16 20:53:27394 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54395 data.SetURL(test_data[i].url);
[email protected]16fca9b82012-04-23 18:40:26396 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13397 EXPECT_EQ(test_data[i].expected_result, url.url_ref().DisplayURL());
[email protected]b37bdfe2012-03-16 20:53:27398 EXPECT_EQ(test_data[i].url,
[email protected]360ba052012-04-04 17:26:13399 TemplateURLRef::DisplayURLToURLRef(url.url_ref().DisplayURL()));
[email protected]d82443b2009-01-15 19:54:56400 }
401}
402
403TEST_F(TemplateURLTest, ReplaceSearchTerms) {
404 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19405 const std::string url;
[email protected]d82443b2009-01-15 19:54:56406 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27407 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19408 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56409 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19410 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56411 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19412 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56413 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19414 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56415 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19416 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56417 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19418 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56419 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19420 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56421 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19422 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56423 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19424 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56425 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19426 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56427 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19428 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56429 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19430 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56431 "http://foo/UTF-8a{language}aXa" },
432 };
[email protected]573889f22012-04-07 01:31:54433 TemplateURLData data;
434 data.input_encodings.push_back("UTF-8");
[email protected]b37bdfe2012-03-16 20:53:27435 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54436 data.SetURL(test_data[i].url);
[email protected]16fca9b82012-04-23 18:40:26437 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13438 EXPECT_TRUE(url.url_ref().IsValid());
[email protected]3954c3a2012-04-10 20:17:55439 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]b37bdfe2012-03-16 20:53:27440 std::string expected_result = test_data[i].expected_result;
[email protected]d82443b2009-01-15 19:54:56441 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}",
[email protected]d70539de2009-06-24 22:17:06442 g_browser_process->GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04443 GURL result(url.url_ref().ReplaceSearchTerms(
444 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]b37bdfe2012-03-16 20:53:27445 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56446 EXPECT_EQ(expected_result, result.spec());
447 }
448}
449
450
451// Tests replacing search terms in various encodings and making sure the
452// generated URL matches the expected value.
453TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
454 struct TestData {
455 const std::string encoding;
[email protected]0085863a2013-12-06 21:19:03456 const base::string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19457 const std::string url;
[email protected]d82443b2009-01-15 19:54:56458 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27459 } test_data[] = {
[email protected]47e870b2013-02-24 21:14:53460 { "BIG5", WideToUTF16(L"\x60BD"),
[email protected]400b133f2011-01-19 18:32:30461 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17462 "http://foo/?%B1~BIG5" },
[email protected]400b133f2011-01-19 18:32:30463 { "UTF-8", ASCIIToUTF16("blah"),
464 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17465 "http://foo/?blahUTF-8" },
[email protected]34e24852012-01-31 18:43:58466 { "Shift_JIS", UTF8ToUTF16("\xe3\x81\x82"),
467 "http://foo/{searchTerms}/bar",
468 "http://foo/%82%A0/bar"},
469 { "Shift_JIS", UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
470 "http://foo/{searchTerms}/bar",
471 "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56472 };
[email protected]573889f22012-04-07 01:31:54473 TemplateURLData data;
[email protected]b37bdfe2012-03-16 20:53:27474 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54475 data.SetURL(test_data[i].url);
476 data.input_encodings.clear();
477 data.input_encodings.push_back(test_data[i].encoding);
[email protected]16fca9b82012-04-23 18:40:26478 TemplateURL url(NULL, data);
[email protected]3954c3a2012-04-10 20:17:55479 EXPECT_TRUE(url.url_ref().IsValid());
480 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04481 GURL result(url.url_ref().ReplaceSearchTerms(
482 TemplateURLRef::SearchTermsArgs(test_data[i].search_term)));
483 ASSERT_TRUE(result.is_valid());
484 EXPECT_EQ(test_data[i].expected_result, result.spec());
485 }
486}
487
488// Tests replacing assisted query stats (AQS) in various scenarios.
489TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
490 struct TestData {
[email protected]0085863a2013-12-06 21:19:03491 const base::string16 search_term;
[email protected]bca359b2012-06-24 07:53:04492 const std::string aqs;
493 const std::string base_url;
494 const std::string url;
495 const std::string expected_result;
496 } test_data[] = {
497 // No HTTPS, no AQS.
498 { ASCIIToUTF16("foo"),
499 "chrome.0.0l6",
500 "http://foo/",
501 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
502 "http://foo/?foo" },
503 // HTTPS available, AQS should be replaced.
504 { ASCIIToUTF16("foo"),
505 "chrome.0.0l6",
506 "https://foo/",
507 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
508 "https://foo/?fooaqs=chrome.0.0l6&" },
509 // HTTPS available, however AQS is empty.
510 { ASCIIToUTF16("foo"),
511 "",
512 "https://foo/",
513 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
514 "https://foo/?foo" },
515 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
516 { ASCIIToUTF16("foo"),
517 "chrome.0.0l6",
518 "",
519 "http://foo?{searchTerms}{google:assistedQueryStats}",
520 "http://foo/?foo" },
521 // A non-Google search provider with HTTPS should allow AQS.
522 { ASCIIToUTF16("foo"),
523 "chrome.0.0l6",
524 "",
525 "https://foo?{searchTerms}{google:assistedQueryStats}",
526 "https://foo/?fooaqs=chrome.0.0l6&" },
527 };
528 TemplateURLData data;
529 data.input_encodings.push_back("UTF-8");
530 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
531 data.SetURL(test_data[i].url);
532 TemplateURL url(NULL, data);
533 EXPECT_TRUE(url.url_ref().IsValid());
534 ASSERT_TRUE(url.url_ref().SupportsReplacement());
535 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
536 search_terms_args.assisted_query_stats = test_data[i].aqs;
537 UIThreadSearchTermsData::SetGoogleBaseURL(test_data[i].base_url);
538 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
[email protected]b37bdfe2012-03-16 20:53:27539 ASSERT_TRUE(result.is_valid());
540 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56541 }
542}
543
[email protected]00790562012-12-14 09:57:16544// Tests replacing cursor position.
545TEST_F(TemplateURLTest, ReplaceCursorPosition) {
546 struct TestData {
[email protected]0085863a2013-12-06 21:19:03547 const base::string16 search_term;
[email protected]00790562012-12-14 09:57:16548 size_t cursor_position;
549 const std::string url;
550 const std::string expected_result;
551 } test_data[] = {
552 { ASCIIToUTF16("foo"),
[email protected]0085863a2013-12-06 21:19:03553 base::string16::npos,
[email protected]00790562012-12-14 09:57:16554 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
555 "http://www.google.com/?foo&" },
556 { ASCIIToUTF16("foo"),
557 2,
558 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
559 "http://www.google.com/?foo&cp=2&" },
560 { ASCIIToUTF16("foo"),
561 15,
562 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
563 "http://www.google.com/?foo&cp=15&" },
564 };
565 TemplateURLData data;
566 data.input_encodings.push_back("UTF-8");
567 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
568 data.SetURL(test_data[i].url);
569 TemplateURL url(NULL, data);
570 EXPECT_TRUE(url.url_ref().IsValid());
571 ASSERT_TRUE(url.url_ref().SupportsReplacement());
572 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
573 search_terms_args.cursor_position = test_data[i].cursor_position;
574 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
575 ASSERT_TRUE(result.is_valid());
576 EXPECT_EQ(test_data[i].expected_result, result.spec());
577 }
578}
579
[email protected]9b9fa672013-11-07 06:04:52580// Tests replacing currentPageUrl.
581TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) {
[email protected]800569d2013-05-06 07:38:48582 struct TestData {
[email protected]0085863a2013-12-06 21:19:03583 const base::string16 search_term;
[email protected]9b9fa672013-11-07 06:04:52584 const std::string current_page_url;
[email protected]800569d2013-05-06 07:38:48585 const std::string url;
586 const std::string expected_result;
587 } test_data[] = {
588 { ASCIIToUTF16("foo"),
589 "http://www.google.com/",
[email protected]9b9fa672013-11-07 06:04:52590 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}",
[email protected]800569d2013-05-06 07:38:48591 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
592 { ASCIIToUTF16("foo"),
593 "",
[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&" },
596 { ASCIIToUTF16("foo"),
597 "http://g.com/+-/*&=",
[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&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
600 };
601 TemplateURLData data;
602 data.input_encodings.push_back("UTF-8");
603 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
604 data.SetURL(test_data[i].url);
605 TemplateURL url(NULL, data);
606 EXPECT_TRUE(url.url_ref().IsValid());
607 ASSERT_TRUE(url.url_ref().SupportsReplacement());
608 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
[email protected]9b9fa672013-11-07 06:04:52609 search_terms_args.current_page_url = test_data[i].current_page_url;
[email protected]800569d2013-05-06 07:38:48610 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
611 ASSERT_TRUE(result.is_valid());
612 EXPECT_EQ(test_data[i].expected_result, result.spec());
613 }
614}
615
[email protected]d82443b2009-01-15 19:54:56616TEST_F(TemplateURLTest, Suggestions) {
617 struct TestData {
618 const int accepted_suggestion;
[email protected]0085863a2013-12-06 21:19:03619 const base::string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56620 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27621 } test_data[] = {
[email protected]0085863a2013-12-06 21:19:03622 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(),
[email protected]29653892013-03-02 19:25:37623 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30624 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37625 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03626 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(),
[email protected]29653892013-03-02 19:25:37627 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30628 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37629 "http://bar/foo?q=foobar" },
[email protected]0085863a2013-12-06 21:19:03630 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" },
[email protected]29653892013-03-02 19:25:37631 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56632 };
[email protected]573889f22012-04-07 01:31:54633 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37634 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
635 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54636 data.input_encodings.push_back("UTF-8");
[email protected]16fca9b82012-04-23 18:40:26637 TemplateURL url(NULL, data);
[email protected]3954c3a2012-04-10 20:17:55638 EXPECT_TRUE(url.url_ref().IsValid());
[email protected]360ba052012-04-04 17:26:13639 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]b37bdfe2012-03-16 20:53:27640 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04641 TemplateURLRef::SearchTermsArgs search_terms_args(
642 ASCIIToUTF16("foobar"));
643 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
644 search_terms_args.original_query =
645 test_data[i].original_query_for_suggestion;
646 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
[email protected]b37bdfe2012-03-16 20:53:27647 ASSERT_TRUE(result.is_valid());
648 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56649 }
650}
651
[email protected]81f808de2009-09-25 01:36:34652TEST_F(TemplateURLTest, RLZ) {
[email protected]0085863a2013-12-06 21:19:03653 base::string16 rlz_string;
[email protected]81d9b72d2012-03-26 22:29:17654#if defined(ENABLE_RLZ)
655 std::string brand;
656 if (google_util::GetBrand(&brand) && !brand.empty() &&
657 !google_util::IsOrganic(brand)) {
[email protected]7e6f4ed5c2012-10-31 21:10:34658 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz_string);
[email protected]81d9b72d2012-03-26 22:29:17659 }
[email protected]fd6d8822012-12-08 06:56:11660#elif defined(OS_ANDROID)
661 SearchTermsDataAndroid::rlz_parameter_value_.Get() =
662 ASCIIToUTF16("android_test");
663 rlz_string = SearchTermsDataAndroid::rlz_parameter_value_.Get();
[email protected]58b64332010-08-13 16:09:39664#endif
[email protected]d82443b2009-01-15 19:54:56665
[email protected]573889f22012-04-07 01:31:54666 TemplateURLData data;
667 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]16fca9b82012-04-23 18:40:26668 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13669 EXPECT_TRUE(url.url_ref().IsValid());
670 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04671 GURL result(url.url_ref().ReplaceSearchTerms(
672 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x"))));
[email protected]d82443b2009-01-15 19:54:56673 ASSERT_TRUE(result.is_valid());
[email protected]12bd05872009-03-17 19:25:06674 std::string expected_url = "http://bar/?";
[email protected]b37bdfe2012-03-16 20:53:27675 if (!rlz_string.empty())
[email protected]8ead4c022012-03-17 02:09:50676 expected_url += "rlz=" + UTF16ToUTF8(rlz_string) + "&";
[email protected]12bd05872009-03-17 19:25:06677 expected_url += "x";
[email protected]b37bdfe2012-03-16 20:53:27678 EXPECT_EQ(expected_url, result.spec());
[email protected]d82443b2009-01-15 19:54:56679}
680
681TEST_F(TemplateURLTest, HostAndSearchTermKey) {
682 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19683 const std::string url;
[email protected]d82443b2009-01-15 19:54:56684 const std::string host;
685 const std::string path;
686 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54687 } test_data[] = {
[email protected]7c60f5042013-02-14 03:39:32688 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56689
690 // No query key should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32691 { "http://blah/{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56692
693 // No term should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32694 { "http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56695
696 // Multiple terms should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32697 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56698
699 // Term in the host shouldn't match.
[email protected]7c60f5042013-02-14 03:39:32700 { "http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56701
[email protected]7c60f5042013-02-14 03:39:32702 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
703 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56704
705 // Single term with extra chars in value should match.
[email protected]7c60f5042013-02-14 03:39:32706 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56707 };
708
[email protected]573889f22012-04-07 01:31:54709 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
710 TemplateURLData data;
711 data.SetURL(test_data[i].url);
[email protected]16fca9b82012-04-23 18:40:26712 TemplateURL url(NULL, data);
[email protected]573889f22012-04-07 01:31:54713 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost());
714 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath());
715 EXPECT_EQ(test_data[i].search_term_key, url.url_ref().GetSearchTermKey());
[email protected]d82443b2009-01-15 19:54:56716 }
717}
718
719TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
720 static const struct {
[email protected]ddd231e2010-06-29 20:35:19721 const char* const base_url;
722 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56723 } data[] = {
[email protected]014010e2011-10-01 04:12:44724 { "http://google.com/", "http://google.com/complete/", },
725 { "http://www.google.com/", "http://www.google.com/complete/", },
726 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
727 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
728 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56729 };
730
[email protected]f63ae312009-02-04 17:58:46731 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i)
[email protected]d82443b2009-01-15 19:54:56732 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
733}
734
[email protected]81c6ef62010-01-21 09:58:47735TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19736 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:54737 TemplateURLData data;
738 data.SetURL(parsed_url);
[email protected]16fca9b82012-04-23 18:40:26739 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47740 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:13741 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19742 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47743 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27744 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47745 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
746}
747
748TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:20749 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:54750 TemplateURLData data;
751 data.SetURL(parsed_url);
[email protected]16fca9b82012-04-23 18:40:26752 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47753 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:44754
755 // By default, TemplateURLRef should not consider itself prepopulated.
756 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:13757 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20758 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:44759 EXPECT_TRUE(replacements.empty());
760
761 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:20762 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:54763 data.prepopulate_id = 1;
[email protected]16fca9b82012-04-23 18:40:26764 TemplateURL url2(NULL, data);
[email protected]495c30b2012-05-15 18:48:15765 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20766 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47767 EXPECT_TRUE(replacements.empty());
768}
769
770TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]16fca9b82012-04-23 18:40:26771 TemplateURL url(NULL, TemplateURLData());
[email protected]81c6ef62010-01-21 09:58:47772 TemplateURLRef::Replacements replacements;
773 bool valid = false;
[email protected]b37bdfe2012-03-16 20:53:27774 EXPECT_EQ(std::string(),
[email protected]93b29062013-07-12 03:09:09775 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47776 EXPECT_TRUE(replacements.empty());
777 EXPECT_TRUE(valid);
778}
779
780TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:54781 TemplateURLData data;
782 data.SetURL("{");
[email protected]16fca9b82012-04-23 18:40:26783 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47784 TemplateURLRef::Replacements replacements;
785 bool valid = false;
[email protected]93b29062013-07-12 03:09:09786 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
787 &valid));
[email protected]81c6ef62010-01-21 09:58:47788 EXPECT_TRUE(replacements.empty());
789 EXPECT_FALSE(valid);
790}
791
792TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:54793 TemplateURLData data;
794 data.SetURL("{}");
[email protected]16fca9b82012-04-23 18:40:26795 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47796 TemplateURLRef::Replacements replacements;
797 bool valid = false;
[email protected]93b29062013-07-12 03:09:09798 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47799 EXPECT_TRUE(replacements.empty());
800 EXPECT_TRUE(valid);
801}
802
803TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:54804 TemplateURLData data;
805 data.SetURL("{}{{%s}}");
[email protected]16fca9b82012-04-23 18:40:26806 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47807 TemplateURLRef::Replacements replacements;
808 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19809 EXPECT_EQ("{}{}",
[email protected]93b29062013-07-12 03:09:09810 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
811 &valid));
[email protected]81c6ef62010-01-21 09:58:47812 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27813 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47814 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
815 EXPECT_TRUE(valid);
816}
817
818TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:54819 TemplateURLData data;
820 data.SetURL("{%s");
[email protected]16fca9b82012-04-23 18:40:26821 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47822 TemplateURLRef::Replacements replacements;
823 bool valid = false;
[email protected]360ba052012-04-04 17:26:13824 EXPECT_EQ("{",
[email protected]93b29062013-07-12 03:09:09825 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
826 &valid));
[email protected]81c6ef62010-01-21 09:58:47827 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27828 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47829 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
830 EXPECT_TRUE(valid);
831}
[email protected]5b3078752012-10-09 18:54:16832
[email protected]fd6d8822012-12-08 06:56:11833#if defined(OS_ANDROID)
834TEST_F(TemplateURLTest, SearchClient) {
835 const std::string base_url_str("http://google.com/?");
836 const std::string terms_str("{searchTerms}&{google:searchClient}");
837 const std::string full_url_str = base_url_str + terms_str;
[email protected]0085863a2013-12-06 21:19:03838 const base::string16 terms(ASCIIToUTF16(terms_str));
[email protected]fd6d8822012-12-08 06:56:11839 UIThreadSearchTermsData::SetGoogleBaseURL(base_url_str);
840
841 TemplateURLData data;
842 data.SetURL(full_url_str);
843 TemplateURL url(NULL, data);
844 EXPECT_TRUE(url.url_ref().IsValid());
845 ASSERT_TRUE(url.url_ref().SupportsReplacement());
846 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
847
848 // Check that the URL is correct when a client is not present.
849 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
850 ASSERT_TRUE(result.is_valid());
851 EXPECT_EQ("http://google.com/?foobar&", result.spec());
852
853 // Check that the URL is correct when a client is present.
854 SearchTermsDataAndroid::search_client_.Get() = "android_test";
855 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args));
856 ASSERT_TRUE(result_2.is_valid());
857 EXPECT_EQ("http://google.com/?foobar&client=android_test&", result_2.spec());
858}
859#endif
860
[email protected]5b3078752012-10-09 18:54:16861TEST_F(TemplateURLTest, GetURLNoInstantURL) {
862 TemplateURLData data;
863 data.SetURL("http://google.com/?q={searchTerms}");
864 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
865 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
866 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
867 TemplateURL url(NULL, data);
868 ASSERT_EQ(3U, url.URLCount());
869 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
870 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
871 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
872}
873
874TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
875 TemplateURLData data;
876 data.SetURL("http://google.com/?q={searchTerms}");
877 data.instant_url = "http://google.com/instant#q={searchTerms}";
878 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
879 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
880 TemplateURL url(NULL, data);
881 ASSERT_EQ(3U, url.URLCount());
882 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
883 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
884 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
885}
886
887TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
888 TemplateURLData data;
889 data.SetURL("http://www.google.co.uk/");
890 TemplateURL url(NULL, data);
891 ASSERT_EQ(1U, url.URLCount());
892 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0));
893}
894
895TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
896 TemplateURLData data;
897 data.SetURL("http://google.com/?q={searchTerms}");
898 data.instant_url = "http://google.com/instant#q={searchTerms}";
899 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
900 data.alternate_urls.push_back(
901 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
902 TemplateURL url(NULL, data);
[email protected]0085863a2013-12-06 21:19:03903 base::string16 result;
[email protected]5b3078752012-10-09 18:54:16904
905 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
906 GURL("http://google.com/?q=something"), &result));
[email protected]4076ea62013-01-09 01:47:19907 EXPECT_EQ(ASCIIToUTF16("something"), result);
908
909 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
910 GURL("http://google.com/?espv&q=something"), &result));
911 EXPECT_EQ(ASCIIToUTF16("something"), result);
912
913 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
914 GURL("http://google.com/?espv=1&q=something"), &result));
915 EXPECT_EQ(ASCIIToUTF16("something"), result);
916
917 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
918 GURL("http://google.com/?espv=0&q=something"), &result));
919 EXPECT_EQ(ASCIIToUTF16("something"), result);
920
921 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
922 GURL("http://google.com/alt/#q=something"), &result));
923 EXPECT_EQ(ASCIIToUTF16("something"), result);
924
925 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
926 GURL("http://google.com/alt/#espv&q=something"), &result));
927 EXPECT_EQ(ASCIIToUTF16("something"), result);
928
929 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
930 GURL("http://google.com/alt/#espv=1&q=something"), &result));
931 EXPECT_EQ(ASCIIToUTF16("something"), result);
932
933 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
934 GURL("http://google.com/alt/#espv=0&q=something"), &result));
[email protected]5b3078752012-10-09 18:54:16935 EXPECT_EQ(ASCIIToUTF16("something"), result);
936
937 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
938 GURL("http://google.ca/?q=something"), &result));
939 EXPECT_EQ(string16(), result);
940
941 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]67d8b752013-04-03 17:33:27942 GURL("http://google.ca/?q=something&q=anything"), &result));
943 EXPECT_EQ(string16(), result);
944
945 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]5b3078752012-10-09 18:54:16946 GURL("http://google.com/foo/?q=foo"), &result));
947 EXPECT_EQ(string16(), result);
948
[email protected]32e2d81b2012-10-16 19:03:25949 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]5b3078752012-10-09 18:54:16950 GURL("https://google.com/?q=foo"), &result));
[email protected]32e2d81b2012-10-16 19:03:25951 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:16952
953 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
954 GURL("http://google.com:8080/?q=foo"), &result));
955 EXPECT_EQ(string16(), result);
956
957 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
958 GURL("http://google.com/?q=1+2+3&b=456"), &result));
959 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
960
961 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
962 GURL("http://google.com/alt/?q=123#q=456"), &result));
963 EXPECT_EQ(ASCIIToUTF16("456"), result);
964
965 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
966 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), &result));
967 EXPECT_EQ(ASCIIToUTF16("123"), result);
968
969 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
970 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), &result));
971 EXPECT_EQ(ASCIIToUTF16("789"), result);
972
973 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
974 GURL("http://google.com/alt/?q="), &result));
975 EXPECT_EQ(string16(), result);
976
977 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
978 GURL("http://google.com/alt/?#q="), &result));
979 EXPECT_EQ(string16(), result);
980
981 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
982 GURL("http://google.com/alt/?q=#q="), &result));
983 EXPECT_EQ(string16(), result);
984
985 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
986 GURL("http://google.com/alt/?q=123#q="), &result));
987 EXPECT_EQ(string16(), result);
988
989 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
990 GURL("http://google.com/alt/?q=#q=123"), &result));
991 EXPECT_EQ(ASCIIToUTF16("123"), result);
992}
[email protected]4076ea62013-01-09 01:47:19993
994TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
995 TemplateURLData data;
996 data.SetURL("http://google.com/?q={searchTerms}");
997 data.instant_url = "http://google.com/instant#q={searchTerms}";
998 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
999 data.alternate_urls.push_back(
1000 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1001 data.search_terms_replacement_key = "espv";
1002 TemplateURL url(NULL, data);
1003
1004 // Test with instant enabled required.
1005 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1006 GURL("http://google.com/")));
1007
1008 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1009 GURL("http://google.com/?espv")));
1010
1011 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1012 GURL("http://google.com/#espv")));
1013
1014 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1015 GURL("http://google.com/?q=something")));
1016
1017 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1018 GURL("http://google.com/?q=something&espv")));
1019
1020 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1021 GURL("http://google.com/?q=something&espv=1")));
1022
1023 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1024 GURL("http://google.com/?q=something&espv=0")));
1025
1026 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1027 GURL("http://google.com/?espv&q=something")));
1028
1029 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1030 GURL("http://google.com/?espv=1&q=something")));
1031
1032 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1033 GURL("http://google.com/?espv=0&q=something")));
1034
1035 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1036 GURL("http://google.com/alt/#q=something")));
1037
1038 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1039 GURL("http://google.com/alt/#q=something&espv")));
1040
1041 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1042 GURL("http://google.com/alt/#q=something&espv=1")));
1043
1044 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1045 GURL("http://google.com/alt/#q=something&espv=0")));
1046
1047 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1048 GURL("http://google.com/alt/#espv&q=something")));
1049
1050 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1051 GURL("http://google.com/alt/#espv=1&q=something")));
1052
1053 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1054 GURL("http://google.com/alt/#espv=0&q=something")));
1055
1056 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1057 GURL("http://google.com/?espv#q=something")));
1058
1059 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1060 GURL("http://google.com/?espv=1#q=something")));
1061
1062 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1063 GURL("http://google.com/?q=something#espv")));
1064
1065 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1066 GURL("http://google.com/?q=something#espv=1")));
1067
1068 // This does not ensure the domain matches.
1069 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1070 GURL("http://bing.com/?espv")));
1071
1072 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1073 GURL("http://bing.com/#espv")));
1074}
[email protected]f62e30f52013-03-23 03:45:151075
1076TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1077 TemplateURLData data;
1078 data.SetURL("http://google.com/?q={searchTerms}");
1079 data.instant_url = "http://google.com/instant#q={searchTerms}";
1080 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1081 data.alternate_urls.push_back(
1082 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1083 TemplateURL url(NULL, data);
1084 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1085 GURL result;
1086
1087 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1088 GURL("http://google.com/?q=something"), search_terms, &result));
1089 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result);
1090
1091 result = GURL("http://should.not.change.com");
1092 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1093 GURL("http://google.ca/?q=something"), search_terms, &result));
1094 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1095
1096 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1097 GURL("http://google.com/foo/?q=foo"), search_terms, &result));
1098
1099 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1100 GURL("https://google.com/?q=foo"), search_terms, &result));
1101 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result);
1102
1103 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1104 GURL("http://google.com:8080/?q=foo"), search_terms, &result));
1105
1106 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1107 GURL("http://google.com/?q=1+2+3&b=456"), search_terms, &result));
1108 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result);
1109
1110 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1111 // template_url.cc for details.
1112 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1113 GURL("http://google.com/alt/?q=123#q=456"), search_terms, &result));
1114 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result);
1115
1116 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1117 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
1118 &result));
1119 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"),
1120 result);
1121
1122 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1123 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1124 search_terms, &result));
1125 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1126 "#j=abc&q=Bob Morane&h=def9"), result);
1127
1128 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1129 GURL("http://google.com/alt/?q="), search_terms, &result));
1130
1131 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1132 GURL("http://google.com/alt/?#q="), search_terms, &result));
1133
1134 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1135 GURL("http://google.com/alt/?q=#q="), search_terms, &result));
1136
1137 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1138 GURL("http://google.com/alt/?q=123#q="), search_terms, &result));
1139
1140 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1141 GURL("http://google.com/alt/?q=#q=123"), search_terms, &result));
1142 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result);
1143}
[email protected]56fa29592013-07-02 20:25:531144
[email protected]621ade062013-10-28 06:27:431145// Test the |suggest_query_params| field of SearchTermsArgs.
1146TEST_F(TemplateURLTest, SuggestQueryParams) {
1147 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1148 TemplateURLData data;
1149 // Pick a URL with replacements before, during, and after the query, to ensure
1150 // we don't goof up any of them.
1151 data.SetURL("{google:baseURL}search?q={searchTerms}"
1152 "#{google:originalQueryForSuggestion}x");
1153 TemplateURL url(NULL, data);
1154
1155 // Baseline: no |suggest_query_params| field.
1156 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1157 search_terms.original_query = ASCIIToUTF16("def");
1158 search_terms.accepted_suggestion = 0;
1159 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1160 url.url_ref().ReplaceSearchTerms(search_terms));
1161
1162 // Set the suggest_query_params.
1163 search_terms.suggest_query_params = "pq=xyz";
1164 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x",
1165 url.url_ref().ReplaceSearchTerms(search_terms));
1166
1167 // Add extra_query_params in the mix, and ensure it works.
1168 search_terms.append_extra_query_params = true;
1169 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1170 switches::kExtraSearchQueryParams, "a=b");
1171 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x",
1172 url.url_ref().ReplaceSearchTerms(search_terms));
1173}
1174
[email protected]56fa29592013-07-02 20:25:531175// Test the |append_extra_query_params| field of SearchTermsArgs.
1176TEST_F(TemplateURLTest, ExtraQueryParams) {
1177 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1178 TemplateURLData data;
1179 // Pick a URL with replacements before, during, and after the query, to ensure
1180 // we don't goof up any of them.
1181 data.SetURL("{google:baseURL}search?q={searchTerms}"
1182 "#{google:originalQueryForSuggestion}x");
1183 TemplateURL url(NULL, data);
1184
1185 // Baseline: no command-line args, no |append_extra_query_params| flag.
1186 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1187 search_terms.original_query = ASCIIToUTF16("def");
1188 search_terms.accepted_suggestion = 0;
1189 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1190 url.url_ref().ReplaceSearchTerms(search_terms));
1191
1192 // Set the flag. Since there are no command-line args, this should have no
1193 // effect.
1194 search_terms.append_extra_query_params = true;
1195 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1196 url.url_ref().ReplaceSearchTerms(search_terms));
1197
1198 // Now append the command-line arg. This should be inserted into the query.
1199 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1200 switches::kExtraSearchQueryParams, "a=b");
1201 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
1202 url.url_ref().ReplaceSearchTerms(search_terms));
1203
1204 // Turn off the flag. Now the command-line arg should be ignored again.
1205 search_terms.append_extra_query_params = false;
1206 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1207 url.url_ref().ReplaceSearchTerms(search_terms));
1208}
[email protected]d5015ca2013-08-08 22:04:181209
1210// Tests replacing pageClassification.
[email protected]57ac99b92013-11-13 01:30:171211TEST_F(TemplateURLTest, ReplacePageClassification) {
1212 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
[email protected]d5015ca2013-08-08 22:04:181213 TemplateURLData data;
1214 data.input_encodings.push_back("UTF-8");
1215 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}");
1216 TemplateURL url(NULL, data);
1217 EXPECT_TRUE(url.url_ref().IsValid());
1218 ASSERT_TRUE(url.url_ref().SupportsReplacement());
1219 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1220
1221 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1222 EXPECT_EQ("http://www.google.com/?q=foo", result);
1223
[email protected]12b5cd052013-11-08 20:59:371224 search_terms_args.page_classification = AutocompleteInput::NTP;
[email protected]d5015ca2013-08-08 22:04:181225 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1226 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result);
1227
1228 search_terms_args.page_classification =
[email protected]0b1a1112013-08-28 05:23:311229 AutocompleteInput::HOME_PAGE;
[email protected]d5015ca2013-08-08 22:04:181230 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1231 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result);
1232}
[email protected]2328ee22013-08-08 23:00:191233
1234// Test the IsSearchResults function.
1235TEST_F(TemplateURLTest, IsSearchResults) {
1236 TemplateURLData data;
1237 data.SetURL("http://bar/search?q={searchTerms}");
1238 data.instant_url = "http://bar/instant#q={searchTerms}";
[email protected]2767c0fd2013-08-16 17:44:161239 data.new_tab_url = "http://bar/newtab";
[email protected]2328ee22013-08-08 23:00:191240 data.alternate_urls.push_back("http://bar/?q={searchTerms}");
1241 data.alternate_urls.push_back("http://bar/#q={searchTerms}");
1242 data.alternate_urls.push_back("http://bar/search#q{searchTerms}");
1243 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}");
1244 TemplateURL search_provider(NULL, data);
1245
1246 const struct {
1247 const char* const url;
1248 bool result;
1249 } url_data[] = {
1250 { "http://bar/search?q=foo&oq=foo", true, },
1251 { "http://bar/?q=foo&oq=foo", true, },
1252 { "http://bar/#output=search&q=foo&oq=foo", true, },
1253 { "http://bar/webhp#q=foo&oq=foo", true, },
1254 { "http://bar/#q=foo&oq=foo", true, },
1255 { "http://bar/?ext=foo&q=foo#ref=bar", true, },
1256 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, },
1257 { "http://bar/", false, },
1258 { "http://foo/", false, },
[email protected]2767c0fd2013-08-16 17:44:161259 { "http://bar/newtab", false, },
[email protected]2328ee22013-08-08 23:00:191260 };
1261
1262 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_data); ++i) {
1263 EXPECT_EQ(url_data[i].result,
1264 search_provider.IsSearchURL(GURL(url_data[i].url)));
1265 }
1266}
[email protected]a048de8a2013-10-02 18:30:061267
1268TEST_F(TemplateURLTest, ReflectsBookmarkBarPinned) {
1269 TemplateURLData data;
1270 data.input_encodings.push_back("UTF-8");
1271 data.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}");
1272 TemplateURL url(NULL, data);
1273 EXPECT_TRUE(url.url_ref().IsValid());
1274 ASSERT_TRUE(url.url_ref().SupportsReplacement());
1275 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo"));
1276
1277 // Do not add the param when InstantExtended is suppressed on SRPs.
1278 url.url_ref_.showing_search_terms_ = false;
1279 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1280 EXPECT_EQ("http://www.google.com/?q=foo", result);
1281
1282 // Add the param when InstantExtended is not suppressed on SRPs.
1283 url.url_ref_.showing_search_terms_ = true;
1284 search_terms_args.bookmark_bar_pinned = false;
1285 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1286 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result);
1287
1288 url.url_ref_.showing_search_terms_ = true;
1289 search_terms_args.bookmark_bar_pinned = true;
1290 result = url.url_ref().ReplaceSearchTerms(search_terms_args);
1291 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result);
1292}