blob: 05087b8977f2470e84f7983eeb34235b4485a23f [file] [log] [blame]
[email protected]58b64332010-08-13 16:09:391// Copyright (c) 2006-2010 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]d54e03a52009-01-16 00:31:0410#include "chrome/browser/search_engines/template_url.h"
[email protected]d82443b2009-01-15 19:54:5611#include "testing/gtest/include/gtest/gtest.h"
12
13class TemplateURLTest : public testing::Test {
14 public:
15 virtual void TearDown() {
16 delete TemplateURLRef::google_base_url_;
17 TemplateURLRef::google_base_url_ = NULL;
18 }
19
[email protected]ddd231e2010-06-29 20:35:1920 void CheckSuggestBaseURL(const char* base_url,
21 const char* base_suggest_url) const {
[email protected]d82443b2009-01-15 19:54:5622 delete TemplateURLRef::google_base_url_;
[email protected]ddd231e2010-06-29 20:35:1923 TemplateURLRef::google_base_url_ = new std::string(base_url);
[email protected]d82443b2009-01-15 19:54:5624 EXPECT_STREQ(base_suggest_url,
25 TemplateURLRef::GoogleBaseSuggestURLValue().c_str());
26 }
27};
28
29TEST_F(TemplateURLTest, Defaults) {
30 TemplateURL url;
31 ASSERT_FALSE(url.show_in_default_list());
32 ASSERT_FALSE(url.safe_for_autoreplace());
33 ASSERT_EQ(0, url.prepopulate_id());
34}
35
36TEST_F(TemplateURLTest, TestValidWithComplete) {
[email protected]ddd231e2010-06-29 20:35:1937 TemplateURLRef ref("{searchTerms}", 0, 0);
[email protected]d82443b2009-01-15 19:54:5638 ASSERT_TRUE(ref.IsValid());
39}
40
41TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1942 struct SearchTermsCase {
[email protected]ddd231e2010-06-29 20:35:1943 const char* url;
[email protected]0d2e6a62010-01-15 20:09:1944 const wchar_t* terms;
45 const char* output;
46 } search_term_cases[] = {
[email protected]ddd231e2010-06-29 20:35:1947 { "http://foo{searchTerms}", L"sea rch/bar", "http://foosea%20rch/bar" },
48 { "http://foo{searchTerms}?boo=abc", L"sea rch/bar",
[email protected]3c75f0d2010-03-02 05:51:1749 "http://foosea%20rch/bar?boo=abc" },
[email protected]ddd231e2010-06-29 20:35:1950 { "http://foo/?boo={searchTerms}", L"sea rch/bar",
[email protected]3c75f0d2010-03-02 05:51:1751 "http://foo/?boo=sea+rch%2Fbar" },
[email protected]ddd231e2010-06-29 20:35:1952 { "http://en.wikipedia.org/{searchTerms}", L"wiki/?",
[email protected]3c75f0d2010-03-02 05:51:1753 "http://en.wikipedia.org/wiki/%3F" }
[email protected]0d2e6a62010-01-15 20:09:1954 };
55 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) {
56 const SearchTermsCase& value = search_term_cases[i];
57 TemplateURL t_url;
58 TemplateURLRef ref(value.url, 0, 0);
59 ASSERT_TRUE(ref.IsValid());
[email protected]d82443b2009-01-15 19:54:5660
[email protected]0d2e6a62010-01-15 20:09:1961 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]ddd231e2010-06-29 20:35:1962 GURL result = GURL(ref.ReplaceSearchTerms(t_url, value.terms,
63 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()));
[email protected]0d2e6a62010-01-15 20:09:1964 ASSERT_TRUE(result.is_valid());
65 ASSERT_EQ(value.output, result.spec());
66 }
[email protected]d82443b2009-01-15 19:54:5667}
68
69TEST_F(TemplateURLTest, URLRefTestCount) {
70 TemplateURL t_url;
[email protected]ddd231e2010-06-29 20:35:1971 TemplateURLRef ref("http://foo{searchTerms}{count?}", 0, 0);
[email protected]d82443b2009-01-15 19:54:5672 ASSERT_TRUE(ref.IsValid());
73 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]ddd231e2010-06-29 20:35:1974 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X",
75 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()));
[email protected]d82443b2009-01-15 19:54:5676 ASSERT_TRUE(result.is_valid());
77 ASSERT_EQ("http://foox/", result.spec());
78}
79
80TEST_F(TemplateURLTest, URLRefTestCount2) {
81 TemplateURL t_url;
[email protected]ddd231e2010-06-29 20:35:1982 TemplateURLRef ref("http://foo{searchTerms}{count}", 0, 0);
[email protected]d82443b2009-01-15 19:54:5683 ASSERT_TRUE(ref.IsValid());
84 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]ddd231e2010-06-29 20:35:1985 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X",
86 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()));
[email protected]d82443b2009-01-15 19:54:5687 ASSERT_TRUE(result.is_valid());
88 ASSERT_EQ("http://foox10/", result.spec());
89}
90
91TEST_F(TemplateURLTest, URLRefTestIndices) {
92 TemplateURL t_url;
[email protected]ddd231e2010-06-29 20:35:1993 TemplateURLRef ref("http://foo{searchTerms}x{startIndex?}y{startPage?}",
[email protected]d82443b2009-01-15 19:54:5694 1, 2);
95 ASSERT_TRUE(ref.IsValid());
96 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]ddd231e2010-06-29 20:35:1997 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X",
98 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()));
[email protected]d82443b2009-01-15 19:54:5699 ASSERT_TRUE(result.is_valid());
100 ASSERT_EQ("http://fooxxy/", result.spec());
101}
102
103TEST_F(TemplateURLTest, URLRefTestIndices2) {
104 TemplateURL t_url;
[email protected]ddd231e2010-06-29 20:35:19105 TemplateURLRef ref("http://foo{searchTerms}x{startIndex}y{startPage}", 1, 2);
[email protected]d82443b2009-01-15 19:54:56106 ASSERT_TRUE(ref.IsValid());
107 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]ddd231e2010-06-29 20:35:19108 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X",
109 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()));
[email protected]d82443b2009-01-15 19:54:56110 ASSERT_TRUE(result.is_valid());
111 ASSERT_EQ("http://fooxx1y2/", result.spec());
112}
113
114TEST_F(TemplateURLTest, URLRefTestEncoding) {
115 TemplateURL t_url;
116 TemplateURLRef ref(
[email protected]ddd231e2010-06-29 20:35:19117 "http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a", 1, 2);
[email protected]d82443b2009-01-15 19:54:56118 ASSERT_TRUE(ref.IsValid());
119 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]ddd231e2010-06-29 20:35:19120 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X",
121 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()));
[email protected]d82443b2009-01-15 19:54:56122 ASSERT_TRUE(result.is_valid());
123 ASSERT_EQ("http://fooxxutf-8ya/", result.spec());
124}
125
126TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
127 TemplateURL t_url;
128 TemplateURLRef ref(
[email protected]ddd231e2010-06-29 20:35:19129 "http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b", 1, 2);
[email protected]d82443b2009-01-15 19:54:56130 ASSERT_TRUE(ref.IsValid());
131 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]ddd231e2010-06-29 20:35:19132 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X",
133 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()));
[email protected]d82443b2009-01-15 19:54:56134 ASSERT_TRUE(result.is_valid());
135 ASSERT_EQ("http://fooxutf-8axyb/", result.spec());
136}
137
138TEST_F(TemplateURLTest, URLRefTestEncoding2) {
139 TemplateURL t_url;
140 TemplateURLRef ref(
[email protected]ddd231e2010-06-29 20:35:19141 "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", 1, 2);
[email protected]d82443b2009-01-15 19:54:56142 ASSERT_TRUE(ref.IsValid());
143 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]ddd231e2010-06-29 20:35:19144 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X",
145 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()));
[email protected]d82443b2009-01-15 19:54:56146 ASSERT_TRUE(result.is_valid());
147 ASSERT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
148}
149
150TEST_F(TemplateURLTest, URLRefTermToWide) {
151 struct ToWideCase {
152 const char* encoded_search_term;
153 const wchar_t* expected_decoded_term;
154 } to_wide_cases[] = {
155 {"hello+world", L"hello world"},
156 // Test some big-5 input.
157 {"%a7A%A6%6e+to+you", L"\x4f60\x597d to you"},
158 // Test some UTF-8 input. We should fall back to this when the encoding
159 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
160 // Big-5 trailing byte.
161 {"%e4%bd%a05%e5%a5%bd+to+you", L"\x4f60\x35\x597d to you"},
162 // Undecodable input should stay escaped.
163 {"%91%01+abcd", L"%91%01 abcd"},
[email protected]7df43482009-07-31 19:37:44164 // Make sure we convert %2B to +.
165 {"C%2B%2B", L"C++"},
166 // C%2B is escaped as C%252B, make sure we unescape it properly.
167 {"C%252B", L"C%2B"},
[email protected]d82443b2009-01-15 19:54:56168 };
169
170 TemplateURL t_url;
171
172 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
173 std::vector<std::string> encodings;
174 encodings.push_back("big-5");
175 t_url.set_input_encodings(encodings);
176
[email protected]ddd231e2010-06-29 20:35:19177 TemplateURLRef ref("http://foo?q={searchTerms}", 1, 2);
[email protected]d82443b2009-01-15 19:54:56178 ASSERT_TRUE(ref.IsValid());
179 ASSERT_TRUE(ref.SupportsReplacement());
180
[email protected]f63ae312009-02-04 17:58:46181 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) {
[email protected]d82443b2009-01-15 19:54:56182 std::wstring result = ref.SearchTermToWide(t_url,
183 to_wide_cases[i].encoded_search_term);
184
185 EXPECT_EQ(std::wstring(to_wide_cases[i].expected_decoded_term), result);
186 }
187}
188
189TEST_F(TemplateURLTest, SetFavIcon) {
190 TemplateURL url;
191 GURL favicon_url("http://favicon.url");
192 url.SetFavIconURL(favicon_url);
[email protected]f63ae312009-02-04 17:58:46193 ASSERT_EQ(1U, url.image_refs().size());
[email protected]d82443b2009-01-15 19:54:56194 ASSERT_TRUE(favicon_url == url.GetFavIconURL());
195
196 GURL favicon_url2("http://favicon2.url");
197 url.SetFavIconURL(favicon_url2);
[email protected]f63ae312009-02-04 17:58:46198 ASSERT_EQ(1U, url.image_refs().size());
[email protected]d82443b2009-01-15 19:54:56199 ASSERT_TRUE(favicon_url2 == url.GetFavIconURL());
200}
201
202TEST_F(TemplateURLTest, DisplayURLToURLRef) {
203 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19204 const std::string url;
[email protected]d82443b2009-01-15 19:54:56205 const std::wstring expected_result;
206 } data[] = {
[email protected]ddd231e2010-06-29 20:35:19207 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56208 L"http://foo%sx{inputEncoding}y{outputEncoding}a" },
[email protected]ddd231e2010-06-29 20:35:19209 { "http://X",
[email protected]d82443b2009-01-15 19:54:56210 L"http://X" },
[email protected]ddd231e2010-06-29 20:35:19211 { "http://foo{searchTerms",
[email protected]d82443b2009-01-15 19:54:56212 L"http://foo{searchTerms" },
[email protected]ddd231e2010-06-29 20:35:19213 { "http://foo{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56214 L"http://foo%s{language}" },
215 };
[email protected]f63ae312009-02-04 17:58:46216 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
[email protected]d82443b2009-01-15 19:54:56217 TemplateURLRef ref(data[i].url, 1, 2);
218 EXPECT_EQ(data[i].expected_result, ref.DisplayURL());
219 EXPECT_EQ(data[i].url,
220 TemplateURLRef::DisplayURLToURLRef(ref.DisplayURL()));
221 }
222}
223
224TEST_F(TemplateURLTest, ReplaceSearchTerms) {
225 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19226 const std::string url;
[email protected]d82443b2009-01-15 19:54:56227 const std::string expected_result;
228 } data[] = {
[email protected]ddd231e2010-06-29 20:35:19229 { "http://foo/{language}{searchTerms}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56230 "http://foo/{language}XUTF-8" },
[email protected]ddd231e2010-06-29 20:35:19231 { "http://foo/{language}{inputEncoding}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56232 "http://foo/{language}UTF-8X" },
[email protected]ddd231e2010-06-29 20:35:19233 { "http://foo/{searchTerms}{language}{inputEncoding}",
[email protected]d82443b2009-01-15 19:54:56234 "http://foo/X{language}UTF-8" },
[email protected]ddd231e2010-06-29 20:35:19235 { "http://foo/{searchTerms}{inputEncoding}{language}",
[email protected]d82443b2009-01-15 19:54:56236 "http://foo/XUTF-8{language}" },
[email protected]ddd231e2010-06-29 20:35:19237 { "http://foo/{inputEncoding}{searchTerms}{language}",
[email protected]d82443b2009-01-15 19:54:56238 "http://foo/UTF-8X{language}" },
[email protected]ddd231e2010-06-29 20:35:19239 { "http://foo/{inputEncoding}{language}{searchTerms}",
[email protected]d82443b2009-01-15 19:54:56240 "http://foo/UTF-8{language}X" },
[email protected]ddd231e2010-06-29 20:35:19241 { "http://foo/{language}a{searchTerms}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56242 "http://foo/{language}aXaUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19243 { "http://foo/{language}a{inputEncoding}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56244 "http://foo/{language}aUTF-8aXa" },
[email protected]ddd231e2010-06-29 20:35:19245 { "http://foo/{searchTerms}a{language}a{inputEncoding}a",
[email protected]d82443b2009-01-15 19:54:56246 "http://foo/Xa{language}aUTF-8a" },
[email protected]ddd231e2010-06-29 20:35:19247 { "http://foo/{searchTerms}a{inputEncoding}a{language}a",
[email protected]d82443b2009-01-15 19:54:56248 "http://foo/XaUTF-8a{language}a" },
[email protected]ddd231e2010-06-29 20:35:19249 { "http://foo/{inputEncoding}a{searchTerms}a{language}a",
[email protected]d82443b2009-01-15 19:54:56250 "http://foo/UTF-8aXa{language}a" },
[email protected]ddd231e2010-06-29 20:35:19251 { "http://foo/{inputEncoding}a{language}a{searchTerms}a",
[email protected]d82443b2009-01-15 19:54:56252 "http://foo/UTF-8a{language}aXa" },
253 };
254 TemplateURL turl;
255 turl.add_input_encoding("UTF-8");
[email protected]f63ae312009-02-04 17:58:46256 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
[email protected]d82443b2009-01-15 19:54:56257 TemplateURLRef ref(data[i].url, 1, 2);
258 EXPECT_TRUE(ref.IsValid());
259 EXPECT_TRUE(ref.SupportsReplacement());
260 std::string expected_result = data[i].expected_result;
261 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}",
[email protected]d70539de2009-06-24 22:17:06262 g_browser_process->GetApplicationLocale());
[email protected]ddd231e2010-06-29 20:35:19263 GURL result = GURL(ref.ReplaceSearchTerms(turl, L"X",
264 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()));
[email protected]d82443b2009-01-15 19:54:56265 EXPECT_TRUE(result.is_valid());
266 EXPECT_EQ(expected_result, result.spec());
267 }
268}
269
270
271// Tests replacing search terms in various encodings and making sure the
272// generated URL matches the expected value.
273TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
274 struct TestData {
275 const std::string encoding;
276 const std::wstring search_term;
[email protected]ddd231e2010-06-29 20:35:19277 const std::string url;
[email protected]d82443b2009-01-15 19:54:56278 const std::string expected_result;
279 } data[] = {
[email protected]ddd231e2010-06-29 20:35:19280 { "BIG5", L"\x60BD", "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17281 "http://foo/?%B1~BIG5" },
[email protected]ddd231e2010-06-29 20:35:19282 { "UTF-8", L"blah", "http://foo/?{searchTerms}{inputEncoding}",
[email protected]3c75f0d2010-03-02 05:51:17283 "http://foo/?blahUTF-8" },
[email protected]d82443b2009-01-15 19:54:56284 };
[email protected]f63ae312009-02-04 17:58:46285 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
[email protected]d82443b2009-01-15 19:54:56286 TemplateURL turl;
287 turl.add_input_encoding(data[i].encoding);
288 TemplateURLRef ref(data[i].url, 1, 2);
[email protected]ddd231e2010-06-29 20:35:19289 GURL result = GURL(ref.ReplaceSearchTerms(turl,
[email protected]7b9f3672009-06-15 18:31:22290 data[i].search_term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE,
[email protected]ddd231e2010-06-29 20:35:19291 std::wstring()));
[email protected]d82443b2009-01-15 19:54:56292 EXPECT_TRUE(result.is_valid());
293 EXPECT_EQ(data[i].expected_result, result.spec());
294 }
295}
296
297TEST_F(TemplateURLTest, Suggestions) {
298 struct TestData {
299 const int accepted_suggestion;
300 const std::wstring original_query_for_suggestion;
301 const std::string expected_result;
302 } data[] = {
303 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring(),
304 "http://bar/foo?q=foobar" },
305 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, L"foo",
306 "http://bar/foo?q=foobar" },
307 { TemplateURLRef::NO_SUGGESTION_CHOSEN, std::wstring(),
308 "http://bar/foo?aq=f&q=foobar" },
309 { TemplateURLRef::NO_SUGGESTION_CHOSEN, L"foo",
310 "http://bar/foo?aq=f&q=foobar" },
311 { 0, std::wstring(), "http://bar/foo?aq=0&oq=&q=foobar" },
312 { 1, L"foo", "http://bar/foo?aq=1&oq=foo&q=foobar" },
313 };
314 TemplateURL turl;
315 turl.add_input_encoding("UTF-8");
[email protected]ddd231e2010-06-29 20:35:19316 TemplateURLRef ref("http://bar/foo?{google:acceptedSuggestion}"
317 "{google:originalQueryForSuggestion}q={searchTerms}", 1, 2);
[email protected]d82443b2009-01-15 19:54:56318 ASSERT_TRUE(ref.IsValid());
319 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]f63ae312009-02-04 17:58:46320 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
[email protected]ddd231e2010-06-29 20:35:19321 GURL result = GURL(ref.ReplaceSearchTerms(turl, L"foobar",
322 data[i].accepted_suggestion, data[i].original_query_for_suggestion));
[email protected]d82443b2009-01-15 19:54:56323 EXPECT_TRUE(result.is_valid());
324 EXPECT_EQ(data[i].expected_result, result.spec());
325 }
326}
327
[email protected]12bd05872009-03-17 19:25:06328#if defined(OS_WIN)
[email protected]81f808de2009-09-25 01:36:34329TEST_F(TemplateURLTest, RLZ) {
[email protected]d82443b2009-01-15 19:54:56330 std::wstring rlz_string;
[email protected]58b64332010-08-13 16:09:39331#if defined(GOOGLE_CHROME_BUILD)
[email protected]1c262172010-06-10 15:25:46332 RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz_string);
[email protected]58b64332010-08-13 16:09:39333#endif
[email protected]d82443b2009-01-15 19:54:56334
335 TemplateURL t_url;
[email protected]ddd231e2010-06-29 20:35:19336 TemplateURLRef ref("http://bar/?{google:RLZ}{searchTerms}", 1, 2);
[email protected]d82443b2009-01-15 19:54:56337 ASSERT_TRUE(ref.IsValid());
338 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]ddd231e2010-06-29 20:35:19339 GURL result(ref.ReplaceSearchTerms(t_url, L"x",
340 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()));
[email protected]d82443b2009-01-15 19:54:56341 ASSERT_TRUE(result.is_valid());
[email protected]12bd05872009-03-17 19:25:06342 std::string expected_url = "http://bar/?";
343 if (!rlz_string.empty()) {
344 expected_url += "rlz=" + WideToUTF8(rlz_string) + "&";
345 }
346 expected_url += "x";
347 ASSERT_EQ(expected_url, result.spec());
[email protected]d82443b2009-01-15 19:54:56348}
[email protected]81f808de2009-09-25 01:36:34349#endif
[email protected]d82443b2009-01-15 19:54:56350
351TEST_F(TemplateURLTest, HostAndSearchTermKey) {
352 struct TestData {
[email protected]ddd231e2010-06-29 20:35:19353 const std::string url;
[email protected]d82443b2009-01-15 19:54:56354 const std::string host;
355 const std::string path;
356 const std::string search_term_key;
357 } data[] = {
[email protected]ddd231e2010-06-29 20:35:19358 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56359
360 // No query key should result in empty values.
[email protected]ddd231e2010-06-29 20:35:19361 { "http://blah/{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56362
363 // No term should result in empty values.
[email protected]ddd231e2010-06-29 20:35:19364 { "http://blah/", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56365
366 // Multiple terms should result in empty values.
[email protected]ddd231e2010-06-29 20:35:19367 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56368
369 // Term in the host shouldn't match.
[email protected]ddd231e2010-06-29 20:35:19370 { "http://{searchTerms}", "", "", ""},
[email protected]d82443b2009-01-15 19:54:56371
[email protected]ddd231e2010-06-29 20:35:19372 { "http://blah/?q={searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56373
374 // Single term with extra chars in value should match.
[email protected]ddd231e2010-06-29 20:35:19375 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56376 };
377
378 TemplateURL t_url;
[email protected]f63ae312009-02-04 17:58:46379 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
[email protected]d82443b2009-01-15 19:54:56380 t_url.SetURL(data[i].url, 0, 0);
381 EXPECT_EQ(data[i].host, t_url.url()->GetHost());
382 EXPECT_EQ(data[i].path, t_url.url()->GetPath());
383 EXPECT_EQ(data[i].search_term_key, t_url.url()->GetSearchTermKey());
384 }
385}
386
387TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
388 static const struct {
[email protected]ddd231e2010-06-29 20:35:19389 const char* const base_url;
390 const char* const base_suggest_url;
[email protected]d82443b2009-01-15 19:54:56391 } data[] = {
[email protected]ddd231e2010-06-29 20:35:19392 { "http://google.com/", "http://clients1.google.com/complete/", },
393 { "http://www.google.com/", "http://clients1.google.com/complete/", },
394 { "http://www.google.co.uk/", "http://clients1.google.co.uk/complete/", },
395 { "http://www.google.com.by/",
396 "http://clients1.google.com.by/complete/", },
397 { "http://google.com/intl/xx/", "http://clients1.google.com/complete/", },
[email protected]d82443b2009-01-15 19:54:56398 };
399
[email protected]f63ae312009-02-04 17:58:46400 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i)
[email protected]d82443b2009-01-15 19:54:56401 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
402}
403
404TEST_F(TemplateURLTest, Keyword) {
405 TemplateURL t_url;
[email protected]ddd231e2010-06-29 20:35:19406 t_url.SetURL("http://www.google.com/search", 0, 0);
[email protected]d82443b2009-01-15 19:54:56407 EXPECT_FALSE(t_url.autogenerate_keyword());
408 t_url.set_keyword(L"foo");
409 EXPECT_EQ(L"foo", t_url.keyword());
410 t_url.set_autogenerate_keyword(true);
411 EXPECT_TRUE(t_url.autogenerate_keyword());
412 EXPECT_EQ(L"google.com", t_url.keyword());
413 t_url.set_keyword(L"foo");
414 EXPECT_FALSE(t_url.autogenerate_keyword());
415 EXPECT_EQ(L"foo", t_url.keyword());
416}
[email protected]81c6ef62010-01-21 09:58:47417
418TEST_F(TemplateURLTest, ParseParameterKnown) {
[email protected]ddd231e2010-06-29 20:35:19419 std::string parsed_url("{searchTerms}");
[email protected]81c6ef62010-01-21 09:58:47420 TemplateURLRef url_ref(parsed_url, 0, 0);
421 TemplateURLRef::Replacements replacements;
422 EXPECT_TRUE(url_ref.ParseParameter(0, 12, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19423 EXPECT_EQ(std::string(), parsed_url);
[email protected]81c6ef62010-01-21 09:58:47424 ASSERT_EQ(1U, replacements.size());
[email protected]4c66d2a2010-05-11 04:36:05425 EXPECT_EQ(static_cast<size_t>(0), replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47426 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
427}
428
429TEST_F(TemplateURLTest, ParseParameterUnknown) {
[email protected]ddd231e2010-06-29 20:35:19430 std::string parsed_url("{}");
[email protected]81c6ef62010-01-21 09:58:47431 TemplateURLRef url_ref(parsed_url, 0, 0);
432 TemplateURLRef::Replacements replacements;
433 EXPECT_FALSE(url_ref.ParseParameter(0, 1, &parsed_url, &replacements));
[email protected]ddd231e2010-06-29 20:35:19434 EXPECT_EQ("{}", parsed_url);
[email protected]81c6ef62010-01-21 09:58:47435 EXPECT_TRUE(replacements.empty());
436}
437
438TEST_F(TemplateURLTest, ParseURLEmpty) {
[email protected]ddd231e2010-06-29 20:35:19439 TemplateURLRef url_ref("", 0, 0);
[email protected]81c6ef62010-01-21 09:58:47440 TemplateURLRef::Replacements replacements;
441 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19442 EXPECT_EQ(std::string(), url_ref.ParseURL("", &replacements, &valid));
[email protected]81c6ef62010-01-21 09:58:47443 EXPECT_TRUE(replacements.empty());
444 EXPECT_TRUE(valid);
445}
446
447TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
[email protected]ddd231e2010-06-29 20:35:19448 TemplateURLRef url_ref("{", 0, 0);
[email protected]81c6ef62010-01-21 09:58:47449 TemplateURLRef::Replacements replacements;
450 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19451 EXPECT_EQ(std::string(), url_ref.ParseURL("{", &replacements, &valid));
[email protected]81c6ef62010-01-21 09:58:47452 EXPECT_TRUE(replacements.empty());
453 EXPECT_FALSE(valid);
454}
455
456TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
[email protected]ddd231e2010-06-29 20:35:19457 TemplateURLRef url_ref("{}", 0, 0);
[email protected]81c6ef62010-01-21 09:58:47458 TemplateURLRef::Replacements replacements;
459 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19460 EXPECT_EQ("{}", url_ref.ParseURL("{}", &replacements, &valid));
[email protected]81c6ef62010-01-21 09:58:47461 EXPECT_TRUE(replacements.empty());
462 EXPECT_TRUE(valid);
463}
464
465TEST_F(TemplateURLTest, ParseURLTwoParameters) {
[email protected]ddd231e2010-06-29 20:35:19466 TemplateURLRef url_ref("{}{{%s}}", 0, 0);
[email protected]81c6ef62010-01-21 09:58:47467 TemplateURLRef::Replacements replacements;
468 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19469 EXPECT_EQ("{}{}",
470 url_ref.ParseURL("{}{{searchTerms}}", &replacements, &valid));
[email protected]81c6ef62010-01-21 09:58:47471 ASSERT_EQ(1U, replacements.size());
[email protected]4c66d2a2010-05-11 04:36:05472 EXPECT_EQ(static_cast<size_t>(3), replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47473 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
474 EXPECT_TRUE(valid);
475}
476
477TEST_F(TemplateURLTest, ParseURLNestedParameter) {
[email protected]ddd231e2010-06-29 20:35:19478 TemplateURLRef url_ref("{%s", 0, 0);
[email protected]81c6ef62010-01-21 09:58:47479 TemplateURLRef::Replacements replacements;
480 bool valid = false;
[email protected]ddd231e2010-06-29 20:35:19481 EXPECT_EQ("{", url_ref.ParseURL("{{searchTerms}", &replacements, &valid));
[email protected]81c6ef62010-01-21 09:58:47482 ASSERT_EQ(1U, replacements.size());
[email protected]4c66d2a2010-05-11 04:36:05483 EXPECT_EQ(static_cast<size_t>(1), replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47484 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
485 EXPECT_TRUE(valid);
486}