blob: 23601f25813015f8f576d0d458f54e5b4c2a4819 [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]24a555b62013-06-10 22:01:177#include "base/strings/string_util.h"
[email protected]e309f312013-06-07 21:50:088#include "base/strings/utf_string_conversions.h"
[email protected]d82443b2009-01-15 19:54:569#include "chrome/browser/browser_process.h"
10#include "chrome/browser/rlz/rlz.h"
[email protected]375bd7312010-08-30 22:18:1311#include "chrome/browser/search_engines/search_terms_data.h"
[email protected]d54e03a52009-01-16 00:31:0412#include "chrome/browser/search_engines/template_url.h"
[email protected]56fa29592013-07-02 20:25:5313#include "chrome/common/chrome_switches.h"
[email protected]93b29062013-07-12 03:09:0914#include "chrome/common/chrome_version_info.h"
[email protected]d82443b2009-01-15 19:54:5615#include "testing/gtest/include/gtest/gtest.h"
16
[email protected]81d9b72d2012-03-26 22:29:1717#if defined(ENABLE_RLZ)
18#include "chrome/browser/google/google_util.h"
19#endif
20
[email protected]fd6d8822012-12-08 06:56:1121#if defined(OS_ANDROID)
22#include "chrome/browser/search_engines/search_terms_data_android.h"
23#endif
24
[email protected]b37bdfe2012-03-16 20:53:2725// TestSearchTermsData --------------------------------------------------------
26
[email protected]375bd7312010-08-30 22:18:1327// Simple implementation of SearchTermsData.
28class TestSearchTermsData : public SearchTermsData {
29 public:
[email protected]b37bdfe2012-03-16 20:53:2730 explicit TestSearchTermsData(const std::string& google_base_url);
[email protected]375bd7312010-08-30 22:18:1331
[email protected]b37bdfe2012-03-16 20:53:2732 virtual std::string GoogleBaseURLValue() const OVERRIDE;
[email protected]375bd7312010-08-30 22:18:1333
34 private:
35 std::string google_base_url_;
36
37 DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData);
38};
39
[email protected]b37bdfe2012-03-16 20:53:2740TestSearchTermsData::TestSearchTermsData(const std::string& google_base_url)
41 : google_base_url_(google_base_url) {
42}
43
44std::string TestSearchTermsData::GoogleBaseURLValue() const {
45 return google_base_url_;
46}
47
[email protected]b37bdfe2012-03-16 20:53:2748// TemplateURLTest ------------------------------------------------------------
49
[email protected]583844c2011-08-27 00:38:3550class TemplateURLTest : public testing::Test {
[email protected]d82443b2009-01-15 19:54:5651 public:
[email protected]b37bdfe2012-03-16 20:53:2752 void CheckSuggestBaseURL(const std::string& base_url,
53 const std::string& base_suggest_url) const;
[email protected]d82443b2009-01-15 19:54:5654};
55
[email protected]b37bdfe2012-03-16 20:53:2756void TemplateURLTest::CheckSuggestBaseURL(
57 const std::string& base_url,
58 const std::string& base_suggest_url) const {
59 TestSearchTermsData search_terms_data(base_url);
60 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue());
61}
62
63
64// Actual tests ---------------------------------------------------------------
65
[email protected]d82443b2009-01-15 19:54:5666TEST_F(TemplateURLTest, Defaults) {
[email protected]573889f22012-04-07 01:31:5467 TemplateURLData data;
68 EXPECT_FALSE(data.show_in_default_list);
69 EXPECT_FALSE(data.safe_for_autoreplace);
70 EXPECT_EQ(0, data.prepopulate_id);
[email protected]d82443b2009-01-15 19:54:5671}
72
73TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]573889f22012-04-07 01:31:5474 TemplateURLData data;
75 data.SetURL("{searchTerms}");
[email protected]16fca9b82012-04-23 18:40:2676 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:1377 EXPECT_TRUE(url.url_ref().IsValid());
[email protected]d82443b2009-01-15 19:54:5678}
79
80TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1981 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1982 const char* url;
[email protected]400b133f2011-01-19 18:32:3083 const string16 terms;
[email protected]34e24852012-01-31 18:43:5884 const std::string output;
[email protected]0d2e6a62010-01-15 20:09:1985 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:3086 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1687 "http://foosea%20rch/bar" },
[email protected]400b133f2011-01-19 18:32:3088 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1689 "http://foosea%20rch/bar?boo=abc" },
[email protected]400b133f2011-01-19 18:32:3090 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]c31a979c2012-05-31 23:57:1691 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]400b133f2011-01-19 18:32:3092 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
[email protected]c31a979c2012-05-31 23:57:1693 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:1994 };
95 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
96 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:5497 TemplateURLData data;
98 data.SetURL(value.url);
[email protected]16fca9b82012-04-23 18:40:2699 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13100 EXPECT_TRUE(url.url_ref().IsValid());
101 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04102 GURL result(url.url_ref().ReplaceSearchTerms(
103 TemplateURLRef::SearchTermsArgs(value.terms)));
[email protected]c31a979c2012-05-31 23:57:16104 ASSERT_TRUE(result.is_valid());
105 EXPECT_EQ(value.output, result.spec());
[email protected]0d2e6a62010-01-15 20:09:19106 }
[email protected]d82443b2009-01-15 19:54:56107}
108
109TEST_F(TemplateURLTest, URLRefTestCount) {
[email protected]573889f22012-04-07 01:31:54110 TemplateURLData data;
111 data.SetURL("http://foo{searchTerms}{count?}");
[email protected]16fca9b82012-04-23 18:40:26112 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13113 EXPECT_TRUE(url.url_ref().IsValid());
114 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04115 GURL result(url.url_ref().ReplaceSearchTerms(
116 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56117 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27118 EXPECT_EQ("http://foox/", result.spec());
[email protected]d82443b2009-01-15 19:54:56119}
120
121TEST_F(TemplateURLTest, URLRefTestCount2) {
[email protected]573889f22012-04-07 01:31:54122 TemplateURLData data;
123 data.SetURL("http://foo{searchTerms}{count}");
[email protected]16fca9b82012-04-23 18:40:26124 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13125 EXPECT_TRUE(url.url_ref().IsValid());
126 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04127 GURL result(url.url_ref().ReplaceSearchTerms(
128 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56129 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27130 EXPECT_EQ("http://foox10/", result.spec());
[email protected]d82443b2009-01-15 19:54:56131}
132
133TEST_F(TemplateURLTest, URLRefTestIndices) {
[email protected]573889f22012-04-07 01:31:54134 TemplateURLData data;
135 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}");
[email protected]16fca9b82012-04-23 18:40:26136 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13137 EXPECT_TRUE(url.url_ref().IsValid());
138 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04139 GURL result(url.url_ref().ReplaceSearchTerms(
140 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56141 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27142 EXPECT_EQ("http://fooxxy/", result.spec());
[email protected]d82443b2009-01-15 19:54:56143}
144
145TEST_F(TemplateURLTest, URLRefTestIndices2) {
[email protected]573889f22012-04-07 01:31:54146 TemplateURLData data;
147 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}");
[email protected]16fca9b82012-04-23 18:40:26148 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13149 EXPECT_TRUE(url.url_ref().IsValid());
150 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04151 GURL result(url.url_ref().ReplaceSearchTerms(
152 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56153 ASSERT_TRUE(result.is_valid());
[email protected]405aae22012-03-29 20:36:13154 EXPECT_EQ("http://fooxx1y1/", result.spec());
[email protected]d82443b2009-01-15 19:54:56155}
156
157TEST_F(TemplateURLTest, URLRefTestEncoding) {
[email protected]573889f22012-04-07 01:31:54158 TemplateURLData data;
159 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a");
[email protected]16fca9b82012-04-23 18:40:26160 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13161 EXPECT_TRUE(url.url_ref().IsValid());
162 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04163 GURL result(url.url_ref().ReplaceSearchTerms(
164 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56165 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27166 EXPECT_EQ("http://fooxxutf-8ya/", result.spec());
[email protected]d82443b2009-01-15 19:54:56167}
168
[email protected]93b29062013-07-12 03:09:09169TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) {
170 const char kInvalidPostParamsString[] =
171 "unknown_template={UnknownTemplate},bad_value=bad{value},"
172 "{google:sbiSource}";
173 // List all accpectable parameter format in valid_post_params_string. it is
174 // expected like: "name0=,name1=value1,name2={template1}"
175 const char kValidPostParamsString[] =
176 "image_content={google:imageThumbnail},image_url={google:imageURL},"
177 "sbisrc={google:imageSearchSource},language={language},empty_param=,"
178 "constant_param=constant";
179 const char KImageSearchURL[] = "http://foo.com/sbi";
180
181 TemplateURLData data;
182 data.image_url = KImageSearchURL;
183
184 // Try to parse invalid post parameters.
185 data.image_url_post_params = kInvalidPostParamsString;
186 TemplateURL url_bad(NULL, data);
187 ASSERT_FALSE(url_bad.image_url_ref().IsValid());
188 const TemplateURLRef::PostParams& bad_post_params =
189 url_bad.image_url_ref().post_params_;
190 ASSERT_EQ(2U, bad_post_params.size());
191 EXPECT_EQ("unknown_template", bad_post_params[0].first);
192 EXPECT_EQ("{UnknownTemplate}", bad_post_params[0].second);
193 EXPECT_EQ("bad_value", bad_post_params[1].first);
194 EXPECT_EQ("bad{value}", bad_post_params[1].second);
195
196 // Try to parse valid post parameters.
197 data.image_url_post_params = kValidPostParamsString;
198 TemplateURL url(NULL, data);
199 ASSERT_TRUE(url.image_url_ref().IsValid());
200 ASSERT_FALSE(url.image_url_ref().SupportsReplacement());
201
202 // Check term replacement.
203 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X"));
204 search_args.image_thumbnail_content = "dummy-image-thumbnail";
205 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg");
206 // Replacement operation with no post_data buffer should still return
207 // the parsed URL.
208 GURL result(url.image_url_ref().ReplaceSearchTerms(search_args));
209 ASSERT_TRUE(result.is_valid());
210 EXPECT_EQ(KImageSearchURL, result.spec());
211 std::string post_data;
212 TestSearchTermsData search_terms_data("http://X");
213 result = GURL(url.image_url_ref().ReplaceSearchTermsUsingTermsData(
214 search_args, search_terms_data, &post_data));
215 ASSERT_TRUE(result.is_valid());
216 EXPECT_EQ(KImageSearchURL, result.spec());
217 ASSERT_FALSE(post_data.empty());
218
219 // Check parsed result of post parameters.
220 const TemplateURLRef::Replacements& replacements =
221 url.image_url_ref().replacements_;
222 const TemplateURLRef::PostParams& post_params =
223 url.image_url_ref().post_params_;
224 EXPECT_EQ(6U, post_params.size());
225 for (TemplateURLRef::PostParams::const_iterator i = post_params.begin();
226 i != post_params.end(); ++i) {
227 TemplateURLRef::Replacements::const_iterator j = replacements.begin();
228 for (; j != replacements.end(); ++j) {
229 if (j->is_post_param && j->index ==
230 static_cast<size_t>(i - post_params.begin())) {
231 switch (j->type) {
232 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL:
233 EXPECT_EQ("image_content", i->first);
234 EXPECT_EQ(search_args.image_thumbnail_content, i->second);
235 break;
236 case TemplateURLRef::GOOGLE_IMAGE_URL:
237 EXPECT_EQ("image_url", i->first);
238 EXPECT_EQ(search_args.image_url.spec(), i->second);
239 break;
240 case TemplateURLRef::LANGUAGE:
241 EXPECT_EQ("language", i->first);
242 EXPECT_EQ("en", i->second);
243 break;
244 default:
245 ADD_FAILURE(); // Should never go here.
246 }
247 break;
248 }
249 }
250 if (j != replacements.end())
251 continue;
252 if (i->first == "empty_param") {
253 EXPECT_TRUE(i->second.empty());
254 } else if (i->first == "sbisrc") {
255 EXPECT_EQ("unknown", i->second);
256 } else {
257 EXPECT_EQ("constant_param", i->first);
258 EXPECT_EQ("constant", i->second);
259 }
260 }
261}
262
[email protected]d88cb202011-08-17 20:03:01263// Test that setting the prepopulate ID from TemplateURL causes the stored
264// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44265TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
[email protected]573889f22012-04-07 01:31:54266 TemplateURLData data;
[email protected]243abf32012-05-15 18:28:20267 data.SetURL("http://foo{fhqwhgads}bar");
[email protected]16fca9b82012-04-23 18:40:26268 TemplateURL url(NULL, data);
[email protected]1a257262011-06-28 22:15:44269 TemplateURLRef::Replacements replacements;
270 bool valid = false;
[email protected]243abf32012-05-15 18:28:20271 EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL(
[email protected]93b29062013-07-12 03:09:09272 "http://foo{fhqwhgads}bar", &replacements, NULL, &valid));
[email protected]1a257262011-06-28 22:15:44273 EXPECT_TRUE(replacements.empty());
274 EXPECT_TRUE(valid);
275
[email protected]573889f22012-04-07 01:31:54276 data.prepopulate_id = 123;
[email protected]16fca9b82012-04-23 18:40:26277 TemplateURL url2(NULL, data);
[email protected]243abf32012-05-15 18:28:20278 EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar",
[email protected]93b29062013-07-12 03:09:09279 &replacements, NULL,
280 &valid));
[email protected]1a257262011-06-28 22:15:44281 EXPECT_TRUE(replacements.empty());
282 EXPECT_TRUE(valid);
283}
284
[email protected]d82443b2009-01-15 19:54:56285TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
[email protected]573889f22012-04-07 01:31:54286 TemplateURLData data;
287 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b");
[email protected]16fca9b82012-04-23 18:40:26288 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13289 EXPECT_TRUE(url.url_ref().IsValid());
290 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04291 GURL result(url.url_ref().ReplaceSearchTerms(
292 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56293 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27294 EXPECT_EQ("http://fooxutf-8axyb/", result.spec());
[email protected]d82443b2009-01-15 19:54:56295}
296
297TEST_F(TemplateURLTest, URLRefTestEncoding2) {
[email protected]573889f22012-04-07 01:31:54298 TemplateURLData data;
299 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a");
[email protected]16fca9b82012-04-23 18:40:26300 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13301 EXPECT_TRUE(url.url_ref().IsValid());
302 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04303 GURL result(url.url_ref().ReplaceSearchTerms(
304 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]d82443b2009-01-15 19:54:56305 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27306 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
[email protected]d82443b2009-01-15 19:54:56307}
308
[email protected]375bd7312010-08-30 22:18:13309TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
310 struct SearchTermsCase {
311 const char* url;
[email protected]400b133f2011-01-19 18:32:30312 const string16 terms;
[email protected]375bd7312010-08-30 22:18:13313 const char* output;
314 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:30315 { "{google:baseURL}{language}{searchTerms}", string16(),
[email protected]b37bdfe2012-03-16 20:53:27316 "http://example.com/e/en" },
[email protected]400b133f2011-01-19 18:32:30317 { "{google:baseSuggestURL}{searchTerms}", string16(),
[email protected]014010e2011-10-01 04:12:44318 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13319 };
320
321 TestSearchTermsData search_terms_data("http://example.com/e/");
[email protected]573889f22012-04-07 01:31:54322 TemplateURLData data;
[email protected]375bd7312010-08-30 22:18:13323 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
324 const SearchTermsCase& value = search_term_cases[i];
[email protected]573889f22012-04-07 01:31:54325 data.SetURL(value.url);
[email protected]16fca9b82012-04-23 18:40:26326 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13327 EXPECT_TRUE(url.url_ref().IsValid());
328 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04329 GURL result(url.url_ref().ReplaceSearchTermsUsingTermsData(
[email protected]93b29062013-07-12 03:09:09330 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL));
[email protected]375bd7312010-08-30 22:18:13331 ASSERT_TRUE(result.is_valid());
[email protected]b37bdfe2012-03-16 20:53:27332 EXPECT_EQ(value.output, result.spec());
[email protected]375bd7312010-08-30 22:18:13333 }
334}
335
[email protected]d82443b2009-01-15 19:54:56336TEST_F(TemplateURLTest, URLRefTermToWide) {
337 struct ToWideCase {
338 const char* encoded_search_term;
[email protected]400b133f2011-01-19 18:32:30339 const string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56340 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30341 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56342 // Test some big-5 input.
[email protected]47e870b2013-02-24 21:14:53343 {"%a7A%A6%6e+to+you", WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56344 // Test some UTF-8 input. We should fall back to this when the encoding
345 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
346 // Big-5 trailing byte.
[email protected]47e870b2013-02-24 21:14:53347 {"%e4%bd%a05%e5%a5%bd+to+you", WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56348 // Undecodable input should stay escaped.
[email protected]47e870b2013-02-24 21:14:53349 {"%91%01+abcd", WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44350 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30351 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44352 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30353 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56354 };
355
[email protected]d82443b2009-01-15 19:54:56356 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
[email protected]573889f22012-04-07 01:31:54357 TemplateURLData data;
358 data.SetURL("http://foo?q={searchTerms}");
359 data.input_encodings.push_back("big-5");
[email protected]16fca9b82012-04-23 18:40:26360 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13361 EXPECT_TRUE(url.url_ref().IsValid());
362 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]f63ae312009-02-04 17:58:46363 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) {
[email protected]9b74ab52012-03-30 16:08:07364 EXPECT_EQ(to_wide_cases[i].expected_decoded_term,
[email protected]360ba052012-04-04 17:26:13365 url.url_ref().SearchTermToString16(
366 to_wide_cases[i].encoded_search_term));
[email protected]d82443b2009-01-15 19:54:56367 }
368}
369
[email protected]d82443b2009-01-15 19:54:56370TEST_F(TemplateURLTest, DisplayURLToURLRef) {
371 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19372 const std::string url;
[email protected]400b133f2011-01-19 18:32:30373 const string16 expected_result;
[email protected]b37bdfe2012-03-16 20:53:27374 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19375 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30376 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19377 { "http://X",
[email protected]400b133f2011-01-19 18:32:30378 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19379 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30380 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19381 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30382 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56383 };
[email protected]573889f22012-04-07 01:31:54384 TemplateURLData data;
[email protected]b37bdfe2012-03-16 20:53:27385 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54386 data.SetURL(test_data[i].url);
[email protected]16fca9b82012-04-23 18:40:26387 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13388 EXPECT_EQ(test_data[i].expected_result, url.url_ref().DisplayURL());
[email protected]b37bdfe2012-03-16 20:53:27389 EXPECT_EQ(test_data[i].url,
[email protected]360ba052012-04-04 17:26:13390 TemplateURLRef::DisplayURLToURLRef(url.url_ref().DisplayURL()));
[email protected]d82443b2009-01-15 19:54:56391 }
392}
393
394TEST_F(TemplateURLTest, ReplaceSearchTerms) {
395 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19396 const std::string url;
[email protected]d82443b2009-01-15 19:54:56397 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27398 } test_data[] = {
[email protected]ddd231e2010-06-29 20:35:19399 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56400 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19401 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56402 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19403 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56404 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19405 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56406 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19407 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56408 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19409 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56410 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19411 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56412 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19413 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56414 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19415 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56416 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19417 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56418 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19419 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56420 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19421 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56422 "http://foo/UTF-8a{language}aXa" },
423 };
[email protected]573889f22012-04-07 01:31:54424 TemplateURLData data;
425 data.input_encodings.push_back("UTF-8");
[email protected]b37bdfe2012-03-16 20:53:27426 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54427 data.SetURL(test_data[i].url);
[email protected]16fca9b82012-04-23 18:40:26428 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13429 EXPECT_TRUE(url.url_ref().IsValid());
[email protected]3954c3a2012-04-10 20:17:55430 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]b37bdfe2012-03-16 20:53:27431 std::string expected_result = test_data[i].expected_result;
[email protected]d82443b2009-01-15 19:54:56432 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}",
[email protected]d70539de2009-06-24 22:17:06433 g_browser_process->GetApplicationLocale());
[email protected]bca359b2012-06-24 07:53:04434 GURL result(url.url_ref().ReplaceSearchTerms(
435 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X"))));
[email protected]b37bdfe2012-03-16 20:53:27436 ASSERT_TRUE(result.is_valid());
[email protected]d82443b2009-01-15 19:54:56437 EXPECT_EQ(expected_result, result.spec());
438 }
439}
440
441
442// Tests replacing search terms in various encodings and making sure the
443// generated URL matches the expected value.
444TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
445 struct TestData {
446 const std::string encoding;
[email protected]400b133f2011-01-19 18:32:30447 const string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19448 const std::string url;
[email protected]d82443b2009-01-15 19:54:56449 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27450 } test_data[] = {
[email protected]47e870b2013-02-24 21:14:53451 { "BIG5", WideToUTF16(L"\x60BD"),
[email protected]400b133f2011-01-19 18:32:30452 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17453 "http://foo/?%B1~BIG5" },
[email protected]400b133f2011-01-19 18:32:30454 { "UTF-8", ASCIIToUTF16("blah"),
455 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17456 "http://foo/?blahUTF-8" },
[email protected]34e24852012-01-31 18:43:58457 { "Shift_JIS", UTF8ToUTF16("\xe3\x81\x82"),
458 "http://foo/{searchTerms}/bar",
459 "http://foo/%82%A0/bar"},
460 { "Shift_JIS", UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"),
461 "http://foo/{searchTerms}/bar",
462 "http://foo/%82%A0%20%82%A2/bar"},
[email protected]d82443b2009-01-15 19:54:56463 };
[email protected]573889f22012-04-07 01:31:54464 TemplateURLData data;
[email protected]b37bdfe2012-03-16 20:53:27465 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]573889f22012-04-07 01:31:54466 data.SetURL(test_data[i].url);
467 data.input_encodings.clear();
468 data.input_encodings.push_back(test_data[i].encoding);
[email protected]16fca9b82012-04-23 18:40:26469 TemplateURL url(NULL, data);
[email protected]3954c3a2012-04-10 20:17:55470 EXPECT_TRUE(url.url_ref().IsValid());
471 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04472 GURL result(url.url_ref().ReplaceSearchTerms(
473 TemplateURLRef::SearchTermsArgs(test_data[i].search_term)));
474 ASSERT_TRUE(result.is_valid());
475 EXPECT_EQ(test_data[i].expected_result, result.spec());
476 }
477}
478
479// Tests replacing assisted query stats (AQS) in various scenarios.
480TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) {
481 struct TestData {
482 const string16 search_term;
483 const std::string aqs;
484 const std::string base_url;
485 const std::string url;
486 const std::string expected_result;
487 } test_data[] = {
488 // No HTTPS, no AQS.
489 { ASCIIToUTF16("foo"),
490 "chrome.0.0l6",
491 "http://foo/",
492 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
493 "http://foo/?foo" },
494 // HTTPS available, AQS should be replaced.
495 { ASCIIToUTF16("foo"),
496 "chrome.0.0l6",
497 "https://foo/",
498 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
499 "https://foo/?fooaqs=chrome.0.0l6&" },
500 // HTTPS available, however AQS is empty.
501 { ASCIIToUTF16("foo"),
502 "",
503 "https://foo/",
504 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}",
505 "https://foo/?foo" },
506 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS.
507 { ASCIIToUTF16("foo"),
508 "chrome.0.0l6",
509 "",
510 "http://foo?{searchTerms}{google:assistedQueryStats}",
511 "http://foo/?foo" },
512 // A non-Google search provider with HTTPS should allow AQS.
513 { ASCIIToUTF16("foo"),
514 "chrome.0.0l6",
515 "",
516 "https://foo?{searchTerms}{google:assistedQueryStats}",
517 "https://foo/?fooaqs=chrome.0.0l6&" },
518 };
519 TemplateURLData data;
520 data.input_encodings.push_back("UTF-8");
521 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
522 data.SetURL(test_data[i].url);
523 TemplateURL url(NULL, data);
524 EXPECT_TRUE(url.url_ref().IsValid());
525 ASSERT_TRUE(url.url_ref().SupportsReplacement());
526 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
527 search_terms_args.assisted_query_stats = test_data[i].aqs;
528 UIThreadSearchTermsData::SetGoogleBaseURL(test_data[i].base_url);
529 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
[email protected]b37bdfe2012-03-16 20:53:27530 ASSERT_TRUE(result.is_valid());
531 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56532 }
533}
534
[email protected]00790562012-12-14 09:57:16535// Tests replacing cursor position.
536TEST_F(TemplateURLTest, ReplaceCursorPosition) {
537 struct TestData {
538 const string16 search_term;
539 size_t cursor_position;
540 const std::string url;
541 const std::string expected_result;
542 } test_data[] = {
543 { ASCIIToUTF16("foo"),
544 string16::npos,
545 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
546 "http://www.google.com/?foo&" },
547 { ASCIIToUTF16("foo"),
548 2,
549 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
550 "http://www.google.com/?foo&cp=2&" },
551 { ASCIIToUTF16("foo"),
552 15,
553 "{google:baseURL}?{searchTerms}&{google:cursorPosition}",
554 "http://www.google.com/?foo&cp=15&" },
555 };
556 TemplateURLData data;
557 data.input_encodings.push_back("UTF-8");
558 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
559 data.SetURL(test_data[i].url);
560 TemplateURL url(NULL, data);
561 EXPECT_TRUE(url.url_ref().IsValid());
562 ASSERT_TRUE(url.url_ref().SupportsReplacement());
563 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
564 search_terms_args.cursor_position = test_data[i].cursor_position;
565 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
566 ASSERT_TRUE(result.is_valid());
567 EXPECT_EQ(test_data[i].expected_result, result.spec());
568 }
569}
570
[email protected]800569d2013-05-06 07:38:48571// Tests replacing zeroPrefixUrl.
572TEST_F(TemplateURLTest, ReplaceZeroPrefixUrl) {
573 struct TestData {
574 const string16 search_term;
575 const std::string zero_prefix_url;
576 const std::string url;
577 const std::string expected_result;
578 } test_data[] = {
579 { ASCIIToUTF16("foo"),
580 "http://www.google.com/",
581 "{google:baseURL}?{searchTerms}&{google:zeroPrefixUrl}",
582 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" },
583 { ASCIIToUTF16("foo"),
584 "",
585 "{google:baseURL}?{searchTerms}&{google:zeroPrefixUrl}",
586 "http://www.google.com/?foo&" },
587 { ASCIIToUTF16("foo"),
588 "http://g.com/+-/*&=",
589 "{google:baseURL}?{searchTerms}&{google:zeroPrefixUrl}",
590 "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" },
591 };
592 TemplateURLData data;
593 data.input_encodings.push_back("UTF-8");
594 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
595 data.SetURL(test_data[i].url);
596 TemplateURL url(NULL, data);
597 EXPECT_TRUE(url.url_ref().IsValid());
598 ASSERT_TRUE(url.url_ref().SupportsReplacement());
599 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term);
600 search_terms_args.zero_prefix_url = test_data[i].zero_prefix_url;
601 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
602 ASSERT_TRUE(result.is_valid());
603 EXPECT_EQ(test_data[i].expected_result, result.spec());
604 }
605}
606
[email protected]d82443b2009-01-15 19:54:56607TEST_F(TemplateURLTest, Suggestions) {
608 struct TestData {
609 const int accepted_suggestion;
[email protected]400b133f2011-01-19 18:32:30610 const string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56611 const std::string expected_result;
[email protected]b37bdfe2012-03-16 20:53:27612 } test_data[] = {
[email protected]400b133f2011-01-19 18:32:30613 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16(),
[email protected]29653892013-03-02 19:25:37614 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30615 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37616 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30617 { TemplateURLRef::NO_SUGGESTION_CHOSEN, string16(),
[email protected]29653892013-03-02 19:25:37618 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30619 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]29653892013-03-02 19:25:37620 "http://bar/foo?q=foobar" },
621 { 0, string16(), "http://bar/foo?oq=&q=foobar" },
622 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56623 };
[email protected]573889f22012-04-07 01:31:54624 TemplateURLData data;
[email protected]29653892013-03-02 19:25:37625 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}"
626 "q={searchTerms}");
[email protected]573889f22012-04-07 01:31:54627 data.input_encodings.push_back("UTF-8");
[email protected]16fca9b82012-04-23 18:40:26628 TemplateURL url(NULL, data);
[email protected]3954c3a2012-04-10 20:17:55629 EXPECT_TRUE(url.url_ref().IsValid());
[email protected]360ba052012-04-04 17:26:13630 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]b37bdfe2012-03-16 20:53:27631 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
[email protected]bca359b2012-06-24 07:53:04632 TemplateURLRef::SearchTermsArgs search_terms_args(
633 ASCIIToUTF16("foobar"));
634 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion;
635 search_terms_args.original_query =
636 test_data[i].original_query_for_suggestion;
637 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
[email protected]b37bdfe2012-03-16 20:53:27638 ASSERT_TRUE(result.is_valid());
639 EXPECT_EQ(test_data[i].expected_result, result.spec());
[email protected]d82443b2009-01-15 19:54:56640 }
641}
642
[email protected]81f808de2009-09-25 01:36:34643TEST_F(TemplateURLTest, RLZ) {
[email protected]400b133f2011-01-19 18:32:30644 string16 rlz_string;
[email protected]81d9b72d2012-03-26 22:29:17645#if defined(ENABLE_RLZ)
646 std::string brand;
647 if (google_util::GetBrand(&brand) && !brand.empty() &&
648 !google_util::IsOrganic(brand)) {
[email protected]7e6f4ed5c2012-10-31 21:10:34649 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz_string);
[email protected]81d9b72d2012-03-26 22:29:17650 }
[email protected]fd6d8822012-12-08 06:56:11651#elif defined(OS_ANDROID)
652 SearchTermsDataAndroid::rlz_parameter_value_.Get() =
653 ASCIIToUTF16("android_test");
654 rlz_string = SearchTermsDataAndroid::rlz_parameter_value_.Get();
[email protected]58b64332010-08-13 16:09:39655#endif
[email protected]d82443b2009-01-15 19:54:56656
[email protected]573889f22012-04-07 01:31:54657 TemplateURLData data;
658 data.SetURL("http://bar/?{google:RLZ}{searchTerms}");
[email protected]16fca9b82012-04-23 18:40:26659 TemplateURL url(NULL, data);
[email protected]360ba052012-04-04 17:26:13660 EXPECT_TRUE(url.url_ref().IsValid());
661 ASSERT_TRUE(url.url_ref().SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:04662 GURL result(url.url_ref().ReplaceSearchTerms(
663 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x"))));
[email protected]d82443b2009-01-15 19:54:56664 ASSERT_TRUE(result.is_valid());
[email protected]12bd05872009-03-17 19:25:06665 std::string expected_url = "http://bar/?";
[email protected]b37bdfe2012-03-16 20:53:27666 if (!rlz_string.empty())
[email protected]8ead4c022012-03-17 02:09:50667 expected_url += "rlz=" + UTF16ToUTF8(rlz_string) + "&";
[email protected]12bd05872009-03-17 19:25:06668 expected_url += "x";
[email protected]b37bdfe2012-03-16 20:53:27669 EXPECT_EQ(expected_url, result.spec());
[email protected]d82443b2009-01-15 19:54:56670}
671
672TEST_F(TemplateURLTest, HostAndSearchTermKey) {
673 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19674 const std::string url;
[email protected]d82443b2009-01-15 19:54:56675 const std::string host;
676 const std::string path;
677 const std::string search_term_key;
[email protected]573889f22012-04-07 01:31:54678 } test_data[] = {
[email protected]7c60f5042013-02-14 03:39:32679 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56680
681 // No query key should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32682 { "http://blah/{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56683
684 // No term should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32685 { "http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56686
687 // Multiple terms should result in empty values.
[email protected]7c60f5042013-02-14 03:39:32688 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56689
690 // Term in the host shouldn't match.
[email protected]7c60f5042013-02-14 03:39:32691 { "http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56692
[email protected]7c60f5042013-02-14 03:39:32693 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
694 { "https://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56695
696 // Single term with extra chars in value should match.
[email protected]7c60f5042013-02-14 03:39:32697 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56698 };
699
[email protected]573889f22012-04-07 01:31:54700 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
701 TemplateURLData data;
702 data.SetURL(test_data[i].url);
[email protected]16fca9b82012-04-23 18:40:26703 TemplateURL url(NULL, data);
[email protected]573889f22012-04-07 01:31:54704 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost());
705 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath());
706 EXPECT_EQ(test_data[i].search_term_key, url.url_ref().GetSearchTermKey());
[email protected]d82443b2009-01-15 19:54:56707 }
708}
709
710TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
711 static const struct {
[email protected]ddd231e2010-06-29 20:35:19712 const char* const base_url;
713 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56714 } data[] = {
[email protected]014010e2011-10-01 04:12:44715 { "http://google.com/", "http://google.com/complete/", },
716 { "http://www.google.com/", "http://www.google.com/complete/", },
717 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
718 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
719 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56720 };
721
[email protected]f63ae312009-02-04 17:58:46722 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i)
[email protected]d82443b2009-01-15 19:54:56723 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
724}
725
[email protected]81c6ef62010-01-21 09:58:47726TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19727 std::string parsed_url("{searchTerms}");
[email protected]573889f22012-04-07 01:31:54728 TemplateURLData data;
729 data.SetURL(parsed_url);
[email protected]16fca9b82012-04-23 18:40:26730 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47731 TemplateURLRef::Replacements replacements;
[email protected]360ba052012-04-04 17:26:13732 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19733 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47734 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27735 EXPECT_EQ(0U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47736 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
737}
738
739TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]243abf32012-05-15 18:28:20740 std::string parsed_url("{fhqwhgads}abc");
[email protected]573889f22012-04-07 01:31:54741 TemplateURLData data;
742 data.SetURL(parsed_url);
[email protected]16fca9b82012-04-23 18:40:26743 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47744 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:44745
746 // By default, TemplateURLRef should not consider itself prepopulated.
747 // Therefore we should not replace the unknown parameter.
[email protected]360ba052012-04-04 17:26:13748 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20749 EXPECT_EQ("{fhqwhgads}abc", parsed_url);
[email protected]1a257262011-06-28 22:15:44750 EXPECT_TRUE(replacements.empty());
751
752 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
[email protected]243abf32012-05-15 18:28:20753 parsed_url = "{fhqwhgads}abc";
[email protected]573889f22012-04-07 01:31:54754 data.prepopulate_id = 1;
[email protected]16fca9b82012-04-23 18:40:26755 TemplateURL url2(NULL, data);
[email protected]495c30b2012-05-15 18:48:15756 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements));
[email protected]243abf32012-05-15 18:28:20757 EXPECT_EQ("abc", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47758 EXPECT_TRUE(replacements.empty());
759}
760
761TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]16fca9b82012-04-23 18:40:26762 TemplateURL url(NULL, TemplateURLData());
[email protected]81c6ef62010-01-21 09:58:47763 TemplateURLRef::Replacements replacements;
764 bool valid = false;
[email protected]b37bdfe2012-03-16 20:53:27765 EXPECT_EQ(std::string(),
[email protected]93b29062013-07-12 03:09:09766 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47767 EXPECT_TRUE(replacements.empty());
768 EXPECT_TRUE(valid);
769}
770
771TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]573889f22012-04-07 01:31:54772 TemplateURLData data;
773 data.SetURL("{");
[email protected]16fca9b82012-04-23 18:40:26774 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47775 TemplateURLRef::Replacements replacements;
776 bool valid = false;
[email protected]93b29062013-07-12 03:09:09777 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL,
778 &valid));
[email protected]81c6ef62010-01-21 09:58:47779 EXPECT_TRUE(replacements.empty());
780 EXPECT_FALSE(valid);
781}
782
783TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]573889f22012-04-07 01:31:54784 TemplateURLData data;
785 data.SetURL("{}");
[email protected]16fca9b82012-04-23 18:40:26786 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47787 TemplateURLRef::Replacements replacements;
788 bool valid = false;
[email protected]93b29062013-07-12 03:09:09789 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid));
[email protected]81c6ef62010-01-21 09:58:47790 EXPECT_TRUE(replacements.empty());
791 EXPECT_TRUE(valid);
792}
793
794TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]573889f22012-04-07 01:31:54795 TemplateURLData data;
796 data.SetURL("{}{{%s}}");
[email protected]16fca9b82012-04-23 18:40:26797 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47798 TemplateURLRef::Replacements replacements;
799 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19800 EXPECT_EQ("{}{}",
[email protected]93b29062013-07-12 03:09:09801 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL,
802 &valid));
[email protected]81c6ef62010-01-21 09:58:47803 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27804 EXPECT_EQ(3U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47805 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
806 EXPECT_TRUE(valid);
807}
808
809TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]573889f22012-04-07 01:31:54810 TemplateURLData data;
811 data.SetURL("{%s");
[email protected]16fca9b82012-04-23 18:40:26812 TemplateURL url(NULL, data);
[email protected]81c6ef62010-01-21 09:58:47813 TemplateURLRef::Replacements replacements;
814 bool valid = false;
[email protected]360ba052012-04-04 17:26:13815 EXPECT_EQ("{",
[email protected]93b29062013-07-12 03:09:09816 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL,
817 &valid));
[email protected]81c6ef62010-01-21 09:58:47818 ASSERT_EQ(1U, replacements.size());
[email protected]b37bdfe2012-03-16 20:53:27819 EXPECT_EQ(1U, replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47820 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
821 EXPECT_TRUE(valid);
822}
[email protected]5b3078752012-10-09 18:54:16823
[email protected]fd6d8822012-12-08 06:56:11824#if defined(OS_ANDROID)
825TEST_F(TemplateURLTest, SearchClient) {
826 const std::string base_url_str("http://google.com/?");
827 const std::string terms_str("{searchTerms}&{google:searchClient}");
828 const std::string full_url_str = base_url_str + terms_str;
829 const string16 terms(ASCIIToUTF16(terms_str));
830 UIThreadSearchTermsData::SetGoogleBaseURL(base_url_str);
831
832 TemplateURLData data;
833 data.SetURL(full_url_str);
834 TemplateURL url(NULL, data);
835 EXPECT_TRUE(url.url_ref().IsValid());
836 ASSERT_TRUE(url.url_ref().SupportsReplacement());
837 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar"));
838
839 // Check that the URL is correct when a client is not present.
840 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args));
841 ASSERT_TRUE(result.is_valid());
842 EXPECT_EQ("http://google.com/?foobar&", result.spec());
843
844 // Check that the URL is correct when a client is present.
845 SearchTermsDataAndroid::search_client_.Get() = "android_test";
846 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args));
847 ASSERT_TRUE(result_2.is_valid());
848 EXPECT_EQ("http://google.com/?foobar&client=android_test&", result_2.spec());
849}
850#endif
851
[email protected]5b3078752012-10-09 18:54:16852TEST_F(TemplateURLTest, GetURLNoInstantURL) {
853 TemplateURLData data;
854 data.SetURL("http://google.com/?q={searchTerms}");
855 data.suggestions_url = "http://google.com/suggest?q={searchTerms}";
856 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
857 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
858 TemplateURL url(NULL, data);
859 ASSERT_EQ(3U, url.URLCount());
860 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
861 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
862 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
863}
864
865TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) {
866 TemplateURLData data;
867 data.SetURL("http://google.com/?q={searchTerms}");
868 data.instant_url = "http://google.com/instant#q={searchTerms}";
869 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}");
870 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}");
871 TemplateURL url(NULL, data);
872 ASSERT_EQ(3U, url.URLCount());
873 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0));
874 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1));
875 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2));
876}
877
878TEST_F(TemplateURLTest, GetURLOnlyOneURL) {
879 TemplateURLData data;
880 data.SetURL("http://www.google.co.uk/");
881 TemplateURL url(NULL, data);
882 ASSERT_EQ(1U, url.URLCount());
883 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0));
884}
885
886TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) {
887 TemplateURLData data;
888 data.SetURL("http://google.com/?q={searchTerms}");
889 data.instant_url = "http://google.com/instant#q={searchTerms}";
890 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
891 data.alternate_urls.push_back(
892 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
893 TemplateURL url(NULL, data);
894 string16 result;
895
896 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
897 GURL("http://google.com/?q=something"), &result));
[email protected]4076ea62013-01-09 01:47:19898 EXPECT_EQ(ASCIIToUTF16("something"), result);
899
900 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
901 GURL("http://google.com/?espv&q=something"), &result));
902 EXPECT_EQ(ASCIIToUTF16("something"), result);
903
904 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
905 GURL("http://google.com/?espv=1&q=something"), &result));
906 EXPECT_EQ(ASCIIToUTF16("something"), result);
907
908 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
909 GURL("http://google.com/?espv=0&q=something"), &result));
910 EXPECT_EQ(ASCIIToUTF16("something"), result);
911
912 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
913 GURL("http://google.com/alt/#q=something"), &result));
914 EXPECT_EQ(ASCIIToUTF16("something"), result);
915
916 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
917 GURL("http://google.com/alt/#espv&q=something"), &result));
918 EXPECT_EQ(ASCIIToUTF16("something"), result);
919
920 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
921 GURL("http://google.com/alt/#espv=1&q=something"), &result));
922 EXPECT_EQ(ASCIIToUTF16("something"), result);
923
924 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
925 GURL("http://google.com/alt/#espv=0&q=something"), &result));
[email protected]5b3078752012-10-09 18:54:16926 EXPECT_EQ(ASCIIToUTF16("something"), result);
927
928 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
929 GURL("http://google.ca/?q=something"), &result));
930 EXPECT_EQ(string16(), result);
931
932 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]67d8b752013-04-03 17:33:27933 GURL("http://google.ca/?q=something&q=anything"), &result));
934 EXPECT_EQ(string16(), result);
935
936 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
[email protected]5b3078752012-10-09 18:54:16937 GURL("http://google.com/foo/?q=foo"), &result));
938 EXPECT_EQ(string16(), result);
939
[email protected]32e2d81b2012-10-16 19:03:25940 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
[email protected]5b3078752012-10-09 18:54:16941 GURL("https://google.com/?q=foo"), &result));
[email protected]32e2d81b2012-10-16 19:03:25942 EXPECT_EQ(ASCIIToUTF16("foo"), result);
[email protected]5b3078752012-10-09 18:54:16943
944 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
945 GURL("http://google.com:8080/?q=foo"), &result));
946 EXPECT_EQ(string16(), result);
947
948 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
949 GURL("http://google.com/?q=1+2+3&b=456"), &result));
950 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result);
951
952 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
953 GURL("http://google.com/alt/?q=123#q=456"), &result));
954 EXPECT_EQ(ASCIIToUTF16("456"), result);
955
956 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
957 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), &result));
958 EXPECT_EQ(ASCIIToUTF16("123"), result);
959
960 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL(
961 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), &result));
962 EXPECT_EQ(ASCIIToUTF16("789"), result);
963
964 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
965 GURL("http://google.com/alt/?q="), &result));
966 EXPECT_EQ(string16(), result);
967
968 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
969 GURL("http://google.com/alt/?#q="), &result));
970 EXPECT_EQ(string16(), result);
971
972 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
973 GURL("http://google.com/alt/?q=#q="), &result));
974 EXPECT_EQ(string16(), result);
975
976 EXPECT_FALSE(url.ExtractSearchTermsFromURL(
977 GURL("http://google.com/alt/?q=123#q="), &result));
978 EXPECT_EQ(string16(), result);
979
980 EXPECT_TRUE(url.ExtractSearchTermsFromURL(
981 GURL("http://google.com/alt/?q=#q=123"), &result));
982 EXPECT_EQ(ASCIIToUTF16("123"), result);
983}
[email protected]4076ea62013-01-09 01:47:19984
985TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) {
986 TemplateURLData data;
987 data.SetURL("http://google.com/?q={searchTerms}");
988 data.instant_url = "http://google.com/instant#q={searchTerms}";
989 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
990 data.alternate_urls.push_back(
991 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
992 data.search_terms_replacement_key = "espv";
993 TemplateURL url(NULL, data);
994
995 // Test with instant enabled required.
996 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
997 GURL("http://google.com/")));
998
999 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1000 GURL("http://google.com/?espv")));
1001
1002 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1003 GURL("http://google.com/#espv")));
1004
1005 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1006 GURL("http://google.com/?q=something")));
1007
1008 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1009 GURL("http://google.com/?q=something&espv")));
1010
1011 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1012 GURL("http://google.com/?q=something&espv=1")));
1013
1014 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1015 GURL("http://google.com/?q=something&espv=0")));
1016
1017 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1018 GURL("http://google.com/?espv&q=something")));
1019
1020 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1021 GURL("http://google.com/?espv=1&q=something")));
1022
1023 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1024 GURL("http://google.com/?espv=0&q=something")));
1025
1026 EXPECT_FALSE(url.HasSearchTermsReplacementKey(
1027 GURL("http://google.com/alt/#q=something")));
1028
1029 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1030 GURL("http://google.com/alt/#q=something&espv")));
1031
1032 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1033 GURL("http://google.com/alt/#q=something&espv=1")));
1034
1035 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1036 GURL("http://google.com/alt/#q=something&espv=0")));
1037
1038 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1039 GURL("http://google.com/alt/#espv&q=something")));
1040
1041 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1042 GURL("http://google.com/alt/#espv=1&q=something")));
1043
1044 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1045 GURL("http://google.com/alt/#espv=0&q=something")));
1046
1047 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1048 GURL("http://google.com/?espv#q=something")));
1049
1050 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1051 GURL("http://google.com/?espv=1#q=something")));
1052
1053 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1054 GURL("http://google.com/?q=something#espv")));
1055
1056 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1057 GURL("http://google.com/?q=something#espv=1")));
1058
1059 // This does not ensure the domain matches.
1060 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1061 GURL("http://bing.com/?espv")));
1062
1063 EXPECT_TRUE(url.HasSearchTermsReplacementKey(
1064 GURL("http://bing.com/#espv")));
1065}
[email protected]f62e30f52013-03-23 03:45:151066
1067TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) {
1068 TemplateURLData data;
1069 data.SetURL("http://google.com/?q={searchTerms}");
1070 data.instant_url = "http://google.com/instant#q={searchTerms}";
1071 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}");
1072 data.alternate_urls.push_back(
1073 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar");
1074 TemplateURL url(NULL, data);
1075 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane"));
1076 GURL result;
1077
1078 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1079 GURL("http://google.com/?q=something"), search_terms, &result));
1080 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result);
1081
1082 result = GURL("http://should.not.change.com");
1083 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1084 GURL("http://google.ca/?q=something"), search_terms, &result));
1085 EXPECT_EQ(GURL("http://should.not.change.com"), result);
1086
1087 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1088 GURL("http://google.com/foo/?q=foo"), search_terms, &result));
1089
1090 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1091 GURL("https://google.com/?q=foo"), search_terms, &result));
1092 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result);
1093
1094 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1095 GURL("http://google.com:8080/?q=foo"), search_terms, &result));
1096
1097 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1098 GURL("http://google.com/?q=1+2+3&b=456"), search_terms, &result));
1099 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result);
1100
1101 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in
1102 // template_url.cc for details.
1103 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1104 GURL("http://google.com/alt/?q=123#q=456"), search_terms, &result));
1105 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result);
1106
1107 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1108 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms,
1109 &result));
1110 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"),
1111 result);
1112
1113 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1114 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"),
1115 search_terms, &result));
1116 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456"
1117 "#j=abc&q=Bob Morane&h=def9"), result);
1118
1119 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1120 GURL("http://google.com/alt/?q="), search_terms, &result));
1121
1122 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1123 GURL("http://google.com/alt/?#q="), search_terms, &result));
1124
1125 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1126 GURL("http://google.com/alt/?q=#q="), search_terms, &result));
1127
1128 EXPECT_FALSE(url.ReplaceSearchTermsInURL(
1129 GURL("http://google.com/alt/?q=123#q="), search_terms, &result));
1130
1131 EXPECT_TRUE(url.ReplaceSearchTermsInURL(
1132 GURL("http://google.com/alt/?q=#q=123"), search_terms, &result));
1133 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result);
1134}
[email protected]56fa29592013-07-02 20:25:531135
1136// Test the |append_extra_query_params| field of SearchTermsArgs.
1137TEST_F(TemplateURLTest, ExtraQueryParams) {
1138 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/");
1139 TemplateURLData data;
1140 // Pick a URL with replacements before, during, and after the query, to ensure
1141 // we don't goof up any of them.
1142 data.SetURL("{google:baseURL}search?q={searchTerms}"
1143 "#{google:originalQueryForSuggestion}x");
1144 TemplateURL url(NULL, data);
1145
1146 // Baseline: no command-line args, no |append_extra_query_params| flag.
1147 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc"));
1148 search_terms.original_query = ASCIIToUTF16("def");
1149 search_terms.accepted_suggestion = 0;
1150 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1151 url.url_ref().ReplaceSearchTerms(search_terms));
1152
1153 // Set the flag. Since there are no command-line args, this should have no
1154 // effect.
1155 search_terms.append_extra_query_params = true;
1156 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1157 url.url_ref().ReplaceSearchTerms(search_terms));
1158
1159 // Now append the command-line arg. This should be inserted into the query.
1160 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1161 switches::kExtraSearchQueryParams, "a=b");
1162 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x",
1163 url.url_ref().ReplaceSearchTerms(search_terms));
1164
1165 // Turn off the flag. Now the command-line arg should be ignored again.
1166 search_terms.append_extra_query_params = false;
1167 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x",
1168 url.url_ref().ReplaceSearchTerms(search_terms));
1169}