blob: e15ae12beb8bcf6581f3fbda98f1bddfba51d035 [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",
341 "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
342
343 // Invalid unescaping for path. The ref will always be valid UTF-8. We
Christopher Grant5ed8d112017-09-12 20:00:06344 // don't bother to do too many edge cases, since these are handled by the
345 // escaper unittest.
miguelgdbcfb122015-07-23 10:45:11346 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
347 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
348 };
349
Christopher Grant5ed8d112017-09-12 20:00:06350 RunElisionTest(testcases);
miguelgdbcfb122015-07-23 10:45:11351}
352
353// Test eliding of file: URLs.
354TEST(TextEliderTest, TestFileURLEliding) {
rsleevi24f64dc22015-08-07 21:39:21355 const std::string kEllipsisStr(gfx::kEllipsis);
Christopher Grant5ed8d112017-09-12 20:00:06356 const std::vector<ProgressiveTestcase> progressive_testcases = {
miguelgdbcfb122015-07-23 10:45:11357 {"file:///C:/path1/path2/path3/filename",
Christopher Grant5ed8d112017-09-12 20:00:06358 {
359 /* clang-format off */
360 "file:///C:/path1/path2/path3/filename",
361 "C:/path1/path2/path3/filename",
362 "C:/path1/path2/" + kEllipsisStr + "/filename",
363 /* clang-format on */
364 }},
miguelgdbcfb122015-07-23 10:45:11365// GURL parses "file:///C:path" differently on windows than it does on posix.
366#if defined(OS_WIN)
367 {"file:///C:path1/path2/path3/filename",
Christopher Grant5ed8d112017-09-12 20:00:06368 {
369 /* clang-format off */
370 "file:///C:/path1/path2/path3/filename",
371 "C:/path1/path2/path3/filename",
372 "C:/path1/path2/" + kEllipsisStr + "/filename",
373 "C:/path1/" + kEllipsisStr + "/filename",
374 "C:/" + kEllipsisStr + "/filename",
375 /* clang-format on */
376 }},
miguelgdbcfb122015-07-23 10:45:11377#endif // defined(OS_WIN)
Christopher Grant5ed8d112017-09-12 20:00:06378 {"file://filer/foo/bar/file",
379 {
380 /* clang-format off */
381 "file://filer/foo/bar/file",
382 "filer/foo/bar/file",
383 "filer/foo/" + kEllipsisStr + "/file",
384 "filer/" + kEllipsisStr + "/file",
385 "filer/foo" + kEllipsisStr,
386 "filer/fo" + kEllipsisStr,
387 "filer/f" + kEllipsisStr,
388 "filer/" + kEllipsisStr,
389 "filer" + kEllipsisStr,
390 "file" + kEllipsisStr,
391 /* clang-format on */
392 }},
miguelgdbcfb122015-07-23 10:45:11393 };
394
Christopher Grant6871e1212017-10-09 15:47:50395 RunProgressiveElisionTest(progressive_testcases, kMethodOriginal);
Christopher Grant5ed8d112017-09-12 20:00:06396
397 const std::vector<Testcase> testcases = {
398 // Eliding file URLs with nothing after the ':' shouldn't crash.
399 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:", "aaa" + kEllipsisStr},
400 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:/", "aaa" + kEllipsisStr},
401 };
402 RunElisionTest(testcases);
miguelgdbcfb122015-07-23 10:45:11403}
404
405TEST(TextEliderTest, TestHostEliding) {
rsleevi24f64dc22015-08-07 21:39:21406 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11407 Testcase testcases[] = {
pkl2affa912017-02-22 15:23:20408 {"http://google.com", "google.com"},
409 {"http://reallyreallyreallylongdomainname.com",
410 "reallyreallyreallylongdomainname.com"},
411 {"http://foo", "foo"},
412 {"http://foo.bar", "foo.bar"},
pkl2affa912017-02-22 15:23:20413 {"http://subdomain.google.com", kEllipsisStr + ".google.com"},
414 {"http://a.b.c.d.e.f.com", kEllipsisStr + "f.com"},
415 {"http://subdomain.foo.bar", kEllipsisStr + "in.foo.bar"},
416 {"http://subdomain.reallylongdomainname.com",
417 kEllipsisStr + "ain.reallylongdomainname.com"},
418 {"http://a.b.c.d.e.f.com", kEllipsisStr + ".e.f.com"},
419 // IDN - Greek alpha.beta.gamma.delta.epsilon.zeta.com
420 {"http://xn--mxa.xn--nxa.xn--oxa.xn--pxa.xn--qxa.xn--rxa.com",
421 kEllipsisStr + ".\xCE\xB5.\xCE\xB6.com"},
miguelgdbcfb122015-07-23 10:45:11422 };
423
424 for (size_t i = 0; i < arraysize(testcases); ++i) {
rsleevi24f64dc22015-08-07 21:39:21425 const float available_width = gfx::GetStringWidthF(
426 base::UTF8ToUTF16(testcases[i].output), gfx::FontList());
427 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
428 url_formatter::ElideHost(GURL(testcases[i].input),
429 gfx::FontList(), available_width));
miguelgdbcfb122015-07-23 10:45:11430 }
431
432 // Trying to elide to a really short length will still keep the full TLD+1
433 EXPECT_EQ(
434 base::ASCIIToUTF16("google.com"),
rsleevi24f64dc22015-08-07 21:39:21435 url_formatter::ElideHost(GURL("http://google.com"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11436 EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"),
rsleevi24f64dc22015-08-07 21:39:21437 url_formatter::ElideHost(GURL("http://subdomain.google.com"),
438 gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11439 EXPECT_EQ(
440 base::ASCIIToUTF16("foo.bar"),
rsleevi24f64dc22015-08-07 21:39:21441 url_formatter::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11442}
443
444#endif // !defined(OS_ANDROID)
445
juncaicb63cac2016-05-13 00:41:59446struct OriginTestData {
447 const char* const description;
448 const char* const input;
449 const wchar_t* const output;
450 const wchar_t* const output_omit_web_scheme;
451 const wchar_t* const output_omit_cryptographic_scheme;
452};
453
454// Common test data for both FormatUrlForSecurityDisplay() and
455// FormatOriginForSecurityDisplay()
456const OriginTestData common_tests[] = {
457 {"Empty URL", "", L"", L"", L""},
458 {"HTTP URL", "http://www.google.com/", L"http://www.google.com",
459 L"www.google.com", L"http://www.google.com"},
460 {"HTTPS URL", "https://www.google.com/", L"https://www.google.com",
461 L"www.google.com", L"www.google.com"},
462 {"Standard HTTP port", "http://www.google.com:80/",
463 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
464 {"Standard HTTPS port", "https://www.google.com:443/",
465 L"https://www.google.com", L"www.google.com", L"www.google.com"},
466 {"Standard HTTP port, IDN Chinese",
467 "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80",
jshin78809c4d82016-10-06 20:15:45468 L"http://\x4e2d\x56fd.icom.museum", L"\x4e2d\x56fd.icom.museum",
469 L"http://\x4e2d\x56fd.icom.museum"},
juncaicb63cac2016-05-13 00:41:59470 {"HTTP URL, IDN Hebrew (RTL)",
471 "http://"
472 "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d."
473 "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/",
474 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum",
475 L"xn--4dbklr2c8d.xn--4dbrk0ce.museum",
476 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum"},
477 {"HTTP URL with query string, IDN Arabic (RTL)",
478 "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no",
479 L"http://xn--wgbh1c.icom.museum", L"xn--wgbh1c.icom.museum",
480 L"http://xn--wgbh1c.icom.museum"},
481 {"Non-standard HTTP port", "http://www.google.com:9000/",
482 L"http://www.google.com:9000", L"www.google.com:9000",
483 L"http://www.google.com:9000"},
484 {"Non-standard HTTPS port", "https://www.google.com:9000/",
485 L"https://www.google.com:9000", L"www.google.com:9000",
486 L"www.google.com:9000"},
487 {"HTTP URL with path", "http://www.google.com/test.html",
488 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
489 {"HTTPS URL with path", "https://www.google.com/test.html",
490 L"https://www.google.com", L"www.google.com", L"www.google.com"},
491 {"Unusual secure scheme (wss)", "wss://www.google.com/",
492 L"wss://www.google.com", L"wss://www.google.com", L"www.google.com"},
493 {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/",
494 L"gopher://www.google.com", L"gopher://www.google.com",
495 L"gopher://www.google.com"},
496 {"Unlisted scheme (chrome)", "chrome://version", L"chrome://version",
497 L"chrome://version", L"chrome://version"},
498 {"HTTP IP address", "http://173.194.65.103", L"http://173.194.65.103",
499 L"173.194.65.103", L"http://173.194.65.103"},
500 {"HTTPS IP address", "https://173.194.65.103", L"https://173.194.65.103",
501 L"173.194.65.103", L"173.194.65.103"},
502 {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
503 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
504 L"http://[fe80::202:b3ff:fe1e:8329]"},
505 {"HTTPs IPv6 address", "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
506 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
507 L"[fe80::202:b3ff:fe1e:8329]"},
508 {"HTTP IPv6 address with port",
509 "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:80/",
510 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
511 L"http://[fe80::202:b3ff:fe1e:8329]"},
512 {"HTTPs IPv6 address with port",
513 "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:443/",
514 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
515 L"[fe80::202:b3ff:fe1e:8329]"},
516 {"HTTPS IP address, non-default port", "https://173.194.65.103:8443",
517 L"https://173.194.65.103:8443", L"173.194.65.103:8443",
518 L"173.194.65.103:8443"},
519 {"Invalid host 1", "https://www.cyber../wow.php", L"https://www.cyber..",
520 L"www.cyber..", L"www.cyber.."},
521 {"Invalid host 2", "https://www...cyber/wow.php", L"https://www...cyber",
522 L"www...cyber", L"www...cyber"},
523 {"Invalid port 3", "https://173.194.65.103:/hello.aspx",
524 L"https://173.194.65.103", L"173.194.65.103", L"173.194.65.103"},
525 {"Trailing dot in DNS name", "https://www.example.com./get/goat",
526 L"https://www.example.com.", L"www.example.com.", L"www.example.com."}};
527
miguelgdbcfb122015-07-23 10:45:11528TEST(TextEliderTest, FormatUrlForSecurityDisplay) {
juncaicb63cac2016-05-13 00:41:59529 for (size_t i = 0; i < arraysize(common_tests); ++i) {
530 base::string16 formatted =
531 url_formatter::FormatUrlForSecurityDisplay(GURL(common_tests[i].input));
532 EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted)
533 << common_tests[i].description;
534
535 base::string16 formatted_omit_web_scheme =
536 url_formatter::FormatUrlForSecurityDisplay(
537 GURL(common_tests[i].input),
538 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
539 EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme),
540 formatted_omit_web_scheme)
541 << common_tests[i].description;
542
543 base::string16 formatted_omit_cryptographic_scheme =
544 url_formatter::FormatUrlForSecurityDisplay(
545 GURL(common_tests[i].input),
546 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
547 EXPECT_EQ(
548 base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme),
549 formatted_omit_cryptographic_scheme)
550 << common_tests[i].description;
551 }
miguelgdbcfb122015-07-23 10:45:11552
553 const OriginTestData tests[] = {
miguelgdbcfb122015-07-23 10:45:11554 {"File URI", "file:///usr/example/file.html",
benwells2337b8102016-04-20 01:53:53555 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
556 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11557 {"File URI with hostname", "file://localhost/usr/example/file.html",
benwells2337b8102016-04-20 01:53:53558 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
559 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11560 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls",
palmer153af982015-09-15 02:04:19561 L"file:///CONTOSO/accounting/money.xls",
benwells2337b8102016-04-20 01:53:53562 L"file:///CONTOSO/accounting/money.xls",
miguelgdbcfb122015-07-23 10:45:11563 L"file:///CONTOSO/accounting/money.xls"},
564 {"UNC File URI 2",
565 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
palmer153af982015-09-15 02:04:19566 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
benwells2337b8102016-04-20 01:53:53567 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
miguelgdbcfb122015-07-23 10:45:11568 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html"},
benwells2337b8102016-04-20 01:53:53569 {"Invalid IPv6 address", "https://[2001:db8:0:1]/",
570 L"https://[2001:db8:0:1]", L"https://[2001:db8:0:1]",
571 L"https://[2001:db8:0:1]"},
miguelgdbcfb122015-07-23 10:45:11572 {"HTTP filesystem: URL with path",
573 "filesystem:http://www.google.com/temporary/test.html",
benwells2337b8102016-04-20 01:53:53574 L"filesystem:http://www.google.com", L"filesystem:http://www.google.com",
miguelgdbcfb122015-07-23 10:45:11575 L"filesystem:http://www.google.com"},
576 {"File filesystem: URL with path",
juncaicb63cac2016-05-13 00:41:59577 "filesystem:file://localhost/temporary/stuff/"
578 "test.html?z=fun&goat=billy",
palmer153af982015-09-15 02:04:19579 L"filesystem:file:///temporary/stuff/test.html",
benwells2337b8102016-04-20 01:53:53580 L"filesystem:file:///temporary/stuff/test.html",
miguelgdbcfb122015-07-23 10:45:11581 L"filesystem:file:///temporary/stuff/test.html"},
582 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53583 L"twelve://www.cyber.org/wow.php", L"twelve://www.cyber.org/wow.php",
584 L"twelve://www.cyber.org/wow.php"},
miguelgdbcfb122015-07-23 10:45:11585 {"Invalid scheme 2", "://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53586 L"://www.cyber.org/wow.php", L"://www.cyber.org/wow.php",
587 L"://www.cyber.org/wow.php"},
miguelgdbcfb122015-07-23 10:45:11588 {"Invalid port 1", "https://173.194.65.103:000",
benwells2337b8102016-04-20 01:53:53589 L"https://173.194.65.103:0", L"173.194.65.103:0", L"173.194.65.103:0"},
miguelgdbcfb122015-07-23 10:45:11590 {"Invalid port 2", "https://173.194.65.103:gruffle",
benwells2337b8102016-04-20 01:53:53591 L"https://173.194.65.103:gruffle", L"https://173.194.65.103:gruffle",
592 L"https://173.194.65.103:gruffle"},
miguelgdbcfb122015-07-23 10:45:11593 {"Blob URL",
nick1b17dc32016-04-15 21:34:04594 "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
595 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19596 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
nick1b17dc32016-04-15 21:34:04597 L"blob:http://www.html5rocks.com/"
benwells2337b8102016-04-20 01:53:53598 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
599 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19600 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}};
miguelgdbcfb122015-07-23 10:45:11601
miguelgdbcfb122015-07-23 10:45:11602 for (size_t i = 0; i < arraysize(tests); ++i) {
jshin1fb76462016-04-05 22:13:03603 base::string16 formatted =
604 url_formatter::FormatUrlForSecurityDisplay(GURL(tests[i].input));
miguelgdbcfb122015-07-23 10:45:11605 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
606 << tests[i].description;
palmer153af982015-09-15 02:04:19607
benwells2337b8102016-04-20 01:53:53608 base::string16 formatted_omit_web_scheme =
609 url_formatter::FormatUrlForSecurityDisplay(
610 GURL(tests[i].input),
611 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
612 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme),
613 formatted_omit_web_scheme)
614 << tests[i].description;
615
616 base::string16 formatted_omit_cryptographic_scheme =
617 url_formatter::FormatUrlForSecurityDisplay(
618 GURL(tests[i].input),
619 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
620 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme),
621 formatted_omit_cryptographic_scheme)
palmer153af982015-09-15 02:04:19622 << tests[i].description;
miguelgdbcfb122015-07-23 10:45:11623 }
624
juncaicb63cac2016-05-13 00:41:59625 base::string16 formatted = url_formatter::FormatUrlForSecurityDisplay(GURL());
miguelgdbcfb122015-07-23 10:45:11626 EXPECT_EQ(base::string16(), formatted)
627 << "Explicitly test the 0-argument GURL constructor";
palmer153af982015-09-15 02:04:19628
629 base::string16 formatted_omit_scheme =
benwells2337b8102016-04-20 01:53:53630 url_formatter::FormatUrlForSecurityDisplay(
631 GURL(), url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
632 EXPECT_EQ(base::string16(), formatted_omit_scheme)
633 << "Explicitly test the 0-argument GURL constructor";
634
635 formatted_omit_scheme = url_formatter::FormatUrlForSecurityDisplay(
636 GURL(), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
palmer153af982015-09-15 02:04:19637 EXPECT_EQ(base::string16(), formatted_omit_scheme)
638 << "Explicitly test the 0-argument GURL constructor";
miguelgdbcfb122015-07-23 10:45:11639}
640
juncaicb63cac2016-05-13 00:41:59641TEST(TextEliderTest, FormatOriginForSecurityDisplay) {
642 for (size_t i = 0; i < arraysize(common_tests); ++i) {
643 base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay(
644 url::Origin(GURL(common_tests[i].input)));
645 EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted)
646 << common_tests[i].description;
647
648 base::string16 formatted_omit_web_scheme =
649 url_formatter::FormatOriginForSecurityDisplay(
650 url::Origin(GURL(common_tests[i].input)),
651 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
652 EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme),
653 formatted_omit_web_scheme)
654 << common_tests[i].description;
655
656 base::string16 formatted_omit_cryptographic_scheme =
657 url_formatter::FormatOriginForSecurityDisplay(
658 url::Origin(GURL(common_tests[i].input)),
659 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
660 EXPECT_EQ(
661 base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme),
662 formatted_omit_cryptographic_scheme)
663 << common_tests[i].description;
664 }
665
666 const OriginTestData tests[] = {
667 {"File URI", "file:///usr/example/file.html", L"file://", L"file://",
668 L"file://"},
669 {"File URI with hostname", "file://localhost/usr/example/file.html",
670 L"file://localhost", L"file://localhost", L"file://localhost"},
671 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls", L"file://",
672 L"file://", L"file://"},
673 {"UNC File URI 2",
674 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
675 L"file://", L"file://", L"file://"},
676 {"Invalid IPv6 address", "https://[2001:db8:0:1]/", L"", L"", L""},
677 {"HTTP filesystem: URL with path",
678 "filesystem:http://www.google.com/temporary/test.html",
679 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
680 {"File filesystem: URL with path",
681 "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy",
682 L"file://", L"file://", L"file://"},
683 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php", L"", L"", L""},
684 {"Invalid scheme 2", "://www.cyber.org/wow.php", L"", L"", L""},
685 {"Invalid port 1", "https://173.194.65.103:000", L"", L"", L""},
686 {"Invalid port 2", "https://173.194.65.103:gruffle", L"", L"", L""},
687 {"Blob URL",
688 "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
689 L"http://www.html5rocks.com", L"www.html5rocks.com",
690 L"http://www.html5rocks.com"}};
691
692 for (size_t i = 0; i < arraysize(tests); ++i) {
693 base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay(
694 url::Origin(GURL(tests[i].input)));
695 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
696 << tests[i].description;
697
698 base::string16 formatted_omit_web_scheme =
699 url_formatter::FormatOriginForSecurityDisplay(
700 url::Origin(GURL(tests[i].input)),
701 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
702 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme),
703 formatted_omit_web_scheme)
704 << tests[i].description;
705
706 base::string16 formatted_omit_cryptographic_scheme =
707 url_formatter::FormatOriginForSecurityDisplay(
708 url::Origin(GURL(tests[i].input)),
709 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
710 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme),
711 formatted_omit_cryptographic_scheme)
712 << tests[i].description;
713 }
714
715 base::string16 formatted =
716 url_formatter::FormatOriginForSecurityDisplay(url::Origin(GURL()));
717 EXPECT_EQ(base::string16(), formatted)
718 << "Explicitly test the url::Origin which takes an empty, invalid URL";
719
720 base::string16 formatted_omit_scheme =
721 url_formatter::FormatOriginForSecurityDisplay(
722 url::Origin(GURL()),
723 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
724 EXPECT_EQ(base::string16(), formatted_omit_scheme)
725 << "Explicitly test the url::Origin which takes an empty, invalid URL";
726
727 formatted_omit_scheme = url_formatter::FormatOriginForSecurityDisplay(
728 url::Origin(GURL()), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
729 EXPECT_EQ(base::string16(), formatted_omit_scheme)
730 << "Explicitly test the url::Origin which takes an empty, invalid URL";
731}
732
Christopher Grant6871e1212017-10-09 15:47:50733TEST(TextEliderTest, TestSimpleElisionMethod) {
734 const std::string kEllipsisStr(gfx::kEllipsis);
735 const std::vector<ProgressiveTestcase> testcases = {
736 {"https://www.abc.com/def/",
737 {
738 /* clang-format off */
739 {"https://www.abc.com/def/"},
740 {"https://www.abc.com/d" + kEllipsisStr},
741 {"https://www.abc.com/" + kEllipsisStr},
742 {"www.abc.com/def/"},
743 {"www.abc.com/d" + kEllipsisStr},
744 {"www.abc.com/" + kEllipsisStr},
745 {kEllipsisStr + "ww.abc.com/" + kEllipsisStr},
746 {kEllipsisStr + "w.abc.com/" + kEllipsisStr},
747 {kEllipsisStr + ".abc.com/" + kEllipsisStr},
748 {kEllipsisStr + "abc.com/" + kEllipsisStr},
749 {kEllipsisStr + "bc.com/" + kEllipsisStr},
750 {kEllipsisStr + "c.com/" + kEllipsisStr},
751 {kEllipsisStr + ".com/" + kEllipsisStr},
752 {kEllipsisStr + "com/" + kEllipsisStr},
753 {kEllipsisStr + "om/" + kEllipsisStr},
754 {kEllipsisStr + "m/" + kEllipsisStr},
755 {kEllipsisStr + "/" + kEllipsisStr},
756 {kEllipsisStr},
757 {""},
758 /* clang-format on */
759 }},
760 {"file://fs/file",
761 {
762 /* clang-format off */
763 "file://fs/file",
764 "file://fs/fi" + kEllipsisStr,
765 "file://fs/f" + kEllipsisStr,
766 "file://fs/" + kEllipsisStr,
767 "file://fs" + kEllipsisStr,
768 "file://f" + kEllipsisStr,
769 "file://" + kEllipsisStr,
770 "file:/" + kEllipsisStr,
771 "file:" + kEllipsisStr,
772 "file" + kEllipsisStr,
773 "fil" + kEllipsisStr,
774 "fi" + kEllipsisStr,
775 "f" + kEllipsisStr,
776 kEllipsisStr,
777 "",
778 /* clang-format on */
779 }},
780 };
781 RunProgressiveElisionTest(testcases, kMethodSimple);
782}
783
784// Verify that the secure elision method returns URL component data that
785// correctly represents the elided URL.
786void RunElisionParsingTest(const std::vector<ParsingTestcase>& testcases) {
787 const gfx::FontList font_list;
788 for (const auto& testcase : testcases) {
789 SCOPED_TRACE(testcase.input + " to " + testcase.output);
790
791 const GURL url(testcase.input);
792 const float available_width =
793 gfx::GetStringWidthF(base::UTF8ToUTF16(testcase.output), font_list);
794
795 url::Parsed parsed;
796 auto elided =
797 url_formatter::ElideUrlSimple(url, font_list, available_width, &parsed);
798 EXPECT_EQ(base::UTF8ToUTF16(testcase.output), elided);
799
800 // Build an expected Parsed struct from the sparse test expectations.
801 url::Parsed expected;
802 for (const auto& expectation : testcase.components) {
803 url::Component* component = GetComponent(&expected, expectation.type);
804 component->begin = expectation.begin;
805 component->len = expectation.len;
806 }
807
808 const std::vector<url::Parsed::ComponentType> kComponents = {
809 url::Parsed::SCHEME, url::Parsed::USERNAME, url::Parsed::PASSWORD,
810 url::Parsed::HOST, url::Parsed::PORT, url::Parsed::PATH,
811 url::Parsed::QUERY, url::Parsed::REF,
812 };
813 for (const auto& type : kComponents) {
814 EXPECT_EQ(GetComponent(&expected, type)->begin,
815 GetComponent(&parsed, type)->begin)
816 << " in component " << type;
817 EXPECT_EQ(GetComponent(&expected, type)->len,
818 GetComponent(&parsed, type)->len)
819 << " in component " << type;
820 }
821 }
822}
823
824// Verify that during elision, the parsed URL components are properly modified.
825TEST(TextEliderTest, TestElisionParsingAdjustments) {
826 const std::string kEllipsisStr(gfx::kEllipsis);
827 const std::vector<ParsingTestcase> testcases = {
828 // HTTPS with path.
829 {"https://www.google.com/intl/en/ads/",
830 "https://www.google.com/intl/en/ads/",
831 {{url::Parsed::ComponentType::SCHEME, 0, 5},
832 {url::Parsed::ComponentType::HOST, 8, 14},
833 {url::Parsed::ComponentType::PATH, 22, 13}}},
834 {"https://www.google.com/intl/en/ads/",
835 "https://www.google.com/intl/en/a" + kEllipsisStr,
836 {{url::Parsed::ComponentType::SCHEME, 0, 5},
837 {url::Parsed::ComponentType::HOST, 8, 14},
838 {url::Parsed::ComponentType::PATH, 22, 11}}},
839 {"https://www.google.com/intl/en/ads/",
840 "https://www.google.com/" + kEllipsisStr,
841 {{url::Parsed::ComponentType::SCHEME, 0, 5},
842 {url::Parsed::ComponentType::HOST, 8, 14},
843 {url::Parsed::ComponentType::PATH, 22, 2}}},
844 {"https://www.google.com/intl/en/ads/",
845 kEllipsisStr + "google.com/" + kEllipsisStr,
846 {{url::Parsed::ComponentType::HOST, 0, 11},
847 {url::Parsed::ComponentType::PATH, 11, 2}}},
848 {"https://www.google.com/intl/en/ads/",
849 kEllipsisStr,
850 {{url::Parsed::ComponentType::PATH, 0, 1}}},
851 // HTTPS with no path.
852 {"https://www.google.com/",
853 "www.google.com",
854 {{url::Parsed::ComponentType::HOST, 0, 14}}},
855 {"https://www.google.com/",
856 kEllipsisStr,
857 {{url::Parsed::ComponentType::HOST, 0, 1}}},
858 // HTTP with no path.
859 {"http://www.google.com/",
860 "www.google.com",
861 {{url::Parsed::ComponentType::HOST, 0, 14}}},
862 // File URLs.
863 {"file:///C:/path1/path2",
864 "file:///C:/path1/" + kEllipsisStr,
865 {{url::Parsed::ComponentType::SCHEME, 0, 4},
866 {url::Parsed::ComponentType::PATH, 7, 11}}},
867 {"file:///C:/path1/path2",
868 "fi" + kEllipsisStr,
869 {{url::Parsed::ComponentType::SCHEME, 0, 3}}},
870 {"file:///C:/path1/path2",
871 kEllipsisStr,
872 {{url::Parsed::ComponentType::SCHEME, 0, 1}}},
873 // RTL URL.
874 {"http://127.0.0.1/ا/http://attack.com‬",
875 kEllipsisStr + "7.0.0.1/" + kEllipsisStr,
876 {{url::Parsed::ComponentType::HOST, 0, 8},
877 {url::Parsed::ComponentType::PATH, 8, 2}}},
878 };
879
880 RunElisionParsingTest(testcases);
881}
882
miguelgdbcfb122015-07-23 10:45:11883} // namespace