[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 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 7 | #include "base/base_paths.h" |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 8 | #include "base/command_line.h" |
a-v-y | 81695d0d | 2017-04-20 08:22:22 | [diff] [blame] | 9 | #include "base/i18n/case_conversion.h" |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 10 | #include "base/stl_util.h" |
[email protected] | 2f3bc651 | 2013-08-28 03:56:27 | [diff] [blame] | 11 | #include "base/strings/string_number_conversions.h" |
[email protected] | 24a555b6 | 2013-06-10 22:01:17 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
John Abd-El-Malek | 9cf3d7f0 | 2018-07-27 02:40:39 | [diff] [blame] | 14 | #include "components/google/core/common/google_util.h" |
[email protected] | 5c3c6e48 | 2014-06-23 09:47:18 | [diff] [blame] | 15 | #include "components/search_engines/search_engines_switches.h" |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 16 | #include "components/search_engines/search_terms_data.h" |
[email protected] | d550cb0 | 2014-06-25 06:48:11 | [diff] [blame] | 17 | #include "components/search_engines/template_url.h" |
hashimoto | 3c83181 | 2014-08-25 07:40:23 | [diff] [blame] | 18 | #include "components/search_engines/testing_search_terms_data.h" |
a-v-y | 02d3f50b | 2017-02-16 12:03:55 | [diff] [blame] | 19 | #include "net/base/url_util.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 20 | #include "testing/gtest/include/gtest/gtest.h" |
Steven Holte | f9d5ed6 | 2017-10-21 02:02:30 | [diff] [blame] | 21 | #include "third_party/metrics_proto/omnibox_event.pb.h" |
| 22 | #include "third_party/metrics_proto/omnibox_input_type.pb.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 23 | |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 24 | using base::ASCIIToUTF16; |
| 25 | |
a-v-y | 81695d0d | 2017-04-20 08:22:22 | [diff] [blame] | 26 | namespace { |
| 27 | bool IsLowerCase(const base::string16& str) { |
| 28 | return str == base::i18n::ToLower(str); |
| 29 | } |
| 30 | } |
| 31 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 32 | class TemplateURLTest : public testing::Test { |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 33 | public: |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 34 | TemplateURLTest() : search_terms_data_("http://www.google.com/") {} |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 35 | void CheckSuggestBaseURL(const std::string& base_url, |
| 36 | const std::string& base_suggest_url) const; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 37 | |
vitbar | deb28539 | 2015-02-20 14:02:55 | [diff] [blame] | 38 | static void ExpectPostParamIs( |
| 39 | const TemplateURLRef::PostParam& param, |
| 40 | const std::string& name, |
| 41 | const std::string& value, |
| 42 | const std::string& content_type = std::string()); |
| 43 | |
hashimoto | 3c83181 | 2014-08-25 07:40:23 | [diff] [blame] | 44 | TestingSearchTermsData search_terms_data_; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 45 | }; |
| 46 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 47 | void TemplateURLTest::CheckSuggestBaseURL( |
| 48 | const std::string& base_url, |
| 49 | const std::string& base_suggest_url) const { |
hashimoto | 3c83181 | 2014-08-25 07:40:23 | [diff] [blame] | 50 | TestingSearchTermsData search_terms_data(base_url); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 51 | EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue()); |
| 52 | } |
| 53 | |
vitbar | deb28539 | 2015-02-20 14:02:55 | [diff] [blame] | 54 | // static |
| 55 | void TemplateURLTest::ExpectPostParamIs(const TemplateURLRef::PostParam& param, |
| 56 | const std::string& name, |
| 57 | const std::string& value, |
| 58 | const std::string& content_type) { |
| 59 | EXPECT_EQ(name, param.name); |
| 60 | EXPECT_EQ(value, param.value); |
| 61 | EXPECT_EQ(content_type, param.content_type); |
| 62 | } |
| 63 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 64 | TEST_F(TemplateURLTest, Defaults) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 65 | TemplateURLData data; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 66 | EXPECT_FALSE(data.safe_for_autoreplace); |
| 67 | EXPECT_EQ(0, data.prepopulate_id); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | TEST_F(TemplateURLTest, TestValidWithComplete) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 71 | TemplateURLData data; |
| 72 | data.SetURL("{searchTerms}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 73 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 74 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST_F(TemplateURLTest, URLRefTestSearchTerms) { |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 78 | struct SearchTermsCase { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 79 | const char* url; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 80 | const base::string16 terms; |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 81 | const std::string output; |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 82 | } search_term_cases[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 83 | { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 84 | "http://foosea%20rch/bar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 85 | { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 86 | "http://foosea%20rch/bar?boo=abc" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 87 | { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 88 | "http://foo/?boo=sea+rch%2Fbar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 89 | { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 90 | "http://en.wikipedia.org/wiki/%3F" } |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 91 | }; |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 92 | for (size_t i = 0; i < base::size(search_term_cases); ++i) { |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 93 | const SearchTermsCase& value = search_term_cases[i]; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 94 | TemplateURLData data; |
| 95 | data.SetURL(value.url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 96 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 97 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 98 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 99 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 100 | TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data_)); |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 101 | ASSERT_TRUE(result.is_valid()); |
| 102 | EXPECT_EQ(value.output, result.spec()); |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 103 | } |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | TEST_F(TemplateURLTest, URLRefTestCount) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 107 | TemplateURLData data; |
| 108 | data.SetURL("http://foo{searchTerms}{count?}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 109 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 110 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 111 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 112 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 113 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 114 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 115 | EXPECT_EQ("http://foox/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | TEST_F(TemplateURLTest, URLRefTestCount2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 119 | TemplateURLData data; |
| 120 | data.SetURL("http://foo{searchTerms}{count}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 121 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 122 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 123 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 124 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 125 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 126 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 127 | EXPECT_EQ("http://foox10/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TEST_F(TemplateURLTest, URLRefTestIndices) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 131 | TemplateURLData data; |
| 132 | data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 133 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 134 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 135 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 136 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 137 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 138 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 139 | EXPECT_EQ("http://fooxxy/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | TEST_F(TemplateURLTest, URLRefTestIndices2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 143 | TemplateURLData data; |
| 144 | data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 145 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 146 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 147 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 148 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 149 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 150 | ASSERT_TRUE(result.is_valid()); |
[email protected] | 405aae2 | 2012-03-29 20:36:13 | [diff] [blame] | 151 | EXPECT_EQ("http://fooxx1y1/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | TEST_F(TemplateURLTest, URLRefTestEncoding) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 155 | TemplateURLData data; |
| 156 | data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 157 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 158 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 159 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 160 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 161 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 162 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 163 | EXPECT_EQ("http://fooxxutf-8ya/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 166 | TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) { |
| 167 | const char kInvalidPostParamsString[] = |
| 168 | "unknown_template={UnknownTemplate},bad_value=bad{value}," |
| 169 | "{google:sbiSource}"; |
| 170 | // List all accpectable parameter format in valid_post_params_string. it is |
| 171 | // expected like: "name0=,name1=value1,name2={template1}" |
| 172 | const char kValidPostParamsString[] = |
| 173 | "image_content={google:imageThumbnail},image_url={google:imageURL}," |
| 174 | "sbisrc={google:imageSearchSource},language={language},empty_param=," |
[email protected] | 2f3bc651 | 2013-08-28 03:56:27 | [diff] [blame] | 175 | "constant_param=constant,width={google:imageOriginalWidth}"; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 176 | const char KImageSearchURL[] = "http://foo.com/sbi"; |
| 177 | |
| 178 | TemplateURLData data; |
| 179 | data.image_url = KImageSearchURL; |
| 180 | |
| 181 | // Try to parse invalid post parameters. |
| 182 | data.image_url_post_params = kInvalidPostParamsString; |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 183 | TemplateURL url_bad(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 184 | ASSERT_FALSE(url_bad.image_url_ref().IsValid(search_terms_data_)); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 185 | const TemplateURLRef::PostParams& bad_post_params = |
| 186 | url_bad.image_url_ref().post_params_; |
| 187 | ASSERT_EQ(2U, bad_post_params.size()); |
vitbar | deb28539 | 2015-02-20 14:02:55 | [diff] [blame] | 188 | ExpectPostParamIs(bad_post_params[0], "unknown_template", |
| 189 | "{UnknownTemplate}"); |
| 190 | ExpectPostParamIs(bad_post_params[1], "bad_value", "bad{value}"); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 191 | |
| 192 | // Try to parse valid post parameters. |
| 193 | data.image_url_post_params = kValidPostParamsString; |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 194 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 195 | ASSERT_TRUE(url.image_url_ref().IsValid(search_terms_data_)); |
| 196 | ASSERT_FALSE(url.image_url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 197 | |
| 198 | // Check term replacement. |
| 199 | TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X")); |
| 200 | search_args.image_thumbnail_content = "dummy-image-thumbnail"; |
| 201 | search_args.image_url = GURL("http://dummyimage.com/dummy.jpg"); |
[email protected] | 2f3bc651 | 2013-08-28 03:56:27 | [diff] [blame] | 202 | search_args.image_original_size = gfx::Size(10, 10); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 203 | // Replacement operation with no post_data buffer should still return |
| 204 | // the parsed URL. |
hashimoto | 3c83181 | 2014-08-25 07:40:23 | [diff] [blame] | 205 | TestingSearchTermsData search_terms_data("http://X"); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 206 | GURL result(url.image_url_ref().ReplaceSearchTerms( |
| 207 | search_args, search_terms_data)); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 208 | ASSERT_TRUE(result.is_valid()); |
| 209 | EXPECT_EQ(KImageSearchURL, result.spec()); |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 210 | TemplateURLRef::PostContent post_content; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 211 | result = GURL(url.image_url_ref().ReplaceSearchTerms( |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 212 | search_args, search_terms_data, &post_content)); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 213 | ASSERT_TRUE(result.is_valid()); |
| 214 | EXPECT_EQ(KImageSearchURL, result.spec()); |
[email protected] | 7c2bcc4 | 2013-08-01 04:03:48 | [diff] [blame] | 215 | ASSERT_FALSE(post_content.first.empty()); |
| 216 | ASSERT_FALSE(post_content.second.empty()); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 217 | |
| 218 | // Check parsed result of post parameters. |
| 219 | const TemplateURLRef::Replacements& replacements = |
| 220 | url.image_url_ref().replacements_; |
| 221 | const TemplateURLRef::PostParams& post_params = |
| 222 | url.image_url_ref().post_params_; |
[email protected] | 2f3bc651 | 2013-08-28 03:56:27 | [diff] [blame] | 223 | EXPECT_EQ(7U, post_params.size()); |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 224 | for (auto i = post_params.begin(); i != post_params.end(); ++i) { |
| 225 | auto j = replacements.begin(); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 226 | for (; j != replacements.end(); ++j) { |
| 227 | if (j->is_post_param && j->index == |
| 228 | static_cast<size_t>(i - post_params.begin())) { |
| 229 | switch (j->type) { |
[email protected] | 2f3bc651 | 2013-08-28 03:56:27 | [diff] [blame] | 230 | case TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH: |
vitbar | deb28539 | 2015-02-20 14:02:55 | [diff] [blame] | 231 | ExpectPostParamIs(*i, "width", |
| 232 | base::IntToString( |
| 233 | search_args.image_original_size.width())); |
[email protected] | 2f3bc651 | 2013-08-28 03:56:27 | [diff] [blame] | 234 | break; |
[email protected] | 1020fead | 2014-06-20 13:40:28 | [diff] [blame] | 235 | case TemplateURLRef::GOOGLE_IMAGE_SEARCH_SOURCE: |
vitbar | deb28539 | 2015-02-20 14:02:55 | [diff] [blame] | 236 | ExpectPostParamIs(*i, "sbisrc", |
| 237 | search_terms_data.GoogleImageSearchSource()); |
[email protected] | 1020fead | 2014-06-20 13:40:28 | [diff] [blame] | 238 | break; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 239 | case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL: |
vitbar | deb28539 | 2015-02-20 14:02:55 | [diff] [blame] | 240 | ExpectPostParamIs(*i, "image_content", |
| 241 | search_args.image_thumbnail_content, |
| 242 | "image/jpeg"); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 243 | break; |
| 244 | case TemplateURLRef::GOOGLE_IMAGE_URL: |
vitbar | deb28539 | 2015-02-20 14:02:55 | [diff] [blame] | 245 | ExpectPostParamIs(*i, "image_url", search_args.image_url.spec()); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 246 | break; |
| 247 | case TemplateURLRef::LANGUAGE: |
vitbar | deb28539 | 2015-02-20 14:02:55 | [diff] [blame] | 248 | ExpectPostParamIs(*i, "language", "en"); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 249 | break; |
| 250 | default: |
| 251 | ADD_FAILURE(); // Should never go here. |
| 252 | } |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | if (j != replacements.end()) |
| 257 | continue; |
vitbar | deb28539 | 2015-02-20 14:02:55 | [diff] [blame] | 258 | if (i->name == "empty_param") |
| 259 | ExpectPostParamIs(*i, "empty_param", std::string()); |
| 260 | else |
| 261 | ExpectPostParamIs(*i, "constant_param", "constant"); |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
Peter Kasting | e59f0c3 | 2017-11-29 05:56:22 | [diff] [blame] | 265 | TEST_F(TemplateURLTest, ImageURLWithGetShouldNotCrash) { |
| 266 | TemplateURLData data; |
| 267 | data.SetURL("http://foo/?q={searchTerms}&t={google:imageThumbnail}"); |
| 268 | TemplateURL url(data); |
| 269 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 270 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| 271 | |
| 272 | TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X")); |
| 273 | search_args.image_thumbnail_content = "dummy-image-thumbnail"; |
| 274 | GURL result( |
| 275 | url.url_ref().ReplaceSearchTerms(search_args, search_terms_data_)); |
| 276 | ASSERT_TRUE(result.is_valid()); |
| 277 | EXPECT_EQ("http://foo/?q=X&t=dummy-image-thumbnail", result.spec()); |
| 278 | } |
| 279 | |
[email protected] | d88cb20 | 2011-08-17 20:03:01 | [diff] [blame] | 280 | // Test that setting the prepopulate ID from TemplateURL causes the stored |
| 281 | // TemplateURLRef to handle parsing the URL parameters differently. |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 282 | TEST_F(TemplateURLTest, SetPrepopulatedAndParse) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 283 | TemplateURLData data; |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 284 | data.SetURL("http://foo{fhqwhgads}bar"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 285 | TemplateURL url(data); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 286 | TemplateURLRef::Replacements replacements; |
| 287 | bool valid = false; |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 288 | EXPECT_EQ("http://foo{fhqwhgads}bar", |
| 289 | url.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements, |
| 290 | nullptr, &valid)); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 291 | EXPECT_TRUE(replacements.empty()); |
| 292 | EXPECT_TRUE(valid); |
| 293 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 294 | data.prepopulate_id = 123; |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 295 | TemplateURL url2(data); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 296 | EXPECT_EQ("http://foobar", |
| 297 | url2.url_ref().ParseURL("http://foo{fhqwhgads}bar", &replacements, |
| 298 | nullptr, &valid)); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 299 | EXPECT_TRUE(replacements.empty()); |
| 300 | EXPECT_TRUE(valid); |
| 301 | } |
| 302 | |
vitbar | 90902c4 | 2016-05-11 06:16:55 | [diff] [blame] | 303 | // Test that setting the prepopulate ID from TemplateURL causes the stored |
| 304 | // TemplateURLRef to handle parsing the URL parameters differently. |
| 305 | TEST_F(TemplateURLTest, SetPrepopulatedAndReplace) { |
| 306 | TemplateURLData data; |
| 307 | data.SetURL("http://foo{fhqwhgads}search/?q={searchTerms}"); |
| 308 | data.suggestions_url = "http://foo{fhqwhgads}suggest/?q={searchTerms}"; |
vitbar | 90902c4 | 2016-05-11 06:16:55 | [diff] [blame] | 309 | data.image_url = "http://foo{fhqwhgads}image/"; |
| 310 | data.new_tab_url = "http://foo{fhqwhgads}newtab/"; |
| 311 | data.contextual_search_url = "http://foo{fhqwhgads}context/"; |
| 312 | data.alternate_urls.push_back( |
| 313 | "http://foo{fhqwhgads}alternate/?q={searchTerms}"); |
| 314 | |
| 315 | TemplateURLRef::SearchTermsArgs args(base::ASCIIToUTF16("X")); |
| 316 | const SearchTermsData& stdata = search_terms_data_; |
| 317 | |
| 318 | TemplateURL url(data); |
| 319 | EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X", |
| 320 | url.url_ref().ReplaceSearchTerms(args, stdata)); |
| 321 | EXPECT_EQ("http://foo%7Bfhqwhgads%7Dalternate/?q=X", |
| 322 | url.url_refs()[0].ReplaceSearchTerms(args, stdata)); |
| 323 | EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsearch/?q=X", |
| 324 | url.url_refs()[1].ReplaceSearchTerms(args, stdata)); |
| 325 | EXPECT_EQ("http://foo%7Bfhqwhgads%7Dsuggest/?q=X", |
| 326 | url.suggestions_url_ref().ReplaceSearchTerms(args, stdata)); |
vitbar | 90902c4 | 2016-05-11 06:16:55 | [diff] [blame] | 327 | EXPECT_EQ("http://foo{fhqwhgads}image/", |
| 328 | url.image_url_ref().ReplaceSearchTerms(args, stdata)); |
| 329 | EXPECT_EQ("http://foo{fhqwhgads}newtab/", |
| 330 | url.new_tab_url_ref().ReplaceSearchTerms(args, stdata)); |
| 331 | EXPECT_EQ("http://foo{fhqwhgads}context/", |
| 332 | url.contextual_search_url_ref().ReplaceSearchTerms(args, stdata)); |
| 333 | |
| 334 | data.prepopulate_id = 123; |
| 335 | TemplateURL url2(data); |
| 336 | EXPECT_EQ("http://foosearch/?q=X", |
| 337 | url2.url_ref().ReplaceSearchTerms(args, stdata)); |
| 338 | EXPECT_EQ("http://fooalternate/?q=X", |
| 339 | url2.url_refs()[0].ReplaceSearchTerms(args, stdata)); |
| 340 | EXPECT_EQ("http://foosearch/?q=X", |
| 341 | url2.url_refs()[1].ReplaceSearchTerms(args, stdata)); |
| 342 | EXPECT_EQ("http://foosuggest/?q=X", |
| 343 | url2.suggestions_url_ref().ReplaceSearchTerms(args, stdata)); |
vitbar | 90902c4 | 2016-05-11 06:16:55 | [diff] [blame] | 344 | EXPECT_EQ("http://fooimage/", |
| 345 | url2.image_url_ref().ReplaceSearchTerms(args, stdata)); |
| 346 | EXPECT_EQ("http://foonewtab/", |
| 347 | url2.new_tab_url_ref().ReplaceSearchTerms(args, stdata)); |
| 348 | EXPECT_EQ("http://foocontext/", |
| 349 | url2.contextual_search_url_ref().ReplaceSearchTerms(args, stdata)); |
| 350 | } |
| 351 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 352 | TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 353 | TemplateURLData data; |
| 354 | data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 355 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 356 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 357 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 358 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 359 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 360 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 361 | EXPECT_EQ("http://fooxutf-8axyb/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | TEST_F(TemplateURLTest, URLRefTestEncoding2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 365 | TemplateURLData data; |
| 366 | data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 367 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 368 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 369 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 370 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 371 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 372 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 373 | EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 374 | } |
| 375 | |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 376 | TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) { |
| 377 | struct SearchTermsCase { |
| 378 | const char* url; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 379 | const base::string16 terms; |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 380 | const char* output; |
| 381 | } search_term_cases[] = { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 382 | { "{google:baseURL}{language}{searchTerms}", base::string16(), |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 383 | "http://example.com/e/en" }, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 384 | { "{google:baseSuggestURL}{searchTerms}", base::string16(), |
[email protected] | 014010e | 2011-10-01 04:12:44 | [diff] [blame] | 385 | "http://example.com/complete/" } |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 386 | }; |
| 387 | |
hashimoto | 3c83181 | 2014-08-25 07:40:23 | [diff] [blame] | 388 | TestingSearchTermsData search_terms_data("http://example.com/e/"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 389 | TemplateURLData data; |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 390 | for (size_t i = 0; i < base::size(search_term_cases); ++i) { |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 391 | const SearchTermsCase& value = search_term_cases[i]; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 392 | data.SetURL(value.url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 393 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 394 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data)); |
| 395 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data)); |
| 396 | GURL result(url.url_ref().ReplaceSearchTerms( |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 397 | TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, |
| 398 | nullptr)); |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 399 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 400 | EXPECT_EQ(value.output, result.spec()); |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 404 | TEST_F(TemplateURLTest, URLRefTermToWide) { |
| 405 | struct ToWideCase { |
| 406 | const char* encoded_search_term; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 407 | const base::string16 expected_decoded_term; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 408 | } to_wide_cases[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 409 | {"hello+world", ASCIIToUTF16("hello world")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 410 | // Test some big-5 input. |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 411 | {"%a7A%A6%6e+to+you", base::WideToUTF16(L"\x4f60\x597d to you")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 412 | // Test some UTF-8 input. We should fall back to this when the encoding |
| 413 | // doesn't look like big-5. We have a '5' in the middle, which is an invalid |
| 414 | // Big-5 trailing byte. |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 415 | {"%e4%bd%a05%e5%a5%bd+to+you", |
| 416 | base::WideToUTF16(L"\x4f60\x35\x597d to you")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 417 | // Undecodable input should stay escaped. |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 418 | {"%91%01+abcd", base::WideToUTF16(L"%91%01 abcd")}, |
[email protected] | 7df4348 | 2009-07-31 19:37:44 | [diff] [blame] | 419 | // Make sure we convert %2B to +. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 420 | {"C%2B%2B", ASCIIToUTF16("C++")}, |
[email protected] | 7df4348 | 2009-07-31 19:37:44 | [diff] [blame] | 421 | // C%2B is escaped as C%252B, make sure we unescape it properly. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 422 | {"C%252B", ASCIIToUTF16("C%2B")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 423 | }; |
| 424 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 425 | // 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] | 426 | TemplateURLData data; |
| 427 | data.SetURL("http://foo?q={searchTerms}"); |
| 428 | data.input_encodings.push_back("big-5"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 429 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 430 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 431 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 432 | for (size_t i = 0; i < base::size(to_wide_cases); i++) { |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 433 | EXPECT_EQ(to_wide_cases[i].expected_decoded_term, |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 434 | url.url_ref().SearchTermToString16( |
| 435 | to_wide_cases[i].encoded_search_term)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 439 | TEST_F(TemplateURLTest, DisplayURLToURLRef) { |
| 440 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 441 | const std::string url; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 442 | const base::string16 expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 443 | } test_data[] = { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 444 | { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 445 | ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 446 | { "http://X", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 447 | ASCIIToUTF16("http://X") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 448 | { "http://foo{searchTerms", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 449 | ASCIIToUTF16("http://foo{searchTerms") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 450 | { "http://foo{searchTerms}{language}", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 451 | ASCIIToUTF16("http://foo%s{language}") }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 452 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 453 | TemplateURLData data; |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 454 | for (size_t i = 0; i < base::size(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 455 | data.SetURL(test_data[i].url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 456 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 457 | EXPECT_EQ(test_data[i].expected_result, |
| 458 | url.url_ref().DisplayURL(search_terms_data_)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 459 | EXPECT_EQ(test_data[i].url, |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 460 | TemplateURLRef::DisplayURLToURLRef( |
| 461 | url.url_ref().DisplayURL(search_terms_data_))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | TEST_F(TemplateURLTest, ReplaceSearchTerms) { |
| 466 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 467 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 468 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 469 | } test_data[] = { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 470 | { "http://foo/{language}{searchTerms}{inputEncoding}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 471 | "http://foo/{language}XUTF-8" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 472 | { "http://foo/{language}{inputEncoding}{searchTerms}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 473 | "http://foo/{language}UTF-8X" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 474 | { "http://foo/{searchTerms}{language}{inputEncoding}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 475 | "http://foo/X{language}UTF-8" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 476 | { "http://foo/{searchTerms}{inputEncoding}{language}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 477 | "http://foo/XUTF-8{language}" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 478 | { "http://foo/{inputEncoding}{searchTerms}{language}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 479 | "http://foo/UTF-8X{language}" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 480 | { "http://foo/{inputEncoding}{language}{searchTerms}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 481 | "http://foo/UTF-8{language}X" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 482 | { "http://foo/{language}a{searchTerms}a{inputEncoding}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 483 | "http://foo/{language}aXaUTF-8a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 484 | { "http://foo/{language}a{inputEncoding}a{searchTerms}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 485 | "http://foo/{language}aUTF-8aXa" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 486 | { "http://foo/{searchTerms}a{language}a{inputEncoding}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 487 | "http://foo/Xa{language}aUTF-8a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 488 | { "http://foo/{searchTerms}a{inputEncoding}a{language}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 489 | "http://foo/XaUTF-8a{language}a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 490 | { "http://foo/{inputEncoding}a{searchTerms}a{language}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 491 | "http://foo/UTF-8aXa{language}a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 492 | { "http://foo/{inputEncoding}a{language}a{searchTerms}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 493 | "http://foo/UTF-8a{language}aXa" }, |
| 494 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 495 | TemplateURLData data; |
| 496 | data.input_encodings.push_back("UTF-8"); |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 497 | for (size_t i = 0; i < base::size(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 498 | data.SetURL(test_data[i].url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 499 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 500 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 501 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 502 | std::string expected_result = test_data[i].expected_result; |
brettw | e6dae46 | 2015-06-24 20:54:45 | [diff] [blame] | 503 | base::ReplaceSubstringsAfterOffset( |
| 504 | &expected_result, 0, "{language}", |
| 505 | search_terms_data_.GetApplicationLocale()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 506 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 507 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")), |
| 508 | search_terms_data_)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 509 | ASSERT_TRUE(result.is_valid()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 510 | EXPECT_EQ(expected_result, result.spec()); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | |
| 515 | // Tests replacing search terms in various encodings and making sure the |
| 516 | // generated URL matches the expected value. |
| 517 | TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) { |
| 518 | struct TestData { |
| 519 | const std::string encoding; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 520 | const base::string16 search_term; |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 521 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 522 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 523 | } test_data[] = { |
Alexander Yashkin | 874b919d | 2018-02-17 15:11:18 | [diff] [blame] | 524 | {"BIG5", base::WideToUTF16(L"\x60BD"), |
| 525 | "http://foo/?{searchTerms}{inputEncoding}", "http://foo/?%B1~BIG5"}, |
| 526 | {"UTF-8", ASCIIToUTF16("blah"), |
| 527 | "http://foo/?{searchTerms}{inputEncoding}", "http://foo/?blahUTF-8"}, |
| 528 | {"Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82"), |
| 529 | "http://foo/{searchTerms}/bar", "http://foo/%82%A0/bar"}, |
| 530 | {"Shift_JIS", base::UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"), |
| 531 | "http://foo/{searchTerms}/bar", "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; |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 534 | for (size_t i = 0; i < base::size(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 | |
Alexander Yashkin | 874b919d | 2018-02-17 15:11:18 | [diff] [blame] | 549 | // Test that encoding with several optional codepages works as intended. |
| 550 | // Codepages are tried in order, fallback is UTF-8. |
| 551 | TEST_F(TemplateURLTest, ReplaceSearchTermsMultipleEncodings) { |
| 552 | struct TestData { |
| 553 | const std::vector<std::string> encodings; |
| 554 | const base::string16 search_term; |
| 555 | const std::string url; |
| 556 | const std::string expected_result; |
| 557 | } test_data[] = { |
| 558 | // First and third encodings are valid. First is used. |
| 559 | {{"windows-1251", "cp-866", "UTF-8"}, |
| 560 | base::UTF8ToUTF16("\xD1\x8F"), |
| 561 | "http://foo/?{searchTerms}{inputEncoding}", |
| 562 | "http://foo/?%FFwindows-1251"}, |
| 563 | // Second and third encodings are valid, second is used. |
| 564 | {{"cp-866", "GB2312", "UTF-8"}, |
| 565 | base::UTF8ToUTF16("\xE7\x8B\x97"), |
| 566 | "http://foo/?{searchTerms}{inputEncoding}", |
| 567 | "http://foo/?%B9%B7GB2312"}, |
| 568 | // Second and third encodings are valid in another order, second is used. |
| 569 | {{"cp-866", "UTF-8", "GB2312"}, |
| 570 | base::UTF8ToUTF16("\xE7\x8B\x97"), |
| 571 | "http://foo/?{searchTerms}{inputEncoding}", |
| 572 | "http://foo/?%E7%8B%97UTF-8"}, |
| 573 | // Both encodings are invalid, fallback to UTF-8. |
| 574 | {{"cp-866", "windows-1251"}, |
| 575 | base::UTF8ToUTF16("\xE7\x8B\x97"), |
| 576 | "http://foo/?{searchTerms}{inputEncoding}", |
| 577 | "http://foo/?%E7%8B%97UTF-8"}, |
| 578 | // No encodings are given, fallback to UTF-8. |
| 579 | {{}, |
| 580 | base::UTF8ToUTF16("\xE7\x8B\x97"), |
| 581 | "http://foo/?{searchTerms}{inputEncoding}", |
| 582 | "http://foo/?%E7%8B%97UTF-8"}, |
| 583 | }; |
| 584 | |
| 585 | TemplateURLData data; |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 586 | for (size_t i = 0; i < base::size(test_data); ++i) { |
Alexander Yashkin | 874b919d | 2018-02-17 15:11:18 | [diff] [blame] | 587 | data.SetURL(test_data[i].url); |
| 588 | data.input_encodings = test_data[i].encodings; |
| 589 | TemplateURL url(data); |
| 590 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 591 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
| 592 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 593 | TemplateURLRef::SearchTermsArgs(test_data[i].search_term), |
| 594 | search_terms_data_)); |
| 595 | ASSERT_TRUE(result.is_valid()); |
| 596 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| 597 | } |
| 598 | } |
| 599 | |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 600 | // Tests replacing assisted query stats (AQS) in various scenarios. |
| 601 | TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) { |
| 602 | struct TestData { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 603 | const base::string16 search_term; |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 604 | const std::string aqs; |
| 605 | const std::string base_url; |
| 606 | const std::string url; |
| 607 | const std::string expected_result; |
| 608 | } test_data[] = { |
| 609 | // No HTTPS, no AQS. |
| 610 | { ASCIIToUTF16("foo"), |
| 611 | "chrome.0.0l6", |
| 612 | "http://foo/", |
| 613 | "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", |
| 614 | "http://foo/?foo" }, |
| 615 | // HTTPS available, AQS should be replaced. |
| 616 | { ASCIIToUTF16("foo"), |
| 617 | "chrome.0.0l6", |
| 618 | "https://foo/", |
| 619 | "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", |
| 620 | "https://foo/?fooaqs=chrome.0.0l6&" }, |
| 621 | // HTTPS available, however AQS is empty. |
| 622 | { ASCIIToUTF16("foo"), |
| 623 | "", |
| 624 | "https://foo/", |
| 625 | "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", |
| 626 | "https://foo/?foo" }, |
| 627 | // No {google:baseURL} and protocol is HTTP, we must not substitute AQS. |
| 628 | { ASCIIToUTF16("foo"), |
| 629 | "chrome.0.0l6", |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 630 | "http://www.google.com", |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 631 | "http://foo?{searchTerms}{google:assistedQueryStats}", |
| 632 | "http://foo/?foo" }, |
| 633 | // A non-Google search provider with HTTPS should allow AQS. |
| 634 | { ASCIIToUTF16("foo"), |
| 635 | "chrome.0.0l6", |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 636 | "https://www.google.com", |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 637 | "https://foo?{searchTerms}{google:assistedQueryStats}", |
| 638 | "https://foo/?fooaqs=chrome.0.0l6&" }, |
| 639 | }; |
| 640 | TemplateURLData data; |
| 641 | data.input_encodings.push_back("UTF-8"); |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 642 | for (size_t i = 0; i < base::size(test_data); ++i) { |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 643 | data.SetURL(test_data[i].url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 644 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 645 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 646 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 647 | TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| 648 | search_terms_args.assisted_query_stats = test_data[i].aqs; |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 649 | search_terms_data_.set_google_base_url(test_data[i].base_url); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 650 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 651 | search_terms_data_)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 652 | ASSERT_TRUE(result.is_valid()); |
| 653 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 657 | // Tests replacing cursor position. |
| 658 | TEST_F(TemplateURLTest, ReplaceCursorPosition) { |
| 659 | struct TestData { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 660 | const base::string16 search_term; |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 661 | size_t cursor_position; |
| 662 | const std::string url; |
| 663 | const std::string expected_result; |
| 664 | } test_data[] = { |
| 665 | { ASCIIToUTF16("foo"), |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 666 | base::string16::npos, |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 667 | "{google:baseURL}?{searchTerms}&{google:cursorPosition}", |
| 668 | "http://www.google.com/?foo&" }, |
| 669 | { ASCIIToUTF16("foo"), |
| 670 | 2, |
| 671 | "{google:baseURL}?{searchTerms}&{google:cursorPosition}", |
| 672 | "http://www.google.com/?foo&cp=2&" }, |
| 673 | { ASCIIToUTF16("foo"), |
| 674 | 15, |
| 675 | "{google:baseURL}?{searchTerms}&{google:cursorPosition}", |
| 676 | "http://www.google.com/?foo&cp=15&" }, |
| 677 | }; |
| 678 | TemplateURLData data; |
| 679 | data.input_encodings.push_back("UTF-8"); |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 680 | for (size_t i = 0; i < base::size(test_data); ++i) { |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 681 | data.SetURL(test_data[i].url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 682 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 683 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 684 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 685 | TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| 686 | search_terms_args.cursor_position = test_data[i].cursor_position; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 687 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 688 | search_terms_data_)); |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 689 | ASSERT_TRUE(result.is_valid()); |
| 690 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| 691 | } |
| 692 | } |
| 693 | |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 694 | // Tests replacing input type (&oit=). |
| 695 | TEST_F(TemplateURLTest, ReplaceInputType) { |
| 696 | struct TestData { |
| 697 | const base::string16 search_term; |
Steven Holte | 3696c941 | 2017-08-24 18:38:03 | [diff] [blame] | 698 | metrics::OmniboxInputType input_type; |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 699 | const std::string url; |
| 700 | const std::string expected_result; |
| 701 | } test_data[] = { |
Steven Holte | 3696c941 | 2017-08-24 18:38:03 | [diff] [blame] | 702 | {ASCIIToUTF16("foo"), metrics::OmniboxInputType::UNKNOWN, |
| 703 | "{google:baseURL}?{searchTerms}&{google:inputType}", |
| 704 | "http://www.google.com/?foo&oit=1&"}, |
| 705 | {ASCIIToUTF16("foo"), metrics::OmniboxInputType::URL, |
| 706 | "{google:baseURL}?{searchTerms}&{google:inputType}", |
| 707 | "http://www.google.com/?foo&oit=3&"}, |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 708 | }; |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 709 | TemplateURLData data; |
| 710 | data.input_encodings.push_back("UTF-8"); |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 711 | for (size_t i = 0; i < base::size(test_data); ++i) { |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 712 | data.SetURL(test_data[i].url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 713 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 714 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 715 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 716 | TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| 717 | search_terms_args.input_type = test_data[i].input_type; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 718 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 719 | search_terms_data_)); |
[email protected] | 420472b2 | 2014-06-10 13:34:43 | [diff] [blame] | 720 | ASSERT_TRUE(result.is_valid()); |
| 721 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| 722 | } |
| 723 | } |
| 724 | |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 725 | // Tests replacing currentPageUrl. |
| 726 | TEST_F(TemplateURLTest, ReplaceCurrentPageUrl) { |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 727 | struct TestData { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 728 | const base::string16 search_term; |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 729 | const std::string current_page_url; |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 730 | const std::string url; |
| 731 | const std::string expected_result; |
| 732 | } test_data[] = { |
| 733 | { ASCIIToUTF16("foo"), |
| 734 | "http://www.google.com/", |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 735 | "{google:baseURL}?{searchTerms}&{google:currentPageUrl}", |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 736 | "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" }, |
| 737 | { ASCIIToUTF16("foo"), |
| 738 | "", |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 739 | "{google:baseURL}?{searchTerms}&{google:currentPageUrl}", |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 740 | "http://www.google.com/?foo&" }, |
| 741 | { ASCIIToUTF16("foo"), |
| 742 | "http://g.com/+-/*&=", |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 743 | "{google:baseURL}?{searchTerms}&{google:currentPageUrl}", |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 744 | "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" }, |
| 745 | }; |
| 746 | TemplateURLData data; |
| 747 | data.input_encodings.push_back("UTF-8"); |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 748 | for (size_t i = 0; i < base::size(test_data); ++i) { |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 749 | data.SetURL(test_data[i].url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 750 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 751 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 752 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 753 | TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 754 | search_terms_args.current_page_url = test_data[i].current_page_url; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 755 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 756 | search_terms_data_)); |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 757 | ASSERT_TRUE(result.is_valid()); |
| 758 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| 759 | } |
| 760 | } |
| 761 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 762 | TEST_F(TemplateURLTest, Suggestions) { |
| 763 | struct TestData { |
| 764 | const int accepted_suggestion; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 765 | const base::string16 original_query_for_suggestion; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 766 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 767 | } test_data[] = { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 768 | { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, base::string16(), |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 769 | "http://bar/foo?q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 770 | { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"), |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 771 | "http://bar/foo?q=foobar" }, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 772 | { TemplateURLRef::NO_SUGGESTION_CHOSEN, base::string16(), |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 773 | "http://bar/foo?q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 774 | { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"), |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 775 | "http://bar/foo?q=foobar" }, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 776 | { 0, base::string16(), "http://bar/foo?oq=&q=foobar" }, |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 777 | { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 778 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 779 | TemplateURLData data; |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 780 | data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}" |
| 781 | "q={searchTerms}"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 782 | data.input_encodings.push_back("UTF-8"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 783 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 784 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 785 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 786 | for (size_t i = 0; i < base::size(test_data); ++i) { |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 787 | TemplateURLRef::SearchTermsArgs search_terms_args( |
| 788 | ASCIIToUTF16("foobar")); |
| 789 | search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion; |
| 790 | search_terms_args.original_query = |
| 791 | test_data[i].original_query_for_suggestion; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 792 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 793 | search_terms_data_)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 794 | ASSERT_TRUE(result.is_valid()); |
| 795 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
[email protected] | 81f808de | 2009-09-25 01:36:34 | [diff] [blame] | 799 | TEST_F(TemplateURLTest, RLZ) { |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 800 | base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(false); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 801 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 802 | TemplateURLData data; |
| 803 | data.SetURL("http://bar/?{google:RLZ}{searchTerms}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 804 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 805 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 806 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 807 | GURL result(url.url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 808 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 809 | ASSERT_TRUE(result.is_valid()); |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 810 | EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x", |
| 811 | result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 812 | } |
| 813 | |
[email protected] | c8ccc41d | 2014-04-10 04:42:12 | [diff] [blame] | 814 | TEST_F(TemplateURLTest, RLZFromAppList) { |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 815 | base::string16 rlz_string = search_terms_data_.GetRlzParameterValue(true); |
[email protected] | c8ccc41d | 2014-04-10 04:42:12 | [diff] [blame] | 816 | |
| 817 | TemplateURLData data; |
| 818 | data.SetURL("http://bar/?{google:RLZ}{searchTerms}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 819 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 820 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 821 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | c8ccc41d | 2014-04-10 04:42:12 | [diff] [blame] | 822 | TemplateURLRef::SearchTermsArgs args(ASCIIToUTF16("x")); |
| 823 | args.from_app_list = true; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 824 | GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_)); |
[email protected] | c8ccc41d | 2014-04-10 04:42:12 | [diff] [blame] | 825 | ASSERT_TRUE(result.is_valid()); |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 826 | EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x", |
| 827 | result.spec()); |
[email protected] | c8ccc41d | 2014-04-10 04:42:12 | [diff] [blame] | 828 | } |
[email protected] | c8ccc41d | 2014-04-10 04:42:12 | [diff] [blame] | 829 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 830 | TEST_F(TemplateURLTest, HostAndSearchTermKey) { |
| 831 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 832 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 833 | const std::string host; |
| 834 | const std::string path; |
| 835 | const std::string search_term_key; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 836 | } test_data[] = { |
vitbar | f2a1156 | 2017-05-15 14:09:50 | [diff] [blame] | 837 | {"http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"}, |
| 838 | {"http://blah/{searchTerms}", "blah", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 839 | |
vitbar | f2a1156 | 2017-05-15 14:09:50 | [diff] [blame] | 840 | // No term should result in empty values. |
| 841 | {"http://blah/", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 842 | |
vitbar | f2a1156 | 2017-05-15 14:09:50 | [diff] [blame] | 843 | // Multiple terms should result in empty values. |
| 844 | {"http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 845 | |
vitbar | f2a1156 | 2017-05-15 14:09:50 | [diff] [blame] | 846 | // Term in the host shouldn't match. |
| 847 | {"http://{searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 848 | |
vitbar | f2a1156 | 2017-05-15 14:09:50 | [diff] [blame] | 849 | {"http://blah/?q={searchTerms}", "blah", "/", "q"}, |
| 850 | {"https://blah/?q={searchTerms}", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 851 | |
vitbar | f2a1156 | 2017-05-15 14:09:50 | [diff] [blame] | 852 | // Single term with extra chars in value should match. |
| 853 | {"http://blah/?q=stock:{searchTerms}", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 854 | }; |
| 855 | |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 856 | for (size_t i = 0; i < base::size(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 857 | TemplateURLData data; |
| 858 | data.SetURL(test_data[i].url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 859 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 860 | EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_)); |
| 861 | EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_)); |
| 862 | EXPECT_EQ(test_data[i].search_term_key, |
| 863 | url.url_ref().GetSearchTermKey(search_terms_data_)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
alexmos | 294849f | 2015-03-14 00:55:06 | [diff] [blame] | 867 | TEST_F(TemplateURLTest, SearchTermKeyLocation) { |
| 868 | struct TestData { |
| 869 | const std::string url; |
| 870 | const url::Parsed::ComponentType location; |
| 871 | const std::string path; |
vitbar | f2a1156 | 2017-05-15 14:09:50 | [diff] [blame] | 872 | const std::string key; |
| 873 | const std::string value_prefix; |
| 874 | const std::string value_suffix; |
alexmos | 294849f | 2015-03-14 00:55:06 | [diff] [blame] | 875 | } test_data[] = { |
vitbar | f2a1156 | 2017-05-15 14:09:50 | [diff] [blame] | 876 | {"http://blah/{searchTerms}/", url::Parsed::PATH, "", "", "/", "/"}, |
| 877 | {"http://blah/{searchTerms}", url::Parsed::PATH, "", "", "/", ""}, |
| 878 | {"http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "", "", |
| 879 | "/begin/", "/end"}, |
| 880 | {"http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY, "/", "q", |
| 881 | "", ""}, |
| 882 | {"http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF, "/", "x", |
| 883 | "", ""}, |
| 884 | {"http://www.example.com/?q=chromium-{searchTerms}@chromium.org/info", |
| 885 | url::Parsed::QUERY, "/", "q", "chromium-", "@chromium.org/info"}, |
alexmos | 294849f | 2015-03-14 00:55:06 | [diff] [blame] | 886 | |
vitbar | f2a1156 | 2017-05-15 14:09:50 | [diff] [blame] | 887 | // searchTerms is a key, not a value, so this should result in an empty |
| 888 | // value. |
| 889 | {"http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x", |
| 890 | url::Parsed::QUERY, "", "", "", ""}, |
alexmos | 294849f | 2015-03-14 00:55:06 | [diff] [blame] | 891 | |
vitbar | f2a1156 | 2017-05-15 14:09:50 | [diff] [blame] | 892 | // Multiple search terms should result in empty values. |
| 893 | {"http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY, "", "", |
| 894 | "", ""}, |
| 895 | {"http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY, "", "", |
| 896 | "", ""}, |
| 897 | {"http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY, "", |
| 898 | "", "", ""}, |
alexmos | 294849f | 2015-03-14 00:55:06 | [diff] [blame] | 899 | }; |
| 900 | |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 901 | for (size_t i = 0; i < base::size(test_data); ++i) { |
alexmos | 294849f | 2015-03-14 00:55:06 | [diff] [blame] | 902 | TemplateURLData data; |
| 903 | data.SetURL(test_data[i].url); |
| 904 | TemplateURL url(data); |
| 905 | EXPECT_EQ(test_data[i].location, |
| 906 | url.url_ref().GetSearchTermKeyLocation(search_terms_data_)); |
| 907 | EXPECT_EQ(test_data[i].path, |
| 908 | url.url_ref().GetPath(search_terms_data_)); |
vitbar | f2a1156 | 2017-05-15 14:09:50 | [diff] [blame] | 909 | EXPECT_EQ(test_data[i].key, |
| 910 | url.url_ref().GetSearchTermKey(search_terms_data_)); |
| 911 | EXPECT_EQ(test_data[i].value_prefix, |
| 912 | url.url_ref().GetSearchTermValuePrefix(search_terms_data_)); |
| 913 | EXPECT_EQ(test_data[i].value_suffix, |
| 914 | url.url_ref().GetSearchTermValueSuffix(search_terms_data_)); |
alexmos | 294849f | 2015-03-14 00:55:06 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 918 | TEST_F(TemplateURLTest, GoogleBaseSuggestURL) { |
| 919 | static const struct { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 920 | const char* const base_url; |
| 921 | const char* const base_suggest_url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 922 | } data[] = { |
[email protected] | 014010e | 2011-10-01 04:12:44 | [diff] [blame] | 923 | { "http://google.com/", "http://google.com/complete/", }, |
| 924 | { "http://www.google.com/", "http://www.google.com/complete/", }, |
| 925 | { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", }, |
| 926 | { "http://www.google.com.by/", "http://www.google.com.by/complete/", }, |
| 927 | { "http://google.com/intl/xx/", "http://google.com/complete/", }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 928 | }; |
| 929 | |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 930 | for (size_t i = 0; i < base::size(data); ++i) |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 931 | CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url); |
| 932 | } |
| 933 | |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 934 | TEST_F(TemplateURLTest, ParseParameterKnown) { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 935 | std::string parsed_url("{searchTerms}"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 936 | TemplateURLData data; |
| 937 | data.SetURL(parsed_url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 938 | TemplateURL url(data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 939 | TemplateURLRef::Replacements replacements; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 940 | EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements)); |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 941 | EXPECT_EQ(std::string(), parsed_url); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 942 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 943 | EXPECT_EQ(0U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 944 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 945 | } |
| 946 | |
| 947 | TEST_F(TemplateURLTest, ParseParameterUnknown) { |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 948 | std::string parsed_url("{fhqwhgads}abc"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 949 | TemplateURLData data; |
| 950 | data.SetURL(parsed_url); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 951 | TemplateURL url(data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 952 | TemplateURLRef::Replacements replacements; |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 953 | |
| 954 | // By default, TemplateURLRef should not consider itself prepopulated. |
| 955 | // Therefore we should not replace the unknown parameter. |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 956 | EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 957 | EXPECT_EQ("{fhqwhgads}abc", parsed_url); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 958 | EXPECT_TRUE(replacements.empty()); |
| 959 | |
| 960 | // If the TemplateURLRef is prepopulated, we should remove unknown parameters. |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 961 | parsed_url = "{fhqwhgads}abc"; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 962 | data.prepopulate_id = 1; |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 963 | TemplateURL url2(data); |
[email protected] | 495c30b | 2012-05-15 18:48:15 | [diff] [blame] | 964 | EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 965 | EXPECT_EQ("abc", parsed_url); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 966 | EXPECT_TRUE(replacements.empty()); |
| 967 | } |
| 968 | |
| 969 | TEST_F(TemplateURLTest, ParseURLEmpty) { |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 970 | TemplateURL url((TemplateURLData())); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 971 | TemplateURLRef::Replacements replacements; |
| 972 | bool valid = false; |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 973 | EXPECT_EQ(std::string(), url.url_ref().ParseURL(std::string(), &replacements, |
| 974 | nullptr, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 975 | EXPECT_TRUE(replacements.empty()); |
| 976 | EXPECT_TRUE(valid); |
| 977 | } |
| 978 | |
| 979 | TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 980 | TemplateURLData data; |
| 981 | data.SetURL("{"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 982 | TemplateURL url(data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 983 | TemplateURLRef::Replacements replacements; |
| 984 | bool valid = false; |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 985 | EXPECT_EQ(std::string(), |
| 986 | url.url_ref().ParseURL("{", &replacements, nullptr, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 987 | EXPECT_TRUE(replacements.empty()); |
| 988 | EXPECT_FALSE(valid); |
| 989 | } |
| 990 | |
| 991 | TEST_F(TemplateURLTest, ParseURLNoKnownParameters) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 992 | TemplateURLData data; |
| 993 | data.SetURL("{}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 994 | TemplateURL url(data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 995 | TemplateURLRef::Replacements replacements; |
| 996 | bool valid = false; |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 997 | EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, nullptr, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 998 | EXPECT_TRUE(replacements.empty()); |
| 999 | EXPECT_TRUE(valid); |
| 1000 | } |
| 1001 | |
| 1002 | TEST_F(TemplateURLTest, ParseURLTwoParameters) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 1003 | TemplateURLData data; |
| 1004 | data.SetURL("{}{{%s}}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1005 | TemplateURL url(data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 1006 | TemplateURLRef::Replacements replacements; |
| 1007 | bool valid = false; |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 1008 | EXPECT_EQ("{}{}", url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, |
| 1009 | nullptr, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 1010 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 1011 | EXPECT_EQ(3U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 1012 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 1013 | EXPECT_TRUE(valid); |
| 1014 | } |
| 1015 | |
| 1016 | TEST_F(TemplateURLTest, ParseURLNestedParameter) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 1017 | TemplateURLData data; |
| 1018 | data.SetURL("{%s"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1019 | TemplateURL url(data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 1020 | TemplateURLRef::Replacements replacements; |
| 1021 | bool valid = false; |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 1022 | EXPECT_EQ("{", url.url_ref().ParseURL("{{searchTerms}", &replacements, |
| 1023 | nullptr, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 1024 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 1025 | EXPECT_EQ(1U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 1026 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 1027 | EXPECT_TRUE(valid); |
| 1028 | } |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1029 | |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 1030 | TEST_F(TemplateURLTest, SearchClient) { |
| 1031 | const std::string base_url_str("http://google.com/?"); |
| 1032 | const std::string terms_str("{searchTerms}&{google:searchClient}"); |
| 1033 | const std::string full_url_str = base_url_str + terms_str; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1034 | const base::string16 terms(ASCIIToUTF16(terms_str)); |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 1035 | search_terms_data_.set_google_base_url(base_url_str); |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 1036 | |
| 1037 | TemplateURLData data; |
| 1038 | data.SetURL(full_url_str); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1039 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1040 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 1041 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 1042 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar")); |
| 1043 | |
| 1044 | // Check that the URL is correct when a client is not present. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1045 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1046 | search_terms_data_)); |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 1047 | ASSERT_TRUE(result.is_valid()); |
| 1048 | EXPECT_EQ("http://google.com/?foobar&", result.spec()); |
| 1049 | |
| 1050 | // Check that the URL is correct when a client is present. |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 1051 | search_terms_data_.set_search_client("search_client"); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1052 | GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1053 | search_terms_data_)); |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 1054 | ASSERT_TRUE(result_2.is_valid()); |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 1055 | EXPECT_EQ("http://google.com/?foobar&client=search_client&", result_2.spec()); |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 1056 | } |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 1057 | |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1058 | TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) { |
| 1059 | TemplateURLData data; |
| 1060 | data.SetURL("http://google.com/?q={searchTerms}"); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1061 | data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}"); |
| 1062 | data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1063 | TemplateURL url(data); |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 1064 | const std::vector<TemplateURLRef>& url_refs = url.url_refs(); |
| 1065 | ASSERT_EQ(3U, url_refs.size()); |
| 1066 | EXPECT_EQ("http://google.com/alt?q={searchTerms}", url_refs[0].GetURL()); |
| 1067 | EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url_refs[1].GetURL()); |
| 1068 | EXPECT_EQ("http://google.com/?q={searchTerms}", url_refs[2].GetURL()); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | TEST_F(TemplateURLTest, GetURLOnlyOneURL) { |
| 1072 | TemplateURLData data; |
| 1073 | data.SetURL("http://www.google.co.uk/"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1074 | TemplateURL url(data); |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 1075 | const std::vector<TemplateURLRef>& url_refs = url.url_refs(); |
| 1076 | ASSERT_EQ(1U, url_refs.size()); |
| 1077 | EXPECT_EQ("http://www.google.co.uk/", url_refs[0].GetURL()); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) { |
| 1081 | TemplateURLData data; |
| 1082 | data.SetURL("http://google.com/?q={searchTerms}"); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1083 | data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}"); |
| 1084 | data.alternate_urls.push_back( |
| 1085 | "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1086 | TemplateURL url(data); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1087 | base::string16 result; |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1088 | |
| 1089 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1090 | GURL("http://google.com/?q=something"), search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1091 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1092 | |
| 1093 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
Marc Treib | f0f6afc9 | 2017-10-04 09:05:47 | [diff] [blame] | 1094 | GURL("http://google.com/?q=something"), search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1095 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1096 | |
| 1097 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
Marc Treib | f0f6afc9 | 2017-10-04 09:05:47 | [diff] [blame] | 1098 | GURL("http://google.com/?q=something"), search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1099 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1100 | |
| 1101 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
Marc Treib | f0f6afc9 | 2017-10-04 09:05:47 | [diff] [blame] | 1102 | GURL("http://google.com/?q=something"), search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1103 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1104 | |
| 1105 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1106 | GURL("http://google.com/alt/#q=something"), |
| 1107 | search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1108 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1109 | |
| 1110 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
Marc Treib | f0f6afc9 | 2017-10-04 09:05:47 | [diff] [blame] | 1111 | GURL("http://google.com/alt/#q=something"), search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1112 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1113 | |
| 1114 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
Marc Treib | f0f6afc9 | 2017-10-04 09:05:47 | [diff] [blame] | 1115 | GURL("http://google.com/alt/#q=something"), search_terms_data_, &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1116 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1117 | |
| 1118 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
Marc Treib | f0f6afc9 | 2017-10-04 09:05:47 | [diff] [blame] | 1119 | GURL("http://google.com/alt/#q=something"), search_terms_data_, &result)); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1120 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1121 | |
| 1122 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1123 | GURL("http://google.ca/?q=something"), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1124 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1125 | |
| 1126 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1127 | GURL("http://google.ca/?q=something&q=anything"), |
| 1128 | search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1129 | EXPECT_EQ(base::string16(), result); |
[email protected] | 67d8b75 | 2013-04-03 17:33:27 | [diff] [blame] | 1130 | |
| 1131 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1132 | GURL("http://google.com/foo/?q=foo"), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1133 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1134 | |
[email protected] | 32e2d81b | 2012-10-16 19:03:25 | [diff] [blame] | 1135 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1136 | GURL("https://google.com/?q=foo"), search_terms_data_, &result)); |
[email protected] | 32e2d81b | 2012-10-16 19:03:25 | [diff] [blame] | 1137 | EXPECT_EQ(ASCIIToUTF16("foo"), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1138 | |
| 1139 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1140 | GURL("http://google.com:8080/?q=foo"), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1141 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1142 | |
| 1143 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1144 | 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] | 1145 | EXPECT_EQ(ASCIIToUTF16("1 2 3"), result); |
| 1146 | |
| 1147 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1148 | GURL("http://google.com/alt/?q=123#q=456"), |
| 1149 | search_terms_data_, &result)); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1150 | EXPECT_EQ(ASCIIToUTF16("456"), result); |
| 1151 | |
| 1152 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1153 | GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), |
| 1154 | search_terms_data_, &result)); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1155 | EXPECT_EQ(ASCIIToUTF16("123"), result); |
| 1156 | |
| 1157 | EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1158 | "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), |
| 1159 | search_terms_data_, &result)); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1160 | EXPECT_EQ(ASCIIToUTF16("789"), result); |
| 1161 | |
| 1162 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1163 | GURL("http://google.com/alt/?q="), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1164 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1165 | |
| 1166 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1167 | GURL("http://google.com/alt/?#q="), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1168 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1169 | |
| 1170 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1171 | GURL("http://google.com/alt/?q=#q="), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1172 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1173 | |
| 1174 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1175 | GURL("http://google.com/alt/?q=123#q="), search_terms_data_, &result)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 1176 | EXPECT_EQ(base::string16(), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1177 | |
| 1178 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1179 | GURL("http://google.com/alt/?q=#q=123"), search_terms_data_, &result)); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 1180 | EXPECT_EQ(ASCIIToUTF16("123"), result); |
| 1181 | } |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 1182 | |
alexmos | 294849f | 2015-03-14 00:55:06 | [diff] [blame] | 1183 | TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) { |
| 1184 | TemplateURLData data; |
| 1185 | data.SetURL("http://term-in-path.com/begin/{searchTerms}/end"); |
| 1186 | TemplateURL url(data); |
| 1187 | base::string16 result; |
| 1188 | |
| 1189 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1190 | GURL("http://term-in-path.com/begin/something/end"), |
| 1191 | search_terms_data_, &result)); |
| 1192 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 1193 | |
| 1194 | // "%20" must be converted to space. |
| 1195 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1196 | GURL("http://term-in-path.com/begin/a%20b%20c/end"), |
| 1197 | search_terms_data_, &result)); |
| 1198 | EXPECT_EQ(ASCIIToUTF16("a b c"), result); |
| 1199 | |
| 1200 | // Plus must not be converted to space. |
| 1201 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1202 | GURL("http://term-in-path.com/begin/1+2+3/end"), |
| 1203 | search_terms_data_, &result)); |
| 1204 | EXPECT_EQ(ASCIIToUTF16("1+2+3"), result); |
| 1205 | |
| 1206 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| 1207 | GURL("http://term-in-path.com/about"), search_terms_data_, &result)); |
| 1208 | EXPECT_EQ(base::string16(), result); |
| 1209 | |
| 1210 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| 1211 | GURL("http://term-in-path.com/begin"), search_terms_data_, &result)); |
| 1212 | EXPECT_EQ(base::string16(), result); |
| 1213 | |
| 1214 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| 1215 | GURL("http://term-in-path.com/end"), search_terms_data_, &result)); |
| 1216 | EXPECT_EQ(base::string16(), result); |
| 1217 | } |
| 1218 | |
vitbar | 27804d10 | 2015-04-15 10:54:41 | [diff] [blame] | 1219 | // Checks that the ExtractSearchTermsFromURL function works correctly |
| 1220 | // for urls containing non-latin characters in UTF8 encoding. |
| 1221 | TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) { |
| 1222 | TemplateURLData data; |
| 1223 | data.SetURL("http://utf-8.ru/?q={searchTerms}"); |
| 1224 | data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}"); |
| 1225 | data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}"); |
| 1226 | TemplateURL url(data); |
| 1227 | base::string16 result; |
| 1228 | |
| 1229 | // Russian text encoded with UTF-8. |
| 1230 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
vitbar | 7060e23c | 2015-04-22 13:19:55 | [diff] [blame] | 1231 | GURL("http://utf-8.ru/?q=%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D1%81%D1%82" |
| 1232 | "%D0%B2%D1%83%D0%B9,+%D0%BC%D0%B8%D1%80!"), |
vitbar | 27804d10 | 2015-04-15 10:54:41 | [diff] [blame] | 1233 | search_terms_data_, &result)); |
| 1234 | EXPECT_EQ( |
| 1235 | base::WideToUTF16( |
| 1236 | L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, " |
| 1237 | L"\x043C\x0438\x0440!"), |
| 1238 | result); |
| 1239 | |
| 1240 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
vitbar | 7060e23c | 2015-04-22 13:19:55 | [diff] [blame] | 1241 | GURL("http://utf-8.ru/#q=%D0%B4%D0%B2%D0%B0+%D1%81%D0%BB%D0%BE%D0%B2" |
| 1242 | "%D0%B0"), |
vitbar | 27804d10 | 2015-04-15 10:54:41 | [diff] [blame] | 1243 | search_terms_data_, &result)); |
| 1244 | EXPECT_EQ( |
| 1245 | base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"), |
| 1246 | result); |
| 1247 | |
| 1248 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
vitbar | 7060e23c | 2015-04-22 13:19:55 | [diff] [blame] | 1249 | GURL("http://utf-8.ru/path/%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%20%D0%90%20" |
| 1250 | "%D0%B8%20A"), |
vitbar | 27804d10 | 2015-04-15 10:54:41 | [diff] [blame] | 1251 | search_terms_data_, &result)); |
| 1252 | EXPECT_EQ( |
| 1253 | base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"), |
| 1254 | result); |
| 1255 | } |
| 1256 | |
| 1257 | // Checks that the ExtractSearchTermsFromURL function works correctly |
| 1258 | // for urls containing non-latin characters in non-UTF8 encoding. |
| 1259 | TEST_F(TemplateURLTest, ExtractSearchTermsFromNonUTF8URL) { |
| 1260 | TemplateURLData data; |
| 1261 | data.SetURL("http://windows-1251.ru/?q={searchTerms}"); |
| 1262 | data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}"); |
| 1263 | data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}"); |
| 1264 | data.input_encodings.push_back("windows-1251"); |
| 1265 | TemplateURL url(data); |
| 1266 | base::string16 result; |
| 1267 | |
| 1268 | // Russian text encoded with Windows-1251. |
| 1269 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1270 | GURL("http://windows-1251.ru/?q=%C7%E4%F0%E0%E2%F1%F2%E2%F3%E9%2C+" |
| 1271 | "%EC%E8%F0!"), |
| 1272 | search_terms_data_, &result)); |
| 1273 | EXPECT_EQ( |
| 1274 | base::WideToUTF16( |
| 1275 | L"\x0417\x0434\x0440\x0430\x0432\x0441\x0442\x0432\x0443\x0439, " |
| 1276 | L"\x043C\x0438\x0440!"), |
| 1277 | result); |
| 1278 | |
| 1279 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1280 | GURL("http://windows-1251.ru/#q=%E4%E2%E0+%F1%EB%EE%E2%E0"), |
| 1281 | search_terms_data_, &result)); |
| 1282 | EXPECT_EQ( |
| 1283 | base::WideToUTF16(L"\x0434\x0432\x0430 \x0441\x043B\x043E\x0432\x0430"), |
| 1284 | result); |
| 1285 | |
| 1286 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1287 | GURL("http://windows-1251.ru/path/%E1%F3%EA%E2%FB%20%C0%20%E8%20A"), |
| 1288 | search_terms_data_, &result)); |
| 1289 | EXPECT_EQ( |
| 1290 | base::WideToUTF16(L"\x0431\x0443\x043A\x0432\x044B \x0410 \x0438 A"), |
| 1291 | result); |
| 1292 | } |
| 1293 | |
jbroman | 5083202 | 2016-04-21 23:53:00 | [diff] [blame] | 1294 | // Checks that the ExtractSearchTermsFromURL function strips constant |
| 1295 | // prefix/suffix strings from the search terms param. |
| 1296 | TEST_F(TemplateURLTest, ExtractSearchTermsWithPrefixAndSuffix) { |
| 1297 | TemplateURLData data; |
| 1298 | data.alternate_urls.push_back( |
| 1299 | "http://www.example.com/?q=chromium-{searchTerms}@chromium.org"); |
| 1300 | data.alternate_urls.push_back( |
| 1301 | "http://www.example.com/chromium-{searchTerms}@chromium.org/info"); |
| 1302 | TemplateURL url(data); |
| 1303 | base::string16 result; |
| 1304 | |
| 1305 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1306 | GURL("http://www.example.com/[email protected]"), |
| 1307 | search_terms_data_, &result)); |
| 1308 | EXPECT_EQ(ASCIIToUTF16("dev"), result); |
| 1309 | |
| 1310 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1311 | GURL("http://www.example.com/[email protected]/info"), |
| 1312 | search_terms_data_, &result)); |
| 1313 | EXPECT_EQ(ASCIIToUTF16("dev"), result); |
| 1314 | |
| 1315 | // Don't match if the prefix and suffix aren't there. |
| 1316 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| 1317 | GURL("http://www.example.com/?q=invalid"), search_terms_data_, &result)); |
jbroman | e8b8728d | 2017-04-12 17:29:39 | [diff] [blame] | 1318 | |
| 1319 | // Don't match if the prefix and suffix overlap. |
| 1320 | TemplateURLData data_with_overlap; |
| 1321 | data.alternate_urls.push_back( |
| 1322 | "http://www.example.com/?q=goo{searchTerms}oogle"); |
| 1323 | TemplateURL url_with_overlap(data); |
| 1324 | EXPECT_FALSE(url_with_overlap.ExtractSearchTermsFromURL( |
| 1325 | GURL("http://www.example.com/?q=google"), search_terms_data_, &result)); |
jbroman | 5083202 | 2016-04-21 23:53:00 | [diff] [blame] | 1326 | } |
| 1327 | |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1328 | TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) { |
| 1329 | TemplateURLData data; |
| 1330 | data.SetURL("http://google.com/?q={searchTerms}"); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1331 | data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}"); |
| 1332 | data.alternate_urls.push_back( |
| 1333 | "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1334 | TemplateURL url(data); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1335 | TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane")); |
| 1336 | GURL result; |
| 1337 | |
| 1338 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1339 | GURL("http://google.com/?q=something"), search_terms, |
| 1340 | search_terms_data_, &result)); |
vitbar | f298455d | 2015-04-21 12:58:10 | [diff] [blame] | 1341 | EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1342 | |
| 1343 | result = GURL("http://should.not.change.com"); |
| 1344 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1345 | GURL("http://google.ca/?q=something"), search_terms, |
| 1346 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1347 | EXPECT_EQ(GURL("http://should.not.change.com"), result); |
| 1348 | |
| 1349 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1350 | GURL("http://google.com/foo/?q=foo"), search_terms, |
| 1351 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1352 | |
| 1353 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1354 | GURL("https://google.com/?q=foo"), search_terms, |
| 1355 | search_terms_data_, &result)); |
vitbar | f298455d | 2015-04-21 12:58:10 | [diff] [blame] | 1356 | EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1357 | |
| 1358 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1359 | GURL("http://google.com:8080/?q=foo"), search_terms, |
| 1360 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1361 | |
| 1362 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1363 | GURL("http://google.com/?q=1+2+3&b=456"), search_terms, |
| 1364 | search_terms_data_, &result)); |
vitbar | f298455d | 2015-04-21 12:58:10 | [diff] [blame] | 1365 | EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1366 | |
| 1367 | // Note: Spaces in REF parameters are not escaped. See TryEncoding() in |
| 1368 | // template_url.cc for details. |
| 1369 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1370 | GURL("http://google.com/alt/?q=123#q=456"), search_terms, |
| 1371 | search_terms_data_, &result)); |
vitbar | f298455d | 2015-04-21 12:58:10 | [diff] [blame] | 1372 | EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1373 | |
| 1374 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1375 | 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] | 1376 | search_terms_data_, &result)); |
vitbar | f298455d | 2015-04-21 12:58:10 | [diff] [blame] | 1377 | EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"), |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1378 | result); |
| 1379 | |
| 1380 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1381 | 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] | 1382 | search_terms, search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1383 | EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456" |
vitbar | f298455d | 2015-04-21 12:58:10 | [diff] [blame] | 1384 | "#j=abc&q=Bob+Morane&h=def9"), result); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1385 | |
| 1386 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1387 | GURL("http://google.com/alt/?q="), search_terms, |
| 1388 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1389 | |
| 1390 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1391 | GURL("http://google.com/alt/?#q="), search_terms, |
| 1392 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1393 | |
| 1394 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1395 | GURL("http://google.com/alt/?q=#q="), search_terms, |
| 1396 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1397 | |
| 1398 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1399 | GURL("http://google.com/alt/?q=123#q="), search_terms, |
| 1400 | search_terms_data_, &result)); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1401 | |
| 1402 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1403 | GURL("http://google.com/alt/?q=#q=123"), search_terms, |
| 1404 | search_terms_data_, &result)); |
vitbar | f298455d | 2015-04-21 12:58:10 | [diff] [blame] | 1405 | EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result); |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1406 | } |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1407 | |
alexmos | 294849f | 2015-03-14 00:55:06 | [diff] [blame] | 1408 | TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) { |
| 1409 | TemplateURLData data; |
| 1410 | data.SetURL("http://term-in-path.com/begin/{searchTerms}/end"); |
| 1411 | TemplateURL url(data); |
| 1412 | TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane")); |
| 1413 | GURL result; |
| 1414 | |
| 1415 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1416 | GURL("http://term-in-path.com/begin/something/end"), search_terms, |
| 1417 | search_terms_data_, &result)); |
| 1418 | EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result); |
| 1419 | |
| 1420 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1421 | GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms, |
| 1422 | search_terms_data_, &result)); |
| 1423 | EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result); |
| 1424 | |
| 1425 | result = GURL("http://should.not.change.com"); |
| 1426 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1427 | GURL("http://term-in-path.com/about"), search_terms, |
| 1428 | search_terms_data_, &result)); |
| 1429 | EXPECT_EQ(GURL("http://should.not.change.com"), result); |
| 1430 | } |
| 1431 | |
vitbar | f298455d | 2015-04-21 12:58:10 | [diff] [blame] | 1432 | // Checks that the ReplaceSearchTermsInURL function works correctly |
| 1433 | // for search terms containing non-latin characters for a search engine |
| 1434 | // using UTF-8 input encoding. |
| 1435 | TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) { |
| 1436 | TemplateURLData data; |
| 1437 | data.SetURL("http://utf-8.ru/?q={searchTerms}"); |
| 1438 | data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}"); |
| 1439 | data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}"); |
| 1440 | TemplateURL url(data); |
| 1441 | |
| 1442 | // Russian text which will be encoded with UTF-8. |
| 1443 | TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16( |
| 1444 | L"\x0442\x0435\x043A\x0441\x0442")); |
| 1445 | GURL result; |
| 1446 | |
| 1447 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1448 | GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_, |
| 1449 | &result)); |
| 1450 | EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"), |
| 1451 | result); |
| 1452 | |
| 1453 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1454 | GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_, |
| 1455 | &result)); |
| 1456 | EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"), |
| 1457 | result); |
| 1458 | |
| 1459 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1460 | GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_, |
| 1461 | &result)); |
| 1462 | EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"), |
| 1463 | result); |
| 1464 | } |
| 1465 | |
| 1466 | // Checks that the ReplaceSearchTermsInURL function works correctly |
| 1467 | // for search terms containing non-latin characters for a search engine |
| 1468 | // using non UTF-8 input encoding. |
| 1469 | TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) { |
| 1470 | TemplateURLData data; |
| 1471 | data.SetURL("http://windows-1251.ru/?q={searchTerms}"); |
| 1472 | data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}"); |
| 1473 | data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}"); |
| 1474 | data.input_encodings.push_back("windows-1251"); |
| 1475 | TemplateURL url(data); |
| 1476 | |
| 1477 | // Russian text which will be encoded with Windows-1251. |
| 1478 | TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16( |
| 1479 | L"\x0442\x0435\x043A\x0441\x0442")); |
| 1480 | GURL result; |
| 1481 | |
| 1482 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1483 | GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_, |
| 1484 | &result)); |
| 1485 | EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"), |
| 1486 | result); |
| 1487 | |
| 1488 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1489 | GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_, |
| 1490 | &result)); |
| 1491 | EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"), |
| 1492 | result); |
| 1493 | |
| 1494 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1495 | GURL("http://windows-1251.ru/path/a%20b"), search_terms, |
| 1496 | search_terms_data_, &result)); |
| 1497 | EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"), |
| 1498 | result); |
| 1499 | } |
| 1500 | |
Mark Pearson | 247fb8a | 2018-08-30 05:12:26 | [diff] [blame] | 1501 | // Test the |additional_query_params| field of SearchTermsArgs. |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1502 | TEST_F(TemplateURLTest, SuggestQueryParams) { |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1503 | TemplateURLData data; |
| 1504 | // Pick a URL with replacements before, during, and after the query, to ensure |
| 1505 | // we don't goof up any of them. |
| 1506 | data.SetURL("{google:baseURL}search?q={searchTerms}" |
| 1507 | "#{google:originalQueryForSuggestion}x"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1508 | TemplateURL url(data); |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1509 | |
Mark Pearson | 247fb8a | 2018-08-30 05:12:26 | [diff] [blame] | 1510 | // Baseline: no |additional_query_params| field. |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1511 | TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc")); |
| 1512 | search_terms.original_query = ASCIIToUTF16("def"); |
| 1513 | search_terms.accepted_suggestion = 0; |
| 1514 | EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1515 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1516 | |
Mark Pearson | 247fb8a | 2018-08-30 05:12:26 | [diff] [blame] | 1517 | // Set the |additional_query_params|. |
| 1518 | search_terms.additional_query_params = "pq=xyz"; |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1519 | 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] | 1520 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1521 | |
Mark Pearson | 247fb8a | 2018-08-30 05:12:26 | [diff] [blame] | 1522 | // Add |append_extra_query_params_from_command_line| into the mix, and ensure |
| 1523 | // it works. |
| 1524 | search_terms.append_extra_query_params_from_command_line = true; |
avi | 1772c1a | 2014-12-22 22:42:33 | [diff] [blame] | 1525 | base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1526 | switches::kExtraSearchQueryParams, "a=b"); |
| 1527 | 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] | 1528 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1529 | } |
| 1530 | |
Mark Pearson | 247fb8a | 2018-08-30 05:12:26 | [diff] [blame] | 1531 | // Test the |search_terms.append_extra_query_params_from_command_line| field of |
| 1532 | // SearchTermsArgs. |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1533 | TEST_F(TemplateURLTest, ExtraQueryParams) { |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1534 | TemplateURLData data; |
| 1535 | // Pick a URL with replacements before, during, and after the query, to ensure |
| 1536 | // we don't goof up any of them. |
| 1537 | data.SetURL("{google:baseURL}search?q={searchTerms}" |
| 1538 | "#{google:originalQueryForSuggestion}x"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1539 | TemplateURL url(data); |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1540 | |
Mark Pearson | 247fb8a | 2018-08-30 05:12:26 | [diff] [blame] | 1541 | // Baseline: no command-line args, no |
| 1542 | // |search_terms.append_extra_query_params_from_command_line| flag. |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1543 | TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc")); |
| 1544 | search_terms.original_query = ASCIIToUTF16("def"); |
| 1545 | search_terms.accepted_suggestion = 0; |
| 1546 | EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1547 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1548 | |
| 1549 | // Set the flag. Since there are no command-line args, this should have no |
| 1550 | // effect. |
Mark Pearson | 247fb8a | 2018-08-30 05:12:26 | [diff] [blame] | 1551 | search_terms.append_extra_query_params_from_command_line = true; |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1552 | EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1553 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1554 | |
| 1555 | // Now append the command-line arg. This should be inserted into the query. |
avi | 1772c1a | 2014-12-22 22:42:33 | [diff] [blame] | 1556 | base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1557 | switches::kExtraSearchQueryParams, "a=b"); |
| 1558 | 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] | 1559 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1560 | |
| 1561 | // Turn off the flag. Now the command-line arg should be ignored again. |
Mark Pearson | 247fb8a | 2018-08-30 05:12:26 | [diff] [blame] | 1562 | search_terms.append_extra_query_params_from_command_line = false; |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1563 | EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1564 | url.url_ref().ReplaceSearchTerms(search_terms, search_terms_data_)); |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1565 | } |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 1566 | |
| 1567 | // Tests replacing pageClassification. |
[email protected] | 57ac99b9 | 2013-11-13 01:30:17 | [diff] [blame] | 1568 | TEST_F(TemplateURLTest, ReplacePageClassification) { |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 1569 | TemplateURLData data; |
| 1570 | data.input_encodings.push_back("UTF-8"); |
| 1571 | data.SetURL("{google:baseURL}?{google:pageClassification}q={searchTerms}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1572 | TemplateURL url(data); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1573 | EXPECT_TRUE(url.url_ref().IsValid(search_terms_data_)); |
| 1574 | ASSERT_TRUE(url.url_ref().SupportsReplacement(search_terms_data_)); |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 1575 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); |
| 1576 | |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1577 | std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1578 | search_terms_data_); |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 1579 | EXPECT_EQ("http://www.google.com/?q=foo", result); |
| 1580 | |
[email protected] | 332d17d2 | 2014-06-20 16:56:03 | [diff] [blame] | 1581 | search_terms_args.page_classification = metrics::OmniboxEventProto::NTP; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1582 | result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1583 | search_terms_data_); |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 1584 | EXPECT_EQ("http://www.google.com/?pgcl=1&q=foo", result); |
| 1585 | |
| 1586 | search_terms_args.page_classification = |
[email protected] | 332d17d2 | 2014-06-20 16:56:03 | [diff] [blame] | 1587 | metrics::OmniboxEventProto::HOME_PAGE; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1588 | result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1589 | search_terms_data_); |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 1590 | EXPECT_EQ("http://www.google.com/?pgcl=3&q=foo", result); |
| 1591 | } |
[email protected] | 2328ee2 | 2013-08-08 23:00:19 | [diff] [blame] | 1592 | |
| 1593 | // Test the IsSearchResults function. |
| 1594 | TEST_F(TemplateURLTest, IsSearchResults) { |
| 1595 | TemplateURLData data; |
| 1596 | data.SetURL("http://bar/search?q={searchTerms}"); |
[email protected] | 2767c0fd | 2013-08-16 17:44:16 | [diff] [blame] | 1597 | data.new_tab_url = "http://bar/newtab"; |
[email protected] | 2328ee2 | 2013-08-08 23:00:19 | [diff] [blame] | 1598 | data.alternate_urls.push_back("http://bar/?q={searchTerms}"); |
| 1599 | data.alternate_urls.push_back("http://bar/#q={searchTerms}"); |
| 1600 | data.alternate_urls.push_back("http://bar/search#q{searchTerms}"); |
| 1601 | data.alternate_urls.push_back("http://bar/webhp#q={searchTerms}"); |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1602 | TemplateURL search_provider(data); |
[email protected] | 2328ee2 | 2013-08-08 23:00:19 | [diff] [blame] | 1603 | |
| 1604 | const struct { |
| 1605 | const char* const url; |
| 1606 | bool result; |
| 1607 | } url_data[] = { |
| 1608 | { "http://bar/search?q=foo&oq=foo", true, }, |
| 1609 | { "http://bar/?q=foo&oq=foo", true, }, |
| 1610 | { "http://bar/#output=search&q=foo&oq=foo", true, }, |
| 1611 | { "http://bar/webhp#q=foo&oq=foo", true, }, |
| 1612 | { "http://bar/#q=foo&oq=foo", true, }, |
| 1613 | { "http://bar/?ext=foo&q=foo#ref=bar", true, }, |
| 1614 | { "http://bar/url?url=http://www.foo.com/&q=foo#ref=bar", false, }, |
| 1615 | { "http://bar/", false, }, |
| 1616 | { "http://foo/", false, }, |
[email protected] | 2767c0fd | 2013-08-16 17:44:16 | [diff] [blame] | 1617 | { "http://bar/newtab", false, }, |
[email protected] | 2328ee2 | 2013-08-08 23:00:19 | [diff] [blame] | 1618 | }; |
| 1619 | |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 1620 | for (size_t i = 0; i < base::size(url_data); ++i) { |
[email protected] | 2328ee2 | 2013-08-08 23:00:19 | [diff] [blame] | 1621 | EXPECT_EQ(url_data[i].result, |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1622 | search_provider.IsSearchURL(GURL(url_data[i].url), |
| 1623 | search_terms_data_)); |
[email protected] | 2328ee2 | 2013-08-08 23:00:19 | [diff] [blame] | 1624 | } |
| 1625 | } |
[email protected] | a048de8a | 2013-10-02 18:30:06 | [diff] [blame] | 1626 | |
jdonnelly | 41c5b46a | 2015-07-10 21:24:38 | [diff] [blame] | 1627 | TEST_F(TemplateURLTest, SearchboxVersionIncludedForAnswers) { |
[email protected] | 9d70de1 | 2014-05-10 02:15:31 | [diff] [blame] | 1628 | TemplateURLData data; |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 1629 | search_terms_data_.set_google_base_url("http://bar/"); |
[email protected] | 9d70de1 | 2014-05-10 02:15:31 | [diff] [blame] | 1630 | data.SetURL("http://bar/search?q={searchTerms}&{google:searchVersion}xssi=t"); |
| 1631 | |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1632 | TemplateURL url(data); |
[email protected] | 9d70de1 | 2014-05-10 02:15:31 | [diff] [blame] | 1633 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1634 | std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1635 | search_terms_data_); |
[email protected] | 9d70de1 | 2014-05-10 02:15:31 | [diff] [blame] | 1636 | EXPECT_EQ("http://bar/search?q=foo&gs_rn=42&xssi=t", result); |
| 1637 | } |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 1638 | |
| 1639 | TEST_F(TemplateURLTest, SessionToken) { |
| 1640 | TemplateURLData data; |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 1641 | search_terms_data_.set_google_base_url("http://bar/"); |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 1642 | data.SetURL("http://bar/search?q={searchTerms}&{google:sessionToken}xssi=t"); |
| 1643 | |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1644 | TemplateURL url(data); |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 1645 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); |
| 1646 | search_terms_args.session_token = "SESSIONTOKENGOESHERE"; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1647 | std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1648 | search_terms_data_); |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 1649 | EXPECT_EQ("http://bar/search?q=foo&psi=SESSIONTOKENGOESHERE&xssi=t", result); |
| 1650 | |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1651 | TemplateURL url2(data); |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 1652 | search_terms_args.session_token = ""; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1653 | result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1654 | search_terms_data_); |
[email protected] | 2018424 | 2014-05-14 02:57:42 | [diff] [blame] | 1655 | EXPECT_EQ("http://bar/search?q=foo&xssi=t", result); |
| 1656 | } |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 1657 | |
| 1658 | TEST_F(TemplateURLTest, ContextualSearchParameters) { |
| 1659 | TemplateURLData data; |
[email protected] | 798baa8e | 2014-06-20 05:42:44 | [diff] [blame] | 1660 | search_terms_data_.set_google_base_url("http://bar/"); |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 1661 | data.SetURL("http://bar/_/contextualsearch?" |
| 1662 | "{google:contextualSearchVersion}" |
| 1663 | "{google:contextualSearchContextData}"); |
| 1664 | |
[email protected] | 168d0872 | 2014-06-18 07:13:28 | [diff] [blame] | 1665 | TemplateURL url(data); |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 1666 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1667 | std::string result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1668 | search_terms_data_); |
donnd | bb98274e | 2016-11-01 21:04:40 | [diff] [blame] | 1669 | EXPECT_EQ("http://bar/_/contextualsearch?", result); |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 1670 | |
Donn Denman | 01b1bf7 | 2018-08-14 16:25:20 | [diff] [blame] | 1671 | // Test the current common case, which uses no home country or previous |
| 1672 | // event. |
| 1673 | TemplateURLRef::SearchTermsArgs::ContextualSearchParams params( |
| 1674 | 2, 1, std::string(), 0, 0); |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 1675 | search_terms_args.contextual_search_params = params; |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 1676 | result = url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1677 | search_terms_data_); |
donnd | d9fc4d9 | 2016-09-16 00:30:49 | [diff] [blame] | 1678 | EXPECT_EQ( |
| 1679 | "http://bar/_/contextualsearch?" |
donnd | 0bc7329 | 2017-01-17 22:07:56 | [diff] [blame] | 1680 | "ctxs=2&" |
donnd | d9fc4d9 | 2016-09-16 00:30:49 | [diff] [blame] | 1681 | "ctxsl_coca=1", |
| 1682 | result); |
donnd | bb98274e | 2016-11-01 21:04:40 | [diff] [blame] | 1683 | |
Donn Denman | 01b1bf7 | 2018-08-14 16:25:20 | [diff] [blame] | 1684 | // Test the home country and non-zero event data case. |
donnd | bb98274e | 2016-11-01 21:04:40 | [diff] [blame] | 1685 | search_terms_args.contextual_search_params = |
Donn Denman | 01b1bf7 | 2018-08-14 16:25:20 | [diff] [blame] | 1686 | TemplateURLRef::SearchTermsArgs::ContextualSearchParams(2, 2, "CH", |
| 1687 | 1657713458, 5); |
donnd | bb98274e | 2016-11-01 21:04:40 | [diff] [blame] | 1688 | result = |
| 1689 | url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); |
| 1690 | |
| 1691 | EXPECT_EQ( |
| 1692 | "http://bar/_/contextualsearch?" |
| 1693 | "ctxs=2&" |
donnd | 0bc7329 | 2017-01-17 22:07:56 | [diff] [blame] | 1694 | "ctxsl_coca=2&" |
Donn Denman | 01b1bf7 | 2018-08-14 16:25:20 | [diff] [blame] | 1695 | "ctxs_hc=CH&" |
| 1696 | "ctxsl_pid=1657713458&" |
| 1697 | "ctxsl_per=5", |
donnd | bb98274e | 2016-11-01 21:04:40 | [diff] [blame] | 1698 | result); |
[email protected] | 448b17f5 | 2014-06-13 01:08:03 | [diff] [blame] | 1699 | } |
[email protected] | 44ccc9f | 2014-06-20 17:36:21 | [diff] [blame] | 1700 | |
| 1701 | TEST_F(TemplateURLTest, GenerateKeyword) { |
| 1702 | ASSERT_EQ(ASCIIToUTF16("foo"), |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 1703 | TemplateURL::GenerateKeyword(GURL("http://foo"))); |
[email protected] | 44ccc9f | 2014-06-20 17:36:21 | [diff] [blame] | 1704 | // www. should be stripped. |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 1705 | ASSERT_EQ(ASCIIToUTF16("foo"), |
| 1706 | TemplateURL::GenerateKeyword(GURL("http://www.foo"))); |
[email protected] | 44ccc9f | 2014-06-20 17:36:21 | [diff] [blame] | 1707 | // Make sure we don't get a trailing '/'. |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 1708 | ASSERT_EQ(ASCIIToUTF16("blah"), |
| 1709 | TemplateURL::GenerateKeyword(GURL("http://blah/"))); |
[email protected] | 44ccc9f | 2014-06-20 17:36:21 | [diff] [blame] | 1710 | // Don't generate the empty string. |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 1711 | ASSERT_EQ(ASCIIToUTF16("www"), |
| 1712 | TemplateURL::GenerateKeyword(GURL("http://www."))); |
alshabalin | e6212ac | 2015-07-23 08:03:00 | [diff] [blame] | 1713 | ASSERT_EQ( |
| 1714 | base::UTF8ToUTF16("\xd0\xb0\xd0\xb1\xd0\xb2"), |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 1715 | TemplateURL::GenerateKeyword(GURL("http://xn--80acd"))); |
a-v-y | 81695d0d | 2017-04-20 08:22:22 | [diff] [blame] | 1716 | |
| 1717 | // Generated keywords must always be in lowercase, because TemplateURLs always |
| 1718 | // converts keywords to lowercase in its constructor and TemplateURLService |
| 1719 | // stores TemplateURLs in maps using keyword as key. |
| 1720 | EXPECT_TRUE(IsLowerCase(TemplateURL::GenerateKeyword(GURL("http://BLAH/")))); |
| 1721 | EXPECT_TRUE(IsLowerCase( |
| 1722 | TemplateURL::GenerateKeyword(GURL("http://embeddedhtml.<head>/")))); |
[email protected] | 44ccc9f | 2014-06-20 17:36:21 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | TEST_F(TemplateURLTest, GenerateSearchURL) { |
| 1726 | struct GenerateSearchURLCase { |
| 1727 | const char* test_name; |
| 1728 | const char* url; |
| 1729 | const char* expected; |
| 1730 | } generate_url_cases[] = { |
| 1731 | { "invalid URL", "foo{searchTerms}", "" }, |
| 1732 | { "URL with no replacements", "http://foo/", "http://foo/" }, |
| 1733 | { "basic functionality", "http://foo/{searchTerms}", |
| 1734 | "http://foo/blah.blah.blah.blah.blah" } |
| 1735 | }; |
| 1736 | |
Avi Drissman | 2244c2d | 2018-12-25 23:08:02 | [diff] [blame^] | 1737 | for (size_t i = 0; i < base::size(generate_url_cases); ++i) { |
[email protected] | 44ccc9f | 2014-06-20 17:36:21 | [diff] [blame] | 1738 | TemplateURLData data; |
| 1739 | data.SetURL(generate_url_cases[i].url); |
| 1740 | TemplateURL t_url(data); |
| 1741 | EXPECT_EQ(t_url.GenerateSearchURL(search_terms_data_).spec(), |
| 1742 | generate_url_cases[i].expected) |
| 1743 | << generate_url_cases[i].test_name << " failed."; |
| 1744 | } |
| 1745 | } |
[email protected] | 9e9130ce | 2014-06-23 23:20:51 | [diff] [blame] | 1746 | |
| 1747 | TEST_F(TemplateURLTest, PrefetchQueryParameters) { |
| 1748 | TemplateURLData data; |
| 1749 | search_terms_data_.set_google_base_url("http://bar/"); |
| 1750 | data.SetURL("http://bar/search?q={searchTerms}&{google:prefetchQuery}xssi=t"); |
| 1751 | |
| 1752 | TemplateURL url(data); |
| 1753 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); |
| 1754 | search_terms_args.prefetch_query = "full query text"; |
| 1755 | search_terms_args.prefetch_query_type = "2338"; |
| 1756 | std::string result = |
| 1757 | url.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); |
| 1758 | EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t", |
| 1759 | result); |
| 1760 | |
| 1761 | TemplateURL url2(data); |
| 1762 | search_terms_args.prefetch_query.clear(); |
| 1763 | search_terms_args.prefetch_query_type.clear(); |
| 1764 | result = |
| 1765 | url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); |
| 1766 | EXPECT_EQ("http://bar/search?q=foo&xssi=t", result); |
| 1767 | } |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 1768 | |
| 1769 | // Tests that TemplateURL works correctly after changing the Google base URL |
| 1770 | // and invalidating cached values. |
| 1771 | TEST_F(TemplateURLTest, InvalidateCachedValues) { |
| 1772 | TemplateURLData data; |
| 1773 | data.SetURL("{google:baseURL}search?q={searchTerms}"); |
| 1774 | data.suggestions_url = "{google:baseSuggestURL}search?q={searchTerms}"; |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 1775 | data.image_url = "{google:baseURL}searchbyimage/upload"; |
| 1776 | data.new_tab_url = "{google:baseURL}_/chrome/newtab"; |
| 1777 | data.contextual_search_url = "{google:baseURL}_/contextualsearch"; |
| 1778 | data.alternate_urls.push_back("{google:baseURL}s#q={searchTerms}"); |
| 1779 | TemplateURL url(data); |
| 1780 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("X")); |
| 1781 | base::string16 search_terms; |
| 1782 | |
| 1783 | EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_)); |
| 1784 | EXPECT_EQ("http://www.google.com/search?q=X", |
| 1785 | url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1786 | search_terms_data_)); |
| 1787 | EXPECT_EQ("http://www.google.com/s#q=X", |
| 1788 | url.url_refs()[0].ReplaceSearchTerms(search_terms_args, |
| 1789 | search_terms_data_)); |
| 1790 | EXPECT_EQ("http://www.google.com/search?q=X", |
| 1791 | url.url_refs()[1].ReplaceSearchTerms(search_terms_args, |
| 1792 | search_terms_data_)); |
| 1793 | EXPECT_EQ("http://www.google.com/complete/search?q=X", |
| 1794 | url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args, |
| 1795 | search_terms_data_)); |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 1796 | EXPECT_EQ("http://www.google.com/searchbyimage/upload", |
| 1797 | url.image_url_ref().ReplaceSearchTerms(search_terms_args, |
| 1798 | search_terms_data_)); |
| 1799 | EXPECT_EQ("http://www.google.com/_/chrome/newtab", |
| 1800 | url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args, |
| 1801 | search_terms_data_)); |
| 1802 | EXPECT_EQ("http://www.google.com/_/contextualsearch", |
| 1803 | url.contextual_search_url_ref().ReplaceSearchTerms( |
| 1804 | search_terms_args, search_terms_data_)); |
| 1805 | |
| 1806 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1807 | GURL("http://www.google.com/search?q=Y+Z"), |
| 1808 | search_terms_data_, &search_terms)); |
| 1809 | EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms); |
| 1810 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1811 | GURL("http://www.google.com/s#q=123"), |
| 1812 | search_terms_data_, &search_terms)); |
| 1813 | EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms); |
| 1814 | |
| 1815 | search_terms_data_.set_google_base_url("https://www.foo.org/"); |
| 1816 | url.InvalidateCachedValues(); |
| 1817 | |
| 1818 | EXPECT_TRUE(url.HasGoogleBaseURLs(search_terms_data_)); |
| 1819 | EXPECT_EQ("https://www.foo.org/search?q=X", |
| 1820 | url.url_ref().ReplaceSearchTerms(search_terms_args, |
| 1821 | search_terms_data_)); |
| 1822 | EXPECT_EQ("https://www.foo.org/s#q=X", |
| 1823 | url.url_refs()[0].ReplaceSearchTerms(search_terms_args, |
| 1824 | search_terms_data_)); |
| 1825 | EXPECT_EQ("https://www.foo.org/search?q=X", |
| 1826 | url.url_refs()[1].ReplaceSearchTerms(search_terms_args, |
| 1827 | search_terms_data_)); |
| 1828 | EXPECT_EQ("https://www.foo.org/complete/search?q=X", |
| 1829 | url.suggestions_url_ref().ReplaceSearchTerms(search_terms_args, |
| 1830 | search_terms_data_)); |
vitbar | 5bd8c25 | 2016-01-29 18:44:51 | [diff] [blame] | 1831 | EXPECT_EQ("https://www.foo.org/searchbyimage/upload", |
| 1832 | url.image_url_ref().ReplaceSearchTerms(search_terms_args, |
| 1833 | search_terms_data_)); |
| 1834 | EXPECT_EQ("https://www.foo.org/_/chrome/newtab", |
| 1835 | url.new_tab_url_ref().ReplaceSearchTerms(search_terms_args, |
| 1836 | search_terms_data_)); |
| 1837 | EXPECT_EQ("https://www.foo.org/_/contextualsearch", |
| 1838 | url.contextual_search_url_ref().ReplaceSearchTerms( |
| 1839 | search_terms_args, search_terms_data_)); |
| 1840 | |
| 1841 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1842 | GURL("https://www.foo.org/search?q=Y+Z"), |
| 1843 | search_terms_data_, &search_terms)); |
| 1844 | EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms); |
| 1845 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1846 | GURL("https://www.foo.org/s#q=123"), |
| 1847 | search_terms_data_, &search_terms)); |
| 1848 | EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms); |
| 1849 | |
| 1850 | search_terms_data_.set_google_base_url("http://www.google.com/"); |
| 1851 | } |
Troy Hildebrandt | 47ac4d8b | 2018-05-10 22:40:00 | [diff] [blame] | 1852 | |
| 1853 | // Tests the use of wildcards in the path to ensure both extracting search terms |
| 1854 | // and generating a search URL work correctly. |
| 1855 | TEST_F(TemplateURLTest, PathWildcard) { |
| 1856 | TemplateURLData data; |
| 1857 | data.SetURL( |
| 1858 | "https://www.google.com/search{google:pathWildcard}?q={searchTerms}"); |
| 1859 | TemplateURL url(data); |
| 1860 | |
| 1861 | // Test extracting search terms from a URL. |
| 1862 | base::string16 search_terms; |
| 1863 | url.ExtractSearchTermsFromURL(GURL("https://www.google.com/search?q=testing"), |
| 1864 | search_terms_data_, &search_terms); |
| 1865 | EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms); |
| 1866 | url.ExtractSearchTermsFromURL( |
| 1867 | GURL("https://www.google.com/search;_this_is_a_test;_?q=testing"), |
| 1868 | search_terms_data_, &search_terms); |
| 1869 | EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms); |
| 1870 | |
| 1871 | // Tests overlapping prefix/suffix. |
| 1872 | data.SetURL( |
| 1873 | "https://www.google.com/search{google:pathWildcard}rch?q={searchTerms}"); |
| 1874 | TemplateURL overlap_url(data); |
| 1875 | overlap_url.ExtractSearchTermsFromURL( |
| 1876 | GURL("https://www.google.com/search?q=testing"), search_terms_data_, |
| 1877 | &search_terms); |
| 1878 | EXPECT_TRUE(search_terms.empty()); |
| 1879 | |
| 1880 | // Tests wildcard at beginning of path so we only have a suffix. |
| 1881 | data.SetURL( |
| 1882 | "https://www.google.com/{google:pathWildcard}rch?q={searchTerms}"); |
| 1883 | TemplateURL suffix_url(data); |
| 1884 | suffix_url.ExtractSearchTermsFromURL( |
| 1885 | GURL("https://www.google.com/search?q=testing"), search_terms_data_, |
| 1886 | &search_terms); |
| 1887 | EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms); |
| 1888 | |
| 1889 | // Tests wildcard between prefix/suffix. |
| 1890 | overlap_url.ExtractSearchTermsFromURL( |
| 1891 | GURL("https://www.google.com/search_testing_rch?q=testing"), |
| 1892 | search_terms_data_, &search_terms); |
| 1893 | EXPECT_EQ(base::ASCIIToUTF16("testing"), search_terms); |
| 1894 | |
| 1895 | // Test generating a URL. |
| 1896 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foo")); |
| 1897 | GURL generated_url; |
| 1898 | url.ReplaceSearchTermsInURL(url.GenerateSearchURL(search_terms_data_), |
| 1899 | search_terms_args, search_terms_data_, |
| 1900 | &generated_url); |
| 1901 | EXPECT_EQ("https://www.google.com/search?q=foo", generated_url.spec()); |
| 1902 | } |