Chromium Code Reviews| 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/base/text/text_elider.h" | 7 #include "ui/base/text/text_elider.h" |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 48 // That's kinda redundant with net_util_unittests. | 48 // That's kinda redundant with net_util_unittests. |
| 49 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), | 49 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), |
| 50 ElideUrl(url, font, | 50 ElideUrl(url, font, |
| 51 font.GetStringWidth(UTF8ToUTF16(testcases[i].output)), | 51 font.GetStringWidth(UTF8ToUTF16(testcases[i].output)), |
| 52 std::string())); | 52 std::string())); |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 | 55 |
| 56 } // namespace | 56 } // namespace |
| 57 | 57 |
| 58 TEST(TextEliderTest, ElideEmail) { | |
| 59 const std::string kEllipsisStr(kEllipsis); | |
| 60 | |
| 61 // Test emails and their expected elided forms (which the available widths | |
|
Alexei Svitkine (slow)
2012/03/13 15:36:54
Nit: "from which the available widths will be deri
gab
2012/03/13 15:49:54
Fixed
| |
| 62 // will be derived from). | |
| 63 // For elided forms in which both the username and domain must be elided: | |
| 64 // the result (how many characters are left on each side) can be font | |
| 65 // dependent. To avoid this, the username is prefixed with the characters | |
| 66 // expected to remain in the domain. | |
| 67 Testcase testcases[] = { | |
| 68 {"g@g.c", "g@g.c"}, | |
| 69 {"g@g.c", kEllipsisStr}, | |
| 70 {"ga@co.ca", "ga@c" + kEllipsisStr + "a"}, | |
| 71 {"short@small.com", "s" + kEllipsisStr + "@s" + kEllipsisStr}, | |
| 72 {"short@small.com", "s" + kEllipsisStr + "@small.com"}, | |
| 73 {"short@longbutlotsofspace.com", "short@longbutlotsofspace.com"}, | |
| 74 {"short@longbutnotverymuchspace.com", | |
| 75 "short@long" + kEllipsisStr + ".com"}, | |
| 76 {"la_short@longbutverytightspace.ca", | |
| 77 "la" + kEllipsisStr + "@l" + kEllipsisStr + "a"}, | |
| 78 {"longusername@gmail.com", "long" + kEllipsisStr + "@gmail.com"}, | |
| 79 {"elidetothemax@justfits.com", "e" + kEllipsisStr + "@justfits.com"}, | |
| 80 {"thatom_somelongemail@thatdoesntfit.com", | |
| 81 "thatom" + kEllipsisStr + "@tha" + kEllipsisStr + "om"}, | |
| 82 {"namefits@butthedomaindoesnt.com", | |
| 83 "namefits@butthedo" + kEllipsisStr + "snt.com"}, | |
| 84 {"widthtootight@nospace.com", kEllipsisStr}, | |
| 85 {"nospaceforusername@l", kEllipsisStr}, | |
| 86 {"little@littlespace.com", "l" + kEllipsisStr + "@l" + kEllipsisStr}, | |
| 87 {"l@lllllllll.com", "l@lllll" + kEllipsisStr + ".com"}, | |
| 88 {"messed\"up@whyanat\"++@notgoogley.com", | |
| 89 "messed\"up@whyanat\"++@notgoogley.com"}, | |
| 90 {"messed\"up@whyanat\"++@notgoogley.com", | |
| 91 "messed\"up@why" + kEllipsisStr + "@notgoogley.com"}, | |
| 92 {"noca_messed\"up@whyanat\"++@notgoogley.ca", | |
| 93 "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"}, | |
| 94 {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com", | |
| 95 "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"}, | |
| 96 // Special case: "m..." takes more than half of the available width; thus | |
| 97 // the domain must elide to "l..." and not "l...l" as it must allow enough | |
| 98 // space for the minimal username elision although its half of the | |
| 99 // available width would normally allow it to elide to "l...l". | |
| 100 {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr}, | |
| 101 }; | |
| 102 | |
| 103 const gfx::Font font; | |
| 104 for (size_t i = 0; i < arraysize(testcases); ++i) { | |
| 105 const string16 expected_output = UTF8ToUTF16(testcases[i].output); | |
| 106 EXPECT_EQ(expected_output, | |
| 107 ElideEmail(UTF8ToUTF16(testcases[i].input), | |
| 108 font, | |
| 109 font.GetStringWidth(expected_output))); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 TEST(TextEliderTest, ElideEmailMoreSpace) { | |
| 114 const int test_width_factors[] = { | |
| 115 100, | |
| 116 10000, | |
| 117 1000000, | |
| 118 }; | |
| 119 const std::string test_emails[] = { | |
| 120 "a@c", | |
| 121 "test@email.com", | |
| 122 "short@verysuperdupperlongdomain.com", | |
| 123 "supermegalongusername@withasuperlonnnggggdomain.gouv.qc.ca", | |
| 124 }; | |
| 125 | |
| 126 const gfx::Font font; | |
| 127 for (size_t i = 0; i < arraysize(test_width_factors); ++i) { | |
| 128 const int test_width = test_width_factors[i] * | |
| 129 font.GetAverageCharacterWidth(); | |
| 130 for (size_t j = 0; j < arraysize(test_emails); ++j) { | |
| 131 // Extra space is available: the email should not be elided. | |
| 132 const string16 test_email = UTF8ToUTF16(test_emails[j]); | |
| 133 EXPECT_EQ(test_email, ElideEmail(test_email, font, test_width)); | |
| 134 } | |
| 135 } | |
| 136 } | |
| 137 | |
| 58 // Test eliding of commonplace URLs. | 138 // Test eliding of commonplace URLs. |
| 59 TEST(TextEliderTest, TestGeneralEliding) { | 139 TEST(TextEliderTest, TestGeneralEliding) { |
| 60 const std::string kEllipsisStr(kEllipsis); | 140 const std::string kEllipsisStr(kEllipsis); |
| 61 Testcase testcases[] = { | 141 Testcase testcases[] = { |
| 62 {"http://www.google.com/intl/en/ads/", | 142 {"http://www.google.com/intl/en/ads/", |
| 63 "www.google.com/intl/en/ads/"}, | 143 "www.google.com/intl/en/ads/"}, |
| 64 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, | 144 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, |
| 65 {"http://www.google.com/intl/en/ads/", | 145 {"http://www.google.com/intl/en/ads/", |
| 66 "google.com/intl/" + kEllipsisStr + "/ads/"}, | 146 "google.com/intl/" + kEllipsisStr + "/ads/"}, |
| 67 {"http://www.google.com/intl/en/ads/", | 147 {"http://www.google.com/intl/en/ads/", |
| (...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 780 | 860 |
| 781 // Test adds ... at right spot when there is not enough room to break at a | 861 // Test adds ... at right spot when there is not enough room to break at a |
| 782 // word boundary. | 862 // word boundary. |
| 783 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); | 863 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); |
| 784 | 864 |
| 785 // Test completely truncates string if break is on initial whitespace. | 865 // Test completely truncates string if break is on initial whitespace. |
| 786 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); | 866 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); |
| 787 } | 867 } |
| 788 | 868 |
| 789 } // namespace ui | 869 } // namespace ui |
| OLD | NEW |