blob: fc7148cbdeb0750f0a4a6371d06f512679e0f321 [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
52base::string16 Elide(const GURL& url,
53 const gfx::FontList& font_list,
54 float available_width,
55 ElisionMethod method) {
56 switch (method) {
57 case kMethodSimple: {
58 url::Parsed parsed;
59 return url_formatter::ElideUrlSimple(url, font_list, available_width,
60 &parsed);
61 }
62#if !defined(OS_ANDROID)
63 case kMethodOriginal:
64 return url_formatter::ElideUrl(url, font_list, available_width);
65#endif
66 default:
67 NOTREACHED();
68 return base::string16();
69 }
70}
71
72url::Component* GetComponent(url::Parsed* parsed,
73 url::Parsed::ComponentType type) {
74 switch (type) {
75 case url::Parsed::SCHEME:
76 return &parsed->scheme;
77 case url::Parsed::USERNAME:
78 return &parsed->username;
79 case url::Parsed::PASSWORD:
80 return &parsed->password;
81 case url::Parsed::HOST:
82 return &parsed->host;
83 case url::Parsed::PORT:
84 return &parsed->port;
85 case url::Parsed::PATH:
86 return &parsed->path;
87 case url::Parsed::QUERY:
88 return &parsed->query;
89 case url::Parsed::REF:
90 return &parsed->ref;
91 default:
92 NOTREACHED();
93 return nullptr;
94 }
95}
96
Christopher Grant5ed8d112017-09-12 20:00:0697// Verify that one or more URLs passes through an explicit sequence of elided
98// strings as available space progressively decreases. This helps ensure that
99// transitional corner cases are handled properly. To be tolerant of
100// character-width variation across platforms, the test allows a limited number
101// of expected strings to be skipped mid-run. The first and last expected
102// strings must be matched. If the algorithm produces a string that isn't in the
103// expected string list, the test fill fail. Example test expectations:
104//
105// google.com/intl/en/.../ads/ <-- Must match.
106// google.com/intl/.../ads/
107// google.com/.../ads/
108// google.com/intl... <- Elider can skip this, in case the 'l' does not fit.
109// google.com/int...
110// google.com/in... <- Must match.
111//
112void RunProgressiveElisionTest(
Christopher Grant6871e1212017-10-09 15:47:50113 const std::vector<ProgressiveTestcase>& testcases,
114 ElisionMethod method) {
Christopher Grant5ed8d112017-09-12 20:00:06115 const gfx::FontList font_list;
116 for (const auto& testcase : testcases) {
117 SCOPED_TRACE("Eliding " + testcase.input);
118 const GURL url(testcase.input);
119
120 // Occasionally, a parsed URL can grow in length before elision, such as
121 // when parsing a Windows file path with missing slashes.
122 ASSERT_FALSE(testcase.output.empty());
123 float width = std::max(
124 gfx::GetStringWidthF(base::UTF8ToUTF16(testcase.input), font_list),
125 gfx::GetStringWidthF(base::UTF8ToUTF16(testcase.output.front()),
126 font_list));
127
128 // Ideally, this test would iterate through all available field widths on a
129 // per-pixel basis, but this is slow. Instead, compute the next input field
130 // width as slightly less than the previous elided string. This approach
131 // misses coverage in cases where a smaller available width generates a
132 // longer string than some other larger available width, but the tradeoff
133 // feels acceptable.
134 int mismatches = 0;
135 const int kMaxConsecutiveMismatches = 3;
136 for (size_t i = 0; i < testcase.output.size(); i++) {
137 const auto& expected = testcase.output[i];
138 base::string16 expected_utf16 = base::UTF8ToUTF16(expected);
Christopher Grant6871e1212017-10-09 15:47:50139 base::string16 elided = Elide(url, font_list, width, method);
Christopher Grant5ed8d112017-09-12 20:00:06140 if (expected_utf16 != elided) {
141 if (i > 0 && i < testcase.output.size() - 1 &&
142 mismatches < kMaxConsecutiveMismatches) {
143 mismatches++;
144 continue;
145 }
146 EXPECT_EQ(expected_utf16, elided);
147 break;
148 }
149 mismatches = 0;
150 float new_width = gfx::GetStringWidthF(elided, font_list);
151 // Elision rounds fractional available widths up.
152 EXPECT_LE(new_width, std::ceil(width)) << " at " << elided;
153 width = new_width - 1.0f;
154 }
155 }
156}
157
Christopher Grant6871e1212017-10-09 15:47:50158#if !defined(OS_ANDROID)
159
160void RunElisionTest(const std::vector<Testcase>& testcases) {
161 const gfx::FontList font_list;
162 for (const auto& testcase : testcases) {
163 SCOPED_TRACE("Eliding " + testcase.input);
164 const GURL url(testcase.input);
165 const float available_width =
166 gfx::GetStringWidthF(base::UTF8ToUTF16(testcase.output), font_list);
167 EXPECT_EQ(base::UTF8ToUTF16(testcase.output),
168 url_formatter::ElideUrl(url, font_list, available_width));
169 }
170}
171
miguelgdbcfb122015-07-23 10:45:11172// Test eliding of commonplace URLs.
173TEST(TextEliderTest, TestGeneralEliding) {
rsleevi24f64dc22015-08-07 21:39:21174 const std::string kEllipsisStr(gfx::kEllipsis);
Christopher Grant5ed8d112017-09-12 20:00:06175 const std::vector<ProgressiveTestcase> progressive_testcases = {
176 // Elide a non-www URL (www URLs are handled differently). In this first
177 // case, elide down to nothing to test the terminal cases.
178 {"http://xyz.google.com/foo?bar",
179 {
180 /* clang-format off */
181 "xyz.google.com/foo?bar",
182 "xyz.google.com/foo?b" + kEllipsisStr,
183 "xyz.google.com/foo?" + kEllipsisStr,
184 "xyz.google.com/foo" + kEllipsisStr,
185 "xyz.google.com/fo" + kEllipsisStr,
186 "xyz.google.com/f" + kEllipsisStr,
187 kEllipsisStr + "google.com/foo" + kEllipsisStr,
188 kEllipsisStr + "google.com/fo" + kEllipsisStr,
189 kEllipsisStr + "google.com/f" + kEllipsisStr,
190 kEllipsisStr + "google.com/" + kEllipsisStr,
191 kEllipsisStr + "google.com" + kEllipsisStr,
192 kEllipsisStr + "google.co" + kEllipsisStr,
193 kEllipsisStr + "google.c" + kEllipsisStr,
194 kEllipsisStr + "google." + kEllipsisStr,
195 kEllipsisStr + "google" + kEllipsisStr,
196 kEllipsisStr + "googl" + kEllipsisStr,
197 kEllipsisStr + "goog" + kEllipsisStr,
198 kEllipsisStr + "goo" + kEllipsisStr,
199 kEllipsisStr + "go" + kEllipsisStr,
200 kEllipsisStr + "g" + kEllipsisStr,
201 kEllipsisStr + kEllipsisStr,
202 kEllipsisStr,
203 ""
204 /* clang-format on */
205 }},
206 // The trailing directory name is preserved
miguelgdbcfb122015-07-23 10:45:11207 {"http://www.google.com/intl/en/ads/",
Christopher Grant5ed8d112017-09-12 20:00:06208 {
209 /* clang-format off */
210 "www.google.com/intl/en/ads/",
211 "google.com/intl/en/ads/",
212 "google.com/intl/" + kEllipsisStr + "/ads/",
213 "google.com/" + kEllipsisStr + "/ads/",
214 "google.com/" + kEllipsisStr + "/ad" + kEllipsisStr,
215 "google.com/" + kEllipsisStr + "/a" + kEllipsisStr,
216 "google.com/intl/e" + kEllipsisStr,
217 "google.com/intl/" + kEllipsisStr,
218 "google.com/intl" + kEllipsisStr,
219 "google.com/int" + kEllipsisStr,
220 "google.com/in" + kEllipsisStr,
221 "google.com/i" + kEllipsisStr,
222 "google.com/" + kEllipsisStr,
223 "google.com" + kEllipsisStr,
224 "google.co" + kEllipsisStr,
225 "google.c" + kEllipsisStr,
226 "google." + kEllipsisStr,
227 "google" + kEllipsisStr,
228 "googl" + kEllipsisStr,
229 /* clang-format on */
230 }},
231 // Subdomain is completely elided if the last path element doesn't fit.
miguelgdbcfb122015-07-23 10:45:11232 {"https://subdomain.foo.com/bar/filename.html",
Christopher Grant5ed8d112017-09-12 20:00:06233 {
234 /* clang-format off */
235 "https://subdomain.foo.com/bar/filename.html",
236 "subdomain.foo.com/bar/filename.html",
237 "subdomain.foo.com/" + kEllipsisStr + "/filename.html",
238 kEllipsisStr + "foo.com/bar/filename.html",
239 kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html",
240 /* clang-format on */
241 }},
242 // Path eliding works when a query is present.
243 {"http://www.g.com/subdir/ads/?query",
244 {
245 /* clang-format off */
246 "www.g.com/subdir/ads/?query",
247 "www.g.com/subdir/ads/?que" + kEllipsisStr,
248 "www.g.com/subdir/ads/?qu" + kEllipsisStr,
249 "www.g.com/subdir/ads/?q" + kEllipsisStr,
250 "www.g.com/subdir/ads/?" + kEllipsisStr,
251 "www.g.com/subdir/ads/" + kEllipsisStr,
252 "www.g.com/subdir/ads" + kEllipsisStr,
253 "www.g.com/subdir/ad" + kEllipsisStr,
254 /* clang-format on */
255 }},
miguelgdbcfb122015-07-23 10:45:11256 };
Christopher Grant6871e1212017-10-09 15:47:50257 RunProgressiveElisionTest(progressive_testcases, kMethodOriginal);
miguelgdbcfb122015-07-23 10:45:11258}
259
260// When there is very little space available, the elision code will shorten
261// both path AND file name to an ellipsis - ".../...". To avoid this result,
262// there is a hack in place that simply treats them as one string in this
263// case.
264TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) {
rsleevi24f64dc22015-08-07 21:39:21265 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11266
267 // Very little space, would cause double ellipsis.
268 gfx::FontList font_list;
269 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html");
rsleevi24f64dc22015-08-07 21:39:21270 float available_width = gfx::GetStringWidthF(
271 base::UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr),
miguelgdbcfb122015-07-23 10:45:11272 font_list);
273
274 // Create the expected string, after elision. Depending on font size, the
275 // directory might become /dir... or /di... or/d... - it never should be
276 // shorter than that. (If it is, the font considers d... to be longer
277 // than .../... - that should never happen).
rsleevi24f64dc22015-08-07 21:39:21278 ASSERT_GT(
279 gfx::GetStringWidthF(base::UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr),
280 font_list),
281 gfx::GetStringWidthF(base::UTF8ToUTF16("d" + kEllipsisStr), font_list));
miguelgdbcfb122015-07-23 10:45:11282 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc");
rsleevi24f64dc22015-08-07 21:39:21283 base::string16 expected = url_formatter::ElideUrl(
jshin1fb76462016-04-05 22:13:03284 long_url, font_list, available_width);
miguelgdbcfb122015-07-23 10:45:11285 // Ensure that the expected result still contains part of the directory name.
286 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length());
jshin1fb76462016-04-05 22:13:03287 EXPECT_EQ(expected, url_formatter::ElideUrl(url, font_list, available_width));
miguelgdbcfb122015-07-23 10:45:11288
Matt Giuca49fcda172017-08-29 07:16:12289 // Regression test for https://crbug.com/756717. An empty path, eliding to a
290 // width in between the full domain ("www.angelfire.lycos.com") and a bit
291 // longer than the ETLD+1 ("…lycos.com…/…UV"). This previously crashed due to
292 // the path being empty.
293 url = GURL("http://www.angelfire.lycos.com/");
294 available_width = gfx::GetStringWidthF(
295 base::UTF8ToUTF16(kEllipsisStr + "angelfire.lycos.com"), font_list);
296 EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + "lycos.com"),
297 url_formatter::ElideUrl(url, font_list, available_width));
298
miguelgdbcfb122015-07-23 10:45:11299 // More space available - elide directories, partially elide filename.
Christopher Grant5ed8d112017-09-12 20:00:06300 const std::vector<Testcase> testcases = {
miguelgdbcfb122015-07-23 10:45:11301 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html",
302 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr},
303 };
Christopher Grant5ed8d112017-09-12 20:00:06304 RunElisionTest(testcases);
miguelgdbcfb122015-07-23 10:45:11305}
306
307// Test eliding of empty strings, URLs with ports, passwords, queries, etc.
Christopher Grant5ed8d112017-09-12 20:00:06308TEST(TextEliderTest, TestElisionSpecialCases) {
kulshince612eb2016-06-29 18:46:51309#if defined(OS_WIN)
310 // Needed to bypass DCHECK in GetFallbackFont.
311 base::MessageLoopForUI message_loop;
312#endif
rsleevi24f64dc22015-08-07 21:39:21313 const std::string kEllipsisStr(gfx::kEllipsis);
Christopher Grant5ed8d112017-09-12 20:00:06314 const std::vector<Testcase> testcases = {
Matt Giuca49fcda172017-08-29 07:16:12315 // URL with "www" subdomain (gets removed specially).
316 {"http://www.google.com/foo?bar", "www.google.com/foo?bar"},
317 {"http://www.google.com/foo?bar", "google.com/foo?bar"},
318
Matt Giuca51f68cc2017-07-07 01:34:57319 // URL with no path.
Matt Giuca49fcda172017-08-29 07:16:12320 {"http://xyz.google.com", kEllipsisStr + "google.com"},
321 {"https://xyz.google.com", kEllipsisStr + "google.com"},
Matt Giuca51f68cc2017-07-07 01:34:57322
miguelgdbcfb122015-07-23 10:45:11323 {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"},
324 {"", ""},
325 {"http://foo.bar..example.com...hello/test/filename.html",
326 "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"},
327 {"http://foo.bar../", "foo.bar.."},
328 {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"},
329 {"http://me:[email protected]:99/foo?bar#baz",
330 "secrethost.com:99/foo?bar#baz"},
331 {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"},
332 {"mailto:[email protected]", "mailto:[email protected]"},
333 {"javascript:click(0)", "javascript:click(0)"},
334 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
335 "chess.eecs.berkeley.edu:4430/login/arbitfilename"},
336 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
337 kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"},
338
339 // Unescaping.
340 {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
Mike Westf8f6ed52017-10-09 20:58:50341 "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#" +
342 kEllipsisStr},
miguelgdbcfb122015-07-23 10:45:11343
344 // Invalid unescaping for path. The ref will always be valid UTF-8. We
Christopher Grant5ed8d112017-09-12 20:00:06345 // don't bother to do too many edge cases, since these are handled by the
346 // escaper unittest.
miguelgdbcfb122015-07-23 10:45:11347 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
Mike Westf8f6ed52017-10-09 20:58:50348 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#" + kEllipsisStr},
miguelgdbcfb122015-07-23 10:45:11349 };
350
Christopher Grant5ed8d112017-09-12 20:00:06351 RunElisionTest(testcases);
miguelgdbcfb122015-07-23 10:45:11352}
353
354// Test eliding of file: URLs.
355TEST(TextEliderTest, TestFileURLEliding) {
rsleevi24f64dc22015-08-07 21:39:21356 const std::string kEllipsisStr(gfx::kEllipsis);
Christopher Grant5ed8d112017-09-12 20:00:06357 const std::vector<ProgressiveTestcase> progressive_testcases = {
miguelgdbcfb122015-07-23 10:45:11358 {"file:///C:/path1/path2/path3/filename",
Christopher Grant5ed8d112017-09-12 20:00:06359 {
360 /* clang-format off */
361 "file:///C:/path1/path2/path3/filename",
362 "C:/path1/path2/path3/filename",
363 "C:/path1/path2/" + kEllipsisStr + "/filename",
364 /* clang-format on */
365 }},
miguelgdbcfb122015-07-23 10:45:11366// GURL parses "file:///C:path" differently on windows than it does on posix.
367#if defined(OS_WIN)
368 {"file:///C:path1/path2/path3/filename",
Christopher Grant5ed8d112017-09-12 20:00:06369 {
370 /* clang-format off */
371 "file:///C:/path1/path2/path3/filename",
372 "C:/path1/path2/path3/filename",
373 "C:/path1/path2/" + kEllipsisStr + "/filename",
374 "C:/path1/" + kEllipsisStr + "/filename",
375 "C:/" + kEllipsisStr + "/filename",
376 /* clang-format on */
377 }},
miguelgdbcfb122015-07-23 10:45:11378#endif // defined(OS_WIN)
Christopher Grant5ed8d112017-09-12 20:00:06379 {"file://filer/foo/bar/file",
380 {
381 /* clang-format off */
382 "file://filer/foo/bar/file",
383 "filer/foo/bar/file",
384 "filer/foo/" + kEllipsisStr + "/file",
385 "filer/" + kEllipsisStr + "/file",
386 "filer/foo" + kEllipsisStr,
387 "filer/fo" + kEllipsisStr,
388 "filer/f" + kEllipsisStr,
389 "filer/" + kEllipsisStr,
390 "filer" + kEllipsisStr,
391 "file" + kEllipsisStr,
392 /* clang-format on */
393 }},
miguelgdbcfb122015-07-23 10:45:11394 };
395
Christopher Grant6871e1212017-10-09 15:47:50396 RunProgressiveElisionTest(progressive_testcases, kMethodOriginal);
Christopher Grant5ed8d112017-09-12 20:00:06397
398 const std::vector<Testcase> testcases = {
399 // Eliding file URLs with nothing after the ':' shouldn't crash.
400 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:", "aaa" + kEllipsisStr},
401 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:/", "aaa" + kEllipsisStr},
402 };
403 RunElisionTest(testcases);
miguelgdbcfb122015-07-23 10:45:11404}
405
406TEST(TextEliderTest, TestHostEliding) {
rsleevi24f64dc22015-08-07 21:39:21407 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11408 Testcase testcases[] = {
pkl2affa912017-02-22 15:23:20409 {"http://google.com", "google.com"},
410 {"http://reallyreallyreallylongdomainname.com",
411 "reallyreallyreallylongdomainname.com"},
412 {"http://foo", "foo"},
413 {"http://foo.bar", "foo.bar"},
pkl2affa912017-02-22 15:23:20414 {"http://subdomain.google.com", kEllipsisStr + ".google.com"},
415 {"http://a.b.c.d.e.f.com", kEllipsisStr + "f.com"},
416 {"http://subdomain.foo.bar", kEllipsisStr + "in.foo.bar"},
417 {"http://subdomain.reallylongdomainname.com",
418 kEllipsisStr + "ain.reallylongdomainname.com"},
419 {"http://a.b.c.d.e.f.com", kEllipsisStr + ".e.f.com"},
420 // IDN - Greek alpha.beta.gamma.delta.epsilon.zeta.com
421 {"http://xn--mxa.xn--nxa.xn--oxa.xn--pxa.xn--qxa.xn--rxa.com",
422 kEllipsisStr + ".\xCE\xB5.\xCE\xB6.com"},
miguelgdbcfb122015-07-23 10:45:11423 };
424
425 for (size_t i = 0; i < arraysize(testcases); ++i) {
rsleevi24f64dc22015-08-07 21:39:21426 const float available_width = gfx::GetStringWidthF(
427 base::UTF8ToUTF16(testcases[i].output), gfx::FontList());
428 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
429 url_formatter::ElideHost(GURL(testcases[i].input),
430 gfx::FontList(), available_width));
miguelgdbcfb122015-07-23 10:45:11431 }
432
433 // Trying to elide to a really short length will still keep the full TLD+1
434 EXPECT_EQ(
435 base::ASCIIToUTF16("google.com"),
rsleevi24f64dc22015-08-07 21:39:21436 url_formatter::ElideHost(GURL("http://google.com"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11437 EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"),
rsleevi24f64dc22015-08-07 21:39:21438 url_formatter::ElideHost(GURL("http://subdomain.google.com"),
439 gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11440 EXPECT_EQ(
441 base::ASCIIToUTF16("foo.bar"),
rsleevi24f64dc22015-08-07 21:39:21442 url_formatter::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11443}
444
445#endif // !defined(OS_ANDROID)
446
juncaicb63cac2016-05-13 00:41:59447struct OriginTestData {
448 const char* const description;
449 const char* const input;
450 const wchar_t* const output;
451 const wchar_t* const output_omit_web_scheme;
452 const wchar_t* const output_omit_cryptographic_scheme;
453};
454
455// Common test data for both FormatUrlForSecurityDisplay() and
456// FormatOriginForSecurityDisplay()
457const OriginTestData common_tests[] = {
458 {"Empty URL", "", L"", L"", L""},
459 {"HTTP URL", "http://www.google.com/", L"http://www.google.com",
460 L"www.google.com", L"http://www.google.com"},
461 {"HTTPS URL", "https://www.google.com/", L"https://www.google.com",
462 L"www.google.com", L"www.google.com"},
463 {"Standard HTTP port", "http://www.google.com:80/",
464 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
465 {"Standard HTTPS port", "https://www.google.com:443/",
466 L"https://www.google.com", L"www.google.com", L"www.google.com"},
467 {"Standard HTTP port, IDN Chinese",
468 "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80",
jshin78809c4d82016-10-06 20:15:45469 L"http://\x4e2d\x56fd.icom.museum", L"\x4e2d\x56fd.icom.museum",
470 L"http://\x4e2d\x56fd.icom.museum"},
juncaicb63cac2016-05-13 00:41:59471 {"HTTP URL, IDN Hebrew (RTL)",
472 "http://"
473 "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d."
474 "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/",
475 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum",
476 L"xn--4dbklr2c8d.xn--4dbrk0ce.museum",
477 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum"},
478 {"HTTP URL with query string, IDN Arabic (RTL)",
479 "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no",
480 L"http://xn--wgbh1c.icom.museum", L"xn--wgbh1c.icom.museum",
481 L"http://xn--wgbh1c.icom.museum"},
482 {"Non-standard HTTP port", "http://www.google.com:9000/",
483 L"http://www.google.com:9000", L"www.google.com:9000",
484 L"http://www.google.com:9000"},
485 {"Non-standard HTTPS port", "https://www.google.com:9000/",
486 L"https://www.google.com:9000", L"www.google.com:9000",
487 L"www.google.com:9000"},
488 {"HTTP URL with path", "http://www.google.com/test.html",
489 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
490 {"HTTPS URL with path", "https://www.google.com/test.html",
491 L"https://www.google.com", L"www.google.com", L"www.google.com"},
492 {"Unusual secure scheme (wss)", "wss://www.google.com/",
493 L"wss://www.google.com", L"wss://www.google.com", L"www.google.com"},
494 {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/",
495 L"gopher://www.google.com", L"gopher://www.google.com",
496 L"gopher://www.google.com"},
497 {"Unlisted scheme (chrome)", "chrome://version", L"chrome://version",
498 L"chrome://version", L"chrome://version"},
499 {"HTTP IP address", "http://173.194.65.103", L"http://173.194.65.103",
500 L"173.194.65.103", L"http://173.194.65.103"},
501 {"HTTPS IP address", "https://173.194.65.103", L"https://173.194.65.103",
502 L"173.194.65.103", L"173.194.65.103"},
503 {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
504 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
505 L"http://[fe80::202:b3ff:fe1e:8329]"},
506 {"HTTPs IPv6 address", "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
507 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
508 L"[fe80::202:b3ff:fe1e:8329]"},
509 {"HTTP IPv6 address with port",
510 "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:80/",
511 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
512 L"http://[fe80::202:b3ff:fe1e:8329]"},
513 {"HTTPs IPv6 address with port",
514 "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:443/",
515 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
516 L"[fe80::202:b3ff:fe1e:8329]"},
517 {"HTTPS IP address, non-default port", "https://173.194.65.103:8443",
518 L"https://173.194.65.103:8443", L"173.194.65.103:8443",
519 L"173.194.65.103:8443"},
520 {"Invalid host 1", "https://www.cyber../wow.php", L"https://www.cyber..",
521 L"www.cyber..", L"www.cyber.."},
522 {"Invalid host 2", "https://www...cyber/wow.php", L"https://www...cyber",
523 L"www...cyber", L"www...cyber"},
524 {"Invalid port 3", "https://173.194.65.103:/hello.aspx",
525 L"https://173.194.65.103", L"173.194.65.103", L"173.194.65.103"},
526 {"Trailing dot in DNS name", "https://www.example.com./get/goat",
527 L"https://www.example.com.", L"www.example.com.", L"www.example.com."}};
528
miguelgdbcfb122015-07-23 10:45:11529TEST(TextEliderTest, FormatUrlForSecurityDisplay) {
juncaicb63cac2016-05-13 00:41:59530 for (size_t i = 0; i < arraysize(common_tests); ++i) {
531 base::string16 formatted =
532 url_formatter::FormatUrlForSecurityDisplay(GURL(common_tests[i].input));
533 EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted)
534 << common_tests[i].description;
535
536 base::string16 formatted_omit_web_scheme =
537 url_formatter::FormatUrlForSecurityDisplay(
538 GURL(common_tests[i].input),
539 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
540 EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme),
541 formatted_omit_web_scheme)
542 << common_tests[i].description;
543
544 base::string16 formatted_omit_cryptographic_scheme =
545 url_formatter::FormatUrlForSecurityDisplay(
546 GURL(common_tests[i].input),
547 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
548 EXPECT_EQ(
549 base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme),
550 formatted_omit_cryptographic_scheme)
551 << common_tests[i].description;
552 }
miguelgdbcfb122015-07-23 10:45:11553
554 const OriginTestData tests[] = {
miguelgdbcfb122015-07-23 10:45:11555 {"File URI", "file:///usr/example/file.html",
benwells2337b8102016-04-20 01:53:53556 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
557 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11558 {"File URI with hostname", "file://localhost/usr/example/file.html",
benwells2337b8102016-04-20 01:53:53559 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
560 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11561 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls",
palmer153af982015-09-15 02:04:19562 L"file:///CONTOSO/accounting/money.xls",
benwells2337b8102016-04-20 01:53:53563 L"file:///CONTOSO/accounting/money.xls",
miguelgdbcfb122015-07-23 10:45:11564 L"file:///CONTOSO/accounting/money.xls"},
565 {"UNC File URI 2",
566 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
palmer153af982015-09-15 02:04:19567 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
benwells2337b8102016-04-20 01:53:53568 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
miguelgdbcfb122015-07-23 10:45:11569 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html"},
benwells2337b8102016-04-20 01:53:53570 {"Invalid IPv6 address", "https://[2001:db8:0:1]/",
571 L"https://[2001:db8:0:1]", L"https://[2001:db8:0:1]",
572 L"https://[2001:db8:0:1]"},
miguelgdbcfb122015-07-23 10:45:11573 {"HTTP filesystem: URL with path",
574 "filesystem:http://www.google.com/temporary/test.html",
benwells2337b8102016-04-20 01:53:53575 L"filesystem:http://www.google.com", L"filesystem:http://www.google.com",
miguelgdbcfb122015-07-23 10:45:11576 L"filesystem:http://www.google.com"},
577 {"File filesystem: URL with path",
juncaicb63cac2016-05-13 00:41:59578 "filesystem:file://localhost/temporary/stuff/"
579 "test.html?z=fun&goat=billy",
palmer153af982015-09-15 02:04:19580 L"filesystem:file:///temporary/stuff/test.html",
benwells2337b8102016-04-20 01:53:53581 L"filesystem:file:///temporary/stuff/test.html",
miguelgdbcfb122015-07-23 10:45:11582 L"filesystem:file:///temporary/stuff/test.html"},
583 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53584 L"twelve://www.cyber.org/wow.php", L"twelve://www.cyber.org/wow.php",
585 L"twelve://www.cyber.org/wow.php"},
miguelgdbcfb122015-07-23 10:45:11586 {"Invalid scheme 2", "://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53587 L"://www.cyber.org/wow.php", L"://www.cyber.org/wow.php",
588 L"://www.cyber.org/wow.php"},
miguelgdbcfb122015-07-23 10:45:11589 {"Invalid port 1", "https://173.194.65.103:000",
benwells2337b8102016-04-20 01:53:53590 L"https://173.194.65.103:0", L"173.194.65.103:0", L"173.194.65.103:0"},
miguelgdbcfb122015-07-23 10:45:11591 {"Invalid port 2", "https://173.194.65.103:gruffle",
benwells2337b8102016-04-20 01:53:53592 L"https://173.194.65.103:gruffle", L"https://173.194.65.103:gruffle",
593 L"https://173.194.65.103:gruffle"},
miguelgdbcfb122015-07-23 10:45:11594 {"Blob URL",
nick1b17dc32016-04-15 21:34:04595 "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
596 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19597 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
nick1b17dc32016-04-15 21:34:04598 L"blob:http://www.html5rocks.com/"
benwells2337b8102016-04-20 01:53:53599 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
600 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19601 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}};
miguelgdbcfb122015-07-23 10:45:11602
miguelgdbcfb122015-07-23 10:45:11603 for (size_t i = 0; i < arraysize(tests); ++i) {
jshin1fb76462016-04-05 22:13:03604 base::string16 formatted =
605 url_formatter::FormatUrlForSecurityDisplay(GURL(tests[i].input));
miguelgdbcfb122015-07-23 10:45:11606 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
607 << tests[i].description;
palmer153af982015-09-15 02:04:19608
benwells2337b8102016-04-20 01:53:53609 base::string16 formatted_omit_web_scheme =
610 url_formatter::FormatUrlForSecurityDisplay(
611 GURL(tests[i].input),
612 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
613 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme),
614 formatted_omit_web_scheme)
615 << tests[i].description;
616
617 base::string16 formatted_omit_cryptographic_scheme =
618 url_formatter::FormatUrlForSecurityDisplay(
619 GURL(tests[i].input),
620 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
621 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme),
622 formatted_omit_cryptographic_scheme)
palmer153af982015-09-15 02:04:19623 << tests[i].description;
miguelgdbcfb122015-07-23 10:45:11624 }
625
juncaicb63cac2016-05-13 00:41:59626 base::string16 formatted = url_formatter::FormatUrlForSecurityDisplay(GURL());
miguelgdbcfb122015-07-23 10:45:11627 EXPECT_EQ(base::string16(), formatted)
628 << "Explicitly test the 0-argument GURL constructor";
palmer153af982015-09-15 02:04:19629
630 base::string16 formatted_omit_scheme =
benwells2337b8102016-04-20 01:53:53631 url_formatter::FormatUrlForSecurityDisplay(
632 GURL(), url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
633 EXPECT_EQ(base::string16(), formatted_omit_scheme)
634 << "Explicitly test the 0-argument GURL constructor";
635
636 formatted_omit_scheme = url_formatter::FormatUrlForSecurityDisplay(
637 GURL(), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
palmer153af982015-09-15 02:04:19638 EXPECT_EQ(base::string16(), formatted_omit_scheme)
639 << "Explicitly test the 0-argument GURL constructor";
miguelgdbcfb122015-07-23 10:45:11640}
641
juncaicb63cac2016-05-13 00:41:59642TEST(TextEliderTest, FormatOriginForSecurityDisplay) {
643 for (size_t i = 0; i < arraysize(common_tests); ++i) {
644 base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay(
645 url::Origin(GURL(common_tests[i].input)));
646 EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted)
647 << common_tests[i].description;
648
649 base::string16 formatted_omit_web_scheme =
650 url_formatter::FormatOriginForSecurityDisplay(
651 url::Origin(GURL(common_tests[i].input)),
652 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
653 EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme),
654 formatted_omit_web_scheme)
655 << common_tests[i].description;
656
657 base::string16 formatted_omit_cryptographic_scheme =
658 url_formatter::FormatOriginForSecurityDisplay(
659 url::Origin(GURL(common_tests[i].input)),
660 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
661 EXPECT_EQ(
662 base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme),
663 formatted_omit_cryptographic_scheme)
664 << common_tests[i].description;
665 }
666
667 const OriginTestData tests[] = {
668 {"File URI", "file:///usr/example/file.html", L"file://", L"file://",
669 L"file://"},
670 {"File URI with hostname", "file://localhost/usr/example/file.html",
671 L"file://localhost", L"file://localhost", L"file://localhost"},
672 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls", L"file://",
673 L"file://", L"file://"},
674 {"UNC File URI 2",
675 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
676 L"file://", L"file://", L"file://"},
677 {"Invalid IPv6 address", "https://[2001:db8:0:1]/", L"", L"", L""},
678 {"HTTP filesystem: URL with path",
679 "filesystem:http://www.google.com/temporary/test.html",
680 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
681 {"File filesystem: URL with path",
682 "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy",
683 L"file://", L"file://", L"file://"},
684 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php", L"", L"", L""},
685 {"Invalid scheme 2", "://www.cyber.org/wow.php", L"", L"", L""},
686 {"Invalid port 1", "https://173.194.65.103:000", L"", L"", L""},
687 {"Invalid port 2", "https://173.194.65.103:gruffle", L"", L"", L""},
688 {"Blob URL",
689 "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
690 L"http://www.html5rocks.com", L"www.html5rocks.com",
691 L"http://www.html5rocks.com"}};
692
693 for (size_t i = 0; i < arraysize(tests); ++i) {
694 base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay(
695 url::Origin(GURL(tests[i].input)));
696 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
697 << tests[i].description;
698
699 base::string16 formatted_omit_web_scheme =
700 url_formatter::FormatOriginForSecurityDisplay(
701 url::Origin(GURL(tests[i].input)),
702 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
703 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme),
704 formatted_omit_web_scheme)
705 << tests[i].description;
706
707 base::string16 formatted_omit_cryptographic_scheme =
708 url_formatter::FormatOriginForSecurityDisplay(
709 url::Origin(GURL(tests[i].input)),
710 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
711 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme),
712 formatted_omit_cryptographic_scheme)
713 << tests[i].description;
714 }
715
716 base::string16 formatted =
717 url_formatter::FormatOriginForSecurityDisplay(url::Origin(GURL()));
718 EXPECT_EQ(base::string16(), formatted)
719 << "Explicitly test the url::Origin which takes an empty, invalid URL";
720
721 base::string16 formatted_omit_scheme =
722 url_formatter::FormatOriginForSecurityDisplay(
723 url::Origin(GURL()),
724 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
725 EXPECT_EQ(base::string16(), formatted_omit_scheme)
726 << "Explicitly test the url::Origin which takes an empty, invalid URL";
727
728 formatted_omit_scheme = url_formatter::FormatOriginForSecurityDisplay(
729 url::Origin(GURL()), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
730 EXPECT_EQ(base::string16(), formatted_omit_scheme)
731 << "Explicitly test the url::Origin which takes an empty, invalid URL";
732}
733
Christopher Grant6871e1212017-10-09 15:47:50734TEST(TextEliderTest, TestSimpleElisionMethod) {
735 const std::string kEllipsisStr(gfx::kEllipsis);
736 const std::vector<ProgressiveTestcase> testcases = {
737 {"https://www.abc.com/def/",
738 {
739 /* clang-format off */
740 {"https://www.abc.com/def/"},
741 {"https://www.abc.com/d" + kEllipsisStr},
742 {"https://www.abc.com/" + kEllipsisStr},
743 {"www.abc.com/def/"},
744 {"www.abc.com/d" + kEllipsisStr},
745 {"www.abc.com/" + kEllipsisStr},
746 {kEllipsisStr + "ww.abc.com/" + kEllipsisStr},
747 {kEllipsisStr + "w.abc.com/" + kEllipsisStr},
748 {kEllipsisStr + ".abc.com/" + kEllipsisStr},
749 {kEllipsisStr + "abc.com/" + kEllipsisStr},
750 {kEllipsisStr + "bc.com/" + kEllipsisStr},
751 {kEllipsisStr + "c.com/" + kEllipsisStr},
752 {kEllipsisStr + ".com/" + kEllipsisStr},
753 {kEllipsisStr + "com/" + kEllipsisStr},
754 {kEllipsisStr + "om/" + kEllipsisStr},
755 {kEllipsisStr + "m/" + kEllipsisStr},
756 {kEllipsisStr + "/" + kEllipsisStr},
757 {kEllipsisStr},
758 {""},
759 /* clang-format on */
760 }},
761 {"file://fs/file",
762 {
763 /* clang-format off */
764 "file://fs/file",
765 "file://fs/fi" + kEllipsisStr,
766 "file://fs/f" + kEllipsisStr,
767 "file://fs/" + kEllipsisStr,
768 "file://fs" + kEllipsisStr,
769 "file://f" + kEllipsisStr,
770 "file://" + kEllipsisStr,
771 "file:/" + kEllipsisStr,
772 "file:" + kEllipsisStr,
773 "file" + kEllipsisStr,
774 "fil" + kEllipsisStr,
775 "fi" + kEllipsisStr,
776 "f" + kEllipsisStr,
777 kEllipsisStr,
778 "",
779 /* clang-format on */
780 }},
781 };
782 RunProgressiveElisionTest(testcases, kMethodSimple);
783}
784
785// Verify that the secure elision method returns URL component data that
786// correctly represents the elided URL.
787void RunElisionParsingTest(const std::vector<ParsingTestcase>& testcases) {
788 const gfx::FontList font_list;
789 for (const auto& testcase : testcases) {
790 SCOPED_TRACE(testcase.input + " to " + testcase.output);
791
792 const GURL url(testcase.input);
793 const float available_width =
794 gfx::GetStringWidthF(base::UTF8ToUTF16(testcase.output), font_list);
795
796 url::Parsed parsed;
797 auto elided =
798 url_formatter::ElideUrlSimple(url, font_list, available_width, &parsed);
799 EXPECT_EQ(base::UTF8ToUTF16(testcase.output), elided);
800
801 // Build an expected Parsed struct from the sparse test expectations.
802 url::Parsed expected;
803 for (const auto& expectation : testcase.components) {
804 url::Component* component = GetComponent(&expected, expectation.type);
805 component->begin = expectation.begin;
806 component->len = expectation.len;
807 }
808
809 const std::vector<url::Parsed::ComponentType> kComponents = {
810 url::Parsed::SCHEME, url::Parsed::USERNAME, url::Parsed::PASSWORD,
811 url::Parsed::HOST, url::Parsed::PORT, url::Parsed::PATH,
812 url::Parsed::QUERY, url::Parsed::REF,
813 };
814 for (const auto& type : kComponents) {
815 EXPECT_EQ(GetComponent(&expected, type)->begin,
816 GetComponent(&parsed, type)->begin)
817 << " in component " << type;
818 EXPECT_EQ(GetComponent(&expected, type)->len,
819 GetComponent(&parsed, type)->len)
820 << " in component " << type;
821 }
822 }
823}
824
825// Verify that during elision, the parsed URL components are properly modified.
826TEST(TextEliderTest, TestElisionParsingAdjustments) {
827 const std::string kEllipsisStr(gfx::kEllipsis);
828 const std::vector<ParsingTestcase> testcases = {
829 // HTTPS with path.
830 {"https://www.google.com/intl/en/ads/",
831 "https://www.google.com/intl/en/ads/",
832 {{url::Parsed::ComponentType::SCHEME, 0, 5},
833 {url::Parsed::ComponentType::HOST, 8, 14},
834 {url::Parsed::ComponentType::PATH, 22, 13}}},
835 {"https://www.google.com/intl/en/ads/",
836 "https://www.google.com/intl/en/a" + kEllipsisStr,
837 {{url::Parsed::ComponentType::SCHEME, 0, 5},
838 {url::Parsed::ComponentType::HOST, 8, 14},
839 {url::Parsed::ComponentType::PATH, 22, 11}}},
840 {"https://www.google.com/intl/en/ads/",
841 "https://www.google.com/" + kEllipsisStr,
842 {{url::Parsed::ComponentType::SCHEME, 0, 5},
843 {url::Parsed::ComponentType::HOST, 8, 14},
844 {url::Parsed::ComponentType::PATH, 22, 2}}},
845 {"https://www.google.com/intl/en/ads/",
846 kEllipsisStr + "google.com/" + kEllipsisStr,
847 {{url::Parsed::ComponentType::HOST, 0, 11},
848 {url::Parsed::ComponentType::PATH, 11, 2}}},
849 {"https://www.google.com/intl/en/ads/",
850 kEllipsisStr,
851 {{url::Parsed::ComponentType::PATH, 0, 1}}},
852 // HTTPS with no path.
853 {"https://www.google.com/",
854 "www.google.com",
855 {{url::Parsed::ComponentType::HOST, 0, 14}}},
856 {"https://www.google.com/",
857 kEllipsisStr,
858 {{url::Parsed::ComponentType::HOST, 0, 1}}},
859 // HTTP with no path.
860 {"http://www.google.com/",
861 "www.google.com",
862 {{url::Parsed::ComponentType::HOST, 0, 14}}},
863 // File URLs.
864 {"file:///C:/path1/path2",
865 "file:///C:/path1/" + kEllipsisStr,
866 {{url::Parsed::ComponentType::SCHEME, 0, 4},
867 {url::Parsed::ComponentType::PATH, 7, 11}}},
868 {"file:///C:/path1/path2",
869 "fi" + kEllipsisStr,
870 {{url::Parsed::ComponentType::SCHEME, 0, 3}}},
871 {"file:///C:/path1/path2",
872 kEllipsisStr,
873 {{url::Parsed::ComponentType::SCHEME, 0, 1}}},
874 // RTL URL.
875 {"http://127.0.0.1/ا/http://attack.com‬",
876 kEllipsisStr + "7.0.0.1/" + kEllipsisStr,
877 {{url::Parsed::ComponentType::HOST, 0, 8},
878 {url::Parsed::ComponentType::PATH, 8, 2}}},
879 };
880
881 RunElisionParsingTest(testcases);
882}
883
miguelgdbcfb122015-07-23 10:45:11884} // namespace