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 <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 1052 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1063 EXPECT_TRUE(ElideRectangleString(str, 3, 3, true, &output)); | 1063 EXPECT_TRUE(ElideRectangleString(str, 3, 3, true, &output)); |
1064 EXPECT_EQ(out, output); | 1064 EXPECT_EQ(out, output); |
1065 } | 1065 } |
1066 | 1066 |
1067 // TODO(338784): Enable this on android. | 1067 // TODO(338784): Enable this on android. |
1068 #if defined(OS_ANDROID) | 1068 #if defined(OS_ANDROID) |
1069 #define MAYBE_TruncateString DISABLED_TruncateString | 1069 #define MAYBE_TruncateString DISABLED_TruncateString |
1070 #else | 1070 #else |
1071 #define MAYBE_TruncateString TruncateString | 1071 #define MAYBE_TruncateString TruncateString |
1072 #endif | 1072 #endif |
1073 TEST(TextEliderTest, MAYBE_TruncateString) { | 1073 TEST(TextEliderTest, MAYBE_TruncateString) { |
msw
2015/09/17 18:42:44
Adding a test with surrogate pairs, etc. would be
Peter Kasting
2015/09/19 02:43:25
Since the code can get those wrong today, let me d
msw
2015/09/21 18:28:10
Acknowledged.
| |
1074 base::string16 string = ASCIIToUTF16("foooooey bxxxar baz"); | 1074 base::string16 str = ASCIIToUTF16("fooooey bxxxar baz "); |
1075 | 1075 |
1076 // Tests that apply to both break behaviors: | 1076 // Test breaking at character 0. |
1077 EXPECT_EQ(base::string16(), TruncateString(str, 0, WORD_BREAK)); | |
1078 EXPECT_EQ(base::string16(), TruncateString(str, 0, CHARACTER_BREAK)); | |
1077 | 1079 |
1078 // Make sure it doesn't modify the string if length > string length. | 1080 // Test breaking at character 1. |
1079 EXPECT_EQ(string, TruncateString(string, 100, WORD_BREAK)); | 1081 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str, 1, WORD_BREAK))); |
msw
2015/09/17 18:42:44
nit: maybe EXPECT_STREQ would make UTF16ToWide unn
Peter Kasting
2015/09/19 02:43:25
Nah, that just means you need to supply two pointe
| |
1080 EXPECT_EQ(string, TruncateString(string, 100, CHARACTER_BREAK)); | 1082 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str, 1, CHARACTER_BREAK))); |
1081 | 1083 |
1082 // Test no characters. | 1084 // Test breaking in the middle of the first word. |
1083 EXPECT_EQ(L"", UTF16ToWide(TruncateString(string, 0, WORD_BREAK))); | 1085 EXPECT_EQ(L"f\x2026", UTF16ToWide(TruncateString(str, 2, WORD_BREAK))); |
1084 EXPECT_EQ(L"", UTF16ToWide(TruncateString(string, 0, CHARACTER_BREAK))); | 1086 EXPECT_EQ(L"f\x2026", UTF16ToWide(TruncateString(str, 2, CHARACTER_BREAK))); |
1085 | 1087 |
1086 // Test 1 character. | 1088 // Test breaking in between words. |
1087 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(string, 1, WORD_BREAK))); | 1089 EXPECT_EQ(L"fooooey\x2026", UTF16ToWide(TruncateString(str, 9, WORD_BREAK))); |
1088 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(string, 1, CHARACTER_BREAK))); | 1090 EXPECT_EQ(L"fooooey\x2026", |
1091 UTF16ToWide(TruncateString(str, 9, CHARACTER_BREAK))); | |
1089 | 1092 |
1090 // Test completely truncates string if break is on initial whitespace. | 1093 // Test breaking at the start of a later word. |
msw
2015/09/17 18:42:44
It's probably worthwhile to test index 12 too (but
Peter Kasting
2015/09/19 02:43:25
Done. Expanded the tests below by a case as well.
| |
1091 EXPECT_EQ(L"\x2026", | 1094 EXPECT_EQ(L"fooooey\x2026", UTF16ToWide(TruncateString(str, 11, WORD_BREAK))); |
1092 UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2, WORD_BREAK))); | 1095 EXPECT_EQ(L"fooooey\x2026", |
1093 EXPECT_EQ(L"\x2026", | 1096 UTF16ToWide(TruncateString(str, 11, CHARACTER_BREAK))); |
1094 UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2, | |
1095 CHARACTER_BREAK))); | |
1096 | 1097 |
1097 // Break-only-at-word-boundaries tests: | 1098 // Test breaking in the middle of a word. |
1099 EXPECT_EQ(L"fooooey\x2026", UTF16ToWide(TruncateString(str, 14, WORD_BREAK))); | |
1100 EXPECT_EQ(L"fooooey bx\x2026", | |
1101 UTF16ToWide(TruncateString(str, 14, CHARACTER_BREAK))); | |
1098 | 1102 |
1099 // Test adds ... at right spot when there is enough room to break at a | 1103 // Test breaking in whitespace at the end of the string. |
1100 // word boundary. | 1104 EXPECT_EQ(L"fooooey bxxxar baz\x2026", |
1101 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 14, | 1105 UTF16ToWide(TruncateString(str, 22, WORD_BREAK))); |
1102 WORD_BREAK))); | 1106 EXPECT_EQ(L"fooooey bxxxar baz\x2026", |
1107 UTF16ToWide(TruncateString(str, 22, CHARACTER_BREAK))); | |
1103 | 1108 |
1104 // Test adds ... at right spot when there is not enough space in first word. | 1109 // Test breaking at the end of the string. |
1105 EXPECT_EQ(L"f\x2026", UTF16ToWide(TruncateString(string, 2, WORD_BREAK))); | 1110 EXPECT_EQ(str, TruncateString(str, str.length(), WORD_BREAK)); |
1111 EXPECT_EQ(str, TruncateString(str, str.length(), CHARACTER_BREAK)); | |
1106 | 1112 |
1107 // Test adds ... at right spot when there is not enough room to break at a | 1113 // Test breaking past the end of the string. |
1108 // word boundary. | 1114 EXPECT_EQ(str, TruncateString(str, str.length() + 10, WORD_BREAK)); |
1109 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11, | 1115 EXPECT_EQ(str, TruncateString(str, str.length() + 10, CHARACTER_BREAK)); |
1110 WORD_BREAK))); | |
1111 | 1116 |
1112 // Break-anywhere tests: | |
1113 | 1117 |
1114 // Test adds ... at right spot within a word. | 1118 // Tests of strings with leading whitespace: |
1115 EXPECT_EQ(L"f\x2026", UTF16ToWide(TruncateString(string, 2, | 1119 base::string16 str2 = ASCIIToUTF16(" foo"); |
1116 CHARACTER_BREAK))); | |
1117 | 1120 |
1118 // Test removes trailing whitespace if break falls between words. | 1121 // Test breaking in leading whitespace. |
1119 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 12, | 1122 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 2, WORD_BREAK))); |
1120 CHARACTER_BREAK))); | 1123 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 2, CHARACTER_BREAK))); |
1124 | |
1125 // Test breaking at the beginning of the first word, with leading whitespace. | |
1126 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 4, WORD_BREAK))); | |
1127 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 4, CHARACTER_BREAK))); | |
1128 | |
1129 // Test breaking in the middle of the first word, with leading whitespace. | |
1130 EXPECT_EQ(L" f\x2026", UTF16ToWide(TruncateString(str2, 5, WORD_BREAK))); | |
1131 EXPECT_EQ(L" f\x2026", | |
1132 UTF16ToWide(TruncateString(str2, 5, CHARACTER_BREAK))); | |
1121 } | 1133 } |
1122 | 1134 |
1123 } // namespace gfx | 1135 } // namespace gfx |
OLD | NEW |