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