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 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 cases[i].width, ELIDE_HEAD); | 295 cases[i].width, ELIDE_HEAD); |
296 EXPECT_EQ(cases[i].output, result); | 296 EXPECT_EQ(cases[i].output, result); |
297 } | 297 } |
298 } | 298 } |
299 | 299 |
300 // Checks that all occurrences of |first_char| are followed by |second_char| and | 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|. | 301 // all occurrences of |second_char| are preceded by |first_char| in |text|. |
302 static void CheckSurrogatePairs(const base::string16& text, | 302 static void CheckSurrogatePairs(const base::string16& text, |
303 base::char16 first_char, | 303 base::char16 first_char, |
304 base::char16 second_char) { | 304 base::char16 second_char) { |
305 size_t index = text.find_first_of(first_char); | 305 for (size_t index = 0; index < text.length(); ++index) { |
306 while (index != base::string16::npos) { | 306 EXPECT_NE(second_char, text[index]); |
307 EXPECT_LT(index, text.length() - 1); | 307 if (text[index] == first_char) { |
308 EXPECT_EQ(second_char, text[index + 1]); | 308 ASSERT_LT(++index, text.length()); |
309 index = text.find_first_of(first_char, index + 1); | 309 EXPECT_EQ(second_char, text[index]); |
310 } | 310 } |
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); | |
316 } | 311 } |
317 } | 312 } |
318 | 313 |
319 // TODO(338784): Enable this on android. | 314 // TODO(338784): Enable this on android. |
320 #if defined(OS_ANDROID) | 315 #if defined(OS_ANDROID) |
321 #define MAYBE_ElideTextSurrogatePairs DISABLED_ElideTextSurrogatePairs | 316 #define MAYBE_ElideTextSurrogatePairs DISABLED_ElideTextSurrogatePairs |
322 #else | 317 #else |
323 #define MAYBE_ElideTextSurrogatePairs ElideTextSurrogatePairs | 318 #define MAYBE_ElideTextSurrogatePairs ElideTextSurrogatePairs |
324 #endif | 319 #endif |
325 TEST(TextEliderTest, MAYBE_ElideTextSurrogatePairs) { | 320 TEST(TextEliderTest, MAYBE_ElideTextSurrogatePairs) { |
326 const FontList font_list; | 321 const FontList font_list; |
327 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as | 322 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as |
328 // two characters forming a surrogate pair 0x0001D11E. | 323 // two characters forming a surrogate pair 0x0001D11E. |
329 const std::string kSurrogate = "\xF0\x9D\x84\x9E"; | 324 const std::string kSurrogate = "\xF0\x9D\x84\x9E"; |
330 const base::string16 kTestString = | 325 const base::string16 kTestString = UTF8ToUTF16(kSurrogate + "x" + kSurrogate); |
331 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); | |
332 const float kTestStringWidth = GetStringWidthF(kTestString, font_list); | 326 const float kTestStringWidth = GetStringWidthF(kTestString, font_list); |
333 const base::char16 kSurrogateFirstChar = kTestString[0]; | 327 const base::char16 kSurrogateFirstChar = kTestString[0]; |
334 const base::char16 kSurrogateSecondChar = kTestString[1]; | 328 const base::char16 kSurrogateSecondChar = kTestString[1]; |
335 base::string16 result; | 329 base::string16 result; |
336 | 330 |
337 // Elide |kTextString| to all possible widths and check that no instance of | 331 // Elide |kTextString| to all possible widths and check that no instance of |
338 // |kSurrogate| was split in two. | 332 // |kSurrogate| was split in two. |
339 for (float width = 0; width <= kTestStringWidth; width++) { | 333 for (float width = 0; width <= kTestStringWidth; width++) { |
340 result = ElideText(kTestString, font_list, width, TRUNCATE); | 334 result = ElideText(kTestString, font_list, width, TRUNCATE); |
341 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 335 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
(...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
900 | 894 |
901 // Test adds ... at right spot when there is not enough room to break at a | 895 // Test adds ... at right spot when there is not enough room to break at a |
902 // word boundary. | 896 // word boundary. |
903 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); | 897 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); |
904 | 898 |
905 // Test completely truncates string if break is on initial whitespace. | 899 // Test completely truncates string if break is on initial whitespace. |
906 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); | 900 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); |
907 } | 901 } |
908 | 902 |
909 } // namespace gfx | 903 } // namespace gfx |
OLD | NEW |