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