[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 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 | |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 5 | #include "base/base_paths.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 6 | #include "base/string_util.h" |
[email protected] | 64048bd | 2010-03-08 23:28:58 | [diff] [blame] | 7 | #include "base/utf_string_conversions.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 8 | #include "chrome/browser/browser_process.h" |
| 9 | #include "chrome/browser/rlz/rlz.h" |
[email protected] | d54e03a5 | 2009-01-16 00:31:04 | [diff] [blame] | 10 | #include "chrome/browser/search_engines/template_url.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | class 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 | |
| 29 | TEST_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 | |
| 36 | TEST_F(TemplateURLTest, TestValidWithComplete) { |
| 37 | TemplateURLRef ref(L"{searchTerms}", 0, 0); |
| 38 | ASSERT_TRUE(ref.IsValid()); |
| 39 | } |
| 40 | |
| 41 | TEST_F(TemplateURLTest, URLRefTestSearchTerms) { |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 42 | struct SearchTermsCase { |
| 43 | const wchar_t* url; |
| 44 | const wchar_t* terms; |
| 45 | const char* output; |
| 46 | } search_term_cases[] = { |
[email protected] | 3c75f0d | 2010-03-02 05:51:17 | [diff] [blame] | 47 | { 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] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 54 | }; |
| 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] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 60 | |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 61 | 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] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | TEST_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] | 7b9f367 | 2009-06-15 18:31:22 | [diff] [blame] | 74 | GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(t_url, L"X", |
| 75 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 76 | ASSERT_TRUE(result.is_valid()); |
| 77 | ASSERT_EQ("http://foox/", result.spec()); |
| 78 | } |
| 79 | |
| 80 | TEST_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] | 7b9f367 | 2009-06-15 18:31:22 | [diff] [blame] | 85 | GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(t_url, L"X", |
| 86 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 87 | ASSERT_TRUE(result.is_valid()); |
| 88 | ASSERT_EQ("http://foox10/", result.spec()); |
| 89 | } |
| 90 | |
| 91 | TEST_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] | 7b9f367 | 2009-06-15 18:31:22 | [diff] [blame] | 97 | GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(t_url, L"X", |
| 98 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 99 | ASSERT_TRUE(result.is_valid()); |
| 100 | ASSERT_EQ("http://fooxxy/", result.spec()); |
| 101 | } |
| 102 | |
| 103 | TEST_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] | 7b9f367 | 2009-06-15 18:31:22 | [diff] [blame] | 108 | GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(t_url, L"X", |
| 109 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 110 | ASSERT_TRUE(result.is_valid()); |
| 111 | ASSERT_EQ("http://fooxx1y2/", result.spec()); |
| 112 | } |
| 113 | |
| 114 | TEST_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] | 7b9f367 | 2009-06-15 18:31:22 | [diff] [blame] | 120 | GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(t_url, L"X", |
| 121 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 122 | ASSERT_TRUE(result.is_valid()); |
| 123 | ASSERT_EQ("http://fooxxutf-8ya/", result.spec()); |
| 124 | } |
| 125 | |
| 126 | TEST_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] | 7b9f367 | 2009-06-15 18:31:22 | [diff] [blame] | 132 | GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(t_url, L"X", |
| 133 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 134 | ASSERT_TRUE(result.is_valid()); |
| 135 | ASSERT_EQ("http://fooxutf-8axyb/", result.spec()); |
| 136 | } |
| 137 | |
| 138 | TEST_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] | 7b9f367 | 2009-06-15 18:31:22 | [diff] [blame] | 144 | GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(t_url, L"X", |
| 145 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 146 | ASSERT_TRUE(result.is_valid()); |
| 147 | ASSERT_EQ("http://fooxxutf-8yutf-8a/", result.spec()); |
| 148 | } |
| 149 | |
| 150 | TEST_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] | 7df4348 | 2009-07-31 19:37:44 | [diff] [blame] | 164 | // 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] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 168 | }; |
| 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] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 181 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) { |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 182 | 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 | |
| 189 | TEST_F(TemplateURLTest, SetFavIcon) { |
| 190 | TemplateURL url; |
| 191 | GURL favicon_url("http://favicon.url"); |
| 192 | url.SetFavIconURL(favicon_url); |
[email protected] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 193 | ASSERT_EQ(1U, url.image_refs().size()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 194 | ASSERT_TRUE(favicon_url == url.GetFavIconURL()); |
| 195 | |
| 196 | GURL favicon_url2("http://favicon2.url"); |
| 197 | url.SetFavIconURL(favicon_url2); |
[email protected] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 198 | ASSERT_EQ(1U, url.image_refs().size()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 199 | ASSERT_TRUE(favicon_url2 == url.GetFavIconURL()); |
| 200 | } |
| 201 | |
| 202 | TEST_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] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 216 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 217 | 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 | |
| 224 | TEST_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] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 256 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 257 | 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] | d70539de | 2009-06-24 22:17:06 | [diff] [blame] | 262 | g_browser_process->GetApplicationLocale()); |
[email protected] | 7b9f367 | 2009-06-15 18:31:22 | [diff] [blame] | 263 | GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(turl, L"X", |
| 264 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 265 | 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. |
| 273 | TEST_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] | 3c75f0d | 2010-03-02 05:51:17 | [diff] [blame] | 280 | { "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] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 284 | }; |
[email protected] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 285 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 286 | TemplateURL turl; |
| 287 | turl.add_input_encoding(data[i].encoding); |
| 288 | TemplateURLRef ref(data[i].url, 1, 2); |
[email protected] | 7b9f367 | 2009-06-15 18:31:22 | [diff] [blame] | 289 | GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(turl, |
| 290 | data[i].search_term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, |
| 291 | std::wstring()))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 292 | EXPECT_TRUE(result.is_valid()); |
| 293 | EXPECT_EQ(data[i].expected_result, result.spec()); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | TEST_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] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 320 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
[email protected] | 7b9f367 | 2009-06-15 18:31:22 | [diff] [blame] | 321 | GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(turl, L"foobar", |
| 322 | data[i].accepted_suggestion, data[i].original_query_for_suggestion))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 323 | EXPECT_TRUE(result.is_valid()); |
| 324 | EXPECT_EQ(data[i].expected_result, result.spec()); |
| 325 | } |
| 326 | } |
| 327 | |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 328 | #if defined(OS_WIN) |
[email protected] | 81f808de | 2009-09-25 01:36:34 | [diff] [blame] | 329 | TEST_F(TemplateURLTest, RLZ) { |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 330 | RLZTracker::InitRlz(base::DIR_EXE); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 331 | std::wstring rlz_string; |
| 332 | RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz_string); |
| 333 | |
| 334 | TemplateURL t_url; |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 335 | TemplateURLRef ref(L"http://bar/?{google:RLZ}{searchTerms}", 1, 2); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 336 | ASSERT_TRUE(ref.IsValid()); |
| 337 | ASSERT_TRUE(ref.SupportsReplacement()); |
[email protected] | 7b9f367 | 2009-06-15 18:31:22 | [diff] [blame] | 338 | GURL result = GURL(WideToUTF8(ref.ReplaceSearchTerms(t_url, L"x", |
| 339 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 340 | ASSERT_TRUE(result.is_valid()); |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 341 | 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] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 347 | } |
[email protected] | 81f808de | 2009-09-25 01:36:34 | [diff] [blame] | 348 | #endif |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 349 | |
| 350 | TEST_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] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 375 | }; |
| 376 | |
| 377 | TemplateURL t_url; |
[email protected] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 378 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 379 | 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 | |
| 386 | TEST_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] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 399 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 400 | CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url); |
| 401 | } |
| 402 | |
| 403 | TEST_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] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 416 | |
| 417 | TEST_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] | 4c66d2a | 2010-05-11 04:36:05 | [diff] [blame^] | 424 | EXPECT_EQ(static_cast<size_t>(0), replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 425 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 426 | } |
| 427 | |
| 428 | TEST_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 | |
| 437 | TEST_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 | |
| 446 | TEST_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 | |
| 455 | TEST_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 | |
| 464 | TEST_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] | 4c66d2a | 2010-05-11 04:36:05 | [diff] [blame^] | 471 | EXPECT_EQ(static_cast<size_t>(3), replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 472 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 473 | EXPECT_TRUE(valid); |
| 474 | } |
| 475 | |
| 476 | TEST_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] | 4c66d2a | 2010-05-11 04:36:05 | [diff] [blame^] | 482 | EXPECT_EQ(static_cast<size_t>(1), replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 483 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 484 | EXPECT_TRUE(valid); |
| 485 | } |