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