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