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