blob: 2a90e5d73cbcca9366f4fa2eeab1a8246fb97284 [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
9#include "base/files/file_path.h"
10#include "base/i18n/rtl.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/strings/string_util.h"
13#include "base/strings/utf_string_conversions.h"
14#include "testing/gtest/include/gtest/gtest.h"
15#include "ui/gfx/font.h"
16#include "url/gurl.h"
17
18namespace gfx {
19
20namespace {
21
22struct Testcase {
23 const std::string input;
24 const std::string output;
25};
26
27struct FileTestcase {
28 const base::FilePath::StringType input;
29 const std::string output;
30};
31
32struct UTF16Testcase {
33 const string16 input;
34 const string16 output;
35};
36
37struct TestData {
38 const std::string a;
39 const std::string b;
40 const int compare_result;
41};
42
43void RunUrlTest(Testcase* testcases, size_t num_testcases) {
44 static const gfx::Font font;
45 for (size_t i = 0; i < num_testcases; ++i) {
46 const GURL url(testcases[i].input);
47 // Should we test with non-empty language list?
48 // That's kinda redundant with net_util_unittests.
49 EXPECT_EQ(UTF8ToUTF16(testcases[i].output),
50 ElideUrl(url, font,
51 font.GetStringWidth(UTF8ToUTF16(testcases[i].output)),
52 std::string()));
53 }
54}
55
[email protected]c3087982013-09-20 17:46:5356gfx::Font GetTestingFont() {
57 gfx::Font font;
58#if defined(OS_MACOSX)
59 // Use a specific font for certain tests on Mac.
60 // 1) Different Mac machines might be configured with different default font.
61 // The number of extra pixels needed to make ElideEmail/TestFilenameEliding
62 // tests work might vary per the default font.
63 // 2) This specific font helps expose the line width exceeding problem as in
64 // ElideRectangleTextCheckLineWidth.
65 font = gfx::Font("LucidaGrande", 12);
66#endif
67 return font;
68}
69
[email protected]dbb97ba2013-09-09 22:15:2570} // namespace
71
72// TODO(ios): Complex eliding is off by one for some of those tests on iOS.
73// See crbug.com/154019
74#if defined(OS_IOS)
75#define MAYBE_ElideEmail DISABLED_ElideEmail
76#else
77#define MAYBE_ElideEmail ElideEmail
78#endif
79TEST(TextEliderTest, MAYBE_ElideEmail) {
80 const std::string kEllipsisStr(kEllipsis);
81
82 // Test emails and their expected elided forms (from which the available
83 // widths will be derived).
84 // For elided forms in which both the username and domain must be elided:
85 // the result (how many characters are left on each side) can be font
86 // dependent. To avoid this, the username is prefixed with the characters
87 // expected to remain in the domain.
88 Testcase testcases[] = {
89 {"[email protected]", "[email protected]"},
90 {"[email protected]", kEllipsisStr},
91 {"[email protected]", "ga@c" + kEllipsisStr + "a"},
92 {"[email protected]", "s" + kEllipsisStr + "@s" + kEllipsisStr},
93 {"[email protected]", "s" + kEllipsisStr + "@small.com"},
94 {"[email protected]", "[email protected]"},
95 {"[email protected]",
96 "short@long" + kEllipsisStr + ".com"},
97 {"[email protected]",
98 "la" + kEllipsisStr + "@l" + kEllipsisStr + "a"},
99 {"[email protected]", "long" + kEllipsisStr + "@gmail.com"},
100 {"[email protected]", "e" + kEllipsisStr + "@justfits.com"},
101 {"[email protected]",
102 "thatom" + kEllipsisStr + "@tha" + kEllipsisStr + "om"},
103 {"[email protected]",
104 "namefits@butthedo" + kEllipsisStr + "snt.com"},
105 {"[email protected]", kEllipsisStr},
106 {"nospaceforusername@l", kEllipsisStr},
107 {"[email protected]", "l" + kEllipsisStr + "@l" + kEllipsisStr},
108 {"[email protected]", "l@lllll" + kEllipsisStr + ".com"},
109 {"messed\"up@whyanat\"[email protected]",
110 "messed\"up@whyanat\"[email protected]"},
111 {"messed\"up@whyanat\"[email protected]",
112 "messed\"up@why" + kEllipsisStr + "@notgoogley.com"},
113 {"noca_messed\"up@whyanat\"[email protected]",
114 "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"},
115 {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com",
116 "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"},
117 // Special case: "m..." takes more than half of the available width; thus
118 // the domain must elide to "l..." and not "l...l" as it must allow enough
119 // space for the minimal username elision although its half of the
120 // available width would normally allow it to elide to "l...l".
121 {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr},
122 };
123
[email protected]c3087982013-09-20 17:46:53124 const gfx::Font font = GetTestingFont();
[email protected]dbb97ba2013-09-09 22:15:25125 for (size_t i = 0; i < arraysize(testcases); ++i) {
126 const string16 expected_output = UTF8ToUTF16(testcases[i].output);
[email protected]c3087982013-09-20 17:46:53127 int available_width = font.GetStringWidth(expected_output);
128#if defined(OS_MACOSX)
129 // Give two extra pixels to offset the ceiling width returned by
130 // GetStringWidth on Mac. This workaround will no longer be needed once
131 // the floating point width is adopted (http://crbug.com/288987).
132 // Note that we need one more pixel than TestFilenameEliding because
133 // multiple strings are elided and we need to offset more.
134 available_width += 2;
135#endif
[email protected]dbb97ba2013-09-09 22:15:25136 EXPECT_EQ(expected_output,
137 ElideEmail(UTF8ToUTF16(testcases[i].input),
138 font,
[email protected]c3087982013-09-20 17:46:53139 available_width));
[email protected]dbb97ba2013-09-09 22:15:25140 }
141}
142
143TEST(TextEliderTest, ElideEmailMoreSpace) {
144 const int test_width_factors[] = {
145 100,
146 10000,
147 1000000,
148 };
149 const std::string test_emails[] = {
150 "a@c",
151 "[email protected]",
152 "[email protected]",
153 "[email protected]",
154 };
155
156 const gfx::Font font;
157 for (size_t i = 0; i < arraysize(test_width_factors); ++i) {
158 const int test_width = test_width_factors[i] *
159 font.GetAverageCharacterWidth();
160 for (size_t j = 0; j < arraysize(test_emails); ++j) {
161 // Extra space is available: the email should not be elided.
162 const string16 test_email = UTF8ToUTF16(test_emails[j]);
163 EXPECT_EQ(test_email, ElideEmail(test_email, font, test_width));
164 }
165 }
166}
167
168// Test eliding of commonplace URLs.
169TEST(TextEliderTest, TestGeneralEliding) {
170 const std::string kEllipsisStr(kEllipsis);
171 Testcase testcases[] = {
172 {"http://www.google.com/intl/en/ads/",
173 "www.google.com/intl/en/ads/"},
174 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
175 {"http://www.google.com/intl/en/ads/",
176 "google.com/intl/" + kEllipsisStr + "/ads/"},
177 {"http://www.google.com/intl/en/ads/",
178 "google.com/" + kEllipsisStr + "/ads/"},
179 {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr},
180 {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr},
181 {"https://subdomain.foo.com/bar/filename.html",
182 "subdomain.foo.com/bar/filename.html"},
183 {"https://subdomain.foo.com/bar/filename.html",
184 "subdomain.foo.com/" + kEllipsisStr + "/filename.html"},
185 {"http://subdomain.foo.com/bar/filename.html",
186 kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"},
187 {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired",
188 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr},
189 };
190
191 RunUrlTest(testcases, arraysize(testcases));
192}
193
194// When there is very little space available, the elision code will shorten
195// both path AND file name to an ellipsis - ".../...". To avoid this result,
196// there is a hack in place that simply treats them as one string in this
197// case.
198TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) {
199 const std::string kEllipsisStr(kEllipsis);
200
201 // Very little space, would cause double ellipsis.
202 gfx::Font font;
203 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html");
204 int available_width = font.GetStringWidth(
205 UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr));
206
207 // Create the expected string, after elision. Depending on font size, the
208 // directory might become /dir... or /di... or/d... - it never should be
209 // shorter than that. (If it is, the font considers d... to be longer
210 // than .../... - that should never happen).
211 ASSERT_GT(font.GetStringWidth(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr)),
212 font.GetStringWidth(UTF8ToUTF16("d" + kEllipsisStr)));
213 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc");
214 string16 expected = ElideUrl(long_url, font, available_width, std::string());
215 // Ensure that the expected result still contains part of the directory name.
216 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length());
217 EXPECT_EQ(expected,
218 ElideUrl(url, font, available_width, std::string()));
219
220 // More space available - elide directories, partially elide filename.
221 Testcase testcases[] = {
222 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html",
223 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr},
224 };
225 RunUrlTest(testcases, arraysize(testcases));
226}
227
228// Test eliding of empty strings, URLs with ports, passwords, queries, etc.
229TEST(TextEliderTest, TestMoreEliding) {
230 const std::string kEllipsisStr(kEllipsis);
231 Testcase testcases[] = {
232 {"http://www.google.com/foo?bar", "www.google.com/foo?bar"},
233 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo?" + kEllipsisStr},
234 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo" + kEllipsisStr},
235 {"http://xyz.google.com/foo?bar", "xyz.google.com/fo" + kEllipsisStr},
236 {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"},
237 {"", ""},
238 {"http://foo.bar..example.com...hello/test/filename.html",
239 "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"},
240 {"http://foo.bar../", "foo.bar.."},
241 {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"},
242 {"http://me:[email protected]:99/foo?bar#baz",
243 "secrethost.com:99/foo?bar#baz"},
244 {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"},
245 {"mailto:[email protected]", "mailto:[email protected]"},
246 {"javascript:click(0)", "javascript:click(0)"},
247 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
248 "chess.eecs.berkeley.edu:4430/login/arbitfilename"},
249 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
250 kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"},
251
252 // Unescaping.
253 {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
254 "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
255
256 // Invalid unescaping for path. The ref will always be valid UTF-8. We don't
257 // bother to do too many edge cases, since these are handled by the escaper
258 // unittest.
259 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
260 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
261 };
262
263 RunUrlTest(testcases, arraysize(testcases));
264}
265
266// Test eliding of file: URLs.
267TEST(TextEliderTest, TestFileURLEliding) {
268 const std::string kEllipsisStr(kEllipsis);
269 Testcase testcases[] = {
270 {"file:///C:/path1/path2/path3/filename",
271 "file:///C:/path1/path2/path3/filename"},
272 {"file:///C:/path1/path2/path3/filename",
273 "C:/path1/path2/path3/filename"},
274// GURL parses "file:///C:path" differently on windows than it does on posix.
275#if defined(OS_WIN)
276 {"file:///C:path1/path2/path3/filename",
277 "C:/path1/path2/" + kEllipsisStr + "/filename"},
278 {"file:///C:path1/path2/path3/filename",
279 "C:/path1/" + kEllipsisStr + "/filename"},
280 {"file:///C:path1/path2/path3/filename",
281 "C:/" + kEllipsisStr + "/filename"},
282#endif
283 {"file://filer/foo/bar/file", "filer/foo/bar/file"},
284 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"},
285 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"},
286 };
287
288 RunUrlTest(testcases, arraysize(testcases));
289}
290
291// TODO(ios): Complex eliding is off by one for some of those tests on iOS.
292// See crbug.com/154019
293#if defined(OS_IOS)
294#define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding
295#else
296#define MAYBE_TestFilenameEliding TestFilenameEliding
297#endif
298TEST(TextEliderTest, MAYBE_TestFilenameEliding) {
299 const std::string kEllipsisStr(kEllipsis);
300 const base::FilePath::StringType kPathSeparator =
301 base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]);
302
303 FileTestcase testcases[] = {
304 {FILE_PATH_LITERAL(""), ""},
305 {FILE_PATH_LITERAL("."), "."},
306 {FILE_PATH_LITERAL("filename.exe"), "filename.exe"},
307 {FILE_PATH_LITERAL(".longext"), ".longext"},
308 {FILE_PATH_LITERAL("pie"), "pie"},
309 {FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") +
310 kPathSeparator + FILE_PATH_LITERAL("filename.pie"),
311 "filename.pie"},
312 {FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") +
313 kPathSeparator + FILE_PATH_LITERAL("longfilename.pie"),
314 "long" + kEllipsisStr + ".pie"},
315 {FILE_PATH_LITERAL("http://path.com/filename.pie"), "filename.pie"},
316 {FILE_PATH_LITERAL("http://path.com/longfilename.pie"),
317 "long" + kEllipsisStr + ".pie"},
318 {FILE_PATH_LITERAL("piesmashingtacularpants"), "pie" + kEllipsisStr},
319 {FILE_PATH_LITERAL(".piesmashingtacularpants"), ".pie" + kEllipsisStr},
320 {FILE_PATH_LITERAL("cheese."), "cheese."},
321 {FILE_PATH_LITERAL("file name.longext"),
322 "file" + kEllipsisStr + ".longext"},
323 {FILE_PATH_LITERAL("fil ename.longext"),
324 "fil " + kEllipsisStr + ".longext"},
325 {FILE_PATH_LITERAL("filename.longext"),
326 "file" + kEllipsisStr + ".longext"},
327 {FILE_PATH_LITERAL("filename.middleext.longext"),
328 "filename.mid" + kEllipsisStr + ".longext"},
329 {FILE_PATH_LITERAL("filename.superduperextremelylongext"),
330 "filename.sup" + kEllipsisStr + "emelylongext"},
331 {FILE_PATH_LITERAL("filenamereallylongtext.superduperextremelylongext"),
332 "filenamereall" + kEllipsisStr + "emelylongext"},
333 {FILE_PATH_LITERAL("file.name.really.long.text.superduperextremelylongext"),
334 "file.name.re" + kEllipsisStr + "emelylongext"}
335 };
336
[email protected]c3087982013-09-20 17:46:53337 static const gfx::Font font = GetTestingFont();
[email protected]dbb97ba2013-09-09 22:15:25338 for (size_t i = 0; i < arraysize(testcases); ++i) {
339 base::FilePath filepath(testcases[i].input);
340 string16 expected = UTF8ToUTF16(testcases[i].output);
341 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected);
[email protected]c3087982013-09-20 17:46:53342 int available_width = font.GetStringWidth(UTF8ToUTF16(testcases[i].output));
343#if defined(OS_MACOSX)
344 // Give one extra pixel to offset the ceiling width returned by
345 // GetStringWidth on Mac. This workaround will no longer be needed once
346 // the floating point width is adopted (http://crbug.com/288987).
347 available_width += 1;
348#endif
349 EXPECT_EQ(expected, ElideFilename(filepath, font, available_width));
[email protected]dbb97ba2013-09-09 22:15:25350 }
351}
352
353TEST(TextEliderTest, ElideTextTruncate) {
354 const gfx::Font font;
355 const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test"));
356 struct TestData {
357 const char* input;
358 int width;
359 const char* output;
360 } cases[] = {
361 { "", 0, "" },
362 { "Test", 0, "" },
363 { "", kTestWidth, "" },
364 { "Tes", kTestWidth, "Tes" },
365 { "Test", kTestWidth, "Test" },
366 { "Tests", kTestWidth, "Test" },
367 };
368
369 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
370 string16 result = ElideText(UTF8ToUTF16(cases[i].input), font,
371 cases[i].width, TRUNCATE_AT_END);
372 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result));
373 }
374}
375
376TEST(TextEliderTest, ElideTextEllipsis) {
377 const gfx::Font font;
378 const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test"));
379 const char* kEllipsis = "\xE2\x80\xA6";
380 const int kEllipsisWidth = font.GetStringWidth(UTF8ToUTF16(kEllipsis));
381 struct TestData {
382 const char* input;
383 int width;
384 const char* output;
385 } cases[] = {
386 { "", 0, "" },
387 { "Test", 0, "" },
388 { "Test", kEllipsisWidth, kEllipsis },
389 { "", kTestWidth, "" },
390 { "Tes", kTestWidth, "Tes" },
391 { "Test", kTestWidth, "Test" },
392 };
393
394 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
395 string16 result = ElideText(UTF8ToUTF16(cases[i].input), font,
396 cases[i].width, ELIDE_AT_END);
397 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result));
398 }
399}
400
401// Checks that all occurrences of |first_char| are followed by |second_char| and
402// all occurrences of |second_char| are preceded by |first_char| in |text|.
403static void CheckSurrogatePairs(const string16& text,
404 char16 first_char,
405 char16 second_char) {
406 size_t index = text.find_first_of(first_char);
407 while (index != string16::npos) {
408 EXPECT_LT(index, text.length() - 1);
409 EXPECT_EQ(second_char, text[index + 1]);
410 index = text.find_first_of(first_char, index + 1);
411 }
412 index = text.find_first_of(second_char);
413 while (index != string16::npos) {
414 EXPECT_GT(index, 0U);
415 EXPECT_EQ(first_char, text[index - 1]);
416 index = text.find_first_of(second_char, index + 1);
417 }
418}
419
420TEST(TextEliderTest, ElideTextSurrogatePairs) {
421 const gfx::Font font;
422 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as
423 // two characters forming a surrogate pair 0x0001D11E.
424 const std::string kSurrogate = "\xF0\x9D\x84\x9E";
425 const string16 kTestString =
426 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd");
427 const int kTestStringWidth = font.GetStringWidth(kTestString);
428 const char16 kSurrogateFirstChar = kTestString[0];
429 const char16 kSurrogateSecondChar = kTestString[1];
430 string16 result;
431
432 // Elide |kTextString| to all possible widths and check that no instance of
433 // |kSurrogate| was split in two.
434 for (int width = 0; width <= kTestStringWidth; width++) {
435 result = ElideText(kTestString, font, width, TRUNCATE_AT_END);
436 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar);
437
438 result = ElideText(kTestString, font, width, ELIDE_AT_END);
439 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar);
440
441 result = ElideText(kTestString, font, width, ELIDE_IN_MIDDLE);
442 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar);
443 }
444}
445
446TEST(TextEliderTest, ElideTextLongStrings) {
447 const string16 kEllipsisStr = UTF8ToUTF16(kEllipsis);
448 string16 data_scheme(UTF8ToUTF16("data:text/plain,"));
449 size_t data_scheme_length = data_scheme.length();
450
451 string16 ten_a(10, 'a');
452 string16 hundred_a(100, 'a');
453 string16 thousand_a(1000, 'a');
454 string16 ten_thousand_a(10000, 'a');
455 string16 hundred_thousand_a(100000, 'a');
456 string16 million_a(1000000, 'a');
457
458 size_t number_of_as = 156;
459 string16 long_string_end(
460 data_scheme + string16(number_of_as, 'a') + kEllipsisStr);
461 UTF16Testcase testcases_end[] = {
462 {data_scheme + ten_a, data_scheme + ten_a},
463 {data_scheme + hundred_a, data_scheme + hundred_a},
464 {data_scheme + thousand_a, long_string_end},
465 {data_scheme + ten_thousand_a, long_string_end},
466 {data_scheme + hundred_thousand_a, long_string_end},
467 {data_scheme + million_a, long_string_end},
468 };
469
470 const gfx::Font font;
471 int ellipsis_width = font.GetStringWidth(kEllipsisStr);
472 for (size_t i = 0; i < arraysize(testcases_end); ++i) {
473 // Compare sizes rather than actual contents because if the test fails,
474 // output is rather long.
475 EXPECT_EQ(testcases_end[i].output.size(),
476 ElideText(testcases_end[i].input, font,
477 font.GetStringWidth(testcases_end[i].output),
478 ELIDE_AT_END).size());
479 EXPECT_EQ(kEllipsisStr,
480 ElideText(testcases_end[i].input, font, ellipsis_width,
481 ELIDE_AT_END));
482 }
483
484 size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2;
485 string16 long_string_middle(data_scheme +
486 string16(number_of_as - number_of_trailing_as, 'a') + kEllipsisStr +
487 string16(number_of_trailing_as, 'a'));
488 UTF16Testcase testcases_middle[] = {
489 {data_scheme + ten_a, data_scheme + ten_a},
490 {data_scheme + hundred_a, data_scheme + hundred_a},
491 {data_scheme + thousand_a, long_string_middle},
492 {data_scheme + ten_thousand_a, long_string_middle},
493 {data_scheme + hundred_thousand_a, long_string_middle},
494 {data_scheme + million_a, long_string_middle},
495 };
496
497 for (size_t i = 0; i < arraysize(testcases_middle); ++i) {
498 // Compare sizes rather than actual contents because if the test fails,
499 // output is rather long.
500 EXPECT_EQ(testcases_middle[i].output.size(),
501 ElideText(testcases_middle[i].input, font,
502 font.GetStringWidth(testcases_middle[i].output),
503 ELIDE_AT_END).size());
504 EXPECT_EQ(kEllipsisStr,
505 ElideText(testcases_middle[i].input, font, ellipsis_width,
506 ELIDE_AT_END));
507 }
508}
509
510// Verifies display_url is set correctly.
511TEST(TextEliderTest, SortedDisplayURL) {
512 SortedDisplayURL d_url(GURL("http://www.google.com"), std::string());
513 EXPECT_EQ("www.google.com", UTF16ToASCII(d_url.display_url()));
514}
515
516// Verifies DisplayURL::Compare works correctly.
517TEST(TextEliderTest, SortedDisplayURLCompare) {
518 UErrorCode create_status = U_ZERO_ERROR;
519 scoped_ptr<icu::Collator> collator(
520 icu::Collator::createInstance(create_status));
521 if (!U_SUCCESS(create_status))
522 return;
523
524 TestData tests[] = {
525 // IDN comparison. Hosts equal, so compares on path.
526 { "http://xn--1lq90i.cn/a", "http://xn--1lq90i.cn/b", -1},
527
528 // Because the host and after host match, this compares the full url.
529 { "http://www.x/b", "http://x/b", -1 },
530
531 // Because the host and after host match, this compares the full url.
532 { "http://www.a:1/b", "http://a:1/b", 1 },
533
534 // The hosts match, so these end up comparing on the after host portion.
535 { "http://www.x:0/b", "http://x:1/b", -1 },
536 { "http://www.x/a", "http://x/b", -1 },
537 { "http://x/b", "http://www.x/a", 1 },
538
539 // Trivial Equality.
540 { "http://a/", "http://a/", 0 },
541
542 // Compares just hosts.
543 { "http://www.a/", "http://b/", -1 },
544 };
545
546 for (size_t i = 0; i < arraysize(tests); ++i) {
547 SortedDisplayURL url1(GURL(tests[i].a), std::string());
548 SortedDisplayURL url2(GURL(tests[i].b), std::string());
549 EXPECT_EQ(tests[i].compare_result, url1.Compare(url2, collator.get()));
550 EXPECT_EQ(-tests[i].compare_result, url2.Compare(url1, collator.get()));
551 }
552}
553
554TEST(TextEliderTest, ElideString) {
555 struct TestData {
556 const char* input;
557 int max_len;
558 bool result;
559 const char* output;
560 } cases[] = {
561 { "Hello", 0, true, "" },
562 { "", 0, false, "" },
563 { "Hello, my name is Tom", 1, true, "H" },
564 { "Hello, my name is Tom", 2, true, "He" },
565 { "Hello, my name is Tom", 3, true, "H.m" },
566 { "Hello, my name is Tom", 4, true, "H..m" },
567 { "Hello, my name is Tom", 5, true, "H...m" },
568 { "Hello, my name is Tom", 6, true, "He...m" },
569 { "Hello, my name is Tom", 7, true, "He...om" },
570 { "Hello, my name is Tom", 10, true, "Hell...Tom" },
571 { "Hello, my name is Tom", 100, false, "Hello, my name is Tom" }
572 };
573 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
574 string16 output;
575 EXPECT_EQ(cases[i].result,
576 ElideString(UTF8ToUTF16(cases[i].input),
577 cases[i].max_len, &output));
578 EXPECT_EQ(cases[i].output, UTF16ToUTF8(output));
579 }
580}
581
582TEST(TextEliderTest, ElideRectangleText) {
583 const gfx::Font font;
584 const int line_height = font.GetHeight();
585 const int test_width = font.GetStringWidth(ASCIIToUTF16("Test"));
586
587 struct TestData {
588 const char* input;
589 int available_pixel_width;
590 int available_pixel_height;
591 bool truncated_y;
592 const char* output;
593 } cases[] = {
594 { "", 0, 0, false, NULL },
595 { "", 1, 1, false, NULL },
596 { "Test", test_width, 0, true, NULL },
597 { "Test", test_width, 1, false, "Test" },
598 { "Test", test_width, line_height, false, "Test" },
599 { "Test Test", test_width, line_height, true, "Test" },
600 { "Test Test", test_width, line_height + 1, false, "Test|Test" },
601 { "Test Test", test_width, line_height * 2, false, "Test|Test" },
602 { "Test Test", test_width, line_height * 3, false, "Test|Test" },
603 { "Test Test", test_width * 2, line_height * 2, false, "Test|Test" },
604 { "Test Test", test_width * 3, line_height, false, "Test Test" },
605 { "Test\nTest", test_width * 3, line_height * 2, false, "Test|Test" },
606 { "Te\nst Te", test_width, line_height * 3, false, "Te|st|Te" },
607 { "\nTest", test_width, line_height * 2, false, "|Test" },
608 { "\nTest", test_width, line_height, true, "" },
609 { "\n\nTest", test_width, line_height * 3, false, "||Test" },
610 { "\n\nTest", test_width, line_height * 2, true, "|" },
611 { "Test\n", 2 * test_width, line_height * 5, false, "Test|" },
612 { "Test\n\n", 2 * test_width, line_height * 5, false, "Test||" },
613 { "Test\n\n\n", 2 * test_width, line_height * 5, false, "Test|||" },
614 { "Test\nTest\n\n", 2 * test_width, line_height * 5, false, "Test|Test||" },
615 { "Test\n\nTest\n", 2 * test_width, line_height * 5, false, "Test||Test|" },
616 { "Test\n\n\nTest", 2 * test_width, line_height * 5, false, "Test|||Test" },
617 { "Te ", test_width, line_height, false, "Te" },
618 { "Te Te Test", test_width, 3 * line_height, false, "Te|Te|Test" },
619 };
620
621 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
622 std::vector<string16> lines;
623 EXPECT_EQ(cases[i].truncated_y ? INSUFFICIENT_SPACE_VERTICAL : 0,
624 ElideRectangleText(UTF8ToUTF16(cases[i].input),
625 font,
626 cases[i].available_pixel_width,
627 cases[i].available_pixel_height,
628 TRUNCATE_LONG_WORDS,
629 &lines));
630 if (cases[i].output) {
631 const std::string result = UTF16ToUTF8(JoinString(lines, '|'));
632 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!";
633 } else {
634 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!";
635 }
636 }
637}
638
639TEST(TextEliderTest, ElideRectangleTextPunctuation) {
640 const gfx::Font font;
641 const int line_height = font.GetHeight();
642 const int test_width = font.GetStringWidth(ASCIIToUTF16("Test"));
643 const int test_t_width = font.GetStringWidth(ASCIIToUTF16("Test T"));
644
645 struct TestData {
646 const char* input;
647 int available_pixel_width;
648 int available_pixel_height;
649 bool wrap_words;
650 bool truncated_x;
651 const char* output;
652 } cases[] = {
653 { "Test T.", test_t_width, line_height * 2, false, false, "Test|T." },
654 { "Test T ?", test_t_width, line_height * 2, false, false, "Test|T ?" },
655 { "Test. Test", test_width, line_height * 3, false, true, "Test|Test" },
656 { "Test. Test", test_width, line_height * 3, true, false, "Test|.|Test" },
657 };
658
659 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
660 std::vector<string16> lines;
661 const WordWrapBehavior wrap_behavior =
662 (cases[i].wrap_words ? WRAP_LONG_WORDS : TRUNCATE_LONG_WORDS);
663 EXPECT_EQ(cases[i].truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0,
664 ElideRectangleText(UTF8ToUTF16(cases[i].input),
665 font,
666 cases[i].available_pixel_width,
667 cases[i].available_pixel_height,
668 wrap_behavior,
669 &lines));
670 if (cases[i].output) {
671 const std::string result = UTF16ToUTF8(JoinString(lines, '|'));
672 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!";
673 } else {
674 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!";
675 }
676 }
677}
678
679TEST(TextEliderTest, ElideRectangleTextLongWords) {
680 const gfx::Font font;
681 const int kAvailableHeight = 1000;
682 const string16 kElidedTesting = UTF8ToUTF16(std::string("Tes") + kEllipsis);
683 const int elided_width = font.GetStringWidth(kElidedTesting);
684 const int test_width = font.GetStringWidth(ASCIIToUTF16("Test"));
685
686 struct TestData {
687 const char* input;
688 int available_pixel_width;
689 WordWrapBehavior wrap_behavior;
690 bool truncated_x;
691 const char* output;
692 } cases[] = {
693 { "Testing", test_width, IGNORE_LONG_WORDS, false, "Testing" },
694 { "X Testing", test_width, IGNORE_LONG_WORDS, false, "X|Testing" },
695 { "Test Testing", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" },
696 { "Test\nTesting", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" },
697 { "Test Tests ", test_width, IGNORE_LONG_WORDS, false, "Test|Tests" },
698 { "Test Tests T", test_width, IGNORE_LONG_WORDS, false, "Test|Tests|T" },
699
700 { "Testing", elided_width, ELIDE_LONG_WORDS, true, "Tes..." },
701 { "X Testing", elided_width, ELIDE_LONG_WORDS, true, "X|Tes..." },
702 { "Test Testing", elided_width, ELIDE_LONG_WORDS, true, "Test|Tes..." },
703 { "Test\nTesting", elided_width, ELIDE_LONG_WORDS, true, "Test|Tes..." },
704
705 { "Testing", test_width, TRUNCATE_LONG_WORDS, true, "Test" },
706 { "X Testing", test_width, TRUNCATE_LONG_WORDS, true, "X|Test" },
707 { "Test Testing", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test" },
708 { "Test\nTesting", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test" },
709 { "Test Tests ", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test" },
710 { "Test Tests T", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test|T" },
711
712 { "Testing", test_width, WRAP_LONG_WORDS, false, "Test|ing" },
713 { "X Testing", test_width, WRAP_LONG_WORDS, false, "X|Test|ing" },
714 { "Test Testing", test_width, WRAP_LONG_WORDS, false, "Test|Test|ing" },
715 { "Test\nTesting", test_width, WRAP_LONG_WORDS, false, "Test|Test|ing" },
716 { "Test Tests ", test_width, WRAP_LONG_WORDS, false, "Test|Test|s" },
717 { "Test Tests T", test_width, WRAP_LONG_WORDS, false, "Test|Test|s T" },
718 { "TestTestTest", test_width, WRAP_LONG_WORDS, false, "Test|Test|Test" },
719 { "TestTestTestT", test_width, WRAP_LONG_WORDS, false, "Test|Test|Test|T" },
720 };
721
722 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
723 std::vector<string16> lines;
724 EXPECT_EQ(cases[i].truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0,
725 ElideRectangleText(UTF8ToUTF16(cases[i].input),
726 font,
727 cases[i].available_pixel_width,
728 kAvailableHeight,
729 cases[i].wrap_behavior,
730 &lines));
731 std::string expected_output(cases[i].output);
732 ReplaceSubstringsAfterOffset(&expected_output, 0, "...", kEllipsis);
733 const std::string result = UTF16ToUTF8(JoinString(lines, '|'));
734 EXPECT_EQ(expected_output, result) << "Case " << i << " failed!";
735 }
736}
737
[email protected]c3087982013-09-20 17:46:53738// TODO(ios): Complex eliding is off by one for some of those tests on iOS.
739// See crbug.com/154019
740#if defined(OS_IOS)
741#define MAYBE_ElideRectangleTextCheckLineWidth \
742 DISABLED_ElideRectangleTextCheckLineWidth
743#else
744#define MAYBE_ElideRectangleTextCheckLineWidth ElideRectangleTextCheckLineWidth
745#endif
746
747// This test is to make sure that the width of each wrapped line does not
748// exceed the available width. On some platform like Mac, this test used to
749// fail because the truncated integer width is returned for the string
750// and the accumulation of the truncated values causes the elide function
751// to wrap incorrectly.
752TEST(TextEliderTest, MAYBE_ElideRectangleTextCheckLineWidth) {
753 gfx::Font font = GetTestingFont();
754 const int kAvailableWidth = 235;
755 const int kAvailableHeight = 1000;
756 const char text[] = "that Russian place we used to go to after fencing";
757 std::vector<string16> lines;
758 EXPECT_EQ(0, ElideRectangleText(UTF8ToUTF16(text),
759 font,
760 kAvailableWidth,
761 kAvailableHeight,
762 WRAP_LONG_WORDS,
763 &lines));
764 ASSERT_EQ(2u, lines.size());
765 EXPECT_LE(font.GetStringWidth(lines[0]), kAvailableWidth);
766 EXPECT_LE(font.GetStringWidth(lines[1]), kAvailableWidth);
767}
768
[email protected]dbb97ba2013-09-09 22:15:25769TEST(TextEliderTest, ElideRectangleString) {
770 struct TestData {
771 const char* input;
772 int max_rows;
773 int max_cols;
774 bool result;
775 const char* output;
776 } cases[] = {
777 { "", 0, 0, false, "" },
778 { "", 1, 1, false, "" },
779 { "Hi, my name is\nTom", 0, 0, true, "..." },
780 { "Hi, my name is\nTom", 1, 0, true, "\n..." },
781 { "Hi, my name is\nTom", 0, 1, true, "..." },
782 { "Hi, my name is\nTom", 1, 1, true, "H\n..." },
783 { "Hi, my name is\nTom", 2, 1, true, "H\ni\n..." },
784 { "Hi, my name is\nTom", 3, 1, true, "H\ni\n,\n..." },
785 { "Hi, my name is\nTom", 4, 1, true, "H\ni\n,\n \n..." },
786 { "Hi, my name is\nTom", 5, 1, true, "H\ni\n,\n \nm\n..." },
787 { "Hi, my name is\nTom", 0, 2, true, "..." },
788 { "Hi, my name is\nTom", 1, 2, true, "Hi\n..." },
789 { "Hi, my name is\nTom", 2, 2, true, "Hi\n, \n..." },
790 { "Hi, my name is\nTom", 3, 2, true, "Hi\n, \nmy\n..." },
791 { "Hi, my name is\nTom", 4, 2, true, "Hi\n, \nmy\n n\n..." },
792 { "Hi, my name is\nTom", 5, 2, true, "Hi\n, \nmy\n n\nam\n..." },
793 { "Hi, my name is\nTom", 0, 3, true, "..." },
794 { "Hi, my name is\nTom", 1, 3, true, "Hi,\n..." },
795 { "Hi, my name is\nTom", 2, 3, true, "Hi,\n my\n..." },
796 { "Hi, my name is\nTom", 3, 3, true, "Hi,\n my\n na\n..." },
797 { "Hi, my name is\nTom", 4, 3, true, "Hi,\n my\n na\nme \n..." },
798 { "Hi, my name is\nTom", 5, 3, true, "Hi,\n my\n na\nme \nis\n..." },
799 { "Hi, my name is\nTom", 1, 4, true, "Hi, \n..." },
800 { "Hi, my name is\nTom", 2, 4, true, "Hi, \nmy n\n..." },
801 { "Hi, my name is\nTom", 3, 4, true, "Hi, \nmy n\name \n..." },
802 { "Hi, my name is\nTom", 4, 4, true, "Hi, \nmy n\name \nis\n..." },
803 { "Hi, my name is\nTom", 5, 4, false, "Hi, \nmy n\name \nis\nTom" },
804 { "Hi, my name is\nTom", 1, 5, true, "Hi, \n..." },
805 { "Hi, my name is\nTom", 2, 5, true, "Hi, \nmy na\n..." },
806 { "Hi, my name is\nTom", 3, 5, true, "Hi, \nmy na\nme \n..." },
807 { "Hi, my name is\nTom", 4, 5, true, "Hi, \nmy na\nme \nis\n..." },
808 { "Hi, my name is\nTom", 5, 5, false, "Hi, \nmy na\nme \nis\nTom" },
809 { "Hi, my name is\nTom", 1, 6, true, "Hi, \n..." },
810 { "Hi, my name is\nTom", 2, 6, true, "Hi, \nmy \n..." },
811 { "Hi, my name is\nTom", 3, 6, true, "Hi, \nmy \nname \n..." },
812 { "Hi, my name is\nTom", 4, 6, true, "Hi, \nmy \nname \nis\n..." },
813 { "Hi, my name is\nTom", 5, 6, false, "Hi, \nmy \nname \nis\nTom" },
814 { "Hi, my name is\nTom", 1, 7, true, "Hi, \n..." },
815 { "Hi, my name is\nTom", 2, 7, true, "Hi, \nmy \n..." },
816 { "Hi, my name is\nTom", 3, 7, true, "Hi, \nmy \nname \n..." },
817 { "Hi, my name is\nTom", 4, 7, true, "Hi, \nmy \nname \nis\n..." },
818 { "Hi, my name is\nTom", 5, 7, false, "Hi, \nmy \nname \nis\nTom" },
819 { "Hi, my name is\nTom", 1, 8, true, "Hi, my \n..." },
820 { "Hi, my name is\nTom", 2, 8, true, "Hi, my \nname \n..." },
821 { "Hi, my name is\nTom", 3, 8, true, "Hi, my \nname \nis\n..." },
822 { "Hi, my name is\nTom", 4, 8, false, "Hi, my \nname \nis\nTom" },
823 { "Hi, my name is\nTom", 1, 9, true, "Hi, my \n..." },
824 { "Hi, my name is\nTom", 2, 9, true, "Hi, my \nname is\n..." },
825 { "Hi, my name is\nTom", 3, 9, false, "Hi, my \nname is\nTom" },
826 { "Hi, my name is\nTom", 1, 10, true, "Hi, my \n..." },
827 { "Hi, my name is\nTom", 2, 10, true, "Hi, my \nname is\n..." },
828 { "Hi, my name is\nTom", 3, 10, false, "Hi, my \nname is\nTom" },
829 { "Hi, my name is\nTom", 1, 11, true, "Hi, my \n..." },
830 { "Hi, my name is\nTom", 2, 11, true, "Hi, my \nname is\n..." },
831 { "Hi, my name is\nTom", 3, 11, false, "Hi, my \nname is\nTom" },
832 { "Hi, my name is\nTom", 1, 12, true, "Hi, my \n..." },
833 { "Hi, my name is\nTom", 2, 12, true, "Hi, my \nname is\n..." },
834 { "Hi, my name is\nTom", 3, 12, false, "Hi, my \nname is\nTom" },
835 { "Hi, my name is\nTom", 1, 13, true, "Hi, my name \n..." },
836 { "Hi, my name is\nTom", 2, 13, true, "Hi, my name \nis\n..." },
837 { "Hi, my name is\nTom", 3, 13, false, "Hi, my name \nis\nTom" },
838 { "Hi, my name is\nTom", 1, 20, true, "Hi, my name is\n..." },
839 { "Hi, my name is\nTom", 2, 20, false, "Hi, my name is\nTom" },
840 { "Hi, my name is Tom", 1, 40, false, "Hi, my name is Tom" },
841 };
842 string16 output;
843 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
844 EXPECT_EQ(cases[i].result,
845 ElideRectangleString(UTF8ToUTF16(cases[i].input),
846 cases[i].max_rows, cases[i].max_cols,
847 true, &output));
848 EXPECT_EQ(cases[i].output, UTF16ToUTF8(output));
849 }
850}
851
852TEST(TextEliderTest, ElideRectangleStringNotStrict) {
853 struct TestData {
854 const char* input;
855 int max_rows;
856 int max_cols;
857 bool result;
858 const char* output;
859 } cases[] = {
860 { "", 0, 0, false, "" },
861 { "", 1, 1, false, "" },
862 { "Hi, my name_is\nDick", 0, 0, true, "..." },
863 { "Hi, my name_is\nDick", 1, 0, true, "\n..." },
864 { "Hi, my name_is\nDick", 0, 1, true, "..." },
865 { "Hi, my name_is\nDick", 1, 1, true, "H\n..." },
866 { "Hi, my name_is\nDick", 2, 1, true, "H\ni\n..." },
867 { "Hi, my name_is\nDick", 3, 1, true, "H\ni\n,\n..." },
868 { "Hi, my name_is\nDick", 4, 1, true, "H\ni\n,\n \n..." },
869 { "Hi, my name_is\nDick", 5, 1, true, "H\ni\n,\n \nm\n..." },
870 { "Hi, my name_is\nDick", 0, 2, true, "..." },
871 { "Hi, my name_is\nDick", 1, 2, true, "Hi\n..." },
872 { "Hi, my name_is\nDick", 2, 2, true, "Hi\n, \n..." },
873 { "Hi, my name_is\nDick", 3, 2, true, "Hi\n, \nmy\n..." },
874 { "Hi, my name_is\nDick", 4, 2, true, "Hi\n, \nmy\n n\n..." },
875 { "Hi, my name_is\nDick", 5, 2, true, "Hi\n, \nmy\n n\nam\n..." },
876 { "Hi, my name_is\nDick", 0, 3, true, "..." },
877 { "Hi, my name_is\nDick", 1, 3, true, "Hi,\n..." },
878 { "Hi, my name_is\nDick", 2, 3, true, "Hi,\n my\n..." },
879 { "Hi, my name_is\nDick", 3, 3, true, "Hi,\n my\n na\n..." },
880 { "Hi, my name_is\nDick", 4, 3, true, "Hi,\n my\n na\nme_\n..." },
881 { "Hi, my name_is\nDick", 5, 3, true, "Hi,\n my\n na\nme_\nis\n..." },
882 { "Hi, my name_is\nDick", 1, 4, true, "Hi, ..." },
883 { "Hi, my name_is\nDick", 2, 4, true, "Hi, my n\n..." },
884 { "Hi, my name_is\nDick", 3, 4, true, "Hi, my n\name_\n..." },
885 { "Hi, my name_is\nDick", 4, 4, true, "Hi, my n\name_\nis\n..." },
886 { "Hi, my name_is\nDick", 5, 4, false, "Hi, my n\name_\nis\nDick" },
887 { "Hi, my name_is\nDick", 1, 5, true, "Hi, ..." },
888 { "Hi, my name_is\nDick", 2, 5, true, "Hi, my na\n..." },
889 { "Hi, my name_is\nDick", 3, 5, true, "Hi, my na\nme_is\n..." },
890 { "Hi, my name_is\nDick", 4, 5, true, "Hi, my na\nme_is\n\n..." },
891 { "Hi, my name_is\nDick", 5, 5, false, "Hi, my na\nme_is\n\nDick" },
892 { "Hi, my name_is\nDick", 1, 6, true, "Hi, ..." },
893 { "Hi, my name_is\nDick", 2, 6, true, "Hi, my nam\n..." },
894 { "Hi, my name_is\nDick", 3, 6, true, "Hi, my nam\ne_is\n..." },
895 { "Hi, my name_is\nDick", 4, 6, false, "Hi, my nam\ne_is\nDick" },
896 { "Hi, my name_is\nDick", 5, 6, false, "Hi, my nam\ne_is\nDick" },
897 { "Hi, my name_is\nDick", 1, 7, true, "Hi, ..." },
898 { "Hi, my name_is\nDick", 2, 7, true, "Hi, my name\n..." },
899 { "Hi, my name_is\nDick", 3, 7, true, "Hi, my name\n_is\n..." },
900 { "Hi, my name_is\nDick", 4, 7, false, "Hi, my name\n_is\nDick" },
901 { "Hi, my name_is\nDick", 5, 7, false, "Hi, my name\n_is\nDick" },
902 { "Hi, my name_is\nDick", 1, 8, true, "Hi, my n\n..." },
903 { "Hi, my name_is\nDick", 2, 8, true, "Hi, my n\name_is\n..." },
904 { "Hi, my name_is\nDick", 3, 8, false, "Hi, my n\name_is\nDick" },
905 { "Hi, my name_is\nDick", 1, 9, true, "Hi, my ..." },
906 { "Hi, my name_is\nDick", 2, 9, true, "Hi, my name_is\n..." },
907 { "Hi, my name_is\nDick", 3, 9, false, "Hi, my name_is\nDick" },
908 { "Hi, my name_is\nDick", 1, 10, true, "Hi, my ..." },
909 { "Hi, my name_is\nDick", 2, 10, true, "Hi, my name_is\n..." },
910 { "Hi, my name_is\nDick", 3, 10, false, "Hi, my name_is\nDick" },
911 { "Hi, my name_is\nDick", 1, 11, true, "Hi, my ..." },
912 { "Hi, my name_is\nDick", 2, 11, true, "Hi, my name_is\n..." },
913 { "Hi, my name_is\nDick", 3, 11, false, "Hi, my name_is\nDick" },
914 { "Hi, my name_is\nDick", 1, 12, true, "Hi, my ..." },
915 { "Hi, my name_is\nDick", 2, 12, true, "Hi, my name_is\n..." },
916 { "Hi, my name_is\nDick", 3, 12, false, "Hi, my name_is\nDick" },
917 { "Hi, my name_is\nDick", 1, 13, true, "Hi, my ..." },
918 { "Hi, my name_is\nDick", 2, 13, true, "Hi, my name_is\n..." },
919 { "Hi, my name_is\nDick", 3, 13, false, "Hi, my name_is\nDick" },
920 { "Hi, my name_is\nDick", 1, 20, true, "Hi, my name_is\n..." },
921 { "Hi, my name_is\nDick", 2, 20, false, "Hi, my name_is\nDick" },
922 { "Hi, my name_is Dick", 1, 40, false, "Hi, my name_is Dick" },
923 };
924 string16 output;
925 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
926 EXPECT_EQ(cases[i].result,
927 ElideRectangleString(UTF8ToUTF16(cases[i].input),
928 cases[i].max_rows, cases[i].max_cols,
929 false, &output));
930 EXPECT_EQ(cases[i].output, UTF16ToUTF8(output));
931 }
932}
933
934TEST(TextEliderTest, ElideRectangleWide16) {
935 // Two greek words separated by space.
936 const string16 str(WideToUTF16(
937 L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9"
938 L"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2"));
939 const string16 out1(WideToUTF16(
940 L"\x03a0\x03b1\x03b3\x03ba\n"
941 L"\x03cc\x03c3\x03bc\x03b9\n"
942 L"..."));
943 const string16 out2(WideToUTF16(
944 L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9\x03bf\x03c2\x0020\n"
945 L"\x0399\x03c3\x03c4\x03cc\x03c2"));
946 string16 output;
947 EXPECT_TRUE(ElideRectangleString(str, 2, 4, true, &output));
948 EXPECT_EQ(out1, output);
949 EXPECT_FALSE(ElideRectangleString(str, 2, 12, true, &output));
950 EXPECT_EQ(out2, output);
951}
952
953TEST(TextEliderTest, ElideRectangleWide32) {
954 // Four U+1D49C MATHEMATICAL SCRIPT CAPITAL A followed by space "aaaaa".
955 const string16 str(UTF8ToUTF16(
956 "\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C"
957 " aaaaa"));
958 const string16 out(UTF8ToUTF16(
959 "\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\n"
960 "\xF0\x9D\x92\x9C \naaa\n..."));
961 string16 output;
962 EXPECT_TRUE(ElideRectangleString(str, 3, 3, true, &output));
963 EXPECT_EQ(out, output);
964}
965
966TEST(TextEliderTest, TruncateString) {
967 string16 string = ASCIIToUTF16("foooooey bxxxar baz");
968
969 // Make sure it doesn't modify the string if length > string length.
970 EXPECT_EQ(string, TruncateString(string, 100));
971
972 // Test no characters.
973 EXPECT_EQ(L"", UTF16ToWide(TruncateString(string, 0)));
974
975 // Test 1 character.
976 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(string, 1)));
977
978 // Test adds ... at right spot when there is enough room to break at a
979 // word boundary.
980 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 14)));
981
982 // Test adds ... at right spot when there is not enough space in first word.
983 EXPECT_EQ(L"f\x2026", UTF16ToWide(TruncateString(string, 2)));
984
985 // Test adds ... at right spot when there is not enough room to break at a
986 // word boundary.
987 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11)));
988
989 // Test completely truncates string if break is on initial whitespace.
990 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2)));
991}
992
993} // namespace gfx