[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] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 6 | #include "base/string_util.h" |
[email protected] | 64048bd | 2010-03-08 23:28:58 | [diff] [blame] | 7 | #include "base/utf_string_conversions.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 8 | #include "chrome/browser/browser_process.h" |
| 9 | #include "chrome/browser/rlz/rlz.h" |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 10 | #include "chrome/browser/search_engines/search_terms_data.h" |
[email protected] | d54e03a5 | 2009-01-16 00:31:04 | [diff] [blame] | 11 | #include "chrome/browser/search_engines/template_url.h" |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | 81d9b72d | 2012-03-26 22:29:17 | [diff] [blame] | 14 | #if defined(ENABLE_RLZ) |
| 15 | #include "chrome/browser/google/google_util.h" |
| 16 | #endif |
| 17 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 18 | // TestSearchTermsData -------------------------------------------------------- |
| 19 | |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 20 | // Simple implementation of SearchTermsData. |
| 21 | class TestSearchTermsData : public SearchTermsData { |
| 22 | public: |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 23 | explicit TestSearchTermsData(const std::string& google_base_url); |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 24 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 25 | virtual std::string GoogleBaseURLValue() const OVERRIDE; |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 26 | |
| 27 | private: |
| 28 | std::string google_base_url_; |
| 29 | |
| 30 | DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData); |
| 31 | }; |
| 32 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 33 | TestSearchTermsData::TestSearchTermsData(const std::string& google_base_url) |
| 34 | : google_base_url_(google_base_url) { |
| 35 | } |
| 36 | |
| 37 | std::string TestSearchTermsData::GoogleBaseURLValue() const { |
| 38 | return google_base_url_; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | // TemplateURLTest ------------------------------------------------------------ |
| 43 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 44 | class TemplateURLTest : public testing::Test { |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 45 | public: |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 46 | void CheckSuggestBaseURL(const std::string& base_url, |
| 47 | const std::string& base_suggest_url) const; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 48 | }; |
| 49 | |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 50 | void TemplateURLTest::CheckSuggestBaseURL( |
| 51 | const std::string& base_url, |
| 52 | const std::string& base_suggest_url) const { |
| 53 | TestSearchTermsData search_terms_data(base_url); |
| 54 | EXPECT_EQ(base_suggest_url, search_terms_data.GoogleBaseSuggestURLValue()); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | // Actual tests --------------------------------------------------------------- |
| 59 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 60 | TEST_F(TemplateURLTest, Defaults) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 61 | TemplateURLData data; |
| 62 | EXPECT_FALSE(data.show_in_default_list); |
| 63 | EXPECT_FALSE(data.safe_for_autoreplace); |
| 64 | EXPECT_EQ(0, data.prepopulate_id); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | TEST_F(TemplateURLTest, TestValidWithComplete) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 68 | TemplateURLData data; |
| 69 | data.SetURL("{searchTerms}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 70 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 71 | EXPECT_TRUE(url.url_ref().IsValid()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | TEST_F(TemplateURLTest, URLRefTestSearchTerms) { |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 75 | struct SearchTermsCase { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 76 | const char* url; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 77 | const string16 terms; |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 78 | const std::string output; |
| 79 | bool valid_url; |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 80 | } search_term_cases[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 81 | { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"), |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 82 | "http://foosea%20rch%2Fbar", false }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 83 | { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"), |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 84 | "http://foosea%20rch%2Fbar?boo=abc", false }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 85 | { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"), |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 86 | "http://foo/?boo=sea+rch%2Fbar", true }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 87 | { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"), |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 88 | "http://en.wikipedia.org/wiki%2F%3F", true } |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 89 | }; |
| 90 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { |
| 91 | const SearchTermsCase& value = search_term_cases[i]; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 92 | TemplateURLData data; |
| 93 | data.SetURL(value.url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 94 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 95 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 96 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 97 | std::string result = url.url_ref().ReplaceSearchTerms(value.terms, |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 98 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 99 | EXPECT_EQ(value.output, result); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 100 | GURL result_url(result); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 101 | EXPECT_EQ(value.valid_url, result_url.is_valid()); |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 102 | } |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | TEST_F(TemplateURLTest, URLRefTestCount) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 106 | TemplateURLData data; |
| 107 | data.SetURL("http://foo{searchTerms}{count?}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 108 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 109 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 110 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 111 | GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 112 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 113 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 114 | EXPECT_EQ("http://foox/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST_F(TemplateURLTest, URLRefTestCount2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 118 | TemplateURLData data; |
| 119 | data.SetURL("http://foo{searchTerms}{count}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 120 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 121 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 122 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 123 | GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 124 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 125 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 126 | EXPECT_EQ("http://foox10/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | TEST_F(TemplateURLTest, URLRefTestIndices) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 130 | TemplateURLData data; |
| 131 | data.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 132 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 133 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 134 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 135 | GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 136 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 137 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 138 | EXPECT_EQ("http://fooxxy/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | TEST_F(TemplateURLTest, URLRefTestIndices2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 142 | TemplateURLData data; |
| 143 | data.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 144 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 145 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 146 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 147 | GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 148 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 149 | ASSERT_TRUE(result.is_valid()); |
[email protected] | 405aae2 | 2012-03-29 20:36:13 | [diff] [blame] | 150 | EXPECT_EQ("http://fooxx1y1/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEST_F(TemplateURLTest, URLRefTestEncoding) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 154 | TemplateURLData data; |
| 155 | data.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 156 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 157 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 158 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 159 | GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 160 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 161 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 162 | EXPECT_EQ("http://fooxxutf-8ya/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | d88cb20 | 2011-08-17 20:03:01 | [diff] [blame] | 165 | // Test that setting the prepopulate ID from TemplateURL causes the stored |
| 166 | // TemplateURLRef to handle parsing the URL parameters differently. |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 167 | TEST_F(TemplateURLTest, SetPrepopulatedAndParse) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 168 | TemplateURLData data; |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 169 | data.SetURL("http://foo{fhqwhgads}bar"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 170 | TemplateURL url(NULL, data); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 171 | TemplateURLRef::Replacements replacements; |
| 172 | bool valid = false; |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 173 | EXPECT_EQ("http://foo{fhqwhgads}bar", url.url_ref().ParseURL( |
| 174 | "http://foo{fhqwhgads}bar", &replacements, &valid)); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 175 | EXPECT_TRUE(replacements.empty()); |
| 176 | EXPECT_TRUE(valid); |
| 177 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 178 | data.prepopulate_id = 123; |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 179 | TemplateURL url2(NULL, data); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 180 | EXPECT_EQ("http://foobar", url2.url_ref().ParseURL("http://foo{fhqwhgads}bar", |
| 181 | &replacements, &valid)); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 182 | EXPECT_TRUE(replacements.empty()); |
| 183 | EXPECT_TRUE(valid); |
| 184 | } |
| 185 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 186 | TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 187 | TemplateURLData data; |
| 188 | data.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 189 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 190 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 191 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 192 | GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 193 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 194 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 195 | EXPECT_EQ("http://fooxutf-8axyb/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | TEST_F(TemplateURLTest, URLRefTestEncoding2) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 199 | TemplateURLData data; |
| 200 | data.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 201 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 202 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 203 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 204 | GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 205 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 206 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 207 | EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 210 | TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) { |
| 211 | struct SearchTermsCase { |
| 212 | const char* url; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 213 | const string16 terms; |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 214 | const char* output; |
| 215 | } search_term_cases[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 216 | { "{google:baseURL}{language}{searchTerms}", string16(), |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 217 | "http://example.com/e/en" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 218 | { "{google:baseSuggestURL}{searchTerms}", string16(), |
[email protected] | 014010e | 2011-10-01 04:12:44 | [diff] [blame] | 219 | "http://example.com/complete/" } |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | TestSearchTermsData search_terms_data("http://example.com/e/"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 223 | TemplateURLData data; |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 224 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { |
| 225 | const SearchTermsCase& value = search_term_cases[i]; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 226 | data.SetURL(value.url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 227 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 228 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 229 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 230 | GURL result(url.url_ref().ReplaceSearchTermsUsingTermsData(value.terms, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 231 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16(), |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 232 | search_terms_data)); |
| 233 | ASSERT_TRUE(result.is_valid()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 234 | EXPECT_EQ(value.output, result.spec()); |
[email protected] | 375bd731 | 2010-08-30 22:18:13 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 238 | TEST_F(TemplateURLTest, URLRefTermToWide) { |
| 239 | struct ToWideCase { |
| 240 | const char* encoded_search_term; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 241 | const string16 expected_decoded_term; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 242 | } to_wide_cases[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 243 | {"hello+world", ASCIIToUTF16("hello world")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 244 | // Test some big-5 input. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 245 | {"%a7A%A6%6e+to+you", WideToUTF16(L"\x4f60\x597d to you")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 246 | // Test some UTF-8 input. We should fall back to this when the encoding |
| 247 | // doesn't look like big-5. We have a '5' in the middle, which is an invalid |
| 248 | // Big-5 trailing byte. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 249 | {"%e4%bd%a05%e5%a5%bd+to+you", WideToUTF16(L"\x4f60\x35\x597d to you")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 250 | // Undecodable input should stay escaped. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 251 | {"%91%01+abcd", WideToUTF16(L"%91%01 abcd")}, |
[email protected] | 7df4348 | 2009-07-31 19:37:44 | [diff] [blame] | 252 | // Make sure we convert %2B to +. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 253 | {"C%2B%2B", ASCIIToUTF16("C++")}, |
[email protected] | 7df4348 | 2009-07-31 19:37:44 | [diff] [blame] | 254 | // C%2B is escaped as C%252B, make sure we unescape it properly. |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 255 | {"C%252B", ASCIIToUTF16("C%2B")}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 256 | }; |
| 257 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 258 | // 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] | 259 | TemplateURLData data; |
| 260 | data.SetURL("http://foo?q={searchTerms}"); |
| 261 | data.input_encodings.push_back("big-5"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 262 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 263 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 264 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 265 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) { |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 266 | EXPECT_EQ(to_wide_cases[i].expected_decoded_term, |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 267 | url.url_ref().SearchTermToString16( |
| 268 | to_wide_cases[i].encoded_search_term)); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 272 | TEST_F(TemplateURLTest, DisplayURLToURLRef) { |
| 273 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 274 | const std::string url; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 275 | const string16 expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 276 | } test_data[] = { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 277 | { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 278 | ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 279 | { "http://X", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 280 | ASCIIToUTF16("http://X") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 281 | { "http://foo{searchTerms", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 282 | ASCIIToUTF16("http://foo{searchTerms") }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 283 | { "http://foo{searchTerms}{language}", |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 284 | ASCIIToUTF16("http://foo%s{language}") }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 285 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 286 | TemplateURLData data; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 287 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 288 | data.SetURL(test_data[i].url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 289 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 290 | EXPECT_EQ(test_data[i].expected_result, url.url_ref().DisplayURL()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 291 | EXPECT_EQ(test_data[i].url, |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 292 | TemplateURLRef::DisplayURLToURLRef(url.url_ref().DisplayURL())); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | TEST_F(TemplateURLTest, ReplaceSearchTerms) { |
| 297 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 298 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 299 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 300 | } test_data[] = { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 301 | { "http://foo/{language}{searchTerms}{inputEncoding}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 302 | "http://foo/{language}XUTF-8" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 303 | { "http://foo/{language}{inputEncoding}{searchTerms}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 304 | "http://foo/{language}UTF-8X" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 305 | { "http://foo/{searchTerms}{language}{inputEncoding}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 306 | "http://foo/X{language}UTF-8" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 307 | { "http://foo/{searchTerms}{inputEncoding}{language}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 308 | "http://foo/XUTF-8{language}" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 309 | { "http://foo/{inputEncoding}{searchTerms}{language}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 310 | "http://foo/UTF-8X{language}" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 311 | { "http://foo/{inputEncoding}{language}{searchTerms}", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 312 | "http://foo/UTF-8{language}X" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 313 | { "http://foo/{language}a{searchTerms}a{inputEncoding}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 314 | "http://foo/{language}aXaUTF-8a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 315 | { "http://foo/{language}a{inputEncoding}a{searchTerms}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 316 | "http://foo/{language}aUTF-8aXa" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 317 | { "http://foo/{searchTerms}a{language}a{inputEncoding}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 318 | "http://foo/Xa{language}aUTF-8a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 319 | { "http://foo/{searchTerms}a{inputEncoding}a{language}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 320 | "http://foo/XaUTF-8a{language}a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 321 | { "http://foo/{inputEncoding}a{searchTerms}a{language}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 322 | "http://foo/UTF-8aXa{language}a" }, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 323 | { "http://foo/{inputEncoding}a{language}a{searchTerms}a", |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 324 | "http://foo/UTF-8a{language}aXa" }, |
| 325 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 326 | TemplateURLData data; |
| 327 | data.input_encodings.push_back("UTF-8"); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 328 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 329 | data.SetURL(test_data[i].url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 330 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 331 | EXPECT_TRUE(url.url_ref().IsValid()); |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 332 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 333 | std::string expected_result = test_data[i].expected_result; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 334 | ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}", |
[email protected] | d70539de | 2009-06-24 22:17:06 | [diff] [blame] | 335 | g_browser_process->GetApplicationLocale()); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 336 | GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 337 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 338 | ASSERT_TRUE(result.is_valid()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 339 | EXPECT_EQ(expected_result, result.spec()); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | |
| 344 | // Tests replacing search terms in various encodings and making sure the |
| 345 | // generated URL matches the expected value. |
| 346 | TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) { |
| 347 | struct TestData { |
| 348 | const std::string encoding; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 349 | const string16 search_term; |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 350 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 351 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 352 | } test_data[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 353 | { "BIG5", WideToUTF16(L"\x60BD"), |
| 354 | "http://foo/?{searchTerms}{inputEncoding}", |
[email protected] | 3c75f0d | 2010-03-02 05:51:17 | [diff] [blame] | 355 | "http://foo/?%B1~BIG5" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 356 | { "UTF-8", ASCIIToUTF16("blah"), |
| 357 | "http://foo/?{searchTerms}{inputEncoding}", |
[email protected] | 3c75f0d | 2010-03-02 05:51:17 | [diff] [blame] | 358 | "http://foo/?blahUTF-8" }, |
[email protected] | 34e2485 | 2012-01-31 18:43:58 | [diff] [blame] | 359 | { "Shift_JIS", UTF8ToUTF16("\xe3\x81\x82"), |
| 360 | "http://foo/{searchTerms}/bar", |
| 361 | "http://foo/%82%A0/bar"}, |
| 362 | { "Shift_JIS", UTF8ToUTF16("\xe3\x81\x82 \xe3\x81\x84"), |
| 363 | "http://foo/{searchTerms}/bar", |
| 364 | "http://foo/%82%A0%20%82%A2/bar"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 365 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 366 | TemplateURLData data; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 367 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 368 | data.SetURL(test_data[i].url); |
| 369 | data.input_encodings.clear(); |
| 370 | data.input_encodings.push_back(test_data[i].encoding); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 371 | TemplateURL url(NULL, data); |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 372 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 373 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 374 | GURL result(url.url_ref().ReplaceSearchTerms(test_data[i].search_term, |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 375 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
| 376 | ASSERT_TRUE(result.is_valid()); |
| 377 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
| 381 | TEST_F(TemplateURLTest, Suggestions) { |
| 382 | struct TestData { |
| 383 | const int accepted_suggestion; |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 384 | const string16 original_query_for_suggestion; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 385 | const std::string expected_result; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 386 | } test_data[] = { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 387 | { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16(), |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 388 | "http://bar/foo?q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 389 | { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"), |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 390 | "http://bar/foo?q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 391 | { TemplateURLRef::NO_SUGGESTION_CHOSEN, string16(), |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 392 | "http://bar/foo?aq=f&q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 393 | { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"), |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 394 | "http://bar/foo?aq=f&q=foobar" }, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 395 | { 0, string16(), "http://bar/foo?aq=0&oq=&q=foobar" }, |
| 396 | { 1, ASCIIToUTF16("foo"), "http://bar/foo?aq=1&oq=foo&q=foobar" }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 397 | }; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 398 | TemplateURLData data; |
| 399 | data.SetURL("http://bar/foo?{google:acceptedSuggestion}" |
| 400 | "{google:originalQueryForSuggestion}q={searchTerms}"); |
| 401 | data.input_encodings.push_back("UTF-8"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 402 | TemplateURL url(NULL, data); |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 403 | EXPECT_TRUE(url.url_ref().IsValid()); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 404 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 405 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 406 | GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("foobar"), |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 407 | test_data[i].accepted_suggestion, |
| 408 | test_data[i].original_query_for_suggestion)); |
| 409 | ASSERT_TRUE(result.is_valid()); |
| 410 | EXPECT_EQ(test_data[i].expected_result, result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
[email protected] | 81d9b72d | 2012-03-26 22:29:17 | [diff] [blame] | 414 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | 81f808de | 2009-09-25 01:36:34 | [diff] [blame] | 415 | TEST_F(TemplateURLTest, RLZ) { |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 416 | string16 rlz_string; |
[email protected] | 81d9b72d | 2012-03-26 22:29:17 | [diff] [blame] | 417 | #if defined(ENABLE_RLZ) |
| 418 | std::string brand; |
| 419 | if (google_util::GetBrand(&brand) && !brand.empty() && |
| 420 | !google_util::IsOrganic(brand)) { |
| 421 | RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz_string); |
| 422 | } |
[email protected] | 58b6433 | 2010-08-13 16:09:39 | [diff] [blame] | 423 | #endif |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 424 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 425 | TemplateURLData data; |
| 426 | data.SetURL("http://bar/?{google:RLZ}{searchTerms}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 427 | TemplateURL url(NULL, data); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 428 | EXPECT_TRUE(url.url_ref().IsValid()); |
| 429 | ASSERT_TRUE(url.url_ref().SupportsReplacement()); |
| 430 | GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("x"), |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 431 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 432 | ASSERT_TRUE(result.is_valid()); |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 433 | std::string expected_url = "http://bar/?"; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 434 | if (!rlz_string.empty()) |
[email protected] | 8ead4c02 | 2012-03-17 02:09:50 | [diff] [blame] | 435 | expected_url += "rlz=" + UTF16ToUTF8(rlz_string) + "&"; |
[email protected] | 12bd0587 | 2009-03-17 19:25:06 | [diff] [blame] | 436 | expected_url += "x"; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 437 | EXPECT_EQ(expected_url, result.spec()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 438 | } |
[email protected] | 81f808de | 2009-09-25 01:36:34 | [diff] [blame] | 439 | #endif |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 440 | |
| 441 | TEST_F(TemplateURLTest, HostAndSearchTermKey) { |
| 442 | struct TestData { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 443 | const std::string url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 444 | const std::string host; |
| 445 | const std::string path; |
| 446 | const std::string search_term_key; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 447 | } test_data[] = { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 448 | { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 449 | |
| 450 | // No query key should result in empty values. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 451 | { "http://blah/{searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 452 | |
| 453 | // No term should result in empty values. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 454 | { "http://blah/", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 455 | |
| 456 | // Multiple terms should result in empty values. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 457 | { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 458 | |
| 459 | // Term in the host shouldn't match. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 460 | { "http://{searchTerms}", "", "", ""}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 461 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 462 | { "http://blah/?q={searchTerms}", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 463 | |
| 464 | // Single term with extra chars in value should match. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 465 | { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"}, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 466 | }; |
| 467 | |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 468 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 469 | TemplateURLData data; |
| 470 | data.SetURL(test_data[i].url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 471 | TemplateURL url(NULL, data); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 472 | EXPECT_EQ(test_data[i].host, url.url_ref().GetHost()); |
| 473 | EXPECT_EQ(test_data[i].path, url.url_ref().GetPath()); |
| 474 | EXPECT_EQ(test_data[i].search_term_key, url.url_ref().GetSearchTermKey()); |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
| 478 | TEST_F(TemplateURLTest, GoogleBaseSuggestURL) { |
| 479 | static const struct { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 480 | const char* const base_url; |
| 481 | const char* const base_suggest_url; |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 482 | } data[] = { |
[email protected] | 014010e | 2011-10-01 04:12:44 | [diff] [blame] | 483 | { "http://google.com/", "http://google.com/complete/", }, |
| 484 | { "http://www.google.com/", "http://www.google.com/complete/", }, |
| 485 | { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", }, |
| 486 | { "http://www.google.com.by/", "http://www.google.com.by/complete/", }, |
| 487 | { "http://google.com/intl/xx/", "http://google.com/complete/", }, |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 488 | }; |
| 489 | |
[email protected] | f63ae31 | 2009-02-04 17:58:46 | [diff] [blame] | 490 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) |
[email protected] | d82443b | 2009-01-15 19:54:56 | [diff] [blame] | 491 | CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url); |
| 492 | } |
| 493 | |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 494 | TEST_F(TemplateURLTest, ParseParameterKnown) { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 495 | std::string parsed_url("{searchTerms}"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 496 | TemplateURLData data; |
| 497 | data.SetURL(parsed_url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 498 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 499 | TemplateURLRef::Replacements replacements; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 500 | EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements)); |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 501 | EXPECT_EQ(std::string(), parsed_url); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 502 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 503 | EXPECT_EQ(0U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 504 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 505 | } |
| 506 | |
| 507 | TEST_F(TemplateURLTest, ParseParameterUnknown) { |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 508 | std::string parsed_url("{fhqwhgads}abc"); |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 509 | TemplateURLData data; |
| 510 | data.SetURL(parsed_url); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 511 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 512 | TemplateURLRef::Replacements replacements; |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 513 | |
| 514 | // By default, TemplateURLRef should not consider itself prepopulated. |
| 515 | // Therefore we should not replace the unknown parameter. |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 516 | EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 517 | EXPECT_EQ("{fhqwhgads}abc", parsed_url); |
[email protected] | 1a25726 | 2011-06-28 22:15:44 | [diff] [blame] | 518 | EXPECT_TRUE(replacements.empty()); |
| 519 | |
| 520 | // If the TemplateURLRef is prepopulated, we should remove unknown parameters. |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 521 | parsed_url = "{fhqwhgads}abc"; |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 522 | data.prepopulate_id = 1; |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 523 | TemplateURL url2(NULL, data); |
[email protected] | 495c30b | 2012-05-15 18:48:15 | [diff] [blame^] | 524 | EXPECT_TRUE(url2.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); |
[email protected] | 243abf3 | 2012-05-15 18:28:20 | [diff] [blame] | 525 | EXPECT_EQ("abc", parsed_url); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 526 | EXPECT_TRUE(replacements.empty()); |
| 527 | } |
| 528 | |
| 529 | TEST_F(TemplateURLTest, ParseURLEmpty) { |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 530 | TemplateURL url(NULL, TemplateURLData()); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 531 | TemplateURLRef::Replacements replacements; |
| 532 | bool valid = false; |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 533 | EXPECT_EQ(std::string(), |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 534 | url.url_ref().ParseURL(std::string(), &replacements, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 535 | EXPECT_TRUE(replacements.empty()); |
| 536 | EXPECT_TRUE(valid); |
| 537 | } |
| 538 | |
| 539 | TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 540 | TemplateURLData data; |
| 541 | data.SetURL("{"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 542 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 543 | TemplateURLRef::Replacements replacements; |
| 544 | bool valid = false; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 545 | EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 546 | EXPECT_TRUE(replacements.empty()); |
| 547 | EXPECT_FALSE(valid); |
| 548 | } |
| 549 | |
| 550 | TEST_F(TemplateURLTest, ParseURLNoKnownParameters) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 551 | TemplateURLData data; |
| 552 | data.SetURL("{}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 553 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 554 | TemplateURLRef::Replacements replacements; |
| 555 | bool valid = false; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 556 | EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 557 | EXPECT_TRUE(replacements.empty()); |
| 558 | EXPECT_TRUE(valid); |
| 559 | } |
| 560 | |
| 561 | TEST_F(TemplateURLTest, ParseURLTwoParameters) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 562 | TemplateURLData data; |
| 563 | data.SetURL("{}{{%s}}"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 564 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 565 | TemplateURLRef::Replacements replacements; |
| 566 | bool valid = false; |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 567 | EXPECT_EQ("{}{}", |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 568 | url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 569 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 570 | EXPECT_EQ(3U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 571 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 572 | EXPECT_TRUE(valid); |
| 573 | } |
| 574 | |
| 575 | TEST_F(TemplateURLTest, ParseURLNestedParameter) { |
[email protected] | 573889f2 | 2012-04-07 01:31:54 | [diff] [blame] | 576 | TemplateURLData data; |
| 577 | data.SetURL("{%s"); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 578 | TemplateURL url(NULL, data); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 579 | TemplateURLRef::Replacements replacements; |
| 580 | bool valid = false; |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 581 | EXPECT_EQ("{", |
| 582 | url.url_ref().ParseURL("{{searchTerms}", &replacements, &valid)); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 583 | ASSERT_EQ(1U, replacements.size()); |
[email protected] | b37bdfe | 2012-03-16 20:53:27 | [diff] [blame] | 584 | EXPECT_EQ(1U, replacements[0].index); |
[email protected] | 81c6ef6 | 2010-01-21 09:58:47 | [diff] [blame] | 585 | EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
| 586 | EXPECT_TRUE(valid); |
| 587 | } |