Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: ui/gfx/text_elider_unittest.cc

Issue 1340423006: Improve text elision in TruncateString(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« ui/gfx/text_elider.cc ('K') | « ui/gfx/text_elider.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 1053 matching lines...) Expand 10 before | Expand all | Expand 10 after
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) {
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)));
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.
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, 12, WORD_BREAK)));
1100 EXPECT_EQ(L"fooooey\x2026",
1101 UTF16ToWide(TruncateString(str, 12, CHARACTER_BREAK)));
msw 2015/09/21 18:28:10 optional nit: test index 13 too for completeness h
Peter Kasting 2015/09/22 23:10:55 The (str2, 5) case below checks this particular ty
1102 EXPECT_EQ(L"fooooey\x2026", UTF16ToWide(TruncateString(str, 14, WORD_BREAK)));
1103 EXPECT_EQ(L"fooooey bx\x2026",
1104 UTF16ToWide(TruncateString(str, 14, CHARACTER_BREAK)));
1098 1105
1099 // Test adds ... at right spot when there is enough room to break at a 1106 // Test breaking in whitespace at the end of the string.
1100 // word boundary. 1107 EXPECT_EQ(L"fooooey bxxxar baz\x2026",
1101 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 14, 1108 UTF16ToWide(TruncateString(str, 22, WORD_BREAK)));
1102 WORD_BREAK))); 1109 EXPECT_EQ(L"fooooey bxxxar baz\x2026",
1110 UTF16ToWide(TruncateString(str, 22, CHARACTER_BREAK)));
1103 1111
1104 // Test adds ... at right spot when there is not enough space in first word. 1112 // Test breaking at the end of the string.
1105 EXPECT_EQ(L"f\x2026", UTF16ToWide(TruncateString(string, 2, WORD_BREAK))); 1113 EXPECT_EQ(str, TruncateString(str, str.length(), WORD_BREAK));
1114 EXPECT_EQ(str, TruncateString(str, str.length(), CHARACTER_BREAK));
1106 1115
1107 // Test adds ... at right spot when there is not enough room to break at a 1116 // Test breaking past the end of the string.
1108 // word boundary. 1117 EXPECT_EQ(str, TruncateString(str, str.length() + 10, WORD_BREAK));
1109 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11, 1118 EXPECT_EQ(str, TruncateString(str, str.length() + 10, CHARACTER_BREAK));
1110 WORD_BREAK)));
1111 1119
1112 // Break-anywhere tests:
1113 1120
1114 // Test adds ... at right spot within a word. 1121 // Tests of strings with leading whitespace:
1115 EXPECT_EQ(L"f\x2026", UTF16ToWide(TruncateString(string, 2, 1122 base::string16 str2 = ASCIIToUTF16(" foo");
1116 CHARACTER_BREAK)));
1117 1123
1118 // Test removes trailing whitespace if break falls between words. 1124 // Test breaking in leading whitespace.
1119 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 12, 1125 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 2, WORD_BREAK)));
1120 CHARACTER_BREAK))); 1126 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 2, CHARACTER_BREAK)));
1127
1128 // Test breaking at the beginning of the first word, with leading whitespace.
1129 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 3, WORD_BREAK)));
1130 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 3, CHARACTER_BREAK)));
1131
1132 // Test breaking in the middle of the first word, with leading whitespace.
1133 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 4, WORD_BREAK)));
1134 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 4, CHARACTER_BREAK)));
1135 EXPECT_EQ(L" f\x2026", UTF16ToWide(TruncateString(str2, 5, WORD_BREAK)));
1136 EXPECT_EQ(L" f\x2026",
1137 UTF16ToWide(TruncateString(str2, 5, CHARACTER_BREAK)));
1121 } 1138 }
1122 1139
1123 } // namespace gfx 1140 } // namespace gfx
OLDNEW
« ui/gfx/text_elider.cc ('K') | « ui/gfx/text_elider.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698