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 Testcase testcases[] = { |
| 62 {"g@g.c", "g@g.c"}, |
| 63 {"g@g.c", kEllipsisStr}, |
| 64 {"ga@co.ca", "ga@c" + kEllipsisStr + "a"}, |
| 65 {"short@small.com", "s" + kEllipsisStr + "@s" + kEllipsisStr}, |
| 66 {"short@small.com", "s" + kEllipsisStr + "@small.com"}, |
| 67 {"short@longbutlotsofspace.com", "short@longbutlotsofspace.com"}, |
| 68 {"short@longbutnotverymuchspace.com", |
| 69 "short@long" + kEllipsisStr + ".com"}, |
| 70 {"short@longbutverytightspace.ca", |
| 71 "sh" + kEllipsisStr + "@l" + kEllipsisStr + "a"}, |
| 72 {"longusername@gmail.com", "long" + kEllipsisStr + "@gmail.com"}, |
| 73 {"elidetothemax@justfits.com", "e" + kEllipsisStr + "@justfits.com"}, |
| 74 {"somelongemail@thatdoesntfit.com", |
| 75 "somelo" + kEllipsisStr + "@tha" + kEllipsisStr + "om"}, |
| 76 {"namefits@butthedomaindoesnt.com", |
| 77 "namefits@butthedo" + kEllipsisStr + "snt.com"}, |
| 78 {"widthtootight@nospace.com", kEllipsisStr}, |
| 79 {"nospaceforusername@l", kEllipsisStr}, |
| 80 {"little@littlespace.com", "l" + kEllipsisStr + "@l" + kEllipsisStr}, |
| 81 {"l@lllllllll.com", "l@lllll" + kEllipsisStr + ".com"}, |
| 82 {"mmm@llllll", "m" + kEllipsisStr + "@l" + kEllipsisStr + "l"}, |
| 83 {"messed\"up@whyanat\"++@notgoogley.com", |
| 84 "messed\"up@whyanat\"++@notgoogley.com"}, |
| 85 {"messed\"up@whyanat\"++@notgoogley.com", |
| 86 "messed\"up@why" + kEllipsisStr + "@notgoogley.com"}, |
| 87 {"messed\"up@whyanat\"++@notgoogley.com", |
| 88 "messe" + kEllipsisStr + "@not" + kEllipsisStr + "om"}, |
| 89 {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com", |
| 90 "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"}, |
| 91 }; |
| 92 |
| 93 const gfx::Font font; |
| 94 for (size_t i = 0; i < arraysize(testcases); ++i) { |
| 95 const string16 expected_output = UTF8ToUTF16(testcases[i].output); |
| 96 EXPECT_EQ(expected_output, |
| 97 ElideEmail(UTF8ToUTF16(testcases[i].input), |
| 98 font, |
| 99 font.GetStringWidth(expected_output))); |
| 100 } |
| 101 } |
| 102 |
| 103 TEST(TextEliderTest, ElideEmailMoreSpace) { |
| 104 const int test_width_factors[] = { |
| 105 100, |
| 106 10000, |
| 107 1000000, |
| 108 }; |
| 109 const std::string test_emails[] = { |
| 110 "a@c", |
| 111 "test@email.com", |
| 112 "short@verysuperdupperlongdomain.com", |
| 113 "supermegalongusername@withasuperlonnnggggdomain.gouv.qc.ca", |
| 114 }; |
| 115 |
| 116 const gfx::Font font; |
| 117 for (size_t i = 0; i < arraysize(test_width_factors); ++i) { |
| 118 const int test_width = test_width_factors[i] * |
| 119 font.GetAverageCharacterWidth(); |
| 120 for (size_t j = 0; j < arraysize(test_emails); ++j) { |
| 121 // Extra space is available: the email should not be elided. |
| 122 const string16 test_email = UTF8ToUTF16(test_emails[j]); |
| 123 EXPECT_EQ(test_email, ElideEmail(test_email, font, test_width)); |
| 124 } |
| 125 } |
| 126 } |
| 127 |
58 // Test eliding of commonplace URLs. | 128 // Test eliding of commonplace URLs. |
59 TEST(TextEliderTest, TestGeneralEliding) { | 129 TEST(TextEliderTest, TestGeneralEliding) { |
60 const std::string kEllipsisStr(kEllipsis); | 130 const std::string kEllipsisStr(kEllipsis); |
61 Testcase testcases[] = { | 131 Testcase testcases[] = { |
62 {"http://www.google.com/intl/en/ads/", | 132 {"http://www.google.com/intl/en/ads/", |
63 "www.google.com/intl/en/ads/"}, | 133 "www.google.com/intl/en/ads/"}, |
64 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, | 134 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, |
65 {"http://www.google.com/intl/en/ads/", | 135 {"http://www.google.com/intl/en/ads/", |
66 "google.com/intl/" + kEllipsisStr + "/ads/"}, | 136 "google.com/intl/" + kEllipsisStr + "/ads/"}, |
67 {"http://www.google.com/intl/en/ads/", | 137 {"http://www.google.com/intl/en/ads/", |
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 | 850 |
781 // Test adds ... at right spot when there is not enough room to break at a | 851 // Test adds ... at right spot when there is not enough room to break at a |
782 // word boundary. | 852 // word boundary. |
783 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); | 853 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); |
784 | 854 |
785 // Test completely truncates string if break is on initial whitespace. | 855 // Test completely truncates string if break is on initial whitespace. |
786 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); | 856 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); |
787 } | 857 } |
788 | 858 |
789 } // namespace ui | 859 } // namespace ui |
OLD | NEW |