diff options
author | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-15 22:09:20 +0000 |
---|---|---|
committer | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-15 22:09:20 +0000 |
commit | da06e3089a9649187c7c93e115376c9b23c96cb7 (patch) | |
tree | 696eac9ae9561add157c724197966dbc74aeedaf /ui/base/text/text_elider_unittest.cc | |
parent | 862deef098a8f161cb9926363bd4786f83fdbdc9 (diff) | |
download | chromium_src-da06e3089a9649187c7c93e115376c9b23c96cb7.zip chromium_src-da06e3089a9649187c7c93e115376c9b23c96cb7.tar.gz chromium_src-da06e3089a9649187c7c93e115376c9b23c96cb7.tar.bz2 |
Fix |ElideText()| to handle UTF16 surrogate pairs correctly.
BUG=107703
TEST=New unit test in text_elider_unittest.cc.
Review URL: http://codereview.chromium.org/8910018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@114705 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/text/text_elider_unittest.cc')
-rw-r--r-- | ui/base/text/text_elider_unittest.cc | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/ui/base/text/text_elider_unittest.cc b/ui/base/text/text_elider_unittest.cc index b716a00..439db7b 100644 --- a/ui/base/text/text_elider_unittest.cc +++ b/ui/base/text/text_elider_unittest.cc @@ -237,6 +237,51 @@ TEST(TextEliderTest, ElideTextTruncate) { } } +// Checks that all occurrences of |first_char| are followed by |second_char| and +// all occurrences of |second_char| are preceded by |first_char| in |text|. +static void CheckSurrogatePairs(const string16& text, + char16 first_char, + char16 second_char) { + size_t index = text.find_first_of(first_char); + while (index != string16::npos) { + EXPECT_LT(index, text.length() - 1); + EXPECT_EQ(second_char, text[index + 1]); + index = text.find_first_of(first_char, index + 1); + } + index = text.find_first_of(second_char); + while (index != string16::npos) { + EXPECT_GT(index, 0U); + EXPECT_EQ(first_char, text[index - 1]); + index = text.find_first_of(second_char, index + 1); + } +} + +TEST(TextEliderTest, ElideTextSurrogatePairs) { + const gfx::Font font; + // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as + // two characters forming a surrogate pair 0x0001D11E. + const std::string kSurrogate = "\xF0\x9D\x84\x9E"; + const string16 kTestString = + UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); + const int kTestStringWidth = font.GetStringWidth(kTestString); + const char16 kSurrogateFirstChar = kTestString[0]; + const char16 kSurrogateSecondChar = kTestString[1]; + string16 result; + + // Elide |kTextString| to all possible widths and check that no instance of + // |kSurrogate| was split in two. + for (int width = 0; width <= kTestStringWidth; width++) { + result = ui::ElideText(kTestString, font, width, ui::TRUNCATE_AT_END); + CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); + + result = ui::ElideText(kTestString, font, width, ui::ELIDE_AT_END); + CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); + + result = ui::ElideText(kTestString, font, width, ui::ELIDE_IN_MIDDLE); + CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); + } +} + TEST(TextEliderTest, ElideTextLongStrings) { const string16 kEllipsisStr = UTF8ToUTF16(kEllipsis); string16 data_scheme(UTF8ToUTF16("data:text/plain,")); |