[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | // |
| 5 | // Unit tests for eliding and formatting utility functions. |
| 6 | |
| 7 | #include "ui/gfx/text_elider.h" |
| 8 | |
| 9 | #include "base/files/file_path.h" |
| 10 | #include "base/i18n/rtl.h" |
| 11 | #include "base/memory/scoped_ptr.h" |
| 12 | #include "base/strings/string_util.h" |
| 13 | #include "base/strings/utf_string_conversions.h" |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | #include "ui/gfx/font.h" |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 16 | #include "ui/gfx/font_list.h" |
| 17 | #include "ui/gfx/text_utils.h" |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 18 | |
[email protected] | dd2cc80 | 2013-12-25 20:09:36 | [diff] [blame] | 19 | using base::ASCIIToUTF16; |
| 20 | using base::UTF16ToUTF8; |
[email protected] | f729d7a | 2013-12-26 07:07:56 | [diff] [blame] | 21 | using base::UTF16ToWide; |
[email protected] | dd2cc80 | 2013-12-25 20:09:36 | [diff] [blame] | 22 | using base::UTF8ToUTF16; |
| 23 | using base::WideToUTF16; |
| 24 | |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 25 | namespace gfx { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | struct Testcase { |
| 30 | const std::string input; |
| 31 | const std::string output; |
| 32 | }; |
| 33 | |
| 34 | struct FileTestcase { |
| 35 | const base::FilePath::StringType input; |
| 36 | const std::string output; |
| 37 | }; |
| 38 | |
| 39 | struct UTF16Testcase { |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 40 | const base::string16 input; |
| 41 | const base::string16 output; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | struct TestData { |
| 45 | const std::string a; |
| 46 | const std::string b; |
| 47 | const int compare_result; |
| 48 | }; |
| 49 | |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 50 | } // namespace |
| 51 | |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 52 | // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF |
| 53 | // that calls [NSString sizeWithFont] returns the rounded string width. |
[email protected] | ebc9b66 | 2014-01-30 03:37:33 | [diff] [blame] | 54 | // TODO(338784): Enable this on android. |
| 55 | #if defined(OS_IOS) || defined(OS_ANDROID) |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 56 | #define MAYBE_ElideEmail DISABLED_ElideEmail |
| 57 | #else |
| 58 | #define MAYBE_ElideEmail ElideEmail |
| 59 | #endif |
| 60 | TEST(TextEliderTest, MAYBE_ElideEmail) { |
| 61 | const std::string kEllipsisStr(kEllipsis); |
| 62 | |
| 63 | // Test emails and their expected elided forms (from which the available |
| 64 | // widths will be derived). |
| 65 | // For elided forms in which both the username and domain must be elided: |
| 66 | // the result (how many characters are left on each side) can be font |
| 67 | // dependent. To avoid this, the username is prefixed with the characters |
| 68 | // expected to remain in the domain. |
| 69 | Testcase testcases[] = { |
| 70 | {"[email protected]", "[email protected]"}, |
| 71 | {"[email protected]", kEllipsisStr}, |
| 72 | {"[email protected]", "ga@c" + kEllipsisStr + "a"}, |
| 73 | {"[email protected]", "s" + kEllipsisStr + "@s" + kEllipsisStr}, |
| 74 | {"[email protected]", "s" + kEllipsisStr + "@small.com"}, |
| 75 | {"[email protected]", "[email protected]"}, |
| 76 | {"[email protected]", |
| 77 | "short@long" + kEllipsisStr + ".com"}, |
| 78 | {"[email protected]", |
| 79 | "la" + kEllipsisStr + "@l" + kEllipsisStr + "a"}, |
| 80 | {"[email protected]", "long" + kEllipsisStr + "@gmail.com"}, |
| 81 | {"[email protected]", "e" + kEllipsisStr + "@justfits.com"}, |
| 82 | {"[email protected]", |
| 83 | "thatom" + kEllipsisStr + "@tha" + kEllipsisStr + "om"}, |
| 84 | {"[email protected]", |
| 85 | "namefits@butthedo" + kEllipsisStr + "snt.com"}, |
| 86 | {"[email protected]", kEllipsisStr}, |
| 87 | {"nospaceforusername@l", kEllipsisStr}, |
| 88 | {"[email protected]", "l" + kEllipsisStr + "@l" + kEllipsisStr}, |
| 89 | {"[email protected]", "l@lllll" + kEllipsisStr + ".com"}, |
| 90 | {"messed\"up@whyanat\"[email protected]", |
| 91 | "messed\"up@whyanat\"[email protected]"}, |
| 92 | {"messed\"up@whyanat\"[email protected]", |
| 93 | "messed\"up@why" + kEllipsisStr + "@notgoogley.com"}, |
| 94 | {"noca_messed\"up@whyanat\"[email protected]", |
| 95 | "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"}, |
| 96 | {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com", |
| 97 | "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"}, |
| 98 | // Special case: "m..." takes more than half of the available width; thus |
| 99 | // the domain must elide to "l..." and not "l...l" as it must allow enough |
| 100 | // space for the minimal username elision although its half of the |
| 101 | // available width would normally allow it to elide to "l...l". |
| 102 | {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr}, |
| 103 | }; |
| 104 | |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 105 | const FontList font_list; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 106 | for (size_t i = 0; i < arraysize(testcases); ++i) { |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 107 | const base::string16 expected_output = UTF8ToUTF16(testcases[i].output); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 108 | EXPECT_EQ(expected_output, |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 109 | ElideText(UTF8ToUTF16(testcases[i].input), font_list, |
| 110 | GetStringWidthF(expected_output, font_list), |
| 111 | ELIDE_EMAIL)); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
[email protected] | ebc9b66 | 2014-01-30 03:37:33 | [diff] [blame] | 115 | // TODO(338784): Enable this on android. |
| 116 | #if defined(OS_ANDROID) |
| 117 | #define MAYBE_ElideEmailMoreSpace DISABLED_ElideEmailMoreSpace |
| 118 | #else |
| 119 | #define MAYBE_ElideEmailMoreSpace ElideEmailMoreSpace |
| 120 | #endif |
| 121 | TEST(TextEliderTest, MAYBE_ElideEmailMoreSpace) { |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 122 | const int test_width_factors[] = { |
| 123 | 100, |
| 124 | 10000, |
| 125 | 1000000, |
| 126 | }; |
| 127 | const std::string test_emails[] = { |
| 128 | "a@c", |
| 129 | "[email protected]", |
| 130 | "[email protected]", |
| 131 | "[email protected]", |
| 132 | }; |
| 133 | |
[email protected] | 2859ff35 | 2013-11-08 15:58:49 | [diff] [blame] | 134 | const FontList font_list; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 135 | for (size_t i = 0; i < arraysize(test_width_factors); ++i) { |
[email protected] | 2859ff35 | 2013-11-08 15:58:49 | [diff] [blame] | 136 | const int test_width = |
| 137 | font_list.GetExpectedTextWidth(test_width_factors[i]); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 138 | for (size_t j = 0; j < arraysize(test_emails); ++j) { |
| 139 | // Extra space is available: the email should not be elided. |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 140 | const base::string16 test_email = UTF8ToUTF16(test_emails[j]); |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 141 | EXPECT_EQ(test_email, |
| 142 | ElideText(test_email, font_list, test_width, ELIDE_EMAIL)); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 147 | // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF |
| 148 | // that calls [NSString sizeWithFont] returns the rounded string width. |
[email protected] | ebc9b66 | 2014-01-30 03:37:33 | [diff] [blame] | 149 | // TODO(338784): Enable this on android. |
| 150 | #if defined(OS_IOS) || defined(OS_ANDROID) |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 151 | #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding |
| 152 | #else |
| 153 | #define MAYBE_TestFilenameEliding TestFilenameEliding |
| 154 | #endif |
| 155 | TEST(TextEliderTest, MAYBE_TestFilenameEliding) { |
| 156 | const std::string kEllipsisStr(kEllipsis); |
| 157 | const base::FilePath::StringType kPathSeparator = |
| 158 | base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]); |
| 159 | |
| 160 | FileTestcase testcases[] = { |
| 161 | {FILE_PATH_LITERAL(""), ""}, |
| 162 | {FILE_PATH_LITERAL("."), "."}, |
| 163 | {FILE_PATH_LITERAL("filename.exe"), "filename.exe"}, |
| 164 | {FILE_PATH_LITERAL(".longext"), ".longext"}, |
| 165 | {FILE_PATH_LITERAL("pie"), "pie"}, |
| 166 | {FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") + |
| 167 | kPathSeparator + FILE_PATH_LITERAL("filename.pie"), |
| 168 | "filename.pie"}, |
| 169 | {FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") + |
| 170 | kPathSeparator + FILE_PATH_LITERAL("longfilename.pie"), |
| 171 | "long" + kEllipsisStr + ".pie"}, |
| 172 | {FILE_PATH_LITERAL("http://path.com/filename.pie"), "filename.pie"}, |
| 173 | {FILE_PATH_LITERAL("http://path.com/longfilename.pie"), |
| 174 | "long" + kEllipsisStr + ".pie"}, |
| 175 | {FILE_PATH_LITERAL("piesmashingtacularpants"), "pie" + kEllipsisStr}, |
| 176 | {FILE_PATH_LITERAL(".piesmashingtacularpants"), ".pie" + kEllipsisStr}, |
| 177 | {FILE_PATH_LITERAL("cheese."), "cheese."}, |
| 178 | {FILE_PATH_LITERAL("file name.longext"), |
| 179 | "file" + kEllipsisStr + ".longext"}, |
| 180 | {FILE_PATH_LITERAL("fil ename.longext"), |
| 181 | "fil " + kEllipsisStr + ".longext"}, |
| 182 | {FILE_PATH_LITERAL("filename.longext"), |
| 183 | "file" + kEllipsisStr + ".longext"}, |
| 184 | {FILE_PATH_LITERAL("filename.middleext.longext"), |
| 185 | "filename.mid" + kEllipsisStr + ".longext"}, |
| 186 | {FILE_PATH_LITERAL("filename.superduperextremelylongext"), |
| 187 | "filename.sup" + kEllipsisStr + "emelylongext"}, |
| 188 | {FILE_PATH_LITERAL("filenamereallylongtext.superduperextremelylongext"), |
| 189 | "filenamereall" + kEllipsisStr + "emelylongext"}, |
| 190 | {FILE_PATH_LITERAL("file.name.really.long.text.superduperextremelylongext"), |
| 191 | "file.name.re" + kEllipsisStr + "emelylongext"} |
| 192 | }; |
| 193 | |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 194 | static const FontList font_list; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 195 | for (size_t i = 0; i < arraysize(testcases); ++i) { |
| 196 | base::FilePath filepath(testcases[i].input); |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 197 | base::string16 expected = UTF8ToUTF16(testcases[i].output); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 198 | expected = base::i18n::GetDisplayStringInLTRDirectionality(expected); |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 199 | EXPECT_EQ(expected, ElideFilename(filepath, font_list, |
| 200 | GetStringWidthF(UTF8ToUTF16(testcases[i].output), font_list))); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
[email protected] | ebc9b66 | 2014-01-30 03:37:33 | [diff] [blame] | 204 | // TODO(338784): Enable this on android. |
| 205 | #if defined(OS_ANDROID) |
| 206 | #define MAYBE_ElideTextTruncate DISABLED_ElideTextTruncate |
| 207 | #else |
| 208 | #define MAYBE_ElideTextTruncate ElideTextTruncate |
| 209 | #endif |
| 210 | TEST(TextEliderTest, MAYBE_ElideTextTruncate) { |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 211 | const FontList font_list; |
| 212 | const float kTestWidth = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 213 | struct TestData { |
| 214 | const char* input; |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 215 | float width; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 216 | const char* output; |
| 217 | } cases[] = { |
| 218 | { "", 0, "" }, |
| 219 | { "Test", 0, "" }, |
| 220 | { "", kTestWidth, "" }, |
| 221 | { "Tes", kTestWidth, "Tes" }, |
| 222 | { "Test", kTestWidth, "Test" }, |
| 223 | { "Tests", kTestWidth, "Test" }, |
| 224 | }; |
| 225 | |
viettrungluu | a5ca99b | 2014-10-16 16:28:48 | [diff] [blame^] | 226 | for (size_t i = 0; i < arraysize(cases); ++i) { |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 227 | base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 228 | cases[i].width, TRUNCATE); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 229 | EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); |
| 230 | } |
| 231 | } |
| 232 | |
[email protected] | ebc9b66 | 2014-01-30 03:37:33 | [diff] [blame] | 233 | // TODO(338784): Enable this on android. |
| 234 | #if defined(OS_ANDROID) |
| 235 | #define MAYBE_ElideTextEllipsis DISABLED_ElideTextEllipsis |
| 236 | #else |
| 237 | #define MAYBE_ElideTextEllipsis ElideTextEllipsis |
| 238 | #endif |
| 239 | TEST(TextEliderTest, MAYBE_ElideTextEllipsis) { |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 240 | const FontList font_list; |
| 241 | const float kTestWidth = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 242 | const char* kEllipsis = "\xE2\x80\xA6"; |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 243 | const float kEllipsisWidth = |
| 244 | GetStringWidthF(UTF8ToUTF16(kEllipsis), font_list); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 245 | struct TestData { |
| 246 | const char* input; |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 247 | float width; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 248 | const char* output; |
| 249 | } cases[] = { |
| 250 | { "", 0, "" }, |
| 251 | { "Test", 0, "" }, |
| 252 | { "Test", kEllipsisWidth, kEllipsis }, |
| 253 | { "", kTestWidth, "" }, |
| 254 | { "Tes", kTestWidth, "Tes" }, |
| 255 | { "Test", kTestWidth, "Test" }, |
| 256 | }; |
| 257 | |
viettrungluu | a5ca99b | 2014-10-16 16:28:48 | [diff] [blame^] | 258 | for (size_t i = 0; i < arraysize(cases); ++i) { |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 259 | base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 260 | cases[i].width, ELIDE_TAIL); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 261 | EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); |
| 262 | } |
| 263 | } |
| 264 | |
[email protected] | 9d3e32b3 | 2014-02-04 16:01:47 | [diff] [blame] | 265 | // TODO(338784): Enable this on android. |
| 266 | #if defined(OS_ANDROID) |
| 267 | #define MAYBE_ElideTextEllipsisFront DISABLED_ElideTextEllipsisFront |
| 268 | #else |
| 269 | #define MAYBE_ElideTextEllipsisFront ElideTextEllipsisFront |
| 270 | #endif |
| 271 | TEST(TextEliderTest, MAYBE_ElideTextEllipsisFront) { |
| 272 | const FontList font_list; |
| 273 | const float kTestWidth = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
| 274 | const std::string kEllipsisStr(kEllipsis); |
| 275 | const float kEllipsisWidth = |
| 276 | GetStringWidthF(UTF8ToUTF16(kEllipsis), font_list); |
| 277 | const float kEllipsis23Width = |
| 278 | GetStringWidthF(UTF8ToUTF16(kEllipsisStr + "23"), font_list); |
| 279 | struct TestData { |
| 280 | const char* input; |
| 281 | float width; |
| 282 | const base::string16 output; |
| 283 | } cases[] = { |
| 284 | { "", 0, base::string16() }, |
| 285 | { "Test", 0, base::string16() }, |
| 286 | { "Test", kEllipsisWidth, UTF8ToUTF16(kEllipsisStr) }, |
| 287 | { "", kTestWidth, base::string16() }, |
| 288 | { "Tes", kTestWidth, ASCIIToUTF16("Tes") }, |
| 289 | { "Test", kTestWidth, ASCIIToUTF16("Test") }, |
| 290 | { "Test123", kEllipsis23Width, UTF8ToUTF16(kEllipsisStr + "23") }, |
| 291 | }; |
| 292 | |
viettrungluu | a5ca99b | 2014-10-16 16:28:48 | [diff] [blame^] | 293 | for (size_t i = 0; i < arraysize(cases); ++i) { |
[email protected] | 9d3e32b3 | 2014-02-04 16:01:47 | [diff] [blame] | 294 | base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 295 | cases[i].width, ELIDE_HEAD); |
[email protected] | 9d3e32b3 | 2014-02-04 16:01:47 | [diff] [blame] | 296 | EXPECT_EQ(cases[i].output, result); |
| 297 | } |
| 298 | } |
| 299 | |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 300 | // Checks that all occurrences of |first_char| are followed by |second_char| and |
| 301 | // all occurrences of |second_char| are preceded by |first_char| in |text|. |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 302 | static void CheckSurrogatePairs(const base::string16& text, |
[email protected] | c84c479 | 2013-12-25 21:57:26 | [diff] [blame] | 303 | base::char16 first_char, |
| 304 | base::char16 second_char) { |
[email protected] | 8d93614 | 2014-07-10 21:40:37 | [diff] [blame] | 305 | for (size_t index = 0; index < text.length(); ++index) { |
| 306 | EXPECT_NE(second_char, text[index]); |
| 307 | if (text[index] == first_char) { |
| 308 | ASSERT_LT(++index, text.length()); |
| 309 | EXPECT_EQ(second_char, text[index]); |
| 310 | } |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
[email protected] | ebc9b66 | 2014-01-30 03:37:33 | [diff] [blame] | 314 | // TODO(338784): Enable this on android. |
| 315 | #if defined(OS_ANDROID) |
| 316 | #define MAYBE_ElideTextSurrogatePairs DISABLED_ElideTextSurrogatePairs |
| 317 | #else |
| 318 | #define MAYBE_ElideTextSurrogatePairs ElideTextSurrogatePairs |
| 319 | #endif |
| 320 | TEST(TextEliderTest, MAYBE_ElideTextSurrogatePairs) { |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 321 | const FontList font_list; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 322 | // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as |
| 323 | // two characters forming a surrogate pair 0x0001D11E. |
| 324 | const std::string kSurrogate = "\xF0\x9D\x84\x9E"; |
[email protected] | 8d93614 | 2014-07-10 21:40:37 | [diff] [blame] | 325 | const base::string16 kTestString = UTF8ToUTF16(kSurrogate + "x" + kSurrogate); |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 326 | const float kTestStringWidth = GetStringWidthF(kTestString, font_list); |
[email protected] | c84c479 | 2013-12-25 21:57:26 | [diff] [blame] | 327 | const base::char16 kSurrogateFirstChar = kTestString[0]; |
| 328 | const base::char16 kSurrogateSecondChar = kTestString[1]; |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 329 | base::string16 result; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 330 | |
| 331 | // Elide |kTextString| to all possible widths and check that no instance of |
| 332 | // |kSurrogate| was split in two. |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 333 | for (float width = 0; width <= kTestStringWidth; width++) { |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 334 | result = ElideText(kTestString, font_list, width, TRUNCATE); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 335 | CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
| 336 | |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 337 | result = ElideText(kTestString, font_list, width, ELIDE_TAIL); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 338 | CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
| 339 | |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 340 | result = ElideText(kTestString, font_list, width, ELIDE_MIDDLE); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 341 | CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
[email protected] | 9d3e32b3 | 2014-02-04 16:01:47 | [diff] [blame] | 342 | |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 343 | result = ElideText(kTestString, font_list, width, ELIDE_HEAD); |
[email protected] | 9d3e32b3 | 2014-02-04 16:01:47 | [diff] [blame] | 344 | CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
[email protected] | ebc9b66 | 2014-01-30 03:37:33 | [diff] [blame] | 348 | // TODO(338784): Enable this on android. |
| 349 | #if defined(OS_ANDROID) |
| 350 | #define MAYBE_ElideTextLongStrings DISABLED_ElideTextLongStrings |
| 351 | #else |
| 352 | #define MAYBE_ElideTextLongStrings ElideTextLongStrings |
| 353 | #endif |
| 354 | TEST(TextEliderTest, MAYBE_ElideTextLongStrings) { |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 355 | const base::string16 kEllipsisStr = UTF8ToUTF16(kEllipsis); |
| 356 | base::string16 data_scheme(UTF8ToUTF16("data:text/plain,")); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 357 | size_t data_scheme_length = data_scheme.length(); |
| 358 | |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 359 | base::string16 ten_a(10, 'a'); |
| 360 | base::string16 hundred_a(100, 'a'); |
| 361 | base::string16 thousand_a(1000, 'a'); |
| 362 | base::string16 ten_thousand_a(10000, 'a'); |
| 363 | base::string16 hundred_thousand_a(100000, 'a'); |
| 364 | base::string16 million_a(1000000, 'a'); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 365 | |
[email protected] | 9d3e32b3 | 2014-02-04 16:01:47 | [diff] [blame] | 366 | // TODO(gbillock): Improve these tests by adding more string diversity and |
| 367 | // doing string compares instead of length compares. See bug 338836. |
| 368 | |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 369 | size_t number_of_as = 156; |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 370 | base::string16 long_string_end( |
| 371 | data_scheme + base::string16(number_of_as, 'a') + kEllipsisStr); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 372 | UTF16Testcase testcases_end[] = { |
[email protected] | 9d3e32b3 | 2014-02-04 16:01:47 | [diff] [blame] | 373 | { data_scheme + ten_a, data_scheme + ten_a }, |
| 374 | { data_scheme + hundred_a, data_scheme + hundred_a }, |
| 375 | { data_scheme + thousand_a, long_string_end }, |
| 376 | { data_scheme + ten_thousand_a, long_string_end }, |
| 377 | { data_scheme + hundred_thousand_a, long_string_end }, |
| 378 | { data_scheme + million_a, long_string_end }, |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 379 | }; |
| 380 | |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 381 | const FontList font_list; |
| 382 | float ellipsis_width = GetStringWidthF(kEllipsisStr, font_list); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 383 | for (size_t i = 0; i < arraysize(testcases_end); ++i) { |
| 384 | // Compare sizes rather than actual contents because if the test fails, |
| 385 | // output is rather long. |
| 386 | EXPECT_EQ(testcases_end[i].output.size(), |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 387 | ElideText(testcases_end[i].input, font_list, |
| 388 | GetStringWidthF(testcases_end[i].output, font_list), |
| 389 | ELIDE_TAIL).size()); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 390 | EXPECT_EQ(kEllipsisStr, |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 391 | ElideText(testcases_end[i].input, font_list, ellipsis_width, |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 392 | ELIDE_TAIL)); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2; |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 396 | base::string16 long_string_middle(data_scheme + |
| 397 | base::string16(number_of_as - number_of_trailing_as, 'a') + kEllipsisStr + |
| 398 | base::string16(number_of_trailing_as, 'a')); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 399 | UTF16Testcase testcases_middle[] = { |
[email protected] | 9d3e32b3 | 2014-02-04 16:01:47 | [diff] [blame] | 400 | { data_scheme + ten_a, data_scheme + ten_a }, |
| 401 | { data_scheme + hundred_a, data_scheme + hundred_a }, |
| 402 | { data_scheme + thousand_a, long_string_middle }, |
| 403 | { data_scheme + ten_thousand_a, long_string_middle }, |
| 404 | { data_scheme + hundred_thousand_a, long_string_middle }, |
| 405 | { data_scheme + million_a, long_string_middle }, |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 406 | }; |
| 407 | |
| 408 | for (size_t i = 0; i < arraysize(testcases_middle); ++i) { |
| 409 | // Compare sizes rather than actual contents because if the test fails, |
| 410 | // output is rather long. |
| 411 | EXPECT_EQ(testcases_middle[i].output.size(), |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 412 | ElideText(testcases_middle[i].input, font_list, |
| 413 | GetStringWidthF(testcases_middle[i].output, font_list), |
| 414 | ELIDE_MIDDLE).size()); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 415 | EXPECT_EQ(kEllipsisStr, |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 416 | ElideText(testcases_middle[i].input, font_list, ellipsis_width, |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 417 | ELIDE_MIDDLE)); |
[email protected] | 9d3e32b3 | 2014-02-04 16:01:47 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | base::string16 long_string_beginning( |
| 421 | kEllipsisStr + base::string16(number_of_as, 'a')); |
| 422 | UTF16Testcase testcases_beginning[] = { |
| 423 | { data_scheme + ten_a, data_scheme + ten_a }, |
| 424 | { data_scheme + hundred_a, data_scheme + hundred_a }, |
| 425 | { data_scheme + thousand_a, long_string_beginning }, |
| 426 | { data_scheme + ten_thousand_a, long_string_beginning }, |
| 427 | { data_scheme + hundred_thousand_a, long_string_beginning }, |
| 428 | { data_scheme + million_a, long_string_beginning }, |
| 429 | }; |
| 430 | for (size_t i = 0; i < arraysize(testcases_beginning); ++i) { |
| 431 | EXPECT_EQ(testcases_beginning[i].output.size(), |
| 432 | ElideText( |
| 433 | testcases_beginning[i].input, font_list, |
| 434 | GetStringWidthF(testcases_beginning[i].output, font_list), |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 435 | ELIDE_HEAD).size()); |
[email protected] | 9d3e32b3 | 2014-02-04 16:01:47 | [diff] [blame] | 436 | EXPECT_EQ(kEllipsisStr, |
| 437 | ElideText(testcases_beginning[i].input, font_list, ellipsis_width, |
[email protected] | f3ce621 | 2014-06-05 22:42:08 | [diff] [blame] | 438 | ELIDE_HEAD)); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 442 | TEST(TextEliderTest, ElideString) { |
| 443 | struct TestData { |
| 444 | const char* input; |
| 445 | int max_len; |
| 446 | bool result; |
| 447 | const char* output; |
| 448 | } cases[] = { |
| 449 | { "Hello", 0, true, "" }, |
| 450 | { "", 0, false, "" }, |
| 451 | { "Hello, my name is Tom", 1, true, "H" }, |
| 452 | { "Hello, my name is Tom", 2, true, "He" }, |
| 453 | { "Hello, my name is Tom", 3, true, "H.m" }, |
| 454 | { "Hello, my name is Tom", 4, true, "H..m" }, |
| 455 | { "Hello, my name is Tom", 5, true, "H...m" }, |
| 456 | { "Hello, my name is Tom", 6, true, "He...m" }, |
| 457 | { "Hello, my name is Tom", 7, true, "He...om" }, |
| 458 | { "Hello, my name is Tom", 10, true, "Hell...Tom" }, |
| 459 | { "Hello, my name is Tom", 100, false, "Hello, my name is Tom" } |
| 460 | }; |
viettrungluu | a5ca99b | 2014-10-16 16:28:48 | [diff] [blame^] | 461 | for (size_t i = 0; i < arraysize(cases); ++i) { |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 462 | base::string16 output; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 463 | EXPECT_EQ(cases[i].result, |
| 464 | ElideString(UTF8ToUTF16(cases[i].input), |
| 465 | cases[i].max_len, &output)); |
| 466 | EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); |
| 467 | } |
| 468 | } |
| 469 | |
[email protected] | ebc9b66 | 2014-01-30 03:37:33 | [diff] [blame] | 470 | // TODO(338784): Enable this on android. |
| 471 | #if defined(OS_ANDROID) |
| 472 | #define MAYBE_ElideRectangleText DISABLED_ElideRectangleText |
| 473 | #else |
| 474 | #define MAYBE_ElideRectangleText ElideRectangleText |
| 475 | #endif |
| 476 | TEST(TextEliderTest, MAYBE_ElideRectangleText) { |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 477 | const FontList font_list; |
| 478 | const int line_height = font_list.GetHeight(); |
| 479 | const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 480 | |
| 481 | struct TestData { |
| 482 | const char* input; |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 483 | float available_pixel_width; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 484 | int available_pixel_height; |
| 485 | bool truncated_y; |
| 486 | const char* output; |
| 487 | } cases[] = { |
| 488 | { "", 0, 0, false, NULL }, |
| 489 | { "", 1, 1, false, NULL }, |
| 490 | { "Test", test_width, 0, true, NULL }, |
| 491 | { "Test", test_width, 1, false, "Test" }, |
| 492 | { "Test", test_width, line_height, false, "Test" }, |
| 493 | { "Test Test", test_width, line_height, true, "Test" }, |
| 494 | { "Test Test", test_width, line_height + 1, false, "Test|Test" }, |
| 495 | { "Test Test", test_width, line_height * 2, false, "Test|Test" }, |
| 496 | { "Test Test", test_width, line_height * 3, false, "Test|Test" }, |
| 497 | { "Test Test", test_width * 2, line_height * 2, false, "Test|Test" }, |
| 498 | { "Test Test", test_width * 3, line_height, false, "Test Test" }, |
| 499 | { "Test\nTest", test_width * 3, line_height * 2, false, "Test|Test" }, |
| 500 | { "Te\nst Te", test_width, line_height * 3, false, "Te|st|Te" }, |
| 501 | { "\nTest", test_width, line_height * 2, false, "|Test" }, |
| 502 | { "\nTest", test_width, line_height, true, "" }, |
| 503 | { "\n\nTest", test_width, line_height * 3, false, "||Test" }, |
| 504 | { "\n\nTest", test_width, line_height * 2, true, "|" }, |
| 505 | { "Test\n", 2 * test_width, line_height * 5, false, "Test|" }, |
| 506 | { "Test\n\n", 2 * test_width, line_height * 5, false, "Test||" }, |
| 507 | { "Test\n\n\n", 2 * test_width, line_height * 5, false, "Test|||" }, |
| 508 | { "Test\nTest\n\n", 2 * test_width, line_height * 5, false, "Test|Test||" }, |
| 509 | { "Test\n\nTest\n", 2 * test_width, line_height * 5, false, "Test||Test|" }, |
| 510 | { "Test\n\n\nTest", 2 * test_width, line_height * 5, false, "Test|||Test" }, |
| 511 | { "Te ", test_width, line_height, false, "Te" }, |
| 512 | { "Te Te Test", test_width, 3 * line_height, false, "Te|Te|Test" }, |
| 513 | }; |
| 514 | |
viettrungluu | a5ca99b | 2014-10-16 16:28:48 | [diff] [blame^] | 515 | for (size_t i = 0; i < arraysize(cases); ++i) { |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 516 | std::vector<base::string16> lines; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 517 | EXPECT_EQ(cases[i].truncated_y ? INSUFFICIENT_SPACE_VERTICAL : 0, |
| 518 | ElideRectangleText(UTF8ToUTF16(cases[i].input), |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 519 | font_list, |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 520 | cases[i].available_pixel_width, |
| 521 | cases[i].available_pixel_height, |
| 522 | TRUNCATE_LONG_WORDS, |
| 523 | &lines)); |
| 524 | if (cases[i].output) { |
| 525 | const std::string result = UTF16ToUTF8(JoinString(lines, '|')); |
| 526 | EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; |
| 527 | } else { |
| 528 | EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
[email protected] | ebc9b66 | 2014-01-30 03:37:33 | [diff] [blame] | 533 | // TODO(338784): Enable this on android. |
| 534 | #if defined(OS_ANDROID) |
| 535 | #define MAYBE_ElideRectangleTextPunctuation \ |
| 536 | DISABLED_ElideRectangleTextPunctuation |
| 537 | #else |
| 538 | #define MAYBE_ElideRectangleTextPunctuation ElideRectangleTextPunctuation |
| 539 | #endif |
| 540 | TEST(TextEliderTest, MAYBE_ElideRectangleTextPunctuation) { |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 541 | const FontList font_list; |
| 542 | const int line_height = font_list.GetHeight(); |
| 543 | const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
| 544 | const float test_t_width = GetStringWidthF(ASCIIToUTF16("Test T"), font_list); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 545 | |
| 546 | struct TestData { |
| 547 | const char* input; |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 548 | float available_pixel_width; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 549 | int available_pixel_height; |
| 550 | bool wrap_words; |
| 551 | bool truncated_x; |
| 552 | const char* output; |
| 553 | } cases[] = { |
| 554 | { "Test T.", test_t_width, line_height * 2, false, false, "Test|T." }, |
| 555 | { "Test T ?", test_t_width, line_height * 2, false, false, "Test|T ?" }, |
| 556 | { "Test. Test", test_width, line_height * 3, false, true, "Test|Test" }, |
| 557 | { "Test. Test", test_width, line_height * 3, true, false, "Test|.|Test" }, |
| 558 | }; |
| 559 | |
viettrungluu | a5ca99b | 2014-10-16 16:28:48 | [diff] [blame^] | 560 | for (size_t i = 0; i < arraysize(cases); ++i) { |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 561 | std::vector<base::string16> lines; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 562 | const WordWrapBehavior wrap_behavior = |
| 563 | (cases[i].wrap_words ? WRAP_LONG_WORDS : TRUNCATE_LONG_WORDS); |
| 564 | EXPECT_EQ(cases[i].truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0, |
| 565 | ElideRectangleText(UTF8ToUTF16(cases[i].input), |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 566 | font_list, |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 567 | cases[i].available_pixel_width, |
| 568 | cases[i].available_pixel_height, |
| 569 | wrap_behavior, |
| 570 | &lines)); |
| 571 | if (cases[i].output) { |
| 572 | const std::string result = UTF16ToUTF8(JoinString(lines, '|')); |
| 573 | EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; |
| 574 | } else { |
| 575 | EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
[email protected] | ebc9b66 | 2014-01-30 03:37:33 | [diff] [blame] | 580 | // TODO(338784): Enable this on android. |
| 581 | #if defined(OS_ANDROID) |
| 582 | #define MAYBE_ElideRectangleTextLongWords DISABLED_ElideRectangleTextLongWords |
| 583 | #else |
| 584 | #define MAYBE_ElideRectangleTextLongWords ElideRectangleTextLongWords |
| 585 | #endif |
| 586 | TEST(TextEliderTest, MAYBE_ElideRectangleTextLongWords) { |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 587 | const FontList font_list; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 588 | const int kAvailableHeight = 1000; |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 589 | const base::string16 kElidedTesting = |
| 590 | UTF8ToUTF16(std::string("Tes") + kEllipsis); |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 591 | const float elided_width = GetStringWidthF(kElidedTesting, font_list); |
| 592 | const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 593 | |
| 594 | struct TestData { |
| 595 | const char* input; |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 596 | float available_pixel_width; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 597 | WordWrapBehavior wrap_behavior; |
| 598 | bool truncated_x; |
| 599 | const char* output; |
| 600 | } cases[] = { |
| 601 | { "Testing", test_width, IGNORE_LONG_WORDS, false, "Testing" }, |
| 602 | { "X Testing", test_width, IGNORE_LONG_WORDS, false, "X|Testing" }, |
| 603 | { "Test Testing", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" }, |
| 604 | { "Test\nTesting", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" }, |
| 605 | { "Test Tests ", test_width, IGNORE_LONG_WORDS, false, "Test|Tests" }, |
| 606 | { "Test Tests T", test_width, IGNORE_LONG_WORDS, false, "Test|Tests|T" }, |
| 607 | |
| 608 | { "Testing", elided_width, ELIDE_LONG_WORDS, true, "Tes..." }, |
| 609 | { "X Testing", elided_width, ELIDE_LONG_WORDS, true, "X|Tes..." }, |
| 610 | { "Test Testing", elided_width, ELIDE_LONG_WORDS, true, "Test|Tes..." }, |
| 611 | { "Test\nTesting", elided_width, ELIDE_LONG_WORDS, true, "Test|Tes..." }, |
| 612 | |
| 613 | { "Testing", test_width, TRUNCATE_LONG_WORDS, true, "Test" }, |
| 614 | { "X Testing", test_width, TRUNCATE_LONG_WORDS, true, "X|Test" }, |
| 615 | { "Test Testing", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test" }, |
| 616 | { "Test\nTesting", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test" }, |
| 617 | { "Test Tests ", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test" }, |
| 618 | { "Test Tests T", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test|T" }, |
| 619 | |
| 620 | { "Testing", test_width, WRAP_LONG_WORDS, false, "Test|ing" }, |
| 621 | { "X Testing", test_width, WRAP_LONG_WORDS, false, "X|Test|ing" }, |
| 622 | { "Test Testing", test_width, WRAP_LONG_WORDS, false, "Test|Test|ing" }, |
| 623 | { "Test\nTesting", test_width, WRAP_LONG_WORDS, false, "Test|Test|ing" }, |
| 624 | { "Test Tests ", test_width, WRAP_LONG_WORDS, false, "Test|Test|s" }, |
| 625 | { "Test Tests T", test_width, WRAP_LONG_WORDS, false, "Test|Test|s T" }, |
| 626 | { "TestTestTest", test_width, WRAP_LONG_WORDS, false, "Test|Test|Test" }, |
| 627 | { "TestTestTestT", test_width, WRAP_LONG_WORDS, false, "Test|Test|Test|T" }, |
| 628 | }; |
| 629 | |
viettrungluu | a5ca99b | 2014-10-16 16:28:48 | [diff] [blame^] | 630 | for (size_t i = 0; i < arraysize(cases); ++i) { |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 631 | std::vector<base::string16> lines; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 632 | EXPECT_EQ(cases[i].truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0, |
| 633 | ElideRectangleText(UTF8ToUTF16(cases[i].input), |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 634 | font_list, |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 635 | cases[i].available_pixel_width, |
| 636 | kAvailableHeight, |
| 637 | cases[i].wrap_behavior, |
| 638 | &lines)); |
| 639 | std::string expected_output(cases[i].output); |
| 640 | ReplaceSubstringsAfterOffset(&expected_output, 0, "...", kEllipsis); |
| 641 | const std::string result = UTF16ToUTF8(JoinString(lines, '|')); |
| 642 | EXPECT_EQ(expected_output, result) << "Case " << i << " failed!"; |
| 643 | } |
| 644 | } |
| 645 | |
[email protected] | c308798 | 2013-09-20 17:46:53 | [diff] [blame] | 646 | // This test is to make sure that the width of each wrapped line does not |
| 647 | // exceed the available width. On some platform like Mac, this test used to |
| 648 | // fail because the truncated integer width is returned for the string |
| 649 | // and the accumulation of the truncated values causes the elide function |
| 650 | // to wrap incorrectly. |
[email protected] | ebc9b66 | 2014-01-30 03:37:33 | [diff] [blame] | 651 | // TODO(338784): Enable this on android. |
| 652 | #if defined(OS_ANDROID) |
| 653 | #define MAYBE_ElideRectangleTextCheckLineWidth \ |
| 654 | DISABLED_ElideRectangleTextCheckLineWidth |
| 655 | #else |
| 656 | #define MAYBE_ElideRectangleTextCheckLineWidth ElideRectangleTextCheckLineWidth |
| 657 | #endif |
| 658 | TEST(TextEliderTest, MAYBE_ElideRectangleTextCheckLineWidth) { |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 659 | FontList font_list; |
| 660 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 661 | // Use a specific font to expose the line width exceeding problem. |
| 662 | font_list = FontList(Font("LucidaGrande", 12)); |
| 663 | #endif |
| 664 | const float kAvailableWidth = 235; |
[email protected] | c308798 | 2013-09-20 17:46:53 | [diff] [blame] | 665 | const int kAvailableHeight = 1000; |
| 666 | const char text[] = "that Russian place we used to go to after fencing"; |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 667 | std::vector<base::string16> lines; |
[email protected] | c308798 | 2013-09-20 17:46:53 | [diff] [blame] | 668 | EXPECT_EQ(0, ElideRectangleText(UTF8ToUTF16(text), |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 669 | font_list, |
[email protected] | c308798 | 2013-09-20 17:46:53 | [diff] [blame] | 670 | kAvailableWidth, |
| 671 | kAvailableHeight, |
| 672 | WRAP_LONG_WORDS, |
| 673 | &lines)); |
| 674 | ASSERT_EQ(2u, lines.size()); |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 675 | EXPECT_LE(GetStringWidthF(lines[0], font_list), kAvailableWidth); |
| 676 | EXPECT_LE(GetStringWidthF(lines[1], font_list), kAvailableWidth); |
[email protected] | c308798 | 2013-09-20 17:46:53 | [diff] [blame] | 677 | } |
| 678 | |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 679 | TEST(TextEliderTest, ElideRectangleString) { |
| 680 | struct TestData { |
| 681 | const char* input; |
| 682 | int max_rows; |
| 683 | int max_cols; |
| 684 | bool result; |
| 685 | const char* output; |
| 686 | } cases[] = { |
| 687 | { "", 0, 0, false, "" }, |
| 688 | { "", 1, 1, false, "" }, |
| 689 | { "Hi, my name is\nTom", 0, 0, true, "..." }, |
| 690 | { "Hi, my name is\nTom", 1, 0, true, "\n..." }, |
| 691 | { "Hi, my name is\nTom", 0, 1, true, "..." }, |
| 692 | { "Hi, my name is\nTom", 1, 1, true, "H\n..." }, |
| 693 | { "Hi, my name is\nTom", 2, 1, true, "H\ni\n..." }, |
| 694 | { "Hi, my name is\nTom", 3, 1, true, "H\ni\n,\n..." }, |
| 695 | { "Hi, my name is\nTom", 4, 1, true, "H\ni\n,\n \n..." }, |
| 696 | { "Hi, my name is\nTom", 5, 1, true, "H\ni\n,\n \nm\n..." }, |
| 697 | { "Hi, my name is\nTom", 0, 2, true, "..." }, |
| 698 | { "Hi, my name is\nTom", 1, 2, true, "Hi\n..." }, |
| 699 | { "Hi, my name is\nTom", 2, 2, true, "Hi\n, \n..." }, |
| 700 | { "Hi, my name is\nTom", 3, 2, true, "Hi\n, \nmy\n..." }, |
| 701 | { "Hi, my name is\nTom", 4, 2, true, "Hi\n, \nmy\n n\n..." }, |
| 702 | { "Hi, my name is\nTom", 5, 2, true, "Hi\n, \nmy\n n\nam\n..." }, |
| 703 | { "Hi, my name is\nTom", 0, 3, true, "..." }, |
| 704 | { "Hi, my name is\nTom", 1, 3, true, "Hi,\n..." }, |
| 705 | { "Hi, my name is\nTom", 2, 3, true, "Hi,\n my\n..." }, |
| 706 | { "Hi, my name is\nTom", 3, 3, true, "Hi,\n my\n na\n..." }, |
| 707 | { "Hi, my name is\nTom", 4, 3, true, "Hi,\n my\n na\nme \n..." }, |
| 708 | { "Hi, my name is\nTom", 5, 3, true, "Hi,\n my\n na\nme \nis\n..." }, |
| 709 | { "Hi, my name is\nTom", 1, 4, true, "Hi, \n..." }, |
| 710 | { "Hi, my name is\nTom", 2, 4, true, "Hi, \nmy n\n..." }, |
| 711 | { "Hi, my name is\nTom", 3, 4, true, "Hi, \nmy n\name \n..." }, |
| 712 | { "Hi, my name is\nTom", 4, 4, true, "Hi, \nmy n\name \nis\n..." }, |
| 713 | { "Hi, my name is\nTom", 5, 4, false, "Hi, \nmy n\name \nis\nTom" }, |
| 714 | { "Hi, my name is\nTom", 1, 5, true, "Hi, \n..." }, |
| 715 | { "Hi, my name is\nTom", 2, 5, true, "Hi, \nmy na\n..." }, |
| 716 | { "Hi, my name is\nTom", 3, 5, true, "Hi, \nmy na\nme \n..." }, |
| 717 | { "Hi, my name is\nTom", 4, 5, true, "Hi, \nmy na\nme \nis\n..." }, |
| 718 | { "Hi, my name is\nTom", 5, 5, false, "Hi, \nmy na\nme \nis\nTom" }, |
| 719 | { "Hi, my name is\nTom", 1, 6, true, "Hi, \n..." }, |
| 720 | { "Hi, my name is\nTom", 2, 6, true, "Hi, \nmy \n..." }, |
| 721 | { "Hi, my name is\nTom", 3, 6, true, "Hi, \nmy \nname \n..." }, |
| 722 | { "Hi, my name is\nTom", 4, 6, true, "Hi, \nmy \nname \nis\n..." }, |
| 723 | { "Hi, my name is\nTom", 5, 6, false, "Hi, \nmy \nname \nis\nTom" }, |
| 724 | { "Hi, my name is\nTom", 1, 7, true, "Hi, \n..." }, |
| 725 | { "Hi, my name is\nTom", 2, 7, true, "Hi, \nmy \n..." }, |
| 726 | { "Hi, my name is\nTom", 3, 7, true, "Hi, \nmy \nname \n..." }, |
| 727 | { "Hi, my name is\nTom", 4, 7, true, "Hi, \nmy \nname \nis\n..." }, |
| 728 | { "Hi, my name is\nTom", 5, 7, false, "Hi, \nmy \nname \nis\nTom" }, |
| 729 | { "Hi, my name is\nTom", 1, 8, true, "Hi, my \n..." }, |
| 730 | { "Hi, my name is\nTom", 2, 8, true, "Hi, my \nname \n..." }, |
| 731 | { "Hi, my name is\nTom", 3, 8, true, "Hi, my \nname \nis\n..." }, |
| 732 | { "Hi, my name is\nTom", 4, 8, false, "Hi, my \nname \nis\nTom" }, |
| 733 | { "Hi, my name is\nTom", 1, 9, true, "Hi, my \n..." }, |
| 734 | { "Hi, my name is\nTom", 2, 9, true, "Hi, my \nname is\n..." }, |
| 735 | { "Hi, my name is\nTom", 3, 9, false, "Hi, my \nname is\nTom" }, |
| 736 | { "Hi, my name is\nTom", 1, 10, true, "Hi, my \n..." }, |
| 737 | { "Hi, my name is\nTom", 2, 10, true, "Hi, my \nname is\n..." }, |
| 738 | { "Hi, my name is\nTom", 3, 10, false, "Hi, my \nname is\nTom" }, |
| 739 | { "Hi, my name is\nTom", 1, 11, true, "Hi, my \n..." }, |
| 740 | { "Hi, my name is\nTom", 2, 11, true, "Hi, my \nname is\n..." }, |
| 741 | { "Hi, my name is\nTom", 3, 11, false, "Hi, my \nname is\nTom" }, |
| 742 | { "Hi, my name is\nTom", 1, 12, true, "Hi, my \n..." }, |
| 743 | { "Hi, my name is\nTom", 2, 12, true, "Hi, my \nname is\n..." }, |
| 744 | { "Hi, my name is\nTom", 3, 12, false, "Hi, my \nname is\nTom" }, |
| 745 | { "Hi, my name is\nTom", 1, 13, true, "Hi, my name \n..." }, |
| 746 | { "Hi, my name is\nTom", 2, 13, true, "Hi, my name \nis\n..." }, |
| 747 | { "Hi, my name is\nTom", 3, 13, false, "Hi, my name \nis\nTom" }, |
| 748 | { "Hi, my name is\nTom", 1, 20, true, "Hi, my name is\n..." }, |
| 749 | { "Hi, my name is\nTom", 2, 20, false, "Hi, my name is\nTom" }, |
| 750 | { "Hi, my name is Tom", 1, 40, false, "Hi, my name is Tom" }, |
| 751 | }; |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 752 | base::string16 output; |
viettrungluu | a5ca99b | 2014-10-16 16:28:48 | [diff] [blame^] | 753 | for (size_t i = 0; i < arraysize(cases); ++i) { |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 754 | EXPECT_EQ(cases[i].result, |
| 755 | ElideRectangleString(UTF8ToUTF16(cases[i].input), |
| 756 | cases[i].max_rows, cases[i].max_cols, |
| 757 | true, &output)); |
| 758 | EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | TEST(TextEliderTest, ElideRectangleStringNotStrict) { |
| 763 | struct TestData { |
| 764 | const char* input; |
| 765 | int max_rows; |
| 766 | int max_cols; |
| 767 | bool result; |
| 768 | const char* output; |
| 769 | } cases[] = { |
| 770 | { "", 0, 0, false, "" }, |
| 771 | { "", 1, 1, false, "" }, |
| 772 | { "Hi, my name_is\nDick", 0, 0, true, "..." }, |
| 773 | { "Hi, my name_is\nDick", 1, 0, true, "\n..." }, |
| 774 | { "Hi, my name_is\nDick", 0, 1, true, "..." }, |
| 775 | { "Hi, my name_is\nDick", 1, 1, true, "H\n..." }, |
| 776 | { "Hi, my name_is\nDick", 2, 1, true, "H\ni\n..." }, |
| 777 | { "Hi, my name_is\nDick", 3, 1, true, "H\ni\n,\n..." }, |
| 778 | { "Hi, my name_is\nDick", 4, 1, true, "H\ni\n,\n \n..." }, |
| 779 | { "Hi, my name_is\nDick", 5, 1, true, "H\ni\n,\n \nm\n..." }, |
| 780 | { "Hi, my name_is\nDick", 0, 2, true, "..." }, |
| 781 | { "Hi, my name_is\nDick", 1, 2, true, "Hi\n..." }, |
| 782 | { "Hi, my name_is\nDick", 2, 2, true, "Hi\n, \n..." }, |
| 783 | { "Hi, my name_is\nDick", 3, 2, true, "Hi\n, \nmy\n..." }, |
| 784 | { "Hi, my name_is\nDick", 4, 2, true, "Hi\n, \nmy\n n\n..." }, |
| 785 | { "Hi, my name_is\nDick", 5, 2, true, "Hi\n, \nmy\n n\nam\n..." }, |
| 786 | { "Hi, my name_is\nDick", 0, 3, true, "..." }, |
| 787 | { "Hi, my name_is\nDick", 1, 3, true, "Hi,\n..." }, |
| 788 | { "Hi, my name_is\nDick", 2, 3, true, "Hi,\n my\n..." }, |
| 789 | { "Hi, my name_is\nDick", 3, 3, true, "Hi,\n my\n na\n..." }, |
| 790 | { "Hi, my name_is\nDick", 4, 3, true, "Hi,\n my\n na\nme_\n..." }, |
| 791 | { "Hi, my name_is\nDick", 5, 3, true, "Hi,\n my\n na\nme_\nis\n..." }, |
| 792 | { "Hi, my name_is\nDick", 1, 4, true, "Hi, ..." }, |
| 793 | { "Hi, my name_is\nDick", 2, 4, true, "Hi, my n\n..." }, |
| 794 | { "Hi, my name_is\nDick", 3, 4, true, "Hi, my n\name_\n..." }, |
| 795 | { "Hi, my name_is\nDick", 4, 4, true, "Hi, my n\name_\nis\n..." }, |
| 796 | { "Hi, my name_is\nDick", 5, 4, false, "Hi, my n\name_\nis\nDick" }, |
| 797 | { "Hi, my name_is\nDick", 1, 5, true, "Hi, ..." }, |
| 798 | { "Hi, my name_is\nDick", 2, 5, true, "Hi, my na\n..." }, |
| 799 | { "Hi, my name_is\nDick", 3, 5, true, "Hi, my na\nme_is\n..." }, |
| 800 | { "Hi, my name_is\nDick", 4, 5, true, "Hi, my na\nme_is\n\n..." }, |
| 801 | { "Hi, my name_is\nDick", 5, 5, false, "Hi, my na\nme_is\n\nDick" }, |
| 802 | { "Hi, my name_is\nDick", 1, 6, true, "Hi, ..." }, |
| 803 | { "Hi, my name_is\nDick", 2, 6, true, "Hi, my nam\n..." }, |
| 804 | { "Hi, my name_is\nDick", 3, 6, true, "Hi, my nam\ne_is\n..." }, |
| 805 | { "Hi, my name_is\nDick", 4, 6, false, "Hi, my nam\ne_is\nDick" }, |
| 806 | { "Hi, my name_is\nDick", 5, 6, false, "Hi, my nam\ne_is\nDick" }, |
| 807 | { "Hi, my name_is\nDick", 1, 7, true, "Hi, ..." }, |
| 808 | { "Hi, my name_is\nDick", 2, 7, true, "Hi, my name\n..." }, |
| 809 | { "Hi, my name_is\nDick", 3, 7, true, "Hi, my name\n_is\n..." }, |
| 810 | { "Hi, my name_is\nDick", 4, 7, false, "Hi, my name\n_is\nDick" }, |
| 811 | { "Hi, my name_is\nDick", 5, 7, false, "Hi, my name\n_is\nDick" }, |
| 812 | { "Hi, my name_is\nDick", 1, 8, true, "Hi, my n\n..." }, |
| 813 | { "Hi, my name_is\nDick", 2, 8, true, "Hi, my n\name_is\n..." }, |
| 814 | { "Hi, my name_is\nDick", 3, 8, false, "Hi, my n\name_is\nDick" }, |
| 815 | { "Hi, my name_is\nDick", 1, 9, true, "Hi, my ..." }, |
| 816 | { "Hi, my name_is\nDick", 2, 9, true, "Hi, my name_is\n..." }, |
| 817 | { "Hi, my name_is\nDick", 3, 9, false, "Hi, my name_is\nDick" }, |
| 818 | { "Hi, my name_is\nDick", 1, 10, true, "Hi, my ..." }, |
| 819 | { "Hi, my name_is\nDick", 2, 10, true, "Hi, my name_is\n..." }, |
| 820 | { "Hi, my name_is\nDick", 3, 10, false, "Hi, my name_is\nDick" }, |
| 821 | { "Hi, my name_is\nDick", 1, 11, true, "Hi, my ..." }, |
| 822 | { "Hi, my name_is\nDick", 2, 11, true, "Hi, my name_is\n..." }, |
| 823 | { "Hi, my name_is\nDick", 3, 11, false, "Hi, my name_is\nDick" }, |
| 824 | { "Hi, my name_is\nDick", 1, 12, true, "Hi, my ..." }, |
| 825 | { "Hi, my name_is\nDick", 2, 12, true, "Hi, my name_is\n..." }, |
| 826 | { "Hi, my name_is\nDick", 3, 12, false, "Hi, my name_is\nDick" }, |
| 827 | { "Hi, my name_is\nDick", 1, 13, true, "Hi, my ..." }, |
| 828 | { "Hi, my name_is\nDick", 2, 13, true, "Hi, my name_is\n..." }, |
| 829 | { "Hi, my name_is\nDick", 3, 13, false, "Hi, my name_is\nDick" }, |
| 830 | { "Hi, my name_is\nDick", 1, 20, true, "Hi, my name_is\n..." }, |
| 831 | { "Hi, my name_is\nDick", 2, 20, false, "Hi, my name_is\nDick" }, |
| 832 | { "Hi, my name_is Dick", 1, 40, false, "Hi, my name_is Dick" }, |
| 833 | }; |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 834 | base::string16 output; |
viettrungluu | a5ca99b | 2014-10-16 16:28:48 | [diff] [blame^] | 835 | for (size_t i = 0; i < arraysize(cases); ++i) { |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 836 | EXPECT_EQ(cases[i].result, |
| 837 | ElideRectangleString(UTF8ToUTF16(cases[i].input), |
| 838 | cases[i].max_rows, cases[i].max_cols, |
| 839 | false, &output)); |
| 840 | EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | TEST(TextEliderTest, ElideRectangleWide16) { |
| 845 | // Two greek words separated by space. |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 846 | const base::string16 str(WideToUTF16( |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 847 | L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9" |
| 848 | L"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2")); |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 849 | const base::string16 out1(WideToUTF16( |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 850 | L"\x03a0\x03b1\x03b3\x03ba\n" |
| 851 | L"\x03cc\x03c3\x03bc\x03b9\n" |
| 852 | L"...")); |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 853 | const base::string16 out2(WideToUTF16( |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 854 | L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9\x03bf\x03c2\x0020\n" |
| 855 | L"\x0399\x03c3\x03c4\x03cc\x03c2")); |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 856 | base::string16 output; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 857 | EXPECT_TRUE(ElideRectangleString(str, 2, 4, true, &output)); |
| 858 | EXPECT_EQ(out1, output); |
| 859 | EXPECT_FALSE(ElideRectangleString(str, 2, 12, true, &output)); |
| 860 | EXPECT_EQ(out2, output); |
| 861 | } |
| 862 | |
| 863 | TEST(TextEliderTest, ElideRectangleWide32) { |
| 864 | // Four U+1D49C MATHEMATICAL SCRIPT CAPITAL A followed by space "aaaaa". |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 865 | const base::string16 str(UTF8ToUTF16( |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 866 | "\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C" |
| 867 | " aaaaa")); |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 868 | const base::string16 out(UTF8ToUTF16( |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 869 | "\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\n" |
| 870 | "\xF0\x9D\x92\x9C \naaa\n...")); |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 871 | base::string16 output; |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 872 | EXPECT_TRUE(ElideRectangleString(str, 3, 3, true, &output)); |
| 873 | EXPECT_EQ(out, output); |
| 874 | } |
| 875 | |
| 876 | TEST(TextEliderTest, TruncateString) { |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 877 | base::string16 string = ASCIIToUTF16("foooooey bxxxar baz"); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 878 | |
[email protected] | 41c0d7c1 | 2014-07-23 15:52:58 | [diff] [blame] | 879 | // Tests that apply to both break behaviors: |
| 880 | |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 881 | // Make sure it doesn't modify the string if length > string length. |
[email protected] | 41c0d7c1 | 2014-07-23 15:52:58 | [diff] [blame] | 882 | EXPECT_EQ(string, TruncateString(string, 100, WORD_BREAK)); |
| 883 | EXPECT_EQ(string, TruncateString(string, 100, CHARACTER_BREAK)); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 884 | |
| 885 | // Test no characters. |
[email protected] | 41c0d7c1 | 2014-07-23 15:52:58 | [diff] [blame] | 886 | EXPECT_EQ(L"", UTF16ToWide(TruncateString(string, 0, WORD_BREAK))); |
| 887 | EXPECT_EQ(L"", UTF16ToWide(TruncateString(string, 0, CHARACTER_BREAK))); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 888 | |
| 889 | // Test 1 character. |
[email protected] | 41c0d7c1 | 2014-07-23 15:52:58 | [diff] [blame] | 890 | EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(string, 1, WORD_BREAK))); |
| 891 | EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(string, 1, CHARACTER_BREAK))); |
| 892 | |
| 893 | // Test completely truncates string if break is on initial whitespace. |
| 894 | EXPECT_EQ(L"\x2026", |
| 895 | UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2, WORD_BREAK))); |
| 896 | EXPECT_EQ(L"\x2026", |
| 897 | UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2, |
| 898 | CHARACTER_BREAK))); |
| 899 | |
| 900 | // Break-only-at-word-boundaries tests: |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 901 | |
| 902 | // Test adds ... at right spot when there is enough room to break at a |
| 903 | // word boundary. |
[email protected] | 41c0d7c1 | 2014-07-23 15:52:58 | [diff] [blame] | 904 | EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 14, |
| 905 | WORD_BREAK))); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 906 | |
| 907 | // Test adds ... at right spot when there is not enough space in first word. |
[email protected] | 41c0d7c1 | 2014-07-23 15:52:58 | [diff] [blame] | 908 | EXPECT_EQ(L"f\x2026", UTF16ToWide(TruncateString(string, 2, WORD_BREAK))); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 909 | |
| 910 | // Test adds ... at right spot when there is not enough room to break at a |
| 911 | // word boundary. |
[email protected] | 41c0d7c1 | 2014-07-23 15:52:58 | [diff] [blame] | 912 | EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11, |
| 913 | WORD_BREAK))); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 914 | |
[email protected] | 41c0d7c1 | 2014-07-23 15:52:58 | [diff] [blame] | 915 | // Break-anywhere tests: |
| 916 | |
| 917 | // Test adds ... at right spot within a word. |
| 918 | EXPECT_EQ(L"f\x2026", UTF16ToWide(TruncateString(string, 2, |
| 919 | CHARACTER_BREAK))); |
| 920 | |
| 921 | // Test removes trailing whitespace if break falls between words. |
| 922 | EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 12, |
| 923 | CHARACTER_BREAK))); |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | } // namespace gfx |