blob: 4490110ccc3d9a1d908919fcb325625e26e22e64 [file] [log] [blame]
[email protected]dbb97ba2013-09-09 22:15:251// 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
3// found in the LICENSE file.
4//
5// Unit tests for eliding and formatting utility functions.
6
7#include "ui/gfx/text_elider.h"
8
avic89eb8d42015-12-23 08:08:189#include <stddef.h>
10
mgiuca034e9302015-04-22 02:44:5311#include <vector>
12
[email protected]dbb97ba2013-09-09 22:15:2513#include "base/files/file_path.h"
14#include "base/i18n/rtl.h"
avic89eb8d42015-12-23 08:08:1815#include "base/macros.h"
[email protected]dbb97ba2013-09-09 22:15:2516#include "base/memory/scoped_ptr.h"
17#include "base/strings/string_util.h"
18#include "base/strings/utf_string_conversions.h"
avic89eb8d42015-12-23 08:08:1819#include "build/build_config.h"
[email protected]dbb97ba2013-09-09 22:15:2520#include "testing/gtest/include/gtest/gtest.h"
21#include "ui/gfx/font.h"
[email protected]8ad3c5a2013-10-10 09:57:1922#include "ui/gfx/font_list.h"
rsorokin370fe4b2014-11-11 15:14:0723#include "ui/gfx/font_render_params.h"
[email protected]8ad3c5a2013-10-10 09:57:1924#include "ui/gfx/text_utils.h"
[email protected]dbb97ba2013-09-09 22:15:2525
[email protected]dd2cc802013-12-25 20:09:3626using base::ASCIIToUTF16;
27using base::UTF16ToUTF8;
[email protected]f729d7a2013-12-26 07:07:5628using base::UTF16ToWide;
[email protected]dd2cc802013-12-25 20:09:3629using base::UTF8ToUTF16;
30using base::WideToUTF16;
31
[email protected]dbb97ba2013-09-09 22:15:2532namespace gfx {
33
34namespace {
35
36struct Testcase {
37 const std::string input;
38 const std::string output;
39};
40
41struct FileTestcase {
42 const base::FilePath::StringType input;
43 const std::string output;
44};
45
46struct UTF16Testcase {
[email protected]2aadf212013-12-18 20:03:4447 const base::string16 input;
48 const base::string16 output;
[email protected]dbb97ba2013-09-09 22:15:2549};
50
51struct TestData {
52 const std::string a;
53 const std::string b;
54 const int compare_result;
55};
56
[email protected]dbb97ba2013-09-09 22:15:2557} // namespace
58
olivierrobin697bbda2015-12-07 14:53:4859// TODO(crbug.com/546240): This test fails on iOS because iOS version of
60// GetStringWidthF that calls [NSString sizeWithFont] returns the rounded string
61// width.
62// TODO(crbug.com/338784): Enable this on android.
[email protected]ebc9b662014-01-30 03:37:3363#if defined(OS_IOS) || defined(OS_ANDROID)
[email protected]dbb97ba2013-09-09 22:15:2564#define MAYBE_ElideEmail DISABLED_ElideEmail
65#else
66#define MAYBE_ElideEmail ElideEmail
67#endif
68TEST(TextEliderTest, MAYBE_ElideEmail) {
69 const std::string kEllipsisStr(kEllipsis);
70
71 // Test emails and their expected elided forms (from which the available
72 // widths will be derived).
73 // For elided forms in which both the username and domain must be elided:
74 // the result (how many characters are left on each side) can be font
75 // dependent. To avoid this, the username is prefixed with the characters
76 // expected to remain in the domain.
77 Testcase testcases[] = {
78 {"[email protected]", "[email protected]"},
79 {"[email protected]", kEllipsisStr},
80 {"[email protected]", "ga@c" + kEllipsisStr + "a"},
81 {"[email protected]", "s" + kEllipsisStr + "@s" + kEllipsisStr},
82 {"[email protected]", "s" + kEllipsisStr + "@small.com"},
83 {"[email protected]", "[email protected]"},
84 {"[email protected]",
85 "short@long" + kEllipsisStr + ".com"},
86 {"[email protected]",
87 "la" + kEllipsisStr + "@l" + kEllipsisStr + "a"},
88 {"[email protected]", "long" + kEllipsisStr + "@gmail.com"},
89 {"[email protected]", "e" + kEllipsisStr + "@justfits.com"},
90 {"[email protected]",
91 "thatom" + kEllipsisStr + "@tha" + kEllipsisStr + "om"},
92 {"[email protected]",
93 "namefits@butthedo" + kEllipsisStr + "snt.com"},
94 {"[email protected]", kEllipsisStr},
95 {"nospaceforusername@l", kEllipsisStr},
96 {"[email protected]", "l" + kEllipsisStr + "@l" + kEllipsisStr},
97 {"[email protected]", "l@lllll" + kEllipsisStr + ".com"},
98 {"messed\"up@whyanat\"[email protected]",
99 "messed\"up@whyanat\"[email protected]"},
100 {"messed\"up@whyanat\"[email protected]",
101 "messed\"up@why" + kEllipsisStr + "@notgoogley.com"},
102 {"noca_messed\"up@whyanat\"[email protected]",
103 "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"},
104 {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com",
105 "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"},
106 // Special case: "m..." takes more than half of the available width; thus
107 // the domain must elide to "l..." and not "l...l" as it must allow enough
108 // space for the minimal username elision although its half of the
109 // available width would normally allow it to elide to "l...l".
110 {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr},
111 };
112
[email protected]8ad3c5a2013-10-10 09:57:19113 const FontList font_list;
[email protected]dbb97ba2013-09-09 22:15:25114 for (size_t i = 0; i < arraysize(testcases); ++i) {
[email protected]2aadf212013-12-18 20:03:44115 const base::string16 expected_output = UTF8ToUTF16(testcases[i].output);
[email protected]dbb97ba2013-09-09 22:15:25116 EXPECT_EQ(expected_output,
[email protected]f3ce6212014-06-05 22:42:08117 ElideText(UTF8ToUTF16(testcases[i].input), font_list,
118 GetStringWidthF(expected_output, font_list),
119 ELIDE_EMAIL));
[email protected]dbb97ba2013-09-09 22:15:25120 }
121}
122
olivierrobin697bbda2015-12-07 14:53:48123// TODO(crbug.com/338784): Enable this on android.
[email protected]ebc9b662014-01-30 03:37:33124#if defined(OS_ANDROID)
125#define MAYBE_ElideEmailMoreSpace DISABLED_ElideEmailMoreSpace
126#else
127#define MAYBE_ElideEmailMoreSpace ElideEmailMoreSpace
128#endif
129TEST(TextEliderTest, MAYBE_ElideEmailMoreSpace) {
[email protected]dbb97ba2013-09-09 22:15:25130 const int test_width_factors[] = {
131 100,
132 10000,
133 1000000,
134 };
135 const std::string test_emails[] = {
136 "a@c",
137 "[email protected]",
138 "[email protected]",
139 "[email protected]",
140 };
141
[email protected]2859ff352013-11-08 15:58:49142 const FontList font_list;
[email protected]dbb97ba2013-09-09 22:15:25143 for (size_t i = 0; i < arraysize(test_width_factors); ++i) {
[email protected]2859ff352013-11-08 15:58:49144 const int test_width =
145 font_list.GetExpectedTextWidth(test_width_factors[i]);
[email protected]dbb97ba2013-09-09 22:15:25146 for (size_t j = 0; j < arraysize(test_emails); ++j) {
147 // Extra space is available: the email should not be elided.
[email protected]2aadf212013-12-18 20:03:44148 const base::string16 test_email = UTF8ToUTF16(test_emails[j]);
[email protected]f3ce6212014-06-05 22:42:08149 EXPECT_EQ(test_email,
150 ElideText(test_email, font_list, test_width, ELIDE_EMAIL));
[email protected]dbb97ba2013-09-09 22:15:25151 }
152 }
153}
154
olivierrobin697bbda2015-12-07 14:53:48155// TODO(crbug.com/546240): This test fails on iOS because iOS version of
156// GetStringWidthF that calls [NSString sizeWithFont] returns the rounded string
157// width.
158// TODO(crbug.com/338784): Enable this on android.
[email protected]ebc9b662014-01-30 03:37:33159#if defined(OS_IOS) || defined(OS_ANDROID)
[email protected]dbb97ba2013-09-09 22:15:25160#define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding
161#else
162#define MAYBE_TestFilenameEliding TestFilenameEliding
163#endif
164TEST(TextEliderTest, MAYBE_TestFilenameEliding) {
165 const std::string kEllipsisStr(kEllipsis);
166 const base::FilePath::StringType kPathSeparator =
167 base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]);
168
169 FileTestcase testcases[] = {
170 {FILE_PATH_LITERAL(""), ""},
171 {FILE_PATH_LITERAL("."), "."},
172 {FILE_PATH_LITERAL("filename.exe"), "filename.exe"},
173 {FILE_PATH_LITERAL(".longext"), ".longext"},
174 {FILE_PATH_LITERAL("pie"), "pie"},
175 {FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") +
176 kPathSeparator + FILE_PATH_LITERAL("filename.pie"),
177 "filename.pie"},
178 {FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") +
179 kPathSeparator + FILE_PATH_LITERAL("longfilename.pie"),
180 "long" + kEllipsisStr + ".pie"},
181 {FILE_PATH_LITERAL("http://path.com/filename.pie"), "filename.pie"},
182 {FILE_PATH_LITERAL("http://path.com/longfilename.pie"),
183 "long" + kEllipsisStr + ".pie"},
184 {FILE_PATH_LITERAL("piesmashingtacularpants"), "pie" + kEllipsisStr},
185 {FILE_PATH_LITERAL(".piesmashingtacularpants"), ".pie" + kEllipsisStr},
186 {FILE_PATH_LITERAL("cheese."), "cheese."},
187 {FILE_PATH_LITERAL("file name.longext"),
188 "file" + kEllipsisStr + ".longext"},
189 {FILE_PATH_LITERAL("fil ename.longext"),
190 "fil " + kEllipsisStr + ".longext"},
191 {FILE_PATH_LITERAL("filename.longext"),
192 "file" + kEllipsisStr + ".longext"},
193 {FILE_PATH_LITERAL("filename.middleext.longext"),
194 "filename.mid" + kEllipsisStr + ".longext"},
195 {FILE_PATH_LITERAL("filename.superduperextremelylongext"),
196 "filename.sup" + kEllipsisStr + "emelylongext"},
197 {FILE_PATH_LITERAL("filenamereallylongtext.superduperextremelylongext"),
198 "filenamereall" + kEllipsisStr + "emelylongext"},
199 {FILE_PATH_LITERAL("file.name.really.long.text.superduperextremelylongext"),
200 "file.name.re" + kEllipsisStr + "emelylongext"}
201 };
202
[email protected]8ad3c5a2013-10-10 09:57:19203 static const FontList font_list;
[email protected]dbb97ba2013-09-09 22:15:25204 for (size_t i = 0; i < arraysize(testcases); ++i) {
205 base::FilePath filepath(testcases[i].input);
[email protected]2aadf212013-12-18 20:03:44206 base::string16 expected = UTF8ToUTF16(testcases[i].output);
[email protected]dbb97ba2013-09-09 22:15:25207 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected);
[email protected]8ad3c5a2013-10-10 09:57:19208 EXPECT_EQ(expected, ElideFilename(filepath, font_list,
209 GetStringWidthF(UTF8ToUTF16(testcases[i].output), font_list)));
[email protected]dbb97ba2013-09-09 22:15:25210 }
211}
212
olivierrobin697bbda2015-12-07 14:53:48213// TODO(crbug.com/338784): Enable this on android.
[email protected]ebc9b662014-01-30 03:37:33214#if defined(OS_ANDROID)
215#define MAYBE_ElideTextTruncate DISABLED_ElideTextTruncate
216#else
217#define MAYBE_ElideTextTruncate ElideTextTruncate
218#endif
219TEST(TextEliderTest, MAYBE_ElideTextTruncate) {
[email protected]8ad3c5a2013-10-10 09:57:19220 const FontList font_list;
221 const float kTestWidth = GetStringWidthF(ASCIIToUTF16("Test"), font_list);
[email protected]dbb97ba2013-09-09 22:15:25222 struct TestData {
223 const char* input;
[email protected]8ad3c5a2013-10-10 09:57:19224 float width;
[email protected]dbb97ba2013-09-09 22:15:25225 const char* output;
226 } cases[] = {
227 { "", 0, "" },
228 { "Test", 0, "" },
229 { "", kTestWidth, "" },
230 { "Tes", kTestWidth, "Tes" },
231 { "Test", kTestWidth, "Test" },
232 { "Tests", kTestWidth, "Test" },
233 };
234
viettrungluua5ca99b2014-10-16 16:28:48235 for (size_t i = 0; i < arraysize(cases); ++i) {
[email protected]2aadf212013-12-18 20:03:44236 base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list,
[email protected]f3ce6212014-06-05 22:42:08237 cases[i].width, TRUNCATE);
[email protected]dbb97ba2013-09-09 22:15:25238 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result));
239 }
240}
241
olivierrobin697bbda2015-12-07 14:53:48242// TODO(crbug.com/338784): Enable this on android.
[email protected]ebc9b662014-01-30 03:37:33243#if defined(OS_ANDROID)
244#define MAYBE_ElideTextEllipsis DISABLED_ElideTextEllipsis
245#else
246#define MAYBE_ElideTextEllipsis ElideTextEllipsis
247#endif
248TEST(TextEliderTest, MAYBE_ElideTextEllipsis) {
[email protected]8ad3c5a2013-10-10 09:57:19249 const FontList font_list;
250 const float kTestWidth = GetStringWidthF(ASCIIToUTF16("Test"), font_list);
[email protected]dbb97ba2013-09-09 22:15:25251 const char* kEllipsis = "\xE2\x80\xA6";
[email protected]8ad3c5a2013-10-10 09:57:19252 const float kEllipsisWidth =
253 GetStringWidthF(UTF8ToUTF16(kEllipsis), font_list);
[email protected]dbb97ba2013-09-09 22:15:25254 struct TestData {
255 const char* input;
[email protected]8ad3c5a2013-10-10 09:57:19256 float width;
[email protected]dbb97ba2013-09-09 22:15:25257 const char* output;
258 } cases[] = {
259 { "", 0, "" },
260 { "Test", 0, "" },
261 { "Test", kEllipsisWidth, kEllipsis },
262 { "", kTestWidth, "" },
263 { "Tes", kTestWidth, "Tes" },
264 { "Test", kTestWidth, "Test" },
265 };
266
viettrungluua5ca99b2014-10-16 16:28:48267 for (size_t i = 0; i < arraysize(cases); ++i) {
[email protected]2aadf212013-12-18 20:03:44268 base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list,
[email protected]f3ce6212014-06-05 22:42:08269 cases[i].width, ELIDE_TAIL);
[email protected]dbb97ba2013-09-09 22:15:25270 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result));
271 }
272}
273
olivierrobin697bbda2015-12-07 14:53:48274// TODO(crbug.com/338784): Enable this on android.
[email protected]9d3e32b32014-02-04 16:01:47275#if defined(OS_ANDROID)
276#define MAYBE_ElideTextEllipsisFront DISABLED_ElideTextEllipsisFront
277#else
278#define MAYBE_ElideTextEllipsisFront ElideTextEllipsisFront
279#endif
280TEST(TextEliderTest, MAYBE_ElideTextEllipsisFront) {
281 const FontList font_list;
282 const float kTestWidth = GetStringWidthF(ASCIIToUTF16("Test"), font_list);
283 const std::string kEllipsisStr(kEllipsis);
284 const float kEllipsisWidth =
285 GetStringWidthF(UTF8ToUTF16(kEllipsis), font_list);
286 const float kEllipsis23Width =
287 GetStringWidthF(UTF8ToUTF16(kEllipsisStr + "23"), font_list);
288 struct TestData {
289 const char* input;
290 float width;
291 const base::string16 output;
292 } cases[] = {
293 { "", 0, base::string16() },
294 { "Test", 0, base::string16() },
295 { "Test", kEllipsisWidth, UTF8ToUTF16(kEllipsisStr) },
296 { "", kTestWidth, base::string16() },
297 { "Tes", kTestWidth, ASCIIToUTF16("Tes") },
298 { "Test", kTestWidth, ASCIIToUTF16("Test") },
299 { "Test123", kEllipsis23Width, UTF8ToUTF16(kEllipsisStr + "23") },
300 };
301
viettrungluua5ca99b2014-10-16 16:28:48302 for (size_t i = 0; i < arraysize(cases); ++i) {
[email protected]9d3e32b32014-02-04 16:01:47303 base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list,
[email protected]f3ce6212014-06-05 22:42:08304 cases[i].width, ELIDE_HEAD);
[email protected]9d3e32b32014-02-04 16:01:47305 EXPECT_EQ(cases[i].output, result);
306 }
307}
308
[email protected]dbb97ba2013-09-09 22:15:25309// Checks that all occurrences of |first_char| are followed by |second_char| and
mgiuca034e9302015-04-22 02:44:53310// all occurrences of |second_char| are preceded by |first_char| in |text|. Can
311// be used to test surrogate pairs or two-character combining sequences.
312static void CheckCodeUnitPairs(const base::string16& text,
313 base::char16 first_char,
314 base::char16 second_char) {
[email protected]8d936142014-07-10 21:40:37315 for (size_t index = 0; index < text.length(); ++index) {
316 EXPECT_NE(second_char, text[index]);
317 if (text[index] == first_char) {
318 ASSERT_LT(++index, text.length());
319 EXPECT_EQ(second_char, text[index]);
320 }
[email protected]dbb97ba2013-09-09 22:15:25321 }
322}
323
mgiuca034e9302015-04-22 02:44:53324// Test that both both UTF-16 surrogate pairs and combining character sequences
325// do not get split by ElideText.
olivierrobin697bbda2015-12-07 14:53:48326// TODO(crbug.com/338784): Enable this on android.
[email protected]ebc9b662014-01-30 03:37:33327#if defined(OS_ANDROID)
mgiuca034e9302015-04-22 02:44:53328#define MAYBE_ElideTextAtomicSequences DISABLED_ElideTextAtomicSequences
[email protected]ebc9b662014-01-30 03:37:33329#else
mgiuca034e9302015-04-22 02:44:53330#define MAYBE_ElideTextAtomicSequences ElideTextAtomicSequences
[email protected]ebc9b662014-01-30 03:37:33331#endif
mgiuca034e9302015-04-22 02:44:53332TEST(TextEliderTest, MAYBE_ElideTextAtomicSequences) {
[email protected]8ad3c5a2013-10-10 09:57:19333 const FontList font_list;
mgiuca034e9302015-04-22 02:44:53334 // The below is 'MUSICAL SYMBOL G CLEF' (U+1D11E), which is represented in
335 // UTF-16 as two code units forming a surrogate pair: 0xD834 0xDD1E.
336 const base::char16 kSurrogate[] = {0xD834, 0xDD1E, 0};
337 // The below is a Devanagari two-character combining sequence U+0921 U+093F.
338 // The sequence forms a single display character and should not be separated.
339 const base::char16 kCombiningSequence[] = {0x921, 0x93F, 0};
340 std::vector<base::string16> pairs;
341 pairs.push_back(kSurrogate);
342 pairs.push_back(kCombiningSequence);
[email protected]dbb97ba2013-09-09 22:15:25343
mgiuca034e9302015-04-22 02:44:53344 for (const base::string16& pair : pairs) {
345 base::char16 first_char = pair[0];
346 base::char16 second_char = pair[1];
347 base::string16 test_string = pair + UTF8ToUTF16("x") + pair;
348 SCOPED_TRACE(test_string);
349 const float test_string_width = GetStringWidthF(test_string, font_list);
350 base::string16 result;
[email protected]dbb97ba2013-09-09 22:15:25351
mgiuca034e9302015-04-22 02:44:53352 // Elide |text_string| to all possible widths and check that no instance of
353 // |pair| was split in two.
354 for (float width = 0; width <= test_string_width; width++) {
355 result = ElideText(test_string, font_list, width, TRUNCATE);
356 CheckCodeUnitPairs(result, first_char, second_char);
[email protected]dbb97ba2013-09-09 22:15:25357
mgiuca034e9302015-04-22 02:44:53358 result = ElideText(test_string, font_list, width, ELIDE_TAIL);
359 CheckCodeUnitPairs(result, first_char, second_char);
[email protected]9d3e32b32014-02-04 16:01:47360
mgiuca034e9302015-04-22 02:44:53361 result = ElideText(test_string, font_list, width, ELIDE_MIDDLE);
362 CheckCodeUnitPairs(result, first_char, second_char);
363
364 result = ElideText(test_string, font_list, width, ELIDE_HEAD);
365 CheckCodeUnitPairs(result, first_char, second_char);
366 }
[email protected]dbb97ba2013-09-09 22:15:25367 }
368}
369
olivierrobin697bbda2015-12-07 14:53:48370// TODO(crbug.com/338784): Enable this on android.
[email protected]ebc9b662014-01-30 03:37:33371#if defined(OS_ANDROID)
372#define MAYBE_ElideTextLongStrings DISABLED_ElideTextLongStrings
373#else
374#define MAYBE_ElideTextLongStrings ElideTextLongStrings
375#endif
376TEST(TextEliderTest, MAYBE_ElideTextLongStrings) {
[email protected]2aadf212013-12-18 20:03:44377 const base::string16 kEllipsisStr = UTF8ToUTF16(kEllipsis);
378 base::string16 data_scheme(UTF8ToUTF16("data:text/plain,"));
[email protected]dbb97ba2013-09-09 22:15:25379 size_t data_scheme_length = data_scheme.length();
380
[email protected]2aadf212013-12-18 20:03:44381 base::string16 ten_a(10, 'a');
382 base::string16 hundred_a(100, 'a');
383 base::string16 thousand_a(1000, 'a');
384 base::string16 ten_thousand_a(10000, 'a');
385 base::string16 hundred_thousand_a(100000, 'a');
386 base::string16 million_a(1000000, 'a');
[email protected]dbb97ba2013-09-09 22:15:25387
[email protected]9d3e32b32014-02-04 16:01:47388 // TODO(gbillock): Improve these tests by adding more string diversity and
389 // doing string compares instead of length compares. See bug 338836.
390
[email protected]dbb97ba2013-09-09 22:15:25391 size_t number_of_as = 156;
[email protected]2aadf212013-12-18 20:03:44392 base::string16 long_string_end(
393 data_scheme + base::string16(number_of_as, 'a') + kEllipsisStr);
[email protected]dbb97ba2013-09-09 22:15:25394 UTF16Testcase testcases_end[] = {
[email protected]9d3e32b32014-02-04 16:01:47395 { data_scheme + ten_a, data_scheme + ten_a },
396 { data_scheme + hundred_a, data_scheme + hundred_a },
397 { data_scheme + thousand_a, long_string_end },
398 { data_scheme + ten_thousand_a, long_string_end },
399 { data_scheme + hundred_thousand_a, long_string_end },
400 { data_scheme + million_a, long_string_end },
[email protected]dbb97ba2013-09-09 22:15:25401 };
402
[email protected]8ad3c5a2013-10-10 09:57:19403 const FontList font_list;
404 float ellipsis_width = GetStringWidthF(kEllipsisStr, font_list);
[email protected]dbb97ba2013-09-09 22:15:25405 for (size_t i = 0; i < arraysize(testcases_end); ++i) {
406 // Compare sizes rather than actual contents because if the test fails,
407 // output is rather long.
408 EXPECT_EQ(testcases_end[i].output.size(),
[email protected]f3ce6212014-06-05 22:42:08409 ElideText(testcases_end[i].input, font_list,
410 GetStringWidthF(testcases_end[i].output, font_list),
411 ELIDE_TAIL).size());
[email protected]dbb97ba2013-09-09 22:15:25412 EXPECT_EQ(kEllipsisStr,
[email protected]8ad3c5a2013-10-10 09:57:19413 ElideText(testcases_end[i].input, font_list, ellipsis_width,
[email protected]f3ce6212014-06-05 22:42:08414 ELIDE_TAIL));
[email protected]dbb97ba2013-09-09 22:15:25415 }
416
417 size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2;
[email protected]2aadf212013-12-18 20:03:44418 base::string16 long_string_middle(data_scheme +
419 base::string16(number_of_as - number_of_trailing_as, 'a') + kEllipsisStr +
420 base::string16(number_of_trailing_as, 'a'));
[email protected]dbb97ba2013-09-09 22:15:25421 UTF16Testcase testcases_middle[] = {
[email protected]9d3e32b32014-02-04 16:01:47422 { data_scheme + ten_a, data_scheme + ten_a },
423 { data_scheme + hundred_a, data_scheme + hundred_a },
424 { data_scheme + thousand_a, long_string_middle },
425 { data_scheme + ten_thousand_a, long_string_middle },
426 { data_scheme + hundred_thousand_a, long_string_middle },
427 { data_scheme + million_a, long_string_middle },
[email protected]dbb97ba2013-09-09 22:15:25428 };
429
430 for (size_t i = 0; i < arraysize(testcases_middle); ++i) {
431 // Compare sizes rather than actual contents because if the test fails,
432 // output is rather long.
433 EXPECT_EQ(testcases_middle[i].output.size(),
[email protected]f3ce6212014-06-05 22:42:08434 ElideText(testcases_middle[i].input, font_list,
435 GetStringWidthF(testcases_middle[i].output, font_list),
436 ELIDE_MIDDLE).size());
[email protected]dbb97ba2013-09-09 22:15:25437 EXPECT_EQ(kEllipsisStr,
[email protected]8ad3c5a2013-10-10 09:57:19438 ElideText(testcases_middle[i].input, font_list, ellipsis_width,
[email protected]f3ce6212014-06-05 22:42:08439 ELIDE_MIDDLE));
[email protected]9d3e32b32014-02-04 16:01:47440 }
441
442 base::string16 long_string_beginning(
443 kEllipsisStr + base::string16(number_of_as, 'a'));
444 UTF16Testcase testcases_beginning[] = {
445 { data_scheme + ten_a, data_scheme + ten_a },
446 { data_scheme + hundred_a, data_scheme + hundred_a },
447 { data_scheme + thousand_a, long_string_beginning },
448 { data_scheme + ten_thousand_a, long_string_beginning },
449 { data_scheme + hundred_thousand_a, long_string_beginning },
450 { data_scheme + million_a, long_string_beginning },
451 };
452 for (size_t i = 0; i < arraysize(testcases_beginning); ++i) {
453 EXPECT_EQ(testcases_beginning[i].output.size(),
454 ElideText(
455 testcases_beginning[i].input, font_list,
456 GetStringWidthF(testcases_beginning[i].output, font_list),
[email protected]f3ce6212014-06-05 22:42:08457 ELIDE_HEAD).size());
[email protected]9d3e32b32014-02-04 16:01:47458 EXPECT_EQ(kEllipsisStr,
459 ElideText(testcases_beginning[i].input, font_list, ellipsis_width,
[email protected]f3ce6212014-06-05 22:42:08460 ELIDE_HEAD));
[email protected]dbb97ba2013-09-09 22:15:25461 }
462}
463
mgiuca034e9302015-04-22 02:44:53464// Detailed tests for StringSlicer. These are faster and test more of the edge
465// cases than the above tests which are more end-to-end.
466
467TEST(TextEliderTest, StringSlicerBasicTest) {
468 // Must store strings in variables (StringSlicer retains a reference to them).
469 base::string16 text(UTF8ToUTF16("Hello, world!"));
470 base::string16 ellipsis(kEllipsisUTF16);
471 StringSlicer slicer(text, ellipsis, false, false);
472
473 EXPECT_EQ(UTF8ToUTF16(""), slicer.CutString(0, false));
474 EXPECT_EQ(base::string16(kEllipsisUTF16), slicer.CutString(0, true));
475
476 EXPECT_EQ(UTF8ToUTF16("Hell"), slicer.CutString(4, false));
477 EXPECT_EQ(UTF8ToUTF16("Hell") + kEllipsisUTF16, slicer.CutString(4, true));
478
479 EXPECT_EQ(text, slicer.CutString(text.length(), false));
480 EXPECT_EQ(text + kEllipsisUTF16, slicer.CutString(text.length(), true));
481
482 StringSlicer slicer_begin(text, ellipsis, false, true);
483 EXPECT_EQ(UTF8ToUTF16("rld!"), slicer_begin.CutString(4, false));
484 EXPECT_EQ(kEllipsisUTF16 + UTF8ToUTF16("rld!"),
485 slicer_begin.CutString(4, true));
486
487 StringSlicer slicer_mid(text, ellipsis, true, false);
488 EXPECT_EQ(UTF8ToUTF16("Held!"), slicer_mid.CutString(5, false));
489 EXPECT_EQ(UTF8ToUTF16("Hel") + kEllipsisUTF16 + UTF8ToUTF16("d!"),
490 slicer_mid.CutString(5, true));
491}
492
493TEST(TextEliderTest, StringSlicerSurrogate) {
494 // The below is 'MUSICAL SYMBOL G CLEF' (U+1D11E), which is represented in
495 // UTF-16 as two code units forming a surrogate pair: 0xD834 0xDD1E.
496 const base::char16 kSurrogate[] = {0xD834, 0xDD1E, 0};
497 base::string16 text(UTF8ToUTF16("abc") + kSurrogate + UTF8ToUTF16("xyz"));
498 base::string16 ellipsis(kEllipsisUTF16);
499 StringSlicer slicer(text, ellipsis, false, false);
500
501 // Cut surrogate on the right. Should round left and exclude the surrogate.
502 EXPECT_EQ(base::string16(kEllipsisUTF16), slicer.CutString(0, true));
503 EXPECT_EQ(UTF8ToUTF16("abc") + kEllipsisUTF16, slicer.CutString(4, true));
504 EXPECT_EQ(text + kEllipsisUTF16, slicer.CutString(text.length(), true));
505
506 // Cut surrogate on the left. Should round left and include the surrogate.
507 StringSlicer slicer_begin(text, ellipsis, false, true);
508 EXPECT_EQ(base::string16(kEllipsisUTF16) + kSurrogate + UTF8ToUTF16("xyz"),
509 slicer_begin.CutString(4, true));
510
511 // Cut surrogate in the middle. Should round right and exclude the surrogate.
512 base::string16 short_text(UTF8ToUTF16("abc") + kSurrogate);
513 StringSlicer slicer_mid(short_text, ellipsis, true, false);
514 EXPECT_EQ(UTF8ToUTF16("a") + kEllipsisUTF16, slicer_mid.CutString(2, true));
515
516 // String that starts with a dangling trailing surrogate.
517 base::char16 dangling_trailing_chars[] = {kSurrogate[1], 0};
518 base::string16 dangling_trailing_text(dangling_trailing_chars);
519 StringSlicer slicer_dangling_trailing(dangling_trailing_text, ellipsis, false,
520 false);
521 EXPECT_EQ(base::string16(kEllipsisUTF16),
522 slicer_dangling_trailing.CutString(0, true));
523 EXPECT_EQ(dangling_trailing_text + kEllipsisUTF16,
524 slicer_dangling_trailing.CutString(1, true));
525}
526
527TEST(TextEliderTest, StringSlicerCombining) {
528 // The following string contains three combining character sequences (one for
529 // each category of combining mark):
530 // LATIN SMALL LETTER E + COMBINING ACUTE ACCENT + COMBINING CEDILLA
531 // LATIN SMALL LETTER X + COMBINING ENCLOSING KEYCAP
532 // DEVANAGARI LETTER DDA + DEVANAGARI VOWEL SIGN I
533 const base::char16 kText[] = {
534 'e', 0x301, 0x327, ' ', 'x', 0x20E3, ' ', 0x921, 0x93F, 0};
535 base::string16 text(kText);
536 base::string16 ellipsis(kEllipsisUTF16);
537 StringSlicer slicer(text, ellipsis, false, false);
538
539 // Attempt to cut the string for all lengths. When a combining sequence is
540 // cut, it should always round left and exclude the combining sequence.
541 // First sequence:
542 EXPECT_EQ(base::string16(kEllipsisUTF16), slicer.CutString(0, true));
543 EXPECT_EQ(base::string16(kEllipsisUTF16), slicer.CutString(1, true));
544 EXPECT_EQ(base::string16(kEllipsisUTF16), slicer.CutString(2, true));
545 EXPECT_EQ(text.substr(0, 3) + kEllipsisUTF16, slicer.CutString(3, true));
546 // Second sequence:
547 EXPECT_EQ(text.substr(0, 4) + kEllipsisUTF16, slicer.CutString(4, true));
548 EXPECT_EQ(text.substr(0, 4) + kEllipsisUTF16, slicer.CutString(5, true));
549 EXPECT_EQ(text.substr(0, 6) + kEllipsisUTF16, slicer.CutString(6, true));
550 // Third sequence:
551 EXPECT_EQ(text.substr(0, 7) + kEllipsisUTF16, slicer.CutString(7, true));
552 EXPECT_EQ(text.substr(0, 7) + kEllipsisUTF16, slicer.CutString(8, true));
553 EXPECT_EQ(text + kEllipsisUTF16, slicer.CutString(9, true));
554
555 // Cut string in the middle, splitting the second sequence in half. Should
556 // round both left and right, excluding the second sequence.
557 StringSlicer slicer_mid(text, ellipsis, true, false);
558 EXPECT_EQ(text.substr(0, 4) + kEllipsisUTF16 + text.substr(6),
559 slicer_mid.CutString(9, true));
560
561 // String that starts with a dangling combining mark.
562 base::char16 dangling_mark_chars[] = {text[1], 0};
563 base::string16 dangling_mark_text(dangling_mark_chars);
564 StringSlicer slicer_dangling_mark(dangling_mark_text, ellipsis, false, false);
565 EXPECT_EQ(base::string16(kEllipsisUTF16),
566 slicer_dangling_mark.CutString(0, true));
567 EXPECT_EQ(dangling_mark_text + kEllipsisUTF16,
568 slicer_dangling_mark.CutString(1, true));
569}
570
571TEST(TextEliderTest, StringSlicerCombiningSurrogate) {
572 // The ultimate test: combining sequences comprised of surrogate pairs.
573 // The following string contains a single combining character sequence:
574 // MUSICAL SYMBOL G CLEF (U+1D11E) + MUSICAL SYMBOL COMBINING FLAG-1 (U+1D16E)
575 // Represented as four UTF-16 code units.
576 const base::char16 kText[] = {0xD834, 0xDD1E, 0xD834, 0xDD6E, 0};
577 base::string16 text(kText);
578 base::string16 ellipsis(kEllipsisUTF16);
579 StringSlicer slicer(text, ellipsis, false, false);
580
581 // Attempt to cut the string for all lengths. Should always round left and
582 // exclude the combining sequence.
583 EXPECT_EQ(base::string16(kEllipsisUTF16), slicer.CutString(0, true));
584 EXPECT_EQ(base::string16(kEllipsisUTF16), slicer.CutString(1, true));
585 EXPECT_EQ(base::string16(kEllipsisUTF16), slicer.CutString(2, true));
586 EXPECT_EQ(base::string16(kEllipsisUTF16), slicer.CutString(3, true));
587 EXPECT_EQ(text + kEllipsisUTF16, slicer.CutString(4, true));
588
589 // Cut string in the middle. Should exclude the sequence.
590 StringSlicer slicer_mid(text, ellipsis, true, false);
591 EXPECT_EQ(base::string16(kEllipsisUTF16), slicer_mid.CutString(4, true));
592}
593
[email protected]dbb97ba2013-09-09 22:15:25594TEST(TextEliderTest, ElideString) {
595 struct TestData {
596 const char* input;
thestigbbf93ac2016-02-02 00:24:49597 size_t max_len;
[email protected]dbb97ba2013-09-09 22:15:25598 bool result;
599 const char* output;
600 } cases[] = {
601 { "Hello", 0, true, "" },
602 { "", 0, false, "" },
603 { "Hello, my name is Tom", 1, true, "H" },
604 { "Hello, my name is Tom", 2, true, "He" },
605 { "Hello, my name is Tom", 3, true, "H.m" },
606 { "Hello, my name is Tom", 4, true, "H..m" },
607 { "Hello, my name is Tom", 5, true, "H...m" },
608 { "Hello, my name is Tom", 6, true, "He...m" },
609 { "Hello, my name is Tom", 7, true, "He...om" },
610 { "Hello, my name is Tom", 10, true, "Hell...Tom" },
611 { "Hello, my name is Tom", 100, false, "Hello, my name is Tom" }
612 };
viettrungluua5ca99b2014-10-16 16:28:48613 for (size_t i = 0; i < arraysize(cases); ++i) {
[email protected]2aadf212013-12-18 20:03:44614 base::string16 output;
[email protected]dbb97ba2013-09-09 22:15:25615 EXPECT_EQ(cases[i].result,
616 ElideString(UTF8ToUTF16(cases[i].input),
617 cases[i].max_len, &output));
618 EXPECT_EQ(cases[i].output, UTF16ToUTF8(output));
619 }
620}
621
olivierrobin697bbda2015-12-07 14:53:48622// TODO(crbug.com/338784): Enable this on android.
[email protected]ebc9b662014-01-30 03:37:33623#if defined(OS_ANDROID)
624#define MAYBE_ElideRectangleText DISABLED_ElideRectangleText
625#else
626#define MAYBE_ElideRectangleText ElideRectangleText
627#endif
628TEST(TextEliderTest, MAYBE_ElideRectangleText) {
[email protected]8ad3c5a2013-10-10 09:57:19629 const FontList font_list;
630 const int line_height = font_list.GetHeight();
631 const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list);
[email protected]dbb97ba2013-09-09 22:15:25632
633 struct TestData {
634 const char* input;
[email protected]8ad3c5a2013-10-10 09:57:19635 float available_pixel_width;
[email protected]dbb97ba2013-09-09 22:15:25636 int available_pixel_height;
637 bool truncated_y;
638 const char* output;
639 } cases[] = {
640 { "", 0, 0, false, NULL },
641 { "", 1, 1, false, NULL },
642 { "Test", test_width, 0, true, NULL },
643 { "Test", test_width, 1, false, "Test" },
644 { "Test", test_width, line_height, false, "Test" },
645 { "Test Test", test_width, line_height, true, "Test" },
646 { "Test Test", test_width, line_height + 1, false, "Test|Test" },
647 { "Test Test", test_width, line_height * 2, false, "Test|Test" },
648 { "Test Test", test_width, line_height * 3, false, "Test|Test" },
649 { "Test Test", test_width * 2, line_height * 2, false, "Test|Test" },
650 { "Test Test", test_width * 3, line_height, false, "Test Test" },
651 { "Test\nTest", test_width * 3, line_height * 2, false, "Test|Test" },
652 { "Te\nst Te", test_width, line_height * 3, false, "Te|st|Te" },
653 { "\nTest", test_width, line_height * 2, false, "|Test" },
654 { "\nTest", test_width, line_height, true, "" },
655 { "\n\nTest", test_width, line_height * 3, false, "||Test" },
656 { "\n\nTest", test_width, line_height * 2, true, "|" },
657 { "Test\n", 2 * test_width, line_height * 5, false, "Test|" },
658 { "Test\n\n", 2 * test_width, line_height * 5, false, "Test||" },
659 { "Test\n\n\n", 2 * test_width, line_height * 5, false, "Test|||" },
660 { "Test\nTest\n\n", 2 * test_width, line_height * 5, false, "Test|Test||" },
661 { "Test\n\nTest\n", 2 * test_width, line_height * 5, false, "Test||Test|" },
662 { "Test\n\n\nTest", 2 * test_width, line_height * 5, false, "Test|||Test" },
663 { "Te ", test_width, line_height, false, "Te" },
664 { "Te Te Test", test_width, 3 * line_height, false, "Te|Te|Test" },
665 };
666
viettrungluua5ca99b2014-10-16 16:28:48667 for (size_t i = 0; i < arraysize(cases); ++i) {
[email protected]2aadf212013-12-18 20:03:44668 std::vector<base::string16> lines;
[email protected]dbb97ba2013-09-09 22:15:25669 EXPECT_EQ(cases[i].truncated_y ? INSUFFICIENT_SPACE_VERTICAL : 0,
670 ElideRectangleText(UTF8ToUTF16(cases[i].input),
[email protected]8ad3c5a2013-10-10 09:57:19671 font_list,
[email protected]dbb97ba2013-09-09 22:15:25672 cases[i].available_pixel_width,
673 cases[i].available_pixel_height,
674 TRUNCATE_LONG_WORDS,
675 &lines));
676 if (cases[i].output) {
brettwd94a22142015-07-15 05:19:26677 const std::string result =
678 UTF16ToUTF8(base::JoinString(lines, ASCIIToUTF16("|")));
[email protected]dbb97ba2013-09-09 22:15:25679 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!";
680 } else {
681 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!";
682 }
683 }
684}
685
olivierrobin697bbda2015-12-07 14:53:48686// TODO(crbug.com/338784): Enable this on android.
[email protected]ebc9b662014-01-30 03:37:33687#if defined(OS_ANDROID)
688#define MAYBE_ElideRectangleTextPunctuation \
689 DISABLED_ElideRectangleTextPunctuation
690#else
691#define MAYBE_ElideRectangleTextPunctuation ElideRectangleTextPunctuation
692#endif
693TEST(TextEliderTest, MAYBE_ElideRectangleTextPunctuation) {
[email protected]8ad3c5a2013-10-10 09:57:19694 const FontList font_list;
695 const int line_height = font_list.GetHeight();
696 const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list);
697 const float test_t_width = GetStringWidthF(ASCIIToUTF16("Test T"), font_list);
[email protected]dbb97ba2013-09-09 22:15:25698
699 struct TestData {
700 const char* input;
[email protected]8ad3c5a2013-10-10 09:57:19701 float available_pixel_width;
[email protected]dbb97ba2013-09-09 22:15:25702 int available_pixel_height;
703 bool wrap_words;
704 bool truncated_x;
705 const char* output;
706 } cases[] = {
707 { "Test T.", test_t_width, line_height * 2, false, false, "Test|T." },
708 { "Test T ?", test_t_width, line_height * 2, false, false, "Test|T ?" },
709 { "Test. Test", test_width, line_height * 3, false, true, "Test|Test" },
710 { "Test. Test", test_width, line_height * 3, true, false, "Test|.|Test" },
711 };
712
viettrungluua5ca99b2014-10-16 16:28:48713 for (size_t i = 0; i < arraysize(cases); ++i) {
[email protected]2aadf212013-12-18 20:03:44714 std::vector<base::string16> lines;
[email protected]dbb97ba2013-09-09 22:15:25715 const WordWrapBehavior wrap_behavior =
716 (cases[i].wrap_words ? WRAP_LONG_WORDS : TRUNCATE_LONG_WORDS);
717 EXPECT_EQ(cases[i].truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0,
718 ElideRectangleText(UTF8ToUTF16(cases[i].input),
[email protected]8ad3c5a2013-10-10 09:57:19719 font_list,
[email protected]dbb97ba2013-09-09 22:15:25720 cases[i].available_pixel_width,
721 cases[i].available_pixel_height,
722 wrap_behavior,
723 &lines));
724 if (cases[i].output) {
brettwd94a22142015-07-15 05:19:26725 const std::string result =
726 UTF16ToUTF8(base::JoinString(lines, base::ASCIIToUTF16("|")));
[email protected]dbb97ba2013-09-09 22:15:25727 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!";
728 } else {
729 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!";
730 }
731 }
732}
733
olivierrobin697bbda2015-12-07 14:53:48734// TODO(crbug.com/338784): Enable this on android.
[email protected]ebc9b662014-01-30 03:37:33735#if defined(OS_ANDROID)
736#define MAYBE_ElideRectangleTextLongWords DISABLED_ElideRectangleTextLongWords
737#else
738#define MAYBE_ElideRectangleTextLongWords ElideRectangleTextLongWords
739#endif
740TEST(TextEliderTest, MAYBE_ElideRectangleTextLongWords) {
[email protected]8ad3c5a2013-10-10 09:57:19741 const FontList font_list;
[email protected]dbb97ba2013-09-09 22:15:25742 const int kAvailableHeight = 1000;
[email protected]2aadf212013-12-18 20:03:44743 const base::string16 kElidedTesting =
744 UTF8ToUTF16(std::string("Tes") + kEllipsis);
[email protected]8ad3c5a2013-10-10 09:57:19745 const float elided_width = GetStringWidthF(kElidedTesting, font_list);
746 const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list);
[email protected]dbb97ba2013-09-09 22:15:25747
748 struct TestData {
749 const char* input;
[email protected]8ad3c5a2013-10-10 09:57:19750 float available_pixel_width;
[email protected]dbb97ba2013-09-09 22:15:25751 WordWrapBehavior wrap_behavior;
752 bool truncated_x;
753 const char* output;
754 } cases[] = {
755 { "Testing", test_width, IGNORE_LONG_WORDS, false, "Testing" },
756 { "X Testing", test_width, IGNORE_LONG_WORDS, false, "X|Testing" },
757 { "Test Testing", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" },
758 { "Test\nTesting", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" },
759 { "Test Tests ", test_width, IGNORE_LONG_WORDS, false, "Test|Tests" },
760 { "Test Tests T", test_width, IGNORE_LONG_WORDS, false, "Test|Tests|T" },
761
762 { "Testing", elided_width, ELIDE_LONG_WORDS, true, "Tes..." },
763 { "X Testing", elided_width, ELIDE_LONG_WORDS, true, "X|Tes..." },
764 { "Test Testing", elided_width, ELIDE_LONG_WORDS, true, "Test|Tes..." },
765 { "Test\nTesting", elided_width, ELIDE_LONG_WORDS, true, "Test|Tes..." },
766
767 { "Testing", test_width, TRUNCATE_LONG_WORDS, true, "Test" },
768 { "X Testing", test_width, TRUNCATE_LONG_WORDS, true, "X|Test" },
769 { "Test Testing", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test" },
770 { "Test\nTesting", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test" },
771 { "Test Tests ", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test" },
772 { "Test Tests T", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test|T" },
773
774 { "Testing", test_width, WRAP_LONG_WORDS, false, "Test|ing" },
775 { "X Testing", test_width, WRAP_LONG_WORDS, false, "X|Test|ing" },
776 { "Test Testing", test_width, WRAP_LONG_WORDS, false, "Test|Test|ing" },
777 { "Test\nTesting", test_width, WRAP_LONG_WORDS, false, "Test|Test|ing" },
778 { "Test Tests ", test_width, WRAP_LONG_WORDS, false, "Test|Test|s" },
779 { "Test Tests T", test_width, WRAP_LONG_WORDS, false, "Test|Test|s T" },
780 { "TestTestTest", test_width, WRAP_LONG_WORDS, false, "Test|Test|Test" },
781 { "TestTestTestT", test_width, WRAP_LONG_WORDS, false, "Test|Test|Test|T" },
782 };
783
viettrungluua5ca99b2014-10-16 16:28:48784 for (size_t i = 0; i < arraysize(cases); ++i) {
[email protected]2aadf212013-12-18 20:03:44785 std::vector<base::string16> lines;
[email protected]dbb97ba2013-09-09 22:15:25786 EXPECT_EQ(cases[i].truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0,
787 ElideRectangleText(UTF8ToUTF16(cases[i].input),
[email protected]8ad3c5a2013-10-10 09:57:19788 font_list,
[email protected]dbb97ba2013-09-09 22:15:25789 cases[i].available_pixel_width,
790 kAvailableHeight,
791 cases[i].wrap_behavior,
792 &lines));
793 std::string expected_output(cases[i].output);
brettwe6dae462015-06-24 20:54:45794 base::ReplaceSubstringsAfterOffset(&expected_output, 0, "...", kEllipsis);
brettwd94a22142015-07-15 05:19:26795 const std::string result =
796 UTF16ToUTF8(base::JoinString(lines, base::ASCIIToUTF16("|")));
[email protected]dbb97ba2013-09-09 22:15:25797 EXPECT_EQ(expected_output, result) << "Case " << i << " failed!";
798 }
799}
800
[email protected]c3087982013-09-20 17:46:53801// This test is to make sure that the width of each wrapped line does not
802// exceed the available width. On some platform like Mac, this test used to
803// fail because the truncated integer width is returned for the string
804// and the accumulation of the truncated values causes the elide function
805// to wrap incorrectly.
olivierrobin697bbda2015-12-07 14:53:48806// TODO(crbug.com/338784): Enable this on android.
[email protected]ebc9b662014-01-30 03:37:33807#if defined(OS_ANDROID)
808#define MAYBE_ElideRectangleTextCheckLineWidth \
809 DISABLED_ElideRectangleTextCheckLineWidth
810#else
811#define MAYBE_ElideRectangleTextCheckLineWidth ElideRectangleTextCheckLineWidth
812#endif
813TEST(TextEliderTest, MAYBE_ElideRectangleTextCheckLineWidth) {
[email protected]8ad3c5a2013-10-10 09:57:19814 FontList font_list;
815#if defined(OS_MACOSX) && !defined(OS_IOS)
816 // Use a specific font to expose the line width exceeding problem.
817 font_list = FontList(Font("LucidaGrande", 12));
818#endif
819 const float kAvailableWidth = 235;
[email protected]c3087982013-09-20 17:46:53820 const int kAvailableHeight = 1000;
821 const char text[] = "that Russian place we used to go to after fencing";
[email protected]2aadf212013-12-18 20:03:44822 std::vector<base::string16> lines;
[email protected]c3087982013-09-20 17:46:53823 EXPECT_EQ(0, ElideRectangleText(UTF8ToUTF16(text),
[email protected]8ad3c5a2013-10-10 09:57:19824 font_list,
[email protected]c3087982013-09-20 17:46:53825 kAvailableWidth,
826 kAvailableHeight,
827 WRAP_LONG_WORDS,
828 &lines));
829 ASSERT_EQ(2u, lines.size());
[email protected]8ad3c5a2013-10-10 09:57:19830 EXPECT_LE(GetStringWidthF(lines[0], font_list), kAvailableWidth);
831 EXPECT_LE(GetStringWidthF(lines[1], font_list), kAvailableWidth);
[email protected]c3087982013-09-20 17:46:53832}
833
thestigbbf93ac2016-02-02 00:24:49834#if defined(OS_CHROMEOS)
rsorokin370fe4b2014-11-11 15:14:07835// This test was created specifically to test a message from crbug.com/415213.
836// It tests that width of concatenation of words equals sum of widths of the
837// words.
838TEST(TextEliderTest, ElideRectangleTextCheckConcatWidthEqualsSumOfWidths) {
839 FontList font_list;
840 font_list = FontList("Noto Sans UI,ui-sans, 12px");
841 SetFontRenderParamsDeviceScaleFactor(1.25f);
842#define WIDTH(x) GetStringWidthF(UTF8ToUTF16(x), font_list)
843 EXPECT_EQ(WIDTH("The administrator for this account has"),
844 WIDTH("The ") + WIDTH("administrator ") + WIDTH("for ") +
845 WIDTH("this ") + WIDTH("account ") + WIDTH("has"));
846#undef WIDTH
847 SetFontRenderParamsDeviceScaleFactor(1.0f);
848}
thestigbbf93ac2016-02-02 00:24:49849#endif // defined(OS_CHROMEOS)
rsorokin370fe4b2014-11-11 15:14:07850
olivierrobin697bbda2015-12-07 14:53:48851// TODO(crbug.com/338784): Enable this on android.
pkotwicz85fd1d92015-05-20 16:36:24852#if defined(OS_ANDROID)
853#define MAYBE_ElideRectangleString DISABLED_ElideRectangleString
854#else
855#define MAYBE_ElideRectangleString ElideRectangleString
856#endif
857TEST(TextEliderTest, MAYBE_ElideRectangleString) {
[email protected]dbb97ba2013-09-09 22:15:25858 struct TestData {
859 const char* input;
860 int max_rows;
861 int max_cols;
862 bool result;
863 const char* output;
864 } cases[] = {
865 { "", 0, 0, false, "" },
866 { "", 1, 1, false, "" },
867 { "Hi, my name is\nTom", 0, 0, true, "..." },
868 { "Hi, my name is\nTom", 1, 0, true, "\n..." },
869 { "Hi, my name is\nTom", 0, 1, true, "..." },
870 { "Hi, my name is\nTom", 1, 1, true, "H\n..." },
871 { "Hi, my name is\nTom", 2, 1, true, "H\ni\n..." },
872 { "Hi, my name is\nTom", 3, 1, true, "H\ni\n,\n..." },
873 { "Hi, my name is\nTom", 4, 1, true, "H\ni\n,\n \n..." },
874 { "Hi, my name is\nTom", 5, 1, true, "H\ni\n,\n \nm\n..." },
875 { "Hi, my name is\nTom", 0, 2, true, "..." },
876 { "Hi, my name is\nTom", 1, 2, true, "Hi\n..." },
877 { "Hi, my name is\nTom", 2, 2, true, "Hi\n, \n..." },
878 { "Hi, my name is\nTom", 3, 2, true, "Hi\n, \nmy\n..." },
879 { "Hi, my name is\nTom", 4, 2, true, "Hi\n, \nmy\n n\n..." },
880 { "Hi, my name is\nTom", 5, 2, true, "Hi\n, \nmy\n n\nam\n..." },
881 { "Hi, my name is\nTom", 0, 3, true, "..." },
882 { "Hi, my name is\nTom", 1, 3, true, "Hi,\n..." },
883 { "Hi, my name is\nTom", 2, 3, true, "Hi,\n my\n..." },
884 { "Hi, my name is\nTom", 3, 3, true, "Hi,\n my\n na\n..." },
885 { "Hi, my name is\nTom", 4, 3, true, "Hi,\n my\n na\nme \n..." },
886 { "Hi, my name is\nTom", 5, 3, true, "Hi,\n my\n na\nme \nis\n..." },
887 { "Hi, my name is\nTom", 1, 4, true, "Hi, \n..." },
888 { "Hi, my name is\nTom", 2, 4, true, "Hi, \nmy n\n..." },
889 { "Hi, my name is\nTom", 3, 4, true, "Hi, \nmy n\name \n..." },
890 { "Hi, my name is\nTom", 4, 4, true, "Hi, \nmy n\name \nis\n..." },
891 { "Hi, my name is\nTom", 5, 4, false, "Hi, \nmy n\name \nis\nTom" },
892 { "Hi, my name is\nTom", 1, 5, true, "Hi, \n..." },
893 { "Hi, my name is\nTom", 2, 5, true, "Hi, \nmy na\n..." },
894 { "Hi, my name is\nTom", 3, 5, true, "Hi, \nmy na\nme \n..." },
895 { "Hi, my name is\nTom", 4, 5, true, "Hi, \nmy na\nme \nis\n..." },
896 { "Hi, my name is\nTom", 5, 5, false, "Hi, \nmy na\nme \nis\nTom" },
897 { "Hi, my name is\nTom", 1, 6, true, "Hi, \n..." },
898 { "Hi, my name is\nTom", 2, 6, true, "Hi, \nmy \n..." },
899 { "Hi, my name is\nTom", 3, 6, true, "Hi, \nmy \nname \n..." },
900 { "Hi, my name is\nTom", 4, 6, true, "Hi, \nmy \nname \nis\n..." },
901 { "Hi, my name is\nTom", 5, 6, false, "Hi, \nmy \nname \nis\nTom" },
902 { "Hi, my name is\nTom", 1, 7, true, "Hi, \n..." },
903 { "Hi, my name is\nTom", 2, 7, true, "Hi, \nmy \n..." },
904 { "Hi, my name is\nTom", 3, 7, true, "Hi, \nmy \nname \n..." },
905 { "Hi, my name is\nTom", 4, 7, true, "Hi, \nmy \nname \nis\n..." },
906 { "Hi, my name is\nTom", 5, 7, false, "Hi, \nmy \nname \nis\nTom" },
907 { "Hi, my name is\nTom", 1, 8, true, "Hi, my \n..." },
908 { "Hi, my name is\nTom", 2, 8, true, "Hi, my \nname \n..." },
909 { "Hi, my name is\nTom", 3, 8, true, "Hi, my \nname \nis\n..." },
910 { "Hi, my name is\nTom", 4, 8, false, "Hi, my \nname \nis\nTom" },
911 { "Hi, my name is\nTom", 1, 9, true, "Hi, my \n..." },
912 { "Hi, my name is\nTom", 2, 9, true, "Hi, my \nname is\n..." },
913 { "Hi, my name is\nTom", 3, 9, false, "Hi, my \nname is\nTom" },
914 { "Hi, my name is\nTom", 1, 10, true, "Hi, my \n..." },
915 { "Hi, my name is\nTom", 2, 10, true, "Hi, my \nname is\n..." },
916 { "Hi, my name is\nTom", 3, 10, false, "Hi, my \nname is\nTom" },
917 { "Hi, my name is\nTom", 1, 11, true, "Hi, my \n..." },
918 { "Hi, my name is\nTom", 2, 11, true, "Hi, my \nname is\n..." },
919 { "Hi, my name is\nTom", 3, 11, false, "Hi, my \nname is\nTom" },
920 { "Hi, my name is\nTom", 1, 12, true, "Hi, my \n..." },
921 { "Hi, my name is\nTom", 2, 12, true, "Hi, my \nname is\n..." },
922 { "Hi, my name is\nTom", 3, 12, false, "Hi, my \nname is\nTom" },
923 { "Hi, my name is\nTom", 1, 13, true, "Hi, my name \n..." },
924 { "Hi, my name is\nTom", 2, 13, true, "Hi, my name \nis\n..." },
925 { "Hi, my name is\nTom", 3, 13, false, "Hi, my name \nis\nTom" },
926 { "Hi, my name is\nTom", 1, 20, true, "Hi, my name is\n..." },
927 { "Hi, my name is\nTom", 2, 20, false, "Hi, my name is\nTom" },
928 { "Hi, my name is Tom", 1, 40, false, "Hi, my name is Tom" },
929 };
[email protected]2aadf212013-12-18 20:03:44930 base::string16 output;
viettrungluua5ca99b2014-10-16 16:28:48931 for (size_t i = 0; i < arraysize(cases); ++i) {
[email protected]dbb97ba2013-09-09 22:15:25932 EXPECT_EQ(cases[i].result,
933 ElideRectangleString(UTF8ToUTF16(cases[i].input),
934 cases[i].max_rows, cases[i].max_cols,
935 true, &output));
936 EXPECT_EQ(cases[i].output, UTF16ToUTF8(output));
937 }
938}
939
olivierrobin697bbda2015-12-07 14:53:48940// TODO(crbug.com/338784): Enable this on android.
pkotwicz85fd1d92015-05-20 16:36:24941#if defined(OS_ANDROID)
942#define MAYBE_ElideRectangleStringNotStrict \
943 DISABLED_ElideRectangleStringNotStrict
944#else
945#define MAYBE_ElideRectangleStringNotStrict ElideRectangleStringNotStrict
946#endif
947TEST(TextEliderTest, MAYBE_ElideRectangleStringNotStrict) {
[email protected]dbb97ba2013-09-09 22:15:25948 struct TestData {
949 const char* input;
950 int max_rows;
951 int max_cols;
952 bool result;
953 const char* output;
954 } cases[] = {
955 { "", 0, 0, false, "" },
956 { "", 1, 1, false, "" },
957 { "Hi, my name_is\nDick", 0, 0, true, "..." },
958 { "Hi, my name_is\nDick", 1, 0, true, "\n..." },
959 { "Hi, my name_is\nDick", 0, 1, true, "..." },
960 { "Hi, my name_is\nDick", 1, 1, true, "H\n..." },
961 { "Hi, my name_is\nDick", 2, 1, true, "H\ni\n..." },
962 { "Hi, my name_is\nDick", 3, 1, true, "H\ni\n,\n..." },
963 { "Hi, my name_is\nDick", 4, 1, true, "H\ni\n,\n \n..." },
964 { "Hi, my name_is\nDick", 5, 1, true, "H\ni\n,\n \nm\n..." },
965 { "Hi, my name_is\nDick", 0, 2, true, "..." },
966 { "Hi, my name_is\nDick", 1, 2, true, "Hi\n..." },
967 { "Hi, my name_is\nDick", 2, 2, true, "Hi\n, \n..." },
968 { "Hi, my name_is\nDick", 3, 2, true, "Hi\n, \nmy\n..." },
969 { "Hi, my name_is\nDick", 4, 2, true, "Hi\n, \nmy\n n\n..." },
970 { "Hi, my name_is\nDick", 5, 2, true, "Hi\n, \nmy\n n\nam\n..." },
971 { "Hi, my name_is\nDick", 0, 3, true, "..." },
972 { "Hi, my name_is\nDick", 1, 3, true, "Hi,\n..." },
973 { "Hi, my name_is\nDick", 2, 3, true, "Hi,\n my\n..." },
974 { "Hi, my name_is\nDick", 3, 3, true, "Hi,\n my\n na\n..." },
975 { "Hi, my name_is\nDick", 4, 3, true, "Hi,\n my\n na\nme_\n..." },
976 { "Hi, my name_is\nDick", 5, 3, true, "Hi,\n my\n na\nme_\nis\n..." },
977 { "Hi, my name_is\nDick", 1, 4, true, "Hi, ..." },
978 { "Hi, my name_is\nDick", 2, 4, true, "Hi, my n\n..." },
979 { "Hi, my name_is\nDick", 3, 4, true, "Hi, my n\name_\n..." },
980 { "Hi, my name_is\nDick", 4, 4, true, "Hi, my n\name_\nis\n..." },
981 { "Hi, my name_is\nDick", 5, 4, false, "Hi, my n\name_\nis\nDick" },
982 { "Hi, my name_is\nDick", 1, 5, true, "Hi, ..." },
983 { "Hi, my name_is\nDick", 2, 5, true, "Hi, my na\n..." },
984 { "Hi, my name_is\nDick", 3, 5, true, "Hi, my na\nme_is\n..." },
985 { "Hi, my name_is\nDick", 4, 5, true, "Hi, my na\nme_is\n\n..." },
986 { "Hi, my name_is\nDick", 5, 5, false, "Hi, my na\nme_is\n\nDick" },
987 { "Hi, my name_is\nDick", 1, 6, true, "Hi, ..." },
988 { "Hi, my name_is\nDick", 2, 6, true, "Hi, my nam\n..." },
989 { "Hi, my name_is\nDick", 3, 6, true, "Hi, my nam\ne_is\n..." },
990 { "Hi, my name_is\nDick", 4, 6, false, "Hi, my nam\ne_is\nDick" },
991 { "Hi, my name_is\nDick", 5, 6, false, "Hi, my nam\ne_is\nDick" },
992 { "Hi, my name_is\nDick", 1, 7, true, "Hi, ..." },
993 { "Hi, my name_is\nDick", 2, 7, true, "Hi, my name\n..." },
994 { "Hi, my name_is\nDick", 3, 7, true, "Hi, my name\n_is\n..." },
995 { "Hi, my name_is\nDick", 4, 7, false, "Hi, my name\n_is\nDick" },
996 { "Hi, my name_is\nDick", 5, 7, false, "Hi, my name\n_is\nDick" },
997 { "Hi, my name_is\nDick", 1, 8, true, "Hi, my n\n..." },
998 { "Hi, my name_is\nDick", 2, 8, true, "Hi, my n\name_is\n..." },
999 { "Hi, my name_is\nDick", 3, 8, false, "Hi, my n\name_is\nDick" },
1000 { "Hi, my name_is\nDick", 1, 9, true, "Hi, my ..." },
1001 { "Hi, my name_is\nDick", 2, 9, true, "Hi, my name_is\n..." },
1002 { "Hi, my name_is\nDick", 3, 9, false, "Hi, my name_is\nDick" },
1003 { "Hi, my name_is\nDick", 1, 10, true, "Hi, my ..." },
1004 { "Hi, my name_is\nDick", 2, 10, true, "Hi, my name_is\n..." },
1005 { "Hi, my name_is\nDick", 3, 10, false, "Hi, my name_is\nDick" },
1006 { "Hi, my name_is\nDick", 1, 11, true, "Hi, my ..." },
1007 { "Hi, my name_is\nDick", 2, 11, true, "Hi, my name_is\n..." },
1008 { "Hi, my name_is\nDick", 3, 11, false, "Hi, my name_is\nDick" },
1009 { "Hi, my name_is\nDick", 1, 12, true, "Hi, my ..." },
1010 { "Hi, my name_is\nDick", 2, 12, true, "Hi, my name_is\n..." },
1011 { "Hi, my name_is\nDick", 3, 12, false, "Hi, my name_is\nDick" },
1012 { "Hi, my name_is\nDick", 1, 13, true, "Hi, my ..." },
1013 { "Hi, my name_is\nDick", 2, 13, true, "Hi, my name_is\n..." },
1014 { "Hi, my name_is\nDick", 3, 13, false, "Hi, my name_is\nDick" },
1015 { "Hi, my name_is\nDick", 1, 20, true, "Hi, my name_is\n..." },
1016 { "Hi, my name_is\nDick", 2, 20, false, "Hi, my name_is\nDick" },
1017 { "Hi, my name_is Dick", 1, 40, false, "Hi, my name_is Dick" },
1018 };
[email protected]2aadf212013-12-18 20:03:441019 base::string16 output;
viettrungluua5ca99b2014-10-16 16:28:481020 for (size_t i = 0; i < arraysize(cases); ++i) {
[email protected]dbb97ba2013-09-09 22:15:251021 EXPECT_EQ(cases[i].result,
1022 ElideRectangleString(UTF8ToUTF16(cases[i].input),
1023 cases[i].max_rows, cases[i].max_cols,
1024 false, &output));
1025 EXPECT_EQ(cases[i].output, UTF16ToUTF8(output));
1026 }
1027}
1028
olivierrobin697bbda2015-12-07 14:53:481029// TODO(crbug.com/338784): Enable this on android.
pkotwicz85fd1d92015-05-20 16:36:241030#if defined(OS_ANDROID)
1031#define MAYBE_ElideRectangleWide16 DISABLED_ElideRectangleWide16
1032#else
1033#define MAYBE_ElideRectangleWide16 ElideRectangleWide16
1034#endif
1035TEST(TextEliderTest, MAYBE_ElideRectangleWide16) {
[email protected]dbb97ba2013-09-09 22:15:251036 // Two greek words separated by space.
[email protected]2aadf212013-12-18 20:03:441037 const base::string16 str(WideToUTF16(
[email protected]dbb97ba2013-09-09 22:15:251038 L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9"
1039 L"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2"));
[email protected]2aadf212013-12-18 20:03:441040 const base::string16 out1(WideToUTF16(
[email protected]dbb97ba2013-09-09 22:15:251041 L"\x03a0\x03b1\x03b3\x03ba\n"
1042 L"\x03cc\x03c3\x03bc\x03b9\n"
1043 L"..."));
[email protected]2aadf212013-12-18 20:03:441044 const base::string16 out2(WideToUTF16(
[email protected]dbb97ba2013-09-09 22:15:251045 L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9\x03bf\x03c2\x0020\n"
1046 L"\x0399\x03c3\x03c4\x03cc\x03c2"));
[email protected]2aadf212013-12-18 20:03:441047 base::string16 output;
[email protected]dbb97ba2013-09-09 22:15:251048 EXPECT_TRUE(ElideRectangleString(str, 2, 4, true, &output));
1049 EXPECT_EQ(out1, output);
1050 EXPECT_FALSE(ElideRectangleString(str, 2, 12, true, &output));
1051 EXPECT_EQ(out2, output);
1052}
1053
olivierrobin697bbda2015-12-07 14:53:481054// TODO(crbug.com/338784): Enable this on android.
pkotwicz85fd1d92015-05-20 16:36:241055#if defined(OS_ANDROID)
1056#define MAYBE_ElideRectangleWide32 DISABLED_ElideRectangleWide32
1057#else
1058#define MAYBE_ElideRectangleWide32 ElideRectangleWide32
1059#endif
1060TEST(TextEliderTest, MAYBE_ElideRectangleWide32) {
[email protected]dbb97ba2013-09-09 22:15:251061 // Four U+1D49C MATHEMATICAL SCRIPT CAPITAL A followed by space "aaaaa".
[email protected]2aadf212013-12-18 20:03:441062 const base::string16 str(UTF8ToUTF16(
[email protected]dbb97ba2013-09-09 22:15:251063 "\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C"
1064 " aaaaa"));
[email protected]2aadf212013-12-18 20:03:441065 const base::string16 out(UTF8ToUTF16(
[email protected]dbb97ba2013-09-09 22:15:251066 "\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\n"
1067 "\xF0\x9D\x92\x9C \naaa\n..."));
[email protected]2aadf212013-12-18 20:03:441068 base::string16 output;
[email protected]dbb97ba2013-09-09 22:15:251069 EXPECT_TRUE(ElideRectangleString(str, 3, 3, true, &output));
1070 EXPECT_EQ(out, output);
1071}
1072
olivierrobin697bbda2015-12-07 14:53:481073// TODO(crbug.com/338784): Enable this on android.
pkotwicz85fd1d92015-05-20 16:36:241074#if defined(OS_ANDROID)
1075#define MAYBE_TruncateString DISABLED_TruncateString
1076#else
1077#define MAYBE_TruncateString TruncateString
1078#endif
1079TEST(TextEliderTest, MAYBE_TruncateString) {
pkastingfc336922015-09-23 05:59:311080 base::string16 str = ASCIIToUTF16("fooooey bxxxar baz ");
[email protected]dbb97ba2013-09-09 22:15:251081
pkastingfc336922015-09-23 05:59:311082 // Test breaking at character 0.
1083 EXPECT_EQ(base::string16(), TruncateString(str, 0, WORD_BREAK));
1084 EXPECT_EQ(base::string16(), TruncateString(str, 0, CHARACTER_BREAK));
[email protected]41c0d7c12014-07-23 15:52:581085
pkastingfc336922015-09-23 05:59:311086 // Test breaking at character 1.
1087 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str, 1, WORD_BREAK)));
1088 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str, 1, CHARACTER_BREAK)));
[email protected]dbb97ba2013-09-09 22:15:251089
pkastingfc336922015-09-23 05:59:311090 // Test breaking in the middle of the first word.
1091 EXPECT_EQ(L"f\x2026", UTF16ToWide(TruncateString(str, 2, WORD_BREAK)));
1092 EXPECT_EQ(L"f\x2026", UTF16ToWide(TruncateString(str, 2, CHARACTER_BREAK)));
[email protected]dbb97ba2013-09-09 22:15:251093
pkastingfc336922015-09-23 05:59:311094 // Test breaking in between words.
1095 EXPECT_EQ(L"fooooey\x2026", UTF16ToWide(TruncateString(str, 9, WORD_BREAK)));
1096 EXPECT_EQ(L"fooooey\x2026",
1097 UTF16ToWide(TruncateString(str, 9, CHARACTER_BREAK)));
[email protected]41c0d7c12014-07-23 15:52:581098
pkastingfc336922015-09-23 05:59:311099 // Test breaking at the start of a later word.
1100 EXPECT_EQ(L"fooooey\x2026", UTF16ToWide(TruncateString(str, 11, WORD_BREAK)));
1101 EXPECT_EQ(L"fooooey\x2026",
1102 UTF16ToWide(TruncateString(str, 11, CHARACTER_BREAK)));
[email protected]41c0d7c12014-07-23 15:52:581103
pkastingfc336922015-09-23 05:59:311104 // Test breaking in the middle of a word.
1105 EXPECT_EQ(L"fooooey\x2026", UTF16ToWide(TruncateString(str, 12, WORD_BREAK)));
1106 EXPECT_EQ(L"fooooey\x2026",
1107 UTF16ToWide(TruncateString(str, 12, CHARACTER_BREAK)));
1108 EXPECT_EQ(L"fooooey\x2026", UTF16ToWide(TruncateString(str, 14, WORD_BREAK)));
1109 EXPECT_EQ(L"fooooey bx\x2026",
1110 UTF16ToWide(TruncateString(str, 14, CHARACTER_BREAK)));
[email protected]dbb97ba2013-09-09 22:15:251111
pkastingfc336922015-09-23 05:59:311112 // Test breaking in whitespace at the end of the string.
1113 EXPECT_EQ(L"fooooey bxxxar baz\x2026",
1114 UTF16ToWide(TruncateString(str, 22, WORD_BREAK)));
1115 EXPECT_EQ(L"fooooey bxxxar baz\x2026",
1116 UTF16ToWide(TruncateString(str, 22, CHARACTER_BREAK)));
[email protected]dbb97ba2013-09-09 22:15:251117
pkastingfc336922015-09-23 05:59:311118 // Test breaking at the end of the string.
1119 EXPECT_EQ(str, TruncateString(str, str.length(), WORD_BREAK));
1120 EXPECT_EQ(str, TruncateString(str, str.length(), CHARACTER_BREAK));
[email protected]dbb97ba2013-09-09 22:15:251121
pkastingfc336922015-09-23 05:59:311122 // Test breaking past the end of the string.
1123 EXPECT_EQ(str, TruncateString(str, str.length() + 10, WORD_BREAK));
1124 EXPECT_EQ(str, TruncateString(str, str.length() + 10, CHARACTER_BREAK));
[email protected]dbb97ba2013-09-09 22:15:251125
[email protected]41c0d7c12014-07-23 15:52:581126
pkastingfc336922015-09-23 05:59:311127 // Tests of strings with leading whitespace:
1128 base::string16 str2 = ASCIIToUTF16(" foo");
[email protected]41c0d7c12014-07-23 15:52:581129
pkastingfc336922015-09-23 05:59:311130 // Test breaking in leading whitespace.
1131 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 2, WORD_BREAK)));
1132 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 2, CHARACTER_BREAK)));
1133
1134 // Test breaking at the beginning of the first word, with leading whitespace.
1135 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 3, WORD_BREAK)));
1136 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 3, CHARACTER_BREAK)));
1137
1138 // Test breaking in the middle of the first word, with leading whitespace.
1139 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 4, WORD_BREAK)));
1140 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(str2, 4, CHARACTER_BREAK)));
1141 EXPECT_EQ(L" f\x2026", UTF16ToWide(TruncateString(str2, 5, WORD_BREAK)));
1142 EXPECT_EQ(L" f\x2026",
1143 UTF16ToWide(TruncateString(str2, 5, CHARACTER_BREAK)));
[email protected]dbb97ba2013-09-09 22:15:251144}
1145
1146} // namespace gfx