1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Unit tests for eliding and formatting utility functions.
#include "ui/gfx/text_elider.h"
#include <stddef.h>
#include <memory>
#include <vector>
#include "base/files/file_path.h"
#include "base/i18n/rtl.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/font.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/font_render_params.h"
#include "ui/gfx/text_utils.h"
namespace gfx {
namespace {
struct StringMappingTestCase {
const std::u16string input;
const std::u16string output;
};
} // namespace
TEST(TextEliderTest, ElideEmail) {
// Test emails and their expected elided forms (from which the available
// widths will be derived).
// For elided forms in which both the username and domain must be elided:
// the result (how many characters are left on each side) can be font
// dependent. To avoid this, the username is prefixed with the characters
// expected to remain in the domain.
const auto cases = std::to_array<StringMappingTestCase>({
{u"g@g.c", u"g@g.c"},
{u"g@g.c", u"…"},
{u"ga@co.ca", u"ga@c…a"},
{u"short@small.com", u"s…@s…"},
{u"short@small.com", u"s…@small.com"},
{u"short@longbutlotsofspace.com", u"short@longbutlotsofspace.com"},
{u"short@longbutnotverymuchspace.com", u"short@long….com"},
{u"la_short@longbutverytightspace.ca", u"la…@l…a"},
{u"longusername@gmail.com", u"long…@gmail.com"},
{u"elidetothemax@justfits.com", u"e…@justfits.com"},
{u"thatom_somelongemail@thatdoesntfit.com", u"thatom…@tha…om"},
{u"namefits@butthedomaindoesnt.com", u"namefits@butthedo…snt.com"},
{u"widthtootight@nospace.com", u"…"},
{u"nospaceforusername@l", u"…"},
{u"little@littlespace.com", u"l…@l…"},
{u"l@llllllllllllllllllllllll.com", u"l@lllll….com"},
{u"messed\"up@whyanat\"++@notgoogley.com",
u"messed\"up@whyanat\"++@notgoogley.com"},
{u"messed\"up@whyanat\"++@notgoogley.com",
u"messed\"up@why…@notgoogley.com"},
{u"noca_messed\"up@whyanat\"++@notgoogley.ca", u"noca…@no…ca"},
{u"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com",
u"at\"@@@@@@@@@...@@.@.…@madness.com"},
// Special case: "m..." takes more than half of the available width; thus
// the domain must elide to "l..." and not "l...l" as it must allow enough
// space for the minimal username elision although its half of the
// available width would normally allow it to elide to "l...l".
{u"mmmmm@llllllllll", u"m…@l…"},
});
const FontList font_list;
for (const auto& c : cases) {
EXPECT_EQ(c.output,
ElideText(c.input, font_list,
GetStringWidthF(c.output, font_list), ELIDE_EMAIL));
}
}
TEST(TextEliderTest, ElideEmailMoreSpace) {
const auto test_widths_extra_spaces = std::to_array<int>({
10,
1000,
100'000,
});
const auto test_emails = std::to_array<const char16_t*>({
u"a@c",
u"test@email.com",
u"short@verysuperdupperlongdomain.com",
u"supermegalongusername@withasuperlonnnggggdomain.gouv.qc.ca",
});
const FontList font_list;
for (const auto* test_email : test_emails) {
const int mimimum_width = GetStringWidth(test_email, font_list);
for (int extra_space : test_widths_extra_spaces) {
// Extra space is available: the email should not be elided.
EXPECT_EQ(test_email,
ElideText(test_email, font_list, mimimum_width + extra_space,
ELIDE_EMAIL));
}
}
}
TEST(TextEliderTest, TestFilenameEliding) {
const base::FilePath::StringType kPathSeparator =
base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]);
struct Case {
const base::FilePath::StringType input;
const std::u16string output;
// If this value is specified, we will try to cut the path down to the
// render width of this string; if not specified, output will be used.
const std::u16string using_width_of = std::u16string();
};
const auto cases = std::to_array<Case>(
{{FILE_PATH_LITERAL(""), u""},
{FILE_PATH_LITERAL("."), u"."},
{FILE_PATH_LITERAL("filename.exe"), u"filename.exe"},
{FILE_PATH_LITERAL(".longext"), u".longext"},
{FILE_PATH_LITERAL("pie"), u"pie"},
{FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") +
kPathSeparator + FILE_PATH_LITERAL("filename.pie"),
u"filename.pie"},
{FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") +
kPathSeparator + FILE_PATH_LITERAL("longfilename.pie"),
u"long….pie"},
{FILE_PATH_LITERAL("http://path.com/filename.pie"), u"filename.pie"},
{FILE_PATH_LITERAL("http://path.com/longfilename.pie"), u"long….pie"},
{FILE_PATH_LITERAL("piesmashingtacularpants"), u"pie…"},
{FILE_PATH_LITERAL(".piesmashingtacularpants"), u".pie…"},
{FILE_PATH_LITERAL("cheese."), u"cheese."},
{FILE_PATH_LITERAL("file name.longext"), u"file….longext"},
{FILE_PATH_LITERAL("fil ename.longext"), u"fil….longext",
u"fil ….longext"},
{FILE_PATH_LITERAL("filename.longext"), u"file….longext"},
{FILE_PATH_LITERAL("filename.middleext.longext"),
u"filename.mid….longext"},
{FILE_PATH_LITERAL("filename.superduperextremelylongext"),
u"filename.sup…emelylongext"},
{FILE_PATH_LITERAL(
"filenamereallylongtext.superdeduperextremelylongext"),
u"filenamereall…emelylongext"},
{FILE_PATH_LITERAL(
"file.name.really.long.text.superduperextremelylongext"),
u"file.name.re…emelylongext"}});
static const FontList font_list;
for (const auto& c : cases) {
std::u16string using_width_of =
c.using_width_of.empty() ? c.output : c.using_width_of;
EXPECT_EQ(base::i18n::GetDisplayStringInLTRDirectionality(c.output),
ElideFilename(base::FilePath(c.input), font_list,
GetStringWidthF(using_width_of, font_list)));
}
}
TEST(TextEliderTest, ElideTextTruncate) {
const FontList font_list;
const float kTestWidth = GetStringWidthF(u"Test", font_list);
struct Case {
const char16_t* input;
float width;
const char16_t* output;
};
const auto cases = std::to_array<Case>({
{u"", 0, u""},
{u"Test", 0, u""},
{u"", kTestWidth, u""},
{u"Tes", kTestWidth, u"Tes"},
{u"Test", kTestWidth, u"Test"},
{u"Tests", kTestWidth, u"Test"},
});
for (const auto& c : cases) {
EXPECT_EQ(c.output, ElideText(c.input, font_list, c.width, TRUNCATE));
}
}
TEST(TextEliderTest, ElideTextEllipsis) {
const FontList font_list;
const float kTestWidth = GetStringWidthF(u"Test", font_list);
const float kEllipsisWidth = GetStringWidthF(u"…", font_list);
struct Case {
const char16_t* input;
float width;
const char16_t* output;
};
const auto cases = std::to_array<Case>({
{u"", 0, u""},
{u"Test", 0, u""},
{u"Test", kEllipsisWidth, u"…"},
{u"", kTestWidth, u""},
{u"Tes", kTestWidth, u"Tes"},
{u"Test", kTestWidth, u"Test"},
});
for (const auto& c : cases) {
EXPECT_EQ(c.output, ElideText(c.input, font_list, c.width, ELIDE_TAIL));
}
}
TEST(TextEliderTest, ElideTextEllipsisFront) {
const FontList font_list;
const float kTestWidth = GetStringWidthF(u"Test", font_list);
const float kEllipsisWidth = GetStringWidthF(u"…", font_list);
const float kEllipsis23Width = GetStringWidthF(u"…23", font_list);
struct Case {
const char16_t* input;
float width;
const std::u16string output;
};
const auto cases = std::to_array<Case>({
{u"", 0, std::u16string()},
{u"Test", 0, std::u16string()},
{u"Test", kEllipsisWidth, u"…"},
{u"", kTestWidth, std::u16string()},
{u"Tes", kTestWidth, u"Tes"},
{u"Test", kTestWidth, u"Test"},
{u"Test123", kEllipsis23Width, u"…23"},
});
for (const auto& c : cases) {
EXPECT_EQ(c.output, ElideText(c.input, font_list, c.width, ELIDE_HEAD));
}
}
// Checks that all occurrences of |first_char| are followed by |second_char| and
// all occurrences of |second_char| are preceded by |first_char| in |text|. Can
// be used to test surrogate pairs or two-character combining sequences.
static void CheckCodeUnitPairs(const std::u16string& text,
char16_t first_char,
char16_t second_char) {
for (size_t index = 0; index < text.length(); ++index) {
EXPECT_NE(second_char, text[index]);
if (text[index] == first_char) {
ASSERT_LT(++index, text.length());
EXPECT_EQ(second_char, text[index]);
}
}
}
// Test that both both UTF-16 surrogate pairs and combining character sequences
// do not get split by ElideText.
TEST(TextEliderTest, ElideTextAtomicSequences) {
#if BUILDFLAG(IS_WIN)
// Needed to bypass DCHECK in GetFallbackFont.
base::test::SingleThreadTaskEnvironment task_environment(
base::test::SingleThreadTaskEnvironment::MainThreadType::UI);
#endif
const FontList font_list;
std::vector<std::u16string> pairs;
// The below is 'MUSICAL SYMBOL G CLEF' (U+1D11E), which is represented in
// UTF-16 as two code units forming a surrogate pair: 0xD834 0xDD1E.
pairs.push_back(u"\U0001d11e");
// The below is a Devanagari two-character combining sequence U+0921 U+093F.
// The sequence forms a single display character and should not be separated.
pairs.push_back(u"\u0921\u093f");
for (const std::u16string& pair : pairs) {
char16_t first_char = pair[0];
char16_t second_char = pair[1];
std::u16string test_string = pair + u"x" + pair;
SCOPED_TRACE(test_string);
const float test_string_width = GetStringWidthF(test_string, font_list);
std::u16string result;
// Elide |text_string| to all possible widths and check that no instance of
// |pair| was split in two.
for (float width = 0; width <= test_string_width; width++) {
result = ElideText(test_string, font_list, width, TRUNCATE);
CheckCodeUnitPairs(result, first_char, second_char);
result = ElideText(test_string, font_list, width, ELIDE_TAIL);
CheckCodeUnitPairs(result, first_char, second_char);
result = ElideText(test_string, font_list, width, ELIDE_MIDDLE);
CheckCodeUnitPairs(result, first_char, second_char);
result = ElideText(test_string, font_list, width, ELIDE_HEAD);
CheckCodeUnitPairs(result, first_char, second_char);
}
}
}
TEST(TextEliderTest, ElideTextLongStrings) {
std::u16string data_scheme(u"data:text/plain,");
size_t data_scheme_length = data_scheme.length();
std::u16string ten_a(10, 'a');
std::u16string hundred_a(100, 'a');
std::u16string thousand_a(1000, 'a');
std::u16string ten_thousand_a(10'000, 'a');
std::u16string hundred_thousand_a(100'000, 'a');
std::u16string million_a(1'000'000, 'a');
// TODO(gbillock): Improve these tests by adding more string diversity and
// doing string compares instead of length compares. See bug 338836.
size_t number_of_as = 156;
std::u16string long_string_end(data_scheme +
std::u16string(number_of_as, 'a') + u"…");
const auto cases_end = std::to_array<StringMappingTestCase>({
{data_scheme + ten_a, data_scheme + ten_a},
{data_scheme + hundred_a, data_scheme + hundred_a},
{data_scheme + thousand_a, long_string_end},
{data_scheme + ten_thousand_a, long_string_end},
{data_scheme + hundred_thousand_a, long_string_end},
{data_scheme + million_a, long_string_end},
});
const FontList font_list;
float ellipsis_width = GetStringWidthF(u"…", font_list);
for (const auto& c : cases_end) {
// Compare sizes rather than actual contents because if the test fails,
// output is rather long.
EXPECT_EQ(c.output.size(),
ElideText(c.input, font_list,
GetStringWidthF(c.output, font_list), ELIDE_TAIL)
.size());
EXPECT_EQ(u"…", ElideText(c.input, font_list, ellipsis_width, ELIDE_TAIL));
}
size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2;
std::u16string long_string_middle(
data_scheme + std::u16string(number_of_as - number_of_trailing_as, 'a') +
u"…" + std::u16string(number_of_trailing_as, 'a'));
#if !BUILDFLAG(IS_IOS)
long_string_middle += u"…";
#endif
const auto cases_middle = std::to_array<StringMappingTestCase>({
{data_scheme + ten_a, data_scheme + ten_a},
{data_scheme + hundred_a, data_scheme + hundred_a},
{data_scheme + thousand_a, long_string_middle},
{data_scheme + ten_thousand_a, long_string_middle},
{data_scheme + hundred_thousand_a, long_string_middle},
{data_scheme + million_a, long_string_middle},
});
for (const auto& c : cases_middle) {
// Compare sizes rather than actual contents because if the test fails,
// output is rather long.
EXPECT_EQ(c.output.size(),
ElideText(c.input, font_list,
GetStringWidthF(c.output, font_list), ELIDE_MIDDLE)
.size());
EXPECT_EQ(u"…",
ElideText(c.input, font_list, ellipsis_width, ELIDE_MIDDLE));
}
std::u16string long_string_beginning(u"…" +
std::u16string(number_of_as, 'a'));
#if !BUILDFLAG(IS_IOS)
long_string_beginning += u"…";
#endif
const auto cases_beginning = std::to_array<StringMappingTestCase>({
{data_scheme + ten_a, data_scheme + ten_a},
{data_scheme + hundred_a, data_scheme + hundred_a},
{data_scheme + thousand_a, long_string_beginning},
{data_scheme + ten_thousand_a, long_string_beginning},
{data_scheme + hundred_thousand_a, long_string_beginning},
{data_scheme + million_a, long_string_beginning},
});
for (const auto& c : cases_beginning) {
EXPECT_EQ(c.output.size(),
ElideText(c.input, font_list,
GetStringWidthF(c.output, font_list), ELIDE_HEAD)
.size());
EXPECT_EQ(u"…", ElideText(c.input, font_list, ellipsis_width, ELIDE_HEAD));
}
}
// Detailed tests for StringSlicer. These are faster and test more of the edge
// cases than the above tests which are more end-to-end.
TEST(TextEliderTest, StringSlicerBasicTest) {
// Must store strings in variables (StringSlicer retains a reference to them).
std::u16string text(u"Hello, world!");
std::u16string ellipsis(u"…");
StringSlicer slicer(text, ellipsis, false, false);
EXPECT_EQ(u"", slicer.CutString(0, false));
EXPECT_EQ(u"…", slicer.CutString(0, true));
EXPECT_EQ(u"Hell", slicer.CutString(4, false));
EXPECT_EQ(u"Hell…", slicer.CutString(4, true));
EXPECT_EQ(text, slicer.CutString(text.length(), false));
EXPECT_EQ(text + u"…", slicer.CutString(text.length(), true));
StringSlicer slicer_begin(text, ellipsis, false, true);
EXPECT_EQ(u"rld!", slicer_begin.CutString(4, false));
EXPECT_EQ(u"…rld!", slicer_begin.CutString(4, true));
StringSlicer slicer_mid(text, ellipsis, true, false);
EXPECT_EQ(u"Held!", slicer_mid.CutString(5, false));
EXPECT_EQ(u"Hel…d!", slicer_mid.CutString(5, true));
}
TEST(TextEliderTest, StringSlicerWhitespace_UseDefault) {
// Must store strings in variables (StringSlicer retains a reference to them).
std::u16string text(u"Hello, world!");
std::u16string ellipsis(u"…");
// Eliding the end of a string should result in whitespace being removed
// before the ellipsis by default.
StringSlicer slicer_end(text, ellipsis, false, false);
EXPECT_EQ(u"Hello,…", slicer_end.CutString(6, true));
EXPECT_EQ(u"Hello,…", slicer_end.CutString(7, true));
EXPECT_EQ(u"Hello, w…", slicer_end.CutString(8, true));
// Eliding the start of a string should result in whitespace being removed
// after the ellipsis by default.
StringSlicer slicer_begin(text, ellipsis, false, true);
EXPECT_EQ(u"…world!", slicer_begin.CutString(6, true));
EXPECT_EQ(u"…world!", slicer_begin.CutString(7, true));
EXPECT_EQ(u"…, world!", slicer_begin.CutString(8, true));
// Eliding the middle of a string should *NOT* result in whitespace being
// removed around the ellipsis by default.
text = u"Hey world!";
StringSlicer slicer_mid(text, ellipsis, true, false);
EXPECT_EQ(u"Hey…ld!", slicer_mid.CutString(6, true));
EXPECT_EQ(u"Hey …ld!", slicer_mid.CutString(7, true));
EXPECT_EQ(u"Hey …rld!", slicer_mid.CutString(8, true));
}
TEST(TextEliderTest, StringSlicerWhitespace_NoTrim) {
// Must store strings in variables (StringSlicer retains a reference to them).
std::u16string text(u"Hello, world!");
std::u16string ellipsis(u"…");
// Eliding the end of a string should not result in whitespace being removed
// before the ellipsis in no-trim mode.
StringSlicer slicer_end(text, ellipsis, false, false, false);
EXPECT_EQ(u"Hello,…", slicer_end.CutString(6, true));
EXPECT_EQ(u"Hello, …", slicer_end.CutString(7, true));
EXPECT_EQ(u"Hello, w…", slicer_end.CutString(8, true));
// Eliding the start of a string should not result in whitespace being removed
// after the ellipsis in no-trim mode.
StringSlicer slicer_begin(text, ellipsis, false, true, false);
EXPECT_EQ(u"…world!", slicer_begin.CutString(6, true));
EXPECT_EQ(u"… world!", slicer_begin.CutString(7, true));
EXPECT_EQ(u"…, world!", slicer_begin.CutString(8, true));
// Eliding the middle of a string should *NOT* result in whitespace being
// removed around the ellipsis in no-trim mode.
text = u"Hey world!";
StringSlicer slicer_mid(text, ellipsis, true, false, false);
EXPECT_EQ(u"Hey…ld!", slicer_mid.CutString(6, true));
EXPECT_EQ(u"Hey …ld!", slicer_mid.CutString(7, true));
EXPECT_EQ(u"Hey …rld!", slicer_mid.CutString(8, true));
}
TEST(TextEliderTest, StringSlicerWhitespace_Trim) {
// Must store strings in variables (StringSlicer retains a reference to them).
std::u16string text(u"Hello, world!");
std::u16string ellipsis(u"…");
// Eliding the end of a string should result in whitespace being removed
// before the ellipsis in trim mode.
StringSlicer slicer_end(text, ellipsis, false, false, true);
EXPECT_EQ(u"Hello,…", slicer_end.CutString(6, true));
EXPECT_EQ(u"Hello,…", slicer_end.CutString(7, true));
EXPECT_EQ(u"Hello, w…", slicer_end.CutString(8, true));
// Eliding the start of a string should result in whitespace being removed
// after the ellipsis in trim mode.
StringSlicer slicer_begin(text, ellipsis, false, true, true);
EXPECT_EQ(u"…world!", slicer_begin.CutString(6, true));
EXPECT_EQ(u"…world!", slicer_begin.CutString(7, true));
EXPECT_EQ(u"…, world!", slicer_begin.CutString(8, true));
// Eliding the middle of a string *should* result in whitespace being removed
// around the ellipsis in trim mode.
text = u"Hey world!";
StringSlicer slicer_mid(text, ellipsis, true, false, true);
EXPECT_EQ(u"Hey…ld!", slicer_mid.CutString(6, true));
EXPECT_EQ(u"Hey…ld!", slicer_mid.CutString(7, true));
EXPECT_EQ(u"Hey…rld!", slicer_mid.CutString(8, true));
}
TEST(TextEliderTest, StringSlicer_ElideMiddle_MultipleWhitespace) {
// Must store strings in variables (StringSlicer retains a reference to them).
std::u16string text(u"Hey U man");
std::u16string ellipsis(u"…");
// Eliding the middle of a string should not result in whitespace being
// removed around the ellipsis in default whitespace mode.
StringSlicer slicer_default(text, ellipsis, true, false);
EXPECT_EQ(u"Hey…man", slicer_default.CutString(6, true));
EXPECT_EQ(u"Hey …man", slicer_default.CutString(7, true));
EXPECT_EQ(u"Hey … man", slicer_default.CutString(8, true));
EXPECT_EQ(u"Hey … man", slicer_default.CutString(9, true));
EXPECT_EQ(u"Hey … man", slicer_default.CutString(10, true));
// Eliding the middle of a string should not result in whitespace being
// removed around the ellipsis in no-trim mode.
text = u"Hey U man";
StringSlicer slicer_notrim(text, ellipsis, true, false, false);
EXPECT_EQ(u"Hey…man", slicer_notrim.CutString(6, true));
EXPECT_EQ(u"Hey …man", slicer_notrim.CutString(7, true));
EXPECT_EQ(u"Hey … man", slicer_notrim.CutString(8, true));
EXPECT_EQ(u"Hey … man", slicer_notrim.CutString(9, true));
EXPECT_EQ(u"Hey … man", slicer_notrim.CutString(10, true));
// Eliding the middle of a string *should* result in whitespace being removed
// around the ellipsis in trim mode.
text = u"Hey U man";
StringSlicer slicer_trim(text, ellipsis, true, false, true);
EXPECT_EQ(u"Hey…man", slicer_trim.CutString(6, true));
EXPECT_EQ(u"Hey…man", slicer_trim.CutString(7, true));
EXPECT_EQ(u"Hey…man", slicer_trim.CutString(8, true));
EXPECT_EQ(u"Hey…man", slicer_trim.CutString(9, true));
EXPECT_EQ(u"Hey…man", slicer_trim.CutString(10, true));
}
TEST(TextEliderTest, StringSlicerSurrogate) {
// The below is 'MUSICAL SYMBOL G CLEF' (U+1D11E), which is represented in
// UTF-16 as two code units forming a surrogate pair: 0xD834 0xDD1E.
const std::u16string kSurrogate = u"\U0001d11e";
ASSERT_EQ(2u, kSurrogate.size());
ASSERT_EQ(u'\xD834', kSurrogate[0]);
ASSERT_EQ(u'\xDD1E', kSurrogate[1]);
std::u16string text(u"abc" + kSurrogate + u"xyz");
std::u16string ellipsis(u"…");
StringSlicer slicer(text, ellipsis, false, false);
// Cut surrogate on the right. Should round left and exclude the surrogate.
EXPECT_EQ(u"…", slicer.CutString(0, true));
EXPECT_EQ(u"abc…", slicer.CutString(4, true));
EXPECT_EQ(text + u"…", slicer.CutString(text.length(), true));
// Cut surrogate on the left. Should round right and exclude the surrogate.
StringSlicer slicer_begin(text, ellipsis, false, true);
EXPECT_EQ(u"…xyz", slicer_begin.CutString(4, true));
// Cut surrogate in the middle. Should round right and exclude the surrogate.
std::u16string short_text(u"abc" + kSurrogate);
StringSlicer slicer_mid(short_text, ellipsis, true, false);
EXPECT_EQ(u"a…", slicer_mid.CutString(2, true));
// String that starts with a dangling trailing surrogate.
std::u16string dangling_trailing_text = kSurrogate.substr(1);
StringSlicer slicer_dangling_trailing(dangling_trailing_text, ellipsis, false,
false);
EXPECT_EQ(u"…", slicer_dangling_trailing.CutString(0, true));
EXPECT_EQ(dangling_trailing_text + u"…",
slicer_dangling_trailing.CutString(1, true));
}
TEST(TextEliderTest, StringSlicerCombining) {
// The following string contains three combining character sequences (one for
// each category of combining mark):
// LATIN SMALL LETTER E + COMBINING ACUTE ACCENT + COMBINING CEDILLA
// LATIN SMALL LETTER X + COMBINING ENCLOSING KEYCAP
// DEVANAGARI LETTER DDA + DEVANAGARI VOWEL SIGN I
std::u16string text(u"e\u0301\u0327 x\u20e3 \u0921\u093f");
std::u16string ellipsis(u"…");
StringSlicer slicer(text, ellipsis, false, false);
// Attempt to cut the string for all lengths. When a combining sequence is
// cut, it should always round left and exclude the combining sequence.
// Whitespace is also cut adjacent to the ellipsis.
// First sequence:
EXPECT_EQ(u"…", slicer.CutString(0, true));
EXPECT_EQ(u"…", slicer.CutString(1, true));
EXPECT_EQ(u"…", slicer.CutString(2, true));
EXPECT_EQ(text.substr(0, 3) + u"…", slicer.CutString(3, true));
// Second sequence:
EXPECT_EQ(text.substr(0, 3) + u"…", slicer.CutString(4, true));
EXPECT_EQ(text.substr(0, 3) + u"…", slicer.CutString(5, true));
EXPECT_EQ(text.substr(0, 6) + u"…", slicer.CutString(6, true));
// Third sequence:
EXPECT_EQ(text.substr(0, 6) + u"…", slicer.CutString(7, true));
EXPECT_EQ(text.substr(0, 6) + u"…", slicer.CutString(8, true));
EXPECT_EQ(text + u"…", slicer.CutString(9, true));
// Cut string in the middle, splitting the second sequence in half. Should
// round both left and right, excluding the second sequence.
StringSlicer slicer_mid(text, ellipsis, true, false);
EXPECT_EQ(text.substr(0, 4) + u"…" + text.substr(6),
slicer_mid.CutString(9, true));
// String that starts with a dangling combining mark.
char16_t dangling_mark_chars[] = {text[1], 0};
std::u16string dangling_mark_text(dangling_mark_chars);
StringSlicer slicer_dangling_mark(dangling_mark_text, ellipsis, false, false);
EXPECT_EQ(u"…", slicer_dangling_mark.CutString(0, true));
EXPECT_EQ(dangling_mark_text + u"…", slicer_dangling_mark.CutString(1, true));
}
TEST(TextEliderTest, StringSlicerCombiningSurrogate) {
// The ultimate test: combining sequences comprised of surrogate pairs.
// The following string contains a single combining character sequence:
// MUSICAL SYMBOL G CLEF (U+1D11E) + MUSICAL SYMBOL COMBINING FLAG-1 (U+1D16E)
// Represented as four UTF-16 code units.
std::u16string text(u"\U0001d11e\U0001d16e");
std::u16string ellipsis(u"…");
StringSlicer slicer(text, ellipsis, false, false);
// Attempt to cut the string for all lengths. Should always round left and
// exclude the combining sequence.
EXPECT_EQ(u"…", slicer.CutString(0, true));
EXPECT_EQ(u"…", slicer.CutString(1, true));
EXPECT_EQ(u"…", slicer.CutString(2, true));
EXPECT_EQ(u"…", slicer.CutString(3, true));
EXPECT_EQ(text + u"…", slicer.CutString(4, true));
// Cut string in the middle. Should exclude the sequence.
StringSlicer slicer_mid(text, ellipsis, true, false);
EXPECT_EQ(u"…", slicer_mid.CutString(4, true));
}
TEST(TextEliderTest, ElideString) {
struct Case {
const char16_t* input;
size_t max_len;
bool result;
const char16_t* output;
};
const auto cases = std::to_array<Case>(
{{u"Hello", 0, true, u""},
{u"", 0, false, u""},
{u"Hello, my name is Tom", 1, true, u"H"},
{u"Hello, my name is Tom", 2, true, u"He"},
{u"Hello, my name is Tom", 3, true, u"H.m"},
{u"Hello, my name is Tom", 4, true, u"H..m"},
{u"Hello, my name is Tom", 5, true, u"H...m"},
{u"Hello, my name is Tom", 6, true, u"He...m"},
{u"Hello, my name is Tom", 7, true, u"He...om"},
{u"Hello, my name is Tom", 10, true, u"Hell...Tom"},
{u"Hello, my name is Tom", 100, false, u"Hello, my name is Tom"}});
for (const auto& c : cases) {
std::u16string output;
EXPECT_EQ(c.result, ElideString(c.input, c.max_len, &output));
EXPECT_EQ(c.output, output);
}
}
TEST(TextEliderTest, ElideRectangleText) {
const FontList font_list;
const int line_height = font_list.GetHeight();
const float test_width = GetStringWidthF(u"Test", font_list);
struct Case {
const char16_t* input;
float available_pixel_width;
int available_pixel_height;
bool truncated_y;
const char16_t* output;
};
const auto cases = std::to_array<Case>({
{u"", 0, 0, false, nullptr},
{u"", 1, 1, false, nullptr},
{u"Test", test_width, 0, true, nullptr},
{u"Test", test_width, 1, false, u"Test"},
{u"Test", test_width, line_height, false, u"Test"},
{u"Test Test", test_width, line_height, true, u"Test"},
{u"Test Test", test_width, line_height + 1, false, u"Test|Test"},
{u"Test Test", test_width, line_height * 2, false, u"Test|Test"},
{u"Test Test", test_width, line_height * 3, false, u"Test|Test"},
{u"Test Test", test_width * 2, line_height * 2, false, u"Test|Test"},
{u"Test Test", test_width * 3, line_height, false, u"Test Test"},
{u"Test\nTest", test_width * 3, line_height * 2, false, u"Test|Test"},
{u"Te\nst Te", test_width, line_height * 3, false, u"Te|st|Te"},
{u"\nTest", test_width, line_height * 2, false, u"|Test"},
{u"\nTest", test_width, line_height, true, u""},
{u"\n\nTest", test_width, line_height * 3, false, u"||Test"},
{u"\n\nTest", test_width, line_height * 2, true, u"|"},
{u"Test\n", 2 * test_width, line_height * 5, false, u"Test|"},
{u"Test\n\n", 2 * test_width, line_height * 5, false, u"Test||"},
{u"Test\n\n\n", 2 * test_width, line_height * 5, false, u"Test|||"},
{u"Test\nTest\n\n", 2 * test_width, line_height * 5, false,
u"Test|Test||"},
{u"Test\n\nTest\n", 2 * test_width, line_height * 5, false,
u"Test||Test|"},
{u"Test\n\n\nTest", 2 * test_width, line_height * 5, false,
u"Test|||Test"},
{u"Te ", test_width, line_height, false, u"Te"},
{u"Te Te Test", test_width, 3 * line_height, false, u"Te|Te|Test"},
});
for (const auto& c : cases) {
std::vector<std::u16string> lines;
EXPECT_EQ(c.truncated_y ? INSUFFICIENT_SPACE_VERTICAL : 0,
ElideRectangleText(c.input, font_list, c.available_pixel_width,
c.available_pixel_height, TRUNCATE_LONG_WORDS,
&lines));
if (c.output) {
const std::u16string result = base::JoinString(lines, u"|");
EXPECT_EQ(c.output, result);
} else {
EXPECT_TRUE(lines.empty());
}
}
}
TEST(TextEliderTest, ElideRectangleTextFirstWordTruncated) {
const FontList font_list;
const int line_height = font_list.GetHeight();
const float test_width = GetStringWidthF(u"Test", font_list);
const float tes_width = GetStringWidthF(u"Tes", font_list);
std::vector<std::u16string> lines;
auto result_for_width = [&](const char16_t* input, float width) {
lines.clear();
return ElideRectangleText(input, font_list, width, line_height * 4,
WRAP_LONG_WORDS, &lines);
};
// Test base case.
EXPECT_EQ(0, result_for_width(u"Test", test_width));
EXPECT_EQ(1u, lines.size());
EXPECT_EQ(u"Test", lines[0]);
// First word truncated.
EXPECT_EQ(INSUFFICIENT_SPACE_FOR_FIRST_WORD,
result_for_width(u"Test", tes_width));
EXPECT_EQ(2u, lines.size());
EXPECT_EQ(u"Tes", lines[0]);
EXPECT_EQ(u"t", lines[1]);
// Two words truncated.
EXPECT_EQ(INSUFFICIENT_SPACE_FOR_FIRST_WORD,
result_for_width(u"Test\nTest", tes_width));
EXPECT_EQ(4u, lines.size());
EXPECT_EQ(u"Tes", lines[0]);
EXPECT_EQ(u"t", lines[1]);
EXPECT_EQ(u"Tes", lines[2]);
EXPECT_EQ(u"t", lines[3]);
// Word truncated, but not the first.
EXPECT_EQ(0, result_for_width(u"T Test", tes_width));
EXPECT_EQ(3u, lines.size());
EXPECT_EQ(u"T", lines[0]);
EXPECT_EQ(u"Tes", lines[1]);
EXPECT_EQ(u"t", lines[2]);
// Leading \n.
EXPECT_EQ(0, result_for_width(u"\nTest", tes_width));
EXPECT_EQ(3u, lines.size());
EXPECT_EQ(u"", lines[0]);
EXPECT_EQ(u"Tes", lines[1]);
EXPECT_EQ(u"t", lines[2]);
}
TEST(TextEliderTest, ElideRectangleTextPunctuation) {
const FontList font_list;
const int line_height = font_list.GetHeight();
const float test_width = GetStringWidthF(u"Test", font_list);
const float test_t_width = GetStringWidthF(u"Test T", font_list);
constexpr int kResultMask =
INSUFFICIENT_SPACE_HORIZONTAL | INSUFFICIENT_SPACE_VERTICAL;
struct Case {
const char16_t* input;
float available_pixel_width;
int available_pixel_height;
bool wrap_words;
bool truncated_x;
const char16_t* output;
};
const auto cases = std::to_array<Case>({
{u"Test T.", test_t_width, line_height * 2, false, false, u"Test|T."},
{u"Test T ?", test_t_width, line_height * 2, false, false, u"Test|T ?"},
{u"Test. Test", test_width, line_height * 3, false, true, u"Test|Test"},
{u"Test. Test", test_width, line_height * 3, true, false, u"Test|.|Test"},
});
for (const auto& c : cases) {
std::vector<std::u16string> lines;
const WordWrapBehavior wrap_behavior =
(c.wrap_words ? WRAP_LONG_WORDS : TRUNCATE_LONG_WORDS);
EXPECT_EQ(
c.truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0,
ElideRectangleText(c.input, font_list, c.available_pixel_width,
c.available_pixel_height, wrap_behavior, &lines) &
kResultMask);
if (c.output) {
const std::u16string result = base::JoinString(lines, u"|");
EXPECT_EQ(c.output, result);
} else {
EXPECT_TRUE(lines.empty());
}
}
}
TEST(TextEliderTest, ElideRectangleTextLongWords) {
const FontList font_list;
const int kAvailableHeight = 1000;
const std::u16string kElidedTesting = u"Tes…";
const float elided_width = GetStringWidthF(kElidedTesting, font_list);
const float test_width = GetStringWidthF(u"Test", font_list);
constexpr int kResultMask =
INSUFFICIENT_SPACE_HORIZONTAL | INSUFFICIENT_SPACE_VERTICAL;
struct Case {
const char16_t* input;
float available_pixel_width;
WordWrapBehavior wrap_behavior;
bool truncated_x;
const char16_t* output;
};
const auto cases = std::to_array<Case>({
{u"Testing", test_width, IGNORE_LONG_WORDS, false, u"Testing"},
{u"X Testing", test_width, IGNORE_LONG_WORDS, false, u"X|Testing"},
{u"Test Testing", test_width, IGNORE_LONG_WORDS, false, u"Test|Testing"},
{u"Test\nTesting", test_width, IGNORE_LONG_WORDS, false, u"Test|Testing"},
{u"Test Tests ", test_width, IGNORE_LONG_WORDS, false, u"Test|Tests"},
{u"Test Tests T", test_width, IGNORE_LONG_WORDS, false, u"Test|Tests|T"},
{u"Testing", elided_width, ELIDE_LONG_WORDS, true, u"Tes…"},
{u"X Testing", elided_width, ELIDE_LONG_WORDS, true, u"X|Tes…"},
{u"Test Testing", elided_width, ELIDE_LONG_WORDS, true, u"Test|Tes…"},
{u"Test\nTesting", elided_width, ELIDE_LONG_WORDS, true, u"Test|Tes…"},
{u"Testing", test_width, TRUNCATE_LONG_WORDS, true, u"Test"},
{u"X Testing", test_width, TRUNCATE_LONG_WORDS, true, u"X|Test"},
{u"Test Testing", test_width, TRUNCATE_LONG_WORDS, true, u"Test|Test"},
{u"Test\nTesting", test_width, TRUNCATE_LONG_WORDS, true, u"Test|Test"},
{u"Test Tests ", test_width, TRUNCATE_LONG_WORDS, true, u"Test|Test"},
{u"Test Tests T", test_width, TRUNCATE_LONG_WORDS, true, u"Test|Test|T"},
{u"Testing", test_width, WRAP_LONG_WORDS, false, u"Test|ing"},
{u"X Testing", test_width, WRAP_LONG_WORDS, false, u"X|Test|ing"},
{u"Test Testing", test_width, WRAP_LONG_WORDS, false, u"Test|Test|ing"},
{u"Test\nTesting", test_width, WRAP_LONG_WORDS, false, u"Test|Test|ing"},
{u"Test Tests ", test_width, WRAP_LONG_WORDS, false, u"Test|Test|s"},
{u"Test Tests T", test_width, WRAP_LONG_WORDS, false, u"Test|Test|s T"},
{u"TestTestTest", test_width, WRAP_LONG_WORDS, false, u"Test|Test|Test"},
{u"TestTestTestT", test_width, WRAP_LONG_WORDS, false,
u"Test|Test|Test|T"},
});
for (const auto& c : cases) {
std::vector<std::u16string> lines;
EXPECT_EQ(c.truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0,
ElideRectangleText(c.input, font_list, c.available_pixel_width,
kAvailableHeight, c.wrap_behavior, &lines) &
kResultMask);
std::u16string expected_output(c.output);
const std::u16string result = base::JoinString(lines, u"|");
EXPECT_EQ(expected_output, result);
}
}
// This test is to make sure that the width of each wrapped line does not
// exceed the available width. On some platform like Mac, this test used to
// fail because the truncated integer width is returned for the string
// and the accumulation of the truncated values causes the elide function
// to wrap incorrectly.
TEST(TextEliderTest, ElideRectangleTextCheckLineWidth) {
FontList font_list;
#if BUILDFLAG(IS_MAC)
// Use a specific font to expose the line width exceeding problem.
font_list = FontList(Font("LucidaGrande", 12));
#endif
const float kAvailableWidth = 235;
const int kAvailableHeight = 1000;
const char16_t text[] = u"that Russian place we used to go to after fencing";
std::vector<std::u16string> lines;
EXPECT_EQ(0, ElideRectangleText(text, font_list, kAvailableWidth,
kAvailableHeight, WRAP_LONG_WORDS, &lines));
ASSERT_EQ(2u, lines.size());
EXPECT_LE(GetStringWidthF(lines[0], font_list), kAvailableWidth);
EXPECT_LE(GetStringWidthF(lines[1], font_list), kAvailableWidth);
}
#if BUILDFLAG(IS_CHROMEOS)
// This test was created specifically to test a message from crbug.com/415213.
// It tests that width of concatenation of words equals sum of widths of the
// words.
TEST(TextEliderTest, ElideRectangleTextCheckConcatWidthEqualsSumOfWidths) {
FontList font_list;
font_list = FontList("Noto Sans UI,ui-sans, 12px");
SetFontRenderParamsDeviceScaleFactor(1.25f);
#define WIDTH(x) GetStringWidthF((x), font_list)
EXPECT_EQ(WIDTH(u"The administrator for this account has"),
WIDTH(u"The ") + WIDTH(u"administrator ") + WIDTH(u"for ") +
WIDTH(u"this ") + WIDTH(u"account ") + WIDTH(u"has"));
#undef WIDTH
SetFontRenderParamsDeviceScaleFactor(1.0f);
}
#endif // BUILDFLAG(IS_CHROMEOS)
TEST(TextEliderTest, ElideRectangleString) {
struct Case {
const char16_t* input;
int max_rows;
int max_cols;
bool result;
const char16_t* output;
};
const auto cases = std::to_array<Case>({
{u"", 0, 0, false, u""},
{u"", 1, 1, false, u""},
{u"Hi, my name is\nTom", 0, 0, true, u"..."},
{u"Hi, my name is\nTom", 1, 0, true, u"\n..."},
{u"Hi, my name is\nTom", 0, 1, true, u"..."},
{u"Hi, my name is\nTom", 1, 1, true, u"H\n..."},
{u"Hi, my name is\nTom", 2, 1, true, u"H\ni\n..."},
{u"Hi, my name is\nTom", 3, 1, true, u"H\ni\n,\n..."},
{u"Hi, my name is\nTom", 4, 1, true, u"H\ni\n,\n \n..."},
{u"Hi, my name is\nTom", 5, 1, true, u"H\ni\n,\n \nm\n..."},
{u"Hi, my name is\nTom", 0, 2, true, u"..."},
{u"Hi, my name is\nTom", 1, 2, true, u"Hi\n..."},
{u"Hi, my name is\nTom", 2, 2, true, u"Hi\n, \n..."},
{u"Hi, my name is\nTom", 3, 2, true, u"Hi\n, \nmy\n..."},
{u"Hi, my name is\nTom", 4, 2, true, u"Hi\n, \nmy\n n\n..."},
{u"Hi, my name is\nTom", 5, 2, true, u"Hi\n, \nmy\n n\nam\n..."},
{u"Hi, my name is\nTom", 0, 3, true, u"..."},
{u"Hi, my name is\nTom", 1, 3, true, u"Hi,\n..."},
{u"Hi, my name is\nTom", 2, 3, true, u"Hi,\n my\n..."},
{u"Hi, my name is\nTom", 3, 3, true, u"Hi,\n my\n na\n..."},
{u"Hi, my name is\nTom", 4, 3, true, u"Hi,\n my\n na\nme \n..."},
{u"Hi, my name is\nTom", 5, 3, true, u"Hi,\n my\n na\nme \nis\n..."},
{u"Hi, my name is\nTom", 1, 4, true, u"Hi, \n..."},
{u"Hi, my name is\nTom", 2, 4, true, u"Hi, \nmy n\n..."},
{u"Hi, my name is\nTom", 3, 4, true, u"Hi, \nmy n\name \n..."},
{u"Hi, my name is\nTom", 4, 4, true, u"Hi, \nmy n\name \nis\n..."},
{u"Hi, my name is\nTom", 5, 4, false, u"Hi, \nmy n\name \nis\nTom"},
{u"Hi, my name is\nTom", 1, 5, true, u"Hi, \n..."},
{u"Hi, my name is\nTom", 2, 5, true, u"Hi, \nmy na\n..."},
{u"Hi, my name is\nTom", 3, 5, true, u"Hi, \nmy na\nme \n..."},
{u"Hi, my name is\nTom", 4, 5, true, u"Hi, \nmy na\nme \nis\n..."},
{u"Hi, my name is\nTom", 5, 5, false, u"Hi, \nmy na\nme \nis\nTom"},
{u"Hi, my name is\nTom", 1, 6, true, u"Hi, \n..."},
{u"Hi, my name is\nTom", 2, 6, true, u"Hi, \nmy \n..."},
{u"Hi, my name is\nTom", 3, 6, true, u"Hi, \nmy \nname \n..."},
{u"Hi, my name is\nTom", 4, 6, true, u"Hi, \nmy \nname \nis\n..."},
{u"Hi, my name is\nTom", 5, 6, false, u"Hi, \nmy \nname \nis\nTom"},
{u"Hi, my name is\nTom", 1, 7, true, u"Hi, \n..."},
{u"Hi, my name is\nTom", 2, 7, true, u"Hi, \nmy \n..."},
{u"Hi, my name is\nTom", 3, 7, true, u"Hi, \nmy \nname \n..."},
{u"Hi, my name is\nTom", 4, 7, true, u"Hi, \nmy \nname \nis\n..."},
{u"Hi, my name is\nTom", 5, 7, false, u"Hi, \nmy \nname \nis\nTom"},
{u"Hi, my name is\nTom", 1, 8, true, u"Hi, my \n..."},
{u"Hi, my name is\nTom", 2, 8, true, u"Hi, my \nname \n..."},
{u"Hi, my name is\nTom", 3, 8, true, u"Hi, my \nname \nis\n..."},
{u"Hi, my name is\nTom", 4, 8, false, u"Hi, my \nname \nis\nTom"},
{u"Hi, my name is\nTom", 1, 9, true, u"Hi, my \n..."},
{u"Hi, my name is\nTom", 2, 9, true, u"Hi, my \nname is\n..."},
{u"Hi, my name is\nTom", 3, 9, false, u"Hi, my \nname is\nTom"},
{u"Hi, my name is\nTom", 1, 10, true, u"Hi, my \n..."},
{u"Hi, my name is\nTom", 2, 10, true, u"Hi, my \nname is\n..."},
{u"Hi, my name is\nTom", 3, 10, false, u"Hi, my \nname is\nTom"},
{u"Hi, my name is\nTom", 1, 11, true, u"Hi, my \n..."},
{u"Hi, my name is\nTom", 2, 11, true, u"Hi, my \nname is\n..."},
{u"Hi, my name is\nTom", 3, 11, false, u"Hi, my \nname is\nTom"},
{u"Hi, my name is\nTom", 1, 12, true, u"Hi, my \n..."},
{u"Hi, my name is\nTom", 2, 12, true, u"Hi, my \nname is\n..."},
{u"Hi, my name is\nTom", 3, 12, false, u"Hi, my \nname is\nTom"},
{u"Hi, my name is\nTom", 1, 13, true, u"Hi, my name \n..."},
{u"Hi, my name is\nTom", 2, 13, true, u"Hi, my name \nis\n..."},
{u"Hi, my name is\nTom", 3, 13, false, u"Hi, my name \nis\nTom"},
{u"Hi, my name is\nTom", 1, 20, true, u"Hi, my name is\n..."},
{u"Hi, my name is\nTom", 2, 20, false, u"Hi, my name is\nTom"},
{u"Hi, my name is Tom", 1, 40, false, u"Hi, my name is Tom"},
});
std::u16string output;
for (const auto& c : cases) {
EXPECT_EQ(c.result, ElideRectangleString(c.input, c.max_rows, c.max_cols,
true, &output));
EXPECT_EQ(c.output, output);
}
}
TEST(TextEliderTest, ElideRectangleStringNotStrict) {
struct Case {
const char16_t* input;
int max_rows;
int max_cols;
bool result;
const char16_t* output;
};
const auto cases = std::to_array<Case>({
{u"", 0, 0, false, u""},
{u"", 1, 1, false, u""},
{u"Hi, my name_is\nDick", 0, 0, true, u"..."},
{u"Hi, my name_is\nDick", 1, 0, true, u"\n..."},
{u"Hi, my name_is\nDick", 0, 1, true, u"..."},
{u"Hi, my name_is\nDick", 1, 1, true, u"H\n..."},
{u"Hi, my name_is\nDick", 2, 1, true, u"H\ni\n..."},
{u"Hi, my name_is\nDick", 3, 1, true, u"H\ni\n,\n..."},
{u"Hi, my name_is\nDick", 4, 1, true, u"H\ni\n,\n \n..."},
{u"Hi, my name_is\nDick", 5, 1, true, u"H\ni\n,\n \nm\n..."},
{u"Hi, my name_is\nDick", 0, 2, true, u"..."},
{u"Hi, my name_is\nDick", 1, 2, true, u"Hi\n..."},
{u"Hi, my name_is\nDick", 2, 2, true, u"Hi\n, \n..."},
{u"Hi, my name_is\nDick", 3, 2, true, u"Hi\n, \nmy\n..."},
{u"Hi, my name_is\nDick", 4, 2, true, u"Hi\n, \nmy\n n\n..."},
{u"Hi, my name_is\nDick", 5, 2, true, u"Hi\n, \nmy\n n\nam\n..."},
{u"Hi, my name_is\nDick", 0, 3, true, u"..."},
{u"Hi, my name_is\nDick", 1, 3, true, u"Hi,\n..."},
{u"Hi, my name_is\nDick", 2, 3, true, u"Hi,\n my\n..."},
{u"Hi, my name_is\nDick", 3, 3, true, u"Hi,\n my\n na\n..."},
{u"Hi, my name_is\nDick", 4, 3, true, u"Hi,\n my\n na\nme_\n..."},
{u"Hi, my name_is\nDick", 5, 3, true, u"Hi,\n my\n na\nme_\nis\n..."},
{u"Hi, my name_is\nDick", 1, 4, true, u"Hi, ..."},
{u"Hi, my name_is\nDick", 2, 4, true, u"Hi, my n\n..."},
{u"Hi, my name_is\nDick", 3, 4, true, u"Hi, my n\name_\n..."},
{u"Hi, my name_is\nDick", 4, 4, true, u"Hi, my n\name_\nis\n..."},
{u"Hi, my name_is\nDick", 5, 4, false, u"Hi, my n\name_\nis\nDick"},
{u"Hi, my name_is\nDick", 1, 5, true, u"Hi, ..."},
{u"Hi, my name_is\nDick", 2, 5, true, u"Hi, my na\n..."},
{u"Hi, my name_is\nDick", 3, 5, true, u"Hi, my na\nme_is\n..."},
{u"Hi, my name_is\nDick", 4, 5, true, u"Hi, my na\nme_is\n\n..."},
{u"Hi, my name_is\nDick", 5, 5, false, u"Hi, my na\nme_is\n\nDick"},
{u"Hi, my name_is\nDick", 1, 6, true, u"Hi, ..."},
{u"Hi, my name_is\nDick", 2, 6, true, u"Hi, my nam\n..."},
{u"Hi, my name_is\nDick", 3, 6, true, u"Hi, my nam\ne_is\n..."},
{u"Hi, my name_is\nDick", 4, 6, false, u"Hi, my nam\ne_is\nDick"},
{u"Hi, my name_is\nDick", 5, 6, false, u"Hi, my nam\ne_is\nDick"},
{u"Hi, my name_is\nDick", 1, 7, true, u"Hi, ..."},
{u"Hi, my name_is\nDick", 2, 7, true, u"Hi, my name\n..."},
{u"Hi, my name_is\nDick", 3, 7, true, u"Hi, my name\n_is\n..."},
{u"Hi, my name_is\nDick", 4, 7, false, u"Hi, my name\n_is\nDick"},
{u"Hi, my name_is\nDick", 5, 7, false, u"Hi, my name\n_is\nDick"},
{u"Hi, my name_is\nDick", 1, 8, true, u"Hi, my n\n..."},
{u"Hi, my name_is\nDick", 2, 8, true, u"Hi, my n\name_is\n..."},
{u"Hi, my name_is\nDick", 3, 8, false, u"Hi, my n\name_is\nDick"},
{u"Hi, my name_is\nDick", 1, 9, true, u"Hi, my ..."},
{u"Hi, my name_is\nDick", 2, 9, true, u"Hi, my name_is\n..."},
{u"Hi, my name_is\nDick", 3, 9, false, u"Hi, my name_is\nDick"},
{u"Hi, my name_is\nDick", 1, 10, true, u"Hi, my ..."},
{u"Hi, my name_is\nDick", 2, 10, true, u"Hi, my name_is\n..."},
{u"Hi, my name_is\nDick", 3, 10, false, u"Hi, my name_is\nDick"},
{u"Hi, my name_is\nDick", 1, 11, true, u"Hi, my ..."},
{u"Hi, my name_is\nDick", 2, 11, true, u"Hi, my name_is\n..."},
{u"Hi, my name_is\nDick", 3, 11, false, u"Hi, my name_is\nDick"},
{u"Hi, my name_is\nDick", 1, 12, true, u"Hi, my ..."},
{u"Hi, my name_is\nDick", 2, 12, true, u"Hi, my name_is\n..."},
{u"Hi, my name_is\nDick", 3, 12, false, u"Hi, my name_is\nDick"},
{u"Hi, my name_is\nDick", 1, 13, true, u"Hi, my ..."},
{u"Hi, my name_is\nDick", 2, 13, true, u"Hi, my name_is\n..."},
{u"Hi, my name_is\nDick", 3, 13, false, u"Hi, my name_is\nDick"},
{u"Hi, my name_is\nDick", 1, 20, true, u"Hi, my name_is\n..."},
{u"Hi, my name_is\nDick", 2, 20, false, u"Hi, my name_is\nDick"},
{u"Hi, my name_is Dick", 1, 40, false, u"Hi, my name_is Dick"},
});
std::u16string output;
for (const auto& c : cases) {
EXPECT_EQ(c.result, ElideRectangleString(c.input, c.max_rows, c.max_cols,
false, &output));
EXPECT_EQ(c.output, output);
}
}
TEST(TextEliderTest, ElideRectangleWide16) {
// Two greek words separated by space.
const std::u16string str(u"Παγκόσμιος Ιστός");
const std::u16string out1(u"Παγκ\nόσμι\n...");
const std::u16string out2(u"Παγκόσμιος \nΙστός");
std::u16string output;
EXPECT_TRUE(ElideRectangleString(str, 2, 4, true, &output));
EXPECT_EQ(out1, output);
EXPECT_FALSE(ElideRectangleString(str, 2, 12, true, &output));
EXPECT_EQ(out2, output);
}
TEST(TextEliderTest, ElideRectangleWide32) {
const std::u16string str(u"𝒜𝒜𝒜𝒜 aaaaa");
const std::u16string out(u"𝒜𝒜𝒜\n𝒜 \naaa\n...");
std::u16string output;
EXPECT_TRUE(ElideRectangleString(str, 3, 3, true, &output));
EXPECT_EQ(out, output);
}
TEST(TextEliderTest, TruncateString) {
std::u16string str = u"fooooey bxxxar baz ";
// Test breaking at character 0.
EXPECT_EQ(std::u16string(), TruncateString(str, 0, WORD_BREAK));
EXPECT_EQ(std::u16string(), TruncateString(str, 0, CHARACTER_BREAK));
// Test breaking at character 1.
EXPECT_EQ(u"…", TruncateString(str, 1, WORD_BREAK));
EXPECT_EQ(u"…", TruncateString(str, 1, CHARACTER_BREAK));
// Test breaking in the middle of the first word.
EXPECT_EQ(u"f…", TruncateString(str, 2, WORD_BREAK));
EXPECT_EQ(u"f…", TruncateString(str, 2, CHARACTER_BREAK));
// Test breaking in between words.
EXPECT_EQ(u"fooooey…", TruncateString(str, 9, WORD_BREAK));
EXPECT_EQ(u"fooooey…", TruncateString(str, 9, CHARACTER_BREAK));
// Test breaking at the start of a later word.
EXPECT_EQ(u"fooooey…", TruncateString(str, 11, WORD_BREAK));
EXPECT_EQ(u"fooooey…", TruncateString(str, 11, CHARACTER_BREAK));
// Test breaking in the middle of a word.
EXPECT_EQ(u"fooooey…", TruncateString(str, 12, WORD_BREAK));
EXPECT_EQ(u"fooooey…", TruncateString(str, 12, CHARACTER_BREAK));
EXPECT_EQ(u"fooooey…", TruncateString(str, 14, WORD_BREAK));
EXPECT_EQ(u"fooooey bx…", TruncateString(str, 14, CHARACTER_BREAK));
// Test breaking in whitespace at the end of the string.
EXPECT_EQ(u"fooooey bxxxar baz…", TruncateString(str, 22, WORD_BREAK));
EXPECT_EQ(u"fooooey bxxxar baz…",
TruncateString(str, 22, CHARACTER_BREAK));
// Test breaking at the end of the string.
EXPECT_EQ(str, TruncateString(str, str.length(), WORD_BREAK));
EXPECT_EQ(str, TruncateString(str, str.length(), CHARACTER_BREAK));
// Test breaking past the end of the string.
EXPECT_EQ(str, TruncateString(str, str.length() + 10, WORD_BREAK));
EXPECT_EQ(str, TruncateString(str, str.length() + 10, CHARACTER_BREAK));
// Tests of strings with leading whitespace:
std::u16string str2 = u" foo";
// Test breaking in leading whitespace.
EXPECT_EQ(u"…", TruncateString(str2, 2, WORD_BREAK));
EXPECT_EQ(u"…", TruncateString(str2, 2, CHARACTER_BREAK));
// Test breaking at the beginning of the first word, with leading whitespace.
EXPECT_EQ(u"…", TruncateString(str2, 3, WORD_BREAK));
EXPECT_EQ(u"…", TruncateString(str2, 3, CHARACTER_BREAK));
// Test breaking in the middle of the first word, with leading whitespace.
EXPECT_EQ(u"…", TruncateString(str2, 4, WORD_BREAK));
EXPECT_EQ(u"…", TruncateString(str2, 4, CHARACTER_BREAK));
EXPECT_EQ(u" f…", TruncateString(str2, 5, WORD_BREAK));
EXPECT_EQ(u" f…", TruncateString(str2, 5, CHARACTER_BREAK));
}
} // namespace gfx
|