OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/base_paths.h" | 5 #include "base/base_paths.h" |
6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/rlz/rlz.h" | 9 #include "chrome/browser/rlz/rlz.h" |
10 #include "chrome/browser/search_engines/search_terms_data.h" | 10 #include "chrome/browser/search_engines/search_terms_data.h" |
11 #include "chrome/browser/search_engines/template_url.h" | 11 #include "chrome/browser/search_engines/template_url.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 // Simple implementation of SearchTermsData. | 14 // Simple implementation of SearchTermsData. |
15 class TestSearchTermsData : public SearchTermsData { | 15 class TestSearchTermsData : public SearchTermsData { |
16 public: | 16 public: |
17 explicit TestSearchTermsData(const char* google_base_url) | 17 explicit TestSearchTermsData(const char* google_base_url) |
18 : google_base_url_(google_base_url) { | 18 : google_base_url_(google_base_url) { |
19 } | 19 } |
20 | 20 |
21 virtual std::string GoogleBaseURLValue() const { | 21 virtual std::string GoogleBaseURLValue() const { |
22 return google_base_url_; | 22 return google_base_url_; |
23 } | 23 } |
24 | 24 |
25 virtual std::string GetApplicationLocale() const { | 25 virtual std::string GetApplicationLocale() const { |
26 return "yy"; | 26 return "yy"; |
27 } | 27 } |
28 | 28 |
29 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) | 29 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) |
30 // Returns the value for the Chrome Omnibox rlz. | 30 // Returns the value for the Chrome Omnibox rlz. |
31 virtual string16 GetRlzParameterValue() const { | 31 virtual std::wstring GetRlzParameterValue() const { |
32 return string16(); | 32 return L""; |
33 } | 33 } |
34 #endif | 34 #endif |
35 | 35 |
36 private: | 36 private: |
37 std::string google_base_url_; | 37 std::string google_base_url_; |
38 | 38 |
39 DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData); | 39 DISALLOW_COPY_AND_ASSIGN(TestSearchTermsData); |
40 }; | 40 }; |
41 | 41 |
42 class TemplateURLTest : public testing::Test { | 42 class TemplateURLTest : public testing::Test { |
(...skipping 18 matching lines...) Expand all Loading... |
61 } | 61 } |
62 | 62 |
63 TEST_F(TemplateURLTest, TestValidWithComplete) { | 63 TEST_F(TemplateURLTest, TestValidWithComplete) { |
64 TemplateURLRef ref("{searchTerms}", 0, 0); | 64 TemplateURLRef ref("{searchTerms}", 0, 0); |
65 ASSERT_TRUE(ref.IsValid()); | 65 ASSERT_TRUE(ref.IsValid()); |
66 } | 66 } |
67 | 67 |
68 TEST_F(TemplateURLTest, URLRefTestSearchTerms) { | 68 TEST_F(TemplateURLTest, URLRefTestSearchTerms) { |
69 struct SearchTermsCase { | 69 struct SearchTermsCase { |
70 const char* url; | 70 const char* url; |
71 const string16 terms; | 71 const wchar_t* terms; |
72 const char* output; | 72 const char* output; |
73 } search_term_cases[] = { | 73 } search_term_cases[] = { |
74 { "http://foo{searchTerms}", ASCIIToUTF16("sea rch/bar"), | 74 { "http://foo{searchTerms}", L"sea rch/bar", "http://foosea%20rch/bar" }, |
75 "http://foosea%20rch/bar" }, | 75 { "http://foo{searchTerms}?boo=abc", L"sea rch/bar", |
76 { "http://foo{searchTerms}?boo=abc", ASCIIToUTF16("sea rch/bar"), | |
77 "http://foosea%20rch/bar?boo=abc" }, | 76 "http://foosea%20rch/bar?boo=abc" }, |
78 { "http://foo/?boo={searchTerms}", ASCIIToUTF16("sea rch/bar"), | 77 { "http://foo/?boo={searchTerms}", L"sea rch/bar", |
79 "http://foo/?boo=sea+rch%2Fbar" }, | 78 "http://foo/?boo=sea+rch%2Fbar" }, |
80 { "http://en.wikipedia.org/{searchTerms}", ASCIIToUTF16("wiki/?"), | 79 { "http://en.wikipedia.org/{searchTerms}", L"wiki/?", |
81 "http://en.wikipedia.org/wiki/%3F" } | 80 "http://en.wikipedia.org/wiki/%3F" } |
82 }; | 81 }; |
83 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { | 82 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { |
84 const SearchTermsCase& value = search_term_cases[i]; | 83 const SearchTermsCase& value = search_term_cases[i]; |
85 TemplateURL t_url; | 84 TemplateURL t_url; |
86 TemplateURLRef ref(value.url, 0, 0); | 85 TemplateURLRef ref(value.url, 0, 0); |
87 ASSERT_TRUE(ref.IsValid()); | 86 ASSERT_TRUE(ref.IsValid()); |
88 | 87 |
89 ASSERT_TRUE(ref.SupportsReplacement()); | 88 ASSERT_TRUE(ref.SupportsReplacement()); |
90 GURL result = GURL(ref.ReplaceSearchTerms(t_url, value.terms, | 89 GURL result = GURL(ref.ReplaceSearchTerms(t_url, value.terms, |
91 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 90 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())); |
92 ASSERT_TRUE(result.is_valid()); | 91 ASSERT_TRUE(result.is_valid()); |
93 ASSERT_EQ(value.output, result.spec()); | 92 ASSERT_EQ(value.output, result.spec()); |
94 } | 93 } |
95 } | 94 } |
96 | 95 |
97 TEST_F(TemplateURLTest, URLRefTestCount) { | 96 TEST_F(TemplateURLTest, URLRefTestCount) { |
98 TemplateURL t_url; | 97 TemplateURL t_url; |
99 TemplateURLRef ref("http://foo{searchTerms}{count?}", 0, 0); | 98 TemplateURLRef ref("http://foo{searchTerms}{count?}", 0, 0); |
100 ASSERT_TRUE(ref.IsValid()); | 99 ASSERT_TRUE(ref.IsValid()); |
101 ASSERT_TRUE(ref.SupportsReplacement()); | 100 ASSERT_TRUE(ref.SupportsReplacement()); |
102 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"), | 101 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X", |
103 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 102 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())); |
104 ASSERT_TRUE(result.is_valid()); | 103 ASSERT_TRUE(result.is_valid()); |
105 ASSERT_EQ("http://foox/", result.spec()); | 104 ASSERT_EQ("http://foox/", result.spec()); |
106 } | 105 } |
107 | 106 |
108 TEST_F(TemplateURLTest, URLRefTestCount2) { | 107 TEST_F(TemplateURLTest, URLRefTestCount2) { |
109 TemplateURL t_url; | 108 TemplateURL t_url; |
110 TemplateURLRef ref("http://foo{searchTerms}{count}", 0, 0); | 109 TemplateURLRef ref("http://foo{searchTerms}{count}", 0, 0); |
111 ASSERT_TRUE(ref.IsValid()); | 110 ASSERT_TRUE(ref.IsValid()); |
112 ASSERT_TRUE(ref.SupportsReplacement()); | 111 ASSERT_TRUE(ref.SupportsReplacement()); |
113 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"), | 112 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X", |
114 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 113 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())); |
115 ASSERT_TRUE(result.is_valid()); | 114 ASSERT_TRUE(result.is_valid()); |
116 ASSERT_EQ("http://foox10/", result.spec()); | 115 ASSERT_EQ("http://foox10/", result.spec()); |
117 } | 116 } |
118 | 117 |
119 TEST_F(TemplateURLTest, URLRefTestIndices) { | 118 TEST_F(TemplateURLTest, URLRefTestIndices) { |
120 TemplateURL t_url; | 119 TemplateURL t_url; |
121 TemplateURLRef ref("http://foo{searchTerms}x{startIndex?}y{startPage?}", | 120 TemplateURLRef ref("http://foo{searchTerms}x{startIndex?}y{startPage?}", |
122 1, 2); | 121 1, 2); |
123 ASSERT_TRUE(ref.IsValid()); | 122 ASSERT_TRUE(ref.IsValid()); |
124 ASSERT_TRUE(ref.SupportsReplacement()); | 123 ASSERT_TRUE(ref.SupportsReplacement()); |
125 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"), | 124 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X", |
126 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 125 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())); |
127 ASSERT_TRUE(result.is_valid()); | 126 ASSERT_TRUE(result.is_valid()); |
128 ASSERT_EQ("http://fooxxy/", result.spec()); | 127 ASSERT_EQ("http://fooxxy/", result.spec()); |
129 } | 128 } |
130 | 129 |
131 TEST_F(TemplateURLTest, URLRefTestIndices2) { | 130 TEST_F(TemplateURLTest, URLRefTestIndices2) { |
132 TemplateURL t_url; | 131 TemplateURL t_url; |
133 TemplateURLRef ref("http://foo{searchTerms}x{startIndex}y{startPage}", 1, 2); | 132 TemplateURLRef ref("http://foo{searchTerms}x{startIndex}y{startPage}", 1, 2); |
134 ASSERT_TRUE(ref.IsValid()); | 133 ASSERT_TRUE(ref.IsValid()); |
135 ASSERT_TRUE(ref.SupportsReplacement()); | 134 ASSERT_TRUE(ref.SupportsReplacement()); |
136 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"), | 135 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X", |
137 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 136 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())); |
138 ASSERT_TRUE(result.is_valid()); | 137 ASSERT_TRUE(result.is_valid()); |
139 ASSERT_EQ("http://fooxx1y2/", result.spec()); | 138 ASSERT_EQ("http://fooxx1y2/", result.spec()); |
140 } | 139 } |
141 | 140 |
142 TEST_F(TemplateURLTest, URLRefTestEncoding) { | 141 TEST_F(TemplateURLTest, URLRefTestEncoding) { |
143 TemplateURL t_url; | 142 TemplateURL t_url; |
144 TemplateURLRef ref( | 143 TemplateURLRef ref( |
145 "http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a", 1, 2); | 144 "http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a", 1, 2); |
146 ASSERT_TRUE(ref.IsValid()); | 145 ASSERT_TRUE(ref.IsValid()); |
147 ASSERT_TRUE(ref.SupportsReplacement()); | 146 ASSERT_TRUE(ref.SupportsReplacement()); |
148 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"), | 147 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X", |
149 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 148 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())); |
150 ASSERT_TRUE(result.is_valid()); | 149 ASSERT_TRUE(result.is_valid()); |
151 ASSERT_EQ("http://fooxxutf-8ya/", result.spec()); | 150 ASSERT_EQ("http://fooxxutf-8ya/", result.spec()); |
152 } | 151 } |
153 | 152 |
154 TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) { | 153 TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) { |
155 TemplateURL t_url; | 154 TemplateURL t_url; |
156 TemplateURLRef ref( | 155 TemplateURLRef ref( |
157 "http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b", 1, 2); | 156 "http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b", 1, 2); |
158 ASSERT_TRUE(ref.IsValid()); | 157 ASSERT_TRUE(ref.IsValid()); |
159 ASSERT_TRUE(ref.SupportsReplacement()); | 158 ASSERT_TRUE(ref.SupportsReplacement()); |
160 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"), | 159 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X", |
161 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 160 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())); |
162 ASSERT_TRUE(result.is_valid()); | 161 ASSERT_TRUE(result.is_valid()); |
163 ASSERT_EQ("http://fooxutf-8axyb/", result.spec()); | 162 ASSERT_EQ("http://fooxutf-8axyb/", result.spec()); |
164 } | 163 } |
165 | 164 |
166 TEST_F(TemplateURLTest, URLRefTestEncoding2) { | 165 TEST_F(TemplateURLTest, URLRefTestEncoding2) { |
167 TemplateURL t_url; | 166 TemplateURL t_url; |
168 TemplateURLRef ref( | 167 TemplateURLRef ref( |
169 "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", 1, 2); | 168 "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", 1, 2); |
170 ASSERT_TRUE(ref.IsValid()); | 169 ASSERT_TRUE(ref.IsValid()); |
171 ASSERT_TRUE(ref.SupportsReplacement()); | 170 ASSERT_TRUE(ref.SupportsReplacement()); |
172 GURL result = GURL(ref.ReplaceSearchTerms(t_url, ASCIIToUTF16("X"), | 171 GURL result = GURL(ref.ReplaceSearchTerms(t_url, L"X", |
173 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 172 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())); |
174 ASSERT_TRUE(result.is_valid()); | 173 ASSERT_TRUE(result.is_valid()); |
175 ASSERT_EQ("http://fooxxutf-8yutf-8a/", result.spec()); | 174 ASSERT_EQ("http://fooxxutf-8yutf-8a/", result.spec()); |
176 } | 175 } |
177 | 176 |
178 TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) { | 177 TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) { |
179 struct SearchTermsCase { | 178 struct SearchTermsCase { |
180 const char* url; | 179 const char* url; |
181 const string16 terms; | 180 const wchar_t* terms; |
182 const char* output; | 181 const char* output; |
183 } search_term_cases[] = { | 182 } search_term_cases[] = { |
184 { "{google:baseURL}{language}{searchTerms}", string16(), | 183 { "{google:baseURL}{language}{searchTerms}", L"", |
185 "http://example.com/e/yy" }, | 184 "http://example.com/e/yy" }, |
186 { "{google:baseSuggestURL}{searchTerms}", string16(), | 185 { "{google:baseSuggestURL}{searchTerms}", L"", |
187 "http://clients1.example.com/complete/" } | 186 "http://clients1.example.com/complete/" } |
188 }; | 187 }; |
189 | 188 |
190 TestSearchTermsData search_terms_data("http://example.com/e/"); | 189 TestSearchTermsData search_terms_data("http://example.com/e/"); |
191 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { | 190 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { |
192 const SearchTermsCase& value = search_term_cases[i]; | 191 const SearchTermsCase& value = search_term_cases[i]; |
193 TemplateURL t_url; | 192 TemplateURL t_url; |
194 TemplateURLRef ref(value.url, 0, 0); | 193 TemplateURLRef ref(value.url, 0, 0); |
195 ASSERT_TRUE(ref.IsValid()); | 194 ASSERT_TRUE(ref.IsValid()); |
196 | 195 |
197 ASSERT_TRUE(ref.SupportsReplacement()); | 196 ASSERT_TRUE(ref.SupportsReplacement()); |
198 GURL result = GURL(ref.ReplaceSearchTermsUsingTermsData( | 197 GURL result = GURL(ref.ReplaceSearchTermsUsingTermsData( |
199 t_url, value.terms, | 198 t_url, value.terms, |
200 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16(), | 199 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring(), |
201 search_terms_data)); | 200 search_terms_data)); |
202 ASSERT_TRUE(result.is_valid()); | 201 ASSERT_TRUE(result.is_valid()); |
203 ASSERT_EQ(value.output, result.spec()); | 202 ASSERT_EQ(value.output, result.spec()); |
204 } | 203 } |
205 } | 204 } |
206 | 205 |
207 TEST_F(TemplateURLTest, URLRefTermToWide) { | 206 TEST_F(TemplateURLTest, URLRefTermToWide) { |
208 struct ToWideCase { | 207 struct ToWideCase { |
209 const char* encoded_search_term; | 208 const char* encoded_search_term; |
210 const string16 expected_decoded_term; | 209 const wchar_t* expected_decoded_term; |
211 } to_wide_cases[] = { | 210 } to_wide_cases[] = { |
212 {"hello+world", ASCIIToUTF16("hello world")}, | 211 {"hello+world", L"hello world"}, |
213 // Test some big-5 input. | 212 // Test some big-5 input. |
214 {"%a7A%A6%6e+to+you", WideToUTF16(L"\x4f60\x597d to you")}, | 213 {"%a7A%A6%6e+to+you", L"\x4f60\x597d to you"}, |
215 // Test some UTF-8 input. We should fall back to this when the encoding | 214 // Test some UTF-8 input. We should fall back to this when the encoding |
216 // doesn't look like big-5. We have a '5' in the middle, which is an invalid | 215 // doesn't look like big-5. We have a '5' in the middle, which is an invalid |
217 // Big-5 trailing byte. | 216 // Big-5 trailing byte. |
218 {"%e4%bd%a05%e5%a5%bd+to+you", WideToUTF16(L"\x4f60\x35\x597d to you")}, | 217 {"%e4%bd%a05%e5%a5%bd+to+you", L"\x4f60\x35\x597d to you"}, |
219 // Undecodable input should stay escaped. | 218 // Undecodable input should stay escaped. |
220 {"%91%01+abcd", WideToUTF16(L"%91%01 abcd")}, | 219 {"%91%01+abcd", L"%91%01 abcd"}, |
221 // Make sure we convert %2B to +. | 220 // Make sure we convert %2B to +. |
222 {"C%2B%2B", ASCIIToUTF16("C++")}, | 221 {"C%2B%2B", L"C++"}, |
223 // C%2B is escaped as C%252B, make sure we unescape it properly. | 222 // C%2B is escaped as C%252B, make sure we unescape it properly. |
224 {"C%252B", ASCIIToUTF16("C%2B")}, | 223 {"C%252B", L"C%2B"}, |
225 }; | 224 }; |
226 | 225 |
227 TemplateURL t_url; | 226 TemplateURL t_url; |
228 | 227 |
229 // Set one input encoding: big-5. This is so we can test fallback to UTF-8. | 228 // Set one input encoding: big-5. This is so we can test fallback to UTF-8. |
230 std::vector<std::string> encodings; | 229 std::vector<std::string> encodings; |
231 encodings.push_back("big-5"); | 230 encodings.push_back("big-5"); |
232 t_url.set_input_encodings(encodings); | 231 t_url.set_input_encodings(encodings); |
233 | 232 |
234 TemplateURLRef ref("http://foo?q={searchTerms}", 1, 2); | 233 TemplateURLRef ref("http://foo?q={searchTerms}", 1, 2); |
235 ASSERT_TRUE(ref.IsValid()); | 234 ASSERT_TRUE(ref.IsValid()); |
236 ASSERT_TRUE(ref.SupportsReplacement()); | 235 ASSERT_TRUE(ref.SupportsReplacement()); |
237 | 236 |
238 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) { | 237 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) { |
239 string16 result = ref.SearchTermToString16(t_url, | 238 std::wstring result = ref.SearchTermToWide(t_url, |
240 to_wide_cases[i].encoded_search_term); | 239 to_wide_cases[i].encoded_search_term); |
241 | 240 |
242 EXPECT_EQ(to_wide_cases[i].expected_decoded_term, result); | 241 EXPECT_EQ(std::wstring(to_wide_cases[i].expected_decoded_term), result); |
243 } | 242 } |
244 } | 243 } |
245 | 244 |
246 TEST_F(TemplateURLTest, SetFavIcon) { | 245 TEST_F(TemplateURLTest, SetFavIcon) { |
247 TemplateURL url; | 246 TemplateURL url; |
248 GURL favicon_url("http://favicon.url"); | 247 GURL favicon_url("http://favicon.url"); |
249 url.SetFavIconURL(favicon_url); | 248 url.SetFavIconURL(favicon_url); |
250 ASSERT_EQ(1U, url.image_refs().size()); | 249 ASSERT_EQ(1U, url.image_refs().size()); |
251 ASSERT_TRUE(favicon_url == url.GetFavIconURL()); | 250 ASSERT_TRUE(favicon_url == url.GetFavIconURL()); |
252 | 251 |
253 GURL favicon_url2("http://favicon2.url"); | 252 GURL favicon_url2("http://favicon2.url"); |
254 url.SetFavIconURL(favicon_url2); | 253 url.SetFavIconURL(favicon_url2); |
255 ASSERT_EQ(1U, url.image_refs().size()); | 254 ASSERT_EQ(1U, url.image_refs().size()); |
256 ASSERT_TRUE(favicon_url2 == url.GetFavIconURL()); | 255 ASSERT_TRUE(favicon_url2 == url.GetFavIconURL()); |
257 } | 256 } |
258 | 257 |
259 TEST_F(TemplateURLTest, DisplayURLToURLRef) { | 258 TEST_F(TemplateURLTest, DisplayURLToURLRef) { |
260 struct TestData { | 259 struct TestData { |
261 const std::string url; | 260 const std::string url; |
262 const string16 expected_result; | 261 const std::wstring expected_result; |
263 } data[] = { | 262 } data[] = { |
264 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", | 263 { "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a", |
265 ASCIIToUTF16("http://foo%sx{inputEncoding}y{outputEncoding}a") }, | 264 L"http://foo%sx{inputEncoding}y{outputEncoding}a" }, |
266 { "http://X", | 265 { "http://X", |
267 ASCIIToUTF16("http://X") }, | 266 L"http://X" }, |
268 { "http://foo{searchTerms", | 267 { "http://foo{searchTerms", |
269 ASCIIToUTF16("http://foo{searchTerms") }, | 268 L"http://foo{searchTerms" }, |
270 { "http://foo{searchTerms}{language}", | 269 { "http://foo{searchTerms}{language}", |
271 ASCIIToUTF16("http://foo%s{language}") }, | 270 L"http://foo%s{language}" }, |
272 }; | 271 }; |
273 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 272 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
274 TemplateURLRef ref(data[i].url, 1, 2); | 273 TemplateURLRef ref(data[i].url, 1, 2); |
275 EXPECT_EQ(data[i].expected_result, ref.DisplayURL()); | 274 EXPECT_EQ(data[i].expected_result, ref.DisplayURL()); |
276 EXPECT_EQ(data[i].url, | 275 EXPECT_EQ(data[i].url, |
277 TemplateURLRef::DisplayURLToURLRef(ref.DisplayURL())); | 276 TemplateURLRef::DisplayURLToURLRef(ref.DisplayURL())); |
278 } | 277 } |
279 } | 278 } |
280 | 279 |
281 TEST_F(TemplateURLTest, ReplaceSearchTerms) { | 280 TEST_F(TemplateURLTest, ReplaceSearchTerms) { |
(...skipping 28 matching lines...) Expand all Loading... |
310 }; | 309 }; |
311 TemplateURL turl; | 310 TemplateURL turl; |
312 turl.add_input_encoding("UTF-8"); | 311 turl.add_input_encoding("UTF-8"); |
313 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 312 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
314 TemplateURLRef ref(data[i].url, 1, 2); | 313 TemplateURLRef ref(data[i].url, 1, 2); |
315 EXPECT_TRUE(ref.IsValid()); | 314 EXPECT_TRUE(ref.IsValid()); |
316 EXPECT_TRUE(ref.SupportsReplacement()); | 315 EXPECT_TRUE(ref.SupportsReplacement()); |
317 std::string expected_result = data[i].expected_result; | 316 std::string expected_result = data[i].expected_result; |
318 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}", | 317 ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}", |
319 g_browser_process->GetApplicationLocale()); | 318 g_browser_process->GetApplicationLocale()); |
320 GURL result = GURL(ref.ReplaceSearchTerms(turl, ASCIIToUTF16("X"), | 319 GURL result = GURL(ref.ReplaceSearchTerms(turl, L"X", |
321 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 320 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())); |
322 EXPECT_TRUE(result.is_valid()); | 321 EXPECT_TRUE(result.is_valid()); |
323 EXPECT_EQ(expected_result, result.spec()); | 322 EXPECT_EQ(expected_result, result.spec()); |
324 } | 323 } |
325 } | 324 } |
326 | 325 |
327 | 326 |
328 // Tests replacing search terms in various encodings and making sure the | 327 // Tests replacing search terms in various encodings and making sure the |
329 // generated URL matches the expected value. | 328 // generated URL matches the expected value. |
330 TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) { | 329 TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) { |
331 struct TestData { | 330 struct TestData { |
332 const std::string encoding; | 331 const std::string encoding; |
333 const string16 search_term; | 332 const std::wstring search_term; |
334 const std::string url; | 333 const std::string url; |
335 const std::string expected_result; | 334 const std::string expected_result; |
336 } data[] = { | 335 } data[] = { |
337 { "BIG5", WideToUTF16(L"\x60BD"), | 336 { "BIG5", L"\x60BD", "http://foo/?{searchTerms}{inputEncoding}", |
338 "http://foo/?{searchTerms}{inputEncoding}", | |
339 "http://foo/?%B1~BIG5" }, | 337 "http://foo/?%B1~BIG5" }, |
340 { "UTF-8", ASCIIToUTF16("blah"), | 338 { "UTF-8", L"blah", "http://foo/?{searchTerms}{inputEncoding}", |
341 "http://foo/?{searchTerms}{inputEncoding}", | |
342 "http://foo/?blahUTF-8" }, | 339 "http://foo/?blahUTF-8" }, |
343 }; | 340 }; |
344 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 341 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
345 TemplateURL turl; | 342 TemplateURL turl; |
346 turl.add_input_encoding(data[i].encoding); | 343 turl.add_input_encoding(data[i].encoding); |
347 TemplateURLRef ref(data[i].url, 1, 2); | 344 TemplateURLRef ref(data[i].url, 1, 2); |
348 GURL result = GURL(ref.ReplaceSearchTerms(turl, | 345 GURL result = GURL(ref.ReplaceSearchTerms(turl, |
349 data[i].search_term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, | 346 data[i].search_term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, |
350 string16())); | 347 std::wstring())); |
351 EXPECT_TRUE(result.is_valid()); | 348 EXPECT_TRUE(result.is_valid()); |
352 EXPECT_EQ(data[i].expected_result, result.spec()); | 349 EXPECT_EQ(data[i].expected_result, result.spec()); |
353 } | 350 } |
354 } | 351 } |
355 | 352 |
356 TEST_F(TemplateURLTest, Suggestions) { | 353 TEST_F(TemplateURLTest, Suggestions) { |
357 struct TestData { | 354 struct TestData { |
358 const int accepted_suggestion; | 355 const int accepted_suggestion; |
359 const string16 original_query_for_suggestion; | 356 const std::wstring original_query_for_suggestion; |
360 const std::string expected_result; | 357 const std::string expected_result; |
361 } data[] = { | 358 } data[] = { |
362 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16(), | 359 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring(), |
363 "http://bar/foo?q=foobar" }, | 360 "http://bar/foo?q=foobar" }, |
364 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, ASCIIToUTF16("foo"), | 361 { TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, L"foo", |
365 "http://bar/foo?q=foobar" }, | 362 "http://bar/foo?q=foobar" }, |
366 { TemplateURLRef::NO_SUGGESTION_CHOSEN, string16(), | 363 { TemplateURLRef::NO_SUGGESTION_CHOSEN, std::wstring(), |
367 "http://bar/foo?aq=f&q=foobar" }, | 364 "http://bar/foo?aq=f&q=foobar" }, |
368 { TemplateURLRef::NO_SUGGESTION_CHOSEN, ASCIIToUTF16("foo"), | 365 { TemplateURLRef::NO_SUGGESTION_CHOSEN, L"foo", |
369 "http://bar/foo?aq=f&q=foobar" }, | 366 "http://bar/foo?aq=f&q=foobar" }, |
370 { 0, string16(), "http://bar/foo?aq=0&oq=&q=foobar" }, | 367 { 0, std::wstring(), "http://bar/foo?aq=0&oq=&q=foobar" }, |
371 { 1, ASCIIToUTF16("foo"), "http://bar/foo?aq=1&oq=foo&q=foobar" }, | 368 { 1, L"foo", "http://bar/foo?aq=1&oq=foo&q=foobar" }, |
372 }; | 369 }; |
373 TemplateURL turl; | 370 TemplateURL turl; |
374 turl.add_input_encoding("UTF-8"); | 371 turl.add_input_encoding("UTF-8"); |
375 TemplateURLRef ref("http://bar/foo?{google:acceptedSuggestion}" | 372 TemplateURLRef ref("http://bar/foo?{google:acceptedSuggestion}" |
376 "{google:originalQueryForSuggestion}q={searchTerms}", 1, 2); | 373 "{google:originalQueryForSuggestion}q={searchTerms}", 1, 2); |
377 ASSERT_TRUE(ref.IsValid()); | 374 ASSERT_TRUE(ref.IsValid()); |
378 ASSERT_TRUE(ref.SupportsReplacement()); | 375 ASSERT_TRUE(ref.SupportsReplacement()); |
379 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 376 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
380 GURL result = GURL(ref.ReplaceSearchTerms(turl, ASCIIToUTF16("foobar"), | 377 GURL result = GURL(ref.ReplaceSearchTerms(turl, L"foobar", |
381 data[i].accepted_suggestion, data[i].original_query_for_suggestion)); | 378 data[i].accepted_suggestion, data[i].original_query_for_suggestion)); |
382 EXPECT_TRUE(result.is_valid()); | 379 EXPECT_TRUE(result.is_valid()); |
383 EXPECT_EQ(data[i].expected_result, result.spec()); | 380 EXPECT_EQ(data[i].expected_result, result.spec()); |
384 } | 381 } |
385 } | 382 } |
386 | 383 |
387 #if defined(OS_WIN) | 384 #if defined(OS_WIN) |
388 TEST_F(TemplateURLTest, RLZ) { | 385 TEST_F(TemplateURLTest, RLZ) { |
389 string16 rlz_string; | 386 std::wstring rlz_string; |
390 #if defined(GOOGLE_CHROME_BUILD) | 387 #if defined(GOOGLE_CHROME_BUILD) |
391 RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz_string); | 388 RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz_string); |
392 #endif | 389 #endif |
393 | 390 |
394 TemplateURL t_url; | 391 TemplateURL t_url; |
395 TemplateURLRef ref("http://bar/?{google:RLZ}{searchTerms}", 1, 2); | 392 TemplateURLRef ref("http://bar/?{google:RLZ}{searchTerms}", 1, 2); |
396 ASSERT_TRUE(ref.IsValid()); | 393 ASSERT_TRUE(ref.IsValid()); |
397 ASSERT_TRUE(ref.SupportsReplacement()); | 394 ASSERT_TRUE(ref.SupportsReplacement()); |
398 GURL result(ref.ReplaceSearchTerms(t_url, L"x", | 395 GURL result(ref.ReplaceSearchTerms(t_url, L"x", |
399 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 396 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())); |
400 ASSERT_TRUE(result.is_valid()); | 397 ASSERT_TRUE(result.is_valid()); |
401 std::string expected_url = "http://bar/?"; | 398 std::string expected_url = "http://bar/?"; |
402 if (!rlz_string.empty()) { | 399 if (!rlz_string.empty()) { |
403 expected_url += "rlz=" + WideToUTF8(rlz_string) + "&"; | 400 expected_url += "rlz=" + WideToUTF8(rlz_string) + "&"; |
404 } | 401 } |
405 expected_url += "x"; | 402 expected_url += "x"; |
406 ASSERT_EQ(expected_url, result.spec()); | 403 ASSERT_EQ(expected_url, result.spec()); |
407 } | 404 } |
408 #endif | 405 #endif |
409 | 406 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 }; | 454 }; |
458 | 455 |
459 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) | 456 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) |
460 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url); | 457 CheckSuggestBaseURL(data[i].base_url, data[i].base_suggest_url); |
461 } | 458 } |
462 | 459 |
463 TEST_F(TemplateURLTest, Keyword) { | 460 TEST_F(TemplateURLTest, Keyword) { |
464 TemplateURL t_url; | 461 TemplateURL t_url; |
465 t_url.SetURL("http://www.google.com/search", 0, 0); | 462 t_url.SetURL("http://www.google.com/search", 0, 0); |
466 EXPECT_FALSE(t_url.autogenerate_keyword()); | 463 EXPECT_FALSE(t_url.autogenerate_keyword()); |
467 t_url.set_keyword(ASCIIToUTF16("foo")); | 464 t_url.set_keyword(L"foo"); |
468 EXPECT_EQ(ASCIIToUTF16("foo"), t_url.keyword()); | 465 EXPECT_EQ(L"foo", t_url.keyword()); |
469 t_url.set_autogenerate_keyword(true); | 466 t_url.set_autogenerate_keyword(true); |
470 EXPECT_TRUE(t_url.autogenerate_keyword()); | 467 EXPECT_TRUE(t_url.autogenerate_keyword()); |
471 EXPECT_EQ(ASCIIToUTF16("google.com"), t_url.keyword()); | 468 EXPECT_EQ(L"google.com", t_url.keyword()); |
472 t_url.set_keyword(ASCIIToUTF16("foo")); | 469 t_url.set_keyword(L"foo"); |
473 EXPECT_FALSE(t_url.autogenerate_keyword()); | 470 EXPECT_FALSE(t_url.autogenerate_keyword()); |
474 EXPECT_EQ(ASCIIToUTF16("foo"), t_url.keyword()); | 471 EXPECT_EQ(L"foo", t_url.keyword()); |
475 } | 472 } |
476 | 473 |
477 TEST_F(TemplateURLTest, ParseParameterKnown) { | 474 TEST_F(TemplateURLTest, ParseParameterKnown) { |
478 std::string parsed_url("{searchTerms}"); | 475 std::string parsed_url("{searchTerms}"); |
479 TemplateURLRef url_ref(parsed_url, 0, 0); | 476 TemplateURLRef url_ref(parsed_url, 0, 0); |
480 TemplateURLRef::Replacements replacements; | 477 TemplateURLRef::Replacements replacements; |
481 EXPECT_TRUE(url_ref.ParseParameter(0, 12, &parsed_url, &replacements)); | 478 EXPECT_TRUE(url_ref.ParseParameter(0, 12, &parsed_url, &replacements)); |
482 EXPECT_EQ(std::string(), parsed_url); | 479 EXPECT_EQ(std::string(), parsed_url); |
483 ASSERT_EQ(1U, replacements.size()); | 480 ASSERT_EQ(1U, replacements.size()); |
484 EXPECT_EQ(static_cast<size_t>(0), replacements[0].index); | 481 EXPECT_EQ(static_cast<size_t>(0), replacements[0].index); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 TEST_F(TemplateURLTest, ParseURLNestedParameter) { | 533 TEST_F(TemplateURLTest, ParseURLNestedParameter) { |
537 TemplateURLRef url_ref("{%s", 0, 0); | 534 TemplateURLRef url_ref("{%s", 0, 0); |
538 TemplateURLRef::Replacements replacements; | 535 TemplateURLRef::Replacements replacements; |
539 bool valid = false; | 536 bool valid = false; |
540 EXPECT_EQ("{", url_ref.ParseURL("{{searchTerms}", &replacements, &valid)); | 537 EXPECT_EQ("{", url_ref.ParseURL("{{searchTerms}", &replacements, &valid)); |
541 ASSERT_EQ(1U, replacements.size()); | 538 ASSERT_EQ(1U, replacements.size()); |
542 EXPECT_EQ(static_cast<size_t>(1), replacements[0].index); | 539 EXPECT_EQ(static_cast<size_t>(1), replacements[0].index); |
543 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); | 540 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
544 EXPECT_TRUE(valid); | 541 EXPECT_TRUE(valid); |
545 } | 542 } |
OLD | NEW |