blob: f834129c58f765a9156203f56a7c96cbcfdfdcb0 [file] [log] [blame]
[email protected]400b133f2011-01-19 18:32:301// Copyright (c) 2011 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]d82443b2009-01-15 19:54:566#include "base/string_util.h"
[email protected]64048bd2010-03-08 23:28:587#include "base/utf_string_conversions.h"
[email protected]d82443b2009-01-15 19:54:568#include "chrome/browser/browser_process.h"
9#include "chrome/browser/rlz/rlz.h"
[email protected]375bd7312010-08-30 22:18:1310#include "chrome/browser/search_engines/search_terms_data.h"
[email protected]d54e03a52009-01-16 00:31:0411#include "chrome/browser/search_engines/template_url.h"
[email protected]d82443b2009-01-15 19:54:5612#include "testing/gtest/include/gtest/gtest.h"
13
[email protected]375bd7312010-08-30 22:18:1314// Simple implementation of SearchTermsData.
15class TestSearchTermsData : public SearchTermsData {
16 public:
17 explicit TestSearchTermsData(const char* google_base_url)
18 : google_base_url_(google_base_url) {
19 }
20
21 virtual std::string GoogleBaseURLValue() const {
22 return google_base_url_;
23 }
24
25 virtual std::string GetApplicationLocale() const {
26 return "yy";
27 }
28
29#if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
30 // Returns the value for the Chrome Omnibox rlz.
[email protected]400b133f2011-01-19 18:32:3031 virtual string16 GetRlzParameterValue() const {
32 return string16();
[email protected]375bd7312010-08-30 22:18:1333 }
34#endif
35
36 private:
37 std::string google_base_url_;
38
39 DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData);
40};
41
[email protected]583844c2011-08-27 00:38:3542class TemplateURLTest : public testing::Test {
[email protected]d82443b2009-01-15 19:54:5643 public:
44 virtual void TearDown() {
[email protected]375bd7312010-08-30 22:18:1345 TemplateURLRef::SetGoogleBaseURL(NULL);
[email protected]d82443b2009-01-15 19:54:5646 }
47
[email protected]ddd231e2010-06-29 20:35:1948 void CheckSuggestBaseURL(const char* base_url,
49 const char* base_suggest_url) const {
[email protected]375bd7312010-08-30 22:18:1350 TestSearchTermsData search_terms_data(base_url);
[email protected]d82443b2009-01-15 19:54:5651 EXPECT_STREQ(base_suggest_url,
[email protected]375bd7312010-08-30 22:18:1352 search_terms_data.GoogleBaseSuggestURLValue().c_str());
[email protected]d82443b2009-01-15 19:54:5653 }
54};
55
56TEST_F(TemplateURLTest, Defaults) {
57 TemplateURL url;
58 ASSERT_FALSE(url.show_in_default_list());
59 ASSERT_FALSE(url.safe_for_autoreplace());
60 ASSERT_EQ(0, url.prepopulate_id());
61}
62
63TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]ddd231e2010-06-29 20:35:1964 TemplateURLRef ref("{searchTerms}", 0, 0);
[email protected]d82443b2009-01-15 19:54:5665 ASSERT_TRUE(ref.IsValid());
66}
67
68TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1969 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1970 const char* url;
[email protected]400b133f2011-01-19 18:32:3071 const string16 terms;
[email protected]0d2e6a62010-01-15 20:09:1972 const char* output;
73 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:3074 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"),
75 "http://foosea%20rch/bar" },
76 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"),
[email protected]3c75f0d2010-03-02 05:51:1777 "http://foosea%20rch/bar?boo=abc" },
[email protected]400b133f2011-01-19 18:32:3078 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"),
[email protected]3c75f0d2010-03-02 05:51:1779 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]400b133f2011-01-19 18:32:3080 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"),
[email protected]3c75f0d2010-03-02 05:51:1781 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:1982 };
83 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
84 const SearchTermsCase& value = search_term_cases[i];
85 TemplateURL t_url;
86 TemplateURLRef ref(value.url, 0, 0);
87 ASSERT_TRUE(ref.IsValid());
[email protected]d82443b2009-01-15 19:54:5688
[email protected]0d2e6a62010-01-15 20:09:1989 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]ddd231e2010-06-29 20:35:1990 GURL result = GURL(ref.ReplaceSearchTerms(t_url, value.terms,
[email protected]400b133f2011-01-19 18:32:3091 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()));
[email protected]0d2e6a62010-01-15 20:09:1992 ASSERT_TRUE(result.is_valid());
93 ASSERT_EQ(value.output, result.spec());
94 }
[email protected]d82443b2009-01-15 19:54:5695}
96
97TEST_F(TemplateURLTest, URLRefTestCount) {
98 TemplateURL t_url;
[email protected]ddd231e2010-06-29 20:35:1999 TemplateURLRef ref("http://foo{searchTerms}{count?}", 0, 0);
[email protected]d82443b2009-01-15 19:54:56100 ASSERT_TRUE(ref.IsValid());
101 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]400b133f2011-01-19 18:32:30102 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"),
103 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()));
[email protected]d82443b2009-01-15 19:54:56104 ASSERT_TRUE(result.is_valid());
105 ASSERT_EQ("http://foox/", result.spec());
106}
107
108TEST_F(TemplateURLTest, URLRefTestCount2) {
109 TemplateURL t_url;
[email protected]ddd231e2010-06-29 20:35:19110 TemplateURLRef ref("http://foo{searchTerms}{count}", 0, 0);
[email protected]d82443b2009-01-15 19:54:56111 ASSERT_TRUE(ref.IsValid());
112 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]400b133f2011-01-19 18:32:30113 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"),
114 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()));
[email protected]d82443b2009-01-15 19:54:56115 ASSERT_TRUE(result.is_valid());
116 ASSERT_EQ("http://foox10/", result.spec());
117}
118
119TEST_F(TemplateURLTest, URLRefTestIndices) {
120 TemplateURL t_url;
[email protected]ddd231e2010-06-29 20:35:19121 TemplateURLRef ref("http://foo{searchTerms}x{startIndex?}y{startPage?}",
[email protected]d82443b2009-01-15 19:54:56122 1, 2);
123 ASSERT_TRUE(ref.IsValid());
124 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]400b133f2011-01-19 18:32:30125 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"),
126 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()));
[email protected]d82443b2009-01-15 19:54:56127 ASSERT_TRUE(result.is_valid());
128 ASSERT_EQ("http://fooxxy/", result.spec());
129}
130
131TEST_F(TemplateURLTest, URLRefTestIndices2) {
132 TemplateURL t_url;
[email protected]ddd231e2010-06-29 20:35:19133 TemplateURLRef ref("http://foo{searchTerms}x{startIndex}y{startPage}", 1, 2);
[email protected]d82443b2009-01-15 19:54:56134 ASSERT_TRUE(ref.IsValid());
135 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]400b133f2011-01-19 18:32:30136 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"),
137 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()));
[email protected]d82443b2009-01-15 19:54:56138 ASSERT_TRUE(result.is_valid());
139 ASSERT_EQ("http://fooxx1y2/", result.spec());
140}
141
142TEST_F(TemplateURLTest, URLRefTestEncoding) {
143 TemplateURL t_url;
144 TemplateURLRef ref(
[email protected]ddd231e2010-06-29 20:35:19145 "http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a", 1, 2);
[email protected]d82443b2009-01-15 19:54:56146 ASSERT_TRUE(ref.IsValid());
147 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]400b133f2011-01-19 18:32:30148 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"),
149 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()));
[email protected]d82443b2009-01-15 19:54:56150 ASSERT_TRUE(result.is_valid());
151 ASSERT_EQ("http://fooxxutf-8ya/", result.spec());
152}
153
[email protected]d88cb202011-08-17 20:03:01154// Test that setting the prepopulate ID from TemplateURL causes the stored
155// TemplateURLRef to handle parsing the URL parameters differently.
[email protected]1a257262011-06-28 22:15:44156TEST_F(TemplateURLTest, SetPrepopulatedAndParse) {
157 TemplateURL t_url;
158 t_url.SetURL("http://foo{fhqwhgads}", 0, 0);
159 TemplateURLRef::Replacements replacements;
160 bool valid = false;
161
162 t_url.SetPrepopulateId(0);
163 EXPECT_EQ("http://foo{fhqwhgads}",
164 t_url.url()->ParseURL("http://foo{fhqwhgads}",
165 &replacements,
166 &valid));
167 EXPECT_TRUE(replacements.empty());
168 EXPECT_TRUE(valid);
169
170 t_url.SetPrepopulateId(123);
171 EXPECT_EQ("http://foo",
172 t_url.url()->ParseURL("http://foo{fhqwhgads}",
173 &replacements,
174 &valid));
175 EXPECT_TRUE(replacements.empty());
176 EXPECT_TRUE(valid);
177}
178
[email protected]d82443b2009-01-15 19:54:56179TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
180 TemplateURL t_url;
181 TemplateURLRef ref(
[email protected]ddd231e2010-06-29 20:35:19182 "http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b", 1, 2);
[email protected]d82443b2009-01-15 19:54:56183 ASSERT_TRUE(ref.IsValid());
184 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]400b133f2011-01-19 18:32:30185 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"),
186 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()));
[email protected]d82443b2009-01-15 19:54:56187 ASSERT_TRUE(result.is_valid());
188 ASSERT_EQ("http://fooxutf-8axyb/", result.spec());
189}
190
191TEST_F(TemplateURLTest, URLRefTestEncoding2) {
192 TemplateURL t_url;
193 TemplateURLRef ref(
[email protected]ddd231e2010-06-29 20:35:19194 "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", 1, 2);
[email protected]d82443b2009-01-15 19:54:56195 ASSERT_TRUE(ref.IsValid());
196 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]400b133f2011-01-19 18:32:30197 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"),
198 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()));
[email protected]d82443b2009-01-15 19:54:56199 ASSERT_TRUE(result.is_valid());
200 ASSERT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
201}
202
[email protected]375bd7312010-08-30 22:18:13203TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) {
204 struct SearchTermsCase {
205 const char* url;
[email protected]400b133f2011-01-19 18:32:30206 const string16 terms;
[email protected]375bd7312010-08-30 22:18:13207 const char* output;
208 } search_term_cases[] = {
[email protected]400b133f2011-01-19 18:32:30209 { "{google:baseURL}{language}{searchTerms}", string16(),
[email protected]375bd7312010-08-30 22:18:13210 "http://example.com/e/yy" },
[email protected]400b133f2011-01-19 18:32:30211 { "{google:baseSuggestURL}{searchTerms}", string16(),
[email protected]014010e2011-10-01 04:12:44212 "http://example.com/complete/" }
[email protected]375bd7312010-08-30 22:18:13213 };
214
215 TestSearchTermsData search_terms_data("http://example.com/e/");
216 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
217 const SearchTermsCase& value = search_term_cases[i];
218 TemplateURL t_url;
219 TemplateURLRef ref(value.url, 0, 0);
220 ASSERT_TRUE(ref.IsValid());
221
222 ASSERT_TRUE(ref.SupportsReplacement());
223 GURL result = GURL(ref.ReplaceSearchTermsUsingTermsData(
224 t_url, value.terms,
[email protected]400b133f2011-01-19 18:32:30225 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16(),
[email protected]375bd7312010-08-30 22:18:13226 search_terms_data));
227 ASSERT_TRUE(result.is_valid());
228 ASSERT_EQ(value.output, result.spec());
229 }
230}
231
[email protected]d82443b2009-01-15 19:54:56232TEST_F(TemplateURLTest, URLRefTermToWide) {
233 struct ToWideCase {
234 const char* encoded_search_term;
[email protected]400b133f2011-01-19 18:32:30235 const string16 expected_decoded_term;
[email protected]d82443b2009-01-15 19:54:56236 } to_wide_cases[] = {
[email protected]400b133f2011-01-19 18:32:30237 {"hello+world", ASCIIToUTF16("hello world")},
[email protected]d82443b2009-01-15 19:54:56238 // Test some big-5 input.
[email protected]400b133f2011-01-19 18:32:30239 {"%a7A%A6%6e+to+you", WideToUTF16(L"\x4f60\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56240 // Test some UTF-8 input. We should fall back to this when the encoding
241 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
242 // Big-5 trailing byte.
[email protected]400b133f2011-01-19 18:32:30243 {"%e4%bd%a05%e5%a5%bd+to+you", WideToUTF16(L"\x4f60\x35\x597d to you")},
[email protected]d82443b2009-01-15 19:54:56244 // Undecodable input should stay escaped.
[email protected]400b133f2011-01-19 18:32:30245 {"%91%01+abcd", WideToUTF16(L"%91%01 abcd")},
[email protected]7df43482009-07-31 19:37:44246 // Make sure we convert %2B to +.
[email protected]400b133f2011-01-19 18:32:30247 {"C%2B%2B", ASCIIToUTF16("C++")},
[email protected]7df43482009-07-31 19:37:44248 // C%2B is escaped as C%252B, make sure we unescape it properly.
[email protected]400b133f2011-01-19 18:32:30249 {"C%252B", ASCIIToUTF16("C%2B")},
[email protected]d82443b2009-01-15 19:54:56250 };
251
252 TemplateURL t_url;
253
254 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
255 std::vector<std::string> encodings;
256 encodings.push_back("big-5");
257 t_url.set_input_encodings(encodings);
258
[email protected]ddd231e2010-06-29 20:35:19259 TemplateURLRef ref("http://foo?q={searchTerms}", 1, 2);
[email protected]d82443b2009-01-15 19:54:56260 ASSERT_TRUE(ref.IsValid());
261 ASSERT_TRUE(ref.SupportsReplacement());
262
[email protected]f63ae312009-02-04 17:58:46263 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) {
[email protected]400b133f2011-01-19 18:32:30264 string16 result = ref.SearchTermToString16(t_url,
[email protected]d82443b2009-01-15 19:54:56265 to_wide_cases[i].encoded_search_term);
266
[email protected]400b133f2011-01-19 18:32:30267 EXPECT_EQ(to_wide_cases[i].expected_decoded_term, result);
[email protected]d82443b2009-01-15 19:54:56268 }
269}
270
[email protected]59762552011-03-15 14:51:02271TEST_F(TemplateURLTest, SetFavicon) {
[email protected]d82443b2009-01-15 19:54:56272 TemplateURL url;
273 GURL favicon_url("http://favicon.url");
[email protected]59762552011-03-15 14:51:02274 url.SetFaviconURL(favicon_url);
[email protected]f63ae312009-02-04 17:58:46275 ASSERT_EQ(1U, url.image_refs().size());
[email protected]b4dc12e2011-03-15 17:58:58276 ASSERT_TRUE(favicon_url == url.GetFaviconURL());
[email protected]d82443b2009-01-15 19:54:56277
278 GURL favicon_url2("http://favicon2.url");
[email protected]59762552011-03-15 14:51:02279 url.SetFaviconURL(favicon_url2);
[email protected]f63ae312009-02-04 17:58:46280 ASSERT_EQ(1U, url.image_refs().size());
[email protected]b4dc12e2011-03-15 17:58:58281 ASSERT_TRUE(favicon_url2 == url.GetFaviconURL());
[email protected]d82443b2009-01-15 19:54:56282}
283
284TEST_F(TemplateURLTest, DisplayURLToURLRef) {
285 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19286 const std::string url;
[email protected]400b133f2011-01-19 18:32:30287 const string16 expected_result;
[email protected]d82443b2009-01-15 19:54:56288 } data[] = {
[email protected]ddd231e2010-06-29 20:35:19289 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]400b133f2011-01-19 18:32:30290 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") },
[email protected]ddd231e2010-06-29 20:35:19291 { "http://X",
[email protected]400b133f2011-01-19 18:32:30292 ASCIIToUTF16("http://X") },
[email protected]ddd231e2010-06-29 20:35:19293 { "http://foo{searchTerms",
[email protected]400b133f2011-01-19 18:32:30294 ASCIIToUTF16("http://foo{searchTerms") },
[email protected]ddd231e2010-06-29 20:35:19295 { "http://foo{searchTerms}{language}",
[email protected]400b133f2011-01-19 18:32:30296 ASCIIToUTF16("http://foo%s{language}") },
[email protected]d82443b2009-01-15 19:54:56297 };
[email protected]f63ae312009-02-04 17:58:46298 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
[email protected]d82443b2009-01-15 19:54:56299 TemplateURLRef ref(data[i].url, 1, 2);
300 EXPECT_EQ(data[i].expected_result, ref.DisplayURL());
301 EXPECT_EQ(data[i].url,
302 TemplateURLRef::DisplayURLToURLRef(ref.DisplayURL()));
303 }
304}
305
306TEST_F(TemplateURLTest, ReplaceSearchTerms) {
307 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19308 const std::string url;
[email protected]d82443b2009-01-15 19:54:56309 const std::string expected_result;
310 } data[] = {
[email protected]ddd231e2010-06-29 20:35:19311 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56312 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19313 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56314 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19315 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56316 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19317 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56318 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19319 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56320 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19321 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56322 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19323 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56324 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19325 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56326 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19327 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56328 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19329 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56330 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19331 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56332 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19333 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56334 "http://foo/UTF-8a{language}aXa" },
335 };
336 TemplateURL turl;
337 turl.add_input_encoding("UTF-8");
[email protected]f63ae312009-02-04 17:58:46338 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
[email protected]d82443b2009-01-15 19:54:56339 TemplateURLRef ref(data[i].url, 1, 2);
340 EXPECT_TRUE(ref.IsValid());
341 EXPECT_TRUE(ref.SupportsReplacement());
342 std::string expected_result = data[i].expected_result;
343 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}",
[email protected]d70539de2009-06-24 22:17:06344 g_browser_process->GetApplicationLocale());
[email protected]400b133f2011-01-19 18:32:30345 GURL result = GURL(ref.ReplaceSearchTerms(turl, ASCIIToUTF16("X"),
346 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()));
[email protected]d82443b2009-01-15 19:54:56347 EXPECT_TRUE(result.is_valid());
348 EXPECT_EQ(expected_result, result.spec());
349 }
350}
351
352
353// Tests replacing search terms in various encodings and making sure the
354// generated URL matches the expected value.
355TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
356 struct TestData {
357 const std::string encoding;
[email protected]400b133f2011-01-19 18:32:30358 const string16 search_term;
[email protected]ddd231e2010-06-29 20:35:19359 const std::string url;
[email protected]d82443b2009-01-15 19:54:56360 const std::string expected_result;
361 } data[] = {
[email protected]400b133f2011-01-19 18:32:30362 { "BIG5", WideToUTF16(L"\x60BD"),
363 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17364 "http://foo/?%B1~BIG5" },
[email protected]400b133f2011-01-19 18:32:30365 { "UTF-8", ASCIIToUTF16("blah"),
366 "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17367 "http://foo/?blahUTF-8" },
[email protected]d82443b2009-01-15 19:54:56368 };
[email protected]f63ae312009-02-04 17:58:46369 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
[email protected]d82443b2009-01-15 19:54:56370 TemplateURL turl;
371 turl.add_input_encoding(data[i].encoding);
372 TemplateURLRef ref(data[i].url, 1, 2);
[email protected]ddd231e2010-06-29 20:35:19373 GURL result = GURL(ref.ReplaceSearchTerms(turl,
[email protected]7b9f3672009-06-15 18:31:22374 data[i].search_term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE,
[email protected]400b133f2011-01-19 18:32:30375 string16()));
[email protected]d82443b2009-01-15 19:54:56376 EXPECT_TRUE(result.is_valid());
377 EXPECT_EQ(data[i].expected_result, result.spec());
378 }
379}
380
381TEST_F(TemplateURLTest, Suggestions) {
382 struct TestData {
383 const int accepted_suggestion;
[email protected]400b133f2011-01-19 18:32:30384 const string16 original_query_for_suggestion;
[email protected]d82443b2009-01-15 19:54:56385 const std::string expected_result;
386 } data[] = {
[email protected]400b133f2011-01-19 18:32:30387 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16(),
[email protected]d82443b2009-01-15 19:54:56388 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30389 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"),
[email protected]d82443b2009-01-15 19:54:56390 "http://bar/foo?q=foobar" },
[email protected]400b133f2011-01-19 18:32:30391 { TemplateURLRef::NO_SUGGESTION_CHOSEN, string16(),
[email protected]d82443b2009-01-15 19:54:56392 "http://bar/foo?aq=f&q=foobar" },
[email protected]400b133f2011-01-19 18:32:30393 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"),
[email protected]d82443b2009-01-15 19:54:56394 "http://bar/foo?aq=f&q=foobar" },
[email protected]400b133f2011-01-19 18:32:30395 { 0, string16(), "http://bar/foo?aq=0&oq=&q=foobar" },
396 { 1, ASCIIToUTF16("foo"), "http://bar/foo?aq=1&oq=foo&q=foobar" },
[email protected]d82443b2009-01-15 19:54:56397 };
398 TemplateURL turl;
399 turl.add_input_encoding("UTF-8");
[email protected]ddd231e2010-06-29 20:35:19400 TemplateURLRef ref("http://bar/foo?{google:acceptedSuggestion}"
401 "{google:originalQueryForSuggestion}q={searchTerms}", 1, 2);
[email protected]d82443b2009-01-15 19:54:56402 ASSERT_TRUE(ref.IsValid());
403 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]f63ae312009-02-04 17:58:46404 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
[email protected]400b133f2011-01-19 18:32:30405 GURL result = GURL(ref.ReplaceSearchTerms(turl, ASCIIToUTF16("foobar"),
[email protected]ddd231e2010-06-29 20:35:19406 data[i].accepted_suggestion, data[i].original_query_for_suggestion));
[email protected]d82443b2009-01-15 19:54:56407 EXPECT_TRUE(result.is_valid());
408 EXPECT_EQ(data[i].expected_result, result.spec());
409 }
410}
411
[email protected]12bd05872009-03-17 19:25:06412#if defined(OS_WIN)
[email protected]81f808de2009-09-25 01:36:34413TEST_F(TemplateURLTest, RLZ) {
[email protected]400b133f2011-01-19 18:32:30414 string16 rlz_string;
[email protected]58b64332010-08-13 16:09:39415#if defined(GOOGLE_CHROME_BUILD)
[email protected]1c262172010-06-10 15:25:46416 RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz_string);
[email protected]58b64332010-08-13 16:09:39417#endif
[email protected]d82443b2009-01-15 19:54:56418
419 TemplateURL t_url;
[email protected]ddd231e2010-06-29 20:35:19420 TemplateURLRef ref("http://bar/?{google:RLZ}{searchTerms}", 1, 2);
[email protected]d82443b2009-01-15 19:54:56421 ASSERT_TRUE(ref.IsValid());
422 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]ddd231e2010-06-29 20:35:19423 GURL result(ref.ReplaceSearchTerms(t_url, L"x",
[email protected]400b133f2011-01-19 18:32:30424 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()));
[email protected]d82443b2009-01-15 19:54:56425 ASSERT_TRUE(result.is_valid());
[email protected]12bd05872009-03-17 19:25:06426 std::string expected_url = "http://bar/?";
427 if (!rlz_string.empty()) {
428 expected_url += "rlz=" + WideToUTF8(rlz_string) + "&";
429 }
430 expected_url += "x";
431 ASSERT_EQ(expected_url, result.spec());
[email protected]d82443b2009-01-15 19:54:56432}
[email protected]81f808de2009-09-25 01:36:34433#endif
[email protected]d82443b2009-01-15 19:54:56434
435TEST_F(TemplateURLTest, HostAndSearchTermKey) {
436 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19437 const std::string url;
[email protected]d82443b2009-01-15 19:54:56438 const std::string host;
439 const std::string path;
440 const std::string search_term_key;
441 } data[] = {
[email protected]ddd231e2010-06-29 20:35:19442 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56443
444 // No query key should result in empty values.
[email protected]ddd231e2010-06-29 20:35:19445 { "http://blah/{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56446
447 // No term should result in empty values.
[email protected]ddd231e2010-06-29 20:35:19448 { "http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56449
450 // Multiple terms should result in empty values.
[email protected]ddd231e2010-06-29 20:35:19451 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56452
453 // Term in the host shouldn't match.
[email protected]ddd231e2010-06-29 20:35:19454 { "http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56455
[email protected]ddd231e2010-06-29 20:35:19456 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56457
458 // Single term with extra chars in value should match.
[email protected]ddd231e2010-06-29 20:35:19459 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56460 };
461
462 TemplateURL t_url;
[email protected]f63ae312009-02-04 17:58:46463 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
[email protected]d82443b2009-01-15 19:54:56464 t_url.SetURL(data[i].url, 0, 0);
465 EXPECT_EQ(data[i].host, t_url.url()->GetHost());
466 EXPECT_EQ(data[i].path, t_url.url()->GetPath());
467 EXPECT_EQ(data[i].search_term_key, t_url.url()->GetSearchTermKey());
468 }
469}
470
471TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
472 static const struct {
[email protected]ddd231e2010-06-29 20:35:19473 const char* const base_url;
474 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56475 } data[] = {
[email protected]014010e2011-10-01 04:12:44476 { "http://google.com/", "http://google.com/complete/", },
477 { "http://www.google.com/", "http://www.google.com/complete/", },
478 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", },
479 { "http://www.google.com.by/", "http://www.google.com.by/complete/", },
480 { "http://google.com/intl/xx/", "http://google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56481 };
482
[email protected]f63ae312009-02-04 17:58:46483 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i)
[email protected]d82443b2009-01-15 19:54:56484 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
485}
486
487TEST_F(TemplateURLTest, Keyword) {
488 TemplateURL t_url;
[email protected]ddd231e2010-06-29 20:35:19489 t_url.SetURL("http://www.google.com/search", 0, 0);
[email protected]d82443b2009-01-15 19:54:56490 EXPECT_FALSE(t_url.autogenerate_keyword());
[email protected]400b133f2011-01-19 18:32:30491 t_url.set_keyword(ASCIIToUTF16("foo"));
492 EXPECT_EQ(ASCIIToUTF16("foo"), t_url.keyword());
[email protected]d82443b2009-01-15 19:54:56493 t_url.set_autogenerate_keyword(true);
494 EXPECT_TRUE(t_url.autogenerate_keyword());
[email protected]400b133f2011-01-19 18:32:30495 EXPECT_EQ(ASCIIToUTF16("google.com"), t_url.keyword());
496 t_url.set_keyword(ASCIIToUTF16("foo"));
[email protected]d82443b2009-01-15 19:54:56497 EXPECT_FALSE(t_url.autogenerate_keyword());
[email protected]400b133f2011-01-19 18:32:30498 EXPECT_EQ(ASCIIToUTF16("foo"), t_url.keyword());
[email protected]d82443b2009-01-15 19:54:56499}
[email protected]81c6ef62010-01-21 09:58:47500
501TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19502 std::string parsed_url("{searchTerms}");
[email protected]81c6ef62010-01-21 09:58:47503 TemplateURLRef url_ref(parsed_url, 0, 0);
504 TemplateURLRef::Replacements replacements;
505 EXPECT_TRUE(url_ref.ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19506 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47507 ASSERT_EQ(1U, replacements.size());
[email protected]4c66d2a2010-05-11 04:36:05508 EXPECT_EQ(static_cast<size_t>(0), replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47509 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
510}
511
512TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]1a257262011-06-28 22:15:44513 std::string parsed_url("{fhqwhgads}");
[email protected]81c6ef62010-01-21 09:58:47514 TemplateURLRef url_ref(parsed_url, 0, 0);
515 TemplateURLRef::Replacements replacements;
[email protected]1a257262011-06-28 22:15:44516
517 // By default, TemplateURLRef should not consider itself prepopulated.
518 // Therefore we should not replace the unknown parameter.
519 EXPECT_FALSE(url_ref.ParseParameter(0, 10, &parsed_url, &replacements));
520 EXPECT_EQ("{fhqwhgads}", parsed_url);
521 EXPECT_TRUE(replacements.empty());
522
523 // If the TemplateURLRef is prepopulated, we should remove unknown parameters.
524 parsed_url = "{fhqwhgads}";
525 url_ref.set_prepopulated(true);
526 EXPECT_FALSE(url_ref.ParseParameter(0, 10, &parsed_url, &replacements));
527 EXPECT_EQ("", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47528 EXPECT_TRUE(replacements.empty());
529}
530
531TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]ddd231e2010-06-29 20:35:19532 TemplateURLRef url_ref("", 0, 0);
[email protected]81c6ef62010-01-21 09:58:47533 TemplateURLRef::Replacements replacements;
534 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19535 EXPECT_EQ(std::string(), url_ref.ParseURL("", &replacements, &valid));
[email protected]81c6ef62010-01-21 09:58:47536 EXPECT_TRUE(replacements.empty());
537 EXPECT_TRUE(valid);
538}
539
540TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]ddd231e2010-06-29 20:35:19541 TemplateURLRef url_ref("{", 0, 0);
[email protected]81c6ef62010-01-21 09:58:47542 TemplateURLRef::Replacements replacements;
543 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19544 EXPECT_EQ(std::string(), url_ref.ParseURL("{", &replacements, &valid));
[email protected]81c6ef62010-01-21 09:58:47545 EXPECT_TRUE(replacements.empty());
546 EXPECT_FALSE(valid);
547}
548
549TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]ddd231e2010-06-29 20:35:19550 TemplateURLRef url_ref("{}", 0, 0);
[email protected]81c6ef62010-01-21 09:58:47551 TemplateURLRef::Replacements replacements;
552 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19553 EXPECT_EQ("{}", url_ref.ParseURL("{}", &replacements, &valid));
[email protected]81c6ef62010-01-21 09:58:47554 EXPECT_TRUE(replacements.empty());
555 EXPECT_TRUE(valid);
556}
557
558TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]ddd231e2010-06-29 20:35:19559 TemplateURLRef url_ref("{}{{%s}}", 0, 0);
[email protected]81c6ef62010-01-21 09:58:47560 TemplateURLRef::Replacements replacements;
561 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19562 EXPECT_EQ("{}{}",
563 url_ref.ParseURL("{}{{searchTerms}}", &replacements, &valid));
[email protected]81c6ef62010-01-21 09:58:47564 ASSERT_EQ(1U, replacements.size());
[email protected]4c66d2a2010-05-11 04:36:05565 EXPECT_EQ(static_cast<size_t>(3), replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47566 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
567 EXPECT_TRUE(valid);
568}
569
570TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]ddd231e2010-06-29 20:35:19571 TemplateURLRef url_ref("{%s", 0, 0);
[email protected]81c6ef62010-01-21 09:58:47572 TemplateURLRef::Replacements replacements;
573 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19574 EXPECT_EQ("{", url_ref.ParseURL("{{searchTerms}", &replacements, &valid));
[email protected]81c6ef62010-01-21 09:58:47575 ASSERT_EQ(1U, replacements.size());
[email protected]4c66d2a2010-05-11 04:36:05576 EXPECT_EQ(static_cast<size_t>(1), replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47577 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
578 EXPECT_TRUE(valid);
579}