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"
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
21 const std::string input
;
22 const std::string output
;
23 enum SupportedPlatforms
{
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
,
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
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
),
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).
89 gfx::GetStringWidthF(base::UTF8ToUTF16(kEllipsisStr
+ "/" + kEllipsisStr
),
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
,
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"},
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"},
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
138 // bother to do too many edge cases, since these are handled by the
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.
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
) {
203 if (testcases
[i
].platforms
== Testcase::NO_IOS
||
204 (testcases
[i
].platforms
== Testcase::NO_IOS9_OR_LATER
&&
205 base::ios::IsRunningOnIOS9OrLater())) {
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
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));
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
;
237 const OriginTestData tests
[] = {
238 {"Empty URL", "", L
""},
239 {"HTTP URL", "http://www.google.com/", L
"http://www.google.com"},
240 {"HTTPS URL", "https://www.google.com/", L
"https://www.google.com"},
241 {"Standard HTTP port", "http://www.google.com:80/",
242 L
"http://www.google.com"},
243 {"Standard HTTPS port", "https://www.google.com:443/",
244 L
"https://www.google.com"},
245 {"Standard HTTP port, IDN Chinese",
246 "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80",
247 L
"http://xn--fiqs8s.icom.museum"},
248 {"HTTP URL, IDN Hebrew (RTL)",
250 "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d."
251 "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/",
252 L
"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum"},
253 {"HTTP URL with query string, IDN Arabic (RTL)",
254 "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no",
255 L
"http://xn--wgbh1c.icom.museum"},
256 {"Non-standard HTTP port", "http://www.google.com:9000/",
257 L
"http://www.google.com:9000"},
258 {"Non-standard HTTPS port", "https://www.google.com:9000/",
259 L
"https://www.google.com:9000"},
260 {"File URI", "file:///usr/example/file.html",
261 L
"file:///usr/example/file.html"},
262 {"File URI with hostname", "file://localhost/usr/example/file.html",
263 L
"file:///usr/example/file.html"},
264 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls",
265 L
"file:///CONTOSO/accounting/money.xls"},
267 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
268 L
"file:///C:/Program%20Files/Music/Web%20Sys/main.html"},
269 {"HTTP URL with path", "http://www.google.com/test.html",
270 L
"http://www.google.com"},
271 {"HTTPS URL with path", "https://www.google.com/test.html",
272 L
"https://www.google.com"},
273 {"Unusual secure scheme (wss)", "wss://www.google.com/",
274 L
"wss://www.google.com"},
275 {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/",
276 L
"gopher://www.google.com"},
277 {"Unlisted scheme (chrome)", "chrome://version", L
"chrome://version"},
278 {"HTTP IP address", "http://173.194.65.103", L
"http://173.194.65.103"},
279 {"HTTPS IP address", "https://173.194.65.103", L
"https://173.194.65.103"},
280 {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
281 L
"http://[fe80::202:b3ff:fe1e:8329]"},
282 {"HTTPS IPv6 address with port", "https://[2001:db8:0:1]:443/",
283 L
"https://[2001:db8:0:1]"},
284 {"HTTPS IP address, non-default port", "https://173.194.65.103:8443",
285 L
"https://173.194.65.103:8443"},
286 {"HTTP filesystem: URL with path",
287 "filesystem:http://www.google.com/temporary/test.html",
288 L
"filesystem:http://www.google.com"},
289 {"File filesystem: URL with path",
290 "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy",
291 L
"filesystem:file:///temporary/stuff/test.html"},
292 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php",
293 L
"twelve://www.cyber.org/wow.php"},
294 {"Invalid scheme 2", "://www.cyber.org/wow.php",
295 L
"://www.cyber.org/wow.php"},
296 {"Invalid host 1", "https://www.cyber../wow.php", L
"https://www.cyber.."},
297 {"Invalid host 2", "https://www...cyber/wow.php", L
"https://www...cyber"},
298 {"Invalid port 1", "https://173.194.65.103:000",
299 L
"https://173.194.65.103:0"},
300 {"Invalid port 2", "https://173.194.65.103:gruffle",
301 L
"https://173.194.65.103:gruffle"},
302 {"Invalid port 3", "https://173.194.65.103:/hello.aspx",
303 L
"https://173.194.65.103"},
304 {"Trailing dot in DNS name", "https://www.example.com./get/goat",
305 L
"https://www.example.com."},
307 "blob:http%3A//www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
308 L
"blob:http%3A//www.html5rocks.com/"
309 L
"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"},
312 const char languages
[] = "zh-TW,en-US,en,am,ar-EG,ar";
313 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
314 base::string16 formatted
= url_formatter::FormatUrlForSecurityDisplay(
315 GURL(tests
[i
].input
), std::string());
316 EXPECT_EQ(base::WideToUTF16(tests
[i
].output
), formatted
)
317 << tests
[i
].description
;
318 base::string16 formatted_with_languages
=
319 url_formatter::FormatUrlForSecurityDisplay(GURL(tests
[i
].input
),
321 EXPECT_EQ(base::WideToUTF16(tests
[i
].output
), formatted_with_languages
)
322 << tests
[i
].description
;
325 base::string16 formatted
=
326 url_formatter::FormatUrlForSecurityDisplay(GURL(), std::string());
327 EXPECT_EQ(base::string16(), formatted
)
328 << "Explicitly test the 0-argument GURL constructor";