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