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