| 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 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 372 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 373 base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, | 373 base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, |
| 374 cases[i].width, ELIDE_AT_END); | 374 cases[i].width, ELIDE_AT_END); |
| 375 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); | 375 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); |
| 376 } | 376 } |
| 377 } | 377 } |
| 378 | 378 |
| 379 // Checks that all occurrences of |first_char| are followed by |second_char| and | 379 // Checks that all occurrences of |first_char| are followed by |second_char| and |
| 380 // all occurrences of |second_char| are preceded by |first_char| in |text|. | 380 // all occurrences of |second_char| are preceded by |first_char| in |text|. |
| 381 static void CheckSurrogatePairs(const base::string16& text, | 381 static void CheckSurrogatePairs(const base::string16& text, |
| 382 char16 first_char, | 382 base::char16 first_char, |
| 383 char16 second_char) { | 383 base::char16 second_char) { |
| 384 size_t index = text.find_first_of(first_char); | 384 size_t index = text.find_first_of(first_char); |
| 385 while (index != base::string16::npos) { | 385 while (index != base::string16::npos) { |
| 386 EXPECT_LT(index, text.length() - 1); | 386 EXPECT_LT(index, text.length() - 1); |
| 387 EXPECT_EQ(second_char, text[index + 1]); | 387 EXPECT_EQ(second_char, text[index + 1]); |
| 388 index = text.find_first_of(first_char, index + 1); | 388 index = text.find_first_of(first_char, index + 1); |
| 389 } | 389 } |
| 390 index = text.find_first_of(second_char); | 390 index = text.find_first_of(second_char); |
| 391 while (index != base::string16::npos) { | 391 while (index != base::string16::npos) { |
| 392 EXPECT_GT(index, 0U); | 392 EXPECT_GT(index, 0U); |
| 393 EXPECT_EQ(first_char, text[index - 1]); | 393 EXPECT_EQ(first_char, text[index - 1]); |
| 394 index = text.find_first_of(second_char, index + 1); | 394 index = text.find_first_of(second_char, index + 1); |
| 395 } | 395 } |
| 396 } | 396 } |
| 397 | 397 |
| 398 TEST(TextEliderTest, ElideTextSurrogatePairs) { | 398 TEST(TextEliderTest, ElideTextSurrogatePairs) { |
| 399 const FontList font_list; | 399 const FontList font_list; |
| 400 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as | 400 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as |
| 401 // two characters forming a surrogate pair 0x0001D11E. | 401 // two characters forming a surrogate pair 0x0001D11E. |
| 402 const std::string kSurrogate = "\xF0\x9D\x84\x9E"; | 402 const std::string kSurrogate = "\xF0\x9D\x84\x9E"; |
| 403 const base::string16 kTestString = | 403 const base::string16 kTestString = |
| 404 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); | 404 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); |
| 405 const float kTestStringWidth = GetStringWidthF(kTestString, font_list); | 405 const float kTestStringWidth = GetStringWidthF(kTestString, font_list); |
| 406 const char16 kSurrogateFirstChar = kTestString[0]; | 406 const base::char16 kSurrogateFirstChar = kTestString[0]; |
| 407 const char16 kSurrogateSecondChar = kTestString[1]; | 407 const base::char16 kSurrogateSecondChar = kTestString[1]; |
| 408 base::string16 result; | 408 base::string16 result; |
| 409 | 409 |
| 410 // Elide |kTextString| to all possible widths and check that no instance of | 410 // Elide |kTextString| to all possible widths and check that no instance of |
| 411 // |kSurrogate| was split in two. | 411 // |kSurrogate| was split in two. |
| 412 for (float width = 0; width <= kTestStringWidth; width++) { | 412 for (float width = 0; width <= kTestStringWidth; width++) { |
| 413 result = ElideText(kTestString, font_list, width, TRUNCATE_AT_END); | 413 result = ElideText(kTestString, font_list, width, TRUNCATE_AT_END); |
| 414 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 414 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
| 415 | 415 |
| 416 result = ElideText(kTestString, font_list, width, ELIDE_AT_END); | 416 result = ElideText(kTestString, font_list, width, ELIDE_AT_END); |
| 417 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 417 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
| (...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 962 | 962 |
| 963 // Test adds ... at right spot when there is not enough room to break at a | 963 // Test adds ... at right spot when there is not enough room to break at a |
| 964 // word boundary. | 964 // word boundary. |
| 965 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); | 965 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); |
| 966 | 966 |
| 967 // Test completely truncates string if break is on initial whitespace. | 967 // Test completely truncates string if break is on initial whitespace. |
| 968 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); | 968 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); |
| 969 } | 969 } |
| 970 | 970 |
| 971 } // namespace gfx | 971 } // namespace gfx |
| OLD | NEW |