Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: chrome/browser/template_url_unittest.cc

Issue 18263: Move search engines files into subdir (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 11 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // 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
5 #include "base/string_util.h"
6 #include "chrome/browser/browser_process.h"
7 #include "chrome/browser/rlz/rlz.h"
8 #include "chrome/browser/template_url.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 class TemplateURLTest : public testing::Test {
12 public:
13 virtual void TearDown() {
14 delete TemplateURLRef::google_base_url_;
15 TemplateURLRef::google_base_url_ = NULL;
16 }
17
18 void CheckSuggestBaseURL(const wchar_t* base_url,
19 const wchar_t* base_suggest_url) const {
20 delete TemplateURLRef::google_base_url_;
21 TemplateURLRef::google_base_url_ = new std::wstring(base_url);
22 EXPECT_STREQ(base_suggest_url,
23 TemplateURLRef::GoogleBaseSuggestURLValue().c_str());
24 }
25 };
26
27 TEST_F(TemplateURLTest, Defaults) {
28 TemplateURL url;
29 ASSERT_FALSE(url.show_in_default_list());
30 ASSERT_FALSE(url.safe_for_autoreplace());
31 ASSERT_EQ(0, url.prepopulate_id());
32 }
33
34 TEST_F(TemplateURLTest, TestValidWithComplete) {
35 TemplateURLRef ref(L"{searchTerms}", 0, 0);
36 ASSERT_TRUE(ref.IsValid());
37 }
38
39 TEST_F(TemplateURLTest, URLRefTestSearchTerms) {
40 TemplateURL t_url;
41 TemplateURLRef ref(L"http://foo{searchTerms}", 0, 0);
42 ASSERT_TRUE(ref.IsValid());
43
44 ASSERT_TRUE(ref.SupportsReplacement());
45 GURL result = ref.ReplaceSearchTerms(t_url, L"search",
46 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring());
47 ASSERT_TRUE(result.is_valid());
48 ASSERT_EQ("http://foosearch/", result.spec());
49 }
50
51 TEST_F(TemplateURLTest, URLRefTestCount) {
52 TemplateURL t_url;
53 TemplateURLRef ref(L"http://foo{searchTerms}{count?}", 0, 0);
54 ASSERT_TRUE(ref.IsValid());
55 ASSERT_TRUE(ref.SupportsReplacement());
56 GURL result = ref.ReplaceSearchTerms(t_url, L"X",
57 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring());
58 ASSERT_TRUE(result.is_valid());
59 ASSERT_EQ("http://foox/", result.spec());
60 }
61
62 TEST_F(TemplateURLTest, URLRefTestCount2) {
63 TemplateURL t_url;
64 TemplateURLRef ref(L"http://foo{searchTerms}{count}", 0, 0);
65 ASSERT_TRUE(ref.IsValid());
66 ASSERT_TRUE(ref.SupportsReplacement());
67 GURL result = ref.ReplaceSearchTerms(t_url, L"X",
68 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring());
69 ASSERT_TRUE(result.is_valid());
70 ASSERT_EQ("http://foox10/", result.spec());
71 }
72
73 TEST_F(TemplateURLTest, URLRefTestIndices) {
74 TemplateURL t_url;
75 TemplateURLRef ref(L"http://foo{searchTerms}x{startIndex?}y{startPage?}",
76 1, 2);
77 ASSERT_TRUE(ref.IsValid());
78 ASSERT_TRUE(ref.SupportsReplacement());
79 GURL result = ref.ReplaceSearchTerms(t_url, L"X",
80 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring());
81 ASSERT_TRUE(result.is_valid());
82 ASSERT_EQ("http://fooxxy/", result.spec());
83 }
84
85 TEST_F(TemplateURLTest, URLRefTestIndices2) {
86 TemplateURL t_url;
87 TemplateURLRef ref(L"http://foo{searchTerms}x{startIndex}y{startPage}", 1, 2);
88 ASSERT_TRUE(ref.IsValid());
89 ASSERT_TRUE(ref.SupportsReplacement());
90 GURL result = ref.ReplaceSearchTerms(t_url, L"X",
91 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring());
92 ASSERT_TRUE(result.is_valid());
93 ASSERT_EQ("http://fooxx1y2/", result.spec());
94 }
95
96 TEST_F(TemplateURLTest, URLRefTestEncoding) {
97 TemplateURL t_url;
98 TemplateURLRef ref(
99 L"http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a", 1, 2);
100 ASSERT_TRUE(ref.IsValid());
101 ASSERT_TRUE(ref.SupportsReplacement());
102 GURL result = ref.ReplaceSearchTerms(t_url, L"X",
103 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring());
104 ASSERT_TRUE(result.is_valid());
105 ASSERT_EQ("http://fooxxutf-8ya/", result.spec());
106 }
107
108 TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) {
109 TemplateURL t_url;
110 TemplateURLRef ref(
111 L"http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b", 1, 2);
112 ASSERT_TRUE(ref.IsValid());
113 ASSERT_TRUE(ref.SupportsReplacement());
114 GURL result = ref.ReplaceSearchTerms(t_url, L"X",
115 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring());
116 ASSERT_TRUE(result.is_valid());
117 ASSERT_EQ("http://fooxutf-8axyb/", result.spec());
118 }
119
120 TEST_F(TemplateURLTest, URLRefTestEncoding2) {
121 TemplateURL t_url;
122 TemplateURLRef ref(
123 L"http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", 1, 2);
124 ASSERT_TRUE(ref.IsValid());
125 ASSERT_TRUE(ref.SupportsReplacement());
126 GURL result = ref.ReplaceSearchTerms(t_url, L"X",
127 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring());
128 ASSERT_TRUE(result.is_valid());
129 ASSERT_EQ("http://fooxxutf-8yutf-8a/", result.spec());
130 }
131
132 TEST_F(TemplateURLTest, URLRefTermToWide) {
133 struct ToWideCase {
134 const char* encoded_search_term;
135 const wchar_t* expected_decoded_term;
136 } to_wide_cases[] = {
137 {"hello+world", L"hello world"},
138 // Test some big-5 input.
139 {"%a7A%A6%6e+to+you", L"\x4f60\x597d to you"},
140 // Test some UTF-8 input. We should fall back to this when the encoding
141 // doesn't look like big-5. We have a '5' in the middle, which is an invalid
142 // Big-5 trailing byte.
143 {"%e4%bd%a05%e5%a5%bd+to+you", L"\x4f60\x35\x597d to you"},
144 // Undecodable input should stay escaped.
145 {"%91%01+abcd", L"%91%01 abcd"},
146 };
147
148 TemplateURL t_url;
149
150 // Set one input encoding: big-5. This is so we can test fallback to UTF-8.
151 std::vector<std::string> encodings;
152 encodings.push_back("big-5");
153 t_url.set_input_encodings(encodings);
154
155 TemplateURLRef ref(L"http://foo?q={searchTerms}", 1, 2);
156 ASSERT_TRUE(ref.IsValid());
157 ASSERT_TRUE(ref.SupportsReplacement());
158
159 for (int i = 0; i < arraysize(to_wide_cases); i++) {
160 std::wstring result = ref.SearchTermToWide(t_url,
161 to_wide_cases[i].encoded_search_term);
162
163 EXPECT_EQ(std::wstring(to_wide_cases[i].expected_decoded_term), result);
164 }
165 }
166
167 TEST_F(TemplateURLTest, SetFavIcon) {
168 TemplateURL url;
169 GURL favicon_url("http://favicon.url");
170 url.SetFavIconURL(favicon_url);
171 ASSERT_EQ(1, url.image_refs().size());
172 ASSERT_TRUE(favicon_url == url.GetFavIconURL());
173
174 GURL favicon_url2("http://favicon2.url");
175 url.SetFavIconURL(favicon_url2);
176 ASSERT_EQ(1, url.image_refs().size());
177 ASSERT_TRUE(favicon_url2 == url.GetFavIconURL());
178 }
179
180 TEST_F(TemplateURLTest, DisplayURLToURLRef) {
181 struct TestData {
182 const std::wstring url;
183 const std::wstring expected_result;
184 } data[] = {
185 { L"http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a",
186 L"http://foo%sx{inputEncoding}y{outputEncoding}a" },
187 { L"http://X",
188 L"http://X" },
189 { L"http://foo{searchTerms",
190 L"http://foo{searchTerms" },
191 { L"http://foo{searchTerms}{language}",
192 L"http://foo%s{language}" },
193 };
194 for (int i = 0; i < arraysize(data); ++i) {
195 TemplateURLRef ref(data[i].url, 1, 2);
196 EXPECT_EQ(data[i].expected_result, ref.DisplayURL());
197 EXPECT_EQ(data[i].url,
198 TemplateURLRef::DisplayURLToURLRef(ref.DisplayURL()));
199 }
200 }
201
202 TEST_F(TemplateURLTest, ReplaceSearchTerms) {
203 struct TestData {
204 const std::wstring url;
205 const std::string expected_result;
206 } data[] = {
207 { L"http://foo/{language}{searchTerms}{inputEncoding}",
208 "http://foo/{language}XUTF-8" },
209 { L"http://foo/{language}{inputEncoding}{searchTerms}",
210 "http://foo/{language}UTF-8X" },
211 { L"http://foo/{searchTerms}{language}{inputEncoding}",
212 "http://foo/X{language}UTF-8" },
213 { L"http://foo/{searchTerms}{inputEncoding}{language}",
214 "http://foo/XUTF-8{language}" },
215 { L"http://foo/{inputEncoding}{searchTerms}{language}",
216 "http://foo/UTF-8X{language}" },
217 { L"http://foo/{inputEncoding}{language}{searchTerms}",
218 "http://foo/UTF-8{language}X" },
219 { L"http://foo/{language}a{searchTerms}a{inputEncoding}a",
220 "http://foo/{language}aXaUTF-8a" },
221 { L"http://foo/{language}a{inputEncoding}a{searchTerms}a",
222 "http://foo/{language}aUTF-8aXa" },
223 { L"http://foo/{searchTerms}a{language}a{inputEncoding}a",
224 "http://foo/Xa{language}aUTF-8a" },
225 { L"http://foo/{searchTerms}a{inputEncoding}a{language}a",
226 "http://foo/XaUTF-8a{language}a" },
227 { L"http://foo/{inputEncoding}a{searchTerms}a{language}a",
228 "http://foo/UTF-8aXa{language}a" },
229 { L"http://foo/{inputEncoding}a{language}a{searchTerms}a",
230 "http://foo/UTF-8a{language}aXa" },
231 };
232 TemplateURL turl;
233 turl.add_input_encoding("UTF-8");
234 for (int i = 0; i < arraysize(data); ++i) {
235 TemplateURLRef ref(data[i].url, 1, 2);
236 EXPECT_TRUE(ref.IsValid());
237 EXPECT_TRUE(ref.SupportsReplacement());
238 std::string expected_result = data[i].expected_result;
239 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}",
240 WideToASCII(g_browser_process->GetApplicationLocale()));
241 GURL result = ref.ReplaceSearchTerms(turl, L"X",
242 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring());
243 EXPECT_TRUE(result.is_valid());
244 EXPECT_EQ(expected_result, result.spec());
245 }
246 }
247
248
249 // Tests replacing search terms in various encodings and making sure the
250 // generated URL matches the expected value.
251 TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) {
252 struct TestData {
253 const std::string encoding;
254 const std::wstring search_term;
255 const std::wstring url;
256 const std::string expected_result;
257 } data[] = {
258 { "BIG5", L"\x60BD", L"http://foo/{searchTerms}{inputEncoding}",
259 "http://foo/%B1~BIG5" },
260 { "UTF-8", L"blah", L"http://foo/{searchTerms}{inputEncoding}",
261 "http://foo/blahUTF-8" },
262 };
263 for (int i = 0; i < arraysize(data); ++i) {
264 TemplateURL turl;
265 turl.add_input_encoding(data[i].encoding);
266 TemplateURLRef ref(data[i].url, 1, 2);
267 GURL result = ref.ReplaceSearchTerms(turl, data[i].search_term,
268 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring());
269 EXPECT_TRUE(result.is_valid());
270 EXPECT_EQ(data[i].expected_result, result.spec());
271 }
272 }
273
274 TEST_F(TemplateURLTest, Suggestions) {
275 struct TestData {
276 const int accepted_suggestion;
277 const std::wstring original_query_for_suggestion;
278 const std::string expected_result;
279 } data[] = {
280 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring(),
281 "http://bar/foo?q=foobar" },
282 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, L"foo",
283 "http://bar/foo?q=foobar" },
284 { TemplateURLRef::NO_SUGGESTION_CHOSEN, std::wstring(),
285 "http://bar/foo?aq=f&q=foobar" },
286 { TemplateURLRef::NO_SUGGESTION_CHOSEN, L"foo",
287 "http://bar/foo?aq=f&q=foobar" },
288 { 0, std::wstring(), "http://bar/foo?aq=0&oq=&q=foobar" },
289 { 1, L"foo", "http://bar/foo?aq=1&oq=foo&q=foobar" },
290 };
291 TemplateURL turl;
292 turl.add_input_encoding("UTF-8");
293 TemplateURLRef ref(L"http://bar/foo?{google:acceptedSuggestion}"
294 L"{google:originalQueryForSuggestion}q={searchTerms}", 1, 2);
295 ASSERT_TRUE(ref.IsValid());
296 ASSERT_TRUE(ref.SupportsReplacement());
297 for (int i = 0; i < arraysize(data); ++i) {
298 GURL result = ref.ReplaceSearchTerms(turl, L"foobar",
299 data[i].accepted_suggestion, data[i].original_query_for_suggestion);
300 EXPECT_TRUE(result.is_valid());
301 EXPECT_EQ(data[i].expected_result, result.spec());
302 }
303 }
304
305 TEST_F(TemplateURLTest, RLZ) {
306 std::wstring rlz_string;
307 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz_string);
308
309 TemplateURL t_url;
310 TemplateURLRef ref(L"http://bar/{google:RLZ}{searchTerms}", 1, 2);
311 ASSERT_TRUE(ref.IsValid());
312 ASSERT_TRUE(ref.SupportsReplacement());
313 GURL result = ref.ReplaceSearchTerms(t_url, L"x",
314 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring());
315 ASSERT_TRUE(result.is_valid());
316 // TODO(levin): fix this!
317 // ASSERT_EQ("http://bar/" + WideToUTF8(rlz_string) + "x", result.spec());
318 }
319
320 TEST_F(TemplateURLTest, HostAndSearchTermKey) {
321 struct TestData {
322 const std::wstring url;
323 const std::string host;
324 const std::string path;
325 const std::string search_term_key;
326 } data[] = {
327 { L"http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
328
329 // No query key should result in empty values.
330 { L"http://blah/{searchTerms}", "", "", ""},
331
332 // No term should result in empty values.
333 { L"http://blah/", "", "", ""},
334
335 // Multiple terms should result in empty values.
336 { L"http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
337
338 // Term in the host shouldn't match.
339 { L"http://{searchTerms}", "", "", ""},
340
341 { L"http://blah/?q={searchTerms}", "blah", "/", "q"},
342
343 // Single term with extra chars in value should match.
344 { L"http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
345
346 };
347
348 TemplateURL t_url;
349 for (int i = 0; i < arraysize(data); ++i) {
350 t_url.SetURL(data[i].url, 0, 0);
351 EXPECT_EQ(data[i].host, t_url.url()->GetHost());
352 EXPECT_EQ(data[i].path, t_url.url()->GetPath());
353 EXPECT_EQ(data[i].search_term_key, t_url.url()->GetSearchTermKey());
354 }
355 }
356
357 TEST_F(TemplateURLTest, GoogleBaseSuggestURL) {
358 static const struct {
359 const wchar_t* const base_url;
360 const wchar_t* const base_suggest_url;
361 } data[] = {
362 { L"http://google.com/", L"http://clients1.google.com/complete/", },
363 { L"http://www.google.com/", L"http://clients1.google.com/complete/", },
364 { L"http://www.google.co.uk/", L"http://clients1.google.co.uk/complete/", },
365 { L"http://www.google.com.by/",
366 L"http://clients1.google.com.by/complete/", },
367 { L"http://google.com/intl/xx/", L"http://clients1.google.com/complete/", },
368 };
369
370 for (int i = 0; i < arraysize(data); ++i)
371 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url);
372 }
373
374 TEST_F(TemplateURLTest, Keyword) {
375 TemplateURL t_url;
376 t_url.SetURL(L"http://www.google.com/search", 0, 0);
377 EXPECT_FALSE(t_url.autogenerate_keyword());
378 t_url.set_keyword(L"foo");
379 EXPECT_EQ(L"foo", t_url.keyword());
380 t_url.set_autogenerate_keyword(true);
381 EXPECT_TRUE(t_url.autogenerate_keyword());
382 EXPECT_EQ(L"google.com", t_url.keyword());
383 t_url.set_keyword(L"foo");
384 EXPECT_FALSE(t_url.autogenerate_keyword());
385 EXPECT_EQ(L"foo", t_url.keyword());
386 }
OLDNEW
« no previous file with comments | « chrome/browser/template_url_prepopulate_data_unittest.cc ('k') | chrome/browser/views/clear_browsing_data.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698