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