| 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" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 // tests work might vary per the default font. | 62 // tests work might vary per the default font. |
| 63 // 2) This specific font helps expose the line width exceeding problem as in | 63 // 2) This specific font helps expose the line width exceeding problem as in |
| 64 // ElideRectangleTextCheckLineWidth. | 64 // ElideRectangleTextCheckLineWidth. |
| 65 font = gfx::Font("LucidaGrande", 12); | 65 font = gfx::Font("LucidaGrande", 12); |
| 66 #endif | 66 #endif |
| 67 return font; | 67 return font; |
| 68 } | 68 } |
| 69 | 69 |
| 70 } // namespace | 70 } // namespace |
| 71 | 71 |
| 72 // TODO(ios): Complex eliding is off by one for some of those tests on iOS. | 72 // TODO(ios): This test fails on iOS because iOS version of gfx::GetStringWidth |
| 73 // See crbug.com/154019 | 73 // that calls [NSString sizeWithFont] returns the rounded string width. |
| 74 #if defined(OS_IOS) | 74 #if defined(OS_IOS) |
| 75 #define MAYBE_ElideEmail DISABLED_ElideEmail | 75 #define MAYBE_ElideEmail DISABLED_ElideEmail |
| 76 #else | 76 #else |
| 77 #define MAYBE_ElideEmail ElideEmail | 77 #define MAYBE_ElideEmail ElideEmail |
| 78 #endif | 78 #endif |
| 79 TEST(TextEliderTest, MAYBE_ElideEmail) { | 79 TEST(TextEliderTest, MAYBE_ElideEmail) { |
| 80 const std::string kEllipsisStr(kEllipsis); | 80 const std::string kEllipsisStr(kEllipsis); |
| 81 | 81 |
| 82 // Test emails and their expected elided forms (from which the available | 82 // Test emails and their expected elided forms (from which the available |
| 83 // widths will be derived). | 83 // widths will be derived). |
| (...skipping 30 matching lines...) Expand all Loading... |
| 114 "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"}, | 114 "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"}, |
| 115 {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com", | 115 {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com", |
| 116 "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"}, | 116 "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"}, |
| 117 // Special case: "m..." takes more than half of the available width; thus | 117 // Special case: "m..." takes more than half of the available width; thus |
| 118 // the domain must elide to "l..." and not "l...l" as it must allow enough | 118 // the domain must elide to "l..." and not "l...l" as it must allow enough |
| 119 // space for the minimal username elision although its half of the | 119 // space for the minimal username elision although its half of the |
| 120 // available width would normally allow it to elide to "l...l". | 120 // available width would normally allow it to elide to "l...l". |
| 121 {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr}, | 121 {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr}, |
| 122 }; | 122 }; |
| 123 | 123 |
| 124 const gfx::Font font = GetTestingFont(); | 124 const gfx::Font font; |
| 125 for (size_t i = 0; i < arraysize(testcases); ++i) { | 125 for (size_t i = 0; i < arraysize(testcases); ++i) { |
| 126 const string16 expected_output = UTF8ToUTF16(testcases[i].output); | 126 const string16 expected_output = UTF8ToUTF16(testcases[i].output); |
| 127 int available_width = font.GetStringWidth(expected_output); | |
| 128 #if defined(OS_MACOSX) | |
| 129 // Give two extra pixels to offset the ceiling width returned by | |
| 130 // GetStringWidth on Mac. This workaround will no longer be needed once | |
| 131 // the floating point width is adopted (http://crbug.com/288987). | |
| 132 // Note that we need one more pixel than TestFilenameEliding because | |
| 133 // multiple strings are elided and we need to offset more. | |
| 134 available_width += 2; | |
| 135 #endif | |
| 136 EXPECT_EQ(expected_output, | 127 EXPECT_EQ(expected_output, |
| 137 ElideEmail(UTF8ToUTF16(testcases[i].input), | 128 ElideEmail(UTF8ToUTF16(testcases[i].input), |
| 138 font, | 129 font, |
| 139 available_width)); | 130 font.GetStringWidth(expected_output))); |
| 140 } | 131 } |
| 141 } | 132 } |
| 142 | 133 |
| 143 TEST(TextEliderTest, ElideEmailMoreSpace) { | 134 TEST(TextEliderTest, ElideEmailMoreSpace) { |
| 144 const int test_width_factors[] = { | 135 const int test_width_factors[] = { |
| 145 100, | 136 100, |
| 146 10000, | 137 10000, |
| 147 1000000, | 138 1000000, |
| 148 }; | 139 }; |
| 149 const std::string test_emails[] = { | 140 const std::string test_emails[] = { |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 "C:/" + kEllipsisStr + "/filename"}, | 272 "C:/" + kEllipsisStr + "/filename"}, |
| 282 #endif | 273 #endif |
| 283 {"file://filer/foo/bar/file", "filer/foo/bar/file"}, | 274 {"file://filer/foo/bar/file", "filer/foo/bar/file"}, |
| 284 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"}, | 275 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"}, |
| 285 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"}, | 276 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"}, |
| 286 }; | 277 }; |
| 287 | 278 |
| 288 RunUrlTest(testcases, arraysize(testcases)); | 279 RunUrlTest(testcases, arraysize(testcases)); |
| 289 } | 280 } |
| 290 | 281 |
| 291 // TODO(ios): Complex eliding is off by one for some of those tests on iOS. | 282 // TODO(ios): This test fails on iOS because iOS version of gfx::GetStringWidth |
| 292 // See crbug.com/154019 | 283 // that calls [NSString sizeWithFont] returns the rounded string width. |
| 293 #if defined(OS_IOS) | 284 #if defined(OS_IOS) |
| 294 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding | 285 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding |
| 295 #else | 286 #else |
| 296 #define MAYBE_TestFilenameEliding TestFilenameEliding | 287 #define MAYBE_TestFilenameEliding TestFilenameEliding |
| 297 #endif | 288 #endif |
| 298 TEST(TextEliderTest, MAYBE_TestFilenameEliding) { | 289 TEST(TextEliderTest, MAYBE_TestFilenameEliding) { |
| 299 const std::string kEllipsisStr(kEllipsis); | 290 const std::string kEllipsisStr(kEllipsis); |
| 300 const base::FilePath::StringType kPathSeparator = | 291 const base::FilePath::StringType kPathSeparator = |
| 301 base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]); | 292 base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]); |
| 302 | 293 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 327 {FILE_PATH_LITERAL("filename.middleext.longext"), | 318 {FILE_PATH_LITERAL("filename.middleext.longext"), |
| 328 "filename.mid" + kEllipsisStr + ".longext"}, | 319 "filename.mid" + kEllipsisStr + ".longext"}, |
| 329 {FILE_PATH_LITERAL("filename.superduperextremelylongext"), | 320 {FILE_PATH_LITERAL("filename.superduperextremelylongext"), |
| 330 "filename.sup" + kEllipsisStr + "emelylongext"}, | 321 "filename.sup" + kEllipsisStr + "emelylongext"}, |
| 331 {FILE_PATH_LITERAL("filenamereallylongtext.superduperextremelylongext"), | 322 {FILE_PATH_LITERAL("filenamereallylongtext.superduperextremelylongext"), |
| 332 "filenamereall" + kEllipsisStr + "emelylongext"}, | 323 "filenamereall" + kEllipsisStr + "emelylongext"}, |
| 333 {FILE_PATH_LITERAL("file.name.really.long.text.superduperextremelylongext"), | 324 {FILE_PATH_LITERAL("file.name.really.long.text.superduperextremelylongext"), |
| 334 "file.name.re" + kEllipsisStr + "emelylongext"} | 325 "file.name.re" + kEllipsisStr + "emelylongext"} |
| 335 }; | 326 }; |
| 336 | 327 |
| 337 static const gfx::Font font = GetTestingFont(); | 328 static const gfx::Font font; |
| 338 for (size_t i = 0; i < arraysize(testcases); ++i) { | 329 for (size_t i = 0; i < arraysize(testcases); ++i) { |
| 339 base::FilePath filepath(testcases[i].input); | 330 base::FilePath filepath(testcases[i].input); |
| 340 string16 expected = UTF8ToUTF16(testcases[i].output); | 331 string16 expected = UTF8ToUTF16(testcases[i].output); |
| 341 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected); | 332 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected); |
| 342 int available_width = font.GetStringWidth(UTF8ToUTF16(testcases[i].output)); | 333 EXPECT_EQ(expected, ElideFilename(filepath, font, |
| 343 #if defined(OS_MACOSX) | 334 font.GetStringWidth(UTF8ToUTF16(testcases[i].output)))); |
| 344 // Give one extra pixel to offset the ceiling width returned by | |
| 345 // GetStringWidth on Mac. This workaround will no longer be needed once | |
| 346 // the floating point width is adopted (http://crbug.com/288987). | |
| 347 available_width += 1; | |
| 348 #endif | |
| 349 EXPECT_EQ(expected, ElideFilename(filepath, font, available_width)); | |
| 350 } | 335 } |
| 351 } | 336 } |
| 352 | 337 |
| 353 TEST(TextEliderTest, ElideTextTruncate) { | 338 TEST(TextEliderTest, ElideTextTruncate) { |
| 354 const gfx::Font font; | 339 const gfx::Font font; |
| 355 const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); | 340 const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); |
| 356 struct TestData { | 341 struct TestData { |
| 357 const char* input; | 342 const char* input; |
| 358 int width; | 343 int width; |
| 359 const char* output; | 344 const char* output; |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 728 kAvailableHeight, | 713 kAvailableHeight, |
| 729 cases[i].wrap_behavior, | 714 cases[i].wrap_behavior, |
| 730 &lines)); | 715 &lines)); |
| 731 std::string expected_output(cases[i].output); | 716 std::string expected_output(cases[i].output); |
| 732 ReplaceSubstringsAfterOffset(&expected_output, 0, "...", kEllipsis); | 717 ReplaceSubstringsAfterOffset(&expected_output, 0, "...", kEllipsis); |
| 733 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); | 718 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); |
| 734 EXPECT_EQ(expected_output, result) << "Case " << i << " failed!"; | 719 EXPECT_EQ(expected_output, result) << "Case " << i << " failed!"; |
| 735 } | 720 } |
| 736 } | 721 } |
| 737 | 722 |
| 738 // TODO(ios): Complex eliding is off by one for some of those tests on iOS. | |
| 739 // See crbug.com/154019 | |
| 740 #if defined(OS_IOS) | |
| 741 #define MAYBE_ElideRectangleTextCheckLineWidth \ | |
| 742 DISABLED_ElideRectangleTextCheckLineWidth | |
| 743 #else | |
| 744 #define MAYBE_ElideRectangleTextCheckLineWidth ElideRectangleTextCheckLineWidth | |
| 745 #endif | |
| 746 | |
| 747 // This test is to make sure that the width of each wrapped line does not | 723 // This test is to make sure that the width of each wrapped line does not |
| 748 // exceed the available width. On some platform like Mac, this test used to | 724 // exceed the available width. On some platform like Mac, this test used to |
| 749 // fail because the truncated integer width is returned for the string | 725 // fail because the truncated integer width is returned for the string |
| 750 // and the accumulation of the truncated values causes the elide function | 726 // and the accumulation of the truncated values causes the elide function |
| 751 // to wrap incorrectly. | 727 // to wrap incorrectly. |
| 752 TEST(TextEliderTest, MAYBE_ElideRectangleTextCheckLineWidth) { | 728 TEST(TextEliderTest, ElideRectangleTextCheckLineWidth) { |
| 753 gfx::Font font = GetTestingFont(); | 729 gfx::Font font; |
| 754 const int kAvailableWidth = 235; | 730 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 731 // Use a specific font to expose the line width exceeding problem. |
| 732 font = gfx::Font("LucidaGrande", 12); |
| 733 #endif |
| 734 const float kAvailableWidth = 235; |
| 755 const int kAvailableHeight = 1000; | 735 const int kAvailableHeight = 1000; |
| 756 const char text[] = "that Russian place we used to go to after fencing"; | 736 const char text[] = "that Russian place we used to go to after fencing"; |
| 757 std::vector<string16> lines; | 737 std::vector<string16> lines; |
| 758 EXPECT_EQ(0, ElideRectangleText(UTF8ToUTF16(text), | 738 EXPECT_EQ(0, ElideRectangleText(UTF8ToUTF16(text), |
| 759 font, | 739 font, |
| 760 kAvailableWidth, | 740 kAvailableWidth, |
| 761 kAvailableHeight, | 741 kAvailableHeight, |
| 762 WRAP_LONG_WORDS, | 742 WRAP_LONG_WORDS, |
| 763 &lines)); | 743 &lines)); |
| 764 ASSERT_EQ(2u, lines.size()); | 744 ASSERT_EQ(2u, lines.size()); |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 984 | 964 |
| 985 // Test adds ... at right spot when there is not enough room to break at a | 965 // Test adds ... at right spot when there is not enough room to break at a |
| 986 // word boundary. | 966 // word boundary. |
| 987 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); | 967 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); |
| 988 | 968 |
| 989 // Test completely truncates string if break is on initial whitespace. | 969 // Test completely truncates string if break is on initial whitespace. |
| 990 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); | 970 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); |
| 991 } | 971 } |
| 992 | 972 |
| 993 } // namespace gfx | 973 } // namespace gfx |
| OLD | NEW |