| // Copyright 2014 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include <stddef.h> |
| |
| #include "base/base64.h" |
| #include "base/base_paths.h" |
| #include "base/command_line.h" |
| #include "base/cxx17_backports.h" |
| #include "base/feature_list.h" |
| #include "base/i18n/case_conversion.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_util.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "components/google/core/common/google_util.h" |
| #include "components/omnibox/common/omnibox_features.h" |
| #include "components/search_engines/search_engines_switches.h" |
| #include "components/search_engines/search_terms_data.h" |
| #include "components/search_engines/template_url.h" |
| #include "components/search_engines/testing_search_terms_data.h" |
| #include "net/base/url_util.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "third_party/metrics_proto/omnibox_event.pb.h" |
| #include "third_party/metrics_proto/omnibox_input_type.pb.h" |
| |
| using base::ASCIIToUTF16; |
| |
| namespace { |
| bool IsLowerCase(const std::u16string& str) { |
| return str == base::i18n::ToLower(str); |
| } |
| } |
| |
| class TemplateURLTest : public testing::Test { |
| public: |
| TemplateURLTest() : search_terms_data_("http://www.google.com/") {} |
| void CheckSuggestBaseURL(const std::string& base_url, |
| const std::string& base_suggest_url) const; |
| |
| static void ExpectPostParamIs( |
| const TemplateURLRef::PostParam& param, |
| const std::string& name, |
| const std::string& value, |
| const std::string& content_type = std::string()); |
| |
| TestingSearchTermsData search_terms_data_; |
| }; |
| |
| void TemplateURLTest::CheckSuggestBaseURL( |
| const std::string& base_url, |
| const std::string& base_suggest_url) const { |
| TestingSearchTermsData search_terms_data(base_url); |
| EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue()); |
| } |
| |
| // static |
| void TemplateURLTest::ExpectPostParamIs(const TemplateURLRef::PostParam& param, |
| const std::string& name, |
| const std::string& value, |
| const std::string& content_type) { |
| EXPECT_EQ(name, param.name); |
| EXPECT_EQ(value, param.value); |
| EXPECT_EQ(content_type, param.content_type); |
| } |
| |
| TEST_F(TemplateURLTest, Defaults) { |
| TemplateURLData data; |
| EXPECT_FALSE(data.safe_for_autoreplace); |
| EXPECT_EQ(0, data.prepopulate_id); |
| } |
| |
| TEST_F(TemplateURLTest, TestValidWithComplete) { |
| TemplateURLData data; |
| data.SetURL("{searchTerms}"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| } |
| |
| TEST_F(TemplateURLTest, URLRefTestSearchTerms) { |
| struct SearchTermsCase { |
| const char* url; |
| const std::u16string terms; |
| const std::string output; |
| } search_term_cases[] = { |
| {"http://foo{searchTerms}", u"sea rch/bar", "http://foosea%20rch/bar"}, |
| {"http://foo{searchTerms}?boo=abc", u"sea rch/bar", |
| "http://foosea%20rch/bar?boo=abc"}, |
| {"http://foo/?boo={searchTerms}", u"sea rch/bar", |
| "http://foo/?boo=sea+rch%2Fbar"}, |
| {"http://en.wikipedia.org/{searchTerms}", u"wiki/?", |
| "http://en.wikipedia.org/wiki/%3F"}}; |
| for (size_t i = 0; i < base::size(search_term_cases); ++i) { |
| const SearchTermsCase& value = search_term_cases[i]; |
| TemplateURLData data; |
| data.SetURL(value.url); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(value.output, result.spec()); |
| } |
| } |
| |
| TEST_F(TemplateURLTest, URLRefTestCount) { |
| TemplateURLData data; |
| data.SetURL("http://foo{searchTerms}{count?}"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://foox/", result.spec()); |
| } |
| |
| TEST_F(TemplateURLTest, URLRefTestCount2) { |
| TemplateURLData data; |
| data.SetURL("http://foo{searchTerms}{count}"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://foox10/", result.spec()); |
| } |
| |
| TEST_F(TemplateURLTest, URLRefTestIndices) { |
| TemplateURLData data; |
| data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://fooxxy/", result.spec()); |
| } |
| |
| TEST_F(TemplateURLTest, URLRefTestIndices2) { |
| TemplateURLData data; |
| data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://fooxx1y1/", result.spec()); |
| } |
| |
| TEST_F(TemplateURLTest, URLRefTestEncoding) { |
| TemplateURLData data; |
| data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://fooxxutf-8ya/", result.spec()); |
| } |
| |
| TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) { |
| const char kInvalidPostParamsString[] = |
| "unknown_template={UnknownTemplate},bad_value=bad{value}," |
| "{google:sbiSource}"; |
| // List all accpectable parameter format in valid_post_params_string. it is |
| // expected like: "name0=,name1=value1,name2={template1}" |
| const char kValidPostParamsString[] = |
| "image_content={google:imageThumbnail},image_url={google:imageURL}," |
| "sbisrc={google:imageSearchSource},language={language},empty_param=," |
| "constant_param=constant,width={google:imageOriginalWidth}," |
| "base64_image_content={google:imageThumbnailBase64}"; |
| const char KImageSearchURL[] = "http://foo.com/sbi"; |
| |
| TemplateURLData data; |
| data.image_url = KImageSearchURL; |
| |
| // Try to parse invalid post parameters. |
| data.image_url_post_params = kInvalidPostParamsString; |
| TemplateURL url_bad(data); |
| ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_)); |
| const TemplateURLRef::PostParams& bad_post_params = |
| url_bad.image_url_ref().post_params_; |
| ASSERT_EQ(2U, bad_post_params.size()); |
| ExpectPostParamIs(bad_post_params[0], "unknown_template", |
| "{UnknownTemplate}"); |
| ExpectPostParamIs(bad_post_params[1], "bad_value", "bad{value}"); |
| |
| // Try to parse valid post parameters. |
| data.image_url_post_params = kValidPostParamsString; |
| TemplateURL url(data); |
| ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_)); |
| ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_)); |
| |
| // Check term replacement. |
| TemplateURLRef::SearchTermsArgs search_args(u"X"); |
| search_args.image_thumbnail_content = "dummy-image-thumbnail"; |
| search_args.image_url = GURL("http://dummyimage.com/dummy.jpg"); |
| search_args.image_original_size = gfx::Size(10, 10); |
| // Replacement operation with no post_data buffer should still return |
| // the parsed URL. |
| TestingSearchTermsData search_terms_data("http://X"); |
| GURL result(url.image_url_ref().ReplaceSearchTerms( |
| search_args, search_terms_data)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(KImageSearchURL, result.spec()); |
| TemplateURLRef::PostContent post_content; |
| result = GURL(url.image_url_ref().ReplaceSearchTerms( |
| search_args, search_terms_data, &post_content)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(KImageSearchURL, result.spec()); |
| ASSERT_FALSE(post_content.first.empty()); |
| ASSERT_FALSE(post_content.second.empty()); |
| |
| // Check parsed result of post parameters. |
| const TemplateURLRef::Replacements& replacements = |
| url.image_url_ref().replacements_; |
| const TemplateURLRef::PostParams& post_params = |
| url.image_url_ref().post_params_; |
| EXPECT_EQ(8U, post_params.size()); |
| for (auto i = post_params.begin(); i != post_params.end(); ++i) { |
| auto j = replacements.begin(); |
| for (; j != replacements.end(); ++j) { |
| if (j->is_post_param && j->index == |
| static_cast<size_t>(i - post_params.begin())) { |
| switch (j->type) { |
| case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH: |
| ExpectPostParamIs( |
| *i, "width", |
| base::NumberToString(search_args.image_original_size.width())); |
| break; |
| case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE: |
| ExpectPostParamIs(*i, "sbisrc", |
| search_terms_data.GoogleImageSearchSource()); |
| break; |
| case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL: |
| ExpectPostParamIs(*i, "image_content", |
| search_args.image_thumbnail_content, |
| "image/jpeg"); |
| break; |
| case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL_BASE64: { |
| std::string base64_image_content; |
| base::Base64Encode(search_args.image_thumbnail_content, |
| &base64_image_content); |
| ExpectPostParamIs(*i, "base64_image_content", base64_image_content, |
| "image/jpeg"); |
| break; |
| } |
| case TemplateURLRef::GOOGLE_IMAGE_URL: |
| ExpectPostParamIs(*i, "image_url", search_args.image_url.spec()); |
| break; |
| case TemplateURLRef::LANGUAGE: |
| ExpectPostParamIs(*i, "language", "en"); |
| break; |
| default: |
| ADD_FAILURE(); // Should never go here. |
| } |
| break; |
| } |
| } |
| if (j != replacements.end()) |
| continue; |
| if (i->name == "empty_param") |
| ExpectPostParamIs(*i, "empty_param", std::string()); |
| else |
| ExpectPostParamIs(*i, "constant_param", "constant"); |
| } |
| } |
| |
| TEST_F(TemplateURLTest, ImageURLWithGetShouldNotCrash) { |
| TemplateURLData data; |
| data.SetURL("http://foo/?q={searchTerms}&t={google:imageThumbnail}"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| |
| TemplateURLRef::SearchTermsArgs search_args(u"X"); |
| search_args.image_thumbnail_content = "dummy-image-thumbnail"; |
| GURL result( |
| url.url_ref().ReplaceSearchTerms(search_args, search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://foo/?q=X&t=dummy-image-thumbnail", result.spec()); |
| } |
| |
| // Test that setting the prepopulate ID from TemplateURL causes the stored |
| // TemplateURLRef to handle parsing the URL parameters differently. |
| TEST_F(TemplateURLTest, SetPrepopulatedAndParse) { |
| TemplateURLData data; |
| data.SetURL("http://foo{fhqwhgads}bar"); |
| TemplateURL url(data); |
| TemplateURLRef::Replacements replacements; |
| bool valid = false; |
| EXPECT_EQ("http://foo{fhqwhgads}bar", |
| url.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements, |
| nullptr, &valid)); |
| EXPECT_TRUE(replacements.empty()); |
| EXPECT_TRUE(valid); |
| |
| data.prepopulate_id = 123; |
| TemplateURL url2(data); |
| EXPECT_EQ("http://foobar", |
| url2.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements, |
| nullptr, &valid)); |
| EXPECT_TRUE(replacements.empty()); |
| EXPECT_TRUE(valid); |
| } |
| |
| // Test that setting the prepopulate ID from TemplateURL causes the stored |
| // TemplateURLRef to handle parsing the URL parameters differently. |
| TEST_F(TemplateURLTest, SetPrepopulatedAndReplace) { |
| TemplateURLData data; |
| data.SetURL("http://foo{fhqwhgads}search/?q={searchTerms}"); |
| data.suggestions_url = "http://foo{fhqwhgads}suggest/?q={searchTerms}"; |
| data.image_url = "http://foo{fhqwhgads}image/"; |
| data.new_tab_url = "http://foo{fhqwhgads}newtab/"; |
| data.contextual_search_url = "http://foo{fhqwhgads}context/"; |
| data.alternate_urls.push_back( |
| "http://foo{fhqwhgads}alternate/?q={searchTerms}"); |
| |
| TemplateURLRef::SearchTermsArgs args(u"X"); |
| const SearchTermsData& stdata = search_terms_data_; |
| |
| TemplateURL url(data); |
| EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X", |
| url.url_ref().ReplaceSearchTerms(args, stdata)); |
| EXPECT_EQ("http://foo%7Bfhqwhgads%7Dalternate/?q=X", |
| url.url_refs()[0].ReplaceSearchTerms(args, stdata)); |
| EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X", |
| url.url_refs()[1].ReplaceSearchTerms(args, stdata)); |
| EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsuggest/?q=X", |
| url.suggestions_url_ref().ReplaceSearchTerms(args, stdata)); |
| EXPECT_EQ("http://foo{fhqwhgads}image/", |
| url.image_url_ref().ReplaceSearchTerms(args, stdata)); |
| EXPECT_EQ("http://foo{fhqwhgads}newtab/", |
| url.new_tab_url_ref().ReplaceSearchTerms(args, stdata)); |
| EXPECT_EQ("http://foo{fhqwhgads}context/", |
| url.contextual_search_url_ref().ReplaceSearchTerms(args, stdata)); |
| |
| data.prepopulate_id = 123; |
| TemplateURL url2(data); |
| EXPECT_EQ("http://foosearch/?q=X", |
| url2.url_ref().ReplaceSearchTerms(args, stdata)); |
| EXPECT_EQ("http://fooalternate/?q=X", |
| url2.url_refs()[0].ReplaceSearchTerms(args, stdata)); |
| EXPECT_EQ("http://foosearch/?q=X", |
| url2.url_refs()[1].ReplaceSearchTerms(args, stdata)); |
| EXPECT_EQ("http://foosuggest/?q=X", |
| url2.suggestions_url_ref().ReplaceSearchTerms(args, stdata)); |
| EXPECT_EQ("http://fooimage/", |
| url2.image_url_ref().ReplaceSearchTerms(args, stdata)); |
| EXPECT_EQ("http://foonewtab/", |
| url2.new_tab_url_ref().ReplaceSearchTerms(args, stdata)); |
| EXPECT_EQ("http://foocontext/", |
| url2.contextual_search_url_ref().ReplaceSearchTerms(args, stdata)); |
| } |
| |
| TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) { |
| TemplateURLData data; |
| data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://fooxutf-8axyb/", result.spec()); |
| } |
| |
| TEST_F(TemplateURLTest, URLRefTestEncoding2) { |
| TemplateURLData data; |
| data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec()); |
| } |
| |
| TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) { |
| struct SearchTermsCase { |
| const char* url; |
| const std::u16string terms; |
| const char* output; |
| } search_term_cases[] = {{"{google:baseURL}{language}{searchTerms}", |
| std::u16string(), "http://example.com/e/en"}, |
| {"{google:baseSuggestURL}{searchTerms}", |
| std::u16string(), "http://example.com/complete/"}}; |
| |
| TestingSearchTermsData search_terms_data("http://example.com/e/"); |
| TemplateURLData data; |
| for (size_t i = 0; i < base::size(search_term_cases); ++i) { |
| const SearchTermsCase& value = search_term_cases[i]; |
| data.SetURL(value.url); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data)); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, |
| nullptr)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(value.output, result.spec()); |
| } |
| } |
| |
| TEST_F(TemplateURLTest, URLRefTermToWide) { |
| struct ToWideCase { |
| const char* encoded_search_term; |
| const std::u16string expected_decoded_term; |
| } to_wide_cases[] = { |
| {"hello+world", u"hello world"}, |
| // Test some big-5 input. |
| {"%a7A%A6%6e+to+you", u"\x4f60\x597d to you"}, |
| // Test some UTF-8 input. We should fall back to this when the encoding |
| // doesn't look like big-5. We have a '5' in the middle, which is an |
| // invalid Big-5 trailing byte. |
| {"%e4%bd%a05%e5%a5%bd+to+you", u"\x4f60\x35\x597d to you"}, |
| // Undecodable input should stay escaped. |
| {"%91%01+abcd", u"%91%01 abcd"}, |
| // Make sure we convert %2B to +. |
| {"C%2B%2B", u"C++"}, |
| // C%2B is escaped as C%252B, make sure we unescape it properly. |
| {"C%252B", u"C%2B"}, |
| }; |
| |
| // Set one input encoding: big-5. This is so we can test fallback to UTF-8. |
| TemplateURLData data; |
| data.SetURL("http://foo?q={searchTerms}"); |
| data.input_encodings.push_back("big-5"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| for (size_t i = 0; i < base::size(to_wide_cases); i++) { |
| EXPECT_EQ(to_wide_cases[i].expected_decoded_term, |
| url.url_ref().SearchTermToString16( |
| to_wide_cases[i].encoded_search_term)); |
| } |
| } |
| |
| TEST_F(TemplateURLTest, DisplayURLToURLRef) { |
| struct TestData { |
| const std::string url; |
| const std::u16string expected_result; |
| } test_data[] = { |
| {"http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", |
| u"http://foo%sx{inputEncoding}y{outputEncoding}a"}, |
| {"http://X", u"http://X"}, |
| {"http://foo{searchTerms", u"http://foo{searchTerms"}, |
| {"http://foo{searchTerms}{language}", u"http://foo%s{language}"}, |
| }; |
| TemplateURLData data; |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| data.SetURL(test_data[i].url); |
| TemplateURL url(data); |
| EXPECT_EQ(test_data[i].expected_result, |
| url.url_ref().DisplayURL(search_terms_data_)); |
| EXPECT_EQ(test_data[i].url, |
| TemplateURLRef::DisplayURLToURLRef( |
| url.url_ref().DisplayURL(search_terms_data_))); |
| } |
| } |
| |
| TEST_F(TemplateURLTest, ReplaceSearchTerms) { |
| struct TestData { |
| const std::string url; |
| const std::string expected_result; |
| } test_data[] = { |
| { "http://foo/{language}{searchTerms}{inputEncoding}", |
| "http://foo/{language}XUTF-8" }, |
| { "http://foo/{language}{inputEncoding}{searchTerms}", |
| "http://foo/{language}UTF-8X" }, |
| { "http://foo/{searchTerms}{language}{inputEncoding}", |
| "http://foo/X{language}UTF-8" }, |
| { "http://foo/{searchTerms}{inputEncoding}{language}", |
| "http://foo/XUTF-8{language}" }, |
| { "http://foo/{inputEncoding}{searchTerms}{language}", |
| "http://foo/UTF-8X{language}" }, |
| { "http://foo/{inputEncoding}{language}{searchTerms}", |
| "http://foo/UTF-8{language}X" }, |
| { "http://foo/{language}a{searchTerms}a{inputEncoding}a", |
| "http://foo/{language}aXaUTF-8a" }, |
| { "http://foo/{language}a{inputEncoding}a{searchTerms}a", |
| "http://foo/{language}aUTF-8aXa" }, |
| { "http://foo/{searchTerms}a{language}a{inputEncoding}a", |
| "http://foo/Xa{language}aUTF-8a" }, |
| { "http://foo/{searchTerms}a{inputEncoding}a{language}a", |
| "http://foo/XaUTF-8a{language}a" }, |
| { "http://foo/{inputEncoding}a{searchTerms}a{language}a", |
| "http://foo/UTF-8aXa{language}a" }, |
| { "http://foo/{inputEncoding}a{language}a{searchTerms}a", |
| "http://foo/UTF-8a{language}aXa" }, |
| }; |
| TemplateURLData data; |
| data.input_encodings.push_back("UTF-8"); |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| data.SetURL(test_data[i].url); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| std::string expected_result = test_data[i].expected_result; |
| base::ReplaceSubstringsAfterOffset( |
| &expected_result, 0, "{language}", |
| search_terms_data_.GetApplicationLocale()); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(u"X"), search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(expected_result, result.spec()); |
| } |
| } |
| |
| |
| // Tests replacing search terms in various encodings and making sure the |
| // generated URL matches the expected value. |
| TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) { |
| struct TestData { |
| const std::string encoding; |
| const std::u16string search_term; |
| const std::string url; |
| const std::string expected_result; |
| } test_data[] = { |
| {"BIG5", u"悽", "http://foo/?{searchTerms}{inputEncoding}", |
| "http://foo/?%B1~BIG5"}, |
| {"UTF-8", u"blah", "http://foo/?{searchTerms}{inputEncoding}", |
| "http://foo/?blahUTF-8"}, |
| {"Shift_JIS", u"あ", "http://foo/{searchTerms}/bar", |
| "http://foo/%82%A0/bar"}, |
| {"Shift_JIS", u"あ い", "http://foo/{searchTerms}/bar", |
| "http://foo/%82%A0%20%82%A2/bar"}, |
| }; |
| TemplateURLData data; |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| data.SetURL(test_data[i].url); |
| data.input_encodings.clear(); |
| data.input_encodings.push_back(test_data[i].encoding); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(test_data[i].search_term), |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| } |
| } |
| |
| // Test that encoding with several optional codepages works as intended. |
| // Codepages are tried in order, fallback is UTF-8. |
| TEST_F(TemplateURLTest, ReplaceSearchTermsMultipleEncodings) { |
| struct TestData { |
| const std::vector<std::string> encodings; |
| const std::u16string search_term; |
| const std::string url; |
| const std::string expected_result; |
| } test_data[] = { |
| // First and third encodings are valid. First is used. |
| {{"windows-1251", "cp-866", "UTF-8"}, |
| u"я", |
| "http://foo/?{searchTerms}{inputEncoding}", |
| "http://foo/?%FFwindows-1251"}, |
| // Second and third encodings are valid, second is used. |
| {{"cp-866", "GB2312", "UTF-8"}, |
| u"狗", |
| "http://foo/?{searchTerms}{inputEncoding}", |
| "http://foo/?%B9%B7GB2312"}, |
| // Second and third encodings are valid in another order, second is used. |
| {{"cp-866", "UTF-8", "GB2312"}, |
| u"狗", |
| "http://foo/?{searchTerms}{inputEncoding}", |
| "http://foo/?%E7%8B%97UTF-8"}, |
| // Both encodings are invalid, fallback to UTF-8. |
| {{"cp-866", "windows-1251"}, |
| u"狗", |
| "http://foo/?{searchTerms}{inputEncoding}", |
| "http://foo/?%E7%8B%97UTF-8"}, |
| // No encodings are given, fallback to UTF-8. |
| {{}, |
| u"狗", |
| "http://foo/?{searchTerms}{inputEncoding}", |
| "http://foo/?%E7%8B%97UTF-8"}, |
| }; |
| |
| TemplateURLData data; |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| data.SetURL(test_data[i].url); |
| data.input_encodings = test_data[i].encodings; |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(test_data[i].search_term), |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| } |
| } |
| |
| // Tests replacing assisted query stats (AQS) in various scenarios. |
| TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) { |
| struct TestData { |
| const std::u16string search_term; |
| const std::string aqs; |
| const std::string base_url; |
| const std::string url; |
| const std::string expected_result; |
| } test_data[] = { |
| // No HTTPS, no AQS. |
| {u"foo", "chrome.0.0l6", "http://foo/", |
| "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", |
| "http://foo/?foo"}, |
| // HTTPS available, AQS should be replaced. |
| {u"foo", "chrome.0.0l6", "https://foo/", |
| "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", |
| "https://foo/?fooaqs=chrome.0.0l6&"}, |
| // HTTPS available, however AQS is empty. |
| {u"foo", "", "https://foo/", |
| "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", |
| "https://foo/?foo"}, |
| // No {google:baseURL} and protocol is HTTP, we must not substitute AQS. |
| {u"foo", "chrome.0.0l6", "http://www.google.com", |
| "http://foo?{searchTerms}{google:assistedQueryStats}", |
| "http://foo/?foo"}, |
| // A non-Google search provider with HTTPS should allow AQS. |
| {u"foo", "chrome.0.0l6", "https://www.google.com", |
| "https://foo?{searchTerms}{google:assistedQueryStats}", |
| "https://foo/?fooaqs=chrome.0.0l6&"}, |
| }; |
| TemplateURLData data; |
| data.input_encodings.push_back("UTF-8"); |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| data.SetURL(test_data[i].url); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| search_terms_args.assisted_query_stats = test_data[i].aqs; |
| search_terms_data_.set_google_base_url(test_data[i].base_url); |
| GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| } |
| } |
| |
| // Tests replacing cursor position. |
| TEST_F(TemplateURLTest, ReplaceCursorPosition) { |
| struct TestData { |
| const std::u16string search_term; |
| size_t cursor_position; |
| const std::string url; |
| const std::string expected_result; |
| } test_data[] = { |
| {u"foo", std::u16string::npos, |
| "{google:baseURL}?{searchTerms}&{google:cursorPosition}", |
| "http://www.google.com/?foo&"}, |
| {u"foo", 2, "{google:baseURL}?{searchTerms}&{google:cursorPosition}", |
| "http://www.google.com/?foo&cp=2&"}, |
| {u"foo", 15, "{google:baseURL}?{searchTerms}&{google:cursorPosition}", |
| "http://www.google.com/?foo&cp=15&"}, |
| }; |
| TemplateURLData data; |
| data.input_encodings.push_back("UTF-8"); |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| data.SetURL(test_data[i].url); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| search_terms_args.cursor_position = test_data[i].cursor_position; |
| GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| } |
| } |
| |
| // Tests replacing input type (&oit=). |
| TEST_F(TemplateURLTest, ReplaceInputType) { |
| struct TestData { |
| const std::u16string search_term; |
| metrics::OmniboxInputType input_type; |
| const std::string url; |
| const std::string expected_result; |
| } test_data[] = { |
| {u"foo", metrics::OmniboxInputType::UNKNOWN, |
| "{google:baseURL}?{searchTerms}&{google:inputType}", |
| "http://www.google.com/?foo&oit=1&"}, |
| {u"foo", metrics::OmniboxInputType::URL, |
| "{google:baseURL}?{searchTerms}&{google:inputType}", |
| "http://www.google.com/?foo&oit=3&"}, |
| }; |
| TemplateURLData data; |
| data.input_encodings.push_back("UTF-8"); |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| data.SetURL(test_data[i].url); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| search_terms_args.input_type = test_data[i].input_type; |
| GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| } |
| } |
| |
| // Tests replacing omnibox focus type (&oft=). |
| TEST_F(TemplateURLTest, ReplaceOmniboxFocusType) { |
| struct TestData { |
| const std::u16string search_term; |
| OmniboxFocusType focus_type; |
| const std::string url; |
| const std::string expected_result; |
| } test_data[] = { |
| {u"foo", OmniboxFocusType::DEFAULT, |
| "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}", |
| "http://www.google.com/?foo&"}, |
| {u"foo", OmniboxFocusType::ON_FOCUS, |
| "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}", |
| "http://www.google.com/?foo&oft=1&"}, |
| {u"foo", OmniboxFocusType::DELETED_PERMANENT_TEXT, |
| "{google:baseURL}?{searchTerms}&{google:omniboxFocusType}", |
| "http://www.google.com/?foo&oft=2&"}, |
| }; |
| TemplateURLData data; |
| data.input_encodings.push_back("UTF-8"); |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| data.SetURL(test_data[i].url); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| search_terms_args.focus_type = test_data[i].focus_type; |
| GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| } |
| } |
| |
| // Tests replacing prefetch source (&pf=). |
| TEST_F(TemplateURLTest, ReplaceIsPrefetch) { |
| struct TestData { |
| const std::u16string search_term; |
| bool is_prefetch; |
| const std::string url; |
| const std::string expected_result; |
| } test_data[] = { |
| {u"foo", false, "{google:baseURL}?{searchTerms}&{google:prefetchSource}", |
| "http://www.google.com/?foo&"}, |
| {u"foo", true, "{google:baseURL}?{searchTerms}&{google:prefetchSource}", |
| "http://www.google.com/?foo&pf=cs&"}, |
| }; |
| TemplateURLData data; |
| data.input_encodings.push_back("UTF-8"); |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| data.SetURL(test_data[i].url); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| search_terms_args.is_prefetch = test_data[i].is_prefetch; |
| GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| } |
| } |
| |
| // Tests replacing currentPageUrl. |
| TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) { |
| struct TestData { |
| const std::u16string search_term; |
| const std::string current_page_url; |
| const std::string url; |
| const std::string expected_result; |
| } test_data[] = { |
| {u"foo", "http://www.google.com/", |
| "{google:baseURL}?{searchTerms}&{google:currentPageUrl}", |
| "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&"}, |
| {u"foo", "", "{google:baseURL}?{searchTerms}&{google:currentPageUrl}", |
| "http://www.google.com/?foo&"}, |
| {u"foo", "http://g.com/+-/*&=", |
| "{google:baseURL}?{searchTerms}&{google:currentPageUrl}", |
| "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&"}, |
| }; |
| TemplateURLData data; |
| data.input_encodings.push_back("UTF-8"); |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| data.SetURL(test_data[i].url); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| search_terms_args.current_page_url = test_data[i].current_page_url; |
| GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| } |
| } |
| |
| // Tests appending attribution parameter to queries originating from Play API |
| // search engine. |
| TEST_F(TemplateURLTest, PlayAPIAttribution) { |
| const struct TestData { |
| const char* url; |
| std::u16string terms; |
| bool created_from_play_api; |
| const char* output; |
| } test_data[] = { |
| {"http://foo/?q={searchTerms}", u"bar", false, "http://foo/?q=bar"}, |
| {"http://foo/?q={searchTerms}", u"bar", true, |
| "http://foo/?q=bar&chrome_dse_attribution=1"}}; |
| TemplateURLData data; |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| data.SetURL(test_data[i].url); |
| data.created_from_play_api = test_data[i].created_from_play_api; |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(test_data[i].terms), |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(test_data[i].output, result.spec()); |
| } |
| } |
| |
| TEST_F(TemplateURLTest, Suggestions) { |
| struct TestData { |
| const int accepted_suggestion; |
| const std::u16string original_query_for_suggestion; |
| const std::string expected_result; |
| } test_data[] = { |
| {TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::u16string(), |
| "http://bar/foo?q=foobar"}, |
| {TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, u"foo", |
| "http://bar/foo?q=foobar"}, |
| {TemplateURLRef::NO_SUGGESTION_CHOSEN, std::u16string(), |
| "http://bar/foo?q=foobar"}, |
| {TemplateURLRef::NO_SUGGESTION_CHOSEN, u"foo", "http://bar/foo?q=foobar"}, |
| {0, std::u16string(), "http://bar/foo?oq=&q=foobar"}, |
| {1, u"foo", "http://bar/foo?oq=foo&q=foobar"}, |
| }; |
| TemplateURLData data; |
| data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}" |
| "q={searchTerms}"); |
| data.input_encodings.push_back("UTF-8"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| TemplateURLRef::SearchTermsArgs search_terms_args(u"foobar"); |
| search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion; |
| search_terms_args.original_query = |
| test_data[i].original_query_for_suggestion; |
| GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| } |
| } |
| |
| TEST_F(TemplateURLTest, RLZ) { |
| std::u16string rlz_string = search_terms_data_.GetRlzParameterValue(false); |
| |
| TemplateURLData data; |
| data.SetURL("http://bar/?{google:RLZ}{searchTerms}"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| GURL result(url.url_ref().ReplaceSearchTerms( |
| TemplateURLRef::SearchTermsArgs(u"x"), search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x", |
| result.spec()); |
| } |
| |
| TEST_F(TemplateURLTest, RLZFromAppList) { |
| std::u16string rlz_string = search_terms_data_.GetRlzParameterValue(true); |
| |
| TemplateURLData data; |
| data.SetURL("http://bar/?{google:RLZ}{searchTerms}"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| TemplateURLRef::SearchTermsArgs args(u"x"); |
| args.request_source = TemplateURLRef::CROS_APP_LIST; |
| GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x", |
| result.spec()); |
| } |
| |
| TEST_F(TemplateURLTest, HostAndSearchTermKey) { |
| struct TestData { |
| const std::string url; |
| const std::string host; |
| const std::string path; |
| const std::string search_term_key; |
| } test_data[] = { |
| {"http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"}, |
| {"http://blah/{searchTerms}", "blah", "", ""}, |
| |
| // No term should result in empty values. |
| {"http://blah/", "", "", ""}, |
| |
| // Multiple terms should result in empty values. |
| {"http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""}, |
| |
| // Term in the host shouldn't match. |
| {"http://{searchTerms}", "", "", ""}, |
| |
| {"http://blah/?q={searchTerms}", "blah", "/", "q"}, |
| {"https://blah/?q={searchTerms}", "blah", "/", "q"}, |
| |
| // Single term with extra chars in value should match. |
| {"http://blah/?q=stock:{searchTerms}", "blah", "/", "q"}, |
| }; |
| |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| TemplateURLData data; |
| data.SetURL(test_data[i].url); |
| TemplateURL url(data); |
| EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_)); |
| EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_)); |
| EXPECT_EQ(test_data[i].search_term_key, |
| url.url_ref().GetSearchTermKey(search_terms_data_)); |
| } |
| } |
| |
| TEST_F(TemplateURLTest, SearchTermKeyLocation) { |
| struct TestData { |
| const std::string url; |
| const url::Parsed::ComponentType location; |
| const std::string path; |
| const std::string key; |
| const std::string value_prefix; |
| const std::string value_suffix; |
| } test_data[] = { |
| {"http://blah/{searchTerms}/", url::Parsed::PATH, "", "", "/", "/"}, |
| {"http://blah/{searchTerms}", url::Parsed::PATH, "", "", "/", ""}, |
| {"http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "", "", |
| "/begin/", "/end"}, |
| {"http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY, "/", "q", |
| "", ""}, |
| {"http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF, "/", "x", |
| "", ""}, |
| {"http://www.example.com/?q=chromium-{searchTerms}@chromium.org/info", |
| url::Parsed::QUERY, "/", "q", "chromium-", "@chromium.org/info"}, |
| |
| // searchTerms is a key, not a value, so this should result in an empty |
| // value. |
| {"http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x", |
| url::Parsed::QUERY, "", "", "", ""}, |
| |
| // Multiple search terms should result in empty values. |
| {"http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY, "", "", |
| "", ""}, |
| {"http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY, "", "", |
| "", ""}, |
| {"http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY, "", |
| "", "", ""}, |
| }; |
| |
| for (size_t i = 0; i < base::size(test_data); ++i) { |
| TemplateURLData data; |
| data.SetURL(test_data[i].url); |
| TemplateURL url(data); |
| EXPECT_EQ(test_data[i].location, |
| url.url_ref().GetSearchTermKeyLocation(search_terms_data_)); |
| EXPECT_EQ(test_data[i].path, |
| url.url_ref().GetPath(search_terms_data_)); |
| EXPECT_EQ(test_data[i].key, |
| url.url_ref().GetSearchTermKey(search_terms_data_)); |
| EXPECT_EQ(test_data[i].value_prefix, |
| url.url_ref().GetSearchTermValuePrefix(search_terms_data_)); |
| EXPECT_EQ(test_data[i].value_suffix, |
| url.url_ref().GetSearchTermValueSuffix(search_terms_data_)); |
| } |
| } |
| |
| TEST_F(TemplateURLTest, GoogleBaseSuggestURL) { |
| static const struct { |
| const char* const base_url; |
| const char* const base_suggest_url; |
| } data[] = { |
| { "http://google.com/", "http://google.com/complete/", }, |
| { "http://www.google.com/", "http://www.google.com/complete/", }, |
| { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", }, |
| { "http://www.google.com.by/", "http://www.google.com.by/complete/", }, |
| { "http://google.com/intl/xx/", "http://google.com/complete/", }, |
| }; |
| |
| for (size_t i = 0; i < base::size(data); ++i) |
| CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url); |
| } |
| |
| TEST_F(TemplateURLTest, ParseParameterKnown) { |
| std::string parsed_url("{searchTerms}"); |
| TemplateURLData data; |
| data.SetURL(parsed_url); |
| TemplateURL url(data); |
| TemplateURLRef::Replacements replacements; |
| EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements)); |
| EXPECT_EQ(std::string(), parsed_url); |
| ASSERT_EQ(1U, replacements.size()); |
| EXPECT_EQ(0U, replacements[0].index); |
| EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| } |
| |
| TEST_F(TemplateURLTest, ParseParameterUnknown) { |
| std::string parsed_url("{fhqwhgads}abc"); |
| TemplateURLData data; |
| data.SetURL(parsed_url); |
| TemplateURL url(data); |
| TemplateURLRef::Replacements replacements; |
| |
| // By default, TemplateURLRef should not consider itself prepopulated. |
| // Therefore we should not replace the unknown parameter. |
| EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); |
| EXPECT_EQ("{fhqwhgads}abc", parsed_url); |
| EXPECT_TRUE(replacements.empty()); |
| |
| // If the TemplateURLRef is prepopulated, we should remove unknown parameters. |
| parsed_url = "{fhqwhgads}abc"; |
| data.prepopulate_id = 1; |
| TemplateURL url2(data); |
| EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); |
| EXPECT_EQ("abc", parsed_url); |
| EXPECT_TRUE(replacements.empty()); |
| } |
| |
| TEST_F(TemplateURLTest, ParseURLEmpty) { |
| TemplateURL url((TemplateURLData())); |
| TemplateURLRef::Replacements replacements; |
| bool valid = false; |
| EXPECT_EQ(std::string(), url.url_ref().ParseURL(std::string(), &replacements, |
| nullptr, &valid)); |
| EXPECT_TRUE(replacements.empty()); |
| EXPECT_TRUE(valid); |
| } |
| |
| TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) { |
| TemplateURLData data; |
| data.SetURL("{"); |
| TemplateURL url(data); |
| TemplateURLRef::Replacements replacements; |
| bool valid = false; |
| EXPECT_EQ(std::string(), |
| url.url_ref().ParseURL("{", &replacements, nullptr, &valid)); |
| EXPECT_TRUE(replacements.empty()); |
| EXPECT_FALSE(valid); |
| } |
| |
| TEST_F(TemplateURLTest, ParseURLNoKnownParameters) { |
| TemplateURLData data; |
| data.SetURL("{}"); |
| TemplateURL url(data); |
| TemplateURLRef::Replacements replacements; |
| bool valid = false; |
| EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, nullptr, &valid)); |
| EXPECT_TRUE(replacements.empty()); |
| EXPECT_TRUE(valid); |
| } |
| |
| TEST_F(TemplateURLTest, ParseURLTwoParameters) { |
| TemplateURLData data; |
| data.SetURL("{}{{%s}}"); |
| TemplateURL url(data); |
| TemplateURLRef::Replacements replacements; |
| bool valid = false; |
| EXPECT_EQ("{}{}", url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, |
| nullptr, &valid)); |
| ASSERT_EQ(1U, replacements.size()); |
| EXPECT_EQ(3U, replacements[0].index); |
| EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| EXPECT_TRUE(valid); |
| } |
| |
| TEST_F(TemplateURLTest, ParseURLNestedParameter) { |
| TemplateURLData data; |
| data.SetURL("{%s"); |
| TemplateURL url(data); |
| TemplateURLRef::Replacements replacements; |
| bool valid = false; |
| EXPECT_EQ("{", url.url_ref().ParseURL("{{searchTerms}", &replacements, |
| nullptr, &valid)); |
| ASSERT_EQ(1U, replacements.size()); |
| EXPECT_EQ(1U, replacements[0].index); |
| EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| EXPECT_TRUE(valid); |
| } |
| |
| TEST_F(TemplateURLTest, SearchClient) { |
| const std::string base_url_str("http://google.com/?"); |
| const std::string terms_str("{searchTerms}&{google:searchClient}"); |
| const std::string full_url_str = base_url_str + terms_str; |
| const std::u16string terms(ASCIIToUTF16(terms_str)); |
| search_terms_data_.set_google_base_url(base_url_str); |
| |
| TemplateURLData data; |
| data.SetURL(full_url_str); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| TemplateURLRef::SearchTermsArgs search_terms_args(u"foobar"); |
| |
| // Check that the URL is correct when a client is not present. |
| GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://google.com/?foobar&", result.spec()); |
| |
| // Check that the URL is correct when a client is present. |
| search_terms_data_.set_search_client("search_client"); |
| GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| ASSERT_TRUE(result_2.is_valid()); |
| EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec()); |
| } |
| |
| TEST_F(TemplateURLTest, SuggestClient) { |
| const std::string base_url_str("http://google.com/?"); |
| const std::string query_params_str("client={google:suggestClient}"); |
| const std::string full_url_str = base_url_str + query_params_str; |
| search_terms_data_.set_google_base_url(base_url_str); |
| |
| TemplateURLData data; |
| data.SetURL(full_url_str); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_FALSE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| TemplateURLRef::SearchTermsArgs search_terms_args; |
| |
| // Check that the URL is correct when a client is not present. |
| GURL result( |
| url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://google.com/?client=", result.spec()); |
| |
| // Check that the URL is correct when a client is present. |
| search_terms_data_.set_suggest_client("suggest_client"); |
| GURL result_2( |
| url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_)); |
| ASSERT_TRUE(result_2.is_valid()); |
| EXPECT_EQ("http://google.com/?client=suggest_client", result_2.spec()); |
| } |
| |
| TEST_F(TemplateURLTest, ZeroSuggestCacheDuration) { |
| const std::string base_url_str("http://google.com/?"); |
| const std::string query_params_str("{google:clientCacheTimeToLive}"); |
| const std::string full_url_str = base_url_str + query_params_str; |
| search_terms_data_.set_google_base_url(base_url_str); |
| TemplateURLData data; |
| data.SetURL(full_url_str); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_FALSE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| |
| { |
| // 'ccttl' query param should not be present if no cache duration is given. |
| TemplateURLRef::SearchTermsArgs search_terms_args; |
| GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://google.com/?", result.spec()); |
| } |
| { |
| // 'ccttl' query param should be present if a positive cache duration is |
| // given. |
| TemplateURLRef::SearchTermsArgs search_terms_args; |
| search_terms_args.zero_suggest_cache_duration_sec = 300; |
| GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://google.com/?ccttl=300&", result.spec()); |
| } |
| { |
| // 'ccttl' query param shouldn't be present if a zero cache duration is |
| // given. |
| TemplateURLRef::SearchTermsArgs search_terms_args; |
| search_terms_args.zero_suggest_cache_duration_sec = 0; |
| GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://google.com/?", result.spec()); |
| } |
| { |
| // 'ccttl' query param should not be present if the request is not a |
| // zero-prefix request. |
| TemplateURLRef::SearchTermsArgs search_terms_args(u"foo"); |
| search_terms_args.zero_suggest_cache_duration_sec = 300; |
| GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| ASSERT_TRUE(result.is_valid()); |
| EXPECT_EQ("http://google.com/?", result.spec()); |
| } |
| } |
| |
| TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) { |
| TemplateURLData data; |
| data.SetURL("http://google.com/?q={searchTerms}"); |
| data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}"); |
| data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}"); |
| TemplateURL url(data); |
| const std::vector<TemplateURLRef>& url_refs = url.url_refs(); |
| ASSERT_EQ(3U, url_refs.size()); |
| EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL()); |
| EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL()); |
| EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL()); |
| } |
| |
| TEST_F(TemplateURLTest, GetURLOnlyOneURL) { |
| TemplateURLData data; |
| data.SetURL("http://www.google.co.uk/"); |
| TemplateURL url(data); |
| const std::vector<TemplateURLRef>& url_refs = url.url_refs(); |
| ASSERT_EQ(1U, url_refs.size()); |
| EXPECT_EQ("http://www.google.co.uk/", url_refs[0].GetURL()); |
| } |
| |
| TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) { |
| TemplateURLData data; |
| data.SetURL("http://google.com/?q={searchTerms}"); |
| data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}"); |
| data.alternate_urls.push_back( |
| "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar"); |
| TemplateURL url(data); |
| std::u16string result; |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/?q=something"), search_terms_data_, &result)); |
| EXPECT_EQ(u"something", result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/?q=something"), search_terms_data_, &result)); |
| EXPECT_EQ(u"something", result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/?q=something"), search_terms_data_, &result)); |
| EXPECT_EQ(u"something", result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/?q=something"), search_terms_data_, &result)); |
| EXPECT_EQ(u"something", result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/alt/#q=something"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(u"something", result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/alt/#q=something"), search_terms_data_, &result)); |
| EXPECT_EQ(u"something", result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/alt/#q=something"), search_terms_data_, &result)); |
| EXPECT_EQ(u"something", result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/alt/#q=something"), search_terms_data_, &result)); |
| EXPECT_EQ(u"something", result); |
| |
| EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.ca/?q=something"), search_terms_data_, &result)); |
| EXPECT_EQ(std::u16string(), result); |
| |
| EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.ca/?q=something&q=anything"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(std::u16string(), result); |
| |
| EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result)); |
| EXPECT_EQ(std::u16string(), result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("https://google.com/?q=foo"), search_terms_data_, &result)); |
| EXPECT_EQ(u"foo", result); |
| |
| EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result)); |
| EXPECT_EQ(std::u16string(), result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result)); |
| EXPECT_EQ(u"1 2 3", result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/alt/?q=123#q=456"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(u"456", result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(u"123", result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL( |
| "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(u"789", result); |
| |
| EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/alt/?q="), search_terms_data_, &result)); |
| EXPECT_EQ(std::u16string(), result); |
| |
| EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/alt/?#q="), search_terms_data_, &result)); |
| EXPECT_EQ(std::u16string(), result); |
| |
| EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result)); |
| EXPECT_EQ(std::u16string(), result); |
| |
| EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result)); |
| EXPECT_EQ(std::u16string(), result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result)); |
| EXPECT_EQ(u"123", result); |
| } |
| |
| TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) { |
| TemplateURLData data; |
| data.SetURL("http://term-in-path.com/begin/{searchTerms}/end"); |
| TemplateURL url(data); |
| std::u16string result; |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://term-in-path.com/begin/something/end"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(u"something", result); |
| |
| // "%20" must be converted to space. |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://term-in-path.com/begin/a%20b%20c/end"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(u"a b c", result); |
| |
| // Plus must not be converted to space. |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://term-in-path.com/begin/1+2+3/end"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(u"1+2+3", result); |
| |
| EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| GURL("http://term-in-path.com/about"), search_terms_data_, &result)); |
| EXPECT_EQ(std::u16string(), result); |
| |
| EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| GURL("http://term-in-path.com/begin"), search_terms_data_, &result)); |
| EXPECT_EQ(std::u16string(), result); |
| |
| EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| GURL("http://term-in-path.com/end"), search_terms_data_, &result)); |
| EXPECT_EQ(std::u16string(), result); |
| } |
| |
| // Checks that the ExtractSearchTermsFromURL function works correctly |
| // for urls containing non-latin characters in UTF8 encoding. |
| TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) { |
| TemplateURLData data; |
| data.SetURL("http://utf-8.ru/?q={searchTerms}"); |
| data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}"); |
| data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}"); |
| TemplateURL url(data); |
| std::u16string result; |
| |
| // Russian text encoded with UTF-8. |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82" |
| "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"), |
| search_terms_data_, &result)); |
| EXPECT_EQ( |
| u"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, " |
| u"\x043C\x0438\x0440!", |
| result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2" |
| "%D0%B0"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(u"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430", result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20" |
| "%D0%B8%20A"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(u"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A", result); |
| } |
| |
| // Checks that the ExtractSearchTermsFromURL function works correctly |
| // for urls containing non-latin characters in non-UTF8 encoding. |
| TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) { |
| TemplateURLData data; |
| data.SetURL("http://windows-1251.ru/?q={searchTerms}"); |
| data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}"); |
| data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}"); |
| data.input_encodings.push_back("windows-1251"); |
| TemplateURL url(data); |
| std::u16string result; |
| |
| // Russian text encoded with Windows-1251. |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+" |
| "%EC%E8%F0!"), |
| search_terms_data_, &result)); |
| EXPECT_EQ( |
| u"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, " |
| u"\x043C\x0438\x0440!", |
| result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(u"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430", result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(u"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A", result); |
| } |
| |
| // Checks that the ExtractSearchTermsFromURL function strips constant |
| // prefix/suffix strings from the search terms param. |
| TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) { |
| TemplateURLData data; |
| data.alternate_urls.push_back( |
| "http://www.example.com/?q=chromium-{searchTerms}@chromium.org"); |
| data.alternate_urls.push_back( |
| "http://www.example.com/chromium-{searchTerms}@chromium.org/info"); |
| TemplateURL url(data); |
| std::u16string result; |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://www.example.com/[email protected]"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(u"dev", result); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://www.example.com/[email protected]/info"), |
| search_terms_data_, &result)); |
| EXPECT_EQ(u"dev", result); |
| |
| // Don't match if the prefix and suffix aren't there. |
| EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| GURL("http://www.example.com/?q=invalid"), search_terms_data_, &result)); |
| |
| // Don't match if the prefix and suffix overlap. |
| TemplateURLData data_with_overlap; |
| data.alternate_urls.push_back( |
| "http://www.example.com/?q=goo{searchTerms}oogle"); |
| TemplateURL url_with_overlap(data); |
| EXPECT_FALSE(url_with_overlap.ExtractSearchTermsFromURL( |
| GURL("http://www.example.com/?q=google"), search_terms_data_, &result)); |
| } |
| |
| TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) { |
| TemplateURLData data; |
| data.SetURL("http://google.com/?q={searchTerms}"); |
| data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}"); |
| data.alternate_urls.push_back( |
| "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar"); |
| TemplateURL url(data); |
| TemplateURLRef::SearchTermsArgs search_terms(u"Bob Morane"); |
| GURL result; |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://google.com/?q=something"), search_terms, |
| search_terms_data_, &result)); |
| EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result); |
| |
| result = GURL("http://should.not.change.com"); |
| EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| GURL("http://google.ca/?q=something"), search_terms, |
| search_terms_data_, &result)); |
| EXPECT_EQ(GURL("http://should.not.change.com"), result); |
| |
| EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| GURL("http://google.com/foo/?q=foo"), search_terms, |
| search_terms_data_, &result)); |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("https://google.com/?q=foo"), search_terms, |
| search_terms_data_, &result)); |
| EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result); |
| |
| EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| GURL("http://google.com:8080/?q=foo"), search_terms, |
| search_terms_data_, &result)); |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://google.com/?q=1+2+3&b=456"), search_terms, |
| search_terms_data_, &result)); |
| EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result); |
| |
| // Note: Spaces in REF parameters are not escaped. See TryEncoding() in |
| // template_url.cc for details. |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://google.com/alt/?q=123#q=456"), search_terms, |
| search_terms_data_, &result)); |
| EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result); |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms, |
| search_terms_data_, &result)); |
| EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"), |
| result); |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), |
| search_terms, search_terms_data_, &result)); |
| EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456" |
| "#j=abc&q=Bob+Morane&h=def9"), result); |
| |
| EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| GURL("http://google.com/alt/?q="), search_terms, |
| search_terms_data_, &result)); |
| |
| EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| GURL("http://google.com/alt/?#q="), search_terms, |
| search_terms_data_, &result)); |
| |
| EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| GURL("http://google.com/alt/?q=#q="), search_terms, |
| search_terms_data_, &result)); |
| |
| EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| GURL("http://google.com/alt/?q=123#q="), search_terms, |
| search_terms_data_, &result)); |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://google.com/alt/?q=#q=123"), search_terms, |
| search_terms_data_, &result)); |
| EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result); |
| } |
| |
| TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) { |
| TemplateURLData data; |
| data.SetURL("http://term-in-path.com/begin/{searchTerms}/end"); |
| TemplateURL url(data); |
| TemplateURLRef::SearchTermsArgs search_terms(u"Bob Morane"); |
| GURL result; |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://term-in-path.com/begin/something/end"), search_terms, |
| search_terms_data_, &result)); |
| EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result); |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms, |
| search_terms_data_, &result)); |
| EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result); |
| |
| result = GURL("http://should.not.change.com"); |
| EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| GURL("http://term-in-path.com/about"), search_terms, |
| search_terms_data_, &result)); |
| EXPECT_EQ(GURL("http://should.not.change.com"), result); |
| } |
| |
| // Checks that the ReplaceSearchTermsInURL function works correctly |
| // for search terms containing non-latin characters for a search engine |
| // using UTF-8 input encoding. |
| TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) { |
| TemplateURLData data; |
| data.SetURL("http://utf-8.ru/?q={searchTerms}"); |
| data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}"); |
| data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}"); |
| TemplateURL url(data); |
| |
| // Russian text which will be encoded with UTF-8. |
| TemplateURLRef::SearchTermsArgs search_terms( |
| u"\x0442\x0435\x043A\x0441\x0442"); |
| GURL result; |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_, |
| &result)); |
| EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"), |
| result); |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_, |
| &result)); |
| EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"), |
| result); |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_, |
| &result)); |
| EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"), |
| result); |
| } |
| |
| // Checks that the ReplaceSearchTermsInURL function works correctly |
| // for search terms containing non-latin characters for a search engine |
| // using non UTF-8 input encoding. |
| TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) { |
| TemplateURLData data; |
| data.SetURL("http://windows-1251.ru/?q={searchTerms}"); |
| data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}"); |
| data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}"); |
| data.input_encodings.push_back("windows-1251"); |
| TemplateURL url(data); |
| |
| // Russian text which will be encoded with Windows-1251. |
| TemplateURLRef::SearchTermsArgs search_terms( |
| u"\x0442\x0435\x043A\x0441\x0442"); |
| GURL result; |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_, |
| &result)); |
| EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"), |
| result); |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_, |
| &result)); |
| EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"), |
| result); |
| |
| EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| GURL("http://windows-1251.ru/path/a%20b"), search_terms, |
| search_terms_data_, &result)); |
| EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"), |
| result); |
| } |
| |
| // Test the |additional_query_params| field of SearchTermsArgs. |
| TEST_F(TemplateURLTest, SuggestQueryParams) { |
| TemplateURLData data; |
| // Pick a URL with replacements before, during, and after the query, to ensure |
| // we don't goof up any of them. |
| data.SetURL("{google:baseURL}search?q={searchTerms}" |
| "#{google:originalQueryForSuggestion}x"); |
| TemplateURL url(data); |
| |
| // Baseline: no |additional_query_params| field. |
| TemplateURLRef::SearchTermsArgs search_terms(u"abc"); |
| search_terms.original_query = u"def"; |
| search_terms.accepted_suggestion = 0; |
| EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", |
| url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
| |
| // Set the |additional_query_params|. |
| search_terms.additional_query_params = "pq=xyz"; |
| EXPECT_EQ("http://www.google.com/search?pq=xyz&q=abc#oq=def&x", |
| url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
| |
| // Add |append_extra_query_params_from_command_line| into the mix, and ensure |
| // it works. |
| search_terms.append_extra_query_params_from_command_line = true; |
| base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| switches::kExtraSearchQueryParams, "a=b"); |
| EXPECT_EQ("http://www.google.com/search?a=b&pq=xyz&q=abc#oq=def&x", |
| url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
| } |
| |
| // Test the |search_terms.append_extra_query_params_from_command_line| field of |
| // SearchTermsArgs. |
| TEST_F(TemplateURLTest, ExtraQueryParams) { |
| TemplateURLData data; |
| // Pick a URL with replacements before, during, and after the query, to ensure |
| // we don't goof up any of them. |
| data.SetURL("{google:baseURL}search?q={searchTerms}" |
| "#{google:originalQueryForSuggestion}x"); |
| TemplateURL url(data); |
| |
| // Baseline: no command-line args, no |
| // |search_terms.append_extra_query_params_from_command_line| flag. |
| TemplateURLRef::SearchTermsArgs search_terms(u"abc"); |
| search_terms.original_query = u"def"; |
| search_terms.accepted_suggestion = 0; |
| EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", |
| url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
| |
| // Set the flag. Since there are no command-line args, this should have no |
| // effect. |
| search_terms.append_extra_query_params_from_command_line = true; |
| EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", |
| url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
| |
| // Now append the command-line arg. This should be inserted into the query. |
| base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| switches::kExtraSearchQueryParams, "a=b"); |
| EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x", |
| url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
| |
| // Turn off the flag. Now the command-line arg should be ignored again. |
| search_terms.append_extra_query_params_from_command_line = false; |
| EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", |
| url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
| } |
| |
| // Tests replacing pageClassification. |
| TEST_F(TemplateURLTest, ReplacePageClassification) { |
| TemplateURLData data; |
| data.input_encodings.push_back("UTF-8"); |
| data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}"); |
| TemplateURL url(data); |
| EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| TemplateURLRef::SearchTermsArgs search_terms_args(u"foo"); |
| |
| std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_); |
| EXPECT_EQ("http://www.google.com/?q=foo", result); |
| |
| search_terms_args.page_classification = metrics::OmniboxEventProto::NTP; |
| result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_); |
| EXPECT_EQ(base::FeatureList::IsEnabled(omnibox::kZeroSuggestPrefetching) |
| ? "http://www.google.com/?q=foo" |
| : "http://www.google.com/?pgcl=1&q=foo", |
| result); |
| |
| search_terms_args.page_classification = |
| metrics::OmniboxEventProto::HOME_PAGE; |
| result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_); |
| EXPECT_EQ(base::FeatureList::IsEnabled(omnibox::kZeroSuggestPrefetching) |
| ? "http://www.google.com/?q=foo" |
| : "http://www.google.com/?pgcl=3&q=foo", |
| result); |
| } |
| |
| // Test the IsSearchResults function. |
| TEST_F(TemplateURLTest, IsSearchResults) { |
| TemplateURLData data; |
| data.SetURL("http://bar/search?q={searchTerms}"); |
| data.new_tab_url = "http://bar/newtab"; |
| data.alternate_urls.push_back("http://bar/?q={searchTerms}"); |
| data.alternate_urls.push_back("http://bar/#q={searchTerms}"); |
| data.alternate_urls.push_back("http://bar/search#q{searchTerms}"); |
| data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}"); |
| TemplateURL search_provider(data); |
| |
| const struct { |
| const char* const url; |
| bool result; |
| } url_data[] = { |
| { "http://bar/search?q=foo&oq=foo", true, }, |
| { "http://bar/?q=foo&oq=foo", true, }, |
| { "http://bar/#output=search&q=foo&oq=foo", true, }, |
| { "http://bar/webhp#q=foo&oq=foo", true, }, |
| { "http://bar/#q=foo&oq=foo", true, }, |
| { "http://bar/?ext=foo&q=foo#ref=bar", true, }, |
| { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, }, |
| { "http://bar/", false, }, |
| { "http://foo/", false, }, |
| { "http://bar/newtab", false, }, |
| }; |
| |
| for (size_t i = 0; i < base::size(url_data); ++i) { |
| EXPECT_EQ(url_data[i].result, |
| search_provider.IsSearchURL(GURL(url_data[i].url), |
| search_terms_data_)); |
| } |
| } |
| |
| TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) { |
| TemplateURLData data; |
| search_terms_data_.set_google_base_url("http://bar/"); |
| data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t"); |
| |
| TemplateURL url(data); |
| TemplateURLRef::SearchTermsArgs search_terms_args(u"foo"); |
| std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_); |
| EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result); |
| } |
| |
| TEST_F(TemplateURLTest, SessionToken) { |
| TemplateURLData data; |
| search_terms_data_.set_google_base_url("http://bar/"); |
| data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t"); |
| |
| TemplateURL url(data); |
| TemplateURLRef::SearchTermsArgs search_terms_args(u"foo"); |
| search_terms_args.session_token = "SESSIONTOKENGOESHERE"; |
| std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_); |
| EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result); |
| |
| TemplateURL url2(data); |
| search_terms_args.session_token = ""; |
| result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_); |
| EXPECT_EQ("http://bar/search?q=foo&xssi=t", result); |
| } |
| |
| TEST_F(TemplateURLTest, ContextualSearchParameters) { |
| TemplateURLData data; |
| search_terms_data_.set_google_base_url("http://bar/"); |
| data.SetURL("http://bar/_/contextualsearch?" |
| "{google:contextualSearchVersion}" |
| "{google:contextualSearchContextData}"); |
| |
| TemplateURL url(data); |
| TemplateURLRef::SearchTermsArgs search_terms_args(u"foo"); |
| std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_); |
| EXPECT_EQ("http://bar/_/contextualsearch?", result); |
| |
| // Test the current common case, which uses no home country or previous |
| // event. |
| TemplateURLRef::SearchTermsArgs::ContextualSearchParams params( |
| 2, 1, std::string(), 0, 0, false, std::string(), std::string(), |
| std::string(), std::string()); |
| search_terms_args.contextual_search_params = params; |
| result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_); |
| EXPECT_EQ( |
| "http://bar/_/contextualsearch?" |
| "ctxs=2&" |
| "ctxsl_coca=1", |
| result); |
| |
| // Test the home country and non-zero event data case. |
| search_terms_args.contextual_search_params = |
| TemplateURLRef::SearchTermsArgs::ContextualSearchParams( |
| 2, 2, "CH", 1657713458, 5, false, std::string(), std::string(), |
| std::string(), std::string()); |
| result = |
| url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); |
| |
| EXPECT_EQ( |
| "http://bar/_/contextualsearch?" |
| "ctxs=2&" |
| "ctxsl_coca=2&" |
| "ctxs_hc=CH&" |
| "ctxsl_pid=1657713458&" |
| "ctxsl_per=5", |
| result); |
| |
| // Test exact-search. |
| search_terms_args.contextual_search_params = |
| TemplateURLRef::SearchTermsArgs::ContextualSearchParams( |
| 2, 1, std::string(), 0, 0, true, std::string(), std::string(), |
| std::string(), std::string()); |
| result = |
| url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); |
| // Find our param. |
| size_t found_pos = result.find("ctxsl_exact=1"); |
| EXPECT_NE(found_pos, std::string::npos); |
| |
| // Test source and target languages. |
| search_terms_args.contextual_search_params = |
| TemplateURLRef::SearchTermsArgs::ContextualSearchParams( |
| 2, 1, std::string(), 0, 0, true, "es", "de", std::string(), |
| std::string()); |
| result = |
| url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); |
| // Find our params. |
| size_t source_pos = result.find("tlitesl=es"); |
| EXPECT_NE(source_pos, std::string::npos); |
| size_t target_pos = result.find("tlitetl=de"); |
| EXPECT_NE(target_pos, std::string::npos); |
| |
| // Test fluent languages. |
| search_terms_args.contextual_search_params = |
| TemplateURLRef::SearchTermsArgs::ContextualSearchParams( |
| 2, 1, std::string(), 0, 0, true, std::string(), std::string(), |
| "es,de", std::string()); |
| result = |
| url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); |
| // Find our param. These may actually be URL encoded. |
| size_t fluent_pos = result.find("&ctxs_fls=es,de"); |
| EXPECT_NE(fluent_pos, std::string::npos); |
| |
| // Test Related Searches. |
| search_terms_args.contextual_search_params = |
| TemplateURLRef::SearchTermsArgs::ContextualSearchParams( |
| 2, 1, std::string(), 0, 0, true, std::string(), std::string(), |
| std::string(), "1RbCu"); |
| result = |
| url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); |
| // Find our param. |
| size_t ctxsl_rs_pos = result.find("&ctxsl_rs=1RbCu"); |
| EXPECT_NE(ctxsl_rs_pos, std::string::npos); |
| } |
| |
| TEST_F(TemplateURLTest, GenerateKeyword) { |
| ASSERT_EQ(u"foo", TemplateURL::GenerateKeyword(GURL("http://foo"))); |
| ASSERT_EQ(u"foo.", TemplateURL::GenerateKeyword(GURL("http://foo."))); |
| // www. should be stripped for a public hostname but not a private/intranet |
| // hostname. |
| ASSERT_EQ(u"google.com", |
| TemplateURL::GenerateKeyword(GURL("http://www.google.com"))); |
| ASSERT_EQ(u"www.foo", TemplateURL::GenerateKeyword(GURL("http://www.foo"))); |
| // Make sure we don't get a trailing '/'. |
| ASSERT_EQ(u"blah", TemplateURL::GenerateKeyword(GURL("http://blah/"))); |
| // Don't generate the empty string. |
| ASSERT_EQ(u"www.", TemplateURL::GenerateKeyword(GURL("http://www."))); |
| ASSERT_EQ(u"абв", TemplateURL::GenerateKeyword(GURL("http://xn--80acd"))); |
| |
| // Generated keywords must always be in lowercase, because TemplateURLs always |
| // converts keywords to lowercase in its constructor and TemplateURLService |
| // stores TemplateURLs in maps using keyword as key. |
| EXPECT_TRUE(IsLowerCase(TemplateURL::GenerateKeyword(GURL("http://BLAH/")))); |
| EXPECT_TRUE(IsLowerCase( |
| TemplateURL::GenerateKeyword(GURL("http://embeddedhtml.<head>/")))); |
| } |
| |
| TEST_F(TemplateURLTest, GenerateSearchURL) { |
| struct GenerateSearchURLCase { |
| const char* test_name; |
| const char* url; |
| const char* expected; |
| } generate_url_cases[] = { |
| { "invalid URL", "foo{searchTerms}", "" }, |
| { "URL with no replacements", "http://foo/", "http://foo/" }, |
| { "basic functionality", "http://foo/{searchTerms}", |
| "http://foo/blah.blah.blah.blah.blah" } |
| }; |
| |
| for (size_t i = 0; i < base::size(generate_url_cases); ++i) { |
| TemplateURLData data; |
| data.SetURL(generate_url_cases[i].url); |
| TemplateURL t_url(data); |
| EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(), |
| generate_url_cases[i].expected) |
| << generate_url_cases[i].test_name << " failed."; |
| } |
| } |
| |
| TEST_F(TemplateURLTest, PrefetchQueryParameters) { |
| TemplateURLData data; |
| search_terms_data_.set_google_base_url("http://bar/"); |
| data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t"); |
| |
| TemplateURL url(data); |
| TemplateURLRef::SearchTermsArgs search_terms_args(u"foo"); |
| search_terms_args.prefetch_query = "full query text"; |
| search_terms_args.prefetch_query_type = "2338"; |
| std::string result = |
| url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); |
| EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t", |
| result); |
| |
| TemplateURL url2(data); |
| search_terms_args.prefetch_query.clear(); |
| search_terms_args.prefetch_query_type.clear(); |
| result = |
| url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); |
| EXPECT_EQ("http://bar/search?q=foo&xssi=t", result); |
| } |
| |
| // Tests that TemplateURL works correctly after changing the Google base URL |
| // and invalidating cached values. |
| TEST_F(TemplateURLTest, InvalidateCachedValues) { |
| TemplateURLData data; |
| data.SetURL("{google:baseURL}search?q={searchTerms}"); |
| data.suggestions_url = "{google:baseSuggestURL}search?q={searchTerms}"; |
| data.image_url = "{google:baseURL}searchbyimage/upload"; |
| data.new_tab_url = "{google:baseURL}_/chrome/newtab"; |
| data.contextual_search_url = "{google:baseURL}_/contextualsearch"; |
| data.alternate_urls.push_back("{google:baseURL}s#q={searchTerms}"); |
| TemplateURL url(data); |
| TemplateURLRef::SearchTermsArgs search_terms_args(u"X"); |
| std::u16string search_terms; |
| |
| EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_)); |
| EXPECT_EQ("http://www.google.com/search?q=X", |
| url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| EXPECT_EQ("http://www.google.com/s#q=X", |
| url.url_refs()[0].ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| EXPECT_EQ("http://www.google.com/search?q=X", |
| url.url_refs()[1].ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| EXPECT_EQ("http://www.google.com/complete/search?q=X", |
| url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| EXPECT_EQ("http://www.google.com/searchbyimage/upload", |
| url.image_url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| EXPECT_EQ("http://www.google.com/_/chrome/newtab", |
| url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| EXPECT_EQ("http://www.google.com/_/contextualsearch", |
| url.contextual_search_url_ref().ReplaceSearchTerms( |
| search_terms_args, search_terms_data_)); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://www.google.com/search?q=Y+Z"), |
| search_terms_data_, &search_terms)); |
| EXPECT_EQ(u"Y Z", search_terms); |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("http://www.google.com/s#q=123"), |
| search_terms_data_, &search_terms)); |
| EXPECT_EQ(u"123", search_terms); |
| |
| search_terms_data_.set_google_base_url("https://www.foo.org/"); |
| url.InvalidateCachedValues(); |
| |
| EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_)); |
| EXPECT_EQ("https://www.foo.org/search?q=X", |
| url.url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| EXPECT_EQ("https://www.foo.org/s#q=X", |
| url.url_refs()[0].ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| EXPECT_EQ("https://www.foo.org/search?q=X", |
| url.url_refs()[1].ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| EXPECT_EQ("https://www.foo.org/complete/search?q=X", |
| url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| EXPECT_EQ("https://www.foo.org/searchbyimage/upload", |
| url.image_url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| EXPECT_EQ("https://www.foo.org/_/chrome/newtab", |
| url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args, |
| search_terms_data_)); |
| EXPECT_EQ("https://www.foo.org/_/contextualsearch", |
| url.contextual_search_url_ref().ReplaceSearchTerms( |
| search_terms_args, search_terms_data_)); |
| |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("https://www.foo.org/search?q=Y+Z"), |
| search_terms_data_, &search_terms)); |
| EXPECT_EQ(u"Y Z", search_terms); |
| EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| GURL("https://www.foo.org/s#q=123"), |
| search_terms_data_, &search_terms)); |
| EXPECT_EQ(u"123", search_terms); |
| |
| search_terms_data_.set_google_base_url("http://www.google.com/"); |
| } |
| |
| // Tests the use of wildcards in the path to ensure both extracting search terms |
| // and generating a search URL work correctly. |
| TEST_F(TemplateURLTest, PathWildcard) { |
| TemplateURLData data; |
| data.SetURL( |
| "https://www.google.com/search{google:pathWildcard}?q={searchTerms}"); |
| TemplateURL url(data); |
| |
| // Test extracting search terms from a URL. |
| std::u16string search_terms; |
| url.ExtractSearchTermsFromURL(GURL("https://www.google.com/search?q=testing"), |
| search_terms_data_, &search_terms); |
| EXPECT_EQ(u"testing", search_terms); |
| url.ExtractSearchTermsFromURL( |
| GURL("https://www.google.com/search;_this_is_a_test;_?q=testing"), |
| search_terms_data_, &search_terms); |
| EXPECT_EQ(u"testing", search_terms); |
| |
| // Tests overlapping prefix/suffix. |
| data.SetURL( |
| "https://www.google.com/search{google:pathWildcard}rch?q={searchTerms}"); |
| TemplateURL overlap_url(data); |
| overlap_url.ExtractSearchTermsFromURL( |
| GURL("https://www.google.com/search?q=testing"), search_terms_data_, |
| &search_terms); |
| EXPECT_TRUE(search_terms.empty()); |
| |
| // Tests wildcard at beginning of path so we only have a suffix. |
| data.SetURL( |
| "https://www.google.com/{google:pathWildcard}rch?q={searchTerms}"); |
| TemplateURL suffix_url(data); |
| suffix_url.ExtractSearchTermsFromURL( |
| GURL("https://www.google.com/search?q=testing"), search_terms_data_, |
| &search_terms); |
| EXPECT_EQ(u"testing", search_terms); |
| |
| // Tests wildcard between prefix/suffix. |
| overlap_url.ExtractSearchTermsFromURL( |
| GURL("https://www.google.com/search_testing_rch?q=testing"), |
| search_terms_data_, &search_terms); |
| EXPECT_EQ(u"testing", search_terms); |
| |
| // Test generating a URL. |
| TemplateURLRef::SearchTermsArgs search_terms_args(u"foo"); |
| GURL generated_url; |
| url.ReplaceSearchTermsInURL(url.GenerateSearchURL(search_terms_data_), |
| search_terms_args, search_terms_data_, |
| &generated_url); |
| EXPECT_EQ("https://www.google.com/search?q=foo", generated_url.spec()); |
| } |