[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 5 | #include "base/base_paths.h" |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 6 | #include "base/command_line.h" |
[email protected] | 24a555b6 | 2013-06-10 22:01:17 | [diff] [blame] | 7 | #include "base/strings/string_util.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 8 | #include "base/strings/utf_string_conversions.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 9 | #include "chrome/browser/browser_process.h" |
| 10 | #include "chrome/browser/rlz/rlz.h" |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 11 | #include "chrome/browser/search_engines/search_terms_data.h" |
[email protected] | d54e03a5 | 2009-01-16 00:31:04 | [diff] [blame] | 12 | #include "chrome/browser/search_engines/template_url.h" |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 13 | #include "chrome/common/chrome_switches.h" |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame^] | 14 | #include "chrome/common/chrome_version_info.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
[email protected] | 81d9b72d | 2012-03-26 22:29:17 | [diff] [blame] | 17 | #if defined(ENABLE_RLZ) |
| 18 | #include "chrome/browser/google/google_util.h" |
| 19 | #endif |
| 20 | |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 21 | #if defined(OS_ANDROID) |
| 22 | #include "chrome/browser/search_engines/search_terms_data_android.h" |
| 23 | #endif |
| 24 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 25 | // TestSearchTermsData -------------------------------------------------------- |
| 26 | |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 27 | // Simple implementation of SearchTermsData. |
| 28 | class TestSearchTermsData : public SearchTermsData { |
| 29 | public: |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 30 | explicit TestSearchTermsData(const std::string& google_base_url); |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 31 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 32 | virtual std::string GoogleBaseURLValue() const OVERRIDE; |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 33 | |
| 34 | private: |
| 35 | std::string google_base_url_; |
| 36 | |
| 37 | DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData); |
| 38 | }; |
| 39 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 40 | TestSearchTermsData::TestSearchTermsData(const std::string& google_base_url) |
| 41 | : google_base_url_(google_base_url) { |
| 42 | } |
| 43 | |
| 44 | std::string TestSearchTermsData::GoogleBaseURLValue() const { |
| 45 | return google_base_url_; |
| 46 | } |
| 47 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 48 | // TemplateURLTest ------------------------------------------------------------ |
| 49 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 50 | class TemplateURLTest : public testing::Test { |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 51 | public: |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 52 | void CheckSuggestBaseURL(const std::string& base_url, |
| 53 | const std::string& base_suggest_url) const; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 54 | }; |
| 55 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 56 | void TemplateURLTest::CheckSuggestBaseURL( |
| 57 | const std::string& base_url, |
| 58 | const std::string& base_suggest_url) const { |
| 59 | TestSearchTermsData search_terms_data(base_url); |
| 60 | EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue()); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | // Actual tests --------------------------------------------------------------- |
| 65 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 66 | TEST_F(TemplateURLTest, Defaults) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 67 | TemplateURLData data; |
| 68 | EXPECT_FALSE(data.show_in_default_list); |
| 69 | EXPECT_FALSE(data.safe_for_autoreplace); |
| 70 | EXPECT_EQ(0, data.prepopulate_id); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST_F(TemplateURLTest, TestValidWithComplete) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 74 | TemplateURLData data; |
| 75 | data.SetURL("{searchTerms}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 76 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 77 | EXPECT_TRUE(url.url_ref().IsValid()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | TEST_F(TemplateURLTest, URLRefTestSearchTerms) { |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 81 | struct SearchTermsCase { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 82 | const char* url; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 83 | const string16 terms; |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 84 | const std::string output; |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 85 | } search_term_cases[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 86 | { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 87 | "http://foosea%20rch/bar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 88 | { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 89 | "http://foosea%20rch/bar?boo=abc" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 90 | { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 91 | "http://foo/?boo=sea+rch%2Fbar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 92 | { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"), |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 93 | "http://en.wikipedia.org/wiki/%3F" } |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 94 | }; |
| 95 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { |
| 96 | const SearchTermsCase& value = search_term_cases[i]; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 97 | TemplateURLData data; |
| 98 | data.SetURL(value.url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 99 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 100 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 101 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 102 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 103 | TemplateURLRef::SearchTermsArgs(value.terms))); |
[email protected] | c31a979c | 2012-05-31 23:57:16 | [diff] [blame] | 104 | ASSERT_TRUE(result.is_valid()); |
| 105 | EXPECT_EQ(value.output, result.spec()); |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 106 | } |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST_F(TemplateURLTest, URLRefTestCount) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 110 | TemplateURLData data; |
| 111 | data.SetURL("http://foo{searchTerms}{count?}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 112 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 113 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 114 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 115 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 116 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 117 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 118 | EXPECT_EQ("http://foox/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | TEST_F(TemplateURLTest, URLRefTestCount2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 122 | TemplateURLData data; |
| 123 | data.SetURL("http://foo{searchTerms}{count}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 124 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 125 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 126 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 127 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 128 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 129 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 130 | EXPECT_EQ("http://foox10/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST_F(TemplateURLTest, URLRefTestIndices) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 134 | TemplateURLData data; |
| 135 | data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 136 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 137 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 138 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 139 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 140 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 141 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 142 | EXPECT_EQ("http://fooxxy/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | TEST_F(TemplateURLTest, URLRefTestIndices2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 146 | TemplateURLData data; |
| 147 | data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 148 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 149 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 150 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 151 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 152 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 153 | ASSERT_TRUE(result.is_valid()); |
[email protected] | 405aae2 | 2012-03-29 20:36:13 | [diff] [blame] | 154 | EXPECT_EQ("http://fooxx1y1/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | TEST_F(TemplateURLTest, URLRefTestEncoding) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 158 | TemplateURLData data; |
| 159 | data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 160 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 161 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 162 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 163 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 164 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 165 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 166 | EXPECT_EQ("http://fooxxutf-8ya/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame^] | 169 | TEST_F(TemplateURLTest, URLRefTestImageURLWithPOST) { |
| 170 | const char kInvalidPostParamsString[] = |
| 171 | "unknown_template={UnknownTemplate},bad_value=bad{value}," |
| 172 | "{google:sbiSource}"; |
| 173 | // List all accpectable parameter format in valid_post_params_string. it is |
| 174 | // expected like: "name0=,name1=value1,name2={template1}" |
| 175 | const char kValidPostParamsString[] = |
| 176 | "image_content={google:imageThumbnail},image_url={google:imageURL}," |
| 177 | "sbisrc={google:imageSearchSource},language={language},empty_param=," |
| 178 | "constant_param=constant"; |
| 179 | const char KImageSearchURL[] = "http://foo.com/sbi"; |
| 180 | |
| 181 | TemplateURLData data; |
| 182 | data.image_url = KImageSearchURL; |
| 183 | |
| 184 | // Try to parse invalid post parameters. |
| 185 | data.image_url_post_params = kInvalidPostParamsString; |
| 186 | TemplateURL url_bad(NULL, data); |
| 187 | ASSERT_FALSE(url_bad.image_url_ref().IsValid()); |
| 188 | const TemplateURLRef::PostParams& bad_post_params = |
| 189 | url_bad.image_url_ref().post_params_; |
| 190 | ASSERT_EQ(2U, bad_post_params.size()); |
| 191 | EXPECT_EQ("unknown_template", bad_post_params[0].first); |
| 192 | EXPECT_EQ("{UnknownTemplate}", bad_post_params[0].second); |
| 193 | EXPECT_EQ("bad_value", bad_post_params[1].first); |
| 194 | EXPECT_EQ("bad{value}", bad_post_params[1].second); |
| 195 | |
| 196 | // Try to parse valid post parameters. |
| 197 | data.image_url_post_params = kValidPostParamsString; |
| 198 | TemplateURL url(NULL, data); |
| 199 | ASSERT_TRUE(url.image_url_ref().IsValid()); |
| 200 | ASSERT_FALSE(url.image_url_ref().SupportsReplacement()); |
| 201 | |
| 202 | // Check term replacement. |
| 203 | TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("X")); |
| 204 | search_args.image_thumbnail_content = "dummy-image-thumbnail"; |
| 205 | search_args.image_url = GURL("http://dummyimage.com/dummy.jpg"); |
| 206 | // Replacement operation with no post_data buffer should still return |
| 207 | // the parsed URL. |
| 208 | GURL result(url.image_url_ref().ReplaceSearchTerms(search_args)); |
| 209 | ASSERT_TRUE(result.is_valid()); |
| 210 | EXPECT_EQ(KImageSearchURL, result.spec()); |
| 211 | std::string post_data; |
| 212 | TestSearchTermsData search_terms_data("http://X"); |
| 213 | result = GURL(url.image_url_ref().ReplaceSearchTermsUsingTermsData( |
| 214 | search_args, search_terms_data, &post_data)); |
| 215 | ASSERT_TRUE(result.is_valid()); |
| 216 | EXPECT_EQ(KImageSearchURL, result.spec()); |
| 217 | ASSERT_FALSE(post_data.empty()); |
| 218 | |
| 219 | // Check parsed result of post parameters. |
| 220 | const TemplateURLRef::Replacements& replacements = |
| 221 | url.image_url_ref().replacements_; |
| 222 | const TemplateURLRef::PostParams& post_params = |
| 223 | url.image_url_ref().post_params_; |
| 224 | EXPECT_EQ(6U, post_params.size()); |
| 225 | for (TemplateURLRef::PostParams::const_iterator i = post_params.begin(); |
| 226 | i != post_params.end(); ++i) { |
| 227 | TemplateURLRef::Replacements::const_iterator j = replacements.begin(); |
| 228 | for (; j != replacements.end(); ++j) { |
| 229 | if (j->is_post_param && j->index == |
| 230 | static_cast<size_t>(i - post_params.begin())) { |
| 231 | switch (j->type) { |
| 232 | case TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL: |
| 233 | EXPECT_EQ("image_content", i->first); |
| 234 | EXPECT_EQ(search_args.image_thumbnail_content, i->second); |
| 235 | break; |
| 236 | case TemplateURLRef::GOOGLE_IMAGE_URL: |
| 237 | EXPECT_EQ("image_url", i->first); |
| 238 | EXPECT_EQ(search_args.image_url.spec(), i->second); |
| 239 | break; |
| 240 | case TemplateURLRef::LANGUAGE: |
| 241 | EXPECT_EQ("language", i->first); |
| 242 | EXPECT_EQ("en", i->second); |
| 243 | break; |
| 244 | default: |
| 245 | ADD_FAILURE(); // Should never go here. |
| 246 | } |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | if (j != replacements.end()) |
| 251 | continue; |
| 252 | if (i->first == "empty_param") { |
| 253 | EXPECT_TRUE(i->second.empty()); |
| 254 | } else if (i->first == "sbisrc") { |
| 255 | EXPECT_EQ("unknown", i->second); |
| 256 | } else { |
| 257 | EXPECT_EQ("constant_param", i->first); |
| 258 | EXPECT_EQ("constant", i->second); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
[email protected] | d88cb20 | 2011-08-17 20:03:01 | [diff] [blame] | 263 | // Test that setting the prepopulate ID from TemplateURL causes the stored |
| 264 | // TemplateURLRef to handle parsing the URL parameters differently. |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 265 | TEST_F(TemplateURLTest, SetPrepopulatedAndParse) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 266 | TemplateURLData data; |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 267 | data.SetURL("http://foo{fhqwhgads}bar"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 268 | TemplateURL url(NULL, data); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 269 | TemplateURLRef::Replacements replacements; |
| 270 | bool valid = false; |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 271 | EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL( |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame^] | 272 | "http://foo{fhqwhgads}bar", &replacements, NULL, &valid)); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 273 | EXPECT_TRUE(replacements.empty()); |
| 274 | EXPECT_TRUE(valid); |
| 275 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 276 | data.prepopulate_id = 123; |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 277 | TemplateURL url2(NULL, data); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 278 | EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar", |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame^] | 279 | &replacements, NULL, |
| 280 | &valid)); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 281 | EXPECT_TRUE(replacements.empty()); |
| 282 | EXPECT_TRUE(valid); |
| 283 | } |
| 284 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 285 | TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 286 | TemplateURLData data; |
| 287 | data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 288 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 289 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 290 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 291 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 292 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 293 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 294 | EXPECT_EQ("http://fooxutf-8axyb/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | TEST_F(TemplateURLTest, URLRefTestEncoding2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 298 | TemplateURLData data; |
| 299 | data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 300 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 301 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 302 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 303 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 304 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 305 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 306 | EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 309 | TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) { |
| 310 | struct SearchTermsCase { |
| 311 | const char* url; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 312 | const string16 terms; |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 313 | const char* output; |
| 314 | } search_term_cases[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 315 | { "{google:baseURL}{language}{searchTerms}", string16(), |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 316 | "http://example.com/e/en" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 317 | { "{google:baseSuggestURL}{searchTerms}", string16(), |
[email protected] | 014010e | 2011-10-01 04:12:44 | [diff] [blame] | 318 | "http://example.com/complete/" } |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | TestSearchTermsData search_terms_data("http://example.com/e/"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 322 | TemplateURLData data; |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 323 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { |
| 324 | const SearchTermsCase& value = search_term_cases[i]; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 325 | data.SetURL(value.url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 326 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 327 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 328 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 329 | GURL result(url.url_ref().ReplaceSearchTermsUsingTermsData( |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame^] | 330 | TemplateURLRef::SearchTermsArgs(value.terms), search_terms_data, NULL)); |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 331 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 332 | EXPECT_EQ(value.output, result.spec()); |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 336 | TEST_F(TemplateURLTest, URLRefTermToWide) { |
| 337 | struct ToWideCase { |
| 338 | const char* encoded_search_term; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 339 | const string16 expected_decoded_term; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 340 | } to_wide_cases[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 341 | {"hello+world", ASCIIToUTF16("hello world")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 342 | // Test some big-5 input. |
[email protected] | 47e870b | 2013-02-24 21:14:53 | [diff] [blame] | 343 | {"%a7A%A6%6e+to+you", WideToUTF16(L"\x4f60\x597d to you")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 344 | // Test some UTF-8 input. We should fall back to this when the encoding |
| 345 | // doesn't look like big-5. We have a '5' in the middle, which is an invalid |
| 346 | // Big-5 trailing byte. |
[email protected] | 47e870b | 2013-02-24 21:14:53 | [diff] [blame] | 347 | {"%e4%bd%a05%e5%a5%bd+to+you", WideToUTF16(L"\x4f60\x35\x597d to you")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 348 | // Undecodable input should stay escaped. |
[email protected] | 47e870b | 2013-02-24 21:14:53 | [diff] [blame] | 349 | {"%91%01+abcd", WideToUTF16(L"%91%01 abcd")}, |
[email protected] | 7df4348 | 2009-07-31 19:37:44 | [diff] [blame] | 350 | // Make sure we convert %2B to +. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 351 | {"C%2B%2B", ASCIIToUTF16("C++")}, |
[email protected] | 7df4348 | 2009-07-31 19:37:44 | [diff] [blame] | 352 | // C%2B is escaped as C%252B, make sure we unescape it properly. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 353 | {"C%252B", ASCIIToUTF16("C%2B")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 354 | }; |
| 355 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 356 | // 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] | 357 | TemplateURLData data; |
| 358 | data.SetURL("http://foo?q={searchTerms}"); |
| 359 | data.input_encodings.push_back("big-5"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 360 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 361 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 362 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 363 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) { |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 364 | EXPECT_EQ(to_wide_cases[i].expected_decoded_term, |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 365 | url.url_ref().SearchTermToString16( |
| 366 | to_wide_cases[i].encoded_search_term)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 370 | TEST_F(TemplateURLTest, DisplayURLToURLRef) { |
| 371 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 372 | const std::string url; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 373 | const string16 expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 374 | } test_data[] = { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 375 | { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 376 | ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 377 | { "http://X", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 378 | ASCIIToUTF16("http://X") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 379 | { "http://foo{searchTerms", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 380 | ASCIIToUTF16("http://foo{searchTerms") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 381 | { "http://foo{searchTerms}{language}", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 382 | ASCIIToUTF16("http://foo%s{language}") }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 383 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 384 | TemplateURLData data; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 385 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 386 | data.SetURL(test_data[i].url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 387 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 388 | EXPECT_EQ(test_data[i].expected_result, url.url_ref().DisplayURL()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 389 | EXPECT_EQ(test_data[i].url, |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 390 | TemplateURLRef::DisplayURLToURLRef(url.url_ref().DisplayURL())); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | TEST_F(TemplateURLTest, ReplaceSearchTerms) { |
| 395 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 396 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 397 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 398 | } test_data[] = { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 399 | { "http://foo/{language}{searchTerms}{inputEncoding}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 400 | "http://foo/{language}XUTF-8" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 401 | { "http://foo/{language}{inputEncoding}{searchTerms}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 402 | "http://foo/{language}UTF-8X" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 403 | { "http://foo/{searchTerms}{language}{inputEncoding}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 404 | "http://foo/X{language}UTF-8" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 405 | { "http://foo/{searchTerms}{inputEncoding}{language}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 406 | "http://foo/XUTF-8{language}" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 407 | { "http://foo/{inputEncoding}{searchTerms}{language}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 408 | "http://foo/UTF-8X{language}" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 409 | { "http://foo/{inputEncoding}{language}{searchTerms}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 410 | "http://foo/UTF-8{language}X" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 411 | { "http://foo/{language}a{searchTerms}a{inputEncoding}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 412 | "http://foo/{language}aXaUTF-8a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 413 | { "http://foo/{language}a{inputEncoding}a{searchTerms}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 414 | "http://foo/{language}aUTF-8aXa" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 415 | { "http://foo/{searchTerms}a{language}a{inputEncoding}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 416 | "http://foo/Xa{language}aUTF-8a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 417 | { "http://foo/{searchTerms}a{inputEncoding}a{language}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 418 | "http://foo/XaUTF-8a{language}a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 419 | { "http://foo/{inputEncoding}a{searchTerms}a{language}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 420 | "http://foo/UTF-8aXa{language}a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 421 | { "http://foo/{inputEncoding}a{language}a{searchTerms}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 422 | "http://foo/UTF-8a{language}aXa" }, |
| 423 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 424 | TemplateURLData data; |
| 425 | data.input_encodings.push_back("UTF-8"); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 426 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 427 | data.SetURL(test_data[i].url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 428 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 429 | EXPECT_TRUE(url.url_ref().IsValid()); |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 430 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 431 | std::string expected_result = test_data[i].expected_result; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 432 | ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}", |
[email protected] | d70539de | 2009-06-24 22:17:06 | [diff] [blame] | 433 | g_browser_process->GetApplicationLocale()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 434 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 435 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("X")))); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 436 | ASSERT_TRUE(result.is_valid()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 437 | EXPECT_EQ(expected_result, result.spec()); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | |
| 442 | // Tests replacing search terms in various encodings and making sure the |
| 443 | // generated URL matches the expected value. |
| 444 | TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) { |
| 445 | struct TestData { |
| 446 | const std::string encoding; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 447 | const string16 search_term; |
[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] | 47e870b | 2013-02-24 21:14:53 | [diff] [blame] | 451 | { "BIG5", WideToUTF16(L"\x60BD"), |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 452 | "http://foo/?{searchTerms}{inputEncoding}", |
[email protected] | 3c75f0d | 2010-03-02 05:51:17 | [diff] [blame] | 453 | "http://foo/?%B1~BIG5" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 454 | { "UTF-8", ASCIIToUTF16("blah"), |
| 455 | "http://foo/?{searchTerms}{inputEncoding}", |
[email protected] | 3c75f0d | 2010-03-02 05:51:17 | [diff] [blame] | 456 | "http://foo/?blahUTF-8" }, |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 457 | { "Shift_JIS", UTF8ToUTF16("\xe3\x81\x82"), |
| 458 | "http://foo/{searchTerms}/bar", |
| 459 | "http://foo/%82%A0/bar"}, |
| 460 | { "Shift_JIS", UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"), |
| 461 | "http://foo/{searchTerms}/bar", |
| 462 | "http://foo/%82%A0%20%82%A2/bar"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 463 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 464 | TemplateURLData data; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 465 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 466 | data.SetURL(test_data[i].url); |
| 467 | data.input_encodings.clear(); |
| 468 | data.input_encodings.push_back(test_data[i].encoding); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 469 | TemplateURL url(NULL, data); |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 470 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 471 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 472 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 473 | TemplateURLRef::SearchTermsArgs(test_data[i].search_term))); |
| 474 | ASSERT_TRUE(result.is_valid()); |
| 475 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // Tests replacing assisted query stats (AQS) in various scenarios. |
| 480 | TEST_F(TemplateURLTest, ReplaceAssistedQueryStats) { |
| 481 | struct TestData { |
| 482 | const string16 search_term; |
| 483 | const std::string aqs; |
| 484 | const std::string base_url; |
| 485 | const std::string url; |
| 486 | const std::string expected_result; |
| 487 | } test_data[] = { |
| 488 | // No HTTPS, no AQS. |
| 489 | { ASCIIToUTF16("foo"), |
| 490 | "chrome.0.0l6", |
| 491 | "http://foo/", |
| 492 | "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", |
| 493 | "http://foo/?foo" }, |
| 494 | // HTTPS available, AQS should be replaced. |
| 495 | { ASCIIToUTF16("foo"), |
| 496 | "chrome.0.0l6", |
| 497 | "https://foo/", |
| 498 | "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", |
| 499 | "https://foo/?fooaqs=chrome.0.0l6&" }, |
| 500 | // HTTPS available, however AQS is empty. |
| 501 | { ASCIIToUTF16("foo"), |
| 502 | "", |
| 503 | "https://foo/", |
| 504 | "{google:baseURL}?{searchTerms}{google:assistedQueryStats}", |
| 505 | "https://foo/?foo" }, |
| 506 | // No {google:baseURL} and protocol is HTTP, we must not substitute AQS. |
| 507 | { ASCIIToUTF16("foo"), |
| 508 | "chrome.0.0l6", |
| 509 | "", |
| 510 | "http://foo?{searchTerms}{google:assistedQueryStats}", |
| 511 | "http://foo/?foo" }, |
| 512 | // A non-Google search provider with HTTPS should allow AQS. |
| 513 | { ASCIIToUTF16("foo"), |
| 514 | "chrome.0.0l6", |
| 515 | "", |
| 516 | "https://foo?{searchTerms}{google:assistedQueryStats}", |
| 517 | "https://foo/?fooaqs=chrome.0.0l6&" }, |
| 518 | }; |
| 519 | TemplateURLData data; |
| 520 | data.input_encodings.push_back("UTF-8"); |
| 521 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 522 | data.SetURL(test_data[i].url); |
| 523 | TemplateURL url(NULL, data); |
| 524 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 525 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 526 | TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| 527 | search_terms_args.assisted_query_stats = test_data[i].aqs; |
| 528 | UIThreadSearchTermsData::SetGoogleBaseURL(test_data[i].base_url); |
| 529 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 530 | ASSERT_TRUE(result.is_valid()); |
| 531 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 535 | // Tests replacing cursor position. |
| 536 | TEST_F(TemplateURLTest, ReplaceCursorPosition) { |
| 537 | struct TestData { |
| 538 | const string16 search_term; |
| 539 | size_t cursor_position; |
| 540 | const std::string url; |
| 541 | const std::string expected_result; |
| 542 | } test_data[] = { |
| 543 | { ASCIIToUTF16("foo"), |
| 544 | string16::npos, |
| 545 | "{google:baseURL}?{searchTerms}&{google:cursorPosition}", |
| 546 | "http://www.google.com/?foo&" }, |
| 547 | { ASCIIToUTF16("foo"), |
| 548 | 2, |
| 549 | "{google:baseURL}?{searchTerms}&{google:cursorPosition}", |
| 550 | "http://www.google.com/?foo&cp=2&" }, |
| 551 | { ASCIIToUTF16("foo"), |
| 552 | 15, |
| 553 | "{google:baseURL}?{searchTerms}&{google:cursorPosition}", |
| 554 | "http://www.google.com/?foo&cp=15&" }, |
| 555 | }; |
| 556 | TemplateURLData data; |
| 557 | data.input_encodings.push_back("UTF-8"); |
| 558 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 559 | data.SetURL(test_data[i].url); |
| 560 | TemplateURL url(NULL, data); |
| 561 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 562 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 563 | TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| 564 | search_terms_args.cursor_position = test_data[i].cursor_position; |
| 565 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args)); |
| 566 | ASSERT_TRUE(result.is_valid()); |
| 567 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| 568 | } |
| 569 | } |
| 570 | |
[email protected] | 800569d | 2013-05-06 07:38:48 | [diff] [blame] | 571 | // Tests replacing zeroPrefixUrl. |
| 572 | TEST_F(TemplateURLTest, ReplaceZeroPrefixUrl) { |
| 573 | struct TestData { |
| 574 | const string16 search_term; |
| 575 | const std::string zero_prefix_url; |
| 576 | const std::string url; |
| 577 | const std::string expected_result; |
| 578 | } test_data[] = { |
| 579 | { ASCIIToUTF16("foo"), |
| 580 | "http://www.google.com/", |
| 581 | "{google:baseURL}?{searchTerms}&{google:zeroPrefixUrl}", |
| 582 | "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fwww.google.com%2F&" }, |
| 583 | { ASCIIToUTF16("foo"), |
| 584 | "", |
| 585 | "{google:baseURL}?{searchTerms}&{google:zeroPrefixUrl}", |
| 586 | "http://www.google.com/?foo&" }, |
| 587 | { ASCIIToUTF16("foo"), |
| 588 | "http://g.com/+-/*&=", |
| 589 | "{google:baseURL}?{searchTerms}&{google:zeroPrefixUrl}", |
| 590 | "http://www.google.com/?foo&url=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fg.com%2F%2B-%2F*%26%3D&" }, |
| 591 | }; |
| 592 | TemplateURLData data; |
| 593 | data.input_encodings.push_back("UTF-8"); |
| 594 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 595 | data.SetURL(test_data[i].url); |
| 596 | TemplateURL url(NULL, data); |
| 597 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 598 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 599 | TemplateURLRef::SearchTermsArgs search_terms_args(test_data[i].search_term); |
| 600 | search_terms_args.zero_prefix_url = test_data[i].zero_prefix_url; |
| 601 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args)); |
| 602 | ASSERT_TRUE(result.is_valid()); |
| 603 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
| 604 | } |
| 605 | } |
| 606 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 607 | TEST_F(TemplateURLTest, Suggestions) { |
| 608 | struct TestData { |
| 609 | const int accepted_suggestion; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 610 | const string16 original_query_for_suggestion; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 611 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 612 | } test_data[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 613 | { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16(), |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 614 | "http://bar/foo?q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 615 | { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"), |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 616 | "http://bar/foo?q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 617 | { TemplateURLRef::NO_SUGGESTION_CHOSEN, string16(), |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 618 | "http://bar/foo?q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 619 | { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"), |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 620 | "http://bar/foo?q=foobar" }, |
| 621 | { 0, string16(), "http://bar/foo?oq=&q=foobar" }, |
| 622 | { 1, ASCIIToUTF16("foo"), "http://bar/foo?oq=foo&q=foobar" }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 623 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 624 | TemplateURLData data; |
[email protected] | 2965389 | 2013-03-02 19:25:37 | [diff] [blame] | 625 | data.SetURL("http://bar/foo?{google:originalQueryForSuggestion}" |
| 626 | "q={searchTerms}"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 627 | data.input_encodings.push_back("UTF-8"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 628 | TemplateURL url(NULL, data); |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 629 | EXPECT_TRUE(url.url_ref().IsValid()); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 630 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 631 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 632 | TemplateURLRef::SearchTermsArgs search_terms_args( |
| 633 | ASCIIToUTF16("foobar")); |
| 634 | search_terms_args.accepted_suggestion = test_data[i].accepted_suggestion; |
| 635 | search_terms_args.original_query = |
| 636 | test_data[i].original_query_for_suggestion; |
| 637 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args)); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 638 | ASSERT_TRUE(result.is_valid()); |
| 639 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
[email protected] | 81f808de | 2009-09-25 01:36:34 | [diff] [blame] | 643 | TEST_F(TemplateURLTest, RLZ) { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 644 | string16 rlz_string; |
[email protected] | 81d9b72d | 2012-03-26 22:29:17 | [diff] [blame] | 645 | #if defined(ENABLE_RLZ) |
| 646 | std::string brand; |
| 647 | if (google_util::GetBrand(&brand) && !brand.empty() && |
| 648 | !google_util::IsOrganic(brand)) { |
[email protected] | 7e6f4ed5c | 2012-10-31 21:10:34 | [diff] [blame] | 649 | RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz_string); |
[email protected] | 81d9b72d | 2012-03-26 22:29:17 | [diff] [blame] | 650 | } |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 651 | #elif defined(OS_ANDROID) |
| 652 | SearchTermsDataAndroid::rlz_parameter_value_.Get() = |
| 653 | ASCIIToUTF16("android_test"); |
| 654 | rlz_string = SearchTermsDataAndroid::rlz_parameter_value_.Get(); |
[email protected] | 58b6433 | 2010-08-13 16:09:39 | [diff] [blame] | 655 | #endif |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 656 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 657 | TemplateURLData data; |
| 658 | data.SetURL("http://bar/?{google:RLZ}{searchTerms}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 659 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 660 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 661 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | bca359b | 2012-06-24 07:53:04 | [diff] [blame] | 662 | GURL result(url.url_ref().ReplaceSearchTerms( |
| 663 | TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")))); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 664 | ASSERT_TRUE(result.is_valid()); |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 665 | std::string expected_url = "http://bar/?"; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 666 | if (!rlz_string.empty()) |
[email protected] | 8ead4c02 | 2012-03-17 02:09:50 | [diff] [blame] | 667 | expected_url += "rlz=" + UTF16ToUTF8(rlz_string) + "&"; |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 668 | expected_url += "x"; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 669 | EXPECT_EQ(expected_url, result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | TEST_F(TemplateURLTest, HostAndSearchTermKey) { |
| 673 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 674 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 675 | const std::string host; |
| 676 | const std::string path; |
| 677 | const std::string search_term_key; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 678 | } test_data[] = { |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 679 | { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 680 | |
| 681 | // No query key should result in empty values. |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 682 | { "http://blah/{searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 683 | |
| 684 | // No term should result in empty values. |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 685 | { "http://blah/", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 686 | |
| 687 | // Multiple terms should result in empty values. |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 688 | { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 689 | |
| 690 | // Term in the host shouldn't match. |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 691 | { "http://{searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 692 | |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 693 | { "http://blah/?q={searchTerms}", "blah", "/", "q"}, |
| 694 | { "https://blah/?q={searchTerms}", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 695 | |
| 696 | // Single term with extra chars in value should match. |
[email protected] | 7c60f504 | 2013-02-14 03:39:32 | [diff] [blame] | 697 | { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 698 | }; |
| 699 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 700 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 701 | TemplateURLData data; |
| 702 | data.SetURL(test_data[i].url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 703 | TemplateURL url(NULL, data); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 704 | EXPECT_EQ(test_data[i].host, url.url_ref().GetHost()); |
| 705 | EXPECT_EQ(test_data[i].path, url.url_ref().GetPath()); |
| 706 | EXPECT_EQ(test_data[i].search_term_key, url.url_ref().GetSearchTermKey()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | TEST_F(TemplateURLTest, GoogleBaseSuggestURL) { |
| 711 | static const struct { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 712 | const char* const base_url; |
| 713 | const char* const base_suggest_url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 714 | } data[] = { |
[email protected] | 014010e | 2011-10-01 04:12:44 | [diff] [blame] | 715 | { "http://google.com/", "http://google.com/complete/", }, |
| 716 | { "http://www.google.com/", "http://www.google.com/complete/", }, |
| 717 | { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", }, |
| 718 | { "http://www.google.com.by/", "http://www.google.com.by/complete/", }, |
| 719 | { "http://google.com/intl/xx/", "http://google.com/complete/", }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 720 | }; |
| 721 | |
[email protected] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 722 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 723 | CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url); |
| 724 | } |
| 725 | |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 726 | TEST_F(TemplateURLTest, ParseParameterKnown) { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 727 | std::string parsed_url("{searchTerms}"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 728 | TemplateURLData data; |
| 729 | data.SetURL(parsed_url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 730 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 731 | TemplateURLRef::Replacements replacements; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 732 | EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements)); |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 733 | EXPECT_EQ(std::string(), parsed_url); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 734 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 735 | EXPECT_EQ(0U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 736 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 737 | } |
| 738 | |
| 739 | TEST_F(TemplateURLTest, ParseParameterUnknown) { |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 740 | std::string parsed_url("{fhqwhgads}abc"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 741 | TemplateURLData data; |
| 742 | data.SetURL(parsed_url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 743 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 744 | TemplateURLRef::Replacements replacements; |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 745 | |
| 746 | // By default, TemplateURLRef should not consider itself prepopulated. |
| 747 | // Therefore we should not replace the unknown parameter. |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 748 | EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 749 | EXPECT_EQ("{fhqwhgads}abc", parsed_url); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 750 | EXPECT_TRUE(replacements.empty()); |
| 751 | |
| 752 | // If the TemplateURLRef is prepopulated, we should remove unknown parameters. |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 753 | parsed_url = "{fhqwhgads}abc"; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 754 | data.prepopulate_id = 1; |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 755 | TemplateURL url2(NULL, data); |
[email protected] | 495c30b | 2012-05-15 18:48:15 | [diff] [blame] | 756 | EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 757 | EXPECT_EQ("abc", parsed_url); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 758 | EXPECT_TRUE(replacements.empty()); |
| 759 | } |
| 760 | |
| 761 | TEST_F(TemplateURLTest, ParseURLEmpty) { |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 762 | TemplateURL url(NULL, TemplateURLData()); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 763 | TemplateURLRef::Replacements replacements; |
| 764 | bool valid = false; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 765 | EXPECT_EQ(std::string(), |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame^] | 766 | url.url_ref().ParseURL(std::string(), &replacements, NULL, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 767 | EXPECT_TRUE(replacements.empty()); |
| 768 | EXPECT_TRUE(valid); |
| 769 | } |
| 770 | |
| 771 | TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 772 | TemplateURLData data; |
| 773 | data.SetURL("{"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 774 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 775 | TemplateURLRef::Replacements replacements; |
| 776 | bool valid = false; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame^] | 777 | EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, NULL, |
| 778 | &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 779 | EXPECT_TRUE(replacements.empty()); |
| 780 | EXPECT_FALSE(valid); |
| 781 | } |
| 782 | |
| 783 | TEST_F(TemplateURLTest, ParseURLNoKnownParameters) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 784 | TemplateURLData data; |
| 785 | data.SetURL("{}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 786 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 787 | TemplateURLRef::Replacements replacements; |
| 788 | bool valid = false; |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame^] | 789 | EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, NULL, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 790 | EXPECT_TRUE(replacements.empty()); |
| 791 | EXPECT_TRUE(valid); |
| 792 | } |
| 793 | |
| 794 | TEST_F(TemplateURLTest, ParseURLTwoParameters) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 795 | TemplateURLData data; |
| 796 | data.SetURL("{}{{%s}}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 797 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 798 | TemplateURLRef::Replacements replacements; |
| 799 | bool valid = false; |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 800 | EXPECT_EQ("{}{}", |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame^] | 801 | url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, NULL, |
| 802 | &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 803 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 804 | EXPECT_EQ(3U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 805 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 806 | EXPECT_TRUE(valid); |
| 807 | } |
| 808 | |
| 809 | TEST_F(TemplateURLTest, ParseURLNestedParameter) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 810 | TemplateURLData data; |
| 811 | data.SetURL("{%s"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 812 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 813 | TemplateURLRef::Replacements replacements; |
| 814 | bool valid = false; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 815 | EXPECT_EQ("{", |
[email protected] | 93b2906 | 2013-07-12 03:09:09 | [diff] [blame^] | 816 | url.url_ref().ParseURL("{{searchTerms}", &replacements, NULL, |
| 817 | &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 818 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 819 | EXPECT_EQ(1U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 820 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 821 | EXPECT_TRUE(valid); |
| 822 | } |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 823 | |
[email protected] | fd6d882 | 2012-12-08 06:56:11 | [diff] [blame] | 824 | #if defined(OS_ANDROID) |
| 825 | TEST_F(TemplateURLTest, SearchClient) { |
| 826 | const std::string base_url_str("http://google.com/?"); |
| 827 | const std::string terms_str("{searchTerms}&{google:searchClient}"); |
| 828 | const std::string full_url_str = base_url_str + terms_str; |
| 829 | const string16 terms(ASCIIToUTF16(terms_str)); |
| 830 | UIThreadSearchTermsData::SetGoogleBaseURL(base_url_str); |
| 831 | |
| 832 | TemplateURLData data; |
| 833 | data.SetURL(full_url_str); |
| 834 | TemplateURL url(NULL, data); |
| 835 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 836 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 837 | TemplateURLRef::SearchTermsArgs search_terms_args(ASCIIToUTF16("foobar")); |
| 838 | |
| 839 | // Check that the URL is correct when a client is not present. |
| 840 | GURL result(url.url_ref().ReplaceSearchTerms(search_terms_args)); |
| 841 | ASSERT_TRUE(result.is_valid()); |
| 842 | EXPECT_EQ("http://google.com/?foobar&", result.spec()); |
| 843 | |
| 844 | // Check that the URL is correct when a client is present. |
| 845 | SearchTermsDataAndroid::search_client_.Get() = "android_test"; |
| 846 | GURL result_2(url.url_ref().ReplaceSearchTerms(search_terms_args)); |
| 847 | ASSERT_TRUE(result_2.is_valid()); |
| 848 | EXPECT_EQ("http://google.com/?foobar&client=android_test&", result_2.spec()); |
| 849 | } |
| 850 | #endif |
| 851 | |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 852 | TEST_F(TemplateURLTest, GetURLNoInstantURL) { |
| 853 | TemplateURLData data; |
| 854 | data.SetURL("http://google.com/?q={searchTerms}"); |
| 855 | data.suggestions_url = "http://google.com/suggest?q={searchTerms}"; |
| 856 | data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}"); |
| 857 | data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}"); |
| 858 | TemplateURL url(NULL, data); |
| 859 | ASSERT_EQ(3U, url.URLCount()); |
| 860 | EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0)); |
| 861 | EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1)); |
| 862 | EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2)); |
| 863 | } |
| 864 | |
| 865 | TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) { |
| 866 | TemplateURLData data; |
| 867 | data.SetURL("http://google.com/?q={searchTerms}"); |
| 868 | data.instant_url = "http://google.com/instant#q={searchTerms}"; |
| 869 | data.alternate_urls.push_back("http://google.com/alt?q={searchTerms}"); |
| 870 | data.alternate_urls.push_back("{google:baseURL}/alt/#q={searchTerms}"); |
| 871 | TemplateURL url(NULL, data); |
| 872 | ASSERT_EQ(3U, url.URLCount()); |
| 873 | EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0)); |
| 874 | EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1)); |
| 875 | EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2)); |
| 876 | } |
| 877 | |
| 878 | TEST_F(TemplateURLTest, GetURLOnlyOneURL) { |
| 879 | TemplateURLData data; |
| 880 | data.SetURL("http://www.google.co.uk/"); |
| 881 | TemplateURL url(NULL, data); |
| 882 | ASSERT_EQ(1U, url.URLCount()); |
| 883 | EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0)); |
| 884 | } |
| 885 | |
| 886 | TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) { |
| 887 | TemplateURLData data; |
| 888 | data.SetURL("http://google.com/?q={searchTerms}"); |
| 889 | data.instant_url = "http://google.com/instant#q={searchTerms}"; |
| 890 | data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}"); |
| 891 | data.alternate_urls.push_back( |
| 892 | "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar"); |
| 893 | TemplateURL url(NULL, data); |
| 894 | string16 result; |
| 895 | |
| 896 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 897 | GURL("http://google.com/?q=something"), &result)); |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 898 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 899 | |
| 900 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 901 | GURL("http://google.com/?espv&q=something"), &result)); |
| 902 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 903 | |
| 904 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 905 | GURL("http://google.com/?espv=1&q=something"), &result)); |
| 906 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 907 | |
| 908 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 909 | GURL("http://google.com/?espv=0&q=something"), &result)); |
| 910 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 911 | |
| 912 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 913 | GURL("http://google.com/alt/#q=something"), &result)); |
| 914 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 915 | |
| 916 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 917 | GURL("http://google.com/alt/#espv&q=something"), &result)); |
| 918 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 919 | |
| 920 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 921 | GURL("http://google.com/alt/#espv=1&q=something"), &result)); |
| 922 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 923 | |
| 924 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 925 | GURL("http://google.com/alt/#espv=0&q=something"), &result)); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 926 | EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 927 | |
| 928 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| 929 | GURL("http://google.ca/?q=something"), &result)); |
| 930 | EXPECT_EQ(string16(), result); |
| 931 | |
| 932 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | 67d8b75 | 2013-04-03 17:33:27 | [diff] [blame] | 933 | GURL("http://google.ca/?q=something&q=anything"), &result)); |
| 934 | EXPECT_EQ(string16(), result); |
| 935 | |
| 936 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 937 | GURL("http://google.com/foo/?q=foo"), &result)); |
| 938 | EXPECT_EQ(string16(), result); |
| 939 | |
[email protected] | 32e2d81b | 2012-10-16 19:03:25 | [diff] [blame] | 940 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 941 | GURL("https://google.com/?q=foo"), &result)); |
[email protected] | 32e2d81b | 2012-10-16 19:03:25 | [diff] [blame] | 942 | EXPECT_EQ(ASCIIToUTF16("foo"), result); |
[email protected] | 5b307875 | 2012-10-09 18:54:16 | [diff] [blame] | 943 | |
| 944 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| 945 | GURL("http://google.com:8080/?q=foo"), &result)); |
| 946 | EXPECT_EQ(string16(), result); |
| 947 | |
| 948 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 949 | GURL("http://google.com/?q=1+2+3&b=456"), &result)); |
| 950 | EXPECT_EQ(ASCIIToUTF16("1 2 3"), result); |
| 951 | |
| 952 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 953 | GURL("http://google.com/alt/?q=123#q=456"), &result)); |
| 954 | EXPECT_EQ(ASCIIToUTF16("456"), result); |
| 955 | |
| 956 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 957 | GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), &result)); |
| 958 | EXPECT_EQ(ASCIIToUTF16("123"), result); |
| 959 | |
| 960 | EXPECT_TRUE(url.ExtractSearchTermsFromURL(GURL( |
| 961 | "http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), &result)); |
| 962 | EXPECT_EQ(ASCIIToUTF16("789"), result); |
| 963 | |
| 964 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| 965 | GURL("http://google.com/alt/?q="), &result)); |
| 966 | EXPECT_EQ(string16(), result); |
| 967 | |
| 968 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| 969 | GURL("http://google.com/alt/?#q="), &result)); |
| 970 | EXPECT_EQ(string16(), result); |
| 971 | |
| 972 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| 973 | GURL("http://google.com/alt/?q=#q="), &result)); |
| 974 | EXPECT_EQ(string16(), result); |
| 975 | |
| 976 | EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| 977 | GURL("http://google.com/alt/?q=123#q="), &result)); |
| 978 | EXPECT_EQ(string16(), result); |
| 979 | |
| 980 | EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 981 | GURL("http://google.com/alt/?q=#q=123"), &result)); |
| 982 | EXPECT_EQ(ASCIIToUTF16("123"), result); |
| 983 | } |
[email protected] | 4076ea6 | 2013-01-09 01:47:19 | [diff] [blame] | 984 | |
| 985 | TEST_F(TemplateURLTest, HasSearchTermsReplacementKey) { |
| 986 | TemplateURLData data; |
| 987 | data.SetURL("http://google.com/?q={searchTerms}"); |
| 988 | data.instant_url = "http://google.com/instant#q={searchTerms}"; |
| 989 | data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}"); |
| 990 | data.alternate_urls.push_back( |
| 991 | "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar"); |
| 992 | data.search_terms_replacement_key = "espv"; |
| 993 | TemplateURL url(NULL, data); |
| 994 | |
| 995 | // Test with instant enabled required. |
| 996 | EXPECT_FALSE(url.HasSearchTermsReplacementKey( |
| 997 | GURL("http://google.com/"))); |
| 998 | |
| 999 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1000 | GURL("http://google.com/?espv"))); |
| 1001 | |
| 1002 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1003 | GURL("http://google.com/#espv"))); |
| 1004 | |
| 1005 | EXPECT_FALSE(url.HasSearchTermsReplacementKey( |
| 1006 | GURL("http://google.com/?q=something"))); |
| 1007 | |
| 1008 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1009 | GURL("http://google.com/?q=something&espv"))); |
| 1010 | |
| 1011 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1012 | GURL("http://google.com/?q=something&espv=1"))); |
| 1013 | |
| 1014 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1015 | GURL("http://google.com/?q=something&espv=0"))); |
| 1016 | |
| 1017 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1018 | GURL("http://google.com/?espv&q=something"))); |
| 1019 | |
| 1020 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1021 | GURL("http://google.com/?espv=1&q=something"))); |
| 1022 | |
| 1023 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1024 | GURL("http://google.com/?espv=0&q=something"))); |
| 1025 | |
| 1026 | EXPECT_FALSE(url.HasSearchTermsReplacementKey( |
| 1027 | GURL("http://google.com/alt/#q=something"))); |
| 1028 | |
| 1029 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1030 | GURL("http://google.com/alt/#q=something&espv"))); |
| 1031 | |
| 1032 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1033 | GURL("http://google.com/alt/#q=something&espv=1"))); |
| 1034 | |
| 1035 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1036 | GURL("http://google.com/alt/#q=something&espv=0"))); |
| 1037 | |
| 1038 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1039 | GURL("http://google.com/alt/#espv&q=something"))); |
| 1040 | |
| 1041 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1042 | GURL("http://google.com/alt/#espv=1&q=something"))); |
| 1043 | |
| 1044 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1045 | GURL("http://google.com/alt/#espv=0&q=something"))); |
| 1046 | |
| 1047 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1048 | GURL("http://google.com/?espv#q=something"))); |
| 1049 | |
| 1050 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1051 | GURL("http://google.com/?espv=1#q=something"))); |
| 1052 | |
| 1053 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1054 | GURL("http://google.com/?q=something#espv"))); |
| 1055 | |
| 1056 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1057 | GURL("http://google.com/?q=something#espv=1"))); |
| 1058 | |
| 1059 | // This does not ensure the domain matches. |
| 1060 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1061 | GURL("http://bing.com/?espv"))); |
| 1062 | |
| 1063 | EXPECT_TRUE(url.HasSearchTermsReplacementKey( |
| 1064 | GURL("http://bing.com/#espv"))); |
| 1065 | } |
[email protected] | f62e30f5 | 2013-03-23 03:45:15 | [diff] [blame] | 1066 | |
| 1067 | TEST_F(TemplateURLTest, ReplaceSearchTermsInURL) { |
| 1068 | TemplateURLData data; |
| 1069 | data.SetURL("http://google.com/?q={searchTerms}"); |
| 1070 | data.instant_url = "http://google.com/instant#q={searchTerms}"; |
| 1071 | data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}"); |
| 1072 | data.alternate_urls.push_back( |
| 1073 | "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar"); |
| 1074 | TemplateURL url(NULL, data); |
| 1075 | TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane")); |
| 1076 | GURL result; |
| 1077 | |
| 1078 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1079 | GURL("http://google.com/?q=something"), search_terms, &result)); |
| 1080 | EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result); |
| 1081 | |
| 1082 | result = GURL("http://should.not.change.com"); |
| 1083 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1084 | GURL("http://google.ca/?q=something"), search_terms, &result)); |
| 1085 | EXPECT_EQ(GURL("http://should.not.change.com"), result); |
| 1086 | |
| 1087 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1088 | GURL("http://google.com/foo/?q=foo"), search_terms, &result)); |
| 1089 | |
| 1090 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1091 | GURL("https://google.com/?q=foo"), search_terms, &result)); |
| 1092 | EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result); |
| 1093 | |
| 1094 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1095 | GURL("http://google.com:8080/?q=foo"), search_terms, &result)); |
| 1096 | |
| 1097 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1098 | GURL("http://google.com/?q=1+2+3&b=456"), search_terms, &result)); |
| 1099 | EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result); |
| 1100 | |
| 1101 | // Note: Spaces in REF parameters are not escaped. See TryEncoding() in |
| 1102 | // template_url.cc for details. |
| 1103 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1104 | GURL("http://google.com/alt/?q=123#q=456"), search_terms, &result)); |
| 1105 | EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result); |
| 1106 | |
| 1107 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1108 | GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms, |
| 1109 | &result)); |
| 1110 | EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"), |
| 1111 | result); |
| 1112 | |
| 1113 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1114 | GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), |
| 1115 | search_terms, &result)); |
| 1116 | EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456" |
| 1117 | "#j=abc&q=Bob Morane&h=def9"), result); |
| 1118 | |
| 1119 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1120 | GURL("http://google.com/alt/?q="), search_terms, &result)); |
| 1121 | |
| 1122 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1123 | GURL("http://google.com/alt/?#q="), search_terms, &result)); |
| 1124 | |
| 1125 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1126 | GURL("http://google.com/alt/?q=#q="), search_terms, &result)); |
| 1127 | |
| 1128 | EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1129 | GURL("http://google.com/alt/?q=123#q="), search_terms, &result)); |
| 1130 | |
| 1131 | EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1132 | GURL("http://google.com/alt/?q=#q=123"), search_terms, &result)); |
| 1133 | EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result); |
| 1134 | } |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 1135 | |
| 1136 | // Test the |append_extra_query_params| field of SearchTermsArgs. |
| 1137 | TEST_F(TemplateURLTest, ExtraQueryParams) { |
| 1138 | UIThreadSearchTermsData::SetGoogleBaseURL("http://www.google.com/"); |
| 1139 | TemplateURLData data; |
| 1140 | // Pick a URL with replacements before, during, and after the query, to ensure |
| 1141 | // we don't goof up any of them. |
| 1142 | data.SetURL("{google:baseURL}search?q={searchTerms}" |
| 1143 | "#{google:originalQueryForSuggestion}x"); |
| 1144 | TemplateURL url(NULL, data); |
| 1145 | |
| 1146 | // Baseline: no command-line args, no |append_extra_query_params| flag. |
| 1147 | TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("abc")); |
| 1148 | search_terms.original_query = ASCIIToUTF16("def"); |
| 1149 | search_terms.accepted_suggestion = 0; |
| 1150 | EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", |
| 1151 | url.url_ref().ReplaceSearchTerms(search_terms)); |
| 1152 | |
| 1153 | // Set the flag. Since there are no command-line args, this should have no |
| 1154 | // effect. |
| 1155 | search_terms.append_extra_query_params = true; |
| 1156 | EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", |
| 1157 | url.url_ref().ReplaceSearchTerms(search_terms)); |
| 1158 | |
| 1159 | // Now append the command-line arg. This should be inserted into the query. |
| 1160 | CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 1161 | switches::kExtraSearchQueryParams, "a=b"); |
| 1162 | EXPECT_EQ("http://www.google.com/search?a=b&q=abc#oq=def&x", |
| 1163 | url.url_ref().ReplaceSearchTerms(search_terms)); |
| 1164 | |
| 1165 | // Turn off the flag. Now the command-line arg should be ignored again. |
| 1166 | search_terms.append_extra_query_params = false; |
| 1167 | EXPECT_EQ("http://www.google.com/search?q=abc#oq=def&x", |
| 1168 | url.url_ref().ReplaceSearchTerms(search_terms)); |
| 1169 | } |