| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/base_paths.h" | |
| 6 #include "base/command_line.h" | |
| 7 #include "base/strings/string_number_conversions.h" | |
| 8 #include "base/strings/string_util.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "components/metrics/proto/omnibox_event.pb.h" | |
| 11 #include "components/metrics/proto/omnibox_input_type.pb.h" | |
| 12 #include "components/search_engines/search_engines_switches.h" | |
| 13 #include "components/search_engines/search_terms_data.h" | |
| 14 #include "components/search_engines/template_url.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 using base::ASCIIToUTF16; | |
| 18 | |
| 19 // TestSearchTermsData -------------------------------------------------------- | |
| 20 | |
| 21 // Simple implementation of SearchTermsData. | |
| 22 class TestSearchTermsData : public SearchTermsData { | |
| 23 public: | |
| 24 explicit TestSearchTermsData(const std::string& google_base_url); | |
| 25 | |
| 26 virtual std::string GoogleBaseURLValue() const OVERRIDE; | |
| 27 virtual base::string16 GetRlzParameterValue( | |
| 28 bool from_app_list) const OVERRIDE; | |
| 29 virtual std::string GetSearchClient() const OVERRIDE; | |
| 30 virtual std::string GoogleImageSearchSource() const OVERRIDE; | |
| 31 virtual bool EnableAnswersInSuggest() const OVERRIDE; | |
| 32 virtual bool IsShowingSearchTermsOnSearchResultsPages() const OVERRIDE; | |
| 33 | |
| 34 void set_google_base_url(const std::string& google_base_url) { | |
| 35 google_base_url_ = google_base_url; | |
| 36 } | |
| 37 void set_search_client(const std::string& search_client) { | |
| 38 search_client_ = search_client; | |
| 39 } | |
| 40 void set_enable_answers_in_suggest(bool enable_answers_in_suggest) { | |
| 41 enable_answers_in_suggest_ = enable_answers_in_suggest; | |
| 42 } | |
| 43 void set_is_showing_search_terms_on_search_results_pages(bool value) { | |
| 44 is_showing_search_terms_on_search_results_pages_ = value; | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 std::string google_base_url_; | |
| 49 std::string search_client_; | |
| 50 bool enable_answers_in_suggest_; | |
| 51 bool is_showing_search_terms_on_search_results_pages_; | |
| 52 | |
| 53 DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData); | |
| 54 }; | |
| 55 | |
| 56 TestSearchTermsData::TestSearchTermsData(const std::string& google_base_url) | |
| 57 : google_base_url_(google_base_url), | |
| 58 enable_answers_in_suggest_(false), | |
| 59 is_showing_search_terms_on_search_results_pages_(false) { | |
| 60 } | |
| 61 | |
| 62 std::string TestSearchTermsData::GoogleBaseURLValue() const { | |
| 63 return google_base_url_; | |
| 64 } | |
| 65 | |
| 66 base::string16 TestSearchTermsData::GetRlzParameterValue( | |
| 67 bool from_app_list) const { | |
| 68 return ASCIIToUTF16( | |
| 69 from_app_list ? "rlz_parameter_from_app_list" : "rlz_parameter"); | |
| 70 } | |
| 71 | |
| 72 std::string TestSearchTermsData::GetSearchClient() const { | |
| 73 return search_client_; | |
| 74 } | |
| 75 | |
| 76 std::string TestSearchTermsData::GoogleImageSearchSource() const { | |
| 77 return "google_image_search_source"; | |
| 78 } | |
| 79 | |
| 80 bool TestSearchTermsData::EnableAnswersInSuggest() const { | |
| 81 return enable_answers_in_suggest_; | |
| 82 } | |
| 83 | |
| 84 bool TestSearchTermsData::IsShowingSearchTermsOnSearchResultsPages() const { | |
| 85 return is_showing_search_terms_on_search_results_pages_; | |
| 86 } | |
| 87 | |
| 88 // TemplateURLTest ------------------------------------------------------------ | |
| 89 | |
| 90 class TemplateURLTest : public testing::Test { | |
| 91 public: | |
| 92 TemplateURLTest() : search_terms_data_("http://www.google.com/") {} | |
| 93 void CheckSuggestBaseURL(const std::string& base_url, | |
| 94 const std::string& base_suggest_url) const; | |
| 95 | |
| 96 TestSearchTermsData search_terms_data_; | |
| 97 }; | |
| 98 | |
| 99 void TemplateURLTest::CheckSuggestBaseURL( | |
| 100 const std::string& base_url, | |
| 101 const std::string& base_suggest_url) const { | |
| 102 TestSearchTermsData search_terms_data(base_url); | |
| 103 EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue()); | |
| 104 } | |
| 105 | |
| 106 | |
| 107 // Actual tests --------------------------------------------------------------- | |
| 108 | |
| 109 TEST_F(TemplateURLTest, Defaults) { | |
| 110 TemplateURLData data; | |
| 111 EXPECT_FALSE(data.show_in_default_list); | |
| 112 EXPECT_FALSE(data.safe_for_autoreplace); | |
| 113 EXPECT_EQ(0, data.prepopulate_id); | |
| 114 } | |
| 115 | |
| 116 TEST_F(TemplateURLTest, TestValidWithComplete) { | |
| 117 TemplateURLData data; | |
| 118 data.SetURL("{searchTerms}"); | |
| 119 TemplateURL url(data); | |
| 120 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 121 } | |
| 122 | |
| 123 TEST_F(TemplateURLTest, URLRefTestSearchTerms) { | |
| 124 struct SearchTermsCase { | |
| 125 const char* url; | |
| 126 const base::string16 terms; | |
| 127 const std::string output; | |
| 128 } search_term_cases[] = { | |
| 129 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"), | |
| 130 "http://foosea%20rch/bar" }, | |
| 131 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"), | |
| 132 "http://foosea%20rch/bar?boo=abc" }, | |
| 133 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"), | |
| 134 "http://foo/?boo=sea+rch%2Fbar" }, | |
| 135 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"), | |
| 136 "http://en.wikipedia.org/wiki/%3F" } | |
| 137 }; | |
| 138 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { | |
| 139 const SearchTermsCase& value = search_term_cases[i]; | |
| 140 TemplateURLData data; | |
| 141 data.SetURL(value.url); | |
| 142 TemplateURL url(data); | |
| 143 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 144 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 145 GURL result(url.url_ref().ReplaceSearchTerms( | |
| 146 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data_)); | |
| 147 ASSERT_TRUE(result.is_valid()); | |
| 148 EXPECT_EQ(value.output, result.spec()); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 TEST_F(TemplateURLTest, URLRefTestCount) { | |
| 153 TemplateURLData data; | |
| 154 data.SetURL("http://foo{searchTerms}{count?}"); | |
| 155 TemplateURL url(data); | |
| 156 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 157 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 158 GURL result(url.url_ref().ReplaceSearchTerms( | |
| 159 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); | |
| 160 ASSERT_TRUE(result.is_valid()); | |
| 161 EXPECT_EQ("http://foox/", result.spec()); | |
| 162 } | |
| 163 | |
| 164 TEST_F(TemplateURLTest, URLRefTestCount2) { | |
| 165 TemplateURLData data; | |
| 166 data.SetURL("http://foo{searchTerms}{count}"); | |
| 167 TemplateURL url(data); | |
| 168 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 169 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 170 GURL result(url.url_ref().ReplaceSearchTerms( | |
| 171 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); | |
| 172 ASSERT_TRUE(result.is_valid()); | |
| 173 EXPECT_EQ("http://foox10/", result.spec()); | |
| 174 } | |
| 175 | |
| 176 TEST_F(TemplateURLTest, URLRefTestIndices) { | |
| 177 TemplateURLData data; | |
| 178 data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}"); | |
| 179 TemplateURL url(data); | |
| 180 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 181 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 182 GURL result(url.url_ref().ReplaceSearchTerms( | |
| 183 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); | |
| 184 ASSERT_TRUE(result.is_valid()); | |
| 185 EXPECT_EQ("http://fooxxy/", result.spec()); | |
| 186 } | |
| 187 | |
| 188 TEST_F(TemplateURLTest, URLRefTestIndices2) { | |
| 189 TemplateURLData data; | |
| 190 data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}"); | |
| 191 TemplateURL url(data); | |
| 192 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 193 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 194 GURL result(url.url_ref().ReplaceSearchTerms( | |
| 195 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); | |
| 196 ASSERT_TRUE(result.is_valid()); | |
| 197 EXPECT_EQ("http://fooxx1y1/", result.spec()); | |
| 198 } | |
| 199 | |
| 200 TEST_F(TemplateURLTest, URLRefTestEncoding) { | |
| 201 TemplateURLData data; | |
| 202 data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a"); | |
| 203 TemplateURL url(data); | |
| 204 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 205 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 206 GURL result(url.url_ref().ReplaceSearchTerms( | |
| 207 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); | |
| 208 ASSERT_TRUE(result.is_valid()); | |
| 209 EXPECT_EQ("http://fooxxutf-8ya/", result.spec()); | |
| 210 } | |
| 211 | |
| 212 TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) { | |
| 213 const char kInvalidPostParamsString[] = | |
| 214 "unknown_template={UnknownTemplate},bad_value=bad{value}," | |
| 215 "{google:sbiSource}"; | |
| 216 // List all accpectable parameter format in valid_post_params_string. it is | |
| 217 // expected like: "name0=,name1=value1,name2={template1}" | |
| 218 const char kValidPostParamsString[] = | |
| 219 "image_content={google:imageThumbnail},image_url={google:imageURL}," | |
| 220 "sbisrc={google:imageSearchSource},language={language},empty_param=," | |
| 221 "constant_param=constant,width={google:imageOriginalWidth}"; | |
| 222 const char KImageSearchURL[] = "http://foo.com/sbi"; | |
| 223 | |
| 224 TemplateURLData data; | |
| 225 data.image_url = KImageSearchURL; | |
| 226 | |
| 227 // Try to parse invalid post parameters. | |
| 228 data.image_url_post_params = kInvalidPostParamsString; | |
| 229 TemplateURL url_bad(data); | |
| 230 ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_)); | |
| 231 const TemplateURLRef::PostParams& bad_post_params = | |
| 232 url_bad.image_url_ref().post_params_; | |
| 233 ASSERT_EQ(2U, bad_post_params.size()); | |
| 234 EXPECT_EQ("unknown_template", bad_post_params[0].first); | |
| 235 EXPECT_EQ("{UnknownTemplate}", bad_post_params[0].second); | |
| 236 EXPECT_EQ("bad_value", bad_post_params[1].first); | |
| 237 EXPECT_EQ("bad{value}", bad_post_params[1].second); | |
| 238 | |
| 239 // Try to parse valid post parameters. | |
| 240 data.image_url_post_params = kValidPostParamsString; | |
| 241 TemplateURL url(data); | |
| 242 ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_)); | |
| 243 ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_)); | |
| 244 | |
| 245 // Check term replacement. | |
| 246 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X")); | |
| 247 search_args.image_thumbnail_content = "dummy-image-thumbnail"; | |
| 248 search_args.image_url = GURL("http://dummyimage.com/dummy.jpg"); | |
| 249 search_args.image_original_size = gfx::Size(10, 10); | |
| 250 // Replacement operation with no post_data buffer should still return | |
| 251 // the parsed URL. | |
| 252 TestSearchTermsData search_terms_data("http://X"); | |
| 253 GURL result(url.image_url_ref().ReplaceSearchTerms( | |
| 254 search_args, search_terms_data)); | |
| 255 ASSERT_TRUE(result.is_valid()); | |
| 256 EXPECT_EQ(KImageSearchURL, result.spec()); | |
| 257 TemplateURLRef::PostContent post_content; | |
| 258 result = GURL(url.image_url_ref().ReplaceSearchTerms( | |
| 259 search_args, search_terms_data, &post_content)); | |
| 260 ASSERT_TRUE(result.is_valid()); | |
| 261 EXPECT_EQ(KImageSearchURL, result.spec()); | |
| 262 ASSERT_FALSE(post_content.first.empty()); | |
| 263 ASSERT_FALSE(post_content.second.empty()); | |
| 264 | |
| 265 // Check parsed result of post parameters. | |
| 266 const TemplateURLRef::Replacements& replacements = | |
| 267 url.image_url_ref().replacements_; | |
| 268 const TemplateURLRef::PostParams& post_params = | |
| 269 url.image_url_ref().post_params_; | |
| 270 EXPECT_EQ(7U, post_params.size()); | |
| 271 for (TemplateURLRef::PostParams::const_iterator i = post_params.begin(); | |
| 272 i != post_params.end(); ++i) { | |
| 273 TemplateURLRef::Replacements::const_iterator j = replacements.begin(); | |
| 274 for (; j != replacements.end(); ++j) { | |
| 275 if (j->is_post_param && j->index == | |
| 276 static_cast<size_t>(i - post_params.begin())) { | |
| 277 switch (j->type) { | |
| 278 case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH: | |
| 279 EXPECT_EQ("width", i->first); | |
| 280 EXPECT_EQ( | |
| 281 base::IntToString(search_args.image_original_size.width()), | |
| 282 i->second); | |
| 283 break; | |
| 284 case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE: | |
| 285 EXPECT_EQ("sbisrc", i->first); | |
| 286 EXPECT_EQ(search_terms_data.GoogleImageSearchSource(), i->second); | |
| 287 break; | |
| 288 case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL: | |
| 289 EXPECT_EQ("image_content", i->first); | |
| 290 EXPECT_EQ(search_args.image_thumbnail_content, i->second); | |
| 291 break; | |
| 292 case TemplateURLRef::GOOGLE_IMAGE_URL: | |
| 293 EXPECT_EQ("image_url", i->first); | |
| 294 EXPECT_EQ(search_args.image_url.spec(), i->second); | |
| 295 break; | |
| 296 case TemplateURLRef::LANGUAGE: | |
| 297 EXPECT_EQ("language", i->first); | |
| 298 EXPECT_EQ("en", i->second); | |
| 299 break; | |
| 300 default: | |
| 301 ADD_FAILURE(); // Should never go here. | |
| 302 } | |
| 303 break; | |
| 304 } | |
| 305 } | |
| 306 if (j != replacements.end()) | |
| 307 continue; | |
| 308 if (i->first == "empty_param") { | |
| 309 EXPECT_TRUE(i->second.empty()); | |
| 310 } else if (i->first == "sbisrc") { | |
| 311 EXPECT_FALSE(i->second.empty()); | |
| 312 } else { | |
| 313 EXPECT_EQ("constant_param", i->first); | |
| 314 EXPECT_EQ("constant", i->second); | |
| 315 } | |
| 316 } | |
| 317 } | |
| 318 | |
| 319 // Test that setting the prepopulate ID from TemplateURL causes the stored | |
| 320 // TemplateURLRef to handle parsing the URL parameters differently. | |
| 321 TEST_F(TemplateURLTest, SetPrepopulatedAndParse) { | |
| 322 TemplateURLData data; | |
| 323 data.SetURL("http://foo{fhqwhgads}bar"); | |
| 324 TemplateURL url(data); | |
| 325 TemplateURLRef::Replacements replacements; | |
| 326 bool valid = false; | |
| 327 EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL( | |
| 328 "http://foo{fhqwhgads}bar", &replacements, NULL, &valid)); | |
| 329 EXPECT_TRUE(replacements.empty()); | |
| 330 EXPECT_TRUE(valid); | |
| 331 | |
| 332 data.prepopulate_id = 123; | |
| 333 TemplateURL url2(data); | |
| 334 EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar", | |
| 335 &replacements, NULL, | |
| 336 &valid)); | |
| 337 EXPECT_TRUE(replacements.empty()); | |
| 338 EXPECT_TRUE(valid); | |
| 339 } | |
| 340 | |
| 341 TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) { | |
| 342 TemplateURLData data; | |
| 343 data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b"); | |
| 344 TemplateURL url(data); | |
| 345 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 346 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 347 GURL result(url.url_ref().ReplaceSearchTerms( | |
| 348 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); | |
| 349 ASSERT_TRUE(result.is_valid()); | |
| 350 EXPECT_EQ("http://fooxutf-8axyb/", result.spec()); | |
| 351 } | |
| 352 | |
| 353 TEST_F(TemplateURLTest, URLRefTestEncoding2) { | |
| 354 TemplateURLData data; | |
| 355 data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a"); | |
| 356 TemplateURL url(data); | |
| 357 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 358 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 359 GURL result(url.url_ref().ReplaceSearchTerms( | |
| 360 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); | |
| 361 ASSERT_TRUE(result.is_valid()); | |
| 362 EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec()); | |
| 363 } | |
| 364 | |
| 365 TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) { | |
| 366 struct SearchTermsCase { | |
| 367 const char* url; | |
| 368 const base::string16 terms; | |
| 369 const char* output; | |
| 370 } search_term_cases[] = { | |
| 371 { "{google:baseURL}{language}{searchTerms}", base::string16(), | |
| 372 "http://example.com/e/en" }, | |
| 373 { "{google:baseSuggestURL}{searchTerms}", base::string16(), | |
| 374 "http://example.com/complete/" } | |
| 375 }; | |
| 376 | |
| 377 TestSearchTermsData search_terms_data("http://example.com/e/"); | |
| 378 TemplateURLData data; | |
| 379 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { | |
| 380 const SearchTermsCase& value = search_term_cases[i]; | |
| 381 data.SetURL(value.url); | |
| 382 TemplateURL url(data); | |
| 383 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data)); | |
| 384 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data)); | |
| 385 GURL result(url.url_ref().ReplaceSearchTerms( | |
| 386 TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL)); | |
| 387 ASSERT_TRUE(result.is_valid()); | |
| 388 EXPECT_EQ(value.output, result.spec()); | |
| 389 } | |
| 390 } | |
| 391 | |
| 392 TEST_F(TemplateURLTest, URLRefTermToWide) { | |
| 393 struct ToWideCase { | |
| 394 const char* encoded_search_term; | |
| 395 const base::string16 expected_decoded_term; | |
| 396 } to_wide_cases[] = { | |
| 397 {"hello+world", ASCIIToUTF16("hello world")}, | |
| 398 // Test some big-5 input. | |
| 399 {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")}, | |
| 400 // Test some UTF-8 input. We should fall back to this when the encoding | |
| 401 // doesn't look like big-5. We have a '5' in the middle, which is an invalid | |
| 402 // Big-5 trailing byte. | |
| 403 {"%e4%bd%a05%e5%a5%bd+to+you", | |
| 404 base::WideToUTF16(L"\x4f60\x35\x597d to you")}, | |
| 405 // Undecodable input should stay escaped. | |
| 406 {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")}, | |
| 407 // Make sure we convert %2B to +. | |
| 408 {"C%2B%2B", ASCIIToUTF16("C++")}, | |
| 409 // C%2B is escaped as C%252B, make sure we unescape it properly. | |
| 410 {"C%252B", ASCIIToUTF16("C%2B")}, | |
| 411 }; | |
| 412 | |
| 413 // Set one input encoding: big-5. This is so we can test fallback to UTF-8. | |
| 414 TemplateURLData data; | |
| 415 data.SetURL("http://foo?q={searchTerms}"); | |
| 416 data.input_encodings.push_back("big-5"); | |
| 417 TemplateURL url(data); | |
| 418 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 419 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 420 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) { | |
| 421 EXPECT_EQ(to_wide_cases[i].expected_decoded_term, | |
| 422 url.url_ref().SearchTermToString16( | |
| 423 to_wide_cases[i].encoded_search_term)); | |
| 424 } | |
| 425 } | |
| 426 | |
| 427 TEST_F(TemplateURLTest, DisplayURLToURLRef) { | |
| 428 struct TestData { | |
| 429 const std::string url; | |
| 430 const base::string16 expected_result; | |
| 431 } test_data[] = { | |
| 432 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", | |
| 433 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") }, | |
| 434 { "http://X", | |
| 435 ASCIIToUTF16("http://X") }, | |
| 436 { "http://foo{searchTerms", | |
| 437 ASCIIToUTF16("http://foo{searchTerms") }, | |
| 438 { "http://foo{searchTerms}{language}", | |
| 439 ASCIIToUTF16("http://foo%s{language}") }, | |
| 440 }; | |
| 441 TemplateURLData data; | |
| 442 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | |
| 443 data.SetURL(test_data[i].url); | |
| 444 TemplateURL url(data); | |
| 445 EXPECT_EQ(test_data[i].expected_result, | |
| 446 url.url_ref().DisplayURL(search_terms_data_)); | |
| 447 EXPECT_EQ(test_data[i].url, | |
| 448 TemplateURLRef::DisplayURLToURLRef( | |
| 449 url.url_ref().DisplayURL(search_terms_data_))); | |
| 450 } | |
| 451 } | |
| 452 | |
| 453 TEST_F(TemplateURLTest, ReplaceSearchTerms) { | |
| 454 struct TestData { | |
| 455 const std::string url; | |
| 456 const std::string expected_result; | |
| 457 } test_data[] = { | |
| 458 { "http://foo/{language}{searchTerms}{inputEncoding}", | |
| 459 "http://foo/{language}XUTF-8" }, | |
| 460 { "http://foo/{language}{inputEncoding}{searchTerms}", | |
| 461 "http://foo/{language}UTF-8X" }, | |
| 462 { "http://foo/{searchTerms}{language}{inputEncoding}", | |
| 463 "http://foo/X{language}UTF-8" }, | |
| 464 { "http://foo/{searchTerms}{inputEncoding}{language}", | |
| 465 "http://foo/XUTF-8{language}" }, | |
| 466 { "http://foo/{inputEncoding}{searchTerms}{language}", | |
| 467 "http://foo/UTF-8X{language}" }, | |
| 468 { "http://foo/{inputEncoding}{language}{searchTerms}", | |
| 469 "http://foo/UTF-8{language}X" }, | |
| 470 { "http://foo/{language}a{searchTerms}a{inputEncoding}a", | |
| 471 "http://foo/{language}aXaUTF-8a" }, | |
| 472 { "http://foo/{language}a{inputEncoding}a{searchTerms}a", | |
| 473 "http://foo/{language}aUTF-8aXa" }, | |
| 474 { "http://foo/{searchTerms}a{language}a{inputEncoding}a", | |
| 475 "http://foo/Xa{language}aUTF-8a" }, | |
| 476 { "http://foo/{searchTerms}a{inputEncoding}a{language}a", | |
| 477 "http://foo/XaUTF-8a{language}a" }, | |
| 478 { "http://foo/{inputEncoding}a{searchTerms}a{language}a", | |
| 479 "http://foo/UTF-8aXa{language}a" }, | |
| 480 { "http://foo/{inputEncoding}a{language}a{searchTerms}a", | |
| 481 "http://foo/UTF-8a{language}aXa" }, | |
| 482 }; | |
| 483 TemplateURLData data; | |
| 484 data.input_encodings.push_back("UTF-8"); | |
| 485 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | |
| 486 data.SetURL(test_data[i].url); | |
| 487 TemplateURL url(data); | |
| 488 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 489 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 490 std::string expected_result = test_data[i].expected_result; | |
| 491 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}", | |
| 492 search_terms_data_.GetApplicationLocale()); | |
| 493 GURL result(url.url_ref().ReplaceSearchTerms( | |
| 494 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), | |
| 495 search_terms_data_)); | |
| 496 ASSERT_TRUE(result.is_valid()); | |
| 497 EXPECT_EQ(expected_result, result.spec()); | |
| 498 } | |
| 499 } | |
| 500 | |
| 501 | |
| 502 // Tests replacing search terms in various encodings and making sure the | |
| 503 // generated URL matches the expected value. | |
| 504 TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) { | |
| 505 struct TestData { | |
| 506 const std::string encoding; | |
| 507 const base::string16 search_term; | |
| 508 const std::string url; | |
| 509 const std::string expected_result; | |
| 510 } test_data[] = { | |
| 511 { "BIG5", base::WideToUTF16(L"\x60BD"), | |
| 512 "http://foo/?{searchTerms}{inputEncoding}", | |
| 513 "http://foo/?%B1~BIG5" }, | |
| 514 { "UTF-8", ASCIIToUTF16("blah"), | |
| 515 "http://foo/?{searchTerms}{inputEncoding}", | |
| 516 "http://foo/?blahUTF-8" }, | |
| 517 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"), | |
| 518 "http://foo/{searchTerms}/bar", | |
| 519 "http://foo/%82%A0/bar"}, | |
| 520 { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"), | |
| 521 "http://foo/{searchTerms}/bar", | |
| 522 "http://foo/%82%A0%20%82%A2/bar"}, | |
| 523 }; | |
| 524 TemplateURLData data; | |
| 525 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | |
| 526 data.SetURL(test_data[i].url); | |
| 527 data.input_encodings.clear(); | |
| 528 data.input_encodings.push_back(test_data[i].encoding); | |
| 529 TemplateURL url(data); | |
| 530 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 531 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 532 GURL result(url.url_ref().ReplaceSearchTerms( | |
| 533 TemplateURLRef::SearchTermsArgs(test_data[i].search_term), | |
| 534 search_terms_data_)); | |
| 535 ASSERT_TRUE(result.is_valid()); | |
| 536 EXPECT_EQ(test_data[i].expected_result, result.spec()); | |
| 537 } | |
| 538 } | |
| 539 | |
| 540 // Tests replacing assisted query stats (AQS) in various scenarios. | |
| 541 TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) { | |
| 542 struct TestData { | |
| 543 const base::string16 search_term; | |
| 544 const std::string aqs; | |
| 545 const std::string base_url; | |
| 546 const std::string url; | |
| 547 const std::string expected_result; | |
| 548 } test_data[] = { | |
| 549 // No HTTPS, no AQS. | |
| 550 { ASCIIToUTF16("foo"), | |
| 551 "chrome.0.0l6", | |
| 552 "http://foo/", | |
| 553 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", | |
| 554 "http://foo/?foo" }, | |
| 555 // HTTPS available, AQS should be replaced. | |
| 556 { ASCIIToUTF16("foo"), | |
| 557 "chrome.0.0l6", | |
| 558 "https://foo/", | |
| 559 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", | |
| 560 "https://foo/?fooaqs=chrome.0.0l6&" }, | |
| 561 // HTTPS available, however AQS is empty. | |
| 562 { ASCIIToUTF16("foo"), | |
| 563 "", | |
| 564 "https://foo/", | |
| 565 "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", | |
| 566 "https://foo/?foo" }, | |
| 567 // No {google:baseURL} and protocol is HTTP, we must not substitute AQS. | |
| 568 { ASCIIToUTF16("foo"), | |
| 569 "chrome.0.0l6", | |
| 570 "http://www.google.com", | |
| 571 "http://foo?{searchTerms}{google:assistedQueryStats}", | |
| 572 "http://foo/?foo" }, | |
| 573 // A non-Google search provider with HTTPS should allow AQS. | |
| 574 { ASCIIToUTF16("foo"), | |
| 575 "chrome.0.0l6", | |
| 576 "https://www.google.com", | |
| 577 "https://foo?{searchTerms}{google:assistedQueryStats}", | |
| 578 "https://foo/?fooaqs=chrome.0.0l6&" }, | |
| 579 }; | |
| 580 TemplateURLData data; | |
| 581 data.input_encodings.push_back("UTF-8"); | |
| 582 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | |
| 583 data.SetURL(test_data[i].url); | |
| 584 TemplateURL url(data); | |
| 585 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 586 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 587 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); | |
| 588 search_terms_args.assisted_query_stats = test_data[i].aqs; | |
| 589 search_terms_data_.set_google_base_url(test_data[i].base_url); | |
| 590 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 591 search_terms_data_)); | |
| 592 ASSERT_TRUE(result.is_valid()); | |
| 593 EXPECT_EQ(test_data[i].expected_result, result.spec()); | |
| 594 } | |
| 595 } | |
| 596 | |
| 597 // Tests replacing cursor position. | |
| 598 TEST_F(TemplateURLTest, ReplaceCursorPosition) { | |
| 599 struct TestData { | |
| 600 const base::string16 search_term; | |
| 601 size_t cursor_position; | |
| 602 const std::string url; | |
| 603 const std::string expected_result; | |
| 604 } test_data[] = { | |
| 605 { ASCIIToUTF16("foo"), | |
| 606 base::string16::npos, | |
| 607 "{google:baseURL}?{searchTerms}&{google:cursorPosition}", | |
| 608 "http://www.google.com/?foo&" }, | |
| 609 { ASCIIToUTF16("foo"), | |
| 610 2, | |
| 611 "{google:baseURL}?{searchTerms}&{google:cursorPosition}", | |
| 612 "http://www.google.com/?foo&cp=2&" }, | |
| 613 { ASCIIToUTF16("foo"), | |
| 614 15, | |
| 615 "{google:baseURL}?{searchTerms}&{google:cursorPosition}", | |
| 616 "http://www.google.com/?foo&cp=15&" }, | |
| 617 }; | |
| 618 TemplateURLData data; | |
| 619 data.input_encodings.push_back("UTF-8"); | |
| 620 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | |
| 621 data.SetURL(test_data[i].url); | |
| 622 TemplateURL url(data); | |
| 623 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 624 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 625 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); | |
| 626 search_terms_args.cursor_position = test_data[i].cursor_position; | |
| 627 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 628 search_terms_data_)); | |
| 629 ASSERT_TRUE(result.is_valid()); | |
| 630 EXPECT_EQ(test_data[i].expected_result, result.spec()); | |
| 631 } | |
| 632 } | |
| 633 | |
| 634 // Tests replacing input type (&oit=). | |
| 635 TEST_F(TemplateURLTest, ReplaceInputType) { | |
| 636 struct TestData { | |
| 637 const base::string16 search_term; | |
| 638 metrics::OmniboxInputType::Type input_type; | |
| 639 const std::string url; | |
| 640 const std::string expected_result; | |
| 641 } test_data[] = { | |
| 642 { ASCIIToUTF16("foo"), | |
| 643 metrics::OmniboxInputType::UNKNOWN, | |
| 644 "{google:baseURL}?{searchTerms}&{google:inputType}", | |
| 645 "http://www.google.com/?foo&oit=1&" }, | |
| 646 { ASCIIToUTF16("foo"), | |
| 647 metrics::OmniboxInputType::URL, | |
| 648 "{google:baseURL}?{searchTerms}&{google:inputType}", | |
| 649 "http://www.google.com/?foo&oit=3&" }, | |
| 650 { ASCIIToUTF16("foo"), | |
| 651 metrics::OmniboxInputType::FORCED_QUERY, | |
| 652 "{google:baseURL}?{searchTerms}&{google:inputType}", | |
| 653 "http://www.google.com/?foo&oit=5&" }, | |
| 654 }; | |
| 655 TemplateURLData data; | |
| 656 data.input_encodings.push_back("UTF-8"); | |
| 657 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | |
| 658 data.SetURL(test_data[i].url); | |
| 659 TemplateURL url(data); | |
| 660 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 661 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 662 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); | |
| 663 search_terms_args.input_type = test_data[i].input_type; | |
| 664 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 665 search_terms_data_)); | |
| 666 ASSERT_TRUE(result.is_valid()); | |
| 667 EXPECT_EQ(test_data[i].expected_result, result.spec()); | |
| 668 } | |
| 669 } | |
| 670 | |
| 671 // Tests replacing currentPageUrl. | |
| 672 TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) { | |
| 673 struct TestData { | |
| 674 const base::string16 search_term; | |
| 675 const std::string current_page_url; | |
| 676 const std::string url; | |
| 677 const std::string expected_result; | |
| 678 } test_data[] = { | |
| 679 { ASCIIToUTF16("foo"), | |
| 680 "http://www.google.com/", | |
| 681 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}", | |
| 682 "http://www.google.com/?foo&url=http%3A%2F%2Fwww.google.com%2F&" }, | |
| 683 { ASCIIToUTF16("foo"), | |
| 684 "", | |
| 685 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}", | |
| 686 "http://www.google.com/?foo&" }, | |
| 687 { ASCIIToUTF16("foo"), | |
| 688 "http://g.com/+-/*&=", | |
| 689 "{google:baseURL}?{searchTerms}&{google:currentPageUrl}", | |
| 690 "http://www.google.com/?foo&url=http%3A%2F%2Fg.com%2F%2B-%2F*%26%3D&" }, | |
| 691 }; | |
| 692 TemplateURLData data; | |
| 693 data.input_encodings.push_back("UTF-8"); | |
| 694 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | |
| 695 data.SetURL(test_data[i].url); | |
| 696 TemplateURL url(data); | |
| 697 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 698 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 699 TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); | |
| 700 search_terms_args.current_page_url = test_data[i].current_page_url; | |
| 701 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 702 search_terms_data_)); | |
| 703 ASSERT_TRUE(result.is_valid()); | |
| 704 EXPECT_EQ(test_data[i].expected_result, result.spec()); | |
| 705 } | |
| 706 } | |
| 707 | |
| 708 TEST_F(TemplateURLTest, Suggestions) { | |
| 709 struct TestData { | |
| 710 const int accepted_suggestion; | |
| 711 const base::string16 original_query_for_suggestion; | |
| 712 const std::string expected_result; | |
| 713 } test_data[] = { | |
| 714 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(), | |
| 715 "http://bar/foo?q=foobar" }, | |
| 716 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"), | |
| 717 "http://bar/foo?q=foobar" }, | |
| 718 { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(), | |
| 719 "http://bar/foo?q=foobar" }, | |
| 720 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"), | |
| 721 "http://bar/foo?q=foobar" }, | |
| 722 { 0, base::string16(), "http://bar/foo?oq=&q=foobar" }, | |
| 723 { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" }, | |
| 724 }; | |
| 725 TemplateURLData data; | |
| 726 data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}" | |
| 727 "q={searchTerms}"); | |
| 728 data.input_encodings.push_back("UTF-8"); | |
| 729 TemplateURL url(data); | |
| 730 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 731 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 732 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | |
| 733 TemplateURLRef::SearchTermsArgs search_terms_args( | |
| 734 ASCIIToUTF16("foobar")); | |
| 735 search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion; | |
| 736 search_terms_args.original_query = | |
| 737 test_data[i].original_query_for_suggestion; | |
| 738 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 739 search_terms_data_)); | |
| 740 ASSERT_TRUE(result.is_valid()); | |
| 741 EXPECT_EQ(test_data[i].expected_result, result.spec()); | |
| 742 } | |
| 743 } | |
| 744 | |
| 745 TEST_F(TemplateURLTest, RLZ) { | |
| 746 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(false); | |
| 747 | |
| 748 TemplateURLData data; | |
| 749 data.SetURL("http://bar/?{google:RLZ}{searchTerms}"); | |
| 750 TemplateURL url(data); | |
| 751 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 752 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 753 GURL result(url.url_ref().ReplaceSearchTerms( | |
| 754 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_)); | |
| 755 ASSERT_TRUE(result.is_valid()); | |
| 756 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x", | |
| 757 result.spec()); | |
| 758 } | |
| 759 | |
| 760 TEST_F(TemplateURLTest, RLZFromAppList) { | |
| 761 base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(true); | |
| 762 | |
| 763 TemplateURLData data; | |
| 764 data.SetURL("http://bar/?{google:RLZ}{searchTerms}"); | |
| 765 TemplateURL url(data); | |
| 766 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 767 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 768 TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x")); | |
| 769 args.from_app_list = true; | |
| 770 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_)); | |
| 771 ASSERT_TRUE(result.is_valid()); | |
| 772 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x", | |
| 773 result.spec()); | |
| 774 } | |
| 775 | |
| 776 TEST_F(TemplateURLTest, HostAndSearchTermKey) { | |
| 777 struct TestData { | |
| 778 const std::string url; | |
| 779 const std::string host; | |
| 780 const std::string path; | |
| 781 const std::string search_term_key; | |
| 782 } test_data[] = { | |
| 783 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"}, | |
| 784 | |
| 785 // No query key should result in empty values. | |
| 786 { "http://blah/{searchTerms}", "", "", ""}, | |
| 787 | |
| 788 // No term should result in empty values. | |
| 789 { "http://blah/", "", "", ""}, | |
| 790 | |
| 791 // Multiple terms should result in empty values. | |
| 792 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""}, | |
| 793 | |
| 794 // Term in the host shouldn't match. | |
| 795 { "http://{searchTerms}", "", "", ""}, | |
| 796 | |
| 797 { "http://blah/?q={searchTerms}", "blah", "/", "q"}, | |
| 798 { "https://blah/?q={searchTerms}", "blah", "/", "q"}, | |
| 799 | |
| 800 // Single term with extra chars in value should match. | |
| 801 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"}, | |
| 802 }; | |
| 803 | |
| 804 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | |
| 805 TemplateURLData data; | |
| 806 data.SetURL(test_data[i].url); | |
| 807 TemplateURL url(data); | |
| 808 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_)); | |
| 809 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_)); | |
| 810 EXPECT_EQ(test_data[i].search_term_key, | |
| 811 url.url_ref().GetSearchTermKey(search_terms_data_)); | |
| 812 } | |
| 813 } | |
| 814 | |
| 815 TEST_F(TemplateURLTest, GoogleBaseSuggestURL) { | |
| 816 static const struct { | |
| 817 const char* const base_url; | |
| 818 const char* const base_suggest_url; | |
| 819 } data[] = { | |
| 820 { "http://google.com/", "http://google.com/complete/", }, | |
| 821 { "http://www.google.com/", "http://www.google.com/complete/", }, | |
| 822 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", }, | |
| 823 { "http://www.google.com.by/", "http://www.google.com.by/complete/", }, | |
| 824 { "http://google.com/intl/xx/", "http://google.com/complete/", }, | |
| 825 }; | |
| 826 | |
| 827 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) | |
| 828 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url); | |
| 829 } | |
| 830 | |
| 831 TEST_F(TemplateURLTest, ParseParameterKnown) { | |
| 832 std::string parsed_url("{searchTerms}"); | |
| 833 TemplateURLData data; | |
| 834 data.SetURL(parsed_url); | |
| 835 TemplateURL url(data); | |
| 836 TemplateURLRef::Replacements replacements; | |
| 837 EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements)); | |
| 838 EXPECT_EQ(std::string(), parsed_url); | |
| 839 ASSERT_EQ(1U, replacements.size()); | |
| 840 EXPECT_EQ(0U, replacements[0].index); | |
| 841 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); | |
| 842 } | |
| 843 | |
| 844 TEST_F(TemplateURLTest, ParseParameterUnknown) { | |
| 845 std::string parsed_url("{fhqwhgads}abc"); | |
| 846 TemplateURLData data; | |
| 847 data.SetURL(parsed_url); | |
| 848 TemplateURL url(data); | |
| 849 TemplateURLRef::Replacements replacements; | |
| 850 | |
| 851 // By default, TemplateURLRef should not consider itself prepopulated. | |
| 852 // Therefore we should not replace the unknown parameter. | |
| 853 EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); | |
| 854 EXPECT_EQ("{fhqwhgads}abc", parsed_url); | |
| 855 EXPECT_TRUE(replacements.empty()); | |
| 856 | |
| 857 // If the TemplateURLRef is prepopulated, we should remove unknown parameters. | |
| 858 parsed_url = "{fhqwhgads}abc"; | |
| 859 data.prepopulate_id = 1; | |
| 860 TemplateURL url2(data); | |
| 861 EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); | |
| 862 EXPECT_EQ("abc", parsed_url); | |
| 863 EXPECT_TRUE(replacements.empty()); | |
| 864 } | |
| 865 | |
| 866 TEST_F(TemplateURLTest, ParseURLEmpty) { | |
| 867 TemplateURL url((TemplateURLData())); | |
| 868 TemplateURLRef::Replacements replacements; | |
| 869 bool valid = false; | |
| 870 EXPECT_EQ(std::string(), | |
| 871 url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid)); | |
| 872 EXPECT_TRUE(replacements.empty()); | |
| 873 EXPECT_TRUE(valid); | |
| 874 } | |
| 875 | |
| 876 TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) { | |
| 877 TemplateURLData data; | |
| 878 data.SetURL("{"); | |
| 879 TemplateURL url(data); | |
| 880 TemplateURLRef::Replacements replacements; | |
| 881 bool valid = false; | |
| 882 EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL, | |
| 883 &valid)); | |
| 884 EXPECT_TRUE(replacements.empty()); | |
| 885 EXPECT_FALSE(valid); | |
| 886 } | |
| 887 | |
| 888 TEST_F(TemplateURLTest, ParseURLNoKnownParameters) { | |
| 889 TemplateURLData data; | |
| 890 data.SetURL("{}"); | |
| 891 TemplateURL url(data); | |
| 892 TemplateURLRef::Replacements replacements; | |
| 893 bool valid = false; | |
| 894 EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid)); | |
| 895 EXPECT_TRUE(replacements.empty()); | |
| 896 EXPECT_TRUE(valid); | |
| 897 } | |
| 898 | |
| 899 TEST_F(TemplateURLTest, ParseURLTwoParameters) { | |
| 900 TemplateURLData data; | |
| 901 data.SetURL("{}{{%s}}"); | |
| 902 TemplateURL url(data); | |
| 903 TemplateURLRef::Replacements replacements; | |
| 904 bool valid = false; | |
| 905 EXPECT_EQ("{}{}", | |
| 906 url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL, | |
| 907 &valid)); | |
| 908 ASSERT_EQ(1U, replacements.size()); | |
| 909 EXPECT_EQ(3U, replacements[0].index); | |
| 910 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); | |
| 911 EXPECT_TRUE(valid); | |
| 912 } | |
| 913 | |
| 914 TEST_F(TemplateURLTest, ParseURLNestedParameter) { | |
| 915 TemplateURLData data; | |
| 916 data.SetURL("{%s"); | |
| 917 TemplateURL url(data); | |
| 918 TemplateURLRef::Replacements replacements; | |
| 919 bool valid = false; | |
| 920 EXPECT_EQ("{", | |
| 921 url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL, | |
| 922 &valid)); | |
| 923 ASSERT_EQ(1U, replacements.size()); | |
| 924 EXPECT_EQ(1U, replacements[0].index); | |
| 925 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); | |
| 926 EXPECT_TRUE(valid); | |
| 927 } | |
| 928 | |
| 929 TEST_F(TemplateURLTest, SearchClient) { | |
| 930 const std::string base_url_str("http://google.com/?"); | |
| 931 const std::string terms_str("{searchTerms}&{google:searchClient}"); | |
| 932 const std::string full_url_str = base_url_str + terms_str; | |
| 933 const base::string16 terms(ASCIIToUTF16(terms_str)); | |
| 934 search_terms_data_.set_google_base_url(base_url_str); | |
| 935 | |
| 936 TemplateURLData data; | |
| 937 data.SetURL(full_url_str); | |
| 938 TemplateURL url(data); | |
| 939 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 940 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 941 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar")); | |
| 942 | |
| 943 // Check that the URL is correct when a client is not present. | |
| 944 GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 945 search_terms_data_)); | |
| 946 ASSERT_TRUE(result.is_valid()); | |
| 947 EXPECT_EQ("http://google.com/?foobar&", result.spec()); | |
| 948 | |
| 949 // Check that the URL is correct when a client is present. | |
| 950 search_terms_data_.set_search_client("search_client"); | |
| 951 GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 952 search_terms_data_)); | |
| 953 ASSERT_TRUE(result_2.is_valid()); | |
| 954 EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec()); | |
| 955 } | |
| 956 | |
| 957 TEST_F(TemplateURLTest, GetURLNoInstantURL) { | |
| 958 TemplateURLData data; | |
| 959 data.SetURL("http://google.com/?q={searchTerms}"); | |
| 960 data.suggestions_url = "http://google.com/suggest?q={searchTerms}"; | |
| 961 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}"); | |
| 962 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}"); | |
| 963 TemplateURL url(data); | |
| 964 ASSERT_EQ(3U, url.URLCount()); | |
| 965 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0)); | |
| 966 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1)); | |
| 967 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2)); | |
| 968 } | |
| 969 | |
| 970 TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) { | |
| 971 TemplateURLData data; | |
| 972 data.SetURL("http://google.com/?q={searchTerms}"); | |
| 973 data.instant_url = "http://google.com/instant#q={searchTerms}"; | |
| 974 data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}"); | |
| 975 data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}"); | |
| 976 TemplateURL url(data); | |
| 977 ASSERT_EQ(3U, url.URLCount()); | |
| 978 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0)); | |
| 979 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1)); | |
| 980 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2)); | |
| 981 } | |
| 982 | |
| 983 TEST_F(TemplateURLTest, GetURLOnlyOneURL) { | |
| 984 TemplateURLData data; | |
| 985 data.SetURL("http://www.google.co.uk/"); | |
| 986 TemplateURL url(data); | |
| 987 ASSERT_EQ(1U, url.URLCount()); | |
| 988 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0)); | |
| 989 } | |
| 990 | |
| 991 TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) { | |
| 992 TemplateURLData data; | |
| 993 data.SetURL("http://google.com/?q={searchTerms}"); | |
| 994 data.instant_url = "http://google.com/instant#q={searchTerms}"; | |
| 995 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}"); | |
| 996 data.alternate_urls.push_back( | |
| 997 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar"); | |
| 998 TemplateURL url(data); | |
| 999 base::string16 result; | |
| 1000 | |
| 1001 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | |
| 1002 GURL("http://google.com/?q=something"), search_terms_data_, &result)); | |
| 1003 EXPECT_EQ(ASCIIToUTF16("something"), result); | |
| 1004 | |
| 1005 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | |
| 1006 GURL("http://google.com/?espv&q=something"), | |
| 1007 search_terms_data_, &result)); | |
| 1008 EXPECT_EQ(ASCIIToUTF16("something"), result); | |
| 1009 | |
| 1010 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | |
| 1011 GURL("http://google.com/?espv=1&q=something"), | |
| 1012 search_terms_data_, &result)); | |
| 1013 EXPECT_EQ(ASCIIToUTF16("something"), result); | |
| 1014 | |
| 1015 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | |
| 1016 GURL("http://google.com/?espv=0&q=something"), | |
| 1017 search_terms_data_, &result)); | |
| 1018 EXPECT_EQ(ASCIIToUTF16("something"), result); | |
| 1019 | |
| 1020 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | |
| 1021 GURL("http://google.com/alt/#q=something"), | |
| 1022 search_terms_data_, &result)); | |
| 1023 EXPECT_EQ(ASCIIToUTF16("something"), result); | |
| 1024 | |
| 1025 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | |
| 1026 GURL("http://google.com/alt/#espv&q=something"), | |
| 1027 search_terms_data_, &result)); | |
| 1028 EXPECT_EQ(ASCIIToUTF16("something"), result); | |
| 1029 | |
| 1030 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | |
| 1031 GURL("http://google.com/alt/#espv=1&q=something"), | |
| 1032 search_terms_data_, &result)); | |
| 1033 EXPECT_EQ(ASCIIToUTF16("something"), result); | |
| 1034 | |
| 1035 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | |
| 1036 GURL("http://google.com/alt/#espv=0&q=something"), | |
| 1037 search_terms_data_, &result)); | |
| 1038 EXPECT_EQ(ASCIIToUTF16("something"), result); | |
| 1039 | |
| 1040 EXPECT_FALSE(url.ExtractSearchTermsFromURL( | |
| 1041 GURL("http://google.ca/?q=something"), search_terms_data_, &result)); | |
| 1042 EXPECT_EQ(base::string16(), result); | |
| 1043 | |
| 1044 EXPECT_FALSE(url.ExtractSearchTermsFromURL( | |
| 1045 GURL("http://google.ca/?q=something&q=anything"), | |
| 1046 search_terms_data_, &result)); | |
| 1047 EXPECT_EQ(base::string16(), result); | |
| 1048 | |
| 1049 EXPECT_FALSE(url.ExtractSearchTermsFromURL( | |
| 1050 GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result)); | |
| 1051 EXPECT_EQ(base::string16(), result); | |
| 1052 | |
| 1053 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | |
| 1054 GURL("https://google.com/?q=foo"), search_terms_data_, &result)); | |
| 1055 EXPECT_EQ(ASCIIToUTF16("foo"), result); | |
| 1056 | |
| 1057 EXPECT_FALSE(url.ExtractSearchTermsFromURL( | |
| 1058 GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result)); | |
| 1059 EXPECT_EQ(base::string16(), result); | |
| 1060 | |
| 1061 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | |
| 1062 GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result)); | |
| 1063 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result); | |
| 1064 | |
| 1065 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | |
| 1066 GURL("http://google.com/alt/?q=123#q=456"), | |
| 1067 search_terms_data_, &result)); | |
| 1068 EXPECT_EQ(ASCIIToUTF16("456"), result); | |
| 1069 | |
| 1070 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | |
| 1071 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), | |
| 1072 search_terms_data_, &result)); | |
| 1073 EXPECT_EQ(ASCIIToUTF16("123"), result); | |
| 1074 | |
| 1075 EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL( | |
| 1076 "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), | |
| 1077 search_terms_data_, &result)); | |
| 1078 EXPECT_EQ(ASCIIToUTF16("789"), result); | |
| 1079 | |
| 1080 EXPECT_FALSE(url.ExtractSearchTermsFromURL( | |
| 1081 GURL("http://google.com/alt/?q="), search_terms_data_, &result)); | |
| 1082 EXPECT_EQ(base::string16(), result); | |
| 1083 | |
| 1084 EXPECT_FALSE(url.ExtractSearchTermsFromURL( | |
| 1085 GURL("http://google.com/alt/?#q="), search_terms_data_, &result)); | |
| 1086 EXPECT_EQ(base::string16(), result); | |
| 1087 | |
| 1088 EXPECT_FALSE(url.ExtractSearchTermsFromURL( | |
| 1089 GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result)); | |
| 1090 EXPECT_EQ(base::string16(), result); | |
| 1091 | |
| 1092 EXPECT_FALSE(url.ExtractSearchTermsFromURL( | |
| 1093 GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result)); | |
| 1094 EXPECT_EQ(base::string16(), result); | |
| 1095 | |
| 1096 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | |
| 1097 GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result)); | |
| 1098 EXPECT_EQ(ASCIIToUTF16("123"), result); | |
| 1099 } | |
| 1100 | |
| 1101 TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) { | |
| 1102 TemplateURLData data; | |
| 1103 data.SetURL("http://google.com/?q={searchTerms}"); | |
| 1104 data.instant_url = "http://google.com/instant#q={searchTerms}"; | |
| 1105 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}"); | |
| 1106 data.alternate_urls.push_back( | |
| 1107 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar"); | |
| 1108 data.search_terms_replacement_key = "espv"; | |
| 1109 TemplateURL url(data); | |
| 1110 | |
| 1111 // Test with instant enabled required. | |
| 1112 EXPECT_FALSE(url.HasSearchTermsReplacementKey( | |
| 1113 GURL("http://google.com/"))); | |
| 1114 | |
| 1115 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1116 GURL("http://google.com/?espv"))); | |
| 1117 | |
| 1118 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1119 GURL("http://google.com/#espv"))); | |
| 1120 | |
| 1121 EXPECT_FALSE(url.HasSearchTermsReplacementKey( | |
| 1122 GURL("http://google.com/?q=something"))); | |
| 1123 | |
| 1124 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1125 GURL("http://google.com/?q=something&espv"))); | |
| 1126 | |
| 1127 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1128 GURL("http://google.com/?q=something&espv=1"))); | |
| 1129 | |
| 1130 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1131 GURL("http://google.com/?q=something&espv=0"))); | |
| 1132 | |
| 1133 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1134 GURL("http://google.com/?espv&q=something"))); | |
| 1135 | |
| 1136 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1137 GURL("http://google.com/?espv=1&q=something"))); | |
| 1138 | |
| 1139 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1140 GURL("http://google.com/?espv=0&q=something"))); | |
| 1141 | |
| 1142 EXPECT_FALSE(url.HasSearchTermsReplacementKey( | |
| 1143 GURL("http://google.com/alt/#q=something"))); | |
| 1144 | |
| 1145 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1146 GURL("http://google.com/alt/#q=something&espv"))); | |
| 1147 | |
| 1148 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1149 GURL("http://google.com/alt/#q=something&espv=1"))); | |
| 1150 | |
| 1151 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1152 GURL("http://google.com/alt/#q=something&espv=0"))); | |
| 1153 | |
| 1154 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1155 GURL("http://google.com/alt/#espv&q=something"))); | |
| 1156 | |
| 1157 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1158 GURL("http://google.com/alt/#espv=1&q=something"))); | |
| 1159 | |
| 1160 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1161 GURL("http://google.com/alt/#espv=0&q=something"))); | |
| 1162 | |
| 1163 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1164 GURL("http://google.com/?espv#q=something"))); | |
| 1165 | |
| 1166 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1167 GURL("http://google.com/?espv=1#q=something"))); | |
| 1168 | |
| 1169 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1170 GURL("http://google.com/?q=something#espv"))); | |
| 1171 | |
| 1172 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1173 GURL("http://google.com/?q=something#espv=1"))); | |
| 1174 | |
| 1175 // This does not ensure the domain matches. | |
| 1176 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1177 GURL("http://bing.com/?espv"))); | |
| 1178 | |
| 1179 EXPECT_TRUE(url.HasSearchTermsReplacementKey( | |
| 1180 GURL("http://bing.com/#espv"))); | |
| 1181 } | |
| 1182 | |
| 1183 TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) { | |
| 1184 TemplateURLData data; | |
| 1185 data.SetURL("http://google.com/?q={searchTerms}"); | |
| 1186 data.instant_url = "http://google.com/instant#q={searchTerms}"; | |
| 1187 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}"); | |
| 1188 data.alternate_urls.push_back( | |
| 1189 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar"); | |
| 1190 TemplateURL url(data); | |
| 1191 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane")); | |
| 1192 GURL result; | |
| 1193 | |
| 1194 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | |
| 1195 GURL("http://google.com/?q=something"), search_terms, | |
| 1196 search_terms_data_, &result)); | |
| 1197 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result); | |
| 1198 | |
| 1199 result = GURL("http://should.not.change.com"); | |
| 1200 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | |
| 1201 GURL("http://google.ca/?q=something"), search_terms, | |
| 1202 search_terms_data_, &result)); | |
| 1203 EXPECT_EQ(GURL("http://should.not.change.com"), result); | |
| 1204 | |
| 1205 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | |
| 1206 GURL("http://google.com/foo/?q=foo"), search_terms, | |
| 1207 search_terms_data_, &result)); | |
| 1208 | |
| 1209 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | |
| 1210 GURL("https://google.com/?q=foo"), search_terms, | |
| 1211 search_terms_data_, &result)); | |
| 1212 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result); | |
| 1213 | |
| 1214 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | |
| 1215 GURL("http://google.com:8080/?q=foo"), search_terms, | |
| 1216 search_terms_data_, &result)); | |
| 1217 | |
| 1218 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | |
| 1219 GURL("http://google.com/?q=1+2+3&b=456"), search_terms, | |
| 1220 search_terms_data_, &result)); | |
| 1221 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result); | |
| 1222 | |
| 1223 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in | |
| 1224 // template_url.cc for details. | |
| 1225 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | |
| 1226 GURL("http://google.com/alt/?q=123#q=456"), search_terms, | |
| 1227 search_terms_data_, &result)); | |
| 1228 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result); | |
| 1229 | |
| 1230 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | |
| 1231 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms, | |
| 1232 search_terms_data_, &result)); | |
| 1233 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"), | |
| 1234 result); | |
| 1235 | |
| 1236 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | |
| 1237 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), | |
| 1238 search_terms, search_terms_data_, &result)); | |
| 1239 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456" | |
| 1240 "#j=abc&q=Bob Morane&h=def9"), result); | |
| 1241 | |
| 1242 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | |
| 1243 GURL("http://google.com/alt/?q="), search_terms, | |
| 1244 search_terms_data_, &result)); | |
| 1245 | |
| 1246 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | |
| 1247 GURL("http://google.com/alt/?#q="), search_terms, | |
| 1248 search_terms_data_, &result)); | |
| 1249 | |
| 1250 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | |
| 1251 GURL("http://google.com/alt/?q=#q="), search_terms, | |
| 1252 search_terms_data_, &result)); | |
| 1253 | |
| 1254 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | |
| 1255 GURL("http://google.com/alt/?q=123#q="), search_terms, | |
| 1256 search_terms_data_, &result)); | |
| 1257 | |
| 1258 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | |
| 1259 GURL("http://google.com/alt/?q=#q=123"), search_terms, | |
| 1260 search_terms_data_, &result)); | |
| 1261 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result); | |
| 1262 } | |
| 1263 | |
| 1264 // Test the |suggest_query_params| field of SearchTermsArgs. | |
| 1265 TEST_F(TemplateURLTest, SuggestQueryParams) { | |
| 1266 TemplateURLData data; | |
| 1267 // Pick a URL with replacements before, during, and after the query, to ensure | |
| 1268 // we don't goof up any of them. | |
| 1269 data.SetURL("{google:baseURL}search?q={searchTerms}" | |
| 1270 "#{google:originalQueryForSuggestion}x"); | |
| 1271 TemplateURL url(data); | |
| 1272 | |
| 1273 // Baseline: no |suggest_query_params| field. | |
| 1274 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc")); | |
| 1275 search_terms.original_query = ASCIIToUTF16("def"); | |
| 1276 search_terms.accepted_suggestion = 0; | |
| 1277 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", | |
| 1278 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); | |
| 1279 | |
| 1280 // Set the suggest_query_params. | |
| 1281 search_terms.suggest_query_params = "pq=xyz"; | |
| 1282 EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x", | |
| 1283 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); | |
| 1284 | |
| 1285 // Add extra_query_params in the mix, and ensure it works. | |
| 1286 search_terms.append_extra_query_params = true; | |
| 1287 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 1288 switches::kExtraSearchQueryParams, "a=b"); | |
| 1289 EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x", | |
| 1290 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); | |
| 1291 } | |
| 1292 | |
| 1293 // Test the |append_extra_query_params| field of SearchTermsArgs. | |
| 1294 TEST_F(TemplateURLTest, ExtraQueryParams) { | |
| 1295 TemplateURLData data; | |
| 1296 // Pick a URL with replacements before, during, and after the query, to ensure | |
| 1297 // we don't goof up any of them. | |
| 1298 data.SetURL("{google:baseURL}search?q={searchTerms}" | |
| 1299 "#{google:originalQueryForSuggestion}x"); | |
| 1300 TemplateURL url(data); | |
| 1301 | |
| 1302 // Baseline: no command-line args, no |append_extra_query_params| flag. | |
| 1303 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc")); | |
| 1304 search_terms.original_query = ASCIIToUTF16("def"); | |
| 1305 search_terms.accepted_suggestion = 0; | |
| 1306 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", | |
| 1307 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); | |
| 1308 | |
| 1309 // Set the flag. Since there are no command-line args, this should have no | |
| 1310 // effect. | |
| 1311 search_terms.append_extra_query_params = true; | |
| 1312 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", | |
| 1313 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); | |
| 1314 | |
| 1315 // Now append the command-line arg. This should be inserted into the query. | |
| 1316 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 1317 switches::kExtraSearchQueryParams, "a=b"); | |
| 1318 EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x", | |
| 1319 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); | |
| 1320 | |
| 1321 // Turn off the flag. Now the command-line arg should be ignored again. | |
| 1322 search_terms.append_extra_query_params = false; | |
| 1323 EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", | |
| 1324 url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); | |
| 1325 } | |
| 1326 | |
| 1327 // Tests replacing pageClassification. | |
| 1328 TEST_F(TemplateURLTest, ReplacePageClassification) { | |
| 1329 TemplateURLData data; | |
| 1330 data.input_encodings.push_back("UTF-8"); | |
| 1331 data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}"); | |
| 1332 TemplateURL url(data); | |
| 1333 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 1334 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 1335 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); | |
| 1336 | |
| 1337 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 1338 search_terms_data_); | |
| 1339 EXPECT_EQ("http://www.google.com/?q=foo", result); | |
| 1340 | |
| 1341 search_terms_args.page_classification = metrics::OmniboxEventProto::NTP; | |
| 1342 result = url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 1343 search_terms_data_); | |
| 1344 EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result); | |
| 1345 | |
| 1346 search_terms_args.page_classification = | |
| 1347 metrics::OmniboxEventProto::HOME_PAGE; | |
| 1348 result = url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 1349 search_terms_data_); | |
| 1350 EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result); | |
| 1351 } | |
| 1352 | |
| 1353 // Test the IsSearchResults function. | |
| 1354 TEST_F(TemplateURLTest, IsSearchResults) { | |
| 1355 TemplateURLData data; | |
| 1356 data.SetURL("http://bar/search?q={searchTerms}"); | |
| 1357 data.instant_url = "http://bar/instant#q={searchTerms}"; | |
| 1358 data.new_tab_url = "http://bar/newtab"; | |
| 1359 data.alternate_urls.push_back("http://bar/?q={searchTerms}"); | |
| 1360 data.alternate_urls.push_back("http://bar/#q={searchTerms}"); | |
| 1361 data.alternate_urls.push_back("http://bar/search#q{searchTerms}"); | |
| 1362 data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}"); | |
| 1363 TemplateURL search_provider(data); | |
| 1364 | |
| 1365 const struct { | |
| 1366 const char* const url; | |
| 1367 bool result; | |
| 1368 } url_data[] = { | |
| 1369 { "http://bar/search?q=foo&oq=foo", true, }, | |
| 1370 { "http://bar/?q=foo&oq=foo", true, }, | |
| 1371 { "http://bar/#output=search&q=foo&oq=foo", true, }, | |
| 1372 { "http://bar/webhp#q=foo&oq=foo", true, }, | |
| 1373 { "http://bar/#q=foo&oq=foo", true, }, | |
| 1374 { "http://bar/?ext=foo&q=foo#ref=bar", true, }, | |
| 1375 { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, }, | |
| 1376 { "http://bar/", false, }, | |
| 1377 { "http://foo/", false, }, | |
| 1378 { "http://bar/newtab", false, }, | |
| 1379 }; | |
| 1380 | |
| 1381 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_data); ++i) { | |
| 1382 EXPECT_EQ(url_data[i].result, | |
| 1383 search_provider.IsSearchURL(GURL(url_data[i].url), | |
| 1384 search_terms_data_)); | |
| 1385 } | |
| 1386 } | |
| 1387 | |
| 1388 TEST_F(TemplateURLTest, ReflectsBookmarkBarPinned) { | |
| 1389 TemplateURLData data; | |
| 1390 data.input_encodings.push_back("UTF-8"); | |
| 1391 data.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}"); | |
| 1392 TemplateURL url(data); | |
| 1393 EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); | |
| 1394 ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); | |
| 1395 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); | |
| 1396 | |
| 1397 // Do not add the param when InstantExtended is suppressed on SRPs. | |
| 1398 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(false); | |
| 1399 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 1400 search_terms_data_); | |
| 1401 EXPECT_EQ("http://www.google.com/?q=foo", result); | |
| 1402 | |
| 1403 // Add the param when InstantExtended is not suppressed on SRPs. | |
| 1404 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true); | |
| 1405 search_terms_args.bookmark_bar_pinned = false; | |
| 1406 result = url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 1407 search_terms_data_); | |
| 1408 EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result); | |
| 1409 | |
| 1410 search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true); | |
| 1411 search_terms_args.bookmark_bar_pinned = true; | |
| 1412 result = url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 1413 search_terms_data_); | |
| 1414 EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result); | |
| 1415 } | |
| 1416 | |
| 1417 TEST_F(TemplateURLTest, AnswersHasVersion) { | |
| 1418 TemplateURLData data; | |
| 1419 search_terms_data_.set_google_base_url("http://bar/"); | |
| 1420 data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t"); | |
| 1421 | |
| 1422 TemplateURL url(data); | |
| 1423 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); | |
| 1424 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 1425 search_terms_data_); | |
| 1426 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result); | |
| 1427 | |
| 1428 search_terms_data_.set_enable_answers_in_suggest(true); | |
| 1429 TemplateURL url2(data); | |
| 1430 result = url2.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 1431 search_terms_data_); | |
| 1432 EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result); | |
| 1433 } | |
| 1434 | |
| 1435 TEST_F(TemplateURLTest, SessionToken) { | |
| 1436 TemplateURLData data; | |
| 1437 search_terms_data_.set_google_base_url("http://bar/"); | |
| 1438 data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t"); | |
| 1439 | |
| 1440 TemplateURL url(data); | |
| 1441 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); | |
| 1442 search_terms_args.session_token = "SESSIONTOKENGOESHERE"; | |
| 1443 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 1444 search_terms_data_); | |
| 1445 EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result); | |
| 1446 | |
| 1447 TemplateURL url2(data); | |
| 1448 search_terms_args.session_token = ""; | |
| 1449 result = url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 1450 search_terms_data_); | |
| 1451 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result); | |
| 1452 } | |
| 1453 | |
| 1454 TEST_F(TemplateURLTest, ContextualSearchParameters) { | |
| 1455 TemplateURLData data; | |
| 1456 search_terms_data_.set_google_base_url("http://bar/"); | |
| 1457 data.SetURL("http://bar/_/contextualsearch?" | |
| 1458 "{google:contextualSearchVersion}" | |
| 1459 "{google:contextualSearchContextData}"); | |
| 1460 | |
| 1461 TemplateURL url(data); | |
| 1462 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); | |
| 1463 std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 1464 search_terms_data_); | |
| 1465 EXPECT_EQ("http://bar/_/contextualsearch?", result); | |
| 1466 | |
| 1467 TemplateURLRef::SearchTermsArgs::ContextualSearchParams params( | |
| 1468 1, 6, 11, "allen", "woody+allen+movies", "www.wikipedia.org", | |
| 1469 "utf-8"); | |
| 1470 search_terms_args.contextual_search_params = params; | |
| 1471 result = url.url_ref().ReplaceSearchTerms(search_terms_args, | |
| 1472 search_terms_data_); | |
| 1473 EXPECT_EQ("http://bar/_/contextualsearch?" | |
| 1474 "ctxs=1&" | |
| 1475 "ctxs_start=6&" | |
| 1476 "ctxs_end=11&" | |
| 1477 "q=allen&" | |
| 1478 "ctxs_content=woody+allen+movies&" | |
| 1479 "ctxs_url=www.wikipedia.org&" | |
| 1480 "ctxs_encoding=utf-8&", result); | |
| 1481 } | |
| 1482 | |
| 1483 TEST_F(TemplateURLTest, GenerateKeyword) { | |
| 1484 ASSERT_EQ(ASCIIToUTF16("foo"), | |
| 1485 TemplateURL::GenerateKeyword(GURL("http://foo"))); | |
| 1486 // www. should be stripped. | |
| 1487 ASSERT_EQ(ASCIIToUTF16("foo"), | |
| 1488 TemplateURL::GenerateKeyword(GURL("http://www.foo"))); | |
| 1489 // Make sure we don't get a trailing '/'. | |
| 1490 ASSERT_EQ(ASCIIToUTF16("blah"), | |
| 1491 TemplateURL::GenerateKeyword(GURL("http://blah/"))); | |
| 1492 // Don't generate the empty string. | |
| 1493 ASSERT_EQ(ASCIIToUTF16("www"), | |
| 1494 TemplateURL::GenerateKeyword(GURL("http://www."))); | |
| 1495 } | |
| 1496 | |
| 1497 TEST_F(TemplateURLTest, GenerateSearchURL) { | |
| 1498 struct GenerateSearchURLCase { | |
| 1499 const char* test_name; | |
| 1500 const char* url; | |
| 1501 const char* expected; | |
| 1502 } generate_url_cases[] = { | |
| 1503 { "invalid URL", "foo{searchTerms}", "" }, | |
| 1504 { "URL with no replacements", "http://foo/", "http://foo/" }, | |
| 1505 { "basic functionality", "http://foo/{searchTerms}", | |
| 1506 "http://foo/blah.blah.blah.blah.blah" } | |
| 1507 }; | |
| 1508 | |
| 1509 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generate_url_cases); ++i) { | |
| 1510 TemplateURLData data; | |
| 1511 data.SetURL(generate_url_cases[i].url); | |
| 1512 TemplateURL t_url(data); | |
| 1513 EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(), | |
| 1514 generate_url_cases[i].expected) | |
| 1515 << generate_url_cases[i].test_name << " failed."; | |
| 1516 } | |
| 1517 } | |
| 1518 | |
| 1519 TEST_F(TemplateURLTest, PrefetchQueryParameters) { | |
| 1520 TemplateURLData data; | |
| 1521 search_terms_data_.set_google_base_url("http://bar/"); | |
| 1522 data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t"); | |
| 1523 | |
| 1524 TemplateURL url(data); | |
| 1525 TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); | |
| 1526 search_terms_args.prefetch_query = "full query text"; | |
| 1527 search_terms_args.prefetch_query_type = "2338"; | |
| 1528 std::string result = | |
| 1529 url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); | |
| 1530 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t", | |
| 1531 result); | |
| 1532 | |
| 1533 TemplateURL url2(data); | |
| 1534 search_terms_args.prefetch_query.clear(); | |
| 1535 search_terms_args.prefetch_query_type.clear(); | |
| 1536 result = | |
| 1537 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); | |
| 1538 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result); | |
| 1539 } | |
| OLD | NEW |