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.
5 // Unit tests for eliding and formatting utility functions.
7 #include "ui/gfx/text_elider.h"
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"
16 #include "ui/gfx/font_list.h"
17 #include "ui/gfx/text_utils.h"
19 using base::ASCIIToUTF16
;
20 using base::UTF16ToUTF8
;
21 using base::UTF16ToWide
;
22 using base::UTF8ToUTF16
;
23 using base::WideToUTF16
;
30 const std::string input
;
31 const std::string output
;
35 const base::FilePath::StringType input
;
36 const std::string output
;
39 struct UTF16Testcase
{
40 const base::string16 input
;
41 const base::string16 output
;
47 const int compare_result
;
52 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF
53 // that calls [NSString sizeWithFont] returns the rounded string width.
54 // TODO(338784): Enable this on android.
55 #if defined(OS_IOS) || defined(OS_ANDROID)
56 #define MAYBE_ElideEmail DISABLED_ElideEmail
58 #define MAYBE_ElideEmail ElideEmail
60 TEST(TextEliderTest
, MAYBE_ElideEmail
) {
61 const std::string
kEllipsisStr(kEllipsis
);
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
[] = {
71 {"g@g.c", kEllipsisStr
},
72 {"ga@co.ca", "ga@c" + kEllipsisStr
+ "a"},
73 {"short@small.com", "s" + kEllipsisStr
+ "@s" + kEllipsisStr
},
74 {"short@small.com", "s" + kEllipsisStr
+ "@small.com"},
75 {"short@longbutlotsofspace.com", "short@longbutlotsofspace.com"},
76 {"short@longbutnotverymuchspace.com",
77 "short@long" + kEllipsisStr
+ ".com"},
78 {"la_short@longbutverytightspace.ca",
79 "la" + kEllipsisStr
+ "@l" + kEllipsisStr
+ "a"},
80 {"longusername@gmail.com", "long" + kEllipsisStr
+ "@gmail.com"},
81 {"elidetothemax@justfits.com", "e" + kEllipsisStr
+ "@justfits.com"},
82 {"thatom_somelongemail@thatdoesntfit.com",
83 "thatom" + kEllipsisStr
+ "@tha" + kEllipsisStr
+ "om"},
84 {"namefits@butthedomaindoesnt.com",
85 "namefits@butthedo" + kEllipsisStr
+ "snt.com"},
86 {"widthtootight@nospace.com", kEllipsisStr
},
87 {"nospaceforusername@l", kEllipsisStr
},
88 {"little@littlespace.com", "l" + kEllipsisStr
+ "@l" + kEllipsisStr
},
89 {"l@llllllllllllllllllllllll.com", "l@lllll" + kEllipsisStr
+ ".com"},
90 {"messed\"up@whyanat\"++@notgoogley.com",
91 "messed\"up@whyanat\"++@notgoogley.com"},
92 {"messed\"up@whyanat\"++@notgoogley.com",
93 "messed\"up@why" + kEllipsisStr
+ "@notgoogley.com"},
94 {"noca_messed\"up@whyanat\"++@notgoogley.ca",
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
},
105 const FontList font_list
;
106 for (size_t i
= 0; i
< arraysize(testcases
); ++i
) {
107 const base::string16 expected_output
= UTF8ToUTF16(testcases
[i
].output
);
108 EXPECT_EQ(expected_output
,
109 ElideText(UTF8ToUTF16(testcases
[i
].input
), font_list
,
110 GetStringWidthF(expected_output
, font_list
),
115 // TODO(338784): Enable this on android.
116 #if defined(OS_ANDROID)
117 #define MAYBE_ElideEmailMoreSpace DISABLED_ElideEmailMoreSpace
119 #define MAYBE_ElideEmailMoreSpace ElideEmailMoreSpace
121 TEST(TextEliderTest
, MAYBE_ElideEmailMoreSpace
) {
122 const int test_width_factors
[] = {
127 const std::string test_emails
[] = {
130 "short@verysuperdupperlongdomain.com",
131 "supermegalongusername@withasuperlonnnggggdomain.gouv.qc.ca",
134 const FontList font_list
;
135 for (size_t i
= 0; i
< arraysize(test_width_factors
); ++i
) {
136 const int test_width
=
137 font_list
.GetExpectedTextWidth(test_width_factors
[i
]);
138 for (size_t j
= 0; j
< arraysize(test_emails
); ++j
) {
139 // Extra space is available: the email should not be elided.
140 const base::string16 test_email
= UTF8ToUTF16(test_emails
[j
]);
141 EXPECT_EQ(test_email
,
142 ElideText(test_email
, font_list
, test_width
, ELIDE_EMAIL
));
147 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF
148 // that calls [NSString sizeWithFont] returns the rounded string width.
149 // TODO(338784): Enable this on android.
150 #if defined(OS_IOS) || defined(OS_ANDROID)
151 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding
153 #define MAYBE_TestFilenameEliding TestFilenameEliding
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]);
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"),
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"}
194 static const FontList font_list
;
195 for (size_t i
= 0; i
< arraysize(testcases
); ++i
) {
196 base::FilePath
filepath(testcases
[i
].input
);
197 base::string16 expected
= UTF8ToUTF16(testcases
[i
].output
);
198 expected
= base::i18n::GetDisplayStringInLTRDirectionality(expected
);
199 EXPECT_EQ(expected
, ElideFilename(filepath
, font_list
,
200 GetStringWidthF(UTF8ToUTF16(testcases
[i
].output
), font_list
)));
204 // TODO(338784): Enable this on android.
205 #if defined(OS_ANDROID)
206 #define MAYBE_ElideTextTruncate DISABLED_ElideTextTruncate
208 #define MAYBE_ElideTextTruncate ElideTextTruncate
210 TEST(TextEliderTest
, MAYBE_ElideTextTruncate
) {
211 const FontList font_list
;
212 const float kTestWidth
= GetStringWidthF(ASCIIToUTF16("Test"), font_list
);
220 { "", kTestWidth
, "" },
221 { "Tes", kTestWidth
, "Tes" },
222 { "Test", kTestWidth
, "Test" },
223 { "Tests", kTestWidth
, "Test" },
226 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
227 base::string16 result
= ElideText(UTF8ToUTF16(cases
[i
].input
), font_list
,
228 cases
[i
].width
, TRUNCATE
);
229 EXPECT_EQ(cases
[i
].output
, UTF16ToUTF8(result
));
233 // TODO(338784): Enable this on android.
234 #if defined(OS_ANDROID)
235 #define MAYBE_ElideTextEllipsis DISABLED_ElideTextEllipsis
237 #define MAYBE_ElideTextEllipsis ElideTextEllipsis
239 TEST(TextEliderTest
, MAYBE_ElideTextEllipsis
) {
240 const FontList font_list
;
241 const float kTestWidth
= GetStringWidthF(ASCIIToUTF16("Test"), font_list
);
242 const char* kEllipsis
= "\xE2\x80\xA6";
243 const float kEllipsisWidth
=
244 GetStringWidthF(UTF8ToUTF16(kEllipsis
), font_list
);
252 { "Test", kEllipsisWidth
, kEllipsis
},
253 { "", kTestWidth
, "" },
254 { "Tes", kTestWidth
, "Tes" },
255 { "Test", kTestWidth
, "Test" },
258 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
259 base::string16 result
= ElideText(UTF8ToUTF16(cases
[i
].input
), font_list
,
260 cases
[i
].width
, ELIDE_TAIL
);
261 EXPECT_EQ(cases
[i
].output
, UTF16ToUTF8(result
));
265 // TODO(338784): Enable this on android.
266 #if defined(OS_ANDROID)
267 #define MAYBE_ElideTextEllipsisFront DISABLED_ElideTextEllipsisFront
269 #define MAYBE_ElideTextEllipsisFront ElideTextEllipsisFront
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
);
282 const base::string16 output
;
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") },
293 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
294 base::string16 result
= ElideText(UTF8ToUTF16(cases
[i
].input
), font_list
,
295 cases
[i
].width
, ELIDE_HEAD
);
296 EXPECT_EQ(cases
[i
].output
, result
);
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|.
302 static void CheckSurrogatePairs(const base::string16
& text
,
303 base::char16 first_char
,
304 base::char16 second_char
) {
305 size_t index
= text
.find_first_of(first_char
);
306 while (index
!= base::string16::npos
) {
307 EXPECT_LT(index
, text
.length() - 1);
308 EXPECT_EQ(second_char
, text
[index
+ 1]);
309 index
= text
.find_first_of(first_char
, index
+ 1);
311 index
= text
.find_first_of(second_char
);
312 while (index
!= base::string16::npos
) {
313 EXPECT_GT(index
, 0U);
314 EXPECT_EQ(first_char
, text
[index
- 1]);
315 index
= text
.find_first_of(second_char
, index
+ 1);
319 // TODO(338784): Enable this on android.
320 #if defined(OS_ANDROID)
321 #define MAYBE_ElideTextSurrogatePairs DISABLED_ElideTextSurrogatePairs
323 #define MAYBE_ElideTextSurrogatePairs ElideTextSurrogatePairs
325 TEST(TextEliderTest
, MAYBE_ElideTextSurrogatePairs
) {
326 const FontList font_list
;
327 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as
328 // two characters forming a surrogate pair 0x0001D11E.
329 const std::string kSurrogate
= "\xF0\x9D\x84\x9E";
330 const base::string16 kTestString
=
331 UTF8ToUTF16(kSurrogate
+ "ab" + kSurrogate
+ kSurrogate
+ "cd");
332 const float kTestStringWidth
= GetStringWidthF(kTestString
, font_list
);
333 const base::char16 kSurrogateFirstChar
= kTestString
[0];
334 const base::char16 kSurrogateSecondChar
= kTestString
[1];
335 base::string16 result
;
337 // Elide |kTextString| to all possible widths and check that no instance of
338 // |kSurrogate| was split in two.
339 for (float width
= 0; width
<= kTestStringWidth
; width
++) {
340 result
= ElideText(kTestString
, font_list
, width
, TRUNCATE
);
341 CheckSurrogatePairs(result
, kSurrogateFirstChar
, kSurrogateSecondChar
);
343 result
= ElideText(kTestString
, font_list
, width
, ELIDE_TAIL
);
344 CheckSurrogatePairs(result
, kSurrogateFirstChar
, kSurrogateSecondChar
);
346 result
= ElideText(kTestString
, font_list
, width
, ELIDE_MIDDLE
);
347 CheckSurrogatePairs(result
, kSurrogateFirstChar
, kSurrogateSecondChar
);
349 result
= ElideText(kTestString
, font_list
, width
, ELIDE_HEAD
);
350 CheckSurrogatePairs(result
, kSurrogateFirstChar
, kSurrogateSecondChar
);
354 // TODO(338784): Enable this on android.
355 #if defined(OS_ANDROID)
356 #define MAYBE_ElideTextLongStrings DISABLED_ElideTextLongStrings
358 #define MAYBE_ElideTextLongStrings ElideTextLongStrings
360 TEST(TextEliderTest
, MAYBE_ElideTextLongStrings
) {
361 const base::string16 kEllipsisStr
= UTF8ToUTF16(kEllipsis
);
362 base::string16
data_scheme(UTF8ToUTF16("data:text/plain,"));
363 size_t data_scheme_length
= data_scheme
.length();
365 base::string16
ten_a(10, 'a');
366 base::string16
hundred_a(100, 'a');
367 base::string16
thousand_a(1000, 'a');
368 base::string16
ten_thousand_a(10000, 'a');
369 base::string16
hundred_thousand_a(100000, 'a');
370 base::string16
million_a(1000000, 'a');
372 // TODO(gbillock): Improve these tests by adding more string diversity and
373 // doing string compares instead of length compares. See bug 338836.
375 size_t number_of_as
= 156;
376 base::string16
long_string_end(
377 data_scheme
+ base::string16(number_of_as
, 'a') + kEllipsisStr
);
378 UTF16Testcase testcases_end
[] = {
379 { data_scheme
+ ten_a
, data_scheme
+ ten_a
},
380 { data_scheme
+ hundred_a
, data_scheme
+ hundred_a
},
381 { data_scheme
+ thousand_a
, long_string_end
},
382 { data_scheme
+ ten_thousand_a
, long_string_end
},
383 { data_scheme
+ hundred_thousand_a
, long_string_end
},
384 { data_scheme
+ million_a
, long_string_end
},
387 const FontList font_list
;
388 float ellipsis_width
= GetStringWidthF(kEllipsisStr
, font_list
);
389 for (size_t i
= 0; i
< arraysize(testcases_end
); ++i
) {
390 // Compare sizes rather than actual contents because if the test fails,
391 // output is rather long.
392 EXPECT_EQ(testcases_end
[i
].output
.size(),
393 ElideText(testcases_end
[i
].input
, font_list
,
394 GetStringWidthF(testcases_end
[i
].output
, font_list
),
396 EXPECT_EQ(kEllipsisStr
,
397 ElideText(testcases_end
[i
].input
, font_list
, ellipsis_width
,
401 size_t number_of_trailing_as
= (data_scheme_length
+ number_of_as
) / 2;
402 base::string16
long_string_middle(data_scheme
+
403 base::string16(number_of_as
- number_of_trailing_as
, 'a') + kEllipsisStr
+
404 base::string16(number_of_trailing_as
, 'a'));
405 UTF16Testcase testcases_middle
[] = {
406 { data_scheme
+ ten_a
, data_scheme
+ ten_a
},
407 { data_scheme
+ hundred_a
, data_scheme
+ hundred_a
},
408 { data_scheme
+ thousand_a
, long_string_middle
},
409 { data_scheme
+ ten_thousand_a
, long_string_middle
},
410 { data_scheme
+ hundred_thousand_a
, long_string_middle
},
411 { data_scheme
+ million_a
, long_string_middle
},
414 for (size_t i
= 0; i
< arraysize(testcases_middle
); ++i
) {
415 // Compare sizes rather than actual contents because if the test fails,
416 // output is rather long.
417 EXPECT_EQ(testcases_middle
[i
].output
.size(),
418 ElideText(testcases_middle
[i
].input
, font_list
,
419 GetStringWidthF(testcases_middle
[i
].output
, font_list
),
420 ELIDE_MIDDLE
).size());
421 EXPECT_EQ(kEllipsisStr
,
422 ElideText(testcases_middle
[i
].input
, font_list
, ellipsis_width
,
426 base::string16
long_string_beginning(
427 kEllipsisStr
+ base::string16(number_of_as
, 'a'));
428 UTF16Testcase testcases_beginning
[] = {
429 { data_scheme
+ ten_a
, data_scheme
+ ten_a
},
430 { data_scheme
+ hundred_a
, data_scheme
+ hundred_a
},
431 { data_scheme
+ thousand_a
, long_string_beginning
},
432 { data_scheme
+ ten_thousand_a
, long_string_beginning
},
433 { data_scheme
+ hundred_thousand_a
, long_string_beginning
},
434 { data_scheme
+ million_a
, long_string_beginning
},
436 for (size_t i
= 0; i
< arraysize(testcases_beginning
); ++i
) {
437 EXPECT_EQ(testcases_beginning
[i
].output
.size(),
439 testcases_beginning
[i
].input
, font_list
,
440 GetStringWidthF(testcases_beginning
[i
].output
, font_list
),
442 EXPECT_EQ(kEllipsisStr
,
443 ElideText(testcases_beginning
[i
].input
, font_list
, ellipsis_width
,
448 TEST(TextEliderTest
, ElideString
) {
455 { "Hello", 0, true, "" },
456 { "", 0, false, "" },
457 { "Hello, my name is Tom", 1, true, "H" },
458 { "Hello, my name is Tom", 2, true, "He" },
459 { "Hello, my name is Tom", 3, true, "H.m" },
460 { "Hello, my name is Tom", 4, true, "H..m" },
461 { "Hello, my name is Tom", 5, true, "H...m" },
462 { "Hello, my name is Tom", 6, true, "He...m" },
463 { "Hello, my name is Tom", 7, true, "He...om" },
464 { "Hello, my name is Tom", 10, true, "Hell...Tom" },
465 { "Hello, my name is Tom", 100, false, "Hello, my name is Tom" }
467 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
468 base::string16 output
;
469 EXPECT_EQ(cases
[i
].result
,
470 ElideString(UTF8ToUTF16(cases
[i
].input
),
471 cases
[i
].max_len
, &output
));
472 EXPECT_EQ(cases
[i
].output
, UTF16ToUTF8(output
));
476 // TODO(338784): Enable this on android.
477 #if defined(OS_ANDROID)
478 #define MAYBE_ElideRectangleText DISABLED_ElideRectangleText
480 #define MAYBE_ElideRectangleText ElideRectangleText
482 TEST(TextEliderTest
, MAYBE_ElideRectangleText
) {
483 const FontList font_list
;
484 const int line_height
= font_list
.GetHeight();
485 const float test_width
= GetStringWidthF(ASCIIToUTF16("Test"), font_list
);
489 float available_pixel_width
;
490 int available_pixel_height
;
494 { "", 0, 0, false, NULL
},
495 { "", 1, 1, false, NULL
},
496 { "Test", test_width
, 0, true, NULL
},
497 { "Test", test_width
, 1, false, "Test" },
498 { "Test", test_width
, line_height
, false, "Test" },
499 { "Test Test", test_width
, line_height
, true, "Test" },
500 { "Test Test", test_width
, line_height
+ 1, false, "Test|Test" },
501 { "Test Test", test_width
, line_height
* 2, false, "Test|Test" },
502 { "Test Test", test_width
, line_height
* 3, false, "Test|Test" },
503 { "Test Test", test_width
* 2, line_height
* 2, false, "Test|Test" },
504 { "Test Test", test_width
* 3, line_height
, false, "Test Test" },
505 { "Test\nTest", test_width
* 3, line_height
* 2, false, "Test|Test" },
506 { "Te\nst Te", test_width
, line_height
* 3, false, "Te|st|Te" },
507 { "\nTest", test_width
, line_height
* 2, false, "|Test" },
508 { "\nTest", test_width
, line_height
, true, "" },
509 { "\n\nTest", test_width
, line_height
* 3, false, "||Test" },
510 { "\n\nTest", test_width
, line_height
* 2, true, "|" },
511 { "Test\n", 2 * test_width
, line_height
* 5, false, "Test|" },
512 { "Test\n\n", 2 * test_width
, line_height
* 5, false, "Test||" },
513 { "Test\n\n\n", 2 * test_width
, line_height
* 5, false, "Test|||" },
514 { "Test\nTest\n\n", 2 * test_width
, line_height
* 5, false, "Test|Test||" },
515 { "Test\n\nTest\n", 2 * test_width
, line_height
* 5, false, "Test||Test|" },
516 { "Test\n\n\nTest", 2 * test_width
, line_height
* 5, false, "Test|||Test" },
517 { "Te ", test_width
, line_height
, false, "Te" },
518 { "Te Te Test", test_width
, 3 * line_height
, false, "Te|Te|Test" },
521 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
522 std::vector
<base::string16
> lines
;
523 EXPECT_EQ(cases
[i
].truncated_y
? INSUFFICIENT_SPACE_VERTICAL
: 0,
524 ElideRectangleText(UTF8ToUTF16(cases
[i
].input
),
526 cases
[i
].available_pixel_width
,
527 cases
[i
].available_pixel_height
,
530 if (cases
[i
].output
) {
531 const std::string result
= UTF16ToUTF8(JoinString(lines
, '|'));
532 EXPECT_EQ(cases
[i
].output
, result
) << "Case " << i
<< " failed!";
534 EXPECT_TRUE(lines
.empty()) << "Case " << i
<< " failed!";
539 // TODO(338784): Enable this on android.
540 #if defined(OS_ANDROID)
541 #define MAYBE_ElideRectangleTextPunctuation \
542 DISABLED_ElideRectangleTextPunctuation
544 #define MAYBE_ElideRectangleTextPunctuation ElideRectangleTextPunctuation
546 TEST(TextEliderTest
, MAYBE_ElideRectangleTextPunctuation
) {
547 const FontList font_list
;
548 const int line_height
= font_list
.GetHeight();
549 const float test_width
= GetStringWidthF(ASCIIToUTF16("Test"), font_list
);
550 const float test_t_width
= GetStringWidthF(ASCIIToUTF16("Test T"), font_list
);
554 float available_pixel_width
;
555 int available_pixel_height
;
560 { "Test T.", test_t_width
, line_height
* 2, false, false, "Test|T." },
561 { "Test T ?", test_t_width
, line_height
* 2, false, false, "Test|T ?" },
562 { "Test. Test", test_width
, line_height
* 3, false, true, "Test|Test" },
563 { "Test. Test", test_width
, line_height
* 3, true, false, "Test|.|Test" },
566 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
567 std::vector
<base::string16
> lines
;
568 const WordWrapBehavior wrap_behavior
=
569 (cases
[i
].wrap_words
? WRAP_LONG_WORDS
: TRUNCATE_LONG_WORDS
);
570 EXPECT_EQ(cases
[i
].truncated_x
? INSUFFICIENT_SPACE_HORIZONTAL
: 0,
571 ElideRectangleText(UTF8ToUTF16(cases
[i
].input
),
573 cases
[i
].available_pixel_width
,
574 cases
[i
].available_pixel_height
,
577 if (cases
[i
].output
) {
578 const std::string result
= UTF16ToUTF8(JoinString(lines
, '|'));
579 EXPECT_EQ(cases
[i
].output
, result
) << "Case " << i
<< " failed!";
581 EXPECT_TRUE(lines
.empty()) << "Case " << i
<< " failed!";
586 // TODO(338784): Enable this on android.
587 #if defined(OS_ANDROID)
588 #define MAYBE_ElideRectangleTextLongWords DISABLED_ElideRectangleTextLongWords
590 #define MAYBE_ElideRectangleTextLongWords ElideRectangleTextLongWords
592 TEST(TextEliderTest
, MAYBE_ElideRectangleTextLongWords
) {
593 const FontList font_list
;
594 const int kAvailableHeight
= 1000;
595 const base::string16 kElidedTesting
=
596 UTF8ToUTF16(std::string("Tes") + kEllipsis
);
597 const float elided_width
= GetStringWidthF(kElidedTesting
, font_list
);
598 const float test_width
= GetStringWidthF(ASCIIToUTF16("Test"), font_list
);
602 float available_pixel_width
;
603 WordWrapBehavior wrap_behavior
;
607 { "Testing", test_width
, IGNORE_LONG_WORDS
, false, "Testing" },
608 { "X Testing", test_width
, IGNORE_LONG_WORDS
, false, "X|Testing" },
609 { "Test Testing", test_width
, IGNORE_LONG_WORDS
, false, "Test|Testing" },
610 { "Test\nTesting", test_width
, IGNORE_LONG_WORDS
, false, "Test|Testing" },
611 { "Test Tests ", test_width
, IGNORE_LONG_WORDS
, false, "Test|Tests" },
612 { "Test Tests T", test_width
, IGNORE_LONG_WORDS
, false, "Test|Tests|T" },
614 { "Testing", elided_width
, ELIDE_LONG_WORDS
, true, "Tes..." },
615 { "X Testing", elided_width
, ELIDE_LONG_WORDS
, true, "X|Tes..." },
616 { "Test Testing", elided_width
, ELIDE_LONG_WORDS
, true, "Test|Tes..." },
617 { "Test\nTesting", elided_width
, ELIDE_LONG_WORDS
, true, "Test|Tes..." },
619 { "Testing", test_width
, TRUNCATE_LONG_WORDS
, true, "Test" },
620 { "X Testing", test_width
, TRUNCATE_LONG_WORDS
, true, "X|Test" },
621 { "Test Testing", test_width
, TRUNCATE_LONG_WORDS
, true, "Test|Test" },
622 { "Test\nTesting", test_width
, TRUNCATE_LONG_WORDS
, true, "Test|Test" },
623 { "Test Tests ", test_width
, TRUNCATE_LONG_WORDS
, true, "Test|Test" },
624 { "Test Tests T", test_width
, TRUNCATE_LONG_WORDS
, true, "Test|Test|T" },
626 { "Testing", test_width
, WRAP_LONG_WORDS
, false, "Test|ing" },
627 { "X Testing", test_width
, WRAP_LONG_WORDS
, false, "X|Test|ing" },
628 { "Test Testing", test_width
, WRAP_LONG_WORDS
, false, "Test|Test|ing" },
629 { "Test\nTesting", test_width
, WRAP_LONG_WORDS
, false, "Test|Test|ing" },
630 { "Test Tests ", test_width
, WRAP_LONG_WORDS
, false, "Test|Test|s" },
631 { "Test Tests T", test_width
, WRAP_LONG_WORDS
, false, "Test|Test|s T" },
632 { "TestTestTest", test_width
, WRAP_LONG_WORDS
, false, "Test|Test|Test" },
633 { "TestTestTestT", test_width
, WRAP_LONG_WORDS
, false, "Test|Test|Test|T" },
636 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
637 std::vector
<base::string16
> lines
;
638 EXPECT_EQ(cases
[i
].truncated_x
? INSUFFICIENT_SPACE_HORIZONTAL
: 0,
639 ElideRectangleText(UTF8ToUTF16(cases
[i
].input
),
641 cases
[i
].available_pixel_width
,
643 cases
[i
].wrap_behavior
,
645 std::string
expected_output(cases
[i
].output
);
646 ReplaceSubstringsAfterOffset(&expected_output
, 0, "...", kEllipsis
);
647 const std::string result
= UTF16ToUTF8(JoinString(lines
, '|'));
648 EXPECT_EQ(expected_output
, result
) << "Case " << i
<< " failed!";
652 // This test is to make sure that the width of each wrapped line does not
653 // exceed the available width. On some platform like Mac, this test used to
654 // fail because the truncated integer width is returned for the string
655 // and the accumulation of the truncated values causes the elide function
656 // to wrap incorrectly.
657 // TODO(338784): Enable this on android.
658 #if defined(OS_ANDROID)
659 #define MAYBE_ElideRectangleTextCheckLineWidth \
660 DISABLED_ElideRectangleTextCheckLineWidth
662 #define MAYBE_ElideRectangleTextCheckLineWidth ElideRectangleTextCheckLineWidth
664 TEST(TextEliderTest
, MAYBE_ElideRectangleTextCheckLineWidth
) {
666 #if defined(OS_MACOSX) && !defined(OS_IOS)
667 // Use a specific font to expose the line width exceeding problem.
668 font_list
= FontList(Font("LucidaGrande", 12));
670 const float kAvailableWidth
= 235;
671 const int kAvailableHeight
= 1000;
672 const char text
[] = "that Russian place we used to go to after fencing";
673 std::vector
<base::string16
> lines
;
674 EXPECT_EQ(0, ElideRectangleText(UTF8ToUTF16(text
),
680 ASSERT_EQ(2u, lines
.size());
681 EXPECT_LE(GetStringWidthF(lines
[0], font_list
), kAvailableWidth
);
682 EXPECT_LE(GetStringWidthF(lines
[1], font_list
), kAvailableWidth
);
685 TEST(TextEliderTest
, ElideRectangleString
) {
693 { "", 0, 0, false, "" },
694 { "", 1, 1, false, "" },
695 { "Hi, my name is\nTom", 0, 0, true, "..." },
696 { "Hi, my name is\nTom", 1, 0, true, "\n..." },
697 { "Hi, my name is\nTom", 0, 1, true, "..." },
698 { "Hi, my name is\nTom", 1, 1, true, "H\n..." },
699 { "Hi, my name is\nTom", 2, 1, true, "H\ni\n..." },
700 { "Hi, my name is\nTom", 3, 1, true, "H\ni\n,\n..." },
701 { "Hi, my name is\nTom", 4, 1, true, "H\ni\n,\n \n..." },
702 { "Hi, my name is\nTom", 5, 1, true, "H\ni\n,\n \nm\n..." },
703 { "Hi, my name is\nTom", 0, 2, true, "..." },
704 { "Hi, my name is\nTom", 1, 2, true, "Hi\n..." },
705 { "Hi, my name is\nTom", 2, 2, true, "Hi\n, \n..." },
706 { "Hi, my name is\nTom", 3, 2, true, "Hi\n, \nmy\n..." },
707 { "Hi, my name is\nTom", 4, 2, true, "Hi\n, \nmy\n n\n..." },
708 { "Hi, my name is\nTom", 5, 2, true, "Hi\n, \nmy\n n\nam\n..." },
709 { "Hi, my name is\nTom", 0, 3, true, "..." },
710 { "Hi, my name is\nTom", 1, 3, true, "Hi,\n..." },
711 { "Hi, my name is\nTom", 2, 3, true, "Hi,\n my\n..." },
712 { "Hi, my name is\nTom", 3, 3, true, "Hi,\n my\n na\n..." },
713 { "Hi, my name is\nTom", 4, 3, true, "Hi,\n my\n na\nme \n..." },
714 { "Hi, my name is\nTom", 5, 3, true, "Hi,\n my\n na\nme \nis\n..." },
715 { "Hi, my name is\nTom", 1, 4, true, "Hi, \n..." },
716 { "Hi, my name is\nTom", 2, 4, true, "Hi, \nmy n\n..." },
717 { "Hi, my name is\nTom", 3, 4, true, "Hi, \nmy n\name \n..." },
718 { "Hi, my name is\nTom", 4, 4, true, "Hi, \nmy n\name \nis\n..." },
719 { "Hi, my name is\nTom", 5, 4, false, "Hi, \nmy n\name \nis\nTom" },
720 { "Hi, my name is\nTom", 1, 5, true, "Hi, \n..." },
721 { "Hi, my name is\nTom", 2, 5, true, "Hi, \nmy na\n..." },
722 { "Hi, my name is\nTom", 3, 5, true, "Hi, \nmy na\nme \n..." },
723 { "Hi, my name is\nTom", 4, 5, true, "Hi, \nmy na\nme \nis\n..." },
724 { "Hi, my name is\nTom", 5, 5, false, "Hi, \nmy na\nme \nis\nTom" },
725 { "Hi, my name is\nTom", 1, 6, true, "Hi, \n..." },
726 { "Hi, my name is\nTom", 2, 6, true, "Hi, \nmy \n..." },
727 { "Hi, my name is\nTom", 3, 6, true, "Hi, \nmy \nname \n..." },
728 { "Hi, my name is\nTom", 4, 6, true, "Hi, \nmy \nname \nis\n..." },
729 { "Hi, my name is\nTom", 5, 6, false, "Hi, \nmy \nname \nis\nTom" },
730 { "Hi, my name is\nTom", 1, 7, true, "Hi, \n..." },
731 { "Hi, my name is\nTom", 2, 7, true, "Hi, \nmy \n..." },
732 { "Hi, my name is\nTom", 3, 7, true, "Hi, \nmy \nname \n..." },
733 { "Hi, my name is\nTom", 4, 7, true, "Hi, \nmy \nname \nis\n..." },
734 { "Hi, my name is\nTom", 5, 7, false, "Hi, \nmy \nname \nis\nTom" },
735 { "Hi, my name is\nTom", 1, 8, true, "Hi, my \n..." },
736 { "Hi, my name is\nTom", 2, 8, true, "Hi, my \nname \n..." },
737 { "Hi, my name is\nTom", 3, 8, true, "Hi, my \nname \nis\n..." },
738 { "Hi, my name is\nTom", 4, 8, false, "Hi, my \nname \nis\nTom" },
739 { "Hi, my name is\nTom", 1, 9, true, "Hi, my \n..." },
740 { "Hi, my name is\nTom", 2, 9, true, "Hi, my \nname is\n..." },
741 { "Hi, my name is\nTom", 3, 9, false, "Hi, my \nname is\nTom" },
742 { "Hi, my name is\nTom", 1, 10, true, "Hi, my \n..." },
743 { "Hi, my name is\nTom", 2, 10, true, "Hi, my \nname is\n..." },
744 { "Hi, my name is\nTom", 3, 10, false, "Hi, my \nname is\nTom" },
745 { "Hi, my name is\nTom", 1, 11, true, "Hi, my \n..." },
746 { "Hi, my name is\nTom", 2, 11, true, "Hi, my \nname is\n..." },
747 { "Hi, my name is\nTom", 3, 11, false, "Hi, my \nname is\nTom" },
748 { "Hi, my name is\nTom", 1, 12, true, "Hi, my \n..." },
749 { "Hi, my name is\nTom", 2, 12, true, "Hi, my \nname is\n..." },
750 { "Hi, my name is\nTom", 3, 12, false, "Hi, my \nname is\nTom" },
751 { "Hi, my name is\nTom", 1, 13, true, "Hi, my name \n..." },
752 { "Hi, my name is\nTom", 2, 13, true, "Hi, my name \nis\n..." },
753 { "Hi, my name is\nTom", 3, 13, false, "Hi, my name \nis\nTom" },
754 { "Hi, my name is\nTom", 1, 20, true, "Hi, my name is\n..." },
755 { "Hi, my name is\nTom", 2, 20, false, "Hi, my name is\nTom" },
756 { "Hi, my name is Tom", 1, 40, false, "Hi, my name is Tom" },
758 base::string16 output
;
759 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
760 EXPECT_EQ(cases
[i
].result
,
761 ElideRectangleString(UTF8ToUTF16(cases
[i
].input
),
762 cases
[i
].max_rows
, cases
[i
].max_cols
,
764 EXPECT_EQ(cases
[i
].output
, UTF16ToUTF8(output
));
768 TEST(TextEliderTest
, ElideRectangleStringNotStrict
) {
776 { "", 0, 0, false, "" },
777 { "", 1, 1, false, "" },
778 { "Hi, my name_is\nDick", 0, 0, true, "..." },
779 { "Hi, my name_is\nDick", 1, 0, true, "\n..." },
780 { "Hi, my name_is\nDick", 0, 1, true, "..." },
781 { "Hi, my name_is\nDick", 1, 1, true, "H\n..." },
782 { "Hi, my name_is\nDick", 2, 1, true, "H\ni\n..." },
783 { "Hi, my name_is\nDick", 3, 1, true, "H\ni\n,\n..." },
784 { "Hi, my name_is\nDick", 4, 1, true, "H\ni\n,\n \n..." },
785 { "Hi, my name_is\nDick", 5, 1, true, "H\ni\n,\n \nm\n..." },
786 { "Hi, my name_is\nDick", 0, 2, true, "..." },
787 { "Hi, my name_is\nDick", 1, 2, true, "Hi\n..." },
788 { "Hi, my name_is\nDick", 2, 2, true, "Hi\n, \n..." },
789 { "Hi, my name_is\nDick", 3, 2, true, "Hi\n, \nmy\n..." },
790 { "Hi, my name_is\nDick", 4, 2, true, "Hi\n, \nmy\n n\n..." },
791 { "Hi, my name_is\nDick", 5, 2, true, "Hi\n, \nmy\n n\nam\n..." },
792 { "Hi, my name_is\nDick", 0, 3, true, "..." },
793 { "Hi, my name_is\nDick", 1, 3, true, "Hi,\n..." },
794 { "Hi, my name_is\nDick", 2, 3, true, "Hi,\n my\n..." },
795 { "Hi, my name_is\nDick", 3, 3, true, "Hi,\n my\n na\n..." },
796 { "Hi, my name_is\nDick", 4, 3, true, "Hi,\n my\n na\nme_\n..." },
797 { "Hi, my name_is\nDick", 5, 3, true, "Hi,\n my\n na\nme_\nis\n..." },
798 { "Hi, my name_is\nDick", 1, 4, true, "Hi, ..." },
799 { "Hi, my name_is\nDick", 2, 4, true, "Hi, my n\n..." },
800 { "Hi, my name_is\nDick", 3, 4, true, "Hi, my n\name_\n..." },
801 { "Hi, my name_is\nDick", 4, 4, true, "Hi, my n\name_\nis\n..." },
802 { "Hi, my name_is\nDick", 5, 4, false, "Hi, my n\name_\nis\nDick" },
803 { "Hi, my name_is\nDick", 1, 5, true, "Hi, ..." },
804 { "Hi, my name_is\nDick", 2, 5, true, "Hi, my na\n..." },
805 { "Hi, my name_is\nDick", 3, 5, true, "Hi, my na\nme_is\n..." },
806 { "Hi, my name_is\nDick", 4, 5, true, "Hi, my na\nme_is\n\n..." },
807 { "Hi, my name_is\nDick", 5, 5, false, "Hi, my na\nme_is\n\nDick" },
808 { "Hi, my name_is\nDick", 1, 6, true, "Hi, ..." },
809 { "Hi, my name_is\nDick", 2, 6, true, "Hi, my nam\n..." },
810 { "Hi, my name_is\nDick", 3, 6, true, "Hi, my nam\ne_is\n..." },
811 { "Hi, my name_is\nDick", 4, 6, false, "Hi, my nam\ne_is\nDick" },
812 { "Hi, my name_is\nDick", 5, 6, false, "Hi, my nam\ne_is\nDick" },
813 { "Hi, my name_is\nDick", 1, 7, true, "Hi, ..." },
814 { "Hi, my name_is\nDick", 2, 7, true, "Hi, my name\n..." },
815 { "Hi, my name_is\nDick", 3, 7, true, "Hi, my name\n_is\n..." },
816 { "Hi, my name_is\nDick", 4, 7, false, "Hi, my name\n_is\nDick" },
817 { "Hi, my name_is\nDick", 5, 7, false, "Hi, my name\n_is\nDick" },
818 { "Hi, my name_is\nDick", 1, 8, true, "Hi, my n\n..." },
819 { "Hi, my name_is\nDick", 2, 8, true, "Hi, my n\name_is\n..." },
820 { "Hi, my name_is\nDick", 3, 8, false, "Hi, my n\name_is\nDick" },
821 { "Hi, my name_is\nDick", 1, 9, true, "Hi, my ..." },
822 { "Hi, my name_is\nDick", 2, 9, true, "Hi, my name_is\n..." },
823 { "Hi, my name_is\nDick", 3, 9, false, "Hi, my name_is\nDick" },
824 { "Hi, my name_is\nDick", 1, 10, true, "Hi, my ..." },
825 { "Hi, my name_is\nDick", 2, 10, true, "Hi, my name_is\n..." },
826 { "Hi, my name_is\nDick", 3, 10, false, "Hi, my name_is\nDick" },
827 { "Hi, my name_is\nDick", 1, 11, true, "Hi, my ..." },
828 { "Hi, my name_is\nDick", 2, 11, true, "Hi, my name_is\n..." },
829 { "Hi, my name_is\nDick", 3, 11, false, "Hi, my name_is\nDick" },
830 { "Hi, my name_is\nDick", 1, 12, true, "Hi, my ..." },
831 { "Hi, my name_is\nDick", 2, 12, true, "Hi, my name_is\n..." },
832 { "Hi, my name_is\nDick", 3, 12, false, "Hi, my name_is\nDick" },
833 { "Hi, my name_is\nDick", 1, 13, true, "Hi, my ..." },
834 { "Hi, my name_is\nDick", 2, 13, true, "Hi, my name_is\n..." },
835 { "Hi, my name_is\nDick", 3, 13, false, "Hi, my name_is\nDick" },
836 { "Hi, my name_is\nDick", 1, 20, true, "Hi, my name_is\n..." },
837 { "Hi, my name_is\nDick", 2, 20, false, "Hi, my name_is\nDick" },
838 { "Hi, my name_is Dick", 1, 40, false, "Hi, my name_is Dick" },
840 base::string16 output
;
841 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
842 EXPECT_EQ(cases
[i
].result
,
843 ElideRectangleString(UTF8ToUTF16(cases
[i
].input
),
844 cases
[i
].max_rows
, cases
[i
].max_cols
,
846 EXPECT_EQ(cases
[i
].output
, UTF16ToUTF8(output
));
850 TEST(TextEliderTest
, ElideRectangleWide16
) {
851 // Two greek words separated by space.
852 const base::string16
str(WideToUTF16(
853 L
"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9"
854 L
"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2"));
855 const base::string16
out1(WideToUTF16(
856 L
"\x03a0\x03b1\x03b3\x03ba\n"
857 L
"\x03cc\x03c3\x03bc\x03b9\n"
859 const base::string16
out2(WideToUTF16(
860 L
"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9\x03bf\x03c2\x0020\n"
861 L
"\x0399\x03c3\x03c4\x03cc\x03c2"));
862 base::string16 output
;
863 EXPECT_TRUE(ElideRectangleString(str
, 2, 4, true, &output
));
864 EXPECT_EQ(out1
, output
);
865 EXPECT_FALSE(ElideRectangleString(str
, 2, 12, true, &output
));
866 EXPECT_EQ(out2
, output
);
869 TEST(TextEliderTest
, ElideRectangleWide32
) {
870 // Four U+1D49C MATHEMATICAL SCRIPT CAPITAL A followed by space "aaaaa".
871 const base::string16
str(UTF8ToUTF16(
872 "\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C"
874 const base::string16
out(UTF8ToUTF16(
875 "\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\n"
876 "\xF0\x9D\x92\x9C \naaa\n..."));
877 base::string16 output
;
878 EXPECT_TRUE(ElideRectangleString(str
, 3, 3, true, &output
));
879 EXPECT_EQ(out
, output
);
882 TEST(TextEliderTest
, TruncateString
) {
883 base::string16 string
= ASCIIToUTF16("foooooey bxxxar baz");
885 // Make sure it doesn't modify the string if length > string length.
886 EXPECT_EQ(string
, TruncateString(string
, 100));
888 // Test no characters.
889 EXPECT_EQ(L
"", UTF16ToWide(TruncateString(string
, 0)));
892 EXPECT_EQ(L
"\x2026", UTF16ToWide(TruncateString(string
, 1)));
894 // Test adds ... at right spot when there is enough room to break at a
896 EXPECT_EQ(L
"foooooey\x2026", UTF16ToWide(TruncateString(string
, 14)));
898 // Test adds ... at right spot when there is not enough space in first word.
899 EXPECT_EQ(L
"f\x2026", UTF16ToWide(TruncateString(string
, 2)));
901 // Test adds ... at right spot when there is not enough room to break at a
903 EXPECT_EQ(L
"foooooey\x2026", UTF16ToWide(TruncateString(string
, 11)));
905 // Test completely truncates string if break is on initial whitespace.
906 EXPECT_EQ(L
"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2)));