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