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" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "ui/gfx/font.h" | 15 #include "ui/gfx/font.h" |
| 16 #include "ui/gfx/font_list.h" |
| 17 #include "ui/gfx/text_utils.h" |
16 #include "url/gurl.h" | 18 #include "url/gurl.h" |
17 | 19 |
18 namespace gfx { | 20 namespace gfx { |
19 | 21 |
20 namespace { | 22 namespace { |
21 | 23 |
22 struct Testcase { | 24 struct Testcase { |
23 const std::string input; | 25 const std::string input; |
24 const std::string output; | 26 const std::string output; |
25 }; | 27 }; |
(...skipping 20 matching lines...) Expand all Loading... |
46 const GURL url(testcases[i].input); | 48 const GURL url(testcases[i].input); |
47 // Should we test with non-empty language list? | 49 // Should we test with non-empty language list? |
48 // That's kinda redundant with net_util_unittests. | 50 // That's kinda redundant with net_util_unittests. |
49 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), | 51 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), |
50 ElideUrl(url, font, | 52 ElideUrl(url, font, |
51 font.GetStringWidth(UTF8ToUTF16(testcases[i].output)), | 53 font.GetStringWidth(UTF8ToUTF16(testcases[i].output)), |
52 std::string())); | 54 std::string())); |
53 } | 55 } |
54 } | 56 } |
55 | 57 |
56 gfx::Font GetTestingFont() { | |
57 gfx::Font font; | |
58 #if defined(OS_MACOSX) | |
59 // Use a specific font for certain tests on Mac. | |
60 // 1) Different Mac machines might be configured with different default font. | |
61 // The number of extra pixels needed to make ElideEmail/TestFilenameEliding | |
62 // tests work might vary per the default font. | |
63 // 2) This specific font helps expose the line width exceeding problem as in | |
64 // ElideRectangleTextCheckLineWidth. | |
65 font = gfx::Font("LucidaGrande", 12); | |
66 #endif | |
67 return font; | |
68 } | |
69 | |
70 } // namespace | 58 } // namespace |
71 | 59 |
72 // TODO(ios): Complex eliding is off by one for some of those tests on iOS. | 60 // TODO(ios): This test fails on iOS because iOS version of gfx::GetStringWidth |
73 // See crbug.com/154019 | 61 // that calls [NSString sizeWithFont] returns the rounded string width. |
74 #if defined(OS_IOS) | 62 #if defined(OS_IOS) |
75 #define MAYBE_ElideEmail DISABLED_ElideEmail | 63 #define MAYBE_ElideEmail DISABLED_ElideEmail |
76 #else | 64 #else |
77 #define MAYBE_ElideEmail ElideEmail | 65 #define MAYBE_ElideEmail ElideEmail |
78 #endif | 66 #endif |
79 TEST(TextEliderTest, MAYBE_ElideEmail) { | 67 TEST(TextEliderTest, MAYBE_ElideEmail) { |
80 const std::string kEllipsisStr(kEllipsis); | 68 const std::string kEllipsisStr(kEllipsis); |
81 | 69 |
82 // Test emails and their expected elided forms (from which the available | 70 // Test emails and their expected elided forms (from which the available |
83 // widths will be derived). | 71 // widths will be derived). |
(...skipping 30 matching lines...) Expand all Loading... |
114 "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"}, | 102 "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"}, |
115 {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com", | 103 {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com", |
116 "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"}, | 104 "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"}, |
117 // Special case: "m..." takes more than half of the available width; thus | 105 // Special case: "m..." takes more than half of the available width; thus |
118 // the domain must elide to "l..." and not "l...l" as it must allow enough | 106 // the domain must elide to "l..." and not "l...l" as it must allow enough |
119 // space for the minimal username elision although its half of the | 107 // space for the minimal username elision although its half of the |
120 // available width would normally allow it to elide to "l...l". | 108 // available width would normally allow it to elide to "l...l". |
121 {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr}, | 109 {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr}, |
122 }; | 110 }; |
123 | 111 |
124 const gfx::Font font = GetTestingFont(); | 112 const gfx::Font font; |
125 for (size_t i = 0; i < arraysize(testcases); ++i) { | 113 for (size_t i = 0; i < arraysize(testcases); ++i) { |
126 const string16 expected_output = UTF8ToUTF16(testcases[i].output); | 114 const string16 expected_output = UTF8ToUTF16(testcases[i].output); |
127 int available_width = font.GetStringWidth(expected_output); | |
128 #if defined(OS_MACOSX) | |
129 // Give two extra pixels to offset the ceiling width returned by | |
130 // GetStringWidth on Mac. This workaround will no longer be needed once | |
131 // the floating point width is adopted (http://crbug.com/288987). | |
132 // Note that we need one more pixel than TestFilenameEliding because | |
133 // multiple strings are elided and we need to offset more. | |
134 available_width += 2; | |
135 #endif | |
136 EXPECT_EQ(expected_output, | 115 EXPECT_EQ(expected_output, |
137 ElideEmail(UTF8ToUTF16(testcases[i].input), | 116 ElideEmail(UTF8ToUTF16(testcases[i].input), |
138 font, | 117 font, |
139 available_width)); | 118 font.GetStringWidth(expected_output))); |
140 } | 119 } |
141 } | 120 } |
142 | 121 |
143 TEST(TextEliderTest, ElideEmailMoreSpace) { | 122 TEST(TextEliderTest, ElideEmailMoreSpace) { |
144 const int test_width_factors[] = { | 123 const int test_width_factors[] = { |
145 100, | 124 100, |
146 10000, | 125 10000, |
147 1000000, | 126 1000000, |
148 }; | 127 }; |
149 const std::string test_emails[] = { | 128 const std::string test_emails[] = { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 // When there is very little space available, the elision code will shorten | 173 // When there is very little space available, the elision code will shorten |
195 // both path AND file name to an ellipsis - ".../...". To avoid this result, | 174 // both path AND file name to an ellipsis - ".../...". To avoid this result, |
196 // there is a hack in place that simply treats them as one string in this | 175 // there is a hack in place that simply treats them as one string in this |
197 // case. | 176 // case. |
198 TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { | 177 TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { |
199 const std::string kEllipsisStr(kEllipsis); | 178 const std::string kEllipsisStr(kEllipsis); |
200 | 179 |
201 // Very little space, would cause double ellipsis. | 180 // Very little space, would cause double ellipsis. |
202 gfx::Font font; | 181 gfx::Font font; |
203 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); | 182 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); |
204 int available_width = font.GetStringWidth( | 183 float available_width = font.GetStringWidth( |
205 UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr)); | 184 UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr)); |
206 | 185 |
207 // Create the expected string, after elision. Depending on font size, the | 186 // Create the expected string, after elision. Depending on font size, the |
208 // directory might become /dir... or /di... or/d... - it never should be | 187 // directory might become /dir... or /di... or/d... - it never should be |
209 // shorter than that. (If it is, the font considers d... to be longer | 188 // shorter than that. (If it is, the font considers d... to be longer |
210 // than .../... - that should never happen). | 189 // than .../... - that should never happen). |
211 ASSERT_GT(font.GetStringWidth(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr)), | 190 ASSERT_GT(font.GetStringWidth(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr)), |
212 font.GetStringWidth(UTF8ToUTF16("d" + kEllipsisStr))); | 191 font.GetStringWidth(UTF8ToUTF16("d" + kEllipsisStr))); |
213 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); | 192 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); |
214 string16 expected = ElideUrl(long_url, font, available_width, std::string()); | 193 string16 expected = ElideUrl(long_url, font, available_width, std::string()); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 "C:/" + kEllipsisStr + "/filename"}, | 260 "C:/" + kEllipsisStr + "/filename"}, |
282 #endif | 261 #endif |
283 {"file://filer/foo/bar/file", "filer/foo/bar/file"}, | 262 {"file://filer/foo/bar/file", "filer/foo/bar/file"}, |
284 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"}, | 263 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"}, |
285 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"}, | 264 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"}, |
286 }; | 265 }; |
287 | 266 |
288 RunUrlTest(testcases, arraysize(testcases)); | 267 RunUrlTest(testcases, arraysize(testcases)); |
289 } | 268 } |
290 | 269 |
291 // TODO(ios): Complex eliding is off by one for some of those tests on iOS. | 270 // TODO(ios): This test fails on iOS because iOS version of gfx::GetStringWidth |
| 271 // that calls [NSString sizeWithFont] returns the rounded string width. |
292 // See crbug.com/154019 | 272 // See crbug.com/154019 |
293 #if defined(OS_IOS) | 273 #if defined(OS_IOS) |
294 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding | 274 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding |
295 #else | 275 #else |
296 #define MAYBE_TestFilenameEliding TestFilenameEliding | 276 #define MAYBE_TestFilenameEliding TestFilenameEliding |
297 #endif | 277 #endif |
298 TEST(TextEliderTest, MAYBE_TestFilenameEliding) { | 278 TEST(TextEliderTest, MAYBE_TestFilenameEliding) { |
299 const std::string kEllipsisStr(kEllipsis); | 279 const std::string kEllipsisStr(kEllipsis); |
300 const base::FilePath::StringType kPathSeparator = | 280 const base::FilePath::StringType kPathSeparator = |
301 base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]); | 281 base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]); |
(...skipping 25 matching lines...) Expand all Loading... |
327 {FILE_PATH_LITERAL("filename.middleext.longext"), | 307 {FILE_PATH_LITERAL("filename.middleext.longext"), |
328 "filename.mid" + kEllipsisStr + ".longext"}, | 308 "filename.mid" + kEllipsisStr + ".longext"}, |
329 {FILE_PATH_LITERAL("filename.superduperextremelylongext"), | 309 {FILE_PATH_LITERAL("filename.superduperextremelylongext"), |
330 "filename.sup" + kEllipsisStr + "emelylongext"}, | 310 "filename.sup" + kEllipsisStr + "emelylongext"}, |
331 {FILE_PATH_LITERAL("filenamereallylongtext.superduperextremelylongext"), | 311 {FILE_PATH_LITERAL("filenamereallylongtext.superduperextremelylongext"), |
332 "filenamereall" + kEllipsisStr + "emelylongext"}, | 312 "filenamereall" + kEllipsisStr + "emelylongext"}, |
333 {FILE_PATH_LITERAL("file.name.really.long.text.superduperextremelylongext"), | 313 {FILE_PATH_LITERAL("file.name.really.long.text.superduperextremelylongext"), |
334 "file.name.re" + kEllipsisStr + "emelylongext"} | 314 "file.name.re" + kEllipsisStr + "emelylongext"} |
335 }; | 315 }; |
336 | 316 |
337 static const gfx::Font font = GetTestingFont(); | 317 static const gfx::Font font; |
338 for (size_t i = 0; i < arraysize(testcases); ++i) { | 318 for (size_t i = 0; i < arraysize(testcases); ++i) { |
339 base::FilePath filepath(testcases[i].input); | 319 base::FilePath filepath(testcases[i].input); |
340 string16 expected = UTF8ToUTF16(testcases[i].output); | 320 string16 expected = UTF8ToUTF16(testcases[i].output); |
341 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected); | 321 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected); |
342 int available_width = font.GetStringWidth(UTF8ToUTF16(testcases[i].output)); | 322 EXPECT_EQ(expected, ElideFilename(filepath, font, |
343 #if defined(OS_MACOSX) | 323 font.GetStringWidth(UTF8ToUTF16(testcases[i].output)))); |
344 // Give one extra pixel to offset the ceiling width returned by | |
345 // GetStringWidth on Mac. This workaround will no longer be needed once | |
346 // the floating point width is adopted (http://crbug.com/288987). | |
347 available_width += 1; | |
348 #endif | |
349 EXPECT_EQ(expected, ElideFilename(filepath, font, available_width)); | |
350 } | 324 } |
351 } | 325 } |
352 | 326 |
353 TEST(TextEliderTest, ElideTextTruncate) { | 327 TEST(TextEliderTest, ElideTextTruncate) { |
354 const gfx::Font font; | 328 const gfx::Font font; |
355 const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); | 329 const float kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); |
356 struct TestData { | 330 struct TestData { |
357 const char* input; | 331 const char* input; |
358 int width; | 332 float width; |
359 const char* output; | 333 const char* output; |
360 } cases[] = { | 334 } cases[] = { |
361 { "", 0, "" }, | 335 { "", 0, "" }, |
362 { "Test", 0, "" }, | 336 { "Test", 0, "" }, |
363 { "", kTestWidth, "" }, | 337 { "", kTestWidth, "" }, |
364 { "Tes", kTestWidth, "Tes" }, | 338 { "Tes", kTestWidth, "Tes" }, |
365 { "Test", kTestWidth, "Test" }, | 339 { "Test", kTestWidth, "Test" }, |
366 { "Tests", kTestWidth, "Test" }, | 340 { "Tests", kTestWidth, "Test" }, |
367 }; | 341 }; |
368 | 342 |
369 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 343 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
370 string16 result = ElideText(UTF8ToUTF16(cases[i].input), font, | 344 string16 result = ElideText(UTF8ToUTF16(cases[i].input), font, |
371 cases[i].width, TRUNCATE_AT_END); | 345 cases[i].width, TRUNCATE_AT_END); |
372 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); | 346 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); |
373 } | 347 } |
374 } | 348 } |
375 | 349 |
376 TEST(TextEliderTest, ElideTextEllipsis) { | 350 TEST(TextEliderTest, ElideTextEllipsis) { |
377 const gfx::Font font; | 351 const gfx::Font font; |
378 const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); | 352 const float kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); |
379 const char* kEllipsis = "\xE2\x80\xA6"; | 353 const char* kEllipsis = "\xE2\x80\xA6"; |
380 const int kEllipsisWidth = font.GetStringWidth(UTF8ToUTF16(kEllipsis)); | 354 const float kEllipsisWidth = font.GetStringWidth(UTF8ToUTF16(kEllipsis)); |
381 struct TestData { | 355 struct TestData { |
382 const char* input; | 356 const char* input; |
383 int width; | 357 float width; |
384 const char* output; | 358 const char* output; |
385 } cases[] = { | 359 } cases[] = { |
386 { "", 0, "" }, | 360 { "", 0, "" }, |
387 { "Test", 0, "" }, | 361 { "Test", 0, "" }, |
388 { "Test", kEllipsisWidth, kEllipsis }, | 362 { "Test", kEllipsisWidth, kEllipsis }, |
389 { "", kTestWidth, "" }, | 363 { "", kTestWidth, "" }, |
390 { "Tes", kTestWidth, "Tes" }, | 364 { "Tes", kTestWidth, "Tes" }, |
391 { "Test", kTestWidth, "Test" }, | 365 { "Test", kTestWidth, "Test" }, |
392 }; | 366 }; |
393 | 367 |
(...skipping 23 matching lines...) Expand all Loading... |
417 } | 391 } |
418 } | 392 } |
419 | 393 |
420 TEST(TextEliderTest, ElideTextSurrogatePairs) { | 394 TEST(TextEliderTest, ElideTextSurrogatePairs) { |
421 const gfx::Font font; | 395 const gfx::Font font; |
422 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as | 396 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as |
423 // two characters forming a surrogate pair 0x0001D11E. | 397 // two characters forming a surrogate pair 0x0001D11E. |
424 const std::string kSurrogate = "\xF0\x9D\x84\x9E"; | 398 const std::string kSurrogate = "\xF0\x9D\x84\x9E"; |
425 const string16 kTestString = | 399 const string16 kTestString = |
426 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); | 400 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); |
427 const int kTestStringWidth = font.GetStringWidth(kTestString); | 401 const float kTestStringWidth = font.GetStringWidth(kTestString); |
428 const char16 kSurrogateFirstChar = kTestString[0]; | 402 const char16 kSurrogateFirstChar = kTestString[0]; |
429 const char16 kSurrogateSecondChar = kTestString[1]; | 403 const char16 kSurrogateSecondChar = kTestString[1]; |
430 string16 result; | 404 string16 result; |
431 | 405 |
432 // Elide |kTextString| to all possible widths and check that no instance of | 406 // Elide |kTextString| to all possible widths and check that no instance of |
433 // |kSurrogate| was split in two. | 407 // |kSurrogate| was split in two. |
434 for (int width = 0; width <= kTestStringWidth; width++) { | 408 for (float width = 0; width <= kTestStringWidth; width++) { |
435 result = ElideText(kTestString, font, width, TRUNCATE_AT_END); | 409 result = ElideText(kTestString, font, width, TRUNCATE_AT_END); |
436 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 410 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
437 | 411 |
438 result = ElideText(kTestString, font, width, ELIDE_AT_END); | 412 result = ElideText(kTestString, font, width, ELIDE_AT_END); |
439 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 413 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
440 | 414 |
441 result = ElideText(kTestString, font, width, ELIDE_IN_MIDDLE); | 415 result = ElideText(kTestString, font, width, ELIDE_IN_MIDDLE); |
442 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 416 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
443 } | 417 } |
444 } | 418 } |
(...skipping 16 matching lines...) Expand all Loading... |
461 UTF16Testcase testcases_end[] = { | 435 UTF16Testcase testcases_end[] = { |
462 {data_scheme + ten_a, data_scheme + ten_a}, | 436 {data_scheme + ten_a, data_scheme + ten_a}, |
463 {data_scheme + hundred_a, data_scheme + hundred_a}, | 437 {data_scheme + hundred_a, data_scheme + hundred_a}, |
464 {data_scheme + thousand_a, long_string_end}, | 438 {data_scheme + thousand_a, long_string_end}, |
465 {data_scheme + ten_thousand_a, long_string_end}, | 439 {data_scheme + ten_thousand_a, long_string_end}, |
466 {data_scheme + hundred_thousand_a, long_string_end}, | 440 {data_scheme + hundred_thousand_a, long_string_end}, |
467 {data_scheme + million_a, long_string_end}, | 441 {data_scheme + million_a, long_string_end}, |
468 }; | 442 }; |
469 | 443 |
470 const gfx::Font font; | 444 const gfx::Font font; |
471 int ellipsis_width = font.GetStringWidth(kEllipsisStr); | 445 float ellipsis_width = font.GetStringWidth(kEllipsisStr); |
472 for (size_t i = 0; i < arraysize(testcases_end); ++i) { | 446 for (size_t i = 0; i < arraysize(testcases_end); ++i) { |
473 // Compare sizes rather than actual contents because if the test fails, | 447 // Compare sizes rather than actual contents because if the test fails, |
474 // output is rather long. | 448 // output is rather long. |
475 EXPECT_EQ(testcases_end[i].output.size(), | 449 EXPECT_EQ(testcases_end[i].output.size(), |
476 ElideText(testcases_end[i].input, font, | 450 ElideText(testcases_end[i].input, font, |
477 font.GetStringWidth(testcases_end[i].output), | 451 font.GetStringWidth(testcases_end[i].output), |
478 ELIDE_AT_END).size()); | 452 ELIDE_AT_END).size()); |
479 EXPECT_EQ(kEllipsisStr, | 453 EXPECT_EQ(kEllipsisStr, |
480 ElideText(testcases_end[i].input, font, ellipsis_width, | 454 ElideText(testcases_end[i].input, font, ellipsis_width, |
481 ELIDE_AT_END)); | 455 ELIDE_AT_END)); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 string16 output; | 548 string16 output; |
575 EXPECT_EQ(cases[i].result, | 549 EXPECT_EQ(cases[i].result, |
576 ElideString(UTF8ToUTF16(cases[i].input), | 550 ElideString(UTF8ToUTF16(cases[i].input), |
577 cases[i].max_len, &output)); | 551 cases[i].max_len, &output)); |
578 EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); | 552 EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); |
579 } | 553 } |
580 } | 554 } |
581 | 555 |
582 TEST(TextEliderTest, ElideRectangleText) { | 556 TEST(TextEliderTest, ElideRectangleText) { |
583 const gfx::Font font; | 557 const gfx::Font font; |
584 const int line_height = font.GetHeight(); | 558 const float line_height = font.GetHeight(); |
585 const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); | 559 const float test_width = font.GetStringWidth(ASCIIToUTF16("Test")); |
586 | 560 |
587 struct TestData { | 561 struct TestData { |
588 const char* input; | 562 const char* input; |
589 int available_pixel_width; | 563 float available_pixel_width; |
590 int available_pixel_height; | 564 float available_pixel_height; |
591 bool truncated_y; | 565 bool truncated_y; |
592 const char* output; | 566 const char* output; |
593 } cases[] = { | 567 } cases[] = { |
594 { "", 0, 0, false, NULL }, | 568 { "", 0, 0, false, NULL }, |
595 { "", 1, 1, false, NULL }, | 569 { "", 1, 1, false, NULL }, |
596 { "Test", test_width, 0, true, NULL }, | 570 { "Test", test_width, 0, true, NULL }, |
597 { "Test", test_width, 1, false, "Test" }, | 571 { "Test", test_width, 1, false, "Test" }, |
598 { "Test", test_width, line_height, false, "Test" }, | 572 { "Test", test_width, line_height, false, "Test" }, |
599 { "Test Test", test_width, line_height, true, "Test" }, | 573 { "Test Test", test_width, line_height, true, "Test" }, |
600 { "Test Test", test_width, line_height + 1, false, "Test|Test" }, | 574 { "Test Test", test_width, line_height + 1, false, "Test|Test" }, |
(...skipping 30 matching lines...) Expand all Loading... |
631 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); | 605 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); |
632 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; | 606 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; |
633 } else { | 607 } else { |
634 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; | 608 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; |
635 } | 609 } |
636 } | 610 } |
637 } | 611 } |
638 | 612 |
639 TEST(TextEliderTest, ElideRectangleTextPunctuation) { | 613 TEST(TextEliderTest, ElideRectangleTextPunctuation) { |
640 const gfx::Font font; | 614 const gfx::Font font; |
641 const int line_height = font.GetHeight(); | 615 const float line_height = font.GetHeight(); |
642 const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); | 616 const float test_width = font.GetStringWidth(ASCIIToUTF16("Test")); |
643 const int test_t_width = font.GetStringWidth(ASCIIToUTF16("Test T")); | 617 const float test_t_width = font.GetStringWidth(ASCIIToUTF16("Test T")); |
644 | 618 |
645 struct TestData { | 619 struct TestData { |
646 const char* input; | 620 const char* input; |
647 int available_pixel_width; | 621 float available_pixel_width; |
648 int available_pixel_height; | 622 float available_pixel_height; |
649 bool wrap_words; | 623 bool wrap_words; |
650 bool truncated_x; | 624 bool truncated_x; |
651 const char* output; | 625 const char* output; |
652 } cases[] = { | 626 } cases[] = { |
653 { "Test T.", test_t_width, line_height * 2, false, false, "Test|T." }, | 627 { "Test T.", test_t_width, line_height * 2, false, false, "Test|T." }, |
654 { "Test T ?", test_t_width, line_height * 2, false, false, "Test|T ?" }, | 628 { "Test T ?", test_t_width, line_height * 2, false, false, "Test|T ?" }, |
655 { "Test. Test", test_width, line_height * 3, false, true, "Test|Test" }, | 629 { "Test. Test", test_width, line_height * 3, false, true, "Test|Test" }, |
656 { "Test. Test", test_width, line_height * 3, true, false, "Test|.|Test" }, | 630 { "Test. Test", test_width, line_height * 3, true, false, "Test|.|Test" }, |
657 }; | 631 }; |
658 | 632 |
(...skipping 12 matching lines...) Expand all Loading... |
671 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); | 645 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); |
672 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; | 646 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; |
673 } else { | 647 } else { |
674 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; | 648 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; |
675 } | 649 } |
676 } | 650 } |
677 } | 651 } |
678 | 652 |
679 TEST(TextEliderTest, ElideRectangleTextLongWords) { | 653 TEST(TextEliderTest, ElideRectangleTextLongWords) { |
680 const gfx::Font font; | 654 const gfx::Font font; |
681 const int kAvailableHeight = 1000; | 655 const float kAvailableHeight = 1000; |
682 const string16 kElidedTesting = UTF8ToUTF16(std::string("Tes") + kEllipsis); | 656 const string16 kElidedTesting = UTF8ToUTF16(std::string("Tes") + kEllipsis); |
683 const int elided_width = font.GetStringWidth(kElidedTesting); | 657 const float elided_width = font.GetStringWidth(kElidedTesting); |
684 const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); | 658 const float test_width = font.GetStringWidth(ASCIIToUTF16("Test")); |
685 | 659 |
686 struct TestData { | 660 struct TestData { |
687 const char* input; | 661 const char* input; |
688 int available_pixel_width; | 662 float available_pixel_width; |
689 WordWrapBehavior wrap_behavior; | 663 WordWrapBehavior wrap_behavior; |
690 bool truncated_x; | 664 bool truncated_x; |
691 const char* output; | 665 const char* output; |
692 } cases[] = { | 666 } cases[] = { |
693 { "Testing", test_width, IGNORE_LONG_WORDS, false, "Testing" }, | 667 { "Testing", test_width, IGNORE_LONG_WORDS, false, "Testing" }, |
694 { "X Testing", test_width, IGNORE_LONG_WORDS, false, "X|Testing" }, | 668 { "X Testing", test_width, IGNORE_LONG_WORDS, false, "X|Testing" }, |
695 { "Test Testing", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" }, | 669 { "Test Testing", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" }, |
696 { "Test\nTesting", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" }, | 670 { "Test\nTesting", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" }, |
697 { "Test Tests ", test_width, IGNORE_LONG_WORDS, false, "Test|Tests" }, | 671 { "Test Tests ", test_width, IGNORE_LONG_WORDS, false, "Test|Tests" }, |
698 { "Test Tests T", test_width, IGNORE_LONG_WORDS, false, "Test|Tests|T" }, | 672 { "Test Tests T", test_width, IGNORE_LONG_WORDS, false, "Test|Tests|T" }, |
(...skipping 29 matching lines...) Expand all Loading... |
728 kAvailableHeight, | 702 kAvailableHeight, |
729 cases[i].wrap_behavior, | 703 cases[i].wrap_behavior, |
730 &lines)); | 704 &lines)); |
731 std::string expected_output(cases[i].output); | 705 std::string expected_output(cases[i].output); |
732 ReplaceSubstringsAfterOffset(&expected_output, 0, "...", kEllipsis); | 706 ReplaceSubstringsAfterOffset(&expected_output, 0, "...", kEllipsis); |
733 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); | 707 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); |
734 EXPECT_EQ(expected_output, result) << "Case " << i << " failed!"; | 708 EXPECT_EQ(expected_output, result) << "Case " << i << " failed!"; |
735 } | 709 } |
736 } | 710 } |
737 | 711 |
738 // TODO(ios): Complex eliding is off by one for some of those tests on iOS. | |
739 // See crbug.com/154019 | |
740 #if defined(OS_IOS) | |
741 #define MAYBE_ElideRectangleTextCheckLineWidth \ | |
742 DISABLED_ElideRectangleTextCheckLineWidth | |
743 #else | |
744 #define MAYBE_ElideRectangleTextCheckLineWidth ElideRectangleTextCheckLineWidth | |
745 #endif | |
746 | |
747 // This test is to make sure that the width of each wrapped line does not | 712 // This test is to make sure that the width of each wrapped line does not |
748 // exceed the available width. On some platform like Mac, this test used to | 713 // exceed the available width. On some platform like Mac, this test used to |
749 // fail because the truncated integer width is returned for the string | 714 // fail because the truncated integer width is returned for the string |
750 // and the accumulation of the truncated values causes the elide function | 715 // and the accumulation of the truncated values causes the elide function |
751 // to wrap incorrectly. | 716 // to wrap incorrectly. |
752 TEST(TextEliderTest, MAYBE_ElideRectangleTextCheckLineWidth) { | 717 TEST(TextEliderTest, ElideRectangleTextCheckLineWidth) { |
753 gfx::Font font = GetTestingFont(); | 718 gfx::Font font; |
754 const int kAvailableWidth = 235; | 719 #if defined(OS_MACOSX) && !defined(OS_IOS) |
755 const int kAvailableHeight = 1000; | 720 // Use a specific font to expose the line width exceeding problem. |
| 721 font = gfx::Font("LucidaGrande", 12); |
| 722 #endif |
| 723 const float kAvailableWidth = 235; |
| 724 const float kAvailableHeight = 1000; |
756 const char text[] = "that Russian place we used to go to after fencing"; | 725 const char text[] = "that Russian place we used to go to after fencing"; |
757 std::vector<string16> lines; | 726 std::vector<string16> lines; |
758 EXPECT_EQ(0, ElideRectangleText(UTF8ToUTF16(text), | 727 EXPECT_EQ(0, ElideRectangleText(UTF8ToUTF16(text), |
759 font, | 728 font, |
760 kAvailableWidth, | 729 kAvailableWidth, |
761 kAvailableHeight, | 730 kAvailableHeight, |
762 WRAP_LONG_WORDS, | 731 WRAP_LONG_WORDS, |
763 &lines)); | 732 &lines)); |
764 ASSERT_EQ(2u, lines.size()); | 733 ASSERT_EQ(2u, lines.size()); |
765 EXPECT_LE(font.GetStringWidth(lines[0]), kAvailableWidth); | 734 EXPECT_LE(font.GetStringWidth(lines[0]), kAvailableWidth); |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
984 | 953 |
985 // Test adds ... at right spot when there is not enough room to break at a | 954 // Test adds ... at right spot when there is not enough room to break at a |
986 // word boundary. | 955 // word boundary. |
987 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); | 956 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); |
988 | 957 |
989 // Test completely truncates string if break is on initial whitespace. | 958 // Test completely truncates string if break is on initial whitespace. |
990 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); | 959 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); |
991 } | 960 } |
992 | 961 |
993 } // namespace gfx | 962 } // namespace gfx |
OLD | NEW |