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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 const GURL url(testcases[i].input); | 46 const GURL url(testcases[i].input); |
47 // Should we test with non-empty language list? | 47 // Should we test with non-empty language list? |
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 void RunEmailTest(Testcase* testcases, |
| 57 size_t num_testcases, |
| 58 size_t max_email_length) { |
| 59 for (size_t i = 0; i < num_testcases; ++i) { |
| 60 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), |
| 61 ElideEmail(UTF8ToUTF16(testcases[i].input), max_email_length)); |
| 62 } |
| 63 } |
| 64 |
56 } // namespace | 65 } // namespace |
57 | 66 |
| 67 TEST(TextEliderTest, SmallestElideEmail) { |
| 68 const std::string kEllipsisStr(kEllipsis); |
| 69 |
| 70 // Make sure the ellipsis is of length 1 in UTF16 as all the tests below |
| 71 // assume this to be true. |
| 72 ASSERT_EQ(UTF8ToUTF16(kEllipsisStr).length(), size_t(1)); |
| 73 |
| 74 // 5 is the smallest max_email_length allowed in ui::ElideEmail |
| 75 const size_t max_email_length = 5; |
| 76 |
| 77 Testcase testcases[] = { |
| 78 {"g@g.c", "g@g.c"}, |
| 79 {"ga@co", "ga@co"}, |
| 80 {"short@small.com", "s" + kEllipsisStr + "@s" + kEllipsisStr}, |
| 81 }; |
| 82 |
| 83 RunEmailTest(testcases, arraysize(testcases), max_email_length); |
| 84 } |
| 85 |
| 86 TEST(TextEliderTest, RegularElideEmail) { |
| 87 const std::string kEllipsisStr(kEllipsis); |
| 88 |
| 89 // Make sure the ellipsis is of length 1 in UTF16 as all the tests below |
| 90 // assume this to be true. |
| 91 ASSERT_EQ(UTF8ToUTF16(kEllipsisStr).length(), size_t(1)); |
| 92 |
| 93 const size_t max_email_length = 20; |
| 94 |
| 95 Testcase testcases[] = { |
| 96 {"short@small.com", "short@small.com"}, |
| 97 {"justunder@test19.ca", "justunder@test19.ca"}, |
| 98 {"exactly@righton20.ca", "exactly@righton20.ca"}, |
| 99 {"justover20@test21.com", "justover" + kEllipsisStr + "@test21.com"}, |
| 100 {"longusernameshortdomain@short.com", "longusern" + kEllipsisStr + |
| 101 "@short.com"}, |
| 102 {"short@verysuperlongdomain.com", "short@verys" + kEllipsisStr + ".com"}, |
| 103 {"waytoobigofanemail@somealsoverybigdomain.com", "waytoobi" + |
| 104 kEllipsisStr + |
| 105 "@somea" + |
| 106 kEllipsisStr + |
| 107 ".com"}, |
| 108 {"toolongusername@maxsizedomain.com", "t" + kEllipsisStr + |
| 109 "@maxsizedomain.com"}, |
| 110 {"toolongusername@justovermaxof17.tv", "toolongu" + kEllipsisStr + |
| 111 "@justo" + kEllipsisStr + |
| 112 "7.tv"}, |
| 113 }; |
| 114 |
| 115 RunEmailTest(testcases, arraysize(testcases), max_email_length); |
| 116 } |
| 117 |
58 // Test eliding of commonplace URLs. | 118 // Test eliding of commonplace URLs. |
59 TEST(TextEliderTest, TestGeneralEliding) { | 119 TEST(TextEliderTest, TestGeneralEliding) { |
60 const std::string kEllipsisStr(kEllipsis); | 120 const std::string kEllipsisStr(kEllipsis); |
61 Testcase testcases[] = { | 121 Testcase testcases[] = { |
62 {"http://www.google.com/intl/en/ads/", | 122 {"http://www.google.com/intl/en/ads/", |
63 "www.google.com/intl/en/ads/"}, | 123 "www.google.com/intl/en/ads/"}, |
64 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, | 124 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, |
65 {"http://www.google.com/intl/en/ads/", | 125 {"http://www.google.com/intl/en/ads/", |
66 "google.com/intl/" + kEllipsisStr + "/ads/"}, | 126 "google.com/intl/" + kEllipsisStr + "/ads/"}, |
67 {"http://www.google.com/intl/en/ads/", | 127 {"http://www.google.com/intl/en/ads/", |
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 | 840 |
781 // Test adds ... at right spot when there is not enough room to break at a | 841 // Test adds ... at right spot when there is not enough room to break at a |
782 // word boundary. | 842 // word boundary. |
783 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); | 843 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); |
784 | 844 |
785 // Test completely truncates string if break is on initial whitespace. | 845 // Test completely truncates string if break is on initial whitespace. |
786 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); | 846 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); |
787 } | 847 } |
788 | 848 |
789 } // namespace ui | 849 } // namespace ui |
OLD | NEW |