miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 5 | #include "components/url_formatter/elide_url.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 6 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 9 | #include "base/macros.h" |
gab | f64a25e | 2017-05-12 19:42:56 | [diff] [blame] | 10 | #include "base/message_loop/message_loop.h" |
kulshin | ce612eb | 2016-06-29 18:46:51 | [diff] [blame] | 11 | #include "base/run_loop.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 13 | #include "build/build_config.h" |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 14 | #include "components/url_formatter/url_formatter.h" |
| 15 | #include "net/base/escape.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 17 | #include "ui/gfx/font_list.h" |
| 18 | #include "ui/gfx/text_elider.h" |
| 19 | #include "ui/gfx/text_utils.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 20 | #include "url/gurl.h" |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 21 | #include "url/origin.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 22 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | struct Testcase { |
| 26 | const std::string input; |
| 27 | const std::string output; |
| 28 | }; |
| 29 | |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 30 | struct ProgressiveTestcase { |
| 31 | const std::string input; |
| 32 | const std::vector<std::string> output; |
| 33 | }; |
| 34 | |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 35 | struct UrlComponent { |
| 36 | url::Parsed::ComponentType type; |
| 37 | int begin; |
| 38 | int len; |
| 39 | }; |
| 40 | |
| 41 | struct ParsingTestcase { |
| 42 | const std::string input; |
| 43 | const std::string output; |
| 44 | const std::vector<UrlComponent> components; |
| 45 | }; |
| 46 | |
Christopher Grant | d477bee | 2018-01-24 17:08:41 | [diff] [blame] | 47 | #if !defined(OS_ANDROID) |
| 48 | |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 49 | // Returns the width of a utf8 or utf16 string using the BROWSER typesetter and |
| 50 | // default UI font, or the provided |font_list|. |
| 51 | float GetWidth(const std::string& utf8, |
| 52 | const gfx::FontList& font_list = gfx::FontList()) { |
| 53 | return gfx::GetStringWidthF(base::UTF8ToUTF16(utf8), font_list, |
| 54 | gfx::Typesetter::BROWSER); |
| 55 | } |
| 56 | float GetWidth(const base::string16& utf16, |
| 57 | const gfx::FontList& font_list = gfx::FontList()) { |
| 58 | return gfx::GetStringWidthF(utf16, font_list, gfx::Typesetter::BROWSER); |
| 59 | } |
| 60 | |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 61 | // Verify that one or more URLs passes through an explicit sequence of elided |
| 62 | // strings as available space progressively decreases. This helps ensure that |
| 63 | // transitional corner cases are handled properly. To be tolerant of |
| 64 | // character-width variation across platforms, the test allows a limited number |
| 65 | // of expected strings to be skipped mid-run. The first and last expected |
| 66 | // strings must be matched. If the algorithm produces a string that isn't in the |
| 67 | // expected string list, the test fill fail. Example test expectations: |
| 68 | // |
| 69 | // google.com/intl/en/.../ads/ <-- Must match. |
| 70 | // google.com/intl/.../ads/ |
| 71 | // google.com/.../ads/ |
| 72 | // google.com/intl... <- Elider can skip this, in case the 'l' does not fit. |
| 73 | // google.com/int... |
| 74 | // google.com/in... <- Must match. |
| 75 | // |
| 76 | void RunProgressiveElisionTest( |
Christopher Grant | d477bee | 2018-01-24 17:08:41 | [diff] [blame] | 77 | const std::vector<ProgressiveTestcase>& testcases) { |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 78 | const gfx::FontList font_list; |
| 79 | for (const auto& testcase : testcases) { |
| 80 | SCOPED_TRACE("Eliding " + testcase.input); |
| 81 | const GURL url(testcase.input); |
| 82 | |
| 83 | // Occasionally, a parsed URL can grow in length before elision, such as |
| 84 | // when parsing a Windows file path with missing slashes. |
| 85 | ASSERT_FALSE(testcase.output.empty()); |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 86 | float width = std::max(GetWidth(testcase.input, font_list), |
| 87 | GetWidth(testcase.output.front(), font_list)); |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 88 | |
| 89 | // Ideally, this test would iterate through all available field widths on a |
| 90 | // per-pixel basis, but this is slow. Instead, compute the next input field |
| 91 | // width as slightly less than the previous elided string. This approach |
| 92 | // misses coverage in cases where a smaller available width generates a |
| 93 | // longer string than some other larger available width, but the tradeoff |
| 94 | // feels acceptable. |
| 95 | int mismatches = 0; |
| 96 | const int kMaxConsecutiveMismatches = 3; |
| 97 | for (size_t i = 0; i < testcase.output.size(); i++) { |
| 98 | const auto& expected = testcase.output[i]; |
| 99 | base::string16 expected_utf16 = base::UTF8ToUTF16(expected); |
Christopher Grant | d477bee | 2018-01-24 17:08:41 | [diff] [blame] | 100 | base::string16 elided = url_formatter::ElideUrl(url, font_list, width, |
| 101 | gfx::Typesetter::BROWSER); |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 102 | if (expected_utf16 != elided) { |
| 103 | if (i > 0 && i < testcase.output.size() - 1 && |
| 104 | mismatches < kMaxConsecutiveMismatches) { |
| 105 | mismatches++; |
| 106 | continue; |
| 107 | } |
| 108 | EXPECT_EQ(expected_utf16, elided); |
| 109 | break; |
| 110 | } |
| 111 | mismatches = 0; |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 112 | float new_width = GetWidth(elided, font_list); |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 113 | // Elision rounds fractional available widths up. |
| 114 | EXPECT_LE(new_width, std::ceil(width)) << " at " << elided; |
| 115 | width = new_width - 1.0f; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 120 | void RunElisionTest(const std::vector<Testcase>& testcases) { |
| 121 | const gfx::FontList font_list; |
| 122 | for (const auto& testcase : testcases) { |
| 123 | SCOPED_TRACE("Eliding " + testcase.input); |
| 124 | const GURL url(testcase.input); |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 125 | const float available_width = GetWidth(testcase.output, font_list); |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 126 | EXPECT_EQ(base::UTF8ToUTF16(testcase.output), |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 127 | url_formatter::ElideUrl(url, font_list, available_width, |
| 128 | gfx::Typesetter::BROWSER)); |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 132 | // Test eliding of commonplace URLs. |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 133 | TEST(TextEliderTest, TestGeneralEliding) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 134 | const std::string kEllipsisStr(gfx::kEllipsis); |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 135 | const std::vector<ProgressiveTestcase> progressive_testcases = { |
| 136 | // Elide a non-www URL (www URLs are handled differently). In this first |
| 137 | // case, elide down to nothing to test the terminal cases. |
| 138 | {"http://xyz.google.com/foo?bar", |
| 139 | { |
| 140 | /* clang-format off */ |
| 141 | "xyz.google.com/foo?bar", |
| 142 | "xyz.google.com/foo?b" + kEllipsisStr, |
| 143 | "xyz.google.com/foo?" + kEllipsisStr, |
| 144 | "xyz.google.com/foo" + kEllipsisStr, |
| 145 | "xyz.google.com/fo" + kEllipsisStr, |
| 146 | "xyz.google.com/f" + kEllipsisStr, |
| 147 | kEllipsisStr + "google.com/foo" + kEllipsisStr, |
| 148 | kEllipsisStr + "google.com/fo" + kEllipsisStr, |
| 149 | kEllipsisStr + "google.com/f" + kEllipsisStr, |
| 150 | kEllipsisStr + "google.com/" + kEllipsisStr, |
| 151 | kEllipsisStr + "google.com" + kEllipsisStr, |
| 152 | kEllipsisStr + "google.co" + kEllipsisStr, |
| 153 | kEllipsisStr + "google.c" + kEllipsisStr, |
| 154 | kEllipsisStr + "google." + kEllipsisStr, |
| 155 | kEllipsisStr + "google" + kEllipsisStr, |
| 156 | kEllipsisStr + "googl" + kEllipsisStr, |
| 157 | kEllipsisStr + "goog" + kEllipsisStr, |
| 158 | kEllipsisStr + "goo" + kEllipsisStr, |
| 159 | kEllipsisStr + "go" + kEllipsisStr, |
| 160 | kEllipsisStr + "g" + kEllipsisStr, |
| 161 | kEllipsisStr + kEllipsisStr, |
| 162 | kEllipsisStr, |
| 163 | "" |
| 164 | /* clang-format on */ |
| 165 | }}, |
| 166 | // The trailing directory name is preserved |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 167 | {"http://www.google.com/intl/en/ads/", |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 168 | { |
| 169 | /* clang-format off */ |
| 170 | "www.google.com/intl/en/ads/", |
| 171 | "google.com/intl/en/ads/", |
| 172 | "google.com/intl/" + kEllipsisStr + "/ads/", |
| 173 | "google.com/" + kEllipsisStr + "/ads/", |
| 174 | "google.com/" + kEllipsisStr + "/ad" + kEllipsisStr, |
| 175 | "google.com/" + kEllipsisStr + "/a" + kEllipsisStr, |
| 176 | "google.com/intl/e" + kEllipsisStr, |
| 177 | "google.com/intl/" + kEllipsisStr, |
| 178 | "google.com/intl" + kEllipsisStr, |
| 179 | "google.com/int" + kEllipsisStr, |
| 180 | "google.com/in" + kEllipsisStr, |
| 181 | "google.com/i" + kEllipsisStr, |
| 182 | "google.com/" + kEllipsisStr, |
| 183 | "google.com" + kEllipsisStr, |
| 184 | "google.co" + kEllipsisStr, |
| 185 | "google.c" + kEllipsisStr, |
| 186 | "google." + kEllipsisStr, |
| 187 | "google" + kEllipsisStr, |
| 188 | "googl" + kEllipsisStr, |
| 189 | /* clang-format on */ |
| 190 | }}, |
| 191 | // Subdomain is completely elided if the last path element doesn't fit. |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 192 | {"https://subdomain.foo.com/bar/filename.html", |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 193 | { |
| 194 | /* clang-format off */ |
| 195 | "https://subdomain.foo.com/bar/filename.html", |
| 196 | "subdomain.foo.com/bar/filename.html", |
| 197 | "subdomain.foo.com/" + kEllipsisStr + "/filename.html", |
| 198 | kEllipsisStr + "foo.com/bar/filename.html", |
| 199 | kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html", |
| 200 | /* clang-format on */ |
| 201 | }}, |
| 202 | // Path eliding works when a query is present. |
| 203 | {"http://www.g.com/subdir/ads/?query", |
| 204 | { |
| 205 | /* clang-format off */ |
| 206 | "www.g.com/subdir/ads/?query", |
| 207 | "www.g.com/subdir/ads/?que" + kEllipsisStr, |
| 208 | "www.g.com/subdir/ads/?qu" + kEllipsisStr, |
| 209 | "www.g.com/subdir/ads/?q" + kEllipsisStr, |
| 210 | "www.g.com/subdir/ads/?" + kEllipsisStr, |
| 211 | "www.g.com/subdir/ads/" + kEllipsisStr, |
| 212 | "www.g.com/subdir/ads" + kEllipsisStr, |
| 213 | "www.g.com/subdir/ad" + kEllipsisStr, |
| 214 | /* clang-format on */ |
| 215 | }}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 216 | }; |
Christopher Grant | d477bee | 2018-01-24 17:08:41 | [diff] [blame] | 217 | RunProgressiveElisionTest(progressive_testcases); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // When there is very little space available, the elision code will shorten |
| 221 | // both path AND file name to an ellipsis - ".../...". To avoid this result, |
| 222 | // there is a hack in place that simply treats them as one string in this |
| 223 | // case. |
| 224 | TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 225 | const std::string kEllipsisStr(gfx::kEllipsis); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 226 | |
| 227 | // Very little space, would cause double ellipsis. |
| 228 | gfx::FontList font_list; |
| 229 | GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 230 | float available_width = GetWidth( |
| 231 | "battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr, font_list); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 232 | |
| 233 | // Create the expected string, after elision. Depending on font size, the |
| 234 | // directory might become /dir... or /di... or/d... - it never should be |
| 235 | // shorter than that. (If it is, the font considers d... to be longer |
| 236 | // than .../... - that should never happen). |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 237 | ASSERT_GT(GetWidth(kEllipsisStr + "/" + kEllipsisStr, font_list), |
| 238 | GetWidth("d" + kEllipsisStr, font_list)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 239 | GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 240 | base::string16 expected = url_formatter::ElideUrl( |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 241 | long_url, font_list, available_width, gfx::Typesetter::BROWSER); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 242 | // Ensure that the expected result still contains part of the directory name. |
| 243 | ASSERT_GT(expected.length(), std::string("battersbox.com/d").length()); |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 244 | EXPECT_EQ(expected, url_formatter::ElideUrl(url, font_list, available_width, |
| 245 | gfx::Typesetter::BROWSER)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 246 | |
Matt Giuca | 49fcda17 | 2017-08-29 07:16:12 | [diff] [blame] | 247 | // Regression test for https://crbug.com/756717. An empty path, eliding to a |
| 248 | // width in between the full domain ("www.angelfire.lycos.com") and a bit |
| 249 | // longer than the ETLD+1 ("…lycos.com…/…UV"). This previously crashed due to |
| 250 | // the path being empty. |
| 251 | url = GURL("http://www.angelfire.lycos.com/"); |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 252 | available_width = GetWidth(kEllipsisStr + "angelfire.lycos.com", font_list); |
Matt Giuca | 49fcda17 | 2017-08-29 07:16:12 | [diff] [blame] | 253 | EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + "lycos.com"), |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 254 | url_formatter::ElideUrl(url, font_list, available_width, |
| 255 | gfx::Typesetter::BROWSER)); |
Matt Giuca | 49fcda17 | 2017-08-29 07:16:12 | [diff] [blame] | 256 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 257 | // More space available - elide directories, partially elide filename. |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 258 | const std::vector<Testcase> testcases = { |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 259 | {"http://battersbox.com/directory/foo/peter_paul_and_mary.html", |
| 260 | "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr}, |
| 261 | }; |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 262 | RunElisionTest(testcases); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | // Test eliding of empty strings, URLs with ports, passwords, queries, etc. |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 266 | TEST(TextEliderTest, TestElisionSpecialCases) { |
kulshin | ce612eb | 2016-06-29 18:46:51 | [diff] [blame] | 267 | #if defined(OS_WIN) |
| 268 | // Needed to bypass DCHECK in GetFallbackFont. |
| 269 | base::MessageLoopForUI message_loop; |
| 270 | #endif |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 271 | const std::string kEllipsisStr(gfx::kEllipsis); |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 272 | const std::vector<Testcase> testcases = { |
Matt Giuca | 49fcda17 | 2017-08-29 07:16:12 | [diff] [blame] | 273 | // URL with "www" subdomain (gets removed specially). |
| 274 | {"http://www.google.com/foo?bar", "www.google.com/foo?bar"}, |
| 275 | {"http://www.google.com/foo?bar", "google.com/foo?bar"}, |
| 276 | |
Matt Giuca | 51f68cc | 2017-07-07 01:34:57 | [diff] [blame] | 277 | // URL with no path. |
Matt Giuca | 49fcda17 | 2017-08-29 07:16:12 | [diff] [blame] | 278 | {"http://xyz.google.com", kEllipsisStr + "google.com"}, |
| 279 | {"https://xyz.google.com", kEllipsisStr + "google.com"}, |
Matt Giuca | 51f68cc | 2017-07-07 01:34:57 | [diff] [blame] | 280 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 281 | {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"}, |
| 282 | {"", ""}, |
| 283 | {"http://foo.bar..example.com...hello/test/filename.html", |
| 284 | "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"}, |
| 285 | {"http://foo.bar../", "foo.bar.."}, |
| 286 | {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"}, |
| 287 | {"http://me:[email protected]:99/foo?bar#baz", |
| 288 | "secrethost.com:99/foo?bar#baz"}, |
| 289 | {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"}, |
| 290 | {"mailto:[email protected]", "mailto:[email protected]"}, |
| 291 | {"javascript:click(0)", "javascript:click(0)"}, |
| 292 | {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", |
| 293 | "chess.eecs.berkeley.edu:4430/login/arbitfilename"}, |
| 294 | {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", |
| 295 | kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"}, |
| 296 | |
| 297 | // Unescaping. |
Eric Lawrence | 03f9a90 | 2017-12-16 04:48:11 | [diff] [blame] | 298 | {"http://www/%E4%BD%A0%E5%A5%BD?" |
| 299 | "q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0\xe4\xbd\xa0\xe4\xbd\xa0", |
| 300 | "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0" + |
Mike West | f8f6ed5 | 2017-10-09 20:58:50 | [diff] [blame] | 301 | kEllipsisStr}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 302 | |
| 303 | // Invalid unescaping for path. The ref will always be valid UTF-8. We |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 304 | // don't bother to do too many edge cases, since these are handled by the |
| 305 | // escaper unittest. |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 306 | {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", |
Eric Lawrence | 03f9a90 | 2017-12-16 04:48:11 | [diff] [blame] | 307 | "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 308 | }; |
| 309 | |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 310 | RunElisionTest(testcases); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | // Test eliding of file: URLs. |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 314 | TEST(TextEliderTest, TestFileURLEliding) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 315 | const std::string kEllipsisStr(gfx::kEllipsis); |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 316 | const std::vector<ProgressiveTestcase> progressive_testcases = { |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 317 | {"file:///C:/path1/path2/path3/filename", |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 318 | { |
| 319 | /* clang-format off */ |
| 320 | "file:///C:/path1/path2/path3/filename", |
| 321 | "C:/path1/path2/path3/filename", |
| 322 | "C:/path1/path2/" + kEllipsisStr + "/filename", |
| 323 | /* clang-format on */ |
| 324 | }}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 325 | // GURL parses "file:///C:path" differently on windows than it does on posix. |
| 326 | #if defined(OS_WIN) |
| 327 | {"file:///C:path1/path2/path3/filename", |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 328 | { |
| 329 | /* clang-format off */ |
| 330 | "file:///C:/path1/path2/path3/filename", |
| 331 | "C:/path1/path2/path3/filename", |
| 332 | "C:/path1/path2/" + kEllipsisStr + "/filename", |
| 333 | "C:/path1/" + kEllipsisStr + "/filename", |
| 334 | "C:/" + kEllipsisStr + "/filename", |
| 335 | /* clang-format on */ |
| 336 | }}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 337 | #endif // defined(OS_WIN) |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 338 | {"file://filer/foo/bar/file", |
| 339 | { |
| 340 | /* clang-format off */ |
| 341 | "file://filer/foo/bar/file", |
| 342 | "filer/foo/bar/file", |
| 343 | "filer/foo/" + kEllipsisStr + "/file", |
| 344 | "filer/" + kEllipsisStr + "/file", |
| 345 | "filer/foo" + kEllipsisStr, |
| 346 | "filer/fo" + kEllipsisStr, |
| 347 | "filer/f" + kEllipsisStr, |
| 348 | "filer/" + kEllipsisStr, |
| 349 | "filer" + kEllipsisStr, |
| 350 | "file" + kEllipsisStr, |
| 351 | /* clang-format on */ |
| 352 | }}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 353 | }; |
| 354 | |
Christopher Grant | d477bee | 2018-01-24 17:08:41 | [diff] [blame] | 355 | RunProgressiveElisionTest(progressive_testcases); |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 356 | |
| 357 | const std::vector<Testcase> testcases = { |
| 358 | // Eliding file URLs with nothing after the ':' shouldn't crash. |
| 359 | {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:", "aaa" + kEllipsisStr}, |
| 360 | {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:/", "aaa" + kEllipsisStr}, |
| 361 | }; |
| 362 | RunElisionTest(testcases); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | TEST(TextEliderTest, TestHostEliding) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 366 | const std::string kEllipsisStr(gfx::kEllipsis); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 367 | Testcase testcases[] = { |
pkl | 2affa91 | 2017-02-22 15:23:20 | [diff] [blame] | 368 | {"http://google.com", "google.com"}, |
| 369 | {"http://reallyreallyreallylongdomainname.com", |
| 370 | "reallyreallyreallylongdomainname.com"}, |
| 371 | {"http://foo", "foo"}, |
| 372 | {"http://foo.bar", "foo.bar"}, |
pkl | 2affa91 | 2017-02-22 15:23:20 | [diff] [blame] | 373 | {"http://subdomain.google.com", kEllipsisStr + ".google.com"}, |
| 374 | {"http://a.b.c.d.e.f.com", kEllipsisStr + "f.com"}, |
| 375 | {"http://subdomain.foo.bar", kEllipsisStr + "in.foo.bar"}, |
| 376 | {"http://subdomain.reallylongdomainname.com", |
| 377 | kEllipsisStr + "ain.reallylongdomainname.com"}, |
| 378 | {"http://a.b.c.d.e.f.com", kEllipsisStr + ".e.f.com"}, |
| 379 | // IDN - Greek alpha.beta.gamma.delta.epsilon.zeta.com |
| 380 | {"http://xn--mxa.xn--nxa.xn--oxa.xn--pxa.xn--qxa.xn--rxa.com", |
| 381 | kEllipsisStr + ".\xCE\xB5.\xCE\xB6.com"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | for (size_t i = 0; i < arraysize(testcases); ++i) { |
Trent Apted | 3bef7a3 | 2018-01-23 05:39:07 | [diff] [blame] | 385 | // Note this does not use GetWidth(), so typesetting will be done with |
| 386 | // gfx::Typesetter::DEFAULT. ElideHost() supports either typesetter on Mac. |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 387 | const float available_width = gfx::GetStringWidthF( |
| 388 | base::UTF8ToUTF16(testcases[i].output), gfx::FontList()); |
| 389 | EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output), |
| 390 | url_formatter::ElideHost(GURL(testcases[i].input), |
| 391 | gfx::FontList(), available_width)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // Trying to elide to a really short length will still keep the full TLD+1 |
| 395 | EXPECT_EQ( |
| 396 | base::ASCIIToUTF16("google.com"), |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 397 | url_formatter::ElideHost(GURL("http://google.com"), gfx::FontList(), 2)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 398 | EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"), |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 399 | url_formatter::ElideHost(GURL("http://subdomain.google.com"), |
| 400 | gfx::FontList(), 2)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 401 | EXPECT_EQ( |
| 402 | base::ASCIIToUTF16("foo.bar"), |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 403 | url_formatter::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | #endif // !defined(OS_ANDROID) |
| 407 | |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 408 | struct OriginTestData { |
| 409 | const char* const description; |
| 410 | const char* const input; |
| 411 | const wchar_t* const output; |
| 412 | const wchar_t* const output_omit_web_scheme; |
| 413 | const wchar_t* const output_omit_cryptographic_scheme; |
| 414 | }; |
| 415 | |
| 416 | // Common test data for both FormatUrlForSecurityDisplay() and |
| 417 | // FormatOriginForSecurityDisplay() |
| 418 | const OriginTestData common_tests[] = { |
| 419 | {"Empty URL", "", L"", L"", L""}, |
| 420 | {"HTTP URL", "http://www.google.com/", L"http://www.google.com", |
| 421 | L"www.google.com", L"http://www.google.com"}, |
| 422 | {"HTTPS URL", "https://www.google.com/", L"https://www.google.com", |
| 423 | L"www.google.com", L"www.google.com"}, |
| 424 | {"Standard HTTP port", "http://www.google.com:80/", |
| 425 | L"http://www.google.com", L"www.google.com", L"http://www.google.com"}, |
| 426 | {"Standard HTTPS port", "https://www.google.com:443/", |
| 427 | L"https://www.google.com", L"www.google.com", L"www.google.com"}, |
| 428 | {"Standard HTTP port, IDN Chinese", |
| 429 | "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80", |
jshin | 78809c4d8 | 2016-10-06 20:15:45 | [diff] [blame] | 430 | L"http://\x4e2d\x56fd.icom.museum", L"\x4e2d\x56fd.icom.museum", |
| 431 | L"http://\x4e2d\x56fd.icom.museum"}, |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 432 | {"HTTP URL, IDN Hebrew (RTL)", |
| 433 | "http://" |
| 434 | "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d." |
| 435 | "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/", |
| 436 | L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum", |
| 437 | L"xn--4dbklr2c8d.xn--4dbrk0ce.museum", |
| 438 | L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum"}, |
| 439 | {"HTTP URL with query string, IDN Arabic (RTL)", |
| 440 | "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no", |
| 441 | L"http://xn--wgbh1c.icom.museum", L"xn--wgbh1c.icom.museum", |
| 442 | L"http://xn--wgbh1c.icom.museum"}, |
| 443 | {"Non-standard HTTP port", "http://www.google.com:9000/", |
| 444 | L"http://www.google.com:9000", L"www.google.com:9000", |
| 445 | L"http://www.google.com:9000"}, |
| 446 | {"Non-standard HTTPS port", "https://www.google.com:9000/", |
| 447 | L"https://www.google.com:9000", L"www.google.com:9000", |
| 448 | L"www.google.com:9000"}, |
| 449 | {"HTTP URL with path", "http://www.google.com/test.html", |
| 450 | L"http://www.google.com", L"www.google.com", L"http://www.google.com"}, |
| 451 | {"HTTPS URL with path", "https://www.google.com/test.html", |
| 452 | L"https://www.google.com", L"www.google.com", L"www.google.com"}, |
| 453 | {"Unusual secure scheme (wss)", "wss://www.google.com/", |
| 454 | L"wss://www.google.com", L"wss://www.google.com", L"www.google.com"}, |
| 455 | {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/", |
| 456 | L"gopher://www.google.com", L"gopher://www.google.com", |
| 457 | L"gopher://www.google.com"}, |
| 458 | {"Unlisted scheme (chrome)", "chrome://version", L"chrome://version", |
| 459 | L"chrome://version", L"chrome://version"}, |
| 460 | {"HTTP IP address", "http://173.194.65.103", L"http://173.194.65.103", |
| 461 | L"173.194.65.103", L"http://173.194.65.103"}, |
| 462 | {"HTTPS IP address", "https://173.194.65.103", L"https://173.194.65.103", |
| 463 | L"173.194.65.103", L"173.194.65.103"}, |
| 464 | {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/", |
| 465 | L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]", |
| 466 | L"http://[fe80::202:b3ff:fe1e:8329]"}, |
| 467 | {"HTTPs IPv6 address", "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/", |
| 468 | L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]", |
| 469 | L"[fe80::202:b3ff:fe1e:8329]"}, |
| 470 | {"HTTP IPv6 address with port", |
| 471 | "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:80/", |
| 472 | L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]", |
| 473 | L"http://[fe80::202:b3ff:fe1e:8329]"}, |
| 474 | {"HTTPs IPv6 address with port", |
| 475 | "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:443/", |
| 476 | L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]", |
| 477 | L"[fe80::202:b3ff:fe1e:8329]"}, |
| 478 | {"HTTPS IP address, non-default port", "https://173.194.65.103:8443", |
| 479 | L"https://173.194.65.103:8443", L"173.194.65.103:8443", |
| 480 | L"173.194.65.103:8443"}, |
| 481 | {"Invalid host 1", "https://www.cyber../wow.php", L"https://www.cyber..", |
| 482 | L"www.cyber..", L"www.cyber.."}, |
| 483 | {"Invalid host 2", "https://www...cyber/wow.php", L"https://www...cyber", |
| 484 | L"www...cyber", L"www...cyber"}, |
| 485 | {"Invalid port 3", "https://173.194.65.103:/hello.aspx", |
| 486 | L"https://173.194.65.103", L"173.194.65.103", L"173.194.65.103"}, |
| 487 | {"Trailing dot in DNS name", "https://www.example.com./get/goat", |
| 488 | L"https://www.example.com.", L"www.example.com.", L"www.example.com."}}; |
| 489 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 490 | TEST(TextEliderTest, FormatUrlForSecurityDisplay) { |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 491 | for (size_t i = 0; i < arraysize(common_tests); ++i) { |
| 492 | base::string16 formatted = |
| 493 | url_formatter::FormatUrlForSecurityDisplay(GURL(common_tests[i].input)); |
| 494 | EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted) |
| 495 | << common_tests[i].description; |
| 496 | |
| 497 | base::string16 formatted_omit_web_scheme = |
| 498 | url_formatter::FormatUrlForSecurityDisplay( |
| 499 | GURL(common_tests[i].input), |
| 500 | url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 501 | EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme), |
| 502 | formatted_omit_web_scheme) |
| 503 | << common_tests[i].description; |
| 504 | |
| 505 | base::string16 formatted_omit_cryptographic_scheme = |
| 506 | url_formatter::FormatUrlForSecurityDisplay( |
| 507 | GURL(common_tests[i].input), |
| 508 | url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
| 509 | EXPECT_EQ( |
| 510 | base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme), |
| 511 | formatted_omit_cryptographic_scheme) |
| 512 | << common_tests[i].description; |
| 513 | } |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 514 | |
| 515 | const OriginTestData tests[] = { |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 516 | {"File URI", "file:///usr/example/file.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 517 | L"file:///usr/example/file.html", L"file:///usr/example/file.html", |
| 518 | L"file:///usr/example/file.html"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 519 | {"File URI with hostname", "file://localhost/usr/example/file.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 520 | L"file:///usr/example/file.html", L"file:///usr/example/file.html", |
| 521 | L"file:///usr/example/file.html"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 522 | {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls", |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 523 | L"file:///CONTOSO/accounting/money.xls", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 524 | L"file:///CONTOSO/accounting/money.xls", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 525 | L"file:///CONTOSO/accounting/money.xls"}, |
| 526 | {"UNC File URI 2", |
| 527 | "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO", |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 528 | L"file:///C:/Program%20Files/Music/Web%20Sys/main.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 529 | L"file:///C:/Program%20Files/Music/Web%20Sys/main.html", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 530 | L"file:///C:/Program%20Files/Music/Web%20Sys/main.html"}, |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 531 | {"Invalid IPv6 address", "https://[2001:db8:0:1]/", |
| 532 | L"https://[2001:db8:0:1]", L"https://[2001:db8:0:1]", |
| 533 | L"https://[2001:db8:0:1]"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 534 | {"HTTP filesystem: URL with path", |
| 535 | "filesystem:http://www.google.com/temporary/test.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 536 | L"filesystem:http://www.google.com", L"filesystem:http://www.google.com", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 537 | L"filesystem:http://www.google.com"}, |
| 538 | {"File filesystem: URL with path", |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 539 | "filesystem:file://localhost/temporary/stuff/" |
| 540 | "test.html?z=fun&goat=billy", |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 541 | L"filesystem:file:///temporary/stuff/test.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 542 | L"filesystem:file:///temporary/stuff/test.html", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 543 | L"filesystem:file:///temporary/stuff/test.html"}, |
| 544 | {"Invalid scheme 1", "twelve://www.cyber.org/wow.php", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 545 | L"twelve://www.cyber.org/wow.php", L"twelve://www.cyber.org/wow.php", |
| 546 | L"twelve://www.cyber.org/wow.php"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 547 | {"Invalid scheme 2", "://www.cyber.org/wow.php", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 548 | L"://www.cyber.org/wow.php", L"://www.cyber.org/wow.php", |
| 549 | L"://www.cyber.org/wow.php"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 550 | {"Invalid port 1", "https://173.194.65.103:000", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 551 | L"https://173.194.65.103:0", L"173.194.65.103:0", L"173.194.65.103:0"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 552 | {"Invalid port 2", "https://173.194.65.103:gruffle", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 553 | L"https://173.194.65.103:gruffle", L"https://173.194.65.103:gruffle", |
| 554 | L"https://173.194.65.103:gruffle"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 555 | {"Blob URL", |
nick | 1b17dc3 | 2016-04-15 21:34:04 | [diff] [blame] | 556 | "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
| 557 | L"blob:http://www.html5rocks.com/" |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 558 | L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
nick | 1b17dc3 | 2016-04-15 21:34:04 | [diff] [blame] | 559 | L"blob:http://www.html5rocks.com/" |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 560 | L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
| 561 | L"blob:http://www.html5rocks.com/" |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 562 | L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}}; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 563 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 564 | for (size_t i = 0; i < arraysize(tests); ++i) { |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 565 | base::string16 formatted = |
| 566 | url_formatter::FormatUrlForSecurityDisplay(GURL(tests[i].input)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 567 | EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted) |
| 568 | << tests[i].description; |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 569 | |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 570 | base::string16 formatted_omit_web_scheme = |
| 571 | url_formatter::FormatUrlForSecurityDisplay( |
| 572 | GURL(tests[i].input), |
| 573 | url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 574 | EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme), |
| 575 | formatted_omit_web_scheme) |
| 576 | << tests[i].description; |
| 577 | |
| 578 | base::string16 formatted_omit_cryptographic_scheme = |
| 579 | url_formatter::FormatUrlForSecurityDisplay( |
| 580 | GURL(tests[i].input), |
| 581 | url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
| 582 | EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme), |
| 583 | formatted_omit_cryptographic_scheme) |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 584 | << tests[i].description; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 585 | } |
| 586 | |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 587 | base::string16 formatted = url_formatter::FormatUrlForSecurityDisplay(GURL()); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 588 | EXPECT_EQ(base::string16(), formatted) |
| 589 | << "Explicitly test the 0-argument GURL constructor"; |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 590 | |
| 591 | base::string16 formatted_omit_scheme = |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 592 | url_formatter::FormatUrlForSecurityDisplay( |
| 593 | GURL(), url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 594 | EXPECT_EQ(base::string16(), formatted_omit_scheme) |
| 595 | << "Explicitly test the 0-argument GURL constructor"; |
| 596 | |
| 597 | formatted_omit_scheme = url_formatter::FormatUrlForSecurityDisplay( |
| 598 | GURL(), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 599 | EXPECT_EQ(base::string16(), formatted_omit_scheme) |
| 600 | << "Explicitly test the 0-argument GURL constructor"; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 601 | } |
| 602 | |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 603 | TEST(TextEliderTest, FormatOriginForSecurityDisplay) { |
| 604 | for (size_t i = 0; i < arraysize(common_tests); ++i) { |
| 605 | base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 606 | url::Origin::Create(GURL(common_tests[i].input))); |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 607 | EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted) |
| 608 | << common_tests[i].description; |
| 609 | |
| 610 | base::string16 formatted_omit_web_scheme = |
| 611 | url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 612 | url::Origin::Create(GURL(common_tests[i].input)), |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 613 | url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 614 | EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme), |
| 615 | formatted_omit_web_scheme) |
| 616 | << common_tests[i].description; |
| 617 | |
| 618 | base::string16 formatted_omit_cryptographic_scheme = |
| 619 | url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 620 | url::Origin::Create(GURL(common_tests[i].input)), |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 621 | url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
| 622 | EXPECT_EQ( |
| 623 | base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme), |
| 624 | formatted_omit_cryptographic_scheme) |
| 625 | << common_tests[i].description; |
| 626 | } |
| 627 | |
| 628 | const OriginTestData tests[] = { |
| 629 | {"File URI", "file:///usr/example/file.html", L"file://", L"file://", |
| 630 | L"file://"}, |
| 631 | {"File URI with hostname", "file://localhost/usr/example/file.html", |
| 632 | L"file://localhost", L"file://localhost", L"file://localhost"}, |
| 633 | {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls", L"file://", |
| 634 | L"file://", L"file://"}, |
| 635 | {"UNC File URI 2", |
| 636 | "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO", |
| 637 | L"file://", L"file://", L"file://"}, |
| 638 | {"Invalid IPv6 address", "https://[2001:db8:0:1]/", L"", L"", L""}, |
| 639 | {"HTTP filesystem: URL with path", |
| 640 | "filesystem:http://www.google.com/temporary/test.html", |
| 641 | L"http://www.google.com", L"www.google.com", L"http://www.google.com"}, |
| 642 | {"File filesystem: URL with path", |
| 643 | "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy", |
| 644 | L"file://", L"file://", L"file://"}, |
| 645 | {"Invalid scheme 1", "twelve://www.cyber.org/wow.php", L"", L"", L""}, |
| 646 | {"Invalid scheme 2", "://www.cyber.org/wow.php", L"", L"", L""}, |
| 647 | {"Invalid port 1", "https://173.194.65.103:000", L"", L"", L""}, |
| 648 | {"Invalid port 2", "https://173.194.65.103:gruffle", L"", L"", L""}, |
| 649 | {"Blob URL", |
| 650 | "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
| 651 | L"http://www.html5rocks.com", L"www.html5rocks.com", |
| 652 | L"http://www.html5rocks.com"}}; |
| 653 | |
| 654 | for (size_t i = 0; i < arraysize(tests); ++i) { |
| 655 | base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 656 | url::Origin::Create(GURL(tests[i].input))); |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 657 | EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted) |
| 658 | << tests[i].description; |
| 659 | |
| 660 | base::string16 formatted_omit_web_scheme = |
| 661 | url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 662 | url::Origin::Create(GURL(tests[i].input)), |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 663 | url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 664 | EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme), |
| 665 | formatted_omit_web_scheme) |
| 666 | << tests[i].description; |
| 667 | |
| 668 | base::string16 formatted_omit_cryptographic_scheme = |
| 669 | url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 670 | url::Origin::Create(GURL(tests[i].input)), |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 671 | url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
| 672 | EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme), |
| 673 | formatted_omit_cryptographic_scheme) |
| 674 | << tests[i].description; |
| 675 | } |
| 676 | |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 677 | base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay( |
| 678 | url::Origin::Create(GURL())); |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 679 | EXPECT_EQ(base::string16(), formatted) |
| 680 | << "Explicitly test the url::Origin which takes an empty, invalid URL"; |
| 681 | |
| 682 | base::string16 formatted_omit_scheme = |
| 683 | url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 684 | url::Origin::Create(GURL()), |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 685 | url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 686 | EXPECT_EQ(base::string16(), formatted_omit_scheme) |
| 687 | << "Explicitly test the url::Origin which takes an empty, invalid URL"; |
| 688 | |
| 689 | formatted_omit_scheme = url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 690 | url::Origin::Create(GURL()), |
| 691 | url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 692 | EXPECT_EQ(base::string16(), formatted_omit_scheme) |
| 693 | << "Explicitly test the url::Origin which takes an empty, invalid URL"; |
| 694 | } |
| 695 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 696 | } // namespace |