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