blob: d403ce065f459dfe97062b8df1d893ed97046be1 [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
avi5dd91f82015-12-25 22:30:469#include "base/macros.h"
gabf64a25e2017-05-12 19:42:5610#include "base/message_loop/message_loop.h"
kulshince612eb2016-06-29 18:46:5111#include "base/run_loop.h"
miguelgdbcfb122015-07-23 10:45:1112#include "base/strings/utf_string_conversions.h"
avi5dd91f82015-12-25 22:30:4613#include "build/build_config.h"
Christopher Grant6871e1212017-10-09 15:47:5014#include "components/url_formatter/url_formatter.h"
15#include "net/base/escape.h"
miguelgdbcfb122015-07-23 10:45:1116#include "testing/gtest/include/gtest/gtest.h"
Christopher Grant6871e1212017-10-09 15:47:5017#include "ui/gfx/font_list.h"
18#include "ui/gfx/text_elider.h"
19#include "ui/gfx/text_utils.h"
miguelgdbcfb122015-07-23 10:45:1120#include "url/gurl.h"
juncaicb63cac2016-05-13 00:41:5921#include "url/origin.h"
miguelgdbcfb122015-07-23 10:45:1122
miguelgdbcfb122015-07-23 10:45:1123namespace {
24
Christopher Grant6871e1212017-10-09 15:47:5025enum ElisionMethod {
26 kMethodOriginal,
27 kMethodSimple,
28};
29
miguelgdbcfb122015-07-23 10:45:1130struct Testcase {
31 const std::string input;
32 const std::string output;
33};
34
Christopher Grant5ed8d112017-09-12 20:00:0635struct ProgressiveTestcase {
36 const std::string input;
37 const std::vector<std::string> output;
38};
39
Christopher Grant6871e1212017-10-09 15:47:5040struct UrlComponent {
41 url::Parsed::ComponentType type;
42 int begin;
43 int len;
44};
45
46struct ParsingTestcase {
47 const std::string input;
48 const std::string output;
49 const std::vector<UrlComponent> components;
50};
51
Christopher Grantbd541e52017-10-13 20:59:4152base::string16 FormatAndElideUrlSimple(const GURL& url,
53 const gfx::FontList& font_list,
54 float available_pixel_width,
55 url::Parsed* parsed) {
56 const base::string16 url_string = url_formatter::FormatUrl(
57 url, url_formatter::kFormatUrlOmitDefaults, net::UnescapeRule::SPACES,
58 parsed, nullptr, nullptr);
59 return url_formatter::ElideUrlSimple(url, url_string, font_list,
60 available_pixel_width, parsed);
61}
62
Christopher Grant6871e1212017-10-09 15:47:5063base::string16 Elide(const GURL& url,
64 const gfx::FontList& font_list,
65 float available_width,
66 ElisionMethod method) {
67 switch (method) {
68 case kMethodSimple: {
69 url::Parsed parsed;
Christopher Grantbd541e52017-10-13 20:59:4170 return FormatAndElideUrlSimple(url, font_list, available_width, &parsed);
Christopher Grant6871e1212017-10-09 15:47:5071 }
72#if !defined(OS_ANDROID)
73 case kMethodOriginal:
74 return url_formatter::ElideUrl(url, font_list, available_width);
75#endif
76 default:
77 NOTREACHED();
78 return base::string16();
79 }
80}
81
82url::Component* GetComponent(url::Parsed* parsed,
83 url::Parsed::ComponentType type) {
84 switch (type) {
85 case url::Parsed::SCHEME:
86 return &parsed->scheme;
87 case url::Parsed::USERNAME:
88 return &parsed->username;
89 case url::Parsed::PASSWORD:
90 return &parsed->password;
91 case url::Parsed::HOST:
92 return &parsed->host;
93 case url::Parsed::PORT:
94 return &parsed->port;
95 case url::Parsed::PATH:
96 return &parsed->path;
97 case url::Parsed::QUERY:
98 return &parsed->query;
99 case url::Parsed::REF:
100 return &parsed->ref;
101 default:
102 NOTREACHED();
103 return nullptr;
104 }
105}
106
Christopher Grant5ed8d112017-09-12 20:00:06107// Verify that one or more URLs passes through an explicit sequence of elided
108// strings as available space progressively decreases. This helps ensure that
109// transitional corner cases are handled properly. To be tolerant of
110// character-width variation across platforms, the test allows a limited number
111// of expected strings to be skipped mid-run. The first and last expected
112// strings must be matched. If the algorithm produces a string that isn't in the
113// expected string list, the test fill fail. Example test expectations:
114//
115// google.com/intl/en/.../ads/ <-- Must match.
116// google.com/intl/.../ads/
117// google.com/.../ads/
118// google.com/intl... <- Elider can skip this, in case the 'l' does not fit.
119// google.com/int...
120// google.com/in... <- Must match.
121//
122void RunProgressiveElisionTest(
Christopher Grant6871e1212017-10-09 15:47:50123 const std::vector<ProgressiveTestcase>& testcases,
124 ElisionMethod method) {
Christopher Grant5ed8d112017-09-12 20:00:06125 const gfx::FontList font_list;
126 for (const auto& testcase : testcases) {
127 SCOPED_TRACE("Eliding " + testcase.input);
128 const GURL url(testcase.input);
129
130 // Occasionally, a parsed URL can grow in length before elision, such as
131 // when parsing a Windows file path with missing slashes.
132 ASSERT_FALSE(testcase.output.empty());
133 float width = std::max(
134 gfx::GetStringWidthF(base::UTF8ToUTF16(testcase.input), font_list),
135 gfx::GetStringWidthF(base::UTF8ToUTF16(testcase.output.front()),
136 font_list));
137
138 // Ideally, this test would iterate through all available field widths on a
139 // per-pixel basis, but this is slow. Instead, compute the next input field
140 // width as slightly less than the previous elided string. This approach
141 // misses coverage in cases where a smaller available width generates a
142 // longer string than some other larger available width, but the tradeoff
143 // feels acceptable.
144 int mismatches = 0;
145 const int kMaxConsecutiveMismatches = 3;
146 for (size_t i = 0; i < testcase.output.size(); i++) {
147 const auto& expected = testcase.output[i];
148 base::string16 expected_utf16 = base::UTF8ToUTF16(expected);
Christopher Grant6871e1212017-10-09 15:47:50149 base::string16 elided = Elide(url, font_list, width, method);
Christopher Grant5ed8d112017-09-12 20:00:06150 if (expected_utf16 != elided) {
151 if (i > 0 && i < testcase.output.size() - 1 &&
152 mismatches < kMaxConsecutiveMismatches) {
153 mismatches++;
154 continue;
155 }
156 EXPECT_EQ(expected_utf16, elided);
157 break;
158 }
159 mismatches = 0;
160 float new_width = gfx::GetStringWidthF(elided, font_list);
161 // Elision rounds fractional available widths up.
162 EXPECT_LE(new_width, std::ceil(width)) << " at " << elided;
163 width = new_width - 1.0f;
164 }
165 }
166}
167
Christopher Grant6871e1212017-10-09 15:47:50168#if !defined(OS_ANDROID)
169
170void RunElisionTest(const std::vector<Testcase>& testcases) {
171 const gfx::FontList font_list;
172 for (const auto& testcase : testcases) {
173 SCOPED_TRACE("Eliding " + testcase.input);
174 const GURL url(testcase.input);
175 const float available_width =
176 gfx::GetStringWidthF(base::UTF8ToUTF16(testcase.output), font_list);
177 EXPECT_EQ(base::UTF8ToUTF16(testcase.output),
178 url_formatter::ElideUrl(url, font_list, available_width));
179 }
180}
181
miguelgdbcfb122015-07-23 10:45:11182// Test eliding of commonplace URLs.
183TEST(TextEliderTest, TestGeneralEliding) {
rsleevi24f64dc22015-08-07 21:39:21184 const std::string kEllipsisStr(gfx::kEllipsis);
Christopher Grant5ed8d112017-09-12 20:00:06185 const std::vector<ProgressiveTestcase> progressive_testcases = {
186 // Elide a non-www URL (www URLs are handled differently). In this first
187 // case, elide down to nothing to test the terminal cases.
188 {"http://xyz.google.com/foo?bar",
189 {
190 /* clang-format off */
191 "xyz.google.com/foo?bar",
192 "xyz.google.com/foo?b" + kEllipsisStr,
193 "xyz.google.com/foo?" + kEllipsisStr,
194 "xyz.google.com/foo" + kEllipsisStr,
195 "xyz.google.com/fo" + kEllipsisStr,
196 "xyz.google.com/f" + kEllipsisStr,
197 kEllipsisStr + "google.com/foo" + kEllipsisStr,
198 kEllipsisStr + "google.com/fo" + kEllipsisStr,
199 kEllipsisStr + "google.com/f" + kEllipsisStr,
200 kEllipsisStr + "google.com/" + kEllipsisStr,
201 kEllipsisStr + "google.com" + kEllipsisStr,
202 kEllipsisStr + "google.co" + kEllipsisStr,
203 kEllipsisStr + "google.c" + kEllipsisStr,
204 kEllipsisStr + "google." + kEllipsisStr,
205 kEllipsisStr + "google" + kEllipsisStr,
206 kEllipsisStr + "googl" + kEllipsisStr,
207 kEllipsisStr + "goog" + kEllipsisStr,
208 kEllipsisStr + "goo" + kEllipsisStr,
209 kEllipsisStr + "go" + kEllipsisStr,
210 kEllipsisStr + "g" + kEllipsisStr,
211 kEllipsisStr + kEllipsisStr,
212 kEllipsisStr,
213 ""
214 /* clang-format on */
215 }},
216 // The trailing directory name is preserved
miguelgdbcfb122015-07-23 10:45:11217 {"http://www.google.com/intl/en/ads/",
Christopher Grant5ed8d112017-09-12 20:00:06218 {
219 /* clang-format off */
220 "www.google.com/intl/en/ads/",
221 "google.com/intl/en/ads/",
222 "google.com/intl/" + kEllipsisStr + "/ads/",
223 "google.com/" + kEllipsisStr + "/ads/",
224 "google.com/" + kEllipsisStr + "/ad" + kEllipsisStr,
225 "google.com/" + kEllipsisStr + "/a" + kEllipsisStr,
226 "google.com/intl/e" + kEllipsisStr,
227 "google.com/intl/" + kEllipsisStr,
228 "google.com/intl" + kEllipsisStr,
229 "google.com/int" + kEllipsisStr,
230 "google.com/in" + kEllipsisStr,
231 "google.com/i" + kEllipsisStr,
232 "google.com/" + kEllipsisStr,
233 "google.com" + kEllipsisStr,
234 "google.co" + kEllipsisStr,
235 "google.c" + kEllipsisStr,
236 "google." + kEllipsisStr,
237 "google" + kEllipsisStr,
238 "googl" + kEllipsisStr,
239 /* clang-format on */
240 }},
241 // Subdomain is completely elided if the last path element doesn't fit.
miguelgdbcfb122015-07-23 10:45:11242 {"https://subdomain.foo.com/bar/filename.html",
Christopher Grant5ed8d112017-09-12 20:00:06243 {
244 /* clang-format off */
245 "https://subdomain.foo.com/bar/filename.html",
246 "subdomain.foo.com/bar/filename.html",
247 "subdomain.foo.com/" + kEllipsisStr + "/filename.html",
248 kEllipsisStr + "foo.com/bar/filename.html",
249 kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html",
250 /* clang-format on */
251 }},
252 // Path eliding works when a query is present.
253 {"http://www.g.com/subdir/ads/?query",
254 {
255 /* clang-format off */
256 "www.g.com/subdir/ads/?query",
257 "www.g.com/subdir/ads/?que" + kEllipsisStr,
258 "www.g.com/subdir/ads/?qu" + kEllipsisStr,
259 "www.g.com/subdir/ads/?q" + kEllipsisStr,
260 "www.g.com/subdir/ads/?" + kEllipsisStr,
261 "www.g.com/subdir/ads/" + kEllipsisStr,
262 "www.g.com/subdir/ads" + kEllipsisStr,
263 "www.g.com/subdir/ad" + kEllipsisStr,
264 /* clang-format on */
265 }},
miguelgdbcfb122015-07-23 10:45:11266 };
Christopher Grant6871e1212017-10-09 15:47:50267 RunProgressiveElisionTest(progressive_testcases, kMethodOriginal);
miguelgdbcfb122015-07-23 10:45:11268}
269
270// When there is very little space available, the elision code will shorten
271// both path AND file name to an ellipsis - ".../...". To avoid this result,
272// there is a hack in place that simply treats them as one string in this
273// case.
274TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) {
rsleevi24f64dc22015-08-07 21:39:21275 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11276
277 // Very little space, would cause double ellipsis.
278 gfx::FontList font_list;
279 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html");
rsleevi24f64dc22015-08-07 21:39:21280 float available_width = gfx::GetStringWidthF(
281 base::UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr),
miguelgdbcfb122015-07-23 10:45:11282 font_list);
283
284 // Create the expected string, after elision. Depending on font size, the
285 // directory might become /dir... or /di... or/d... - it never should be
286 // shorter than that. (If it is, the font considers d... to be longer
287 // than .../... - that should never happen).
rsleevi24f64dc22015-08-07 21:39:21288 ASSERT_GT(
289 gfx::GetStringWidthF(base::UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr),
290 font_list),
291 gfx::GetStringWidthF(base::UTF8ToUTF16("d" + kEllipsisStr), font_list));
miguelgdbcfb122015-07-23 10:45:11292 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc");
rsleevi24f64dc22015-08-07 21:39:21293 base::string16 expected = url_formatter::ElideUrl(
jshin1fb76462016-04-05 22:13:03294 long_url, font_list, available_width);
miguelgdbcfb122015-07-23 10:45:11295 // Ensure that the expected result still contains part of the directory name.
296 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length());
jshin1fb76462016-04-05 22:13:03297 EXPECT_EQ(expected, url_formatter::ElideUrl(url, font_list, available_width));
miguelgdbcfb122015-07-23 10:45:11298
Matt Giuca49fcda172017-08-29 07:16:12299 // Regression test for https://crbug.com/756717. An empty path, eliding to a
300 // width in between the full domain ("www.angelfire.lycos.com") and a bit
301 // longer than the ETLD+1 ("…lycos.com…/…UV"). This previously crashed due to
302 // the path being empty.
303 url = GURL("http://www.angelfire.lycos.com/");
304 available_width = gfx::GetStringWidthF(
305 base::UTF8ToUTF16(kEllipsisStr + "angelfire.lycos.com"), font_list);
306 EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + "lycos.com"),
307 url_formatter::ElideUrl(url, font_list, available_width));
308
miguelgdbcfb122015-07-23 10:45:11309 // More space available - elide directories, partially elide filename.
Christopher Grant5ed8d112017-09-12 20:00:06310 const std::vector<Testcase> testcases = {
miguelgdbcfb122015-07-23 10:45:11311 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html",
312 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr},
313 };
Christopher Grant5ed8d112017-09-12 20:00:06314 RunElisionTest(testcases);
miguelgdbcfb122015-07-23 10:45:11315}
316
317// Test eliding of empty strings, URLs with ports, passwords, queries, etc.
Christopher Grant5ed8d112017-09-12 20:00:06318TEST(TextEliderTest, TestElisionSpecialCases) {
kulshince612eb2016-06-29 18:46:51319#if defined(OS_WIN)
320 // Needed to bypass DCHECK in GetFallbackFont.
321 base::MessageLoopForUI message_loop;
322#endif
rsleevi24f64dc22015-08-07 21:39:21323 const std::string kEllipsisStr(gfx::kEllipsis);
Christopher Grant5ed8d112017-09-12 20:00:06324 const std::vector<Testcase> testcases = {
Matt Giuca49fcda172017-08-29 07:16:12325 // URL with "www" subdomain (gets removed specially).
326 {"http://www.google.com/foo?bar", "www.google.com/foo?bar"},
327 {"http://www.google.com/foo?bar", "google.com/foo?bar"},
328
Matt Giuca51f68cc2017-07-07 01:34:57329 // URL with no path.
Matt Giuca49fcda172017-08-29 07:16:12330 {"http://xyz.google.com", kEllipsisStr + "google.com"},
331 {"https://xyz.google.com", kEllipsisStr + "google.com"},
Matt Giuca51f68cc2017-07-07 01:34:57332
miguelgdbcfb122015-07-23 10:45:11333 {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"},
334 {"", ""},
335 {"http://foo.bar..example.com...hello/test/filename.html",
336 "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"},
337 {"http://foo.bar../", "foo.bar.."},
338 {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"},
339 {"http://me:[email protected]:99/foo?bar#baz",
340 "secrethost.com:99/foo?bar#baz"},
341 {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"},
342 {"mailto:[email protected]", "mailto:[email protected]"},
343 {"javascript:click(0)", "javascript:click(0)"},
344 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
345 "chess.eecs.berkeley.edu:4430/login/arbitfilename"},
346 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
347 kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"},
348
349 // Unescaping.
Eric Lawrence03f9a902017-12-16 04:48:11350 {"http://www/%E4%BD%A0%E5%A5%BD?"
351 "q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0\xe4\xbd\xa0\xe4\xbd\xa0",
352 "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0" +
Mike Westf8f6ed52017-10-09 20:58:50353 kEllipsisStr},
miguelgdbcfb122015-07-23 10:45:11354
355 // Invalid unescaping for path. The ref will always be valid UTF-8. We
Christopher Grant5ed8d112017-09-12 20:00:06356 // don't bother to do too many edge cases, since these are handled by the
357 // escaper unittest.
miguelgdbcfb122015-07-23 10:45:11358 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
Eric Lawrence03f9a902017-12-16 04:48:11359 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
miguelgdbcfb122015-07-23 10:45:11360 };
361
Christopher Grant5ed8d112017-09-12 20:00:06362 RunElisionTest(testcases);
miguelgdbcfb122015-07-23 10:45:11363}
364
365// Test eliding of file: URLs.
366TEST(TextEliderTest, TestFileURLEliding) {
rsleevi24f64dc22015-08-07 21:39:21367 const std::string kEllipsisStr(gfx::kEllipsis);
Christopher Grant5ed8d112017-09-12 20:00:06368 const std::vector<ProgressiveTestcase> progressive_testcases = {
miguelgdbcfb122015-07-23 10:45:11369 {"file:///C:/path1/path2/path3/filename",
Christopher Grant5ed8d112017-09-12 20:00:06370 {
371 /* clang-format off */
372 "file:///C:/path1/path2/path3/filename",
373 "C:/path1/path2/path3/filename",
374 "C:/path1/path2/" + kEllipsisStr + "/filename",
375 /* clang-format on */
376 }},
miguelgdbcfb122015-07-23 10:45:11377// GURL parses "file:///C:path" differently on windows than it does on posix.
378#if defined(OS_WIN)
379 {"file:///C:path1/path2/path3/filename",
Christopher Grant5ed8d112017-09-12 20:00:06380 {
381 /* clang-format off */
382 "file:///C:/path1/path2/path3/filename",
383 "C:/path1/path2/path3/filename",
384 "C:/path1/path2/" + kEllipsisStr + "/filename",
385 "C:/path1/" + kEllipsisStr + "/filename",
386 "C:/" + kEllipsisStr + "/filename",
387 /* clang-format on */
388 }},
miguelgdbcfb122015-07-23 10:45:11389#endif // defined(OS_WIN)
Christopher Grant5ed8d112017-09-12 20:00:06390 {"file://filer/foo/bar/file",
391 {
392 /* clang-format off */
393 "file://filer/foo/bar/file",
394 "filer/foo/bar/file",
395 "filer/foo/" + kEllipsisStr + "/file",
396 "filer/" + kEllipsisStr + "/file",
397 "filer/foo" + kEllipsisStr,
398 "filer/fo" + kEllipsisStr,
399 "filer/f" + kEllipsisStr,
400 "filer/" + kEllipsisStr,
401 "filer" + kEllipsisStr,
402 "file" + kEllipsisStr,
403 /* clang-format on */
404 }},
miguelgdbcfb122015-07-23 10:45:11405 };
406
Christopher Grant6871e1212017-10-09 15:47:50407 RunProgressiveElisionTest(progressive_testcases, kMethodOriginal);
Christopher Grant5ed8d112017-09-12 20:00:06408
409 const std::vector<Testcase> testcases = {
410 // Eliding file URLs with nothing after the ':' shouldn't crash.
411 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:", "aaa" + kEllipsisStr},
412 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:/", "aaa" + kEllipsisStr},
413 };
414 RunElisionTest(testcases);
miguelgdbcfb122015-07-23 10:45:11415}
416
417TEST(TextEliderTest, TestHostEliding) {
rsleevi24f64dc22015-08-07 21:39:21418 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11419 Testcase testcases[] = {
pkl2affa912017-02-22 15:23:20420 {"http://google.com", "google.com"},
421 {"http://reallyreallyreallylongdomainname.com",
422 "reallyreallyreallylongdomainname.com"},
423 {"http://foo", "foo"},
424 {"http://foo.bar", "foo.bar"},
pkl2affa912017-02-22 15:23:20425 {"http://subdomain.google.com", kEllipsisStr + ".google.com"},
426 {"http://a.b.c.d.e.f.com", kEllipsisStr + "f.com"},
427 {"http://subdomain.foo.bar", kEllipsisStr + "in.foo.bar"},
428 {"http://subdomain.reallylongdomainname.com",
429 kEllipsisStr + "ain.reallylongdomainname.com"},
430 {"http://a.b.c.d.e.f.com", kEllipsisStr + ".e.f.com"},
431 // IDN - Greek alpha.beta.gamma.delta.epsilon.zeta.com
432 {"http://xn--mxa.xn--nxa.xn--oxa.xn--pxa.xn--qxa.xn--rxa.com",
433 kEllipsisStr + ".\xCE\xB5.\xCE\xB6.com"},
miguelgdbcfb122015-07-23 10:45:11434 };
435
436 for (size_t i = 0; i < arraysize(testcases); ++i) {
rsleevi24f64dc22015-08-07 21:39:21437 const float available_width = gfx::GetStringWidthF(
438 base::UTF8ToUTF16(testcases[i].output), gfx::FontList());
439 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
440 url_formatter::ElideHost(GURL(testcases[i].input),
441 gfx::FontList(), available_width));
miguelgdbcfb122015-07-23 10:45:11442 }
443
444 // Trying to elide to a really short length will still keep the full TLD+1
445 EXPECT_EQ(
446 base::ASCIIToUTF16("google.com"),
rsleevi24f64dc22015-08-07 21:39:21447 url_formatter::ElideHost(GURL("http://google.com"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11448 EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"),
rsleevi24f64dc22015-08-07 21:39:21449 url_formatter::ElideHost(GURL("http://subdomain.google.com"),
450 gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11451 EXPECT_EQ(
452 base::ASCIIToUTF16("foo.bar"),
rsleevi24f64dc22015-08-07 21:39:21453 url_formatter::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11454}
455
456#endif // !defined(OS_ANDROID)
457
juncaicb63cac2016-05-13 00:41:59458struct OriginTestData {
459 const char* const description;
460 const char* const input;
461 const wchar_t* const output;
462 const wchar_t* const output_omit_web_scheme;
463 const wchar_t* const output_omit_cryptographic_scheme;
464};
465
466// Common test data for both FormatUrlForSecurityDisplay() and
467// FormatOriginForSecurityDisplay()
468const OriginTestData common_tests[] = {
469 {"Empty URL", "", L"", L"", L""},
470 {"HTTP URL", "http://www.google.com/", L"http://www.google.com",
471 L"www.google.com", L"http://www.google.com"},
472 {"HTTPS URL", "https://www.google.com/", L"https://www.google.com",
473 L"www.google.com", L"www.google.com"},
474 {"Standard HTTP port", "http://www.google.com:80/",
475 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
476 {"Standard HTTPS port", "https://www.google.com:443/",
477 L"https://www.google.com", L"www.google.com", L"www.google.com"},
478 {"Standard HTTP port, IDN Chinese",
479 "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80",
jshin78809c4d82016-10-06 20:15:45480 L"http://\x4e2d\x56fd.icom.museum", L"\x4e2d\x56fd.icom.museum",
481 L"http://\x4e2d\x56fd.icom.museum"},
juncaicb63cac2016-05-13 00:41:59482 {"HTTP URL, IDN Hebrew (RTL)",
483 "http://"
484 "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d."
485 "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/",
486 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum",
487 L"xn--4dbklr2c8d.xn--4dbrk0ce.museum",
488 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum"},
489 {"HTTP URL with query string, IDN Arabic (RTL)",
490 "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no",
491 L"http://xn--wgbh1c.icom.museum", L"xn--wgbh1c.icom.museum",
492 L"http://xn--wgbh1c.icom.museum"},
493 {"Non-standard HTTP port", "http://www.google.com:9000/",
494 L"http://www.google.com:9000", L"www.google.com:9000",
495 L"http://www.google.com:9000"},
496 {"Non-standard HTTPS port", "https://www.google.com:9000/",
497 L"https://www.google.com:9000", L"www.google.com:9000",
498 L"www.google.com:9000"},
499 {"HTTP URL with path", "http://www.google.com/test.html",
500 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
501 {"HTTPS URL with path", "https://www.google.com/test.html",
502 L"https://www.google.com", L"www.google.com", L"www.google.com"},
503 {"Unusual secure scheme (wss)", "wss://www.google.com/",
504 L"wss://www.google.com", L"wss://www.google.com", L"www.google.com"},
505 {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/",
506 L"gopher://www.google.com", L"gopher://www.google.com",
507 L"gopher://www.google.com"},
508 {"Unlisted scheme (chrome)", "chrome://version", L"chrome://version",
509 L"chrome://version", L"chrome://version"},
510 {"HTTP IP address", "http://173.194.65.103", L"http://173.194.65.103",
511 L"173.194.65.103", L"http://173.194.65.103"},
512 {"HTTPS IP address", "https://173.194.65.103", L"https://173.194.65.103",
513 L"173.194.65.103", L"173.194.65.103"},
514 {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
515 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
516 L"http://[fe80::202:b3ff:fe1e:8329]"},
517 {"HTTPs IPv6 address", "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
518 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
519 L"[fe80::202:b3ff:fe1e:8329]"},
520 {"HTTP IPv6 address with port",
521 "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:80/",
522 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
523 L"http://[fe80::202:b3ff:fe1e:8329]"},
524 {"HTTPs IPv6 address with port",
525 "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:443/",
526 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
527 L"[fe80::202:b3ff:fe1e:8329]"},
528 {"HTTPS IP address, non-default port", "https://173.194.65.103:8443",
529 L"https://173.194.65.103:8443", L"173.194.65.103:8443",
530 L"173.194.65.103:8443"},
531 {"Invalid host 1", "https://www.cyber../wow.php", L"https://www.cyber..",
532 L"www.cyber..", L"www.cyber.."},
533 {"Invalid host 2", "https://www...cyber/wow.php", L"https://www...cyber",
534 L"www...cyber", L"www...cyber"},
535 {"Invalid port 3", "https://173.194.65.103:/hello.aspx",
536 L"https://173.194.65.103", L"173.194.65.103", L"173.194.65.103"},
537 {"Trailing dot in DNS name", "https://www.example.com./get/goat",
538 L"https://www.example.com.", L"www.example.com.", L"www.example.com."}};
539
miguelgdbcfb122015-07-23 10:45:11540TEST(TextEliderTest, FormatUrlForSecurityDisplay) {
juncaicb63cac2016-05-13 00:41:59541 for (size_t i = 0; i < arraysize(common_tests); ++i) {
542 base::string16 formatted =
543 url_formatter::FormatUrlForSecurityDisplay(GURL(common_tests[i].input));
544 EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted)
545 << common_tests[i].description;
546
547 base::string16 formatted_omit_web_scheme =
548 url_formatter::FormatUrlForSecurityDisplay(
549 GURL(common_tests[i].input),
550 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
551 EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme),
552 formatted_omit_web_scheme)
553 << common_tests[i].description;
554
555 base::string16 formatted_omit_cryptographic_scheme =
556 url_formatter::FormatUrlForSecurityDisplay(
557 GURL(common_tests[i].input),
558 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
559 EXPECT_EQ(
560 base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme),
561 formatted_omit_cryptographic_scheme)
562 << common_tests[i].description;
563 }
miguelgdbcfb122015-07-23 10:45:11564
565 const OriginTestData tests[] = {
miguelgdbcfb122015-07-23 10:45:11566 {"File URI", "file:///usr/example/file.html",
benwells2337b8102016-04-20 01:53:53567 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
568 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11569 {"File URI with hostname", "file://localhost/usr/example/file.html",
benwells2337b8102016-04-20 01:53:53570 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
571 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11572 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls",
palmer153af982015-09-15 02:04:19573 L"file:///CONTOSO/accounting/money.xls",
benwells2337b8102016-04-20 01:53:53574 L"file:///CONTOSO/accounting/money.xls",
miguelgdbcfb122015-07-23 10:45:11575 L"file:///CONTOSO/accounting/money.xls"},
576 {"UNC File URI 2",
577 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
palmer153af982015-09-15 02:04:19578 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
benwells2337b8102016-04-20 01:53:53579 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
miguelgdbcfb122015-07-23 10:45:11580 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html"},
benwells2337b8102016-04-20 01:53:53581 {"Invalid IPv6 address", "https://[2001:db8:0:1]/",
582 L"https://[2001:db8:0:1]", L"https://[2001:db8:0:1]",
583 L"https://[2001:db8:0:1]"},
miguelgdbcfb122015-07-23 10:45:11584 {"HTTP filesystem: URL with path",
585 "filesystem:http://www.google.com/temporary/test.html",
benwells2337b8102016-04-20 01:53:53586 L"filesystem:http://www.google.com", L"filesystem:http://www.google.com",
miguelgdbcfb122015-07-23 10:45:11587 L"filesystem:http://www.google.com"},
588 {"File filesystem: URL with path",
juncaicb63cac2016-05-13 00:41:59589 "filesystem:file://localhost/temporary/stuff/"
590 "test.html?z=fun&goat=billy",
palmer153af982015-09-15 02:04:19591 L"filesystem:file:///temporary/stuff/test.html",
benwells2337b8102016-04-20 01:53:53592 L"filesystem:file:///temporary/stuff/test.html",
miguelgdbcfb122015-07-23 10:45:11593 L"filesystem:file:///temporary/stuff/test.html"},
594 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53595 L"twelve://www.cyber.org/wow.php", L"twelve://www.cyber.org/wow.php",
596 L"twelve://www.cyber.org/wow.php"},
miguelgdbcfb122015-07-23 10:45:11597 {"Invalid scheme 2", "://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53598 L"://www.cyber.org/wow.php", L"://www.cyber.org/wow.php",
599 L"://www.cyber.org/wow.php"},
miguelgdbcfb122015-07-23 10:45:11600 {"Invalid port 1", "https://173.194.65.103:000",
benwells2337b8102016-04-20 01:53:53601 L"https://173.194.65.103:0", L"173.194.65.103:0", L"173.194.65.103:0"},
miguelgdbcfb122015-07-23 10:45:11602 {"Invalid port 2", "https://173.194.65.103:gruffle",
benwells2337b8102016-04-20 01:53:53603 L"https://173.194.65.103:gruffle", L"https://173.194.65.103:gruffle",
604 L"https://173.194.65.103:gruffle"},
miguelgdbcfb122015-07-23 10:45:11605 {"Blob URL",
nick1b17dc32016-04-15 21:34:04606 "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
607 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19608 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
nick1b17dc32016-04-15 21:34:04609 L"blob:http://www.html5rocks.com/"
benwells2337b8102016-04-20 01:53:53610 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
611 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19612 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}};
miguelgdbcfb122015-07-23 10:45:11613
miguelgdbcfb122015-07-23 10:45:11614 for (size_t i = 0; i < arraysize(tests); ++i) {
jshin1fb76462016-04-05 22:13:03615 base::string16 formatted =
616 url_formatter::FormatUrlForSecurityDisplay(GURL(tests[i].input));
miguelgdbcfb122015-07-23 10:45:11617 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
618 << tests[i].description;
palmer153af982015-09-15 02:04:19619
benwells2337b8102016-04-20 01:53:53620 base::string16 formatted_omit_web_scheme =
621 url_formatter::FormatUrlForSecurityDisplay(
622 GURL(tests[i].input),
623 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
624 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme),
625 formatted_omit_web_scheme)
626 << tests[i].description;
627
628 base::string16 formatted_omit_cryptographic_scheme =
629 url_formatter::FormatUrlForSecurityDisplay(
630 GURL(tests[i].input),
631 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
632 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme),
633 formatted_omit_cryptographic_scheme)
palmer153af982015-09-15 02:04:19634 << tests[i].description;
miguelgdbcfb122015-07-23 10:45:11635 }
636
juncaicb63cac2016-05-13 00:41:59637 base::string16 formatted = url_formatter::FormatUrlForSecurityDisplay(GURL());
miguelgdbcfb122015-07-23 10:45:11638 EXPECT_EQ(base::string16(), formatted)
639 << "Explicitly test the 0-argument GURL constructor";
palmer153af982015-09-15 02:04:19640
641 base::string16 formatted_omit_scheme =
benwells2337b8102016-04-20 01:53:53642 url_formatter::FormatUrlForSecurityDisplay(
643 GURL(), url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
644 EXPECT_EQ(base::string16(), formatted_omit_scheme)
645 << "Explicitly test the 0-argument GURL constructor";
646
647 formatted_omit_scheme = url_formatter::FormatUrlForSecurityDisplay(
648 GURL(), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
palmer153af982015-09-15 02:04:19649 EXPECT_EQ(base::string16(), formatted_omit_scheme)
650 << "Explicitly test the 0-argument GURL constructor";
miguelgdbcfb122015-07-23 10:45:11651}
652
juncaicb63cac2016-05-13 00:41:59653TEST(TextEliderTest, FormatOriginForSecurityDisplay) {
654 for (size_t i = 0; i < arraysize(common_tests); ++i) {
655 base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay(
Daniel Cheng88186bd52017-10-20 08:14:46656 url::Origin::Create(GURL(common_tests[i].input)));
juncaicb63cac2016-05-13 00:41:59657 EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted)
658 << common_tests[i].description;
659
660 base::string16 formatted_omit_web_scheme =
661 url_formatter::FormatOriginForSecurityDisplay(
Daniel Cheng88186bd52017-10-20 08:14:46662 url::Origin::Create(GURL(common_tests[i].input)),
juncaicb63cac2016-05-13 00:41:59663 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
664 EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme),
665 formatted_omit_web_scheme)
666 << common_tests[i].description;
667
668 base::string16 formatted_omit_cryptographic_scheme =
669 url_formatter::FormatOriginForSecurityDisplay(
Daniel Cheng88186bd52017-10-20 08:14:46670 url::Origin::Create(GURL(common_tests[i].input)),
juncaicb63cac2016-05-13 00:41:59671 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
672 EXPECT_EQ(
673 base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme),
674 formatted_omit_cryptographic_scheme)
675 << common_tests[i].description;
676 }
677
678 const OriginTestData tests[] = {
679 {"File URI", "file:///usr/example/file.html", L"file://", L"file://",
680 L"file://"},
681 {"File URI with hostname", "file://localhost/usr/example/file.html",
682 L"file://localhost", L"file://localhost", L"file://localhost"},
683 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls", L"file://",
684 L"file://", L"file://"},
685 {"UNC File URI 2",
686 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
687 L"file://", L"file://", L"file://"},
688 {"Invalid IPv6 address", "https://[2001:db8:0:1]/", L"", L"", L""},
689 {"HTTP filesystem: URL with path",
690 "filesystem:http://www.google.com/temporary/test.html",
691 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
692 {"File filesystem: URL with path",
693 "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy",
694 L"file://", L"file://", L"file://"},
695 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php", L"", L"", L""},
696 {"Invalid scheme 2", "://www.cyber.org/wow.php", L"", L"", L""},
697 {"Invalid port 1", "https://173.194.65.103:000", L"", L"", L""},
698 {"Invalid port 2", "https://173.194.65.103:gruffle", L"", L"", L""},
699 {"Blob URL",
700 "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
701 L"http://www.html5rocks.com", L"www.html5rocks.com",
702 L"http://www.html5rocks.com"}};
703
704 for (size_t i = 0; i < arraysize(tests); ++i) {
705 base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay(
Daniel Cheng88186bd52017-10-20 08:14:46706 url::Origin::Create(GURL(tests[i].input)));
juncaicb63cac2016-05-13 00:41:59707 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
708 << tests[i].description;
709
710 base::string16 formatted_omit_web_scheme =
711 url_formatter::FormatOriginForSecurityDisplay(
Daniel Cheng88186bd52017-10-20 08:14:46712 url::Origin::Create(GURL(tests[i].input)),
juncaicb63cac2016-05-13 00:41:59713 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
714 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme),
715 formatted_omit_web_scheme)
716 << tests[i].description;
717
718 base::string16 formatted_omit_cryptographic_scheme =
719 url_formatter::FormatOriginForSecurityDisplay(
Daniel Cheng88186bd52017-10-20 08:14:46720 url::Origin::Create(GURL(tests[i].input)),
juncaicb63cac2016-05-13 00:41:59721 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
722 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme),
723 formatted_omit_cryptographic_scheme)
724 << tests[i].description;
725 }
726
Daniel Cheng88186bd52017-10-20 08:14:46727 base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay(
728 url::Origin::Create(GURL()));
juncaicb63cac2016-05-13 00:41:59729 EXPECT_EQ(base::string16(), formatted)
730 << "Explicitly test the url::Origin which takes an empty, invalid URL";
731
732 base::string16 formatted_omit_scheme =
733 url_formatter::FormatOriginForSecurityDisplay(
Daniel Cheng88186bd52017-10-20 08:14:46734 url::Origin::Create(GURL()),
juncaicb63cac2016-05-13 00:41:59735 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
736 EXPECT_EQ(base::string16(), formatted_omit_scheme)
737 << "Explicitly test the url::Origin which takes an empty, invalid URL";
738
739 formatted_omit_scheme = url_formatter::FormatOriginForSecurityDisplay(
Daniel Cheng88186bd52017-10-20 08:14:46740 url::Origin::Create(GURL()),
741 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
juncaicb63cac2016-05-13 00:41:59742 EXPECT_EQ(base::string16(), formatted_omit_scheme)
743 << "Explicitly test the url::Origin which takes an empty, invalid URL";
744}
745
Christopher Grant6871e1212017-10-09 15:47:50746TEST(TextEliderTest, TestSimpleElisionMethod) {
747 const std::string kEllipsisStr(gfx::kEllipsis);
748 const std::vector<ProgressiveTestcase> testcases = {
749 {"https://www.abc.com/def/",
750 {
751 /* clang-format off */
752 {"https://www.abc.com/def/"},
753 {"https://www.abc.com/d" + kEllipsisStr},
754 {"https://www.abc.com/" + kEllipsisStr},
755 {"www.abc.com/def/"},
756 {"www.abc.com/d" + kEllipsisStr},
757 {"www.abc.com/" + kEllipsisStr},
758 {kEllipsisStr + "ww.abc.com/" + kEllipsisStr},
759 {kEllipsisStr + "w.abc.com/" + kEllipsisStr},
760 {kEllipsisStr + ".abc.com/" + kEllipsisStr},
761 {kEllipsisStr + "abc.com/" + kEllipsisStr},
762 {kEllipsisStr + "bc.com/" + kEllipsisStr},
763 {kEllipsisStr + "c.com/" + kEllipsisStr},
764 {kEllipsisStr + ".com/" + kEllipsisStr},
765 {kEllipsisStr + "com/" + kEllipsisStr},
766 {kEllipsisStr + "om/" + kEllipsisStr},
767 {kEllipsisStr + "m/" + kEllipsisStr},
768 {kEllipsisStr + "/" + kEllipsisStr},
769 {kEllipsisStr},
770 {""},
771 /* clang-format on */
772 }},
773 {"file://fs/file",
774 {
775 /* clang-format off */
776 "file://fs/file",
777 "file://fs/fi" + kEllipsisStr,
778 "file://fs/f" + kEllipsisStr,
779 "file://fs/" + kEllipsisStr,
780 "file://fs" + kEllipsisStr,
781 "file://f" + kEllipsisStr,
782 "file://" + kEllipsisStr,
783 "file:/" + kEllipsisStr,
784 "file:" + kEllipsisStr,
785 "file" + kEllipsisStr,
786 "fil" + kEllipsisStr,
787 "fi" + kEllipsisStr,
788 "f" + kEllipsisStr,
789 kEllipsisStr,
790 "",
791 /* clang-format on */
792 }},
793 };
794 RunProgressiveElisionTest(testcases, kMethodSimple);
795}
796
797// Verify that the secure elision method returns URL component data that
798// correctly represents the elided URL.
799void RunElisionParsingTest(const std::vector<ParsingTestcase>& testcases) {
800 const gfx::FontList font_list;
801 for (const auto& testcase : testcases) {
802 SCOPED_TRACE(testcase.input + " to " + testcase.output);
803
804 const GURL url(testcase.input);
805 const float available_width =
806 gfx::GetStringWidthF(base::UTF8ToUTF16(testcase.output), font_list);
807
808 url::Parsed parsed;
809 auto elided =
Christopher Grantbd541e52017-10-13 20:59:41810 FormatAndElideUrlSimple(url, font_list, available_width, &parsed);
Christopher Grant6871e1212017-10-09 15:47:50811 EXPECT_EQ(base::UTF8ToUTF16(testcase.output), elided);
812
813 // Build an expected Parsed struct from the sparse test expectations.
814 url::Parsed expected;
815 for (const auto& expectation : testcase.components) {
816 url::Component* component = GetComponent(&expected, expectation.type);
817 component->begin = expectation.begin;
818 component->len = expectation.len;
819 }
820
821 const std::vector<url::Parsed::ComponentType> kComponents = {
822 url::Parsed::SCHEME, url::Parsed::USERNAME, url::Parsed::PASSWORD,
823 url::Parsed::HOST, url::Parsed::PORT, url::Parsed::PATH,
824 url::Parsed::QUERY, url::Parsed::REF,
825 };
826 for (const auto& type : kComponents) {
827 EXPECT_EQ(GetComponent(&expected, type)->begin,
828 GetComponent(&parsed, type)->begin)
829 << " in component " << type;
830 EXPECT_EQ(GetComponent(&expected, type)->len,
831 GetComponent(&parsed, type)->len)
832 << " in component " << type;
833 }
834 }
835}
836
837// Verify that during elision, the parsed URL components are properly modified.
838TEST(TextEliderTest, TestElisionParsingAdjustments) {
839 const std::string kEllipsisStr(gfx::kEllipsis);
840 const std::vector<ParsingTestcase> testcases = {
841 // HTTPS with path.
842 {"https://www.google.com/intl/en/ads/",
843 "https://www.google.com/intl/en/ads/",
844 {{url::Parsed::ComponentType::SCHEME, 0, 5},
845 {url::Parsed::ComponentType::HOST, 8, 14},
846 {url::Parsed::ComponentType::PATH, 22, 13}}},
847 {"https://www.google.com/intl/en/ads/",
848 "https://www.google.com/intl/en/a" + kEllipsisStr,
849 {{url::Parsed::ComponentType::SCHEME, 0, 5},
850 {url::Parsed::ComponentType::HOST, 8, 14},
851 {url::Parsed::ComponentType::PATH, 22, 11}}},
852 {"https://www.google.com/intl/en/ads/",
853 "https://www.google.com/" + kEllipsisStr,
854 {{url::Parsed::ComponentType::SCHEME, 0, 5},
855 {url::Parsed::ComponentType::HOST, 8, 14},
856 {url::Parsed::ComponentType::PATH, 22, 2}}},
857 {"https://www.google.com/intl/en/ads/",
858 kEllipsisStr + "google.com/" + kEllipsisStr,
859 {{url::Parsed::ComponentType::HOST, 0, 11},
860 {url::Parsed::ComponentType::PATH, 11, 2}}},
861 {"https://www.google.com/intl/en/ads/",
862 kEllipsisStr,
863 {{url::Parsed::ComponentType::PATH, 0, 1}}},
864 // HTTPS with no path.
865 {"https://www.google.com/",
866 "www.google.com",
867 {{url::Parsed::ComponentType::HOST, 0, 14}}},
868 {"https://www.google.com/",
869 kEllipsisStr,
870 {{url::Parsed::ComponentType::HOST, 0, 1}}},
871 // HTTP with no path.
872 {"http://www.google.com/",
873 "www.google.com",
874 {{url::Parsed::ComponentType::HOST, 0, 14}}},
875 // File URLs.
876 {"file:///C:/path1/path2",
877 "file:///C:/path1/" + kEllipsisStr,
878 {{url::Parsed::ComponentType::SCHEME, 0, 4},
879 {url::Parsed::ComponentType::PATH, 7, 11}}},
880 {"file:///C:/path1/path2",
881 "fi" + kEllipsisStr,
882 {{url::Parsed::ComponentType::SCHEME, 0, 3}}},
883 {"file:///C:/path1/path2",
884 kEllipsisStr,
885 {{url::Parsed::ComponentType::SCHEME, 0, 1}}},
886 // RTL URL.
887 {"http://127.0.0.1/ا/http://attack.com‬",
888 kEllipsisStr + "7.0.0.1/" + kEllipsisStr,
889 {{url::Parsed::ComponentType::HOST, 0, 8},
890 {url::Parsed::ComponentType::PATH, 8, 2}}},
891 };
892
893 RunElisionParsingTest(testcases);
894}
895
miguelgdbcfb122015-07-23 10:45:11896} // namespace