Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / url_formatter / elide_url_unittest.cc
blobf0f4fbb3439393807148394148f4b972ff04c791
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/url_formatter/elide_url.h"
7 #include "base/ios/ios_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "url/gurl.h"
12 #if !defined(OS_ANDROID)
13 #include "ui/gfx/font_list.h" // nogncheck
14 #include "ui/gfx/text_elider.h" // nogncheck
15 #include "ui/gfx/text_utils.h" // nogncheck
16 #endif
18 namespace {
20 struct Testcase {
21 const std::string input;
22 const std::string output;
23 enum SupportedPlatforms {
24 ALL = 0,
25 NO_IOS9_OR_LATER,
26 NO_IOS,
27 } platforms;
30 #if !defined(OS_ANDROID)
31 void RunUrlTest(Testcase* testcases, size_t num_testcases) {
32 static const gfx::FontList font_list;
33 for (size_t i = 0; i < num_testcases; ++i) {
34 const GURL url(testcases[i].input);
35 // Should we test with non-empty language list?
36 // That's kinda redundant with net_util_unittests.
37 const float available_width =
38 gfx::GetStringWidthF(base::UTF8ToUTF16(testcases[i].output), font_list);
39 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
40 url_formatter::ElideUrl(url, font_list, available_width,
41 std::string()));
45 // Test eliding of commonplace URLs.
46 TEST(TextEliderTest, TestGeneralEliding) {
47 const std::string kEllipsisStr(gfx::kEllipsis);
48 Testcase testcases[] = {
49 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
50 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
51 {"http://www.google.com/intl/en/ads/",
52 "google.com/intl/" + kEllipsisStr + "/ads/"},
53 {"http://www.google.com/intl/en/ads/",
54 "google.com/" + kEllipsisStr + "/ads/"},
55 {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr},
56 {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr},
57 {"https://subdomain.foo.com/bar/filename.html",
58 "subdomain.foo.com/bar/filename.html"},
59 {"https://subdomain.foo.com/bar/filename.html",
60 "subdomain.foo.com/" + kEllipsisStr + "/filename.html"},
61 {"http://subdomain.foo.com/bar/filename.html",
62 kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"},
63 {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired",
64 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr},
67 RunUrlTest(testcases, arraysize(testcases));
70 // When there is very little space available, the elision code will shorten
71 // both path AND file name to an ellipsis - ".../...". To avoid this result,
72 // there is a hack in place that simply treats them as one string in this
73 // case.
74 TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) {
75 const std::string kEllipsisStr(gfx::kEllipsis);
77 // Very little space, would cause double ellipsis.
78 gfx::FontList font_list;
79 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html");
80 float available_width = gfx::GetStringWidthF(
81 base::UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr),
82 font_list);
84 // Create the expected string, after elision. Depending on font size, the
85 // directory might become /dir... or /di... or/d... - it never should be
86 // shorter than that. (If it is, the font considers d... to be longer
87 // than .../... - that should never happen).
88 ASSERT_GT(
89 gfx::GetStringWidthF(base::UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr),
90 font_list),
91 gfx::GetStringWidthF(base::UTF8ToUTF16("d" + kEllipsisStr), font_list));
92 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc");
93 base::string16 expected = url_formatter::ElideUrl(
94 long_url, font_list, available_width, std::string());
95 // Ensure that the expected result still contains part of the directory name.
96 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length());
97 EXPECT_EQ(expected, url_formatter::ElideUrl(url, font_list, available_width,
98 std::string()));
100 // More space available - elide directories, partially elide filename.
101 Testcase testcases[] = {
102 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html",
103 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr},
105 RunUrlTest(testcases, arraysize(testcases));
108 // Test eliding of empty strings, URLs with ports, passwords, queries, etc.
109 TEST(TextEliderTest, TestMoreEliding) {
110 const std::string kEllipsisStr(gfx::kEllipsis);
111 Testcase testcases[] = {
112 {"http://www.google.com/foo?bar", "www.google.com/foo?bar"},
113 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo?" + kEllipsisStr},
114 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo" + kEllipsisStr},
115 {"http://xyz.google.com/foo?bar", "xyz.google.com/fo" + kEllipsisStr},
116 {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"},
117 {"", ""},
118 {"http://foo.bar..example.com...hello/test/filename.html",
119 "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"},
120 {"http://foo.bar../", "foo.bar.."},
121 {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"},
122 {"http://me:mypass@secrethost.com:99/foo?bar#baz",
123 "secrethost.com:99/foo?bar#baz"},
124 {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"},
125 {"mailto:elgoato@elgoato.com", "mailto:elgoato@elgoato.com"},
126 {"javascript:click(0)", "javascript:click(0)"},
127 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
128 "chess.eecs.berkeley.edu:4430/login/arbitfilename"},
129 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
130 kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"},
132 // Unescaping.
133 {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
134 "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
136 // Invalid unescaping for path. The ref will always be valid UTF-8. We
137 // don't
138 // bother to do too many edge cases, since these are handled by the
139 // escaper
140 // unittest.
141 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
142 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
145 RunUrlTest(testcases, arraysize(testcases));
148 // Test eliding of file: URLs.
149 TEST(TextEliderTest, TestFileURLEliding) {
150 const std::string kEllipsisStr(gfx::kEllipsis);
151 Testcase testcases[] = {
152 {"file:///C:/path1/path2/path3/filename",
153 "file:///C:/path1/path2/path3/filename"},
154 {"file:///C:/path1/path2/path3/filename", "C:/path1/path2/path3/filename"},
155 // GURL parses "file:///C:path" differently on windows than it does on posix.
156 #if defined(OS_WIN)
157 {"file:///C:path1/path2/path3/filename",
158 "C:/path1/path2/" + kEllipsisStr + "/filename"},
159 {"file:///C:path1/path2/path3/filename",
160 "C:/path1/" + kEllipsisStr + "/filename"},
161 {"file:///C:path1/path2/path3/filename",
162 "C:/" + kEllipsisStr + "/filename"},
163 #endif // defined(OS_WIN)
164 {"file://filer/foo/bar/file", "filer/foo/bar/file"},
165 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"},
166 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"},
167 {"file://filer/foo/", "file://filer/foo/"},
168 {"file://filer/foo/", "filer/foo/"},
169 {"file://filer/foo/", "filer" + kEllipsisStr},
170 // Eliding file URLs with nothing after the ':' shouldn't crash.
171 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:", "aaa" + kEllipsisStr},
172 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:/", "aaa" + kEllipsisStr},
175 RunUrlTest(testcases, arraysize(testcases));
178 TEST(TextEliderTest, TestHostEliding) {
179 const std::string kEllipsisStr(gfx::kEllipsis);
180 Testcase testcases[] = {
181 {"http://google.com", "google.com"},
182 // iOS width calculations are off by a letter from other platforms for
183 // strings with too many kerned letters on the default font set.
184 // TODO(rohitrao): Fix secure_display::ElideHost for iOS
185 // (crbug.com/517604).
186 {"http://subdomain.google.com", kEllipsisStr + ".google.com",
187 Testcase::NO_IOS9_OR_LATER},
188 {"http://reallyreallyreallylongdomainname.com",
189 "reallyreallyreallylongdomainname.com"},
190 {"http://a.b.c.d.e.f.com", kEllipsisStr + "f.com",
191 Testcase::NO_IOS9_OR_LATER},
192 {"http://foo", "foo"},
193 {"http://foo.bar", "foo.bar"},
194 {"http://subdomain.foo.bar", kEllipsisStr + "in.foo.bar",
195 Testcase::NO_IOS9_OR_LATER},
196 {"http://subdomain.reallylongdomainname.com",
197 kEllipsisStr + "ain.reallylongdomainname.com", Testcase::NO_IOS},
198 {"http://a.b.c.d.e.f.com", kEllipsisStr + ".e.f.com", Testcase::NO_IOS},
201 for (size_t i = 0; i < arraysize(testcases); ++i) {
202 #if defined(OS_IOS)
203 if (testcases[i].platforms == Testcase::NO_IOS ||
204 (testcases[i].platforms == Testcase::NO_IOS9_OR_LATER &&
205 base::ios::IsRunningOnIOS9OrLater())) {
206 continue;
208 #endif
209 const float available_width = gfx::GetStringWidthF(
210 base::UTF8ToUTF16(testcases[i].output), gfx::FontList());
211 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
212 url_formatter::ElideHost(GURL(testcases[i].input),
213 gfx::FontList(), available_width));
216 // Trying to elide to a really short length will still keep the full TLD+1
217 EXPECT_EQ(
218 base::ASCIIToUTF16("google.com"),
219 url_formatter::ElideHost(GURL("http://google.com"), gfx::FontList(), 2));
220 EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"),
221 url_formatter::ElideHost(GURL("http://subdomain.google.com"),
222 gfx::FontList(), 2));
223 EXPECT_EQ(
224 base::ASCIIToUTF16("foo.bar"),
225 url_formatter::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2));
228 #endif // !defined(OS_ANDROID)
230 TEST(TextEliderTest, FormatUrlForSecurityDisplay) {
231 struct OriginTestData {
232 const char* const description;
233 const char* const input;
234 const wchar_t* const output;
235 const wchar_t* const output_omit_scheme;
238 const OriginTestData tests[] = {
239 {"Empty URL", "", L"", L""},
240 {"HTTP URL", "http://www.google.com/", L"http://www.google.com",
241 L"www.google.com"},
242 {"HTTPS URL", "https://www.google.com/", L"https://www.google.com",
243 L"www.google.com"},
244 {"Standard HTTP port", "http://www.google.com:80/",
245 L"http://www.google.com", L"www.google.com"},
246 {"Standard HTTPS port", "https://www.google.com:443/",
247 L"https://www.google.com", L"www.google.com"},
248 {"Standard HTTP port, IDN Chinese",
249 "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80",
250 L"http://xn--fiqs8s.icom.museum", L"xn--fiqs8s.icom.museum"},
251 {"HTTP URL, IDN Hebrew (RTL)",
252 "http://"
253 "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d."
254 "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/",
255 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum",
256 L"xn--4dbklr2c8d.xn--4dbrk0ce.museum"},
257 {"HTTP URL with query string, IDN Arabic (RTL)",
258 "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no",
259 L"http://xn--wgbh1c.icom.museum", L"xn--wgbh1c.icom.museum"},
260 {"Non-standard HTTP port", "http://www.google.com:9000/",
261 L"http://www.google.com:9000", L"www.google.com:9000"},
262 {"Non-standard HTTPS port", "https://www.google.com:9000/",
263 L"https://www.google.com:9000", L"www.google.com:9000"},
264 {"File URI", "file:///usr/example/file.html",
265 L"file:///usr/example/file.html", L"file:///usr/example/file.html"},
266 {"File URI with hostname", "file://localhost/usr/example/file.html",
267 L"file:///usr/example/file.html", L"file:///usr/example/file.html"},
268 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls",
269 L"file:///CONTOSO/accounting/money.xls",
270 L"file:///CONTOSO/accounting/money.xls"},
271 {"UNC File URI 2",
272 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
273 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
274 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html"},
275 {"HTTP URL with path", "http://www.google.com/test.html",
276 L"http://www.google.com", L"www.google.com"},
277 {"HTTPS URL with path", "https://www.google.com/test.html",
278 L"https://www.google.com", L"www.google.com"},
279 {"Unusual secure scheme (wss)", "wss://www.google.com/",
280 L"wss://www.google.com", L"wss://www.google.com"},
281 {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/",
282 L"gopher://www.google.com", L"gopher://www.google.com"},
283 {"Unlisted scheme (chrome)", "chrome://version", L"chrome://version",
284 L"chrome://version"},
285 {"HTTP IP address", "http://173.194.65.103", L"http://173.194.65.103",
286 L"173.194.65.103"},
287 {"HTTPS IP address", "https://173.194.65.103", L"https://173.194.65.103",
288 L"173.194.65.103"},
289 {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
290 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]"},
291 {"HTTPS IPv6 address with port", "https://[2001:db8:0:1]:443/",
292 L"https://[2001:db8:0:1]", L"https://[2001:db8:0:1]"},
293 {"HTTPS IP address, non-default port", "https://173.194.65.103:8443",
294 L"https://173.194.65.103:8443", L"173.194.65.103:8443"},
295 {"HTTP filesystem: URL with path",
296 "filesystem:http://www.google.com/temporary/test.html",
297 L"filesystem:http://www.google.com",
298 L"filesystem:http://www.google.com"},
299 {"File filesystem: URL with path",
300 "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy",
301 L"filesystem:file:///temporary/stuff/test.html",
302 L"filesystem:file:///temporary/stuff/test.html"},
303 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php",
304 L"twelve://www.cyber.org/wow.php", L"twelve://www.cyber.org/wow.php"},
305 {"Invalid scheme 2", "://www.cyber.org/wow.php",
306 L"://www.cyber.org/wow.php", L"://www.cyber.org/wow.php"},
307 {"Invalid host 1", "https://www.cyber../wow.php", L"https://www.cyber..",
308 L"www.cyber.."},
309 {"Invalid host 2", "https://www...cyber/wow.php", L"https://www...cyber",
310 L"www...cyber"},
311 {"Invalid port 1", "https://173.194.65.103:000",
312 L"https://173.194.65.103:0", L"173.194.65.103:0"},
313 {"Invalid port 2", "https://173.194.65.103:gruffle",
314 L"https://173.194.65.103:gruffle", L"https://173.194.65.103:gruffle"},
315 {"Invalid port 3", "https://173.194.65.103:/hello.aspx",
316 L"https://173.194.65.103", L"173.194.65.103"},
317 {"Trailing dot in DNS name", "https://www.example.com./get/goat",
318 L"https://www.example.com.", L"www.example.com."},
319 {"Blob URL",
320 "blob:http%3A//www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
321 L"blob:http%3A//www.html5rocks.com/"
322 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
323 L"blob:http%3A//www.html5rocks.com/"
324 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}};
326 const char languages[] = "zh-TW,en-US,en,am,ar-EG,ar";
327 for (size_t i = 0; i < arraysize(tests); ++i) {
328 base::string16 formatted = url_formatter::FormatUrlForSecurityDisplay(
329 GURL(tests[i].input), std::string());
330 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
331 << tests[i].description;
333 base::string16 formatted_omit_scheme =
334 url_formatter::FormatUrlForSecurityDisplayOmitScheme(
335 GURL(tests[i].input), std::string());
336 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_scheme),
337 formatted_omit_scheme)
338 << tests[i].description;
340 base::string16 formatted_with_languages =
341 url_formatter::FormatUrlForSecurityDisplay(GURL(tests[i].input),
342 languages);
343 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted_with_languages)
344 << tests[i].description;
346 base::string16 formatted_with_languages_omit_scheme =
347 url_formatter::FormatUrlForSecurityDisplayOmitScheme(
348 GURL(tests[i].input), languages);
349 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_scheme),
350 formatted_with_languages_omit_scheme)
351 << tests[i].description;
354 base::string16 formatted =
355 url_formatter::FormatUrlForSecurityDisplay(GURL(), std::string());
356 EXPECT_EQ(base::string16(), formatted)
357 << "Explicitly test the 0-argument GURL constructor";
359 base::string16 formatted_omit_scheme =
360 url_formatter::FormatUrlForSecurityDisplayOmitScheme(GURL(),
361 std::string());
362 EXPECT_EQ(base::string16(), formatted_omit_scheme)
363 << "Explicitly test the 0-argument GURL constructor";
366 } // namespace