blob: db7f5e634086a806831d86e6e62dbc2001ae1b23 [file] [log] [blame]
miguelgdbcfb122015-07-23 10:45:111// 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
rsleevi24f64dc22015-08-07 21:39:215#include "components/url_formatter/elide_url.h"
miguelgdbcfb122015-07-23 10:45:116
avi5dd91f82015-12-25 22:30:467#include <stddef.h>
8
justincohenb96612bf2015-07-24 22:26:099#include "base/ios/ios_util.h"
avi5dd91f82015-12-25 22:30:4610#include "base/macros.h"
miguelgdbcfb122015-07-23 10:45:1111#include "base/strings/utf_string_conversions.h"
avi5dd91f82015-12-25 22:30:4612#include "build/build_config.h"
miguelgdbcfb122015-07-23 10:45:1113#include "testing/gtest/include/gtest/gtest.h"
miguelgdbcfb122015-07-23 10:45:1114#include "url/gurl.h"
15
rsleevi24f64dc22015-08-07 21:39:2116#if !defined(OS_ANDROID)
17#include "ui/gfx/font_list.h" // nogncheck
18#include "ui/gfx/text_elider.h" // nogncheck
19#include "ui/gfx/text_utils.h" // nogncheck
20#endif
miguelgdbcfb122015-07-23 10:45:1121
22namespace {
23
24struct Testcase {
25 const std::string input;
26 const std::string output;
eugenebutbec2c5f2015-08-07 23:22:3227 enum SupportedPlatforms {
28 ALL = 0,
29 NO_IOS9_OR_LATER,
30 NO_IOS,
31 } platforms;
miguelgdbcfb122015-07-23 10:45:1132};
33
34#if !defined(OS_ANDROID)
35void RunUrlTest(Testcase* testcases, size_t num_testcases) {
36 static const gfx::FontList font_list;
37 for (size_t i = 0; i < num_testcases; ++i) {
38 const GURL url(testcases[i].input);
miguelgdbcfb122015-07-23 10:45:1139 const float available_width =
rsleevi24f64dc22015-08-07 21:39:2140 gfx::GetStringWidthF(base::UTF8ToUTF16(testcases[i].output), font_list);
41 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
jshin1fb76462016-04-05 22:13:0342 url_formatter::ElideUrl(url, font_list, available_width));
miguelgdbcfb122015-07-23 10:45:1143 }
44}
45
46// Test eliding of commonplace URLs.
47TEST(TextEliderTest, TestGeneralEliding) {
rsleevi24f64dc22015-08-07 21:39:2148 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:1149 Testcase testcases[] = {
50 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
51 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
52 {"http://www.google.com/intl/en/ads/",
53 "google.com/intl/" + kEllipsisStr + "/ads/"},
54 {"http://www.google.com/intl/en/ads/",
55 "google.com/" + kEllipsisStr + "/ads/"},
56 {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr},
57 {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr},
58 {"https://subdomain.foo.com/bar/filename.html",
59 "subdomain.foo.com/bar/filename.html"},
60 {"https://subdomain.foo.com/bar/filename.html",
61 "subdomain.foo.com/" + kEllipsisStr + "/filename.html"},
62 {"http://subdomain.foo.com/bar/filename.html",
63 kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"},
64 {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired",
65 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr},
66 };
67
68 RunUrlTest(testcases, arraysize(testcases));
69}
70
71// When there is very little space available, the elision code will shorten
72// both path AND file name to an ellipsis - ".../...". To avoid this result,
73// there is a hack in place that simply treats them as one string in this
74// case.
75TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) {
rsleevi24f64dc22015-08-07 21:39:2176 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:1177
78 // Very little space, would cause double ellipsis.
79 gfx::FontList font_list;
80 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html");
rsleevi24f64dc22015-08-07 21:39:2181 float available_width = gfx::GetStringWidthF(
82 base::UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr),
miguelgdbcfb122015-07-23 10:45:1183 font_list);
84
85 // Create the expected string, after elision. Depending on font size, the
86 // directory might become /dir... or /di... or/d... - it never should be
87 // shorter than that. (If it is, the font considers d... to be longer
88 // than .../... - that should never happen).
rsleevi24f64dc22015-08-07 21:39:2189 ASSERT_GT(
90 gfx::GetStringWidthF(base::UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr),
91 font_list),
92 gfx::GetStringWidthF(base::UTF8ToUTF16("d" + kEllipsisStr), font_list));
miguelgdbcfb122015-07-23 10:45:1193 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc");
rsleevi24f64dc22015-08-07 21:39:2194 base::string16 expected = url_formatter::ElideUrl(
jshin1fb76462016-04-05 22:13:0395 long_url, font_list, available_width);
miguelgdbcfb122015-07-23 10:45:1196 // Ensure that the expected result still contains part of the directory name.
97 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length());
jshin1fb76462016-04-05 22:13:0398 EXPECT_EQ(expected, url_formatter::ElideUrl(url, font_list, available_width));
miguelgdbcfb122015-07-23 10:45:1199
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},
104 };
105 RunUrlTest(testcases, arraysize(testcases));
106}
107
108// Test eliding of empty strings, URLs with ports, passwords, queries, etc.
109TEST(TextEliderTest, TestMoreEliding) {
rsleevi24f64dc22015-08-07 21:39:21110 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11111 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:[email protected]: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:[email protected]", "mailto:[email protected]"},
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"},
131
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"},
135
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"},
143 };
144
145 RunUrlTest(testcases, arraysize(testcases));
146}
147
148// Test eliding of file: URLs.
149TEST(TextEliderTest, TestFileURLEliding) {
rsleevi24f64dc22015-08-07 21:39:21150 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11151 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},
173 };
174
175 RunUrlTest(testcases, arraysize(testcases));
176}
177
178TEST(TextEliderTest, TestHostEliding) {
rsleevi24f64dc22015-08-07 21:39:21179 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11180 Testcase testcases[] = {
eugenebutbec2c5f2015-08-07 23:22:32181 {"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},
jshinee46c77d2016-04-07 00:16:42199 // IDN - Greek alpha.beta.gamma.delta.epsilon.zeta.com
200 {"http://xn--mxa.xn--nxa.xn--oxa.xn--pxa.xn--qxa.xn--rxa.com",
201 kEllipsisStr + ".\xCE\xB5.\xCE\xB6.com", Testcase::NO_IOS},
miguelgdbcfb122015-07-23 10:45:11202 };
203
204 for (size_t i = 0; i < arraysize(testcases); ++i) {
eugenebutbec2c5f2015-08-07 23:22:32205#if defined(OS_IOS)
206 if (testcases[i].platforms == Testcase::NO_IOS ||
207 (testcases[i].platforms == Testcase::NO_IOS9_OR_LATER &&
208 base::ios::IsRunningOnIOS9OrLater())) {
209 continue;
210 }
211#endif
rsleevi24f64dc22015-08-07 21:39:21212 const float available_width = gfx::GetStringWidthF(
213 base::UTF8ToUTF16(testcases[i].output), gfx::FontList());
214 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
215 url_formatter::ElideHost(GURL(testcases[i].input),
216 gfx::FontList(), available_width));
miguelgdbcfb122015-07-23 10:45:11217 }
218
219 // Trying to elide to a really short length will still keep the full TLD+1
220 EXPECT_EQ(
221 base::ASCIIToUTF16("google.com"),
rsleevi24f64dc22015-08-07 21:39:21222 url_formatter::ElideHost(GURL("http://google.com"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11223 EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"),
rsleevi24f64dc22015-08-07 21:39:21224 url_formatter::ElideHost(GURL("http://subdomain.google.com"),
225 gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11226 EXPECT_EQ(
227 base::ASCIIToUTF16("foo.bar"),
rsleevi24f64dc22015-08-07 21:39:21228 url_formatter::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11229}
230
231#endif // !defined(OS_ANDROID)
232
233TEST(TextEliderTest, FormatUrlForSecurityDisplay) {
234 struct OriginTestData {
235 const char* const description;
236 const char* const input;
237 const wchar_t* const output;
benwells2337b8102016-04-20 01:53:53238 const wchar_t* const output_omit_web_scheme;
239 const wchar_t* const output_omit_cryptographic_scheme;
miguelgdbcfb122015-07-23 10:45:11240 };
241
242 const OriginTestData tests[] = {
benwells2337b8102016-04-20 01:53:53243 {"Empty URL", "", L"", L"", L""},
palmer153af982015-09-15 02:04:19244 {"HTTP URL", "http://www.google.com/", L"http://www.google.com",
benwells2337b8102016-04-20 01:53:53245 L"www.google.com", L"http://www.google.com"},
palmer153af982015-09-15 02:04:19246 {"HTTPS URL", "https://www.google.com/", L"https://www.google.com",
benwells2337b8102016-04-20 01:53:53247 L"www.google.com", L"www.google.com"},
miguelgdbcfb122015-07-23 10:45:11248 {"Standard HTTP port", "http://www.google.com:80/",
benwells2337b8102016-04-20 01:53:53249 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
miguelgdbcfb122015-07-23 10:45:11250 {"Standard HTTPS port", "https://www.google.com:443/",
benwells2337b8102016-04-20 01:53:53251 L"https://www.google.com", L"www.google.com", L"www.google.com"},
miguelgdbcfb122015-07-23 10:45:11252 {"Standard HTTP port, IDN Chinese",
253 "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80",
benwells2337b8102016-04-20 01:53:53254 L"http://xn--fiqs8s.icom.museum", L"xn--fiqs8s.icom.museum",
255 L"http://xn--fiqs8s.icom.museum"},
miguelgdbcfb122015-07-23 10:45:11256 {"HTTP URL, IDN Hebrew (RTL)",
257 "http://"
258 "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d."
259 "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/",
palmer153af982015-09-15 02:04:19260 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum",
benwells2337b8102016-04-20 01:53:53261 L"xn--4dbklr2c8d.xn--4dbrk0ce.museum",
262 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum"},
miguelgdbcfb122015-07-23 10:45:11263 {"HTTP URL with query string, IDN Arabic (RTL)",
264 "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no",
benwells2337b8102016-04-20 01:53:53265 L"http://xn--wgbh1c.icom.museum", L"xn--wgbh1c.icom.museum",
266 L"http://xn--wgbh1c.icom.museum"},
miguelgdbcfb122015-07-23 10:45:11267 {"Non-standard HTTP port", "http://www.google.com:9000/",
benwells2337b8102016-04-20 01:53:53268 L"http://www.google.com:9000", L"www.google.com:9000",
269 L"http://www.google.com:9000"},
miguelgdbcfb122015-07-23 10:45:11270 {"Non-standard HTTPS port", "https://www.google.com:9000/",
benwells2337b8102016-04-20 01:53:53271 L"https://www.google.com:9000", L"www.google.com:9000",
272 L"www.google.com:9000"},
miguelgdbcfb122015-07-23 10:45:11273 {"File URI", "file:///usr/example/file.html",
benwells2337b8102016-04-20 01:53:53274 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
275 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11276 {"File URI with hostname", "file://localhost/usr/example/file.html",
benwells2337b8102016-04-20 01:53:53277 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
278 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11279 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls",
palmer153af982015-09-15 02:04:19280 L"file:///CONTOSO/accounting/money.xls",
benwells2337b8102016-04-20 01:53:53281 L"file:///CONTOSO/accounting/money.xls",
miguelgdbcfb122015-07-23 10:45:11282 L"file:///CONTOSO/accounting/money.xls"},
283 {"UNC File URI 2",
284 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
palmer153af982015-09-15 02:04:19285 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
benwells2337b8102016-04-20 01:53:53286 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
miguelgdbcfb122015-07-23 10:45:11287 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html"},
288 {"HTTP URL with path", "http://www.google.com/test.html",
benwells2337b8102016-04-20 01:53:53289 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
miguelgdbcfb122015-07-23 10:45:11290 {"HTTPS URL with path", "https://www.google.com/test.html",
benwells2337b8102016-04-20 01:53:53291 L"https://www.google.com", L"www.google.com", L"www.google.com"},
miguelgdbcfb122015-07-23 10:45:11292 {"Unusual secure scheme (wss)", "wss://www.google.com/",
benwells2337b8102016-04-20 01:53:53293 L"wss://www.google.com", L"wss://www.google.com", L"www.google.com"},
miguelgdbcfb122015-07-23 10:45:11294 {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/",
benwells2337b8102016-04-20 01:53:53295 L"gopher://www.google.com", L"gopher://www.google.com",
296 L"gopher://www.google.com"},
palmer153af982015-09-15 02:04:19297 {"Unlisted scheme (chrome)", "chrome://version", L"chrome://version",
benwells2337b8102016-04-20 01:53:53298 L"chrome://version", L"chrome://version"},
palmer153af982015-09-15 02:04:19299 {"HTTP IP address", "http://173.194.65.103", L"http://173.194.65.103",
benwells2337b8102016-04-20 01:53:53300 L"173.194.65.103", L"http://173.194.65.103"},
palmer153af982015-09-15 02:04:19301 {"HTTPS IP address", "https://173.194.65.103", L"https://173.194.65.103",
benwells2337b8102016-04-20 01:53:53302 L"173.194.65.103", L"173.194.65.103"},
miguelgdbcfb122015-07-23 10:45:11303 {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
benwells2337b8102016-04-20 01:53:53304 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
305 L"http://[fe80::202:b3ff:fe1e:8329]"},
306 {"HTTPs IPv6 address",
307 "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
308 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
309 L"[fe80::202:b3ff:fe1e:8329]"},
310 {"HTTP IPv6 address with port",
311 "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:80/",
312 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
313 L"http://[fe80::202:b3ff:fe1e:8329]"},
314 {"HTTPs IPv6 address with port",
315 "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:443/",
316 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
317 L"[fe80::202:b3ff:fe1e:8329]"},
318 {"Invalid IPv6 address", "https://[2001:db8:0:1]/",
319 L"https://[2001:db8:0:1]", L"https://[2001:db8:0:1]",
320 L"https://[2001:db8:0:1]"},
miguelgdbcfb122015-07-23 10:45:11321 {"HTTPS IP address, non-default port", "https://173.194.65.103:8443",
benwells2337b8102016-04-20 01:53:53322 L"https://173.194.65.103:8443", L"173.194.65.103:8443",
323 L"173.194.65.103:8443"},
miguelgdbcfb122015-07-23 10:45:11324 {"HTTP filesystem: URL with path",
325 "filesystem:http://www.google.com/temporary/test.html",
benwells2337b8102016-04-20 01:53:53326 L"filesystem:http://www.google.com", L"filesystem:http://www.google.com",
miguelgdbcfb122015-07-23 10:45:11327 L"filesystem:http://www.google.com"},
328 {"File filesystem: URL with path",
329 "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy",
palmer153af982015-09-15 02:04:19330 L"filesystem:file:///temporary/stuff/test.html",
benwells2337b8102016-04-20 01:53:53331 L"filesystem:file:///temporary/stuff/test.html",
miguelgdbcfb122015-07-23 10:45:11332 L"filesystem:file:///temporary/stuff/test.html"},
333 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53334 L"twelve://www.cyber.org/wow.php", L"twelve://www.cyber.org/wow.php",
335 L"twelve://www.cyber.org/wow.php"},
miguelgdbcfb122015-07-23 10:45:11336 {"Invalid scheme 2", "://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53337 L"://www.cyber.org/wow.php", L"://www.cyber.org/wow.php",
338 L"://www.cyber.org/wow.php"},
palmer153af982015-09-15 02:04:19339 {"Invalid host 1", "https://www.cyber../wow.php", L"https://www.cyber..",
benwells2337b8102016-04-20 01:53:53340 L"www.cyber..", L"www.cyber.."},
palmer153af982015-09-15 02:04:19341 {"Invalid host 2", "https://www...cyber/wow.php", L"https://www...cyber",
benwells2337b8102016-04-20 01:53:53342 L"www...cyber", L"www...cyber"},
miguelgdbcfb122015-07-23 10:45:11343 {"Invalid port 1", "https://173.194.65.103:000",
benwells2337b8102016-04-20 01:53:53344 L"https://173.194.65.103:0", L"173.194.65.103:0", L"173.194.65.103:0"},
miguelgdbcfb122015-07-23 10:45:11345 {"Invalid port 2", "https://173.194.65.103:gruffle",
benwells2337b8102016-04-20 01:53:53346 L"https://173.194.65.103:gruffle", L"https://173.194.65.103:gruffle",
347 L"https://173.194.65.103:gruffle"},
miguelgdbcfb122015-07-23 10:45:11348 {"Invalid port 3", "https://173.194.65.103:/hello.aspx",
benwells2337b8102016-04-20 01:53:53349 L"https://173.194.65.103", L"173.194.65.103", L"173.194.65.103"},
miguelgdbcfb122015-07-23 10:45:11350 {"Trailing dot in DNS name", "https://www.example.com./get/goat",
benwells2337b8102016-04-20 01:53:53351 L"https://www.example.com.", L"www.example.com.", L"www.example.com."},
miguelgdbcfb122015-07-23 10:45:11352 {"Blob URL",
nick1b17dc32016-04-15 21:34:04353 "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
354 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19355 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
nick1b17dc32016-04-15 21:34:04356 L"blob:http://www.html5rocks.com/"
benwells2337b8102016-04-20 01:53:53357 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
358 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19359 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}};
miguelgdbcfb122015-07-23 10:45:11360
miguelgdbcfb122015-07-23 10:45:11361 for (size_t i = 0; i < arraysize(tests); ++i) {
jshin1fb76462016-04-05 22:13:03362 base::string16 formatted =
363 url_formatter::FormatUrlForSecurityDisplay(GURL(tests[i].input));
miguelgdbcfb122015-07-23 10:45:11364 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
365 << tests[i].description;
palmer153af982015-09-15 02:04:19366
benwells2337b8102016-04-20 01:53:53367 base::string16 formatted_omit_web_scheme =
368 url_formatter::FormatUrlForSecurityDisplay(
369 GURL(tests[i].input),
370 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
371 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme),
372 formatted_omit_web_scheme)
373 << tests[i].description;
374
375 base::string16 formatted_omit_cryptographic_scheme =
376 url_formatter::FormatUrlForSecurityDisplay(
377 GURL(tests[i].input),
378 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
379 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme),
380 formatted_omit_cryptographic_scheme)
palmer153af982015-09-15 02:04:19381 << tests[i].description;
miguelgdbcfb122015-07-23 10:45:11382 }
383
384 base::string16 formatted =
jshin1fb76462016-04-05 22:13:03385 url_formatter::FormatUrlForSecurityDisplay(GURL());
miguelgdbcfb122015-07-23 10:45:11386 EXPECT_EQ(base::string16(), formatted)
387 << "Explicitly test the 0-argument GURL constructor";
palmer153af982015-09-15 02:04:19388
389 base::string16 formatted_omit_scheme =
benwells2337b8102016-04-20 01:53:53390 url_formatter::FormatUrlForSecurityDisplay(
391 GURL(), url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
392 EXPECT_EQ(base::string16(), formatted_omit_scheme)
393 << "Explicitly test the 0-argument GURL constructor";
394
395 formatted_omit_scheme = url_formatter::FormatUrlForSecurityDisplay(
396 GURL(), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
palmer153af982015-09-15 02:04:19397 EXPECT_EQ(base::string16(), formatted_omit_scheme)
398 << "Explicitly test the 0-argument GURL constructor";
miguelgdbcfb122015-07-23 10:45:11399}
400
401} // namespace