| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Unit tests for eliding and formatting utility functions. | 5 // Unit tests for eliding and formatting utility functions. |
| 6 | 6 |
| 7 #include "ui/gfx/text_elider.h" | 7 #include "ui/gfx/text_elider.h" |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "ui/gfx/font.h" | 15 #include "ui/gfx/font.h" |
| 16 #include "ui/gfx/font_list.h" | 16 #include "ui/gfx/font_list.h" |
| 17 #include "ui/gfx/text_utils.h" | 17 #include "ui/gfx/text_utils.h" |
| 18 #include "url/gurl.h" | |
| 19 | 18 |
| 20 using base::ASCIIToUTF16; | 19 using base::ASCIIToUTF16; |
| 21 using base::UTF16ToUTF8; | 20 using base::UTF16ToUTF8; |
| 22 using base::UTF16ToWide; | 21 using base::UTF16ToWide; |
| 23 using base::UTF8ToUTF16; | 22 using base::UTF8ToUTF16; |
| 24 using base::WideToUTF16; | 23 using base::WideToUTF16; |
| 25 | 24 |
| 26 namespace gfx { | 25 namespace gfx { |
| 27 | 26 |
| 28 namespace { | 27 namespace { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 41 const base::string16 input; | 40 const base::string16 input; |
| 42 const base::string16 output; | 41 const base::string16 output; |
| 43 }; | 42 }; |
| 44 | 43 |
| 45 struct TestData { | 44 struct TestData { |
| 46 const std::string a; | 45 const std::string a; |
| 47 const std::string b; | 46 const std::string b; |
| 48 const int compare_result; | 47 const int compare_result; |
| 49 }; | 48 }; |
| 50 | 49 |
| 51 void RunUrlTest(Testcase* testcases, size_t num_testcases) { | |
| 52 static const FontList font_list; | |
| 53 for (size_t i = 0; i < num_testcases; ++i) { | |
| 54 const GURL url(testcases[i].input); | |
| 55 // Should we test with non-empty language list? | |
| 56 // That's kinda redundant with net_util_unittests. | |
| 57 const float available_width = | |
| 58 GetStringWidthF(UTF8ToUTF16(testcases[i].output), font_list); | |
| 59 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), | |
| 60 ElideUrl(url, font_list, available_width, std::string())); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 } // namespace | 50 } // namespace |
| 65 | 51 |
| 66 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF | 52 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF |
| 67 // that calls [NSString sizeWithFont] returns the rounded string width. | 53 // that calls [NSString sizeWithFont] returns the rounded string width. |
| 68 // TODO(338784): Enable this on android. | 54 // TODO(338784): Enable this on android. |
| 69 #if defined(OS_IOS) || defined(OS_ANDROID) | 55 #if defined(OS_IOS) || defined(OS_ANDROID) |
| 70 #define MAYBE_ElideEmail DISABLED_ElideEmail | 56 #define MAYBE_ElideEmail DISABLED_ElideEmail |
| 71 #else | 57 #else |
| 72 #define MAYBE_ElideEmail ElideEmail | 58 #define MAYBE_ElideEmail ElideEmail |
| 73 #endif | 59 #endif |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 const int test_width = | 137 const int test_width = |
| 152 font_list.GetExpectedTextWidth(test_width_factors[i]); | 138 font_list.GetExpectedTextWidth(test_width_factors[i]); |
| 153 for (size_t j = 0; j < arraysize(test_emails); ++j) { | 139 for (size_t j = 0; j < arraysize(test_emails); ++j) { |
| 154 // Extra space is available: the email should not be elided. | 140 // Extra space is available: the email should not be elided. |
| 155 const base::string16 test_email = UTF8ToUTF16(test_emails[j]); | 141 const base::string16 test_email = UTF8ToUTF16(test_emails[j]); |
| 156 EXPECT_EQ(test_email, ElideEmail(test_email, font_list, test_width)); | 142 EXPECT_EQ(test_email, ElideEmail(test_email, font_list, test_width)); |
| 157 } | 143 } |
| 158 } | 144 } |
| 159 } | 145 } |
| 160 | 146 |
| 161 // Test eliding of commonplace URLs. | |
| 162 // TODO(338784): Enable this on android. | |
| 163 #if defined(OS_ANDROID) | |
| 164 #define MAYBE_TestGeneralEliding DISABLED_TestGeneralEliding | |
| 165 #else | |
| 166 #define MAYBE_TestGeneralEliding TestGeneralEliding | |
| 167 #endif | |
| 168 TEST(TextEliderTest, MAYBE_TestGeneralEliding) { | |
| 169 const std::string kEllipsisStr(kEllipsis); | |
| 170 Testcase testcases[] = { | |
| 171 {"http://www.google.com/intl/en/ads/", | |
| 172 "www.google.com/intl/en/ads/"}, | |
| 173 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, | |
| 174 {"http://www.google.com/intl/en/ads/", | |
| 175 "google.com/intl/" + kEllipsisStr + "/ads/"}, | |
| 176 {"http://www.google.com/intl/en/ads/", | |
| 177 "google.com/" + kEllipsisStr + "/ads/"}, | |
| 178 {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr}, | |
| 179 {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr}, | |
| 180 {"https://subdomain.foo.com/bar/filename.html", | |
| 181 "subdomain.foo.com/bar/filename.html"}, | |
| 182 {"https://subdomain.foo.com/bar/filename.html", | |
| 183 "subdomain.foo.com/" + kEllipsisStr + "/filename.html"}, | |
| 184 {"http://subdomain.foo.com/bar/filename.html", | |
| 185 kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"}, | |
| 186 {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired", | |
| 187 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr}, | |
| 188 }; | |
| 189 | |
| 190 RunUrlTest(testcases, arraysize(testcases)); | |
| 191 } | |
| 192 | |
| 193 // When there is very little space available, the elision code will shorten | |
| 194 // both path AND file name to an ellipsis - ".../...". To avoid this result, | |
| 195 // there is a hack in place that simply treats them as one string in this | |
| 196 // case. | |
| 197 // TODO(338784): Enable this on android. | |
| 198 #if defined(OS_ANDROID) | |
| 199 #define MAYBE_TestTrailingEllipsisSlashEllipsisHack \ | |
| 200 DISABLED_TestTrailingEllipsisSlashEllipsisHack | |
| 201 #else | |
| 202 #define MAYBE_TestTrailingEllipsisSlashEllipsisHack \ | |
| 203 TestTrailingEllipsisSlashEllipsisHack | |
| 204 #endif | |
| 205 TEST(TextEliderTest, MAYBE_TestTrailingEllipsisSlashEllipsisHack) { | |
| 206 const std::string kEllipsisStr(kEllipsis); | |
| 207 | |
| 208 // Very little space, would cause double ellipsis. | |
| 209 FontList font_list; | |
| 210 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); | |
| 211 float available_width = GetStringWidthF( | |
| 212 UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr), | |
| 213 font_list); | |
| 214 | |
| 215 // Create the expected string, after elision. Depending on font size, the | |
| 216 // directory might become /dir... or /di... or/d... - it never should be | |
| 217 // shorter than that. (If it is, the font considers d... to be longer | |
| 218 // than .../... - that should never happen). | |
| 219 ASSERT_GT(GetStringWidthF(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr), | |
| 220 font_list), | |
| 221 GetStringWidthF(UTF8ToUTF16("d" + kEllipsisStr), font_list)); | |
| 222 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); | |
| 223 base::string16 expected = | |
| 224 ElideUrl(long_url, font_list, available_width, std::string()); | |
| 225 // Ensure that the expected result still contains part of the directory name. | |
| 226 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length()); | |
| 227 EXPECT_EQ(expected, | |
| 228 ElideUrl(url, font_list, available_width, std::string())); | |
| 229 | |
| 230 // More space available - elide directories, partially elide filename. | |
| 231 Testcase testcases[] = { | |
| 232 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html", | |
| 233 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr}, | |
| 234 }; | |
| 235 RunUrlTest(testcases, arraysize(testcases)); | |
| 236 } | |
| 237 | |
| 238 // Test eliding of empty strings, URLs with ports, passwords, queries, etc. | |
| 239 // TODO(338784): Enable this on android. | |
| 240 #if defined(OS_ANDROID) | |
| 241 #define MAYBE_TestMoreEliding DISABLED_TestMoreEliding | |
| 242 #else | |
| 243 #define MAYBE_TestMoreEliding TestMoreEliding | |
| 244 #endif | |
| 245 TEST(TextEliderTest, MAYBE_TestMoreEliding) { | |
| 246 const std::string kEllipsisStr(kEllipsis); | |
| 247 Testcase testcases[] = { | |
| 248 {"http://www.google.com/foo?bar", "www.google.com/foo?bar"}, | |
| 249 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo?" + kEllipsisStr}, | |
| 250 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo" + kEllipsisStr}, | |
| 251 {"http://xyz.google.com/foo?bar", "xyz.google.com/fo" + kEllipsisStr}, | |
| 252 {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"}, | |
| 253 {"", ""}, | |
| 254 {"http://foo.bar..example.com...hello/test/filename.html", | |
| 255 "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"}, | |
| 256 {"http://foo.bar../", "foo.bar.."}, | |
| 257 {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"}, | |
| 258 {"http://me:mypass@secrethost.com:99/foo?bar#baz", | |
| 259 "secrethost.com:99/foo?bar#baz"}, | |
| 260 {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"}, | |
| 261 {"mailto:elgoato@elgoato.com", "mailto:elgoato@elgoato.com"}, | |
| 262 {"javascript:click(0)", "javascript:click(0)"}, | |
| 263 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", | |
| 264 "chess.eecs.berkeley.edu:4430/login/arbitfilename"}, | |
| 265 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", | |
| 266 kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"}, | |
| 267 | |
| 268 // Unescaping. | |
| 269 {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", | |
| 270 "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"}, | |
| 271 | |
| 272 // Invalid unescaping for path. The ref will always be valid UTF-8. We don't | |
| 273 // bother to do too many edge cases, since these are handled by the escaper | |
| 274 // unittest. | |
| 275 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", | |
| 276 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"}, | |
| 277 }; | |
| 278 | |
| 279 RunUrlTest(testcases, arraysize(testcases)); | |
| 280 } | |
| 281 | |
| 282 // Test eliding of file: URLs. | |
| 283 // TODO(338784): Enable this on android. | |
| 284 #if defined(OS_ANDROID) | |
| 285 #define MAYBE_TestFileURLEliding DISABLED_TestFileURLEliding | |
| 286 #else | |
| 287 #define MAYBE_TestFileURLEliding TestFileURLEliding | |
| 288 #endif | |
| 289 TEST(TextEliderTest, MAYBE_TestFileURLEliding) { | |
| 290 const std::string kEllipsisStr(kEllipsis); | |
| 291 Testcase testcases[] = { | |
| 292 {"file:///C:/path1/path2/path3/filename", | |
| 293 "file:///C:/path1/path2/path3/filename"}, | |
| 294 {"file:///C:/path1/path2/path3/filename", | |
| 295 "C:/path1/path2/path3/filename"}, | |
| 296 // GURL parses "file:///C:path" differently on windows than it does on posix. | |
| 297 #if defined(OS_WIN) | |
| 298 {"file:///C:path1/path2/path3/filename", | |
| 299 "C:/path1/path2/" + kEllipsisStr + "/filename"}, | |
| 300 {"file:///C:path1/path2/path3/filename", | |
| 301 "C:/path1/" + kEllipsisStr + "/filename"}, | |
| 302 {"file:///C:path1/path2/path3/filename", | |
| 303 "C:/" + kEllipsisStr + "/filename"}, | |
| 304 #endif | |
| 305 {"file://filer/foo/bar/file", "filer/foo/bar/file"}, | |
| 306 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"}, | |
| 307 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"}, | |
| 308 }; | |
| 309 | |
| 310 RunUrlTest(testcases, arraysize(testcases)); | |
| 311 } | |
| 312 | |
| 313 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF | 147 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF |
| 314 // that calls [NSString sizeWithFont] returns the rounded string width. | 148 // that calls [NSString sizeWithFont] returns the rounded string width. |
| 315 // TODO(338784): Enable this on android. | 149 // TODO(338784): Enable this on android. |
| 316 #if defined(OS_IOS) || defined(OS_ANDROID) | 150 #if defined(OS_IOS) || defined(OS_ANDROID) |
| 317 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding | 151 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding |
| 318 #else | 152 #else |
| 319 #define MAYBE_TestFilenameEliding TestFilenameEliding | 153 #define MAYBE_TestFilenameEliding TestFilenameEliding |
| 320 #endif | 154 #endif |
| 321 TEST(TextEliderTest, MAYBE_TestFilenameEliding) { | 155 TEST(TextEliderTest, MAYBE_TestFilenameEliding) { |
| 322 const std::string kEllipsisStr(kEllipsis); | 156 const std::string kEllipsisStr(kEllipsis); |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 testcases_middle[i].input, | 380 testcases_middle[i].input, |
| 547 font_list, | 381 font_list, |
| 548 GetStringWidthF(testcases_middle[i].output, font_list), | 382 GetStringWidthF(testcases_middle[i].output, font_list), |
| 549 ELIDE_AT_END).size()); | 383 ELIDE_AT_END).size()); |
| 550 EXPECT_EQ(kEllipsisStr, | 384 EXPECT_EQ(kEllipsisStr, |
| 551 ElideText(testcases_middle[i].input, font_list, ellipsis_width, | 385 ElideText(testcases_middle[i].input, font_list, ellipsis_width, |
| 552 ELIDE_AT_END)); | 386 ELIDE_AT_END)); |
| 553 } | 387 } |
| 554 } | 388 } |
| 555 | 389 |
| 556 // Verifies display_url is set correctly. | |
| 557 TEST(TextEliderTest, SortedDisplayURL) { | |
| 558 SortedDisplayURL d_url(GURL("http://www.google.com"), std::string()); | |
| 559 EXPECT_EQ("www.google.com", UTF16ToASCII(d_url.display_url())); | |
| 560 } | |
| 561 | |
| 562 // Verifies DisplayURL::Compare works correctly. | |
| 563 TEST(TextEliderTest, SortedDisplayURLCompare) { | |
| 564 UErrorCode create_status = U_ZERO_ERROR; | |
| 565 scoped_ptr<icu::Collator> collator( | |
| 566 icu::Collator::createInstance(create_status)); | |
| 567 if (!U_SUCCESS(create_status)) | |
| 568 return; | |
| 569 | |
| 570 TestData tests[] = { | |
| 571 // IDN comparison. Hosts equal, so compares on path. | |
| 572 { "http://xn--1lq90i.cn/a", "http://xn--1lq90i.cn/b", -1}, | |
| 573 | |
| 574 // Because the host and after host match, this compares the full url. | |
| 575 { "http://www.x/b", "http://x/b", -1 }, | |
| 576 | |
| 577 // Because the host and after host match, this compares the full url. | |
| 578 { "http://www.a:1/b", "http://a:1/b", 1 }, | |
| 579 | |
| 580 // The hosts match, so these end up comparing on the after host portion. | |
| 581 { "http://www.x:0/b", "http://x:1/b", -1 }, | |
| 582 { "http://www.x/a", "http://x/b", -1 }, | |
| 583 { "http://x/b", "http://www.x/a", 1 }, | |
| 584 | |
| 585 // Trivial Equality. | |
| 586 { "http://a/", "http://a/", 0 }, | |
| 587 | |
| 588 // Compares just hosts. | |
| 589 { "http://www.a/", "http://b/", -1 }, | |
| 590 }; | |
| 591 | |
| 592 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 593 SortedDisplayURL url1(GURL(tests[i].a), std::string()); | |
| 594 SortedDisplayURL url2(GURL(tests[i].b), std::string()); | |
| 595 EXPECT_EQ(tests[i].compare_result, url1.Compare(url2, collator.get())); | |
| 596 EXPECT_EQ(-tests[i].compare_result, url2.Compare(url1, collator.get())); | |
| 597 } | |
| 598 } | |
| 599 | |
| 600 TEST(TextEliderTest, ElideString) { | 390 TEST(TextEliderTest, ElideString) { |
| 601 struct TestData { | 391 struct TestData { |
| 602 const char* input; | 392 const char* input; |
| 603 int max_len; | 393 int max_len; |
| 604 bool result; | 394 bool result; |
| 605 const char* output; | 395 const char* output; |
| 606 } cases[] = { | 396 } cases[] = { |
| 607 { "Hello", 0, true, "" }, | 397 { "Hello", 0, true, "" }, |
| 608 { "", 0, false, "" }, | 398 { "", 0, false, "" }, |
| 609 { "Hello, my name is Tom", 1, true, "H" }, | 399 { "Hello, my name is Tom", 1, true, "H" }, |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1052 | 842 |
| 1053 // Test adds ... at right spot when there is not enough room to break at a | 843 // Test adds ... at right spot when there is not enough room to break at a |
| 1054 // word boundary. | 844 // word boundary. |
| 1055 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); | 845 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); |
| 1056 | 846 |
| 1057 // Test completely truncates string if break is on initial whitespace. | 847 // Test completely truncates string if break is on initial whitespace. |
| 1058 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); | 848 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); |
| 1059 } | 849 } |
| 1060 | 850 |
| 1061 } // namespace gfx | 851 } // namespace gfx |
| OLD | NEW |