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