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 }; |
| 84 |
| 85 const gfx::Font font; |
| 86 for (size_t i = 0; i < arraysize(testcases); ++i) { |
| 87 const string16 expected_output = UTF8ToUTF16(testcases[i].output); |
| 88 EXPECT_EQ(expected_output, |
| 89 ElideEmail(UTF8ToUTF16(testcases[i].input), |
| 90 font, |
| 91 font.GetStringWidth(expected_output))); |
| 92 } |
| 93 } |
| 94 |
| 95 TEST(TextEliderTest, ElideEmailMoreSpace) { |
| 96 const int test_width_factors[] = { |
| 97 100, |
| 98 10000, |
| 99 1000000, |
| 100 }; |
| 101 const std::string test_emails[] = { |
| 102 "a@c", |
| 103 "test@email.com", |
| 104 "short@verysuperdupperlongdomain.com", |
| 105 "supermegalongusername@withasuperlonnnggggdomain.gouv.qc.ca", |
| 106 }; |
| 107 |
| 108 const gfx::Font font; |
| 109 for (size_t i = 0; i < arraysize(test_width_factors); ++i) { |
| 110 const int test_width = test_width_factors[i] * |
| 111 font.GetAverageCharacterWidth(); |
| 112 for (size_t j = 0; j < arraysize(test_emails); ++j) { |
| 113 // Extra space is available: the email should not be elided. |
| 114 const string16 test_email = UTF8ToUTF16(test_emails[j]); |
| 115 EXPECT_EQ(test_email, ElideEmail(test_email, font, test_width)); |
| 116 } |
| 117 } |
| 118 } |
| 119 |
58 // Test eliding of commonplace URLs. | 120 // Test eliding of commonplace URLs. |
59 TEST(TextEliderTest, TestGeneralEliding) { | 121 TEST(TextEliderTest, TestGeneralEliding) { |
60 const std::string kEllipsisStr(kEllipsis); | 122 const std::string kEllipsisStr(kEllipsis); |
61 Testcase testcases[] = { | 123 Testcase testcases[] = { |
62 {"http://www.google.com/intl/en/ads/", | 124 {"http://www.google.com/intl/en/ads/", |
63 "www.google.com/intl/en/ads/"}, | 125 "www.google.com/intl/en/ads/"}, |
64 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, | 126 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, |
65 {"http://www.google.com/intl/en/ads/", | 127 {"http://www.google.com/intl/en/ads/", |
66 "google.com/intl/" + kEllipsisStr + "/ads/"}, | 128 "google.com/intl/" + kEllipsisStr + "/ads/"}, |
67 {"http://www.google.com/intl/en/ads/", | 129 {"http://www.google.com/intl/en/ads/", |
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 | 842 |
781 // 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 |
782 // word boundary. | 844 // word boundary. |
783 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); | 845 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); |
784 | 846 |
785 // Test completely truncates string if break is on initial whitespace. | 847 // Test completely truncates string if break is on initial whitespace. |
786 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); | 848 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); |
787 } | 849 } |
788 | 850 |
789 } // namespace ui | 851 } // namespace ui |
OLD | NEW |