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/secure_display/elide_url.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/font_list.h"
10 #include "ui/gfx/text_elider.h"
11 #include "ui/gfx/text_utils.h"
14 using base::UTF8ToUTF16
;
15 using gfx::GetStringWidthF
;
21 const std::string input
;
22 const std::string output
;
25 #if !defined(OS_ANDROID)
26 void RunUrlTest(Testcase
* testcases
, size_t num_testcases
) {
27 static const gfx::FontList font_list
;
28 for (size_t i
= 0; i
< num_testcases
; ++i
) {
29 const GURL
url(testcases
[i
].input
);
30 // Should we test with non-empty language list?
31 // That's kinda redundant with net_util_unittests.
32 const float available_width
=
33 GetStringWidthF(UTF8ToUTF16(testcases
[i
].output
), font_list
);
34 EXPECT_EQ(UTF8ToUTF16(testcases
[i
].output
),
35 secure_display::ElideUrl(url
, font_list
, available_width
,
40 // Test eliding of commonplace URLs.
41 TEST(TextEliderTest
, TestGeneralEliding
) {
42 const std::string
kEllipsisStr(kEllipsis
);
43 Testcase testcases
[] = {
44 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
45 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
46 {"http://www.google.com/intl/en/ads/",
47 "google.com/intl/" + kEllipsisStr
+ "/ads/"},
48 {"http://www.google.com/intl/en/ads/",
49 "google.com/" + kEllipsisStr
+ "/ads/"},
50 {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr
},
51 {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr
},
52 {"https://subdomain.foo.com/bar/filename.html",
53 "subdomain.foo.com/bar/filename.html"},
54 {"https://subdomain.foo.com/bar/filename.html",
55 "subdomain.foo.com/" + kEllipsisStr
+ "/filename.html"},
56 {"http://subdomain.foo.com/bar/filename.html",
57 kEllipsisStr
+ "foo.com/" + kEllipsisStr
+ "/filename.html"},
58 {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired",
59 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr
},
62 RunUrlTest(testcases
, arraysize(testcases
));
65 // When there is very little space available, the elision code will shorten
66 // both path AND file name to an ellipsis - ".../...". To avoid this result,
67 // there is a hack in place that simply treats them as one string in this
69 TEST(TextEliderTest
, TestTrailingEllipsisSlashEllipsisHack
) {
70 const std::string
kEllipsisStr(kEllipsis
);
72 // Very little space, would cause double ellipsis.
73 gfx::FontList font_list
;
74 GURL
url("http://battersbox.com/directory/foo/peter_paul_and_mary.html");
75 float available_width
= GetStringWidthF(
76 UTF8ToUTF16("battersbox.com/" + kEllipsisStr
+ "/" + kEllipsisStr
),
79 // Create the expected string, after elision. Depending on font size, the
80 // directory might become /dir... or /di... or/d... - it never should be
81 // shorter than that. (If it is, the font considers d... to be longer
82 // than .../... - that should never happen).
83 ASSERT_GT(GetStringWidthF(UTF8ToUTF16(kEllipsisStr
+ "/" + kEllipsisStr
),
85 GetStringWidthF(UTF8ToUTF16("d" + kEllipsisStr
), font_list
));
86 GURL
long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc");
87 base::string16 expected
= secure_display::ElideUrl(
88 long_url
, font_list
, available_width
, std::string());
89 // Ensure that the expected result still contains part of the directory name.
90 ASSERT_GT(expected
.length(), std::string("battersbox.com/d").length());
91 EXPECT_EQ(expected
, secure_display::ElideUrl(url
, font_list
, available_width
,
94 // More space available - elide directories, partially elide filename.
95 Testcase testcases
[] = {
96 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html",
97 "battersbox.com/" + kEllipsisStr
+ "/peter" + kEllipsisStr
},
99 RunUrlTest(testcases
, arraysize(testcases
));
102 // Test eliding of empty strings, URLs with ports, passwords, queries, etc.
103 TEST(TextEliderTest
, TestMoreEliding
) {
104 const std::string
kEllipsisStr(kEllipsis
);
105 Testcase testcases
[] = {
106 {"http://www.google.com/foo?bar", "www.google.com/foo?bar"},
107 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo?" + kEllipsisStr
},
108 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo" + kEllipsisStr
},
109 {"http://xyz.google.com/foo?bar", "xyz.google.com/fo" + kEllipsisStr
},
110 {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr
+ "/c?d"},
112 {"http://foo.bar..example.com...hello/test/filename.html",
113 "foo.bar..example.com...hello/" + kEllipsisStr
+ "/filename.html"},
114 {"http://foo.bar../", "foo.bar.."},
115 {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"},
116 {"http://me:mypass@secrethost.com:99/foo?bar#baz",
117 "secrethost.com:99/foo?bar#baz"},
118 {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"},
119 {"mailto:elgoato@elgoato.com", "mailto:elgoato@elgoato.com"},
120 {"javascript:click(0)", "javascript:click(0)"},
121 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
122 "chess.eecs.berkeley.edu:4430/login/arbitfilename"},
123 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
124 kEllipsisStr
+ "berkeley.edu:4430/" + kEllipsisStr
+ "/arbitfilename"},
127 {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
128 "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
130 // Invalid unescaping for path. The ref will always be valid UTF-8. We
132 // bother to do too many edge cases, since these are handled by the
135 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
136 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
139 RunUrlTest(testcases
, arraysize(testcases
));
142 // Test eliding of file: URLs.
143 TEST(TextEliderTest
, TestFileURLEliding
) {
144 const std::string
kEllipsisStr(kEllipsis
);
145 Testcase testcases
[] = {
146 {"file:///C:/path1/path2/path3/filename",
147 "file:///C:/path1/path2/path3/filename"},
148 {"file:///C:/path1/path2/path3/filename", "C:/path1/path2/path3/filename"},
149 // GURL parses "file:///C:path" differently on windows than it does on posix.
151 {"file:///C:path1/path2/path3/filename",
152 "C:/path1/path2/" + kEllipsisStr
+ "/filename"},
153 {"file:///C:path1/path2/path3/filename",
154 "C:/path1/" + kEllipsisStr
+ "/filename"},
155 {"file:///C:path1/path2/path3/filename",
156 "C:/" + kEllipsisStr
+ "/filename"},
157 #endif // defined(OS_WIN)
158 {"file://filer/foo/bar/file", "filer/foo/bar/file"},
159 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr
+ "/file"},
160 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr
+ "/file"},
161 {"file://filer/foo/", "file://filer/foo/"},
162 {"file://filer/foo/", "filer/foo/"},
163 {"file://filer/foo/", "filer" + kEllipsisStr
},
164 // Eliding file URLs with nothing after the ':' shouldn't crash.
165 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:", "aaa" + kEllipsisStr
},
166 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:/", "aaa" + kEllipsisStr
},
169 RunUrlTest(testcases
, arraysize(testcases
));
172 TEST(TextEliderTest
, TestHostEliding
) {
173 const std::string
kEllipsisStr(kEllipsis
);
174 Testcase testcases
[] = {
175 {"http://google.com", "google.com"},
176 {"http://subdomain.google.com", kEllipsisStr
+ ".google.com"},
177 {"http://reallyreallyreallylongdomainname.com",
178 "reallyreallyreallylongdomainname.com"},
179 {"http://a.b.c.d.e.f.com", kEllipsisStr
+ "f.com"},
180 {"http://foo", "foo"},
181 {"http://foo.bar", "foo.bar"},
182 {"http://subdomain.foo.bar", kEllipsisStr
+ "in.foo.bar"},
183 // IOS width calculations are off by a letter from other platforms for
184 // some strings from other platforms, probably for strings with too
185 // many kerned letters on the default font set.
187 {"http://subdomain.reallylongdomainname.com",
188 kEllipsisStr
+ "ain.reallylongdomainname.com"},
189 {"http://a.b.c.d.e.f.com", kEllipsisStr
+ ".e.f.com"},
190 #endif // !defined(OS_IOS)
193 for (size_t i
= 0; i
< arraysize(testcases
); ++i
) {
194 const float available_width
=
195 GetStringWidthF(UTF8ToUTF16(testcases
[i
].output
), gfx::FontList());
196 EXPECT_EQ(UTF8ToUTF16(testcases
[i
].output
),
197 secure_display::ElideHost(GURL(testcases
[i
].input
),
198 gfx::FontList(), available_width
));
201 // Trying to elide to a really short length will still keep the full TLD+1
203 base::ASCIIToUTF16("google.com"),
204 secure_display::ElideHost(GURL("http://google.com"), gfx::FontList(), 2));
205 EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr
+ ".google.com"),
206 secure_display::ElideHost(GURL("http://subdomain.google.com"),
207 gfx::FontList(), 2));
209 base::ASCIIToUTF16("foo.bar"),
210 secure_display::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2));
213 #endif // !defined(OS_ANDROID)
215 TEST(TextEliderTest
, FormatUrlForSecurityDisplay
) {
216 struct OriginTestData
{
217 const char* const description
;
218 const char* const input
;
219 const wchar_t* const output
;
222 const OriginTestData tests
[] = {
223 {"Empty URL", "", L
""},
224 {"HTTP URL", "http://www.google.com/", L
"http://www.google.com"},
225 {"HTTPS URL", "https://www.google.com/", L
"https://www.google.com"},
226 {"Standard HTTP port", "http://www.google.com:80/",
227 L
"http://www.google.com"},
228 {"Standard HTTPS port", "https://www.google.com:443/",
229 L
"https://www.google.com"},
230 {"Standard HTTP port, IDN Chinese",
231 "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80",
232 L
"http://xn--fiqs8s.icom.museum"},
233 {"HTTP URL, IDN Hebrew (RTL)",
235 "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d."
236 "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/",
237 L
"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum"},
238 {"HTTP URL with query string, IDN Arabic (RTL)",
239 "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no",
240 L
"http://xn--wgbh1c.icom.museum"},
241 {"Non-standard HTTP port", "http://www.google.com:9000/",
242 L
"http://www.google.com:9000"},
243 {"Non-standard HTTPS port", "https://www.google.com:9000/",
244 L
"https://www.google.com:9000"},
245 {"File URI", "file:///usr/example/file.html",
246 L
"file:///usr/example/file.html"},
247 {"File URI with hostname", "file://localhost/usr/example/file.html",
248 L
"file:///usr/example/file.html"},
249 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls",
250 L
"file:///CONTOSO/accounting/money.xls"},
252 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
253 L
"file:///C:/Program%20Files/Music/Web%20Sys/main.html"},
254 {"HTTP URL with path", "http://www.google.com/test.html",
255 L
"http://www.google.com"},
256 {"HTTPS URL with path", "https://www.google.com/test.html",
257 L
"https://www.google.com"},
258 {"Unusual secure scheme (wss)", "wss://www.google.com/",
259 L
"wss://www.google.com"},
260 {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/",
261 L
"gopher://www.google.com"},
262 {"Unlisted scheme (chrome)", "chrome://version", L
"chrome://version"},
263 {"HTTP IP address", "http://173.194.65.103", L
"http://173.194.65.103"},
264 {"HTTPS IP address", "https://173.194.65.103", L
"https://173.194.65.103"},
265 {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
266 L
"http://[fe80::202:b3ff:fe1e:8329]"},
267 {"HTTPS IPv6 address with port", "https://[2001:db8:0:1]:443/",
268 L
"https://[2001:db8:0:1]"},
269 {"HTTPS IP address, non-default port", "https://173.194.65.103:8443",
270 L
"https://173.194.65.103:8443"},
271 {"HTTP filesystem: URL with path",
272 "filesystem:http://www.google.com/temporary/test.html",
273 L
"filesystem:http://www.google.com"},
274 {"File filesystem: URL with path",
275 "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy",
276 L
"filesystem:file:///temporary/stuff/test.html"},
277 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php",
278 L
"twelve://www.cyber.org/wow.php"},
279 {"Invalid scheme 2", "://www.cyber.org/wow.php",
280 L
"://www.cyber.org/wow.php"},
281 {"Invalid host 1", "https://www.cyber../wow.php", L
"https://www.cyber.."},
282 {"Invalid host 2", "https://www...cyber/wow.php", L
"https://www...cyber"},
283 {"Invalid port 1", "https://173.194.65.103:000",
284 L
"https://173.194.65.103:0"},
285 {"Invalid port 2", "https://173.194.65.103:gruffle",
286 L
"https://173.194.65.103:gruffle"},
287 {"Invalid port 3", "https://173.194.65.103:/hello.aspx",
288 L
"https://173.194.65.103"},
289 {"Trailing dot in DNS name", "https://www.example.com./get/goat",
290 L
"https://www.example.com."},
292 "blob:http%3A//www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
293 L
"blob:http%3A//www.html5rocks.com/"
294 L
"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"},
297 const char languages
[] = "zh-TW,en-US,en,am,ar-EG,ar";
298 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
299 base::string16 formatted
= secure_display::FormatUrlForSecurityDisplay(
300 GURL(tests
[i
].input
), std::string());
301 EXPECT_EQ(base::WideToUTF16(tests
[i
].output
), formatted
)
302 << tests
[i
].description
;
303 base::string16 formatted_with_languages
=
304 secure_display::FormatUrlForSecurityDisplay(GURL(tests
[i
].input
),
306 EXPECT_EQ(base::WideToUTF16(tests
[i
].output
), formatted_with_languages
)
307 << tests
[i
].description
;
310 base::string16 formatted
=
311 secure_display::FormatUrlForSecurityDisplay(GURL(), std::string());
312 EXPECT_EQ(base::string16(), formatted
)
313 << "Explicitly test the 0-argument GURL constructor";