| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Unit tests for eliding and formatting utility functions. | 5 // Unit tests for eliding and formatting utility functions. |
| 6 | 6 |
| 7 #include "ui/base/text/text_elider.h" | 7 #include "ui/base/text/text_elider.h" |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 { "Tests", kTestWidth, "Test" }, | 338 { "Tests", kTestWidth, "Test" }, |
| 339 }; | 339 }; |
| 340 | 340 |
| 341 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 341 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 342 string16 result = ElideText(UTF8ToUTF16(cases[i].input), font, | 342 string16 result = ElideText(UTF8ToUTF16(cases[i].input), font, |
| 343 cases[i].width, TRUNCATE_AT_END); | 343 cases[i].width, TRUNCATE_AT_END); |
| 344 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); | 344 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); |
| 345 } | 345 } |
| 346 } | 346 } |
| 347 | 347 |
| 348 TEST(TextEliderTest, ElideTextEllipsis) { |
| 349 const gfx::Font font; |
| 350 const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); |
| 351 const char* kEllipsis = "\xE2\x80\xA6"; |
| 352 const int kEllipsisWidth = font.GetStringWidth(UTF8ToUTF16(kEllipsis)); |
| 353 struct TestData { |
| 354 const char* input; |
| 355 int width; |
| 356 const char* output; |
| 357 } cases[] = { |
| 358 { "", 0, "" }, |
| 359 { "Test", 0, "" }, |
| 360 { "Test", kEllipsisWidth, kEllipsis }, |
| 361 { "", kTestWidth, "" }, |
| 362 { "Tes", kTestWidth, "Tes" }, |
| 363 { "Test", kTestWidth, "Test" }, |
| 364 }; |
| 365 |
| 366 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 367 string16 result = ElideText(UTF8ToUTF16(cases[i].input), font, |
| 368 cases[i].width, ELIDE_AT_END); |
| 369 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); |
| 370 } |
| 371 } |
| 372 |
| 348 // Checks that all occurrences of |first_char| are followed by |second_char| and | 373 // Checks that all occurrences of |first_char| are followed by |second_char| and |
| 349 // all occurrences of |second_char| are preceded by |first_char| in |text|. | 374 // all occurrences of |second_char| are preceded by |first_char| in |text|. |
| 350 static void CheckSurrogatePairs(const string16& text, | 375 static void CheckSurrogatePairs(const string16& text, |
| 351 char16 first_char, | 376 char16 first_char, |
| 352 char16 second_char) { | 377 char16 second_char) { |
| 353 size_t index = text.find_first_of(first_char); | 378 size_t index = text.find_first_of(first_char); |
| 354 while (index != string16::npos) { | 379 while (index != string16::npos) { |
| 355 EXPECT_LT(index, text.length() - 1); | 380 EXPECT_LT(index, text.length() - 1); |
| 356 EXPECT_EQ(second_char, text[index + 1]); | 381 EXPECT_EQ(second_char, text[index + 1]); |
| 357 index = text.find_first_of(first_char, index + 1); | 382 index = text.find_first_of(first_char, index + 1); |
| (...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 900 | 925 |
| 901 // Test adds ... at right spot when there is not enough room to break at a | 926 // Test adds ... at right spot when there is not enough room to break at a |
| 902 // word boundary. | 927 // word boundary. |
| 903 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); | 928 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); |
| 904 | 929 |
| 905 // Test completely truncates string if break is on initial whitespace. | 930 // Test completely truncates string if break is on initial whitespace. |
| 906 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); | 931 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); |
| 907 } | 932 } |
| 908 | 933 |
| 909 } // namespace ui | 934 } // namespace ui |
| OLD | NEW |