[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 5 | #include "base/base_paths.h" |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 6 | #include "base/command_line.h" |
[email protected] | 2f3bc651 | 2013-08-28 03:56:27 | [diff] [blame] | 7 | #include "base/strings/string_number_conversions.h" |
[email protected] | 24a555b6 | 2013-06-10 22:01:17 | [diff] [blame] | 8 | #include "base/strings/string_util.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
[email protected] | d54e03a5 | 2009-01-16 00:31:04 | [diff] [blame] | 10 | #include "chrome/browser/search_engines/template_url.h" |
[email protected] | 332d17d2 | 2014-06-20 16:56:03 | [diff] [blame] | 11 | #include "components/metrics/proto/omnibox_event.pb.h" |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 12 | #include "components/metrics/proto/omnibox_input_type.pb.h" |
[email protected] | 5c3c6e48 | 2014-06-23 09:47:18 | [diff] [blame] | 13 | #include "components/search_engines/search_engines_switches.h" |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 14 | #include "components/search_engines/search_terms_data.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 17 | using base::ASCIIToUTF16; |
| 18 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 19 | // TestSearchTermsData -------------------------------------------------------- |
| 20 | |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 21 | // Simple implementation of SearchTermsData. |
| 22 | class TestSearchTermsData : public SearchTermsData { |
| 23 | public: |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 24 | explicit TestSearchTermsData(const std::string& google_base_url); |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 25 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 26 | virtual std::string GoogleBaseURLValue() const OVERRIDE; |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 27 | virtual base::string16 GetRlzParameterValue( |
| 28 | bool from_app_list) const OVERRIDE; |
| 29 | virtual std::string GetSearchClient() const OVERRIDE; |
[email protected] | 1020fead | 2014-06-20 13:40:28 | [diff] [blame] | 30 | virtual std::string GoogleImageSearchSource() const OVERRIDE; |
[email protected] | 5c3c6e48 | 2014-06-23 09:47:18 | [diff] [blame] | 31 | virtual bool EnableAnswersInSuggest() const OVERRIDE; |
| 32 | virtual bool IsShowingSearchTermsOnSearchResultsPages() const OVERRIDE; |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 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 | } |
[email protected] | 5c3c6e48 | 2014-06-23 09:47:18 | [diff] [blame] | 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 | } |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | std::string google_base_url_; |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 49 | std::string search_client_; |
[email protected] | 5c3c6e48 | 2014-06-23 09:47:18 | [diff] [blame] | 50 | bool enable_answers_in_suggest_; |
| 51 | bool is_showing_search_terms_on_search_results_pages_; |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 52 | |
| 53 | DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData); |
| 54 | }; |
| 55 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 56 | TestSearchTermsData::TestSearchTermsData(const std::string& google_base_url) |
[email protected] | 5c3c6e48 | 2014-06-23 09:47:18 | [diff] [blame] | 57 | : google_base_url_(google_base_url), |
| 58 | enable_answers_in_suggest_(false), |
| 59 | is_showing_search_terms_on_search_results_pages_(false) { |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | std::string TestSearchTermsData::GoogleBaseURLValue() const { |
| 63 | return google_base_url_; |
| 64 | } |
| 65 | |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 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 | |
[email protected] | 1020fead | 2014-06-20 13:40:28 | [diff] [blame] | 76 | std::string TestSearchTermsData::GoogleImageSearchSource() const { |
| 77 | return "google_image_search_source"; |
| 78 | } |
| 79 | |
[email protected] | 5c3c6e48 | 2014-06-23 09:47:18 | [diff] [blame] | 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 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 88 | // TemplateURLTest ------------------------------------------------------------ |
| 89 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 90 | class TemplateURLTest : public testing::Test { |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 91 | public: |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 92 | TemplateURLTest() : search_terms_data_("http://www.google.com/") {} |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 93 | void CheckSuggestBaseURL(const std::string& base_url, |
| 94 | const std::string& base_suggest_url) const; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 95 | |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 96 | TestSearchTermsData search_terms_data_; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 97 | }; |
| 98 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 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 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 109 | TEST_F(TemplateURLTest, Defaults) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 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); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST_F(TemplateURLTest, TestValidWithComplete) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 117 | TemplateURLData data; |
| 118 | data.SetURL("{searchTerms}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 119 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 120 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST_F(TemplateURLTest, URLRefTestSearchTerms) { |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 124 | struct SearchTermsCase { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 125 | const char* url; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 126 | const base::string16 terms; |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 127 | const std::string output; |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 128 | } search_term_cases[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 129 | { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 130 | "http://foosea%20rch/bar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 131 | { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 132 | "http://foosea%20rch/bar?boo=abc" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 133 | { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 134 | "http://foo/?boo=sea+rch%2Fbar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 135 | { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 136 | "http://en.wikipedia.org/wiki/%3F" } |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 137 | }; |
| 138 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { |
| 139 | const SearchTermsCase& value = search_term_cases[i]; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 140 | TemplateURLData data; |
| 141 | data.SetURL(value.url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 142 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 143 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 144 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 145 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 146 | TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data_)); |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 147 | ASSERT_TRUE(result.is_valid()); |
| 148 | EXPECT_EQ(value.output, result.spec()); |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 149 | } |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | TEST_F(TemplateURLTest, URLRefTestCount) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 153 | TemplateURLData data; |
| 154 | data.SetURL("http://foo{searchTerms}{count?}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 155 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 156 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 157 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 158 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 159 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 160 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 161 | EXPECT_EQ("http://foox/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | TEST_F(TemplateURLTest, URLRefTestCount2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 165 | TemplateURLData data; |
| 166 | data.SetURL("http://foo{searchTerms}{count}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 167 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 168 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 169 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 170 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 171 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 172 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 173 | EXPECT_EQ("http://foox10/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | TEST_F(TemplateURLTest, URLRefTestIndices) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 177 | TemplateURLData data; |
| 178 | data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 179 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 180 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 181 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 182 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 183 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 184 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 185 | EXPECT_EQ("http://fooxxy/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | TEST_F(TemplateURLTest, URLRefTestIndices2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 189 | TemplateURLData data; |
| 190 | data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 191 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 192 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 193 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 194 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 195 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 196 | ASSERT_TRUE(result.is_valid()); |
[email protected] | 405aae2 | 2012-03-29 20:36:13 | [diff] [blame] | 197 | EXPECT_EQ("http://fooxx1y1/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | TEST_F(TemplateURLTest, URLRefTestEncoding) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 201 | TemplateURLData data; |
| 202 | data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 203 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 204 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 205 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 206 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 207 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 208 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 209 | EXPECT_EQ("http://fooxxutf-8ya/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 210 | } |
| 211 | |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 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=," |
[email protected] | 2f3bc651 | 2013-08-28 03:56:27 | [diff] [blame] | 221 | "constant_param=constant,width={google:imageOriginalWidth}"; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 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; |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 229 | TemplateURL url_bad(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 230 | ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_)); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 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; |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 241 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 242 | ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_)); |
| 243 | ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 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"); |
[email protected] | 2f3bc651 | 2013-08-28 03:56:27 | [diff] [blame] | 249 | search_args.image_original_size = gfx::Size(10, 10); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 250 | // Replacement operation with no post_data buffer should still return |
| 251 | // the parsed URL. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 252 | TestSearchTermsData search_terms_data("http://X"); |
| 253 | GURL result(url.image_url_ref().ReplaceSearchTerms( |
| 254 | search_args, search_terms_data)); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 255 | ASSERT_TRUE(result.is_valid()); |
| 256 | EXPECT_EQ(KImageSearchURL, result.spec()); |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 257 | TemplateURLRef::PostContent post_content; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 258 | result = GURL(url.image_url_ref().ReplaceSearchTerms( |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 259 | search_args, search_terms_data, &post_content)); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 260 | ASSERT_TRUE(result.is_valid()); |
| 261 | EXPECT_EQ(KImageSearchURL, result.spec()); |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 262 | ASSERT_FALSE(post_content.first.empty()); |
| 263 | ASSERT_FALSE(post_content.second.empty()); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 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_; |
[email protected] | 2f3bc651 | 2013-08-28 03:56:27 | [diff] [blame] | 270 | EXPECT_EQ(7U, post_params.size()); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 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) { |
[email protected] | 2f3bc651 | 2013-08-28 03:56:27 | [diff] [blame] | 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; |
[email protected] | 1020fead | 2014-06-20 13:40:28 | [diff] [blame] | 284 | case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE: |
| 285 | EXPECT_EQ("sbisrc", i->first); |
| 286 | EXPECT_EQ(search_terms_data.GoogleImageSearchSource(), i->second); |
| 287 | break; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 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") { |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 311 | EXPECT_FALSE(i->second.empty()); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 312 | } else { |
| 313 | EXPECT_EQ("constant_param", i->first); |
| 314 | EXPECT_EQ("constant", i->second); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
[email protected] | d88cb20 | 2011-08-17 20:03:01 | [diff] [blame] | 319 | // Test that setting the prepopulate ID from TemplateURL causes the stored |
| 320 | // TemplateURLRef to handle parsing the URL parameters differently. |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 321 | TEST_F(TemplateURLTest, SetPrepopulatedAndParse) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 322 | TemplateURLData data; |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 323 | data.SetURL("http://foo{fhqwhgads}bar"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 324 | TemplateURL url(data); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 325 | TemplateURLRef::Replacements replacements; |
| 326 | bool valid = false; |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 327 | EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL( |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 328 | "http://foo{fhqwhgads}bar", &replacements, NULL, &valid)); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 329 | EXPECT_TRUE(replacements.empty()); |
| 330 | EXPECT_TRUE(valid); |
| 331 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 332 | data.prepopulate_id = 123; |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 333 | TemplateURL url2(data); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 334 | EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar", |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 335 | &replacements, NULL, |
| 336 | &valid)); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 337 | EXPECT_TRUE(replacements.empty()); |
| 338 | EXPECT_TRUE(valid); |
| 339 | } |
| 340 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 341 | TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 342 | TemplateURLData data; |
| 343 | data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 344 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 345 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 346 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 347 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 348 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 349 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 350 | EXPECT_EQ("http://fooxutf-8axyb/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | TEST_F(TemplateURLTest, URLRefTestEncoding2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 354 | TemplateURLData data; |
| 355 | data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 356 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 357 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 358 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 359 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 360 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 361 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 362 | EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 363 | } |
| 364 | |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 365 | TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) { |
| 366 | struct SearchTermsCase { |
| 367 | const char* url; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 368 | const base::string16 terms; |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 369 | const char* output; |
| 370 | } search_term_cases[] = { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 371 | { "{google:baseURL}{language}{searchTerms}", base::string16(), |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 372 | "http://example.com/e/en" }, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 373 | { "{google:baseSuggestURL}{searchTerms}", base::string16(), |
[email protected] | 014010e | 2011-10-01 04:12:44 | [diff] [blame] | 374 | "http://example.com/complete/" } |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 375 | }; |
| 376 | |
| 377 | TestSearchTermsData search_terms_data("http://example.com/e/"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 378 | TemplateURLData data; |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 379 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { |
| 380 | const SearchTermsCase& value = search_term_cases[i]; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 381 | data.SetURL(value.url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 382 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 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( |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 386 | TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL)); |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 387 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 388 | EXPECT_EQ(value.output, result.spec()); |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 392 | TEST_F(TemplateURLTest, URLRefTermToWide) { |
| 393 | struct ToWideCase { |
| 394 | const char* encoded_search_term; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 395 | const base::string16 expected_decoded_term; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 396 | } to_wide_cases[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 397 | {"hello+world", ASCIIToUTF16("hello world")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 398 | // Test some big-5 input. |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 399 | {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 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. |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 403 | {"%e4%bd%a05%e5%a5%bd+to+you", |
| 404 | base::WideToUTF16(L"\x4f60\x35\x597d to you")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 405 | // Undecodable input should stay escaped. |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 406 | {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")}, |
[email protected] | 7df4348 | 2009-07-31 19:37:44 | [diff] [blame] | 407 | // Make sure we convert %2B to +. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 408 | {"C%2B%2B", ASCIIToUTF16("C++")}, |
[email protected] | 7df4348 | 2009-07-31 19:37:44 | [diff] [blame] | 409 | // C%2B is escaped as C%252B, make sure we unescape it properly. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 410 | {"C%252B", ASCIIToUTF16("C%2B")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 411 | }; |
| 412 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 413 | // Set one input encoding: big-5. This is so we can test fallback to UTF-8. |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 414 | TemplateURLData data; |
| 415 | data.SetURL("http://foo?q={searchTerms}"); |
| 416 | data.input_encodings.push_back("big-5"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 417 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 418 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 419 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 420 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) { |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 421 | EXPECT_EQ(to_wide_cases[i].expected_decoded_term, |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 422 | url.url_ref().SearchTermToString16( |
| 423 | to_wide_cases[i].encoded_search_term)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 427 | TEST_F(TemplateURLTest, DisplayURLToURLRef) { |
| 428 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 429 | const std::string url; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 430 | const base::string16 expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 431 | } test_data[] = { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 432 | { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 433 | ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 434 | { "http://X", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 435 | ASCIIToUTF16("http://X") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 436 | { "http://foo{searchTerms", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 437 | ASCIIToUTF16("http://foo{searchTerms") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 438 | { "http://foo{searchTerms}{language}", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 439 | ASCIIToUTF16("http://foo%s{language}") }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 440 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 441 | TemplateURLData data; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 442 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 443 | data.SetURL(test_data[i].url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 444 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 445 | EXPECT_EQ(test_data[i].expected_result, |
| 446 | url.url_ref().DisplayURL(search_terms_data_)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 447 | EXPECT_EQ(test_data[i].url, |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 448 | TemplateURLRef::DisplayURLToURLRef( |
| 449 | url.url_ref().DisplayURL(search_terms_data_))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | TEST_F(TemplateURLTest, ReplaceSearchTerms) { |
| 454 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 455 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 456 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 457 | } test_data[] = { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 458 | { "http://foo/{language}{searchTerms}{inputEncoding}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 459 | "http://foo/{language}XUTF-8" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 460 | { "http://foo/{language}{inputEncoding}{searchTerms}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 461 | "http://foo/{language}UTF-8X" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 462 | { "http://foo/{searchTerms}{language}{inputEncoding}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 463 | "http://foo/X{language}UTF-8" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 464 | { "http://foo/{searchTerms}{inputEncoding}{language}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 465 | "http://foo/XUTF-8{language}" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 466 | { "http://foo/{inputEncoding}{searchTerms}{language}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 467 | "http://foo/UTF-8X{language}" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 468 | { "http://foo/{inputEncoding}{language}{searchTerms}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 469 | "http://foo/UTF-8{language}X" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 470 | { "http://foo/{language}a{searchTerms}a{inputEncoding}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 471 | "http://foo/{language}aXaUTF-8a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 472 | { "http://foo/{language}a{inputEncoding}a{searchTerms}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 473 | "http://foo/{language}aUTF-8aXa" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 474 | { "http://foo/{searchTerms}a{language}a{inputEncoding}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 475 | "http://foo/Xa{language}aUTF-8a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 476 | { "http://foo/{searchTerms}a{inputEncoding}a{language}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 477 | "http://foo/XaUTF-8a{language}a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 478 | { "http://foo/{inputEncoding}a{searchTerms}a{language}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 479 | "http://foo/UTF-8aXa{language}a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 480 | { "http://foo/{inputEncoding}a{language}a{searchTerms}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 481 | "http://foo/UTF-8a{language}aXa" }, |
| 482 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 483 | TemplateURLData data; |
| 484 | data.input_encodings.push_back("UTF-8"); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 485 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 486 | data.SetURL(test_data[i].url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 487 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 488 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 489 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 490 | std::string expected_result = test_data[i].expected_result; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 491 | ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}", |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 492 | search_terms_data_.GetApplicationLocale()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 493 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 494 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), |
| 495 | search_terms_data_)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 496 | ASSERT_TRUE(result.is_valid()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 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; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 507 | const base::string16 search_term; |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 508 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 509 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 510 | } test_data[] = { |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 511 | { "BIG5", base::WideToUTF16(L"\x60BD"), |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 512 | "http://foo/?{searchTerms}{inputEncoding}", |
[email protected] | 3c75f0d | 2010-03-02 05:51:17 | [diff] [blame] | 513 | "http://foo/?%B1~BIG5" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 514 | { "UTF-8", ASCIIToUTF16("blah"), |
| 515 | "http://foo/?{searchTerms}{inputEncoding}", |
[email protected] | 3c75f0d | 2010-03-02 05:51:17 | [diff] [blame] | 516 | "http://foo/?blahUTF-8" }, |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 517 | { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"), |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 518 | "http://foo/{searchTerms}/bar", |
| 519 | "http://foo/%82%A0/bar"}, |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 520 | { "Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"), |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 521 | "http://foo/{searchTerms}/bar", |
| 522 | "http://foo/%82%A0%20%82%A2/bar"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 523 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 524 | TemplateURLData data; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 525 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 526 | data.SetURL(test_data[i].url); |
| 527 | data.input_encodings.clear(); |
| 528 | data.input_encodings.push_back(test_data[i].encoding); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 529 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 530 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 531 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 532 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 533 | TemplateURLRef::SearchTermsArgs(test_data[i].search_term), |
| 534 | search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 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 { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 543 | const base::string16 search_term; |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 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", |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 570 | "http://www.google.com", |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 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", |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 576 | "https://www.google.com", |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 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); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 584 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 585 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 586 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 587 | TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| 588 | search_terms_args.assisted_query_stats = test_data[i].aqs; |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 589 | search_terms_data_.set_google_base_url(test_data[i].base_url); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 590 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 591 | search_terms_data_)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 592 | ASSERT_TRUE(result.is_valid()); |
| 593 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 597 | // Tests replacing cursor position. |
| 598 | TEST_F(TemplateURLTest, ReplaceCursorPosition) { |
| 599 | struct TestData { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 600 | const base::string16 search_term; |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 601 | size_t cursor_position; |
| 602 | const std::string url; |
| 603 | const std::string expected_result; |
| 604 | } test_data[] = { |
| 605 | { ASCIIToUTF16("foo"), |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 606 | base::string16::npos, |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 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 | }; |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 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); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 622 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 623 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 624 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 625 | TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| 626 | search_terms_args.cursor_position = test_data[i].cursor_position; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 627 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 628 | search_terms_data_)); |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 629 | ASSERT_TRUE(result.is_valid()); |
| 630 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| 631 | } |
| 632 | } |
| 633 | |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 634 | // Tests replacing input type (&oit=). |
| 635 | TEST_F(TemplateURLTest, ReplaceInputType) { |
| 636 | struct TestData { |
| 637 | const base::string16 search_term; |
[email protected] | 332d17d2 | 2014-06-20 16:56:03 | [diff] [blame] | 638 | metrics::OmniboxInputType::Type input_type; |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 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 | }; |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 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); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 659 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 660 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 661 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 662 | TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| 663 | search_terms_args.input_type = test_data[i].input_type; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 664 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 665 | search_terms_data_)); |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 666 | ASSERT_TRUE(result.is_valid()); |
| 667 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| 668 | } |
| 669 | } |
| 670 | |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 671 | // Tests replacing currentPageUrl. |
| 672 | TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) { |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 673 | struct TestData { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 674 | const base::string16 search_term; |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 675 | const std::string current_page_url; |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 676 | const std::string url; |
| 677 | const std::string expected_result; |
| 678 | } test_data[] = { |
| 679 | { ASCIIToUTF16("foo"), |
| 680 | "http://www.google.com/", |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 681 | "{google:baseURL}?{searchTerms}&{google:currentPageUrl}", |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 682 | "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" }, |
| 683 | { ASCIIToUTF16("foo"), |
| 684 | "", |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 685 | "{google:baseURL}?{searchTerms}&{google:currentPageUrl}", |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 686 | "http://www.google.com/?foo&" }, |
| 687 | { ASCIIToUTF16("foo"), |
| 688 | "http://g.com/+-/*&=", |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 689 | "{google:baseURL}?{searchTerms}&{google:currentPageUrl}", |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 690 | "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" }, |
| 691 | }; |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 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); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 696 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 697 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 698 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 699 | TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 700 | search_terms_args.current_page_url = test_data[i].current_page_url; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 701 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 702 | search_terms_data_)); |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 703 | ASSERT_TRUE(result.is_valid()); |
| 704 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| 705 | } |
| 706 | } |
| 707 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 708 | TEST_F(TemplateURLTest, Suggestions) { |
| 709 | struct TestData { |
| 710 | const int accepted_suggestion; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 711 | const base::string16 original_query_for_suggestion; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 712 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 713 | } test_data[] = { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 714 | { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(), |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 715 | "http://bar/foo?q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 716 | { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"), |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 717 | "http://bar/foo?q=foobar" }, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 718 | { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(), |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 719 | "http://bar/foo?q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 720 | { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"), |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 721 | "http://bar/foo?q=foobar" }, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 722 | { 0, base::string16(), "http://bar/foo?oq=&q=foobar" }, |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 723 | { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 724 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 725 | TemplateURLData data; |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 726 | data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}" |
| 727 | "q={searchTerms}"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 728 | data.input_encodings.push_back("UTF-8"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 729 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 730 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 731 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 732 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 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; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 738 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 739 | search_terms_data_)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 740 | ASSERT_TRUE(result.is_valid()); |
| 741 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
[email protected] | 81f808de | 2009-09-25 01:36:34 | [diff] [blame] | 745 | TEST_F(TemplateURLTest, RLZ) { |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 746 | base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(false); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 747 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 748 | TemplateURLData data; |
| 749 | data.SetURL("http://bar/?{google:RLZ}{searchTerms}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 750 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 751 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 752 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 753 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 754 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 755 | ASSERT_TRUE(result.is_valid()); |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 756 | EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x", |
| 757 | result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 758 | } |
| 759 | |
[email protected] | c8ccc41d | 2014-04-10 04:42:12 | [diff] [blame] | 760 | TEST_F(TemplateURLTest, RLZFromAppList) { |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 761 | base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(true); |
[email protected] | c8ccc41d | 2014-04-10 04:42:12 | [diff] [blame] | 762 | |
| 763 | TemplateURLData data; |
| 764 | data.SetURL("http://bar/?{google:RLZ}{searchTerms}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 765 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 766 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 767 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | c8ccc41d | 2014-04-10 04:42:12 | [diff] [blame] | 768 | TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x")); |
| 769 | args.from_app_list = true; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 770 | GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_)); |
[email protected] | c8ccc41d | 2014-04-10 04:42:12 | [diff] [blame] | 771 | ASSERT_TRUE(result.is_valid()); |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 772 | EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x", |
| 773 | result.spec()); |
[email protected] | c8ccc41d | 2014-04-10 04:42:12 | [diff] [blame] | 774 | } |
[email protected] | c8ccc41d | 2014-04-10 04:42:12 | [diff] [blame] | 775 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 776 | TEST_F(TemplateURLTest, HostAndSearchTermKey) { |
| 777 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 778 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 779 | const std::string host; |
| 780 | const std::string path; |
| 781 | const std::string search_term_key; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 782 | } test_data[] = { |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 783 | { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 784 | |
| 785 | // No query key should result in empty values. |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 786 | { "http://blah/{searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 787 | |
| 788 | // No term should result in empty values. |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 789 | { "http://blah/", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 790 | |
| 791 | // Multiple terms should result in empty values. |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 792 | { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 793 | |
| 794 | // Term in the host shouldn't match. |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 795 | { "http://{searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 796 | |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 797 | { "http://blah/?q={searchTerms}", "blah", "/", "q"}, |
| 798 | { "https://blah/?q={searchTerms}", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 799 | |
| 800 | // Single term with extra chars in value should match. |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 801 | { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 802 | }; |
| 803 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 804 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 805 | TemplateURLData data; |
| 806 | data.SetURL(test_data[i].url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 807 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 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_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
| 815 | TEST_F(TemplateURLTest, GoogleBaseSuggestURL) { |
| 816 | static const struct { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 817 | const char* const base_url; |
| 818 | const char* const base_suggest_url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 819 | } data[] = { |
[email protected] | 014010e | 2011-10-01 04:12:44 | [diff] [blame] | 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/", }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 825 | }; |
| 826 | |
[email protected] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 827 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 828 | CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url); |
| 829 | } |
| 830 | |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 831 | TEST_F(TemplateURLTest, ParseParameterKnown) { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 832 | std::string parsed_url("{searchTerms}"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 833 | TemplateURLData data; |
| 834 | data.SetURL(parsed_url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 835 | TemplateURL url(data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 836 | TemplateURLRef::Replacements replacements; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 837 | EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements)); |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 838 | EXPECT_EQ(std::string(), parsed_url); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 839 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 840 | EXPECT_EQ(0U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 841 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 842 | } |
| 843 | |
| 844 | TEST_F(TemplateURLTest, ParseParameterUnknown) { |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 845 | std::string parsed_url("{fhqwhgads}abc"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 846 | TemplateURLData data; |
| 847 | data.SetURL(parsed_url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 848 | TemplateURL url(data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 849 | TemplateURLRef::Replacements replacements; |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 850 | |
| 851 | // By default, TemplateURLRef should not consider itself prepopulated. |
| 852 | // Therefore we should not replace the unknown parameter. |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 853 | EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 854 | EXPECT_EQ("{fhqwhgads}abc", parsed_url); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 855 | EXPECT_TRUE(replacements.empty()); |
| 856 | |
| 857 | // If the TemplateURLRef is prepopulated, we should remove unknown parameters. |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 858 | parsed_url = "{fhqwhgads}abc"; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 859 | data.prepopulate_id = 1; |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 860 | TemplateURL url2(data); |
[email protected] | 495c30b | 2012-05-15 18:48:15 | [diff] [blame] | 861 | EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 862 | EXPECT_EQ("abc", parsed_url); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 863 | EXPECT_TRUE(replacements.empty()); |
| 864 | } |
| 865 | |
| 866 | TEST_F(TemplateURLTest, ParseURLEmpty) { |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 867 | TemplateURL url((TemplateURLData())); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 868 | TemplateURLRef::Replacements replacements; |
| 869 | bool valid = false; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 870 | EXPECT_EQ(std::string(), |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 871 | url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 872 | EXPECT_TRUE(replacements.empty()); |
| 873 | EXPECT_TRUE(valid); |
| 874 | } |
| 875 | |
| 876 | TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 877 | TemplateURLData data; |
| 878 | data.SetURL("{"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 879 | TemplateURL url(data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 880 | TemplateURLRef::Replacements replacements; |
| 881 | bool valid = false; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 882 | EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL, |
| 883 | &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 884 | EXPECT_TRUE(replacements.empty()); |
| 885 | EXPECT_FALSE(valid); |
| 886 | } |
| 887 | |
| 888 | TEST_F(TemplateURLTest, ParseURLNoKnownParameters) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 889 | TemplateURLData data; |
| 890 | data.SetURL("{}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 891 | TemplateURL url(data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 892 | TemplateURLRef::Replacements replacements; |
| 893 | bool valid = false; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 894 | EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 895 | EXPECT_TRUE(replacements.empty()); |
| 896 | EXPECT_TRUE(valid); |
| 897 | } |
| 898 | |
| 899 | TEST_F(TemplateURLTest, ParseURLTwoParameters) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 900 | TemplateURLData data; |
| 901 | data.SetURL("{}{{%s}}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 902 | TemplateURL url(data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 903 | TemplateURLRef::Replacements replacements; |
| 904 | bool valid = false; |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 905 | EXPECT_EQ("{}{}", |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 906 | url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL, |
| 907 | &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 908 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 909 | EXPECT_EQ(3U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 910 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 911 | EXPECT_TRUE(valid); |
| 912 | } |
| 913 | |
| 914 | TEST_F(TemplateURLTest, ParseURLNestedParameter) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 915 | TemplateURLData data; |
| 916 | data.SetURL("{%s"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 917 | TemplateURL url(data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 918 | TemplateURLRef::Replacements replacements; |
| 919 | bool valid = false; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 920 | EXPECT_EQ("{", |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 921 | url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL, |
| 922 | &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 923 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 924 | EXPECT_EQ(1U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 925 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 926 | EXPECT_TRUE(valid); |
| 927 | } |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 928 | |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 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; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 933 | const base::string16 terms(ASCIIToUTF16(terms_str)); |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 934 | search_terms_data_.set_google_base_url(base_url_str); |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 935 | |
| 936 | TemplateURLData data; |
| 937 | data.SetURL(full_url_str); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 938 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 939 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 940 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 941 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar")); |
| 942 | |
| 943 | // Check that the URL is correct when a client is not present. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 944 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 945 | search_terms_data_)); |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 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. |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 950 | search_terms_data_.set_search_client("search_client"); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 951 | GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 952 | search_terms_data_)); |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 953 | ASSERT_TRUE(result_2.is_valid()); |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 954 | EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec()); |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 955 | } |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 956 | |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 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}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 963 | TemplateURL url(data); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 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}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 976 | TemplateURL url(data); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 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/"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 986 | TemplateURL url(data); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 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"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 998 | TemplateURL url(data); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 999 | base::string16 result; |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1000 | |
| 1001 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1002 | GURL("http://google.com/?q=something"), search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1003 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1004 | |
| 1005 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1006 | GURL("http://google.com/?espv&q=something"), |
| 1007 | search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1008 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1009 | |
| 1010 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1011 | GURL("http://google.com/?espv=1&q=something"), |
| 1012 | search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1013 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1014 | |
| 1015 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1016 | GURL("http://google.com/?espv=0&q=something"), |
| 1017 | search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1018 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1019 | |
| 1020 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1021 | GURL("http://google.com/alt/#q=something"), |
| 1022 | search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1023 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1024 | |
| 1025 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1026 | GURL("http://google.com/alt/#espv&q=something"), |
| 1027 | search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1028 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1029 | |
| 1030 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1031 | GURL("http://google.com/alt/#espv=1&q=something"), |
| 1032 | search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1033 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1034 | |
| 1035 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1036 | GURL("http://google.com/alt/#espv=0&q=something"), |
| 1037 | search_terms_data_, &result)); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1038 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1039 | |
| 1040 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1041 | GURL("http://google.ca/?q=something"), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1042 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1043 | |
| 1044 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1045 | GURL("http://google.ca/?q=something&q=anything"), |
| 1046 | search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1047 | EXPECT_EQ(base::string16(), result); |
[email protected] | 67d8b75 | 2013-04-03 17:33:27 | [diff] [blame] | 1048 | |
| 1049 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1050 | GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1051 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1052 | |
[email protected] | 32e2d81b | 2012-10-16 19:03:25 | [diff] [blame] | 1053 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1054 | GURL("https://google.com/?q=foo"), search_terms_data_, &result)); |
[email protected] | 32e2d81b | 2012-10-16 19:03:25 | [diff] [blame] | 1055 | EXPECT_EQ(ASCIIToUTF16("foo"), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1056 | |
| 1057 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1058 | GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1059 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1060 | |
| 1061 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1062 | GURL("http://google.com/?q=1+2+3&b=456"), search_terms_data_, &result)); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1063 | EXPECT_EQ(ASCIIToUTF16("1 2 3"), result); |
| 1064 | |
| 1065 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1066 | GURL("http://google.com/alt/?q=123#q=456"), |
| 1067 | search_terms_data_, &result)); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1068 | EXPECT_EQ(ASCIIToUTF16("456"), result); |
| 1069 | |
| 1070 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1071 | GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), |
| 1072 | search_terms_data_, &result)); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1073 | EXPECT_EQ(ASCIIToUTF16("123"), result); |
| 1074 | |
| 1075 | EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1076 | "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), |
| 1077 | search_terms_data_, &result)); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1078 | EXPECT_EQ(ASCIIToUTF16("789"), result); |
| 1079 | |
| 1080 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1081 | GURL("http://google.com/alt/?q="), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1082 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1083 | |
| 1084 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1085 | GURL("http://google.com/alt/?#q="), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1086 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1087 | |
| 1088 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1089 | GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1090 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1091 | |
| 1092 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1093 | GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1094 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1095 | |
| 1096 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1097 | GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result)); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1098 | EXPECT_EQ(ASCIIToUTF16("123"), result); |
| 1099 | } |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 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"; |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1109 | TemplateURL url(data); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 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 | } |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 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"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1190 | TemplateURL url(data); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1191 | TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane")); |
| 1192 | GURL result; |
| 1193 | |
| 1194 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1195 | GURL("http://google.com/?q=something"), search_terms, |
| 1196 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 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( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1201 | GURL("http://google.ca/?q=something"), search_terms, |
| 1202 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1203 | EXPECT_EQ(GURL("http://should.not.change.com"), result); |
| 1204 | |
| 1205 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1206 | GURL("http://google.com/foo/?q=foo"), search_terms, |
| 1207 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1208 | |
| 1209 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1210 | GURL("https://google.com/?q=foo"), search_terms, |
| 1211 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1212 | EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result); |
| 1213 | |
| 1214 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1215 | GURL("http://google.com:8080/?q=foo"), search_terms, |
| 1216 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1217 | |
| 1218 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1219 | GURL("http://google.com/?q=1+2+3&b=456"), search_terms, |
| 1220 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 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( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1226 | GURL("http://google.com/alt/?q=123#q=456"), search_terms, |
| 1227 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 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, |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1232 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 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"), |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1238 | search_terms, search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 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( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1243 | GURL("http://google.com/alt/?q="), search_terms, |
| 1244 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1245 | |
| 1246 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1247 | GURL("http://google.com/alt/?#q="), search_terms, |
| 1248 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1249 | |
| 1250 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1251 | GURL("http://google.com/alt/?q=#q="), search_terms, |
| 1252 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1253 | |
| 1254 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1255 | GURL("http://google.com/alt/?q=123#q="), search_terms, |
| 1256 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1257 | |
| 1258 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1259 | GURL("http://google.com/alt/?q=#q=123"), search_terms, |
| 1260 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1261 | EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result); |
| 1262 | } |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1263 | |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1264 | // Test the |suggest_query_params| field of SearchTermsArgs. |
| 1265 | TEST_F(TemplateURLTest, SuggestQueryParams) { |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 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"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1271 | TemplateURL url(data); |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 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", |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1278 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 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", |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1283 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 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", |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1290 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1291 | } |
| 1292 | |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1293 | // Test the |append_extra_query_params| field of SearchTermsArgs. |
| 1294 | TEST_F(TemplateURLTest, ExtraQueryParams) { |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 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"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1300 | TemplateURL url(data); |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 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", |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1307 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 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", |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1313 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 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", |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1319 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 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", |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1324 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1325 | } |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 1326 | |
| 1327 | // Tests replacing pageClassification. |
[email protected] | 57ac99b9 | 2013-11-13 01:30:17 | [diff] [blame] | 1328 | TEST_F(TemplateURLTest, ReplacePageClassification) { |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 1329 | TemplateURLData data; |
| 1330 | data.input_encodings.push_back("UTF-8"); |
| 1331 | data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1332 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1333 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 1334 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 1335 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); |
| 1336 | |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1337 | std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1338 | search_terms_data_); |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 1339 | EXPECT_EQ("http://www.google.com/?q=foo", result); |
| 1340 | |
[email protected] | 332d17d2 | 2014-06-20 16:56:03 | [diff] [blame] | 1341 | search_terms_args.page_classification = metrics::OmniboxEventProto::NTP; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1342 | result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1343 | search_terms_data_); |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 1344 | EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result); |
| 1345 | |
| 1346 | search_terms_args.page_classification = |
[email protected] | 332d17d2 | 2014-06-20 16:56:03 | [diff] [blame] | 1347 | metrics::OmniboxEventProto::HOME_PAGE; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1348 | result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1349 | search_terms_data_); |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 1350 | EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result); |
| 1351 | } |
[email protected] | 2328ee2 | 2013-08-08 23:00:19 | [diff] [blame] | 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}"; |
[email protected] | 2767c0fd | 2013-08-16 17:44:16 | [diff] [blame] | 1358 | data.new_tab_url = "http://bar/newtab"; |
[email protected] | 2328ee2 | 2013-08-08 23:00:19 | [diff] [blame] | 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}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1363 | TemplateURL search_provider(data); |
[email protected] | 2328ee2 | 2013-08-08 23:00:19 | [diff] [blame] | 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, }, |
[email protected] | 2767c0fd | 2013-08-16 17:44:16 | [diff] [blame] | 1378 | { "http://bar/newtab", false, }, |
[email protected] | 2328ee2 | 2013-08-08 23:00:19 | [diff] [blame] | 1379 | }; |
| 1380 | |
| 1381 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_data); ++i) { |
| 1382 | EXPECT_EQ(url_data[i].result, |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1383 | search_provider.IsSearchURL(GURL(url_data[i].url), |
| 1384 | search_terms_data_)); |
[email protected] | 2328ee2 | 2013-08-08 23:00:19 | [diff] [blame] | 1385 | } |
| 1386 | } |
[email protected] | a048de8a | 2013-10-02 18:30:06 | [diff] [blame] | 1387 | |
| 1388 | TEST_F(TemplateURLTest, ReflectsBookmarkBarPinned) { |
[email protected] | a048de8a | 2013-10-02 18:30:06 | [diff] [blame] | 1389 | TemplateURLData data; |
| 1390 | data.input_encodings.push_back("UTF-8"); |
| 1391 | data.SetURL("{google:baseURL}?{google:bookmarkBarPinned}q={searchTerms}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1392 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1393 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 1394 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | a048de8a | 2013-10-02 18:30:06 | [diff] [blame] | 1395 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); |
| 1396 | |
| 1397 | // Do not add the param when InstantExtended is suppressed on SRPs. |
[email protected] | 5c3c6e48 | 2014-06-23 09:47:18 | [diff] [blame] | 1398 | search_terms_data_.set_is_showing_search_terms_on_search_results_pages(false); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1399 | std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1400 | search_terms_data_); |
[email protected] | a048de8a | 2013-10-02 18:30:06 | [diff] [blame] | 1401 | EXPECT_EQ("http://www.google.com/?q=foo", result); |
| 1402 | |
| 1403 | // Add the param when InstantExtended is not suppressed on SRPs. |
[email protected] | 5c3c6e48 | 2014-06-23 09:47:18 | [diff] [blame] | 1404 | search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true); |
[email protected] | a048de8a | 2013-10-02 18:30:06 | [diff] [blame] | 1405 | search_terms_args.bookmark_bar_pinned = false; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1406 | result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1407 | search_terms_data_); |
[email protected] | a048de8a | 2013-10-02 18:30:06 | [diff] [blame] | 1408 | EXPECT_EQ("http://www.google.com/?bmbp=0&q=foo", result); |
| 1409 | |
[email protected] | 5c3c6e48 | 2014-06-23 09:47:18 | [diff] [blame] | 1410 | search_terms_data_.set_is_showing_search_terms_on_search_results_pages(true); |
[email protected] | a048de8a | 2013-10-02 18:30:06 | [diff] [blame] | 1411 | search_terms_args.bookmark_bar_pinned = true; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1412 | result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1413 | search_terms_data_); |
[email protected] | a048de8a | 2013-10-02 18:30:06 | [diff] [blame] | 1414 | EXPECT_EQ("http://www.google.com/?bmbp=1&q=foo", result); |
| 1415 | } |
[email protected] | 9d70de1 | 2014-05-10 02:15:31 | [diff] [blame] | 1416 | |
[email protected] | 9d70de1 | 2014-05-10 02:15:31 | [diff] [blame] | 1417 | TEST_F(TemplateURLTest, AnswersHasVersion) { |
| 1418 | TemplateURLData data; |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 1419 | search_terms_data_.set_google_base_url("http://bar/"); |
[email protected] | 9d70de1 | 2014-05-10 02:15:31 | [diff] [blame] | 1420 | data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t"); |
| 1421 | |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1422 | TemplateURL url(data); |
[email protected] | 9d70de1 | 2014-05-10 02:15:31 | [diff] [blame] | 1423 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1424 | std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1425 | search_terms_data_); |
[email protected] | 9d70de1 | 2014-05-10 02:15:31 | [diff] [blame] | 1426 | EXPECT_EQ("http://bar/search?q=foo&xssi=t", result); |
| 1427 | |
[email protected] | 5c3c6e48 | 2014-06-23 09:47:18 | [diff] [blame] | 1428 | search_terms_data_.set_enable_answers_in_suggest(true); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1429 | TemplateURL url2(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1430 | result = url2.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1431 | search_terms_data_); |
[email protected] | 9d70de1 | 2014-05-10 02:15:31 | [diff] [blame] | 1432 | EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result); |
| 1433 | } |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 1434 | |
| 1435 | TEST_F(TemplateURLTest, SessionToken) { |
| 1436 | TemplateURLData data; |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 1437 | search_terms_data_.set_google_base_url("http://bar/"); |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 1438 | data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t"); |
| 1439 | |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1440 | TemplateURL url(data); |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 1441 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); |
| 1442 | search_terms_args.session_token = "SESSIONTOKENGOESHERE"; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1443 | std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1444 | search_terms_data_); |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 1445 | EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result); |
| 1446 | |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1447 | TemplateURL url2(data); |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 1448 | search_terms_args.session_token = ""; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1449 | result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1450 | search_terms_data_); |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 1451 | EXPECT_EQ("http://bar/search?q=foo&xssi=t", result); |
| 1452 | } |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 1453 | |
| 1454 | TEST_F(TemplateURLTest, ContextualSearchParameters) { |
| 1455 | TemplateURLData data; |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 1456 | search_terms_data_.set_google_base_url("http://bar/"); |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 1457 | data.SetURL("http://bar/_/contextualsearch?" |
| 1458 | "{google:contextualSearchVersion}" |
| 1459 | "{google:contextualSearchContextData}"); |
| 1460 | |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1461 | TemplateURL url(data); |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 1462 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1463 | std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1464 | search_terms_data_); |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 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; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1471 | result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1472 | search_terms_data_); |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 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 | } |
[email protected] | 44ccc9f | 2014-06-20 17:36:21 | [diff] [blame] | 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 | } |
[email protected] | 9e9130ce | 2014-06-23 23:20:51 | [diff] [blame^] | 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 | } |