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