blob: 5f9bfff80929da9683e1aca58ffe079bab947187 [file] [log] [blame]
[email protected]d82443b2009-01-15 19:54:561// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// 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
20 void CheckSuggestBaseURL(const wchar_t* base_url,
21 const wchar_t* base_suggest_url) const {
22 delete TemplateURLRef::google_base_url_;
23 TemplateURLRef::google_base_url_ = new std::wstring(base_url);
24 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) {
37 TemplateURLRef ref(L"{searchTerms}", 0, 0);
38 ASSERT_TRUE(ref.IsValid());
39}
40
41TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
[email protected]0d2e6a62010-01-15 20:09:1942 struct SearchTermsCase {
43 const wchar_t* url;
44 const wchar_t* terms;
45 const char* output;
46 } search_term_cases[] = {
[email protected]3c75f0d2010-03-02 05:51:1747 { L"http://foo{searchTerms}", L"sea rch/bar", "http://foosea%20rch/bar" },
48 { L"http://foo{searchTerms}?boo=abc", L"sea rch/bar",
49 "http://foosea%20rch/bar?boo=abc" },
50 { L"http://foo/?boo={searchTerms}", L"sea rch/bar",
51 "http://foo/?boo=sea+rch%2Fbar" },
52 { L"http://en.wikipedia.org/{searchTerms}", L"wiki/?",
53 "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());
62 GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(t_url, value.terms,
63 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())));
64 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;
71 TemplateURLRef ref(L"http://foo{searchTerms}{count?}", 0, 0);
72 ASSERT_TRUE(ref.IsValid());
73 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]7b9f3672009-06-15 18:31:2274 GURL result = GURL(WideToUTF8(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;
82 TemplateURLRef ref(L"http://foo{searchTerms}{count}", 0, 0);
83 ASSERT_TRUE(ref.IsValid());
84 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]7b9f3672009-06-15 18:31:2285 GURL result = GURL(WideToUTF8(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;
93 TemplateURLRef ref(L"http://foo{searchTerms}x{startIndex?}y{startPage?}",
94 1, 2);
95 ASSERT_TRUE(ref.IsValid());
96 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]7b9f3672009-06-15 18:31:2297 GURL result = GURL(WideToUTF8(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;
105 TemplateURLRef ref(L"http://foo{searchTerms}x{startIndex}y{startPage}", 1, 2);
106 ASSERT_TRUE(ref.IsValid());
107 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]7b9f3672009-06-15 18:31:22108 GURL result = GURL(WideToUTF8(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(
117 L"http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a", 1, 2);
118 ASSERT_TRUE(ref.IsValid());
119 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]7b9f3672009-06-15 18:31:22120 GURL result = GURL(WideToUTF8(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(
129 L"http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b", 1, 2);
130 ASSERT_TRUE(ref.IsValid());
131 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]7b9f3672009-06-15 18:31:22132 GURL result = GURL(WideToUTF8(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(
141 L"http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", 1, 2);
142 ASSERT_TRUE(ref.IsValid());
143 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]7b9f3672009-06-15 18:31:22144 GURL result = GURL(WideToUTF8(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
177 TemplateURLRef ref(L"http://foo?q={searchTerms}", 1, 2);
178 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 {
204 const std::wstring url;
205 const std::wstring expected_result;
206 } data[] = {
207 { L"http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
208 L"http://foo%sx{inputEncoding}y{outputEncoding}a" },
209 { L"http://X",
210 L"http://X" },
211 { L"http://foo{searchTerms",
212 L"http://foo{searchTerms" },
213 { L"http://foo{searchTerms}{language}",
214 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 {
226 const std::wstring url;
227 const std::string expected_result;
228 } data[] = {
229 { L"http://foo/{language}{searchTerms}{inputEncoding}",
230 "http://foo/{language}XUTF-8" },
231 { L"http://foo/{language}{inputEncoding}{searchTerms}",
232 "http://foo/{language}UTF-8X" },
233 { L"http://foo/{searchTerms}{language}{inputEncoding}",
234 "http://foo/X{language}UTF-8" },
235 { L"http://foo/{searchTerms}{inputEncoding}{language}",
236 "http://foo/XUTF-8{language}" },
237 { L"http://foo/{inputEncoding}{searchTerms}{language}",
238 "http://foo/UTF-8X{language}" },
239 { L"http://foo/{inputEncoding}{language}{searchTerms}",
240 "http://foo/UTF-8{language}X" },
241 { L"http://foo/{language}a{searchTerms}a{inputEncoding}a",
242 "http://foo/{language}aXaUTF-8a" },
243 { L"http://foo/{language}a{inputEncoding}a{searchTerms}a",
244 "http://foo/{language}aUTF-8aXa" },
245 { L"http://foo/{searchTerms}a{language}a{inputEncoding}a",
246 "http://foo/Xa{language}aUTF-8a" },
247 { L"http://foo/{searchTerms}a{inputEncoding}a{language}a",
248 "http://foo/XaUTF-8a{language}a" },
249 { L"http://foo/{inputEncoding}a{searchTerms}a{language}a",
250 "http://foo/UTF-8aXa{language}a" },
251 { L"http://foo/{inputEncoding}a{language}a{searchTerms}a",
252 "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]7b9f3672009-06-15 18:31:22263 GURL result = GURL(WideToUTF8(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;
277 const std::wstring url;
278 const std::string expected_result;
279 } data[] = {
[email protected]3c75f0d2010-03-02 05:51:17280 { "BIG5", L"\x60BD", L"http://foo/?{searchTerms}{inputEncoding}",
281 "http://foo/?%B1~BIG5" },
282 { "UTF-8", L"blah", L"http://foo/?{searchTerms}{inputEncoding}",
283 "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]7b9f3672009-06-15 18:31:22289 GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(turl,
290 data[i].search_term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE,
291 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");
316 TemplateURLRef ref(L"http://bar/foo?{google:acceptedSuggestion}"
317 L"{google:originalQueryForSuggestion}q={searchTerms}", 1, 2);
318 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]7b9f3672009-06-15 18:31:22321 GURL result = GURL(WideToUTF8(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]12bd05872009-03-17 19:25:06330 RLZTracker::InitRlz(base::DIR_EXE);
[email protected]d82443b2009-01-15 19:54:56331 std::wstring rlz_string;
332 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz_string);
333
334 TemplateURL t_url;
[email protected]12bd05872009-03-17 19:25:06335 TemplateURLRef ref(L"http://bar/?{google:RLZ}{searchTerms}", 1, 2);
[email protected]d82443b2009-01-15 19:54:56336 ASSERT_TRUE(ref.IsValid());
337 ASSERT_TRUE(ref.SupportsReplacement());
[email protected]7b9f3672009-06-15 18:31:22338 GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(t_url, L"x",
339 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())));
[email protected]d82443b2009-01-15 19:54:56340 ASSERT_TRUE(result.is_valid());
[email protected]12bd05872009-03-17 19:25:06341 std::string expected_url = "http://bar/?";
342 if (!rlz_string.empty()) {
343 expected_url += "rlz=" + WideToUTF8(rlz_string) + "&";
344 }
345 expected_url += "x";
346 ASSERT_EQ(expected_url, result.spec());
[email protected]d82443b2009-01-15 19:54:56347}
[email protected]81f808de2009-09-25 01:36:34348#endif
[email protected]d82443b2009-01-15 19:54:56349
350TEST_F(TemplateURLTest, HostAndSearchTermKey) {
351 struct TestData {
352 const std::wstring url;
353 const std::string host;
354 const std::string path;
355 const std::string search_term_key;
356 } data[] = {
357 { L"http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
358
359 // No query key should result in empty values.
360 { L"http://blah/{searchTerms}", "", "", ""},
361
362 // No term should result in empty values.
363 { L"http://blah/", "", "", ""},
364
365 // Multiple terms should result in empty values.
366 { L"http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
367
368 // Term in the host shouldn't match.
369 { L"http://{searchTerms}", "", "", ""},
370
371 { L"http://blah/?q={searchTerms}", "blah", "/", "q"},
372
373 // Single term with extra chars in value should match.
374 { L"http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
[email protected]d82443b2009-01-15 19:54:56375 };
376
377 TemplateURL t_url;
[email protected]f63ae312009-02-04 17:58:46378 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
[email protected]d82443b2009-01-15 19:54:56379 t_url.SetURL(data[i].url, 0, 0);
380 EXPECT_EQ(data[i].host, t_url.url()->GetHost());
381 EXPECT_EQ(data[i].path, t_url.url()->GetPath());
382 EXPECT_EQ(data[i].search_term_key, t_url.url()->GetSearchTermKey());
383 }
384}
385
386TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
387 static const struct {
388 const wchar_t* const base_url;
389 const wchar_t* const base_suggest_url;
390 } data[] = {
391 { L"http://google.com/", L"http://clients1.google.com/complete/", },
392 { L"http://www.google.com/", L"http://clients1.google.com/complete/", },
393 { L"http://www.google.co.uk/", L"http://clients1.google.co.uk/complete/", },
394 { L"http://www.google.com.by/",
395 L"http://clients1.google.com.by/complete/", },
396 { L"http://google.com/intl/xx/", L"http://clients1.google.com/complete/", },
397 };
398
[email protected]f63ae312009-02-04 17:58:46399 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i)
[email protected]d82443b2009-01-15 19:54:56400 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
401}
402
403TEST_F(TemplateURLTest, Keyword) {
404 TemplateURL t_url;
405 t_url.SetURL(L"http://www.google.com/search", 0, 0);
406 EXPECT_FALSE(t_url.autogenerate_keyword());
407 t_url.set_keyword(L"foo");
408 EXPECT_EQ(L"foo", t_url.keyword());
409 t_url.set_autogenerate_keyword(true);
410 EXPECT_TRUE(t_url.autogenerate_keyword());
411 EXPECT_EQ(L"google.com", t_url.keyword());
412 t_url.set_keyword(L"foo");
413 EXPECT_FALSE(t_url.autogenerate_keyword());
414 EXPECT_EQ(L"foo", t_url.keyword());
415}
[email protected]81c6ef62010-01-21 09:58:47416
417TEST_F(TemplateURLTest, ParseParameterKnown) {
418 std::wstring parsed_url(L"{searchTerms}");
419 TemplateURLRef url_ref(parsed_url, 0, 0);
420 TemplateURLRef::Replacements replacements;
421 EXPECT_TRUE(url_ref.ParseParameter(0, 12, &parsed_url, &replacements));
422 EXPECT_EQ(std::wstring(), parsed_url);
423 ASSERT_EQ(1U, replacements.size());
[email protected]4c66d2a2010-05-11 04:36:05424 EXPECT_EQ(static_cast<size_t>(0), replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47425 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
426}
427
428TEST_F(TemplateURLTest, ParseParameterUnknown) {
429 std::wstring parsed_url(L"{}");
430 TemplateURLRef url_ref(parsed_url, 0, 0);
431 TemplateURLRef::Replacements replacements;
432 EXPECT_FALSE(url_ref.ParseParameter(0, 1, &parsed_url, &replacements));
433 EXPECT_EQ(L"{}", parsed_url);
434 EXPECT_TRUE(replacements.empty());
435}
436
437TEST_F(TemplateURLTest, ParseURLEmpty) {
438 TemplateURLRef url_ref(L"", 0, 0);
439 TemplateURLRef::Replacements replacements;
440 bool valid = false;
441 EXPECT_EQ(std::wstring(), url_ref.ParseURL(L"", &replacements, &valid));
442 EXPECT_TRUE(replacements.empty());
443 EXPECT_TRUE(valid);
444}
445
446TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) {
447 TemplateURLRef url_ref(L"{", 0, 0);
448 TemplateURLRef::Replacements replacements;
449 bool valid = false;
450 EXPECT_EQ(std::wstring(), url_ref.ParseURL(L"{", &replacements, &valid));
451 EXPECT_TRUE(replacements.empty());
452 EXPECT_FALSE(valid);
453}
454
455TEST_F(TemplateURLTest, ParseURLNoKnownParameters) {
456 TemplateURLRef url_ref(L"{}", 0, 0);
457 TemplateURLRef::Replacements replacements;
458 bool valid = false;
459 EXPECT_EQ(L"{}", url_ref.ParseURL(L"{}", &replacements, &valid));
460 EXPECT_TRUE(replacements.empty());
461 EXPECT_TRUE(valid);
462}
463
464TEST_F(TemplateURLTest, ParseURLTwoParameters) {
465 TemplateURLRef url_ref(L"{}{{%s}}", 0, 0);
466 TemplateURLRef::Replacements replacements;
467 bool valid = false;
468 EXPECT_EQ(L"{}{}",
469 url_ref.ParseURL(L"{}{{searchTerms}}", &replacements, &valid));
470 ASSERT_EQ(1U, replacements.size());
[email protected]4c66d2a2010-05-11 04:36:05471 EXPECT_EQ(static_cast<size_t>(3), replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47472 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
473 EXPECT_TRUE(valid);
474}
475
476TEST_F(TemplateURLTest, ParseURLNestedParameter) {
477 TemplateURLRef url_ref(L"{%s", 0, 0);
478 TemplateURLRef::Replacements replacements;
479 bool valid = false;
480 EXPECT_EQ(L"{", url_ref.ParseURL(L"{{searchTerms}", &replacements, &valid));
481 ASSERT_EQ(1U, replacements.size());
[email protected]4c66d2a2010-05-11 04:36:05482 EXPECT_EQ(static_cast<size_t>(1), replacements[0].index);
[email protected]81c6ef62010-01-21 09:58:47483 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type);
484 EXPECT_TRUE(valid);
485}