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/gfx/text_elider.h" | 7 #include "ui/gfx/text_elider.h" |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 // the domain must elide to "l..." and not "l...l" as it must allow enough | 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 | 100 // space for the minimal username elision although its half of the |
101 // available width would normally allow it to elide to "l...l". | 101 // available width would normally allow it to elide to "l...l". |
102 {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr}, | 102 {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr}, |
103 }; | 103 }; |
104 | 104 |
105 const FontList font_list; | 105 const FontList font_list; |
106 for (size_t i = 0; i < arraysize(testcases); ++i) { | 106 for (size_t i = 0; i < arraysize(testcases); ++i) { |
107 const base::string16 expected_output = UTF8ToUTF16(testcases[i].output); | 107 const base::string16 expected_output = UTF8ToUTF16(testcases[i].output); |
108 EXPECT_EQ(expected_output, | 108 EXPECT_EQ(expected_output, |
109 ElideEmail( | 109 ElideText(UTF8ToUTF16(testcases[i].input), font_list, |
110 UTF8ToUTF16(testcases[i].input), | 110 GetStringWidthF(expected_output, font_list), |
111 font_list, | 111 ELIDE_EMAIL)); |
112 GetStringWidthF(expected_output, font_list))); | |
113 } | 112 } |
114 } | 113 } |
115 | 114 |
116 // TODO(338784): Enable this on android. | 115 // TODO(338784): Enable this on android. |
117 #if defined(OS_ANDROID) | 116 #if defined(OS_ANDROID) |
118 #define MAYBE_ElideEmailMoreSpace DISABLED_ElideEmailMoreSpace | 117 #define MAYBE_ElideEmailMoreSpace DISABLED_ElideEmailMoreSpace |
119 #else | 118 #else |
120 #define MAYBE_ElideEmailMoreSpace ElideEmailMoreSpace | 119 #define MAYBE_ElideEmailMoreSpace ElideEmailMoreSpace |
121 #endif | 120 #endif |
122 TEST(TextEliderTest, MAYBE_ElideEmailMoreSpace) { | 121 TEST(TextEliderTest, MAYBE_ElideEmailMoreSpace) { |
123 const int test_width_factors[] = { | 122 const int test_width_factors[] = { |
124 100, | 123 100, |
125 10000, | 124 10000, |
126 1000000, | 125 1000000, |
127 }; | 126 }; |
128 const std::string test_emails[] = { | 127 const std::string test_emails[] = { |
129 "a@c", | 128 "a@c", |
130 "test@email.com", | 129 "test@email.com", |
131 "short@verysuperdupperlongdomain.com", | 130 "short@verysuperdupperlongdomain.com", |
132 "supermegalongusername@withasuperlonnnggggdomain.gouv.qc.ca", | 131 "supermegalongusername@withasuperlonnnggggdomain.gouv.qc.ca", |
133 }; | 132 }; |
134 | 133 |
135 const FontList font_list; | 134 const FontList font_list; |
136 for (size_t i = 0; i < arraysize(test_width_factors); ++i) { | 135 for (size_t i = 0; i < arraysize(test_width_factors); ++i) { |
137 const int test_width = | 136 const int test_width = |
138 font_list.GetExpectedTextWidth(test_width_factors[i]); | 137 font_list.GetExpectedTextWidth(test_width_factors[i]); |
139 for (size_t j = 0; j < arraysize(test_emails); ++j) { | 138 for (size_t j = 0; j < arraysize(test_emails); ++j) { |
140 // Extra space is available: the email should not be elided. | 139 // Extra space is available: the email should not be elided. |
141 const base::string16 test_email = UTF8ToUTF16(test_emails[j]); | 140 const base::string16 test_email = UTF8ToUTF16(test_emails[j]); |
142 EXPECT_EQ(test_email, ElideEmail(test_email, font_list, test_width)); | 141 EXPECT_EQ(test_email, |
| 142 ElideText(test_email, font_list, test_width, ELIDE_EMAIL)); |
143 } | 143 } |
144 } | 144 } |
145 } | 145 } |
146 | 146 |
147 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF | 147 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF |
148 // that calls [NSString sizeWithFont] returns the rounded string width. | 148 // that calls [NSString sizeWithFont] returns the rounded string width. |
149 // TODO(338784): Enable this on android. | 149 // TODO(338784): Enable this on android. |
150 #if defined(OS_IOS) || defined(OS_ANDROID) | 150 #if defined(OS_IOS) || defined(OS_ANDROID) |
151 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding | 151 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding |
152 #else | 152 #else |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 { "", 0, "" }, | 218 { "", 0, "" }, |
219 { "Test", 0, "" }, | 219 { "Test", 0, "" }, |
220 { "", kTestWidth, "" }, | 220 { "", kTestWidth, "" }, |
221 { "Tes", kTestWidth, "Tes" }, | 221 { "Tes", kTestWidth, "Tes" }, |
222 { "Test", kTestWidth, "Test" }, | 222 { "Test", kTestWidth, "Test" }, |
223 { "Tests", kTestWidth, "Test" }, | 223 { "Tests", kTestWidth, "Test" }, |
224 }; | 224 }; |
225 | 225 |
226 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 226 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
227 base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, | 227 base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, |
228 cases[i].width, TRUNCATE_AT_END); | 228 cases[i].width, TRUNCATE); |
229 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); | 229 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); |
230 } | 230 } |
231 } | 231 } |
232 | 232 |
233 // TODO(338784): Enable this on android. | 233 // TODO(338784): Enable this on android. |
234 #if defined(OS_ANDROID) | 234 #if defined(OS_ANDROID) |
235 #define MAYBE_ElideTextEllipsis DISABLED_ElideTextEllipsis | 235 #define MAYBE_ElideTextEllipsis DISABLED_ElideTextEllipsis |
236 #else | 236 #else |
237 #define MAYBE_ElideTextEllipsis ElideTextEllipsis | 237 #define MAYBE_ElideTextEllipsis ElideTextEllipsis |
238 #endif | 238 #endif |
(...skipping 11 matching lines...) Expand all Loading... |
250 { "", 0, "" }, | 250 { "", 0, "" }, |
251 { "Test", 0, "" }, | 251 { "Test", 0, "" }, |
252 { "Test", kEllipsisWidth, kEllipsis }, | 252 { "Test", kEllipsisWidth, kEllipsis }, |
253 { "", kTestWidth, "" }, | 253 { "", kTestWidth, "" }, |
254 { "Tes", kTestWidth, "Tes" }, | 254 { "Tes", kTestWidth, "Tes" }, |
255 { "Test", kTestWidth, "Test" }, | 255 { "Test", kTestWidth, "Test" }, |
256 }; | 256 }; |
257 | 257 |
258 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 258 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
259 base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, | 259 base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, |
260 cases[i].width, ELIDE_AT_END); | 260 cases[i].width, ELIDE_TAIL); |
261 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); | 261 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); |
262 } | 262 } |
263 } | 263 } |
264 | 264 |
265 // TODO(338784): Enable this on android. | 265 // TODO(338784): Enable this on android. |
266 #if defined(OS_ANDROID) | 266 #if defined(OS_ANDROID) |
267 #define MAYBE_ElideTextEllipsisFront DISABLED_ElideTextEllipsisFront | 267 #define MAYBE_ElideTextEllipsisFront DISABLED_ElideTextEllipsisFront |
268 #else | 268 #else |
269 #define MAYBE_ElideTextEllipsisFront ElideTextEllipsisFront | 269 #define MAYBE_ElideTextEllipsisFront ElideTextEllipsisFront |
270 #endif | 270 #endif |
(...skipping 14 matching lines...) Expand all Loading... |
285 { "Test", 0, base::string16() }, | 285 { "Test", 0, base::string16() }, |
286 { "Test", kEllipsisWidth, UTF8ToUTF16(kEllipsisStr) }, | 286 { "Test", kEllipsisWidth, UTF8ToUTF16(kEllipsisStr) }, |
287 { "", kTestWidth, base::string16() }, | 287 { "", kTestWidth, base::string16() }, |
288 { "Tes", kTestWidth, ASCIIToUTF16("Tes") }, | 288 { "Tes", kTestWidth, ASCIIToUTF16("Tes") }, |
289 { "Test", kTestWidth, ASCIIToUTF16("Test") }, | 289 { "Test", kTestWidth, ASCIIToUTF16("Test") }, |
290 { "Test123", kEllipsis23Width, UTF8ToUTF16(kEllipsisStr + "23") }, | 290 { "Test123", kEllipsis23Width, UTF8ToUTF16(kEllipsisStr + "23") }, |
291 }; | 291 }; |
292 | 292 |
293 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 293 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
294 base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, | 294 base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, |
295 cases[i].width, ELIDE_AT_BEGINNING); | 295 cases[i].width, ELIDE_HEAD); |
296 EXPECT_EQ(cases[i].output, result); | 296 EXPECT_EQ(cases[i].output, result); |
297 } | 297 } |
298 } | 298 } |
299 | 299 |
300 // Checks that all occurrences of |first_char| are followed by |second_char| and | 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|. | 301 // all occurrences of |second_char| are preceded by |first_char| in |text|. |
302 static void CheckSurrogatePairs(const base::string16& text, | 302 static void CheckSurrogatePairs(const base::string16& text, |
303 base::char16 first_char, | 303 base::char16 first_char, |
304 base::char16 second_char) { | 304 base::char16 second_char) { |
305 size_t index = text.find_first_of(first_char); | 305 size_t index = text.find_first_of(first_char); |
(...skipping 24 matching lines...) Expand all Loading... |
330 const base::string16 kTestString = | 330 const base::string16 kTestString = |
331 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); | 331 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); |
332 const float kTestStringWidth = GetStringWidthF(kTestString, font_list); | 332 const float kTestStringWidth = GetStringWidthF(kTestString, font_list); |
333 const base::char16 kSurrogateFirstChar = kTestString[0]; | 333 const base::char16 kSurrogateFirstChar = kTestString[0]; |
334 const base::char16 kSurrogateSecondChar = kTestString[1]; | 334 const base::char16 kSurrogateSecondChar = kTestString[1]; |
335 base::string16 result; | 335 base::string16 result; |
336 | 336 |
337 // Elide |kTextString| to all possible widths and check that no instance of | 337 // Elide |kTextString| to all possible widths and check that no instance of |
338 // |kSurrogate| was split in two. | 338 // |kSurrogate| was split in two. |
339 for (float width = 0; width <= kTestStringWidth; width++) { | 339 for (float width = 0; width <= kTestStringWidth; width++) { |
340 result = ElideText(kTestString, font_list, width, TRUNCATE_AT_END); | 340 result = ElideText(kTestString, font_list, width, TRUNCATE); |
341 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 341 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
342 | 342 |
343 result = ElideText(kTestString, font_list, width, ELIDE_AT_END); | 343 result = ElideText(kTestString, font_list, width, ELIDE_TAIL); |
344 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 344 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
345 | 345 |
346 result = ElideText(kTestString, font_list, width, ELIDE_IN_MIDDLE); | 346 result = ElideText(kTestString, font_list, width, ELIDE_MIDDLE); |
347 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 347 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
348 | 348 |
349 result = ElideText(kTestString, font_list, width, ELIDE_AT_BEGINNING); | 349 result = ElideText(kTestString, font_list, width, ELIDE_HEAD); |
350 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 350 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
351 } | 351 } |
352 } | 352 } |
353 | 353 |
354 // TODO(338784): Enable this on android. | 354 // TODO(338784): Enable this on android. |
355 #if defined(OS_ANDROID) | 355 #if defined(OS_ANDROID) |
356 #define MAYBE_ElideTextLongStrings DISABLED_ElideTextLongStrings | 356 #define MAYBE_ElideTextLongStrings DISABLED_ElideTextLongStrings |
357 #else | 357 #else |
358 #define MAYBE_ElideTextLongStrings ElideTextLongStrings | 358 #define MAYBE_ElideTextLongStrings ElideTextLongStrings |
359 #endif | 359 #endif |
(...skipping 23 matching lines...) Expand all Loading... |
383 { data_scheme + hundred_thousand_a, long_string_end }, | 383 { data_scheme + hundred_thousand_a, long_string_end }, |
384 { data_scheme + million_a, long_string_end }, | 384 { data_scheme + million_a, long_string_end }, |
385 }; | 385 }; |
386 | 386 |
387 const FontList font_list; | 387 const FontList font_list; |
388 float ellipsis_width = GetStringWidthF(kEllipsisStr, font_list); | 388 float ellipsis_width = GetStringWidthF(kEllipsisStr, font_list); |
389 for (size_t i = 0; i < arraysize(testcases_end); ++i) { | 389 for (size_t i = 0; i < arraysize(testcases_end); ++i) { |
390 // Compare sizes rather than actual contents because if the test fails, | 390 // Compare sizes rather than actual contents because if the test fails, |
391 // output is rather long. | 391 // output is rather long. |
392 EXPECT_EQ(testcases_end[i].output.size(), | 392 EXPECT_EQ(testcases_end[i].output.size(), |
393 ElideText( | 393 ElideText(testcases_end[i].input, font_list, |
394 testcases_end[i].input, | 394 GetStringWidthF(testcases_end[i].output, font_list), |
395 font_list, | 395 ELIDE_TAIL).size()); |
396 GetStringWidthF(testcases_end[i].output, font_list), | |
397 ELIDE_AT_END).size()); | |
398 EXPECT_EQ(kEllipsisStr, | 396 EXPECT_EQ(kEllipsisStr, |
399 ElideText(testcases_end[i].input, font_list, ellipsis_width, | 397 ElideText(testcases_end[i].input, font_list, ellipsis_width, |
400 ELIDE_AT_END)); | 398 ELIDE_TAIL)); |
401 } | 399 } |
402 | 400 |
403 size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2; | 401 size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2; |
404 base::string16 long_string_middle(data_scheme + | 402 base::string16 long_string_middle(data_scheme + |
405 base::string16(number_of_as - number_of_trailing_as, 'a') + kEllipsisStr + | 403 base::string16(number_of_as - number_of_trailing_as, 'a') + kEllipsisStr + |
406 base::string16(number_of_trailing_as, 'a')); | 404 base::string16(number_of_trailing_as, 'a')); |
407 UTF16Testcase testcases_middle[] = { | 405 UTF16Testcase testcases_middle[] = { |
408 { data_scheme + ten_a, data_scheme + ten_a }, | 406 { data_scheme + ten_a, data_scheme + ten_a }, |
409 { data_scheme + hundred_a, data_scheme + hundred_a }, | 407 { data_scheme + hundred_a, data_scheme + hundred_a }, |
410 { data_scheme + thousand_a, long_string_middle }, | 408 { data_scheme + thousand_a, long_string_middle }, |
411 { data_scheme + ten_thousand_a, long_string_middle }, | 409 { data_scheme + ten_thousand_a, long_string_middle }, |
412 { data_scheme + hundred_thousand_a, long_string_middle }, | 410 { data_scheme + hundred_thousand_a, long_string_middle }, |
413 { data_scheme + million_a, long_string_middle }, | 411 { data_scheme + million_a, long_string_middle }, |
414 }; | 412 }; |
415 | 413 |
416 for (size_t i = 0; i < arraysize(testcases_middle); ++i) { | 414 for (size_t i = 0; i < arraysize(testcases_middle); ++i) { |
417 // Compare sizes rather than actual contents because if the test fails, | 415 // Compare sizes rather than actual contents because if the test fails, |
418 // output is rather long. | 416 // output is rather long. |
419 EXPECT_EQ(testcases_middle[i].output.size(), | 417 EXPECT_EQ(testcases_middle[i].output.size(), |
420 ElideText( | 418 ElideText(testcases_middle[i].input, font_list, |
421 testcases_middle[i].input, | 419 GetStringWidthF(testcases_middle[i].output, font_list), |
422 font_list, | 420 ELIDE_MIDDLE).size()); |
423 GetStringWidthF(testcases_middle[i].output, font_list), | |
424 ELIDE_IN_MIDDLE).size()); | |
425 EXPECT_EQ(kEllipsisStr, | 421 EXPECT_EQ(kEllipsisStr, |
426 ElideText(testcases_middle[i].input, font_list, ellipsis_width, | 422 ElideText(testcases_middle[i].input, font_list, ellipsis_width, |
427 ELIDE_IN_MIDDLE)); | 423 ELIDE_MIDDLE)); |
428 } | 424 } |
429 | 425 |
430 base::string16 long_string_beginning( | 426 base::string16 long_string_beginning( |
431 kEllipsisStr + base::string16(number_of_as, 'a')); | 427 kEllipsisStr + base::string16(number_of_as, 'a')); |
432 UTF16Testcase testcases_beginning[] = { | 428 UTF16Testcase testcases_beginning[] = { |
433 { data_scheme + ten_a, data_scheme + ten_a }, | 429 { data_scheme + ten_a, data_scheme + ten_a }, |
434 { data_scheme + hundred_a, data_scheme + hundred_a }, | 430 { data_scheme + hundred_a, data_scheme + hundred_a }, |
435 { data_scheme + thousand_a, long_string_beginning }, | 431 { data_scheme + thousand_a, long_string_beginning }, |
436 { data_scheme + ten_thousand_a, long_string_beginning }, | 432 { data_scheme + ten_thousand_a, long_string_beginning }, |
437 { data_scheme + hundred_thousand_a, long_string_beginning }, | 433 { data_scheme + hundred_thousand_a, long_string_beginning }, |
438 { data_scheme + million_a, long_string_beginning }, | 434 { data_scheme + million_a, long_string_beginning }, |
439 }; | 435 }; |
440 for (size_t i = 0; i < arraysize(testcases_beginning); ++i) { | 436 for (size_t i = 0; i < arraysize(testcases_beginning); ++i) { |
441 EXPECT_EQ(testcases_beginning[i].output.size(), | 437 EXPECT_EQ(testcases_beginning[i].output.size(), |
442 ElideText( | 438 ElideText( |
443 testcases_beginning[i].input, font_list, | 439 testcases_beginning[i].input, font_list, |
444 GetStringWidthF(testcases_beginning[i].output, font_list), | 440 GetStringWidthF(testcases_beginning[i].output, font_list), |
445 ELIDE_AT_BEGINNING).size()); | 441 ELIDE_HEAD).size()); |
446 EXPECT_EQ(kEllipsisStr, | 442 EXPECT_EQ(kEllipsisStr, |
447 ElideText(testcases_beginning[i].input, font_list, ellipsis_width, | 443 ElideText(testcases_beginning[i].input, font_list, ellipsis_width, |
448 ELIDE_AT_BEGINNING)); | 444 ELIDE_HEAD)); |
449 } | 445 } |
450 } | 446 } |
451 | 447 |
452 TEST(TextEliderTest, ElideString) { | 448 TEST(TextEliderTest, ElideString) { |
453 struct TestData { | 449 struct TestData { |
454 const char* input; | 450 const char* input; |
455 int max_len; | 451 int max_len; |
456 bool result; | 452 bool result; |
457 const char* output; | 453 const char* output; |
458 } cases[] = { | 454 } cases[] = { |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
904 | 900 |
905 // Test adds ... at right spot when there is not enough room to break at a | 901 // Test adds ... at right spot when there is not enough room to break at a |
906 // word boundary. | 902 // word boundary. |
907 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); | 903 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); |
908 | 904 |
909 // Test completely truncates string if break is on initial whitespace. | 905 // Test completely truncates string if break is on initial whitespace. |
910 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); | 906 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); |
911 } | 907 } |
912 | 908 |
913 } // namespace gfx | 909 } // namespace gfx |
OLD | NEW |